location_user.module 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @file
  4. * Associate locations with users.
  5. */
  6. /**
  7. * Implements hook_permission().
  8. */
  9. function location_user_permission() {
  10. return array(
  11. 'administer user locations' => array(
  12. 'title' => t('administer user locations'),
  13. ),
  14. 'view own user location' => array(
  15. 'title' => t('view own user location'),
  16. ),
  17. 'view all user locations' => array(
  18. 'title' => t('view all user locations'),
  19. ),
  20. 'set own user location' => array(
  21. 'title' => t('set own user location'),
  22. ),
  23. );
  24. }
  25. /**
  26. * Implements hook_form_FORM_ID_alter().
  27. * Alter the user_admin_settings form.
  28. */
  29. function location_user_form_user_admin_settings_alter(&$form, &$form_state, $form_id) {
  30. if (isset($form_state['values']['location_settings_user'])) {
  31. $settings = $form_state['values']['location_settings_user'];
  32. }
  33. else {
  34. $settings = variable_get('location_settings_user', array());
  35. }
  36. $form['location_settings_user'] = location_settings($settings);
  37. $form['location_settings_user']['#title'] = t('User locations');
  38. $form['location_settings_user']['form']['register'] = array(
  39. '#type' => 'checkbox',
  40. '#title' => t('Collect during registration'),
  41. '#default_value' => isset($settings['form']['register']) ? $settings['form']['register'] : FALSE,
  42. '#weight' => -5,
  43. );
  44. }
  45. /**
  46. * Implements hook_user_load().
  47. *
  48. * @todo
  49. * Make this load all locations at once instead of running separate queries
  50. * for each user to enhance performance.
  51. * location_load_locations() and location_load_location() will need
  52. * changing to make this happen.
  53. */
  54. function location_user_user_load($users) {
  55. foreach ($users as $uid => $user) {
  56. $users[$uid]->locations = location_load_locations($user->uid, 'uid');
  57. $users[$uid]->location = count($users[$uid]->locations) ? $users[$uid]->locations[0] : array();
  58. }
  59. }
  60. /**
  61. * Implements hook_user_insert().
  62. */
  63. function location_user_user_insert(&$edit, $account, $category) {
  64. if (!empty($edit['locations'])) {
  65. location_save_locations($edit['locations'], array('uid' => $account->uid));
  66. }
  67. unset($edit['locations']);
  68. }
  69. /**
  70. * Implements hook_user_update().
  71. */
  72. function location_user_user_update(&$edit, $account, $category) {
  73. if (!empty($edit['locations'])) {
  74. location_save_locations($edit['locations'], array('uid' => $account->uid));
  75. }
  76. unset($edit['locations']);
  77. }
  78. /**
  79. * Implements hook_user_delete().
  80. */
  81. function location_user_user_delete($account) {
  82. $locations = array();
  83. location_save_locations($locations, array('uid' => $account->uid));
  84. }
  85. /**
  86. * Implements hook_user_view().
  87. */
  88. function location_user_user_view($account, $view_mode, $langcode) {
  89. global $user;
  90. 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')) {
  91. if (variable_get('location_display_location', 1) && isset($account->locations) && count($account->locations)) {
  92. $settings = variable_get('location_settings_user', array());
  93. $account->content['locations'] = location_display($settings, $account->locations);
  94. }
  95. }
  96. }
  97. /**
  98. * Implements hook_form_FORM_ID_alter().
  99. * Alter the user profile form.
  100. */
  101. function location_user_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
  102. global $user;
  103. if ($form['#user_category'] == 'account') {
  104. $account = $form['#user'];
  105. if ((($user->uid == $account->uid) && user_access('set own user location')) || user_access('administer user locations')) {
  106. $settings = variable_get('location_settings_user', array());
  107. $form['locations'] = location_form($settings, $account->locations);
  108. }
  109. }
  110. }
  111. /**
  112. * Implements hook_form_FORM_ID_alter().
  113. * Alter the user registration form.
  114. */
  115. function location_user_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  116. $settings = variable_get('location_settings_user', array());
  117. if (isset($settings['form']['register']) && $settings['form']['register']) {
  118. $form['locations'] = location_form($settings, array());
  119. }
  120. }
  121. /**
  122. * Implements hook_locationapi().
  123. */
  124. function location_user_locationapi(&$obj, $op, $a3 = NULL, $a4 = NULL, $a5 = NULL) {
  125. switch ($op) {
  126. case 'instance_links':
  127. foreach ($obj as $k => $v) {
  128. if ($v['uid'] != 0) {
  129. $account = user_load($v['uid']);
  130. $obj[$k]['href'] = 'user/' . $v['uid'];
  131. $obj[$k]['title'] = $account->name;
  132. $obj[$k]['type'] = t('User location');
  133. }
  134. }
  135. }
  136. }