xmlsitemap_user.module 4.14 KB
Newer Older
1 2 3 4 5 6
<?php

/**
 * Implements hook_entity_info_alter().
 */
function xmlsitemap_user_entity_info_alter(&$entity_info) {
7
  $entity_info['user']['bundle label'] = t('User');
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  $entity_info['user']['xmlsitemap'] = array(
    'process callback' => 'xmlsitemap_user_xmlsitemap_process_user_links',
  );
}

/**
 * Implements hook_cron().
 *
 * Process old users not found in the {xmlsitemap} table.
 */
function xmlsitemap_user_cron() {
  xmlsitemap_user_xmlsitemap_index_links(xmlsitemap_var('batch_limit'));
}

/**
 * Implements hook_xmlsitemap_index_links().
 */
function xmlsitemap_user_xmlsitemap_index_links($limit) {
  $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();
  xmlsitemap_user_xmlsitemap_process_user_links($uids);
}

/**
 * Process user sitemap links.
 *
 * @param $uids
 *   An array of user IDs.
 */
function xmlsitemap_user_xmlsitemap_process_user_links(array $uids) {
  $accounts = user_load_multiple($uids);
  foreach ($accounts as $account) {
    $link = xmlsitemap_user_create_link($account);
40
    xmlsitemap_link_save($link);
41 42 43 44
  }
}

/**
45
 * Implements hook_user_presave().
46
 */
47
function xmlsitemap_user_user_presave(&$edit, $account, $category) {
48 49 50 51 52 53 54
  if (!empty($account->uid)) {
    $link = xmlsitemap_user_create_link($account);
    if (isset($edit['xmlsitemap'])) {
      $link = $edit['xmlsitemap'] + $link;
      unset($edit['xmlsitemap']);
    }
    xmlsitemap_link_save($link);
55
  }
56 57
}

58 59 60 61 62
/**
 * Implements hook_user_insert().
 */
function xmlsitemap_user_user_insert(&$edit, $account, $category) {
  $link = xmlsitemap_user_create_link($account);
63
  xmlsitemap_link_save($link);
64 65 66 67 68 69 70
}

/**
 * Implements hook_user_update().
 */
function xmlsitemap_user_user_update(&$edit, $account, $category) {
  $link = xmlsitemap_user_create_link($account);
71
  xmlsitemap_link_save($link);
72 73
}

74
/**
75
 * Implements hook_user_delete().
76
 */
77
function xmlsitemap_user_user_delete($account) {
78 79 80
  xmlsitemap_link_delete('user', $account->uid);
}

81 82 83 84 85 86 87
/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @see user_admin_settings()
 * @see xmlsitemap_add_link_bundle_settings()
 */
function xmlsitemap_user_form_user_profile_form_alter(&$form, $form_state) {
88
  // Add the link options.
89 90 91 92
  module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
  xmlsitemap_add_form_link_options($form, 'user', 'user', $form['#user']->uid);
}

93 94 95 96 97 98 99 100 101 102 103 104 105
/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @see user_admin_settings()
 * @see xmlsitemap_add_link_bundle_settings()
 */
function xmlsitemap_user_form_user_admin_settings_alter(&$form, $form_state) {
  module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
  xmlsitemap_add_link_bundle_settings($form, $form_state, 'user', 'user');
  $form['email_title'] += array('#weight' => 10);
  $form['email'] += array('#weight' => 10);
}

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
/**
 * Create a sitemap link from a user.
 *
 * The link will be saved as $account->xmlsitemap.
 *
 * @param $account
 *   A user object.
 */
function xmlsitemap_user_create_link(stdClass &$account) {
  if (!isset($account->xmlsitemap)) {
    $account->xmlsitemap = array();
    if ($account->uid && $link = xmlsitemap_link_load('user', $account->uid)) {
      $account->xmlsitemap = $link;
    }
  }

  $settings = xmlsitemap_link_bundle_load('user', 'user');
123
  $uri = entity_uri('user', $account);
124 125 126 127

  $account->xmlsitemap += array(
    'type' => 'user',
    'id' => $account->uid,
128
    'subtype' => 'user',
129
    'status' => $settings['status'],
130 131 132 133 134 135 136 137
    'status_default' => $settings['status'],
    'status_override' => 0,
    'priority' => $settings['priority'],
    'priority_default' => $settings['priority'],
    'priority_override' => 0,
  );

  // The following values must always be checked because they are volatile.
138
  $account->xmlsitemap['loc'] = $uri['path'];
139
  $account->xmlsitemap['access'] = $account->uid && $account->status && $account->login && $account->access;
140 141 142 143 144 145 146 147 148 149 150 151
  $account->xmlsitemap['language'] = !empty($account->language) ? $account->language : LANGUAGE_NONE;

  return $account->xmlsitemap;
}

/**
 * Implementation of hook_variables().
 */
function xmlsitemap_user_variables() {
  $defaults = array();
  return $defaults;
}