location_user.module

Tracking 6.x-3.x branch
  1. drupal
    1. 6 contributions/location/location_user.module

Associate locations with users.

Functions & methods

NameDescription
location_user_form_user_admin_settings_alterAlter the user_admin_settings form.
location_user_locationapiImplementation of hook_locationapi().
location_user_perm
location_user_userImplementation of hook_user().

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Associate locations with users.
  5. */
  6. function location_user_perm() {
  7. return array(
  8. 'administer user locations',
  9. 'set own user location',
  10. 'view own user location',
  11. 'view all user locations',
  12. );
  13. }
  14. /**
  15. * Alter the user_admin_settings form.
  16. */
  17. function location_user_form_user_admin_settings_alter(&$form, &$form_state) {
  18. if (isset($form_state['values']['location_settings_user'])) {
  19. $settings = $form_state['values']['location_settings_user'];
  20. }
  21. else {
  22. $settings = variable_get('location_settings_user', array());
  23. }
  24. $form['location_settings_user'] = location_settings($settings);
  25. $form['location_settings_user']['#title'] = t('User locations');
  26. $form['location_settings_user']['form']['register'] = array(
  27. '#type' => 'checkbox',
  28. '#title' => t('Collect during registration'),
  29. '#default_value' => isset($settings['form']['register']) ? $settings['form']['register'] : FALSE,
  30. '#weight' => -5,
  31. );
  32. }
  33. /**
  34. * Implementation of hook_user().
  35. */
  36. function location_user_user($op, &$edit, &$account, $category = NULL) {
  37. global $user;
  38. switch ($op) {
  39. case 'load':
  40. $account->locations = location_load_locations($account->uid, 'uid');
  41. $account->location = count($account->locations) ? $account->locations[0] : array();
  42. break;
  43. case 'insert':
  44. case 'update':
  45. if (!empty($edit['locations'])) {
  46. location_save_locations($edit['locations'], array('uid' => $account->uid));
  47. }
  48. unset($edit['locations']);
  49. break;
  50. case 'delete':
  51. $locations = array();
  52. location_save_locations($locations, array('uid' => $account->uid));
  53. break;
  54. case 'form':
  55. if ($category == 'account') {
  56. if ((($user->uid == $account->uid) && user_access('set own user location')) || user_access('administer user locations')) {
  57. $settings = variable_get('location_settings_user', array());
  58. $form['locations'] = location_form($settings, $account->locations);
  59. return $form;
  60. }
  61. }
  62. break;
  63. case 'register':
  64. $settings = variable_get('location_settings_user', array());
  65. if (isset($settings['form']['register']) && $settings['form']['register']) {
  66. $form['locations'] = location_form($settings, array());
  67. return $form;
  68. }
  69. break;
  70. case 'view':
  71. if ((($user->uid == $account->uid) && user_access('view own user location')) || user_access('administer users') || user_access('view all user locations') || user_access('administer user locations')) {
  72. if (variable_get('location_display_location', 1) && isset($account->locations) && count($account->locations)) {
  73. $settings = variable_get('location_settings_user', array());
  74. $account->content['locations'] = location_display($settings, $account->locations);
  75. }
  76. }
  77. break;
  78. }
  79. }
  80. /**
  81. * Implementation of hook_locationapi().
  82. */
  83. function location_user_locationapi(&$obj, $op, $a3 = NULL, $a4 = NULL, $a5 = NULL) {
  84. switch ($op) {
  85. case 'instance_links':
  86. foreach ($obj as $k => $v) {
  87. if ($v['uid'] != 0) {
  88. $account = user_load(array('uid' => $v['uid']));
  89. $obj[$k]['href'] = 'user/'. $v['uid'];
  90. $obj[$k]['title'] = $account->name;
  91. $obj[$k]['type'] = t('User location');
  92. }
  93. }
  94. }
  95. }