taxonomy.pages.inc

  1. 7.x drupal-7.x/modules/taxonomy/taxonomy.pages.inc
  2. 6.x drupal-6.x/modules/taxonomy/taxonomy.pages.inc

Page callbacks for the taxonomy module.

File

drupal-6.x/modules/taxonomy/taxonomy.pages.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Page callbacks for the taxonomy module.
  5. */
  6. /**
  7. * Menu callback; displays all nodes associated with a term.
  8. */
  9. function taxonomy_term_page($str_tids = '', $depth = 0, $op = 'page') {
  10. $terms = taxonomy_terms_parse_string($str_tids);
  11. if ($terms['operator'] != 'and' && $terms['operator'] != 'or') {
  12. drupal_not_found();
  13. }
  14. if ($terms['tids']) {
  15. $result = db_query(db_rewrite_sql('SELECT t.tid, t.name FROM {term_data} t WHERE t.tid IN ('. db_placeholders($terms['tids']) .')', 't', 'tid'), $terms['tids']);
  16. $tids = array(); // we rebuild the $tids-array so it only contains terms the user has access to.
  17. $names = array();
  18. while ($term = db_fetch_object($result)) {
  19. $tids[] = $term->tid;
  20. $names[] = $term->name;
  21. }
  22. if ($names) {
  23. $title = implode(', ', $names);
  24. drupal_set_title(check_plain($title));
  25. switch ($op) {
  26. case 'page':
  27. // Build breadcrumb based on first hierarchy of first term:
  28. $current->tid = $tids[0];
  29. $breadcrumb = array();
  30. while ($parents = taxonomy_get_parents($current->tid)) {
  31. $current = array_shift($parents);
  32. $breadcrumb[] = l($current->name, 'taxonomy/term/'. $current->tid);
  33. }
  34. $breadcrumb[] = l(t('Home'), NULL);
  35. $breadcrumb = array_reverse($breadcrumb);
  36. drupal_set_breadcrumb($breadcrumb);
  37. $output = theme('taxonomy_term_page', $tids, taxonomy_select_nodes($tids, $terms['operator'], $depth, TRUE));
  38. drupal_add_feed(url('taxonomy/term/'. $str_tids .'/'. $depth .'/feed'), 'RSS - '. $title);
  39. return $output;
  40. break;
  41. case 'feed':
  42. $channel['link'] = url('taxonomy/term/'. $str_tids .'/'. $depth, array('absolute' => TRUE));
  43. $channel['title'] = variable_get('site_name', 'Drupal') .' - '. $title;
  44. // Only display the description if we have a single term, to avoid clutter and confusion.
  45. if (count($tids) == 1) {
  46. $term = taxonomy_get_term($tids[0]);
  47. // HTML will be removed from feed description, so no need to filter here.
  48. $channel['description'] = $term->description;
  49. }
  50. $result = taxonomy_select_nodes($tids, $terms['operator'], $depth, FALSE);
  51. $items = array();
  52. while ($row = db_fetch_object($result)) {
  53. $items[] = $row->nid;
  54. }
  55. node_feed($items, $channel);
  56. break;
  57. default:
  58. drupal_not_found();
  59. }
  60. }
  61. else {
  62. drupal_not_found();
  63. }
  64. }
  65. }
  66. /**
  67. * Render a taxonomy term page HTML output.
  68. *
  69. * @param $tids
  70. * An array of term ids.
  71. * @param $result
  72. * A pager_query() result, such as that performed by taxonomy_select_nodes().
  73. *
  74. * @ingroup themeable
  75. */
  76. function theme_taxonomy_term_page($tids, $result) {
  77. drupal_add_css(drupal_get_path('module', 'taxonomy') .'/taxonomy.css');
  78. $output = '';
  79. // Only display the description if we have a single term, to avoid clutter and confusion.
  80. if (count($tids) == 1) {
  81. $term = taxonomy_get_term($tids[0]);
  82. $description = $term->description;
  83. // Check that a description is set.
  84. if (!empty($description)) {
  85. $output .= '<div class="taxonomy-term-description">';
  86. $output .= filter_xss_admin($description);
  87. $output .= '</div>';
  88. }
  89. }
  90. $output .= taxonomy_render_nodes($result);
  91. return $output;
  92. }
  93. /**
  94. * Helper function for autocompletion
  95. */
  96. function taxonomy_autocomplete($vid, $string = '') {
  97. // The user enters a comma-separated list of tags. We only autocomplete the last tag.
  98. $array = drupal_explode_tags($string);
  99. // Fetch last tag
  100. $last_string = trim(array_pop($array));
  101. $matches = array();
  102. if ($last_string != '') {
  103. $result = db_query_range(db_rewrite_sql("SELECT t.tid, t.name FROM {term_data} t WHERE t.vid = %d AND LOWER(t.name) LIKE LOWER('%%%s%%')", 't', 'tid'), $vid, $last_string, 0, 10);
  104. $prefix = count($array) ? implode(', ', $array) .', ' : '';
  105. while ($tag = db_fetch_object($result)) {
  106. $n = $tag->name;
  107. // Commas and quotes in terms are special cases, so encode 'em.
  108. if (strpos($tag->name, ',') !== FALSE || strpos($tag->name, '"') !== FALSE) {
  109. $n = '"'. str_replace('"', '""', $tag->name) .'"';
  110. }
  111. $matches[$prefix . $n] = check_plain($tag->name);
  112. }
  113. }
  114. drupal_json($matches);
  115. }