web/drupal/modules/xmlsitemap/xmlsitemap_taxonomy/xmlsitemap_taxonomy.module
branchdrupal
changeset 74 0ff3ba646492
equal deleted inserted replaced
73:fcf75e232c5b 74:0ff3ba646492
       
     1 <?php
       
     2 // $Id: xmlsitemap_taxonomy.module,v 1.1.2.19 2009/07/16 12:56:04 earnie Exp $
       
     3 
       
     4 /**
       
     5  * @file
       
     6  * Adds taxonomy terms to the sitemap.
       
     7  */
       
     8 
       
     9 /**
       
    10  * @addtogroup xmlsitemap
       
    11  * @{
       
    12  */
       
    13 
       
    14 /*****************************************************************************
       
    15  * Drupal hooks.
       
    16  ****************************************************************************/
       
    17 
       
    18 /**
       
    19  * Implementation of hook_cron().
       
    20  */
       
    21 function xmlsitemap_taxonomy_cron() {
       
    22   if (($limit = variable_get('xmlsitemap_cron_limit', 100)) != -1) {
       
    23     $sql = "SELECT t.* FROM {term_data} t
       
    24       LEFT JOIN {xmlsitemap_taxonomy} xt ON xt.tid = t.tid
       
    25       WHERE xt.tid IS NULL";
       
    26     $result = db_query_range($sql, 0, $limit);
       
    27     while ($term = db_fetch_object($result)) {
       
    28       $row = new stdClass();
       
    29       $row->tid = $term->tid;
       
    30       $row->vid = $term->vid;
       
    31       $row->changed = REQUEST_TIME;
       
    32       drupal_write_record('xmlsitemap_taxonomy', $row);
       
    33     }
       
    34     if ($result) {
       
    35       xmlsitemap_flag_sitemap();
       
    36     }
       
    37   }
       
    38 }
       
    39 
       
    40 /**
       
    41  * Implementation of hook_form_FORM_ID_alter().
       
    42  */
       
    43 function xmlsitemap_taxonomy_form_taxonomy_form_term_alter(&$form, &$from_state) {
       
    44   $priority = db_result(db_query("SELECT priority_override
       
    45     FROM {xmlsitemap_taxonomy}
       
    46     WHERE tid = %d", $form['tid']['#value'])
       
    47   );
       
    48   if ($priority === FALSE) {
       
    49     $priority = -2.0;
       
    50   }
       
    51   $options = xmlsitemap_priority_options('both');
       
    52   $default = variable_get('xmlsitemap_taxonomy_vocabulary_priority_'. $form['vid']['#value'], '0.5');
       
    53   if (!isset($form['xmlsitemap'])) {
       
    54     $form['xmlsitemap'] = array(
       
    55       '#type' => 'fieldset',
       
    56       '#title' => t('XML sitemap'),
       
    57       '#collapsible' => TRUE,
       
    58     );
       
    59   }
       
    60   $form['xmlsitemap']['xmlsitemap_taxonomy_priority'] = array(
       
    61     '#type' => 'select',
       
    62     '#title' => t('Priority'),
       
    63     '#description' => t('The default priority is %priority.', array('%priority' => $options[$default])),
       
    64     '#default_value' => $priority,
       
    65     '#options' => $options,
       
    66   );
       
    67   $form['submit']['#weight'] = isset($form['submit']['#weight']) ? $form['submit']['#weight'] + 1 : 1;
       
    68   $form['delete']['#weight'] = isset($form['delete']['#weight']) ? $form['delete']['#weight'] + 1 : 1;
       
    69 }
       
    70 
       
    71 /**
       
    72  * Implementation of hook_form_FORM_ID_alter().
       
    73  */
       
    74 function xmlsitemap_taxonomy_form_taxonomy_form_vocabulary_alter(&$form, &$from_state) {
       
    75   $form['xmlsitemap'] = array(
       
    76     '#type' => 'fieldset',
       
    77     '#title' => t('XML sitemap'),
       
    78     '#collapsible' => TRUE,
       
    79   );
       
    80   $form['xmlsitemap']['xmlsitemap_taxonomy_vocabulary_priority'] = array(
       
    81     '#type' => 'select',
       
    82     '#title' => t('Priority'),
       
    83     '#description' => t('This will be the default priority of terms in this vocabulary.'),
       
    84     '#default_value' => variable_get('xmlsitemap_taxonomy_vocabulary_priority_'. $form['vid']['#value'], 0.5),
       
    85     '#options' => xmlsitemap_priority_options('exclude'),
       
    86   );
       
    87   $form['submit']['#weight'] = isset($form['submit']['#weight']) ? $form['submit']['#weight'] + 1 : 1;
       
    88   $form['delete']['#weight'] = isset($form['delete']['#weight']) ? $form['delete']['#weight'] + 1 : 1;
       
    89 }
       
    90 
       
    91 /**
       
    92  * Implementation of hook_nodeapi().
       
    93  */
       
    94 function xmlsitemap_taxonomy_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
       
    95   switch ($op) {
       
    96     case 'update':
       
    97       $terms = taxonomy_node_get_terms($node);
       
    98       foreach ($terms as $term) {
       
    99         $result = db_fetch_object(db_query("SELECT tid, changed, previously_changed, priority_override
       
   100           FROM {xmlsitemap_taxonomy}
       
   101           WHERE tid = %d", $term->tid));
       
   102         if ($result === FALSE) {
       
   103           $row = new stdClass();
       
   104           $row->tid = $term->tid;
       
   105           $row->vid = $term->vid;
       
   106           $row->changed = $node->changed;
       
   107           $row->previously_changed = $node->created;
       
   108         }
       
   109         else {
       
   110           $row = $result;
       
   111           if ($row->changed < $node->changed) {
       
   112             $row->previously_changed = $row->changed;
       
   113             $row->changed = $node->changed;
       
   114           }
       
   115         }
       
   116         drupal_write_record('xmlsitemap_taxonomy', $row, $result === FALSE ? NULL : 'tid');
       
   117         xmlsitemap_flag_sitemap();
       
   118       }
       
   119       break;
       
   120   }
       
   121 }
       
   122 
       
   123 /**
       
   124  * Implementation of hook_taxonomy().
       
   125  */
       
   126 
       
   127 function xmlsitemap_taxonomy_taxonomy($op, $type, $array = NULL) {
       
   128   if ($type == 'vocabulary') {
       
   129     switch ($op) {
       
   130       case 'delete':
       
   131         db_query("DELETE FROM {xmlsitemap_taxonomy} WHERE vid = %d", $array['vid']);
       
   132         variable_del('xmlsitemap_taxonomy_vocabulary_priority_'. $array['vid']);
       
   133         xmlsitemap_flag_sitemap();
       
   134         break;
       
   135       case 'insert':
       
   136       case 'update':
       
   137         if (isset($array['vid'])) {
       
   138           if (variable_get('xmlsitemap_taxonomy_vocabulary_priority_'. $array['vid'], 0.5) != $array['xmlsitemap_taxonomy_vocabulary_priority']) {
       
   139             variable_set('xmlsitemap_taxonomy_vocabulary_priority_'. $array['vid'], $array['xmlsitemap_taxonomy_vocabulary_priority']);
       
   140             xmlsitemap_flag_sitemap();
       
   141           }
       
   142         }
       
   143         break;
       
   144     }
       
   145   }
       
   146   else {
       
   147     switch ($op) {
       
   148       case 'delete':
       
   149         db_query("DELETE FROM {xmlsitemap_taxonomy} WHERE tid = %d", $array['tid']);
       
   150         db_query("DELETE FROM {xmlsitemap} WHERE type = 'taxonomy' AND id = %d", $array['tid']);
       
   151         break;
       
   152       case 'insert':
       
   153         if (isset($array['tid']) && isset($array['vid'])) {
       
   154           $row = new stdClass();
       
   155           $row->tid = $array['tid'];
       
   156           $row->vid = $array['vid'];
       
   157           $row->changed = REQUEST_TIME;
       
   158           $row->priority_override = isset($array['xmlsitemap_taxonomy_priority']) ? $array['xmlsitemap_taxonomy_priority'] : -2.0;
       
   159           drupal_write_record('xmlsitemap_taxonomy', $row);
       
   160         }
       
   161         break;
       
   162       case 'update':
       
   163         $result = db_fetch_object(db_query("SELECT tid, vid, changed, previously_changed, priority_override
       
   164           FROM {xmlsitemap_taxonomy}
       
   165           WHERE tid = %d", $array['tid'])
       
   166         );
       
   167         if ($result === FALSE) {
       
   168           $row = new stdClass();
       
   169           $row->tid = $array['tid'];
       
   170           $row->vid = $array['vid'];
       
   171           $row->changed = REQUEST_TIME;
       
   172           $row->priority_override = isset($array['xmlsitemap_taxonomy_priority']) ? $array['xmlsitemap_taxonomy_priority'] : -2.0;
       
   173         }
       
   174         else {
       
   175           $row = $result;
       
   176           if (isset($array['xmlsitemap_taxonomy_priority'])) {
       
   177             $row->priority_override = $array['xmlsitemap_taxonomy_priority'];
       
   178           }
       
   179         }
       
   180         drupal_write_record('xmlsitemap_taxonomy', $row, $result === FALSE ? NULL : 'tid');
       
   181         break;
       
   182     }
       
   183     xmlsitemap_flag_sitemap();
       
   184   }
       
   185 }
       
   186 
       
   187 /**
       
   188  * Implementation of hook_xmlsitemap_description().
       
   189  */
       
   190 function xmlsitemap_taxonomy_xmlsitemap_description() {
       
   191   return '<dt>'. t('XML sitemap taxonomy') .'</dt>'.
       
   192     '<dd>'. t('The module adds <a href="@terms">taxonomy terms</a> (categories) to the sitemap. The term default priority may be changed while adding or editing vocabularies and the term default priority can be overridden while adding or editing terms.', array('@terms' => url('admin/content/taxonomy'))) .'</dd>';
       
   193 }
       
   194 
       
   195 /**
       
   196  * Implementation of hook_xmlsitemap_links().
       
   197  */
       
   198 function xmlsitemap_taxonomy_xmlsitemap_links() {
       
   199   $anon_account = drupal_anonymous_user();
       
   200   $user_access = user_access('access content', $anon_account);
       
   201 
       
   202   $result = db_query(
       
   203     db_rewrite_sql(
       
   204       "SELECT t.tid, t.vid, v.module, xt.changed, xt.previously_changed, xt.priority_override
       
   205         FROM {term_data} t
       
   206         LEFT JOIN {vocabulary} v ON t.vid = v.vid
       
   207         LEFT JOIN {xmlsitemap_taxonomy} xt ON t.tid = xt.tid",
       
   208       't', 'tid'
       
   209     )
       
   210   );
       
   211   $row = new stdClass();
       
   212   $row->module = 'xmlsitemap_taxonomy';
       
   213   $row->type = 'taxonomy';
       
   214   while ($term = db_fetch_object($result)) {
       
   215     $row->loc = taxonomy_term_path($term);
       
   216     $row->id = $term->tid;
       
   217     $row->changed = $term->changed;
       
   218     $row->changefreq = max(REQUEST_TIME - $term->changed, empty($term->previously_changed) ? 0 : $term->changed - $term->previously_changed);
       
   219     if ($term->priority_override != -2.0) {
       
   220       $priority = $term->priority_override;
       
   221     }
       
   222     else {
       
   223       $priority = variable_get('xmlsitemap_taxonomy_vocabulary_priority_'. $term->vid, 0.5);
       
   224     }
       
   225     $row->priority = $user_access ? $priority == -1.0 ? $priority : min(max(round($priority, 1), 0.0), 1.0) : -1;
       
   226     $old_row = db_fetch_object(db_query("SELECT lid, type, priority FROM {xmlsitemap} WHERE loc = '%s'", $row->loc));
       
   227     if ($old_row === FALSE) {
       
   228       drupal_write_record('xmlsitemap', $row);
       
   229     }
       
   230     elseif ($old_row->type = 'taxonomy' && $old_row->priority != $row->priority) {
       
   231       $row->lid = $old_row->lid;
       
   232       drupal_write_record('xmlsitemap', $row, 'lid');
       
   233     }
       
   234   }
       
   235 }
       
   236 
       
   237 /**
       
   238  * @} End of "addtogroup xmlsitemap".
       
   239  */