You are here

xmlsitemap_user.module in XML sitemap 5.2

Adds user profiles to the sitemap.

File

xmlsitemap_user/xmlsitemap_user.module
View source
<?php

/**
 * @file
 * Adds user profiles to the sitemap.
 */

/**
 * @addtogroup xmlsitemap
 * @{
 */

/*****************************************************************************
 * Drupal hooks.
 ****************************************************************************/

/**
 * Implementation of hook_form_alter().
 */
function xmlsitemap_user_form_alter($form_id, &$form) {
  switch ($form_id) {
    case 'user_admin_settings':
      $form['xmlsitemap'] = array(
        '#type' => 'fieldset',
        '#title' => t('sitemap'),
      );
      $default = variable_get('xmlsitemap_user_default_priority', 0.5);
      $form['xmlsitemap']['xmlsitemap_user_old_default_priority'] = array(
        '#type' => 'value',
        '#value' => $default,
      );
      $form['xmlsitemap']['xmlsitemap_user_default_priority'] = array(
        '#type' => 'select',
        '#title' => t('Default user priority'),
        '#default_value' => $default,
        '#options' => xmlsitemap_priority_options('exclude'),
        '#description' => t('Choose the default priority for users who have only the authenticated user role.'),
      );
      $form['buttons']['#weight'] = 1;
      $form['#submit']['_xmlsitemap_user_submit'] = array();
      break;
    case 'user_admin_role':
      $options = xmlsitemap_priority_options('both');
      $priority = db_result(db_query("SELECT priority FROM {xmlsitemap_user_role} WHERE rid = %d", $form['rid']['#value']));
      $priority = $priority === FALSE ? 'NULL' : $priority;
      $form['xmlsitemap_user_role_priority'] = array(
        '#type' => 'select',
        '#title' => t('Default sitemap priority'),
        '#default_value' => $priority,
        '#options' => $options,
        '#description' => t("The priority of all a user's roles will be added together to set the user's default priority. If all role priorities are left at default, the user's default priority will be %priority.", array(
          '%priority' => $options[variable_get('xmlsitemap_user_default_priority', '0.5')],
        )),
      );
      $form['submit']['#weight'] = 1;
      $form['delete']['#weight'] = 1;
      $form['#submit']['_xmlsitemap_user_submit'] = array();
      break;
  }
}

/**
 * Implementation of hook_user().
 */
function xmlsitemap_user_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'form':
      if ($category != 'account') {
        return array();
      }
    case 'register':
      $priority = db_result(db_query("SELECT priority_override FROM {xmlsitemap_user} WHERE uid = %d", $account->uid));
      if (isset($account->roles)) {
        $roles = array_keys($account->roles);
        $default = db_fetch_object(db_query("SELECT MIN(priority) AS min, SUM(priority) AS sum FROM {xmlsitemap_user_role} WHERE rid IN (" . xmlsitemap_placeholders($roles, 'int') . ")", $roles));
      }
      if (isset($default->min) && isset($default->sum)) {
        $default_priority = $default->min < 0 ? -1 : round(min($default->sum, 1), 1);
      }
      else {
        $default_priority = variable_get('xmlsitemap_user_default_priority', '0.5');
      }
      $disabled = !user_access('administer users');
      $form = array();
      if (user_access('override profile settings')) {
        $form['xmlsitemap_user'] = array(
          '#type' => 'fieldset',
          '#title' => t('sitemap settings'),
          '#collapsible' => TRUE,
          '#weight' => 7,
        );
        $options = xmlsitemap_priority_options('both');
        $form['xmlsitemap']['xmlsitemap_user_priority'] = array(
          '#type' => 'select',
          '#title' => t('Priority'),
          '#default_value' => $priority !== FALSE ? $priority : 'NULL',
          '#options' => $options,
          '#disabled' => $disabled,
          '#description' => t('The default priority is %priority.', array(
            '%priority' => $options[(string) $default_priority],
          )),
        );
        $form['xmlsitemap_user']['xmlsitemap_user_priority'] = array(
          '#type' => 'select',
          '#title' => t('sitemap priority'),
          '#default_value' => isset($priority) ? $priority : 'NULL',
          '#options' => $options,
          '#description' => t('The default priority is %priority.', array(
            '%priority' => $options[$default],
          )),
        );
      }
      else {
        $form['xmlsitemap_user_priority'] = array(
          '#type' => 'value',
          '#value' => isset($priority) ? $priority : 'NULL',
        );
      }
      return $form;
    case 'insert':
      if (isset($edit['xmlsitemap_user_priority'])) {
        $priority = $edit['xmlsitemap_user_priority'];
      }
      else {
        $priority = db_result(db_query("SELECT priority_override FROM {xmlsitemap_user} WHERE uid = %d", $account->uid));
      }
      db_query("INSERT INTO {xmlsitemap_user} (uid, last_changed, priority_override) VALUES (%d, %d, %s)", $account->uid, REQUEST_TIME, isset($priority) && $priority !== FALSE ? $priority : 'NULL');
      xmlsitemap_flag_sitemap();
      break;
    case 'update':
      if (isset($edit['xmlsitemap_user_priority'])) {
        $priority = $edit['xmlsitemap_user_priority'];
      }
      else {
        $priority = db_result(db_query("SELECT priority_override FROM {xmlsitemap_user} WHERE uid = %d", $account->uid));
      }
      db_query("UPDATE {xmlsitemap_user}\n        SET previously_changed = last_changed, last_changed = %d, priority_override = %s\n        WHERE uid = %d", REQUEST_TIME, $priority !== FALSE ? $priority : 'NULL', $account->uid);
      xmlsitemap_flag_sitemap();
      break;
    case 'delete':
      db_query("DELETE FROM {xmlsitemap_user} WHERE uid = %d", $account->uid);
      xmlsitemap_flag_sitemap();
      break;
  }
}

/**
 * Implementation of hook_xmlsitemap_description().
 */
function xmlsitemap_user_xmlsitemap_description() {
  return '<dt>' . t('XML sitemap user') . '</dt>' . '<dd>' . t('It adds user profiles to the sitemap. The <em>anonymous user</em> role must have permission to access user profiles on the <a href="@access">access control page</a>. You can change the default user priority on the <a href="@user_settings">user settings</a> page. The <a href="@user">user role priority</a> will override the default user priority. You can override both the default priority and the role priority when you add or edit a user.', array(
    '@access' => url('admin/user/access'),
    '@user_settings' => url('admin/user/settings'),
    '@user_role' => url('admin/user/roles'),
  )) . '</dd>';
}

/**
 * Implementation of hook_xmlsitemap_links().
 */
function xmlsitemap_user_xmlsitemap_links() {
  $anonymous = user_load(array(
    'uid' => 0,
  ));
  if (user_access('access user profiles', $anonymous)) {
    switch ($GLOBALS['db_type']) {
      case 'mysql':
      case 'mysqli':
        $coalesce = 'COALESCE';
        break;
      case 'pgsql':
        $coalesce = 'FIRST';
        break;
    }
    $uid = 0;
    $frontpage = explode('/', drupal_get_normal_path(variable_get('site_frontpage', 'node')));
    if (count($frontpage) == 2 && $frontpage[0] == 'user' && is_numeric($frontpage[1])) {
      $uid = $frontpage[1];
    }
    $result = db_query("SELECT u.uid, xu.last_changed, xu.previously_changed, xu.priority_override, SUM(xur.priority) as priority, {$coalesce}(ua.dst) AS alias\n      FROM {users} u\n      LEFT JOIN {users_roles} ur ON u.uid = ur.uid\n      LEFT JOIN {xmlsitemap_user_role} xur ON ur.rid = xur.rid\n      LEFT JOIN {xmlsitemap_user} xu ON u.uid = xu.uid\n      LEFT JOIN {url_alias} ua ON ua.src = CONCAT('user/', u.uid)\n      WHERE (xu.priority_override IS NULL OR xu.priority_override >= 0) AND u.uid <> %d AND u.uid > 0 AND u.status <> 0\n      GROUP BY u.uid, xu.last_changed, xu.previously_changed, xu.priority_override\n      HAVING COUNT(xu.priority_override) > 0 OR (SUM(xur.priority) IS NULL AND %f <> -1 OR MIN(xur.priority) <> -1)", $uid, variable_get('xmlsitemap_user_default_priority', 0.5));
    while ($user = db_fetch_object($result)) {
      $age = REQUEST_TIME - $user->last_changed;
      $interval = empty($user->previously_changed) ? 0 : $user->last_changed - $user->previously_changed;
      if (isset($user->priority_override)) {
        $priority = $user->priority_override;
      }
      else {
        $priority = min(isset($user->priority) ? $user->priority : variable_get('xmlsitemap_user_default_priority', 0.5), 1);
      }
      db_query("INSERT INTO {xmlsitemap} (loc, lastmod, changefreq, priority) VALUES ('%s', %d, %d, %f)", xmlsitemap_url("user/{$user->uid}", $user->alias, NULL, NULL, TRUE), $user->last_changed, max($age, $interval), $priority);
    }
  }
}

/*****************************************************************************
 * Menu callbacks / form builders, submit/validate functions.
 ****************************************************************************/

/**
 * Add submit actions to forms.
 * @return None
 */
function _xmlsitemap_user_submit($form_id, $form_values) {
  if ($form_id == 'user_admin_settings' && $form_values['xmlsitemap_user_default_priority'] != $form_values['xmlsitemap_user_old_default_priority']) {
    xmlsitemap_flag_sitemap();
  }
  elseif ($form_id == 'user_admin_role') {
    $priority = db_result(db_query("SELECT priority FROM {xmlsitemap_user_role} WHERE rid = %d", $form_values['rid']));
    if ($form_values['op'] == t('Delete role')) {
      db_query("DELETE FROM {xmlsitemap_user_role} WHERE rid = %d", $form_values['rid']);
      if ($priority !== FALSE) {
        xmlsitemap_flag_sitemap();
      }
    }
    elseif ($form_values['op'] == t('Save role')) {
      $query_args = array(
        $form_values['xmlsitemap_user_role_priority'],
        $form_values['rid'],
      );
      if ($priority === FALSE) {
        db_query("INSERT INTO {xmlsitemap_user_role} (priority, rid) VALUES (%s, %d)", $query_args);
      }
      elseif ($form_values['xmlsitemap_user_role_priority'] != $priority) {
        db_query("UPDATE {xmlsitemap_user_role} SET priority = %s WHERE rid = %d", $query_args);
      }
      xmlsitemap_flag_sitemap();
    }
  }
}

/**
 * @} End of "addtogroup xmlsitemap".
 */

Functions

Namesort descending Description
xmlsitemap_user_form_alter Implementation of hook_form_alter().
xmlsitemap_user_user Implementation of hook_user().
xmlsitemap_user_xmlsitemap_description Implementation of hook_xmlsitemap_description().
xmlsitemap_user_xmlsitemap_links Implementation of hook_xmlsitemap_links().
_xmlsitemap_user_submit Add submit actions to forms.