xmlsitemap_user.module 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @file
  4. * Default file for XML sitemap user.
  5. */
  6. /**
  7. * Implements hook_entity_info_alter().
  8. */
  9. function xmlsitemap_user_entity_info_alter(&$entity_info) {
  10. $entity_info['user']['bundle label'] = t('User');
  11. $entity_info['user']['xmlsitemap'] = array(
  12. 'process callback' => 'xmlsitemap_user_xmlsitemap_process_user_links',
  13. );
  14. }
  15. /**
  16. * Implements hook_cron().
  17. *
  18. * Process old users not found in the {xmlsitemap} table.
  19. */
  20. function xmlsitemap_user_cron() {
  21. xmlsitemap_user_xmlsitemap_index_links(xmlsitemap_var('batch_limit'));
  22. }
  23. /**
  24. * Implements hook_xmlsitemap_index_links().
  25. */
  26. function xmlsitemap_user_xmlsitemap_index_links($limit) {
  27. $uids = db_query_range("SELECT u.uid FROM {users} u LEFT JOIN {xmlsitemap} x ON x.type = 'user' AND u.uid = x.id WHERE x.id IS NULL AND u.uid > 0 ORDER BY u.uid DESC", 0, $limit)->fetchCol();
  28. xmlsitemap_user_xmlsitemap_process_user_links($uids);
  29. }
  30. /**
  31. * Process user sitemap links.
  32. *
  33. * @param array $uids
  34. * An array of user IDs.
  35. */
  36. function xmlsitemap_user_xmlsitemap_process_user_links(array $uids) {
  37. $accounts = user_load_multiple($uids);
  38. foreach ($accounts as $account) {
  39. $link = xmlsitemap_user_create_link($account);
  40. xmlsitemap_link_save($link, array($link['type'] => $account));
  41. }
  42. }
  43. /**
  44. * Implements hook_user_presave().
  45. */
  46. function xmlsitemap_user_user_presave(&$edit, $account, $category) {
  47. if (!empty($account->uid)) {
  48. $link = xmlsitemap_user_create_link($account);
  49. if (isset($edit['xmlsitemap'])) {
  50. $link = $edit['xmlsitemap'] + $link;
  51. unset($edit['xmlsitemap']);
  52. }
  53. xmlsitemap_link_save($link, array($link['type'] => $account));
  54. }
  55. }
  56. /**
  57. * Implements hook_user_insert().
  58. */
  59. function xmlsitemap_user_user_insert(&$edit, $account, $category) {
  60. $link = xmlsitemap_user_create_link($account);
  61. xmlsitemap_link_save($link, array($link['type'] => $account));
  62. }
  63. /**
  64. * Implements hook_user_update().
  65. */
  66. function xmlsitemap_user_user_update(&$edit, $account, $category) {
  67. $link = xmlsitemap_user_create_link($account);
  68. xmlsitemap_link_save($link, array($link['type'] => $account));
  69. }
  70. /**
  71. * Implements hook_user_delete().
  72. */
  73. function xmlsitemap_user_user_delete($account) {
  74. xmlsitemap_link_delete('user', $account->uid);
  75. }
  76. /**
  77. * Implements hook_field_extra_fields().
  78. */
  79. function xmlsitemap_user_field_extra_fields() {
  80. $extras['user']['user']['form']['xmlsitemap'] = array(
  81. 'label' => t('XML sitemap'),
  82. 'description' => t('XML sitemap module element'),
  83. 'weight' => 30,
  84. );
  85. return $extras;
  86. }
  87. /**
  88. * Implements hook_form_FORM_ID_alter().
  89. *
  90. * @see user_admin_settings()
  91. * @see xmlsitemap_add_link_bundle_settings()
  92. */
  93. function xmlsitemap_user_form_user_profile_form_alter(&$form, $form_state) {
  94. // Add the link options.
  95. module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
  96. xmlsitemap_add_form_link_options($form, 'user', 'user', $form['#user']->uid);
  97. }
  98. /**
  99. * Implements hook_form_FORM_ID_alter().
  100. *
  101. * @see user_admin_settings()
  102. * @see xmlsitemap_add_link_bundle_settings()
  103. */
  104. function xmlsitemap_user_form_user_admin_settings_alter(&$form, $form_state) {
  105. module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
  106. xmlsitemap_add_link_bundle_settings($form, $form_state, 'user', 'user');
  107. $form['email_title'] += array('#weight' => 10);
  108. $form['email'] += array('#weight' => 10);
  109. }
  110. /**
  111. * Create a sitemap link from a user.
  112. *
  113. * The link will be saved as $account->xmlsitemap.
  114. *
  115. * @param object $account
  116. * A user object.
  117. */
  118. function xmlsitemap_user_create_link(stdClass &$account) {
  119. if (!isset($account->xmlsitemap)) {
  120. $account->xmlsitemap = array();
  121. if ($account->uid && $link = xmlsitemap_link_load('user', $account->uid)) {
  122. $account->xmlsitemap = $link;
  123. }
  124. }
  125. $settings = xmlsitemap_link_bundle_load('user', 'user');
  126. $uri = entity_uri('user', $account);
  127. $account->xmlsitemap += array(
  128. 'type' => 'user',
  129. 'id' => $account->uid,
  130. 'subtype' => 'user',
  131. 'status' => $settings['status'],
  132. 'status_default' => $settings['status'],
  133. 'status_override' => 0,
  134. 'priority' => $settings['priority'],
  135. 'priority_default' => $settings['priority'],
  136. 'priority_override' => 0,
  137. );
  138. // The following values must always be checked because they are volatile.
  139. $account->xmlsitemap['loc'] = $uri['path'];
  140. $account->xmlsitemap['access'] = $account->uid && $account->status;
  141. $account->xmlsitemap['language'] = !empty($account->language) ? $account->language : LANGUAGE_NONE;
  142. return $account->xmlsitemap;
  143. }
  144. /**
  145. * Implements hook_variables().
  146. */
  147. function xmlsitemap_user_variables() {
  148. $defaults = array();
  149. return $defaults;
  150. }