/[drupal]/contributions/modules/taxonomy_breadcrumb/taxonomy_breadcrumb.module
ViewVC logotype

Diff of /contributions/modules/taxonomy_breadcrumb/taxonomy_breadcrumb.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.5.2.9, Thu Mar 15 23:37:31 2007 UTC revision 1.5.2.10, Thu Mar 12 19:16:56 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: taxonomy_breadcrumb.module,v 1.5.2.8 2007/02/25 01:33:10 craig Exp $.  // $Id: taxonomy_breadcrumb.module,v 1.5.2.9 2007/03/15 23:37:31 craig Exp $.
3    
4  /**  /**
5   * @file   * @file
6   * The taxonomy_breadcrumb module generates taxonomy based breadcrumbs on node   * The taxonomy_breadcrumb module generates taxonomy based breadcrumbs on node
7   * pages and taxonomy/term pages.  The breadcrumb trail takes on the form:   * pages and taxonomy/term pages.  The breadcrumb trail takes on the form:
8   *   [HOME] >> [VOCABULARY] >> TERM >> [TERM] ...   *   [HOME] >> [VOCABULARY] >> TERM >> [TERM] ...
9   *   *
10   *   - The HOME breadcrumb (if present) links to the homepage.  The text   *   - The HOME breadcrumb (if present) links to the homepage.  The text
11   *     displayed for HOME is administrator configurable.  If the HOME   *     displayed for HOME is administrator configurable.  If the HOME
12   *     breadcrumb is not defined by the administrator, it will not appear   *     breadcrumb is not defined by the administrator, it will not appear
13   *     in the breadcrumb trail.   *     in the breadcrumb trail.
14   *   - The VOCABULARY breadcrumb (if present) will link to an administrator   *   - The VOCABULARY breadcrumb (if present) will link to an administrator
15   *     defined page.  If the VOCABULARY does not have an administrator   *     defined page.  If the VOCABULARY does not have an administrator
16   *     defined page, it will not appear in the breadcrumb trail.   *     defined page, it will not appear in the breadcrumb trail.
17   *   - Each TERM breadcrumb will link to either   *   - Each TERM breadcrumb will link to either
18   *     (1) taxonomy/term/tid by default, or   *     (1) taxonomy/term/tid by default, or
19   *     (2) an administrator defined page if one is defined for the term.   *     (2) an administrator defined page if one is defined for the term.
20   *   - These administrator defined "breadcrumb links" for VOCABULARIES and TERMS   *   - These administrator defined "breadcrumb links" for VOCABULARIES and TERMS
21   *     are controlled from the add/edit vocabulary and add/edit term   *     are controlled from the add/edit vocabulary and add/edit term
# Line 35  Line 35 
35   *     taxonomy_get_parents_all.   *     taxonomy_get_parents_all.
36   */   */
37    
   
38  // default value for Advanced Settings, Node Types  // default value for Advanced Settings, Node Types
39  define('TAXONOMY_BREADCRUMB_NODE_TYPES_DEFAULT', 'book');  define('TAXONOMY_BREADCRUMB_NODE_TYPES_DEFAULT', 'book');
   
40    
41  /**  /**
42   * Return lightest term for given node ($nid).   * Return lightest term for given node ($nid).
43   * Similar to taxonomy_node_get_terms, but only return the lightest term in the   * Similar to taxonomy_node_get_terms, but only return the lightest term in the
44   * lightest vocab for the node.   * lightest vocab for the node.
45   */   */
46  function taxonomy_breadcrumb_node_get_lightest_term($nid) {  function taxonomy_breadcrumb_node_get_lightest_term($nid) {
47    // We only want the first row of the result--this is the lightest term of the    // We only want the first row of the result--this is the lightest term of the
48    // lightest vocab.  This query should be the same as the query found in    // lightest vocab.  This query should be the same as the query found in
49    // taxonomy_node_get_terms.    // taxonomy_node_get_terms.
50    $result = db_query(db_rewrite_sql('SELECT t.* FROM {term_node} r INNER JOIN {term_data} t ON r.tid = t.tid INNER JOIN {vocabulary} v ON t.vid = v.vid WHERE r.nid = %d ORDER BY v.weight, t.weight, t.name', 't', 'tid'), $nid);    $result = db_query(db_rewrite_sql('SELECT t.* FROM {term_node} r INNER JOIN {term_data} t ON r.tid = t.tid INNER JOIN {vocabulary} v ON t.vid = v.vid WHERE r.nid = %d ORDER BY v.weight, t.weight, t.name', 't', 'tid'), $nid);
51    $term = db_fetch_object($result);  // extract first row of query    $term = db_fetch_object($result);  // extract first row of query
52    return $term;    return $term;
53  }  }
54    
   
55  /**  /**
56   * Return the administrator defined vocabulary path for a given vocabulary   * Return the administrator defined vocabulary path for a given vocabulary
57   * ($vid).  If a path doesn't exist, NULL is returned.   * ($vid).  If a path doesn't exist, NULL is returned.
58   */   */
59  function taxonomy_breadcrumb_get_vocabulary_path($vid) {  function taxonomy_breadcrumb_get_vocabulary_path($vid) {
# Line 66  function taxonomy_breadcrumb_get_vocabul Line 63  function taxonomy_breadcrumb_get_vocabul
63      $path = $row['path'];      $path = $row['path'];
64    }    }
65    return $path;    return $path;
66  }  }
   
67    
68  /**  /**
69   * Return the administrator defined term path for a given term ($tid).   * Return the administrator defined term path for a given term ($tid).
# Line 79  function taxonomy_breadcrumb_get_term_pa Line 75  function taxonomy_breadcrumb_get_term_pa
75    if ($row = db_fetch_array($result)) {    if ($row = db_fetch_array($result)) {
76      $path = $row['path'];      $path = $row['path'];
77    }    }
78    return $path;    return $path;
79  }  }
   
80    
81  /**  /**
82   * If the current drupal path (q=) is /node/nid, generate the breadcrumb trail   * If the current drupal path (q=) is /node/nid, generate the breadcrumb trail
# Line 90  function taxonomy_breadcrumb_get_term_pa Line 85  function taxonomy_breadcrumb_get_term_pa
85  function taxonomy_breadcrumb_generate_breadcrumb($tid, $is_term_page = FALSE) {  function taxonomy_breadcrumb_generate_breadcrumb($tid, $is_term_page = FALSE) {
86    
87    $term = taxonomy_get_term($tid);    $term = taxonomy_get_term($tid);
88    
89    // HOME breadcrumb generation    // HOME breadcrumb generation
90    $home_text = variable_get('taxonomy_breadcrumb_home', '');    $home_text = variable_get('taxonomy_breadcrumb_home', '');
91    if ($home_text != '') {    if ($home_text != '') {
# Line 101  function taxonomy_breadcrumb_generate_br Line 96  function taxonomy_breadcrumb_generate_br
96    $vocabulary_path = taxonomy_breadcrumb_get_vocabulary_path($term->vid);    $vocabulary_path = taxonomy_breadcrumb_get_vocabulary_path($term->vid);
97    if ($vocabulary_path != NULL) {    if ($vocabulary_path != NULL) {
98      $vocabulary = taxonomy_get_vocabulary($term->vid);      $vocabulary = taxonomy_get_vocabulary($term->vid);
99      $breadcrumb[] = l($vocabulary->name, $vocabulary_path);      $breadcrumb[] = l($vocabulary->name, $vocabulary_path);
100    }    }
101    
102    // TERM breadcrumb generation    // TERM breadcrumb generation
# Line 122  function taxonomy_breadcrumb_generate_br Line 117  function taxonomy_breadcrumb_generate_br
117    
118    // Remove current TERM from end of breadcrumb trail    // Remove current TERM from end of breadcrumb trail
119    if (!variable_get('taxonomy_breadcrumb_show_current_term', TRUE) && !is_null($breadcrumb)) {    if (!variable_get('taxonomy_breadcrumb_show_current_term', TRUE) && !is_null($breadcrumb)) {
120        array_pop($breadcrumb);      array_pop($breadcrumb);
121    }    }
122    return $breadcrumb;    return $breadcrumb;
   
123  }  }
124    
   
125  /**  /**
126   * Implementation of hook_menu().   * Implementation of hook_menu().
127   */   */
# Line 145  function taxonomy_breadcrumb_menu($may_c Line 138  function taxonomy_breadcrumb_menu($may_c
138        'access' => user_access('administer site configuration'),        'access' => user_access('administer site configuration'),
139        'type' => MENU_NORMAL_ITEM, // optional        'type' => MENU_NORMAL_ITEM, // optional
140      );      );
141    
142      // Similiar to core menu item in taxonomy_menu, except callback is different      // Similiar to core menu item in taxonomy_menu, except callback is different
143      $items[] = array('path' => 'taxonomy/term',      $items[] = array('path' => 'taxonomy/term',
144        'title' => t('Taxonomy term'),        'title' => t('Taxonomy term'),
145        'callback' => 'taxonomy_breadcrumb_term_page',        'callback' => 'taxonomy_breadcrumb_term_page',
146        'access' => user_access('access content'),        'access' => user_access('access content'),
147        'type' => MENU_CALLBACK);        'type' => MENU_CALLBACK,
148        );
149    }    }
   
150    return $items;    return $items;
151  }  }
152    
# Line 173  function taxonomy_breadcrumb_admin_setti Line 165  function taxonomy_breadcrumb_admin_setti
165      '#title' => t('Home breadcrumb text'),      '#title' => t('Home breadcrumb text'),
166      '#default_value' => variable_get('taxonomy_breadcrumb_home', ''),      '#default_value' => variable_get('taxonomy_breadcrumb_home', ''),
167      '#description' => t('Text to display at top of breadcrumb trail.  Typically home or your site name.  Leave blank to have no home breadcrumb.'),      '#description' => t('Text to display at top of breadcrumb trail.  Typically home or your site name.  Leave blank to have no home breadcrumb.'),
168    );    );
169    
170    $form['settings']['taxonomy_breadcrumb_show_current_term'] = array(    $form['settings']['taxonomy_breadcrumb_show_current_term'] = array(
171      '#type' => 'checkbox',      '#type' => 'checkbox',
172      '#title' => t('Show current term in breadcrumb trail?'),      '#title' => t('Show current term in breadcrumb trail?'),
173      '#default_value' => variable_get('taxonomy_breadcrumb_show_current_term', TRUE),      '#default_value' => variable_get('taxonomy_breadcrumb_show_current_term', TRUE),
174      '#description' => t('When enabled, the lightest term associated with node is shown as the last breadcrumb in the breadcrumb trail.  When disabled, the only terms shown in the breadcrumb trail are parent terms (if any parents exist).  The recommended setting is enabled.'),      '#description' => t('When enabled, the lightest term associated with node is shown as the last breadcrumb in the breadcrumb trail.  When disabled, the only terms shown in the breadcrumb trail are parent terms (if any parents exist).  The recommended setting is enabled.'),
175    );    );
176    
177    $form['advanced'] = array(    $form['advanced'] = array(
178      '#type' => 'fieldset',      '#type' => 'fieldset',
# Line 197  function taxonomy_breadcrumb_admin_setti Line 189  function taxonomy_breadcrumb_admin_setti
189      '#options' => array(TRUE => t('Include'), FALSE => t('Exclude')),      '#options' => array(TRUE => t('Include'), FALSE => t('Exclude')),
190      '#weight' => 10,      '#weight' => 10,
191    );    );
192    
193    // Get all of the node types enabled.    $tb_types = (array) variable_get('taxonomy_breadcrumb_node_types', TAXONOMY_BREADCRUMB_NODE_TYPES_DEFAULT);
194    $node_bases = array();    $default = array();
195    $node_types = node_get_types();    foreach ($tb_types as $index => $value) {
196    foreach ($node_types as $node_type) {      if ($value) {
197      $node_bases[] = $node_type->type;        $default[] = $index;
198        }
199    }    }
200    
201    $form['advanced']['taxonomy_breadcrumb_node_types'] = array(    $form['advanced']['taxonomy_breadcrumb_node_types'] = array(
202      '#type' => 'textfield',      '#type'           => 'checkboxes',
203      '#title' => t('Node types to include or exclude'),      '#multiple'       => TRUE,
204      '#default_value' => variable_get('taxonomy_breadcrumb_node_types', TAXONOMY_BREADCRUMB_NODE_TYPES_DEFAULT),      '#title'          => t('Node types to include or exclude'),
205      '#description' => t('Enter a list of node types to include or exclude applying taxonomy based breadcrumbs to.  Separate multiple values with spaces. <p>Node types currently enabled:') . '<ul><li>' . implode($node_bases, '</li><li>') . '</li></ul></p>',      '#default_value'  => $default,
206      '#weight' => 20,      '#options'        => array_map('check_plain', node_get_types('names')),
207    );      '#description'    => t('A list of node types to include or exclude applying taxonomy based breadcrumbs to.'),
208        '#weight'         => 20,
209      );
210    
211    return system_settings_form($form);    return system_settings_form($form);
212  }  }
# Line 222  function taxonomy_breadcrumb_admin_setti Line 217  function taxonomy_breadcrumb_admin_setti
217  function taxonomy_breadcrumb_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {  function taxonomy_breadcrumb_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
218    // If we are on a page view (not just a teaser), set the breadcrumb    // If we are on a page view (not just a teaser), set the breadcrumb
219    // $a4 contains TRUE if we are on a page view    // $a4 contains TRUE if we are on a page view
220    if ($op == 'view' && $a4 && !drupal_is_front_page()) {    if ($op == 'view' && $a4 && !drupal_is_front_page() && arg(1) == $node->nid ) {
221    
222      // See if the node type of the current node is part of the node types listed on the advanced settings page.      // See if the node type of the current node is part of the node types listed on the advanced settings page.
223      $array_of_types = explode(' ', variable_get('taxonomy_breadcrumb_node_types', TAXONOMY_BREADCRUMB_NODE_TYPES_DEFAULT));      $array_of_types = array_filter((array)variable_get('taxonomy_breadcrumb_node_types', TAXONOMY_BREADCRUMB_NODE_TYPES_DEFAULT));
224      $in_list = in_array($node->type, $array_of_types);      $in_list = in_array($node->type, $array_of_types);
225    
226      // if the node type IS IN     the node types list and the list IS     inclusive OR      // if the node type IS IN     the node types list and the list IS     inclusive OR
227      // if the node type IS NOT IN the node types list and the list IS NOT inclusive (e.g. exclusive)      // if the node type IS NOT IN the node types list and the list IS NOT inclusive (e.g. exclusive)
228      // THEN modify the breadcrumb trail.      // THEN modify the breadcrumb trail.
229      if ($in_list == variable_get('taxonomy_breadcrumb_include_nodes', FALSE) ) {      if ($in_list == variable_get('taxonomy_breadcrumb_include_nodes', FALSE) ) {
230    
231        // Extract lightest term from lightest vocabulary assosciated with node.        // Extract lightest term from lightest vocabulary associated with node.
232        $term = taxonomy_breadcrumb_node_get_lightest_term($node->nid);        $term = taxonomy_breadcrumb_node_get_lightest_term($node->nid);
233        $breadcrumb = taxonomy_breadcrumb_generate_breadcrumb($term->tid);        $breadcrumb = taxonomy_breadcrumb_generate_breadcrumb($term->tid);
234        drupal_set_breadcrumb($breadcrumb);        drupal_set_breadcrumb($breadcrumb);
# Line 241  function taxonomy_breadcrumb_nodeapi(&$n Line 236  function taxonomy_breadcrumb_nodeapi(&$n
236    }    }
237  }  }
238    
   
239  /**  /**
240   * This function overrides the core taxonomy_term_page.  First, call the core   * This function overrides the core taxonomy_term_page.  First, call the core
241   * taxonomy_term_page.  Then, alter the breadcrumb trail.  This module's   * taxonomy_term_page.  Then, alter the breadcrumb trail.  This module's
# Line 259  function taxonomy_breadcrumb_term_page($ Line 253  function taxonomy_breadcrumb_term_page($
253    $breadcrumb = taxonomy_breadcrumb_generate_breadcrumb($terms['tids'][0], TRUE);    $breadcrumb = taxonomy_breadcrumb_generate_breadcrumb($terms['tids'][0], TRUE);
254    drupal_set_breadcrumb($breadcrumb);    drupal_set_breadcrumb($breadcrumb);
255    return $output;    return $output;
   
 }  
256    
257    }
258    
259  /**  /**
260   * Implementation of hook_help().   * Implementation of hook_help().
261   */   */
262  function taxonomy_breadcrumb_help($section) {  function taxonomy_breadcrumb_help($section) {
263    
264    switch ($section) {    switch ($section) {
265      case 'admin/help#taxonomy_breadcrumb':      case 'admin/help#taxonomy_breadcrumb':
266        $output .= t('<p>See %link.</p>', array('%link' => l('admin/settings/taxonomy-breadcrumb', 'admin/settings/taxonomy-breadcrumb')));        $output .= t('<p>See %link.</p>', array('%link' => l('admin/settings/taxonomy-breadcrumb', 'admin/settings/taxonomy-breadcrumb')));
# Line 293  function taxonomy_breadcrumb_help($secti Line 286  function taxonomy_breadcrumb_help($secti
286    return $output;    return $output;
287  }  }
288    
   
289  /**  /**
290   * Implementation of hook_form_alter().  This must be used over hook_taxonomy to   * Implementation of hook_form_alter().  This must be used over hook_taxonomy to
291   * add the Breadcrumb Path fields to the vocabulary and term forms.  The   * add the Breadcrumb Path fields to the vocabulary and term forms.  The
292   * hook_taxonomy function does not provide a way to obtain the vid or tid   * hook_taxonomy function does not provide a way to obtain the vid or tid
293   * of the vocabulary or term.   * of the vocabulary or term.
294   */   */
# Line 306  function taxonomy_breadcrumb_form_alter( Line 298  function taxonomy_breadcrumb_form_alter(
298      $form['taxonomy_breadcrumb_path'] = array(      $form['taxonomy_breadcrumb_path'] = array(
299        '#type' => 'textfield',        '#type' => 'textfield',
300        '#title' => t('Breadcrumb path (taxonomy_breadcrumb)'),        '#title' => t('Breadcrumb path (taxonomy_breadcrumb)'),
301        '#default_value' => taxonomy_breadcrumb_get_vocabulary_path($form['vid']['#value']),        '#default_value' => taxonomy_breadcrumb_get_vocabulary_path($form['vid']['#value']),
302        '#maxlength' => 128,        '#maxlength' => 128,
303        '#description' => t('Specify the path this vocabulary links to as a breadcrumb.  If blank, the breadcrumb will not appear.  Use a relative path and don\'t add a trailing slash.  For example: node/42 or my/path/alias.'),        '#description' => t('Specify the path this vocabulary links to as a breadcrumb.  If blank, the breadcrumb will not appear.  Use a relative path and don\'t add a trailing slash.  For example: node/42 or my/path/alias.'),
304        '#weight' => 0,        '#weight' => 0,
# Line 316  function taxonomy_breadcrumb_form_alter( Line 308  function taxonomy_breadcrumb_form_alter(
308      $form['taxonomy_breadcrumb_path'] = array(      $form['taxonomy_breadcrumb_path'] = array(
309        '#type' => 'textfield',        '#type' => 'textfield',
310        '#title' => t('Breadcrumb path (taxonomy_breadcrumb)'),        '#title' => t('Breadcrumb path (taxonomy_breadcrumb)'),
311        '#default_value' => taxonomy_breadcrumb_get_term_path($form['tid']['#value']),        '#default_value' => taxonomy_breadcrumb_get_term_path($form['tid']['#value']),
312        '#maxlength' => 128,        '#maxlength' => 128,
313        '#description' => t('Specify the path this term links to as a breadcrumb.  If blank, the breadcrumb links to the default taxonomy page.  Use a relative path and don\'t add a trailing slash.  For example: node/42 or my/path/alias.'),        '#description' => t('Specify the path this term links to as a breadcrumb.  If blank, the breadcrumb links to the default taxonomy page.  Use a relative path and don\'t add a trailing slash.  For example: node/42 or my/path/alias.'),
314        '#weight' => 0,        '#weight' => 0,
315      );      );
   
316    }    }
317  }  }
   
318    
319  /**  /**
320   * Implementation of hook_taxonomy().  This implementation checks to see if a   * Implementation of hook_taxonomy().  This implementation checks to see if a
# Line 337  function taxonomy_breadcrumb_taxonomy($o Line 327  function taxonomy_breadcrumb_taxonomy($o
327    // called by module_invoke_all('taxonomy', 'update', 'term', $edit);  in taxonomy.module    // called by module_invoke_all('taxonomy', 'update', 'term', $edit);  in taxonomy.module
328    if ( $op == 'update' && ($type == 'vocabulary' || $type == 'term') ) {    if ( $op == 'update' && ($type == 'vocabulary' || $type == 'term') ) {
329    
330      // Set variables to used in SQL query to reflect if vocabulary or term is      // Set variables to used in SQL query to reflect if vocabulary or term is
331      // being updated.      // being updated.
332      if ($type == 'vocabulary') {      if ($type == 'vocabulary') {
333        $table = '{taxonomy_breadcrumb_vocabulary}';        $table = '{taxonomy_breadcrumb_vocabulary}';
# Line 351  function taxonomy_breadcrumb_taxonomy($o Line 341  function taxonomy_breadcrumb_taxonomy($o
341      }      }
342      $key = $object[$key_type];      $key = $object[$key_type];
343      $new_path = $object['taxonomy_breadcrumb_path'];      $new_path = $object['taxonomy_breadcrumb_path'];
344    
345      // Delete record from taxonomy_breadcrumb_vocabulary or taxonomy_breadcrumb_term      // Delete record from taxonomy_breadcrumb_vocabulary or taxonomy_breadcrumb_term
346      if (strlen($new_path) == 0 && $old_path !== NULL) {      if (strlen($new_path) == 0 && $old_path !== NULL) {
347        db_query("DELETE FROM $table WHERE $key_type = %d", $key);        db_query("DELETE FROM $table WHERE $key_type = %d", $key);
348      }      }
349    
350      // Update existing record in taxonomy_breadcrumb_vocabulary or taxonomy_breadcrumb_term      // Update existing record in taxonomy_breadcrumb_vocabulary or taxonomy_breadcrumb_term
351      elseif (strlen($new_path) != 0 && $old_path != NULL) {      elseif (strlen($new_path) != 0 && $old_path != NULL) {
352        db_query("UPDATE $table SET path = '%s' WHERE $key_type = %d", $new_path, $key );        db_query("UPDATE $table SET path = '%s' WHERE $key_type = %d", $new_path, $key );
353      }      }
354    
355      // Create new record in taxonomy_breadcrumb_vocabulary or taxonomy_breadcrumb_term      // Create new record in taxonomy_breadcrumb_vocabulary or taxonomy_breadcrumb_term
356      elseif (strlen($new_path) != 0 && $old_path == NULL) {      elseif (strlen($new_path) != 0 && $old_path == NULL) {
357        db_query("INSERT INTO $table ($key_type, path) VALUES (%d, '%s')", $key, $new_path);        db_query("INSERT INTO $table ($key_type, path) VALUES (%d, '%s')", $key, $new_path);
358      }      }
   
359    }    }
360  }  }

Legend:
Removed from v.1.5.2.9  
changed lines
  Added in v.1.5.2.10

  ViewVC Help
Powered by ViewVC 1.1.2