xmlsitemap_taxonomy.module 11.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php
// $Id$

/**
 * Implements hook_cron().
 *
 * Process old taxonomy terms not found in the {xmlsitemap} table.
 */
function xmlsitemap_taxonomy_cron() {
  if ($vids = xmlsitemap_taxonomy_get_vids()) {
11 12
    $tids = db_query_range("SELECT t.tid FROM {term_data} t LEFT JOIN {xmlsitemap} x ON x.type = 'taxonomy' AND t.tid = x.id WHERE x.id IS NULL AND t.vid IN (" . db_placeholders($vids) . ")", $vids, 0, xmlsitemap_var('batch_limit'));
    foreach ($tids as $tid) {
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
      $term = xmlsitemap_taxonomy_taxonomy_term_load($tid);
      $link = xmlsitemap_taxonomy_create_link($term);
      xmlsitemap_save_link($link);
    }
  }
}

/**
 * Implements hook_xmlsitemap_links().
 */
function xmlsitemap_taxonomy_xmlsitemap_links($offset = 0, $limit = 0) {
  $links = array();

  if ($vids = xmlsitemap_taxonomy_get_vids()) {
    $sql = "SELECT t.tid FROM {term_data} t WHERE t.tid > %d AND t.vid IN (" . db_placeholders($vids) . ") ORDER BY t.tid";
    $args = array_merge(array($offset), $vids);
29
    $tids = ($limit ? db_query_range($sql, $args, 0, $limit) : db_query($sql, $args));
30

31
    foreach ($tids as $tid) {
32 33 34 35 36 37 38 39 40 41 42 43 44 45
      $term = xmlsitemap_taxonomy_taxonomy_term_load($tid);
      $links[] = xmlsitemap_taxonomy_create_link($term);
    }
  }

  return $links;
}

/**
 * Implements hook_xmlsitemap_links_batch_info().
 */
function xmlsitemap_taxonomy_xmlsitemap_links_batch_info() {
  $vids = xmlsitemap_taxonomy_get_vids();
  return array(
46
    'max' => $vids ? db_query("SELECT COUNT(t.tid) FROM {term_data} t WHERE t.vid IN (" . db_placeholders($vids) . ")", $vids)->fetchField() : 0,
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
  );
}

/**
 * Implements hook_xmlsitemap_link_info().
 */
function xmlsitemap_taxonomy_xmlsitemap_link_info() {
  return array(
    'taxononomy' => array(
      'purge' => TRUE,
      'table' => 'term_data',
      'id' => 'tid',
      'subtype' => 'vid',
      'subtypes' => xmlsitemap_taxonomy_get_vids(),
    ),
  );
}

/**
 * Load a taxonomy term and its associated sitemap link data.
 *
 * Use this instead of taxonomy_get_term().
 *
 * @todo Convert this to hook_taxonomy_term_load() in Drupal 7.
 */
function xmlsitemap_taxonomy_taxonomy_term_load($tid) {
  $term = taxonomy_get_term($tid);
  if ($data = xmlsitemap_load_link(array('type' => 'taxonomy', 'id' => $tid))) {
    $term->xmlsitemap = $data;
  }
  return $term;
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Show a summary of vocabularies on the XML sitemap settings page.
 */
function xmlsitemap_taxonomy_form_xmlsitemap_settings_form_alter(&$form, $form_state) {
  $type = array(
    'type' => 'taxonomy',
    'title' => t('Taxonomy'),
    'item_title' => t('Vocabulary'),
    'access' => user_access('administer taxonomy'),
  );

  $vocabularies = taxonomy_get_vocabularies();
  foreach ($vocabularies as $vid => $vocabulary) {
    $vocabularies[$vid] = array(
      'name' => $vocabulary->name,
      'link' => 'admin/content/taxonomy/edit/vocabulary/' . $vid,
      'status' => xmlsitemap_taxonomy_var('status_' . $vid),
      'priority' => xmlsitemap_taxonomy_var('priority_' . $vid),
    );
  }

  xmlsitemap_add_form_type_summary($form, $type, $vocabularies);
  $form['taxonomy']['#weight'] = 40;
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @see taxonomy_form_vocabulary()
 * @see xmlsitemap_add_form_type_options()
 */
function xmlsitemap_taxonomy_form_taxonomy_form_vocabulary_alter(&$form, $form_state) {
  $vid = isset($form['vid']['#value']) ? $form['vid']['#value'] : 0;

  module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
  $options = array(
    'status' => variable_get('xmlsitemap_taxonomy_status_' . $vid, 0),
    'priority' => variable_get('xmlsitemap_taxonomy_priority_' . $vid, 0.5),
  );
  xmlsitemap_add_form_type_options($form, 'taxonomy', $options);

  // @todo Enable these features:
  //$form['xmlsitemap']['xmlsitemap_taxonomy_calculate_priority'] = array(
  //  '#type' => 'checkbox',
  //  '#title' => t('Calculate priority based on term depth and weight.'),
  //  '#default_value' => xmlsitemap_taxonomy_var('calculate_priority_' . $vid),
  //);
  //$form['xmlsitemap']['xmlsitemap_taxonomy_include_empty_terms'] = array(
  //  '#type' => 'checkbox',
  //  '#title' => t('Include terms that do not have any associated content.'),
  //  '#default_value' => xmlsitemap_taxonomy_var('include_empty_terms_' . $vid),
  //);

  // The submit and delete buttons need to be weighted down.
  $form['submit'] += array('#weight' => 50);
  if (isset($form['delete'])) {
    $form['delete'] += array('#weight' => 51);
  }

  $form['#submit'][] = 'xmlsitemap_taxonomy_taxonomy_form_vocabulary_submit';
}

/**
 * Form submit handler; update settings when a taxonomy vocabulary is saved.
 */
function xmlsitemap_taxonomy_taxonomy_form_vocabulary_submit($form, $form_state) {
  $vid = $form_state['values']['vid'];
  $new_status = $form_state['values']['xmlsitemap_taxonomy_status'];
  $new_priority = $form_state['values']['xmlsitemap_taxonomy_priority'];

  if ($new_status != variable_get('xmlsitemap_taxonomy_status_' . $vid, 0)) {
    xmlsitemap_update_links(array('status' => $new_status), array('type' => 'taxonomy', 'subtype' => $vid, 'status_override' => 0));
  }
  if ($new_priority != variable_get('xmlsitemap_taxonomy_priority_' . $vid, 0.5)) {
    xmlsitemap_update_links(array('priority' => $new_priority), array('type' => 'taxonomy', 'subtype' => $vid, 'priority_override' => 0));
  }

  variable_set('xmlsitemap_taxonomy_status_' . $vid, $new_status);
  variable_set('xmlsitemap_taxonomy_priority_' . $vid, $new_priority);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function xmlsitemap_taxonomy_form_taxonomy_form_term_alter(&$form, $form_state) {
  // Because the same form is used for deletion in confirm_form, we must check
  // if the normal editing form elements are present. Hopefully this is fixed
  // in Drupal 7.
  if (isset($form['identification'])) {
    if ($form['#term']['tid']) {
      $term = xmlsitemap_taxonomy_taxonomy_term_load($form['#term']['tid']);
    }
    else {
      $term = (object) $form['#term'];
    }
    $term->vid = $form['vid']['#value'];
    $link = xmlsitemap_taxonomy_create_link($term);

    // Add the link options.
    module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
    xmlsitemap_add_form_link_options($form, $link);

    $form['xmlsitemap']['#access'] |= user_access('administer taxonomy');
    if (user_access('administer taxonomy')) {
      $form['xmlsitemap']['priority']['#description'] .= ' ' . t('The default priority for this vocabulary can be changed <a href="@link-type">here</a>.', array('@link-type' => url('admin/content/taxonomy/edit/vocabulary/' . $term->vid, array('query' => drupal_get_destination()))));
    }

    // The submit and delete buttons need to be weighted down.
    $form['submit'] += array('#weight' => 50);
    if (isset($form['delete'])) {
      $form['delete'] += array('#weight' => 51);
    }
  }
}

/**
 * Implements hook_taxonomy().
 */
function xmlsitemap_taxonomy_taxonomy($op, $type, $array = NULL) {
  if ($type == 'vocabulary') {
    $vid = $array['vid'];
    // Insert and update actions handled by xmlsitemap_taxonomy_taxonomy_form_vocabulary_submit().
    if ($op == 'delete') {
      xmlsitemap_delete_link(array('type' => 'taxonomy', 'subtype' => $vid));
      variable_del('xmlsitemap_taxonomy_status_' . $vid);
      variable_del('xmlsitemap_taxonomy_priority_' . $vid);
    }
  }
  if ($type == 'term') {
    $tid = $array['tid'];
    if ($op == 'insert' || $op == 'update') {
      $link = xmlsitemap_taxonomy_create_link((object) $array);
      xmlsitemap_save_link($link);
    }
    elseif ($op == 'delete') {
      xmlsitemap_delete_link(array('type' => 'taxonomy', 'id' => $tid));
    }
  }
}

/**
 * Create a sitemap link from a taxonomy term.
 *
 * @param $term
 *   A taxonomy term object.
 * @return
 *   An array representing a sitemap link.
 */
function xmlsitemap_taxonomy_create_link(stdClass $term) {
  if (!isset($term->xmlsitemap)) {
    $term->xmlsitemap = array();
  }

  $term->xmlsitemap += array(
    'id' => $term->tid,
    'type' => 'taxonomy',
    'subtype' => $term->vid,
    'loc' => taxonomy_term_path($term),
    'status' => variable_get('xmlsitemap_taxonomy_status_' . $term->vid, 0),
    'status_default' => variable_get('xmlsitemap_taxonomy_status_' . $term->vid, 0),
    'status_override' => 0,
    'priority' => variable_get('xmlsitemap_taxonomy_priority_' . $term->vid, 0.5),
    'priority_default' => variable_get('xmlsitemap_taxonomy_priority_' . $term->vid, 0.5),
    'priority_override' => 0,
  );

  // The following values must always be checked because they are volatile.
  // @todo How can/should we check taxonomy term access?
  $term->xmlsitemap['access'] = 1;
  $term->xmlsitemap['language'] = isset($term->language) ? $term->language : '';

  return $term->xmlsitemap;
}

/**
 * Calculate the priority of a taxonomy term based on depth and weight.
 */
function xmlsitemap_taxonomy_calculate_term_priority($term) {
  // Calculate priority.
  // Min weight = -128
  // Max weight = 127
  // Max depth = ?
  return NULL;
}

/**
 * Find the tree depth of a taxonomy term.
 *
 * @param $tid
 *   A term ID.
 * @return
 *   The tree depth of the term.
 */
function xmlsitemap_taxonomy_get_term_depth($tid) {
  static $depths = array();

  if (!isset($depths[$tid])) {
279
    if ($parent = db_query("SELECT parent FROM {term_hierarchy} WHERE tid = %d", $tid)->fetchField()) {
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
      // If the term has a parent, the term's depth is the parent's depth + 1.
      if (!isset($depths[$parent])) {
        $depths[$parent] = xmlsitemap_taxonomy_get_term_depth($parent);
      }
      $depths[$tid] = $depths[$parent] + 1;
    }
    else {
      // Term has no parents, so depth is 0.
      $depths[$tid] = 0;
    }
  }

  return $depths[$tid];
}

function xmlsitemap_taxonomy_get_node_count($term) {
  // @todo Use db_rewrite_sql() w/ switch user.
297
  return db_query_range("SELECT COUNT(tn.nid) FROM {term_node} tn LEFT JOIN {node n} USING (nid) WHERE tn.tid = %d AND n.status = 1", $term->tid, 0, 1)->fetchField();
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
}

/**
 * Fetch an array of vocabulary IDs to be included in the sitemap.
 */
function xmlsitemap_taxonomy_get_vids() {
  $vids = array_keys(taxonomy_get_vocabularies());
  foreach ($vids as $index => $vid) {
    if (!variable_get('xmlsitemap_taxonomy_status_' . $vid, 0)) {
      unset($vids[$index]);
    }
  }
  return $vids;
}

/**
 * Internal default variables for template_var().
 */
function xmlsitemap_taxonomy_variables() {
  $defaults = array(
    // Removed variables set to NULL.
    'xmlsitemap_taxonomy_include_empty_terms' => NULL,
    'xmlsitemap_taxonomy_calculate_priority' => NULL,
  );
  $vids = array_keys(taxonomy_get_vocabularies());
  foreach ($vids as $vid) {
    $defaults['xmlsitemap_taxonomy_status_' . $vid] = 0;
    $defaults['xmlsitemap_taxonomy_priority_' . $vid] = '0.5';
    $defaults['xmlsitemap_taxonomy_calculate_priority_' . $vid] = FALSE;
    $defaults['xmlsitemap_taxonomy_include_empty_terms_' . $vid] = FALSE;
    //$defaults += _xmlsitemap_taxonomy_variables_vid($vid);
  }
  return $defaults;
}

//function _xmlsitemap_taxonomy_variables_vid($vid) {
//  $defaults = array();
//  $defaults['xmlsitemap_taxonomy_status_' . $vid] = 0;
//  $defaults['xmlsitemap_taxonomy_priority_' . $vid] = 0.5;
//  $defaults['xmlsitemap_taxonomy_calculate_priority_' . $vid] = FALSE;
//  $defaults['xmlsitemap_taxonomy_include_empty_terms_' . $vid] = TRUE;
//  return $defaults;
//}

/**
 * Internal implementation of variable_get().
 */
function xmlsitemap_taxonomy_var($name, $default = NULL) {
  static $defaults = NULL;
  if (!isset($defaults)) {
    $defaults = xmlsitemap_taxonomy_variables();
  }

  $name = 'xmlsitemap_taxonomy_' . $name;

  // @todo Remove when stable.
  if (!isset($defaults[$name])) {
    trigger_error(t('Default variable for %variable not found.', array('%variable' => $name)));
  }

  return variable_get($name, isset($default) || !isset($defaults[$name]) ? $default : $defaults[$name]);
}