community_tags_taxonomy.module

Tracking 6.x-2.x branch
  1. drupal
    1. 6 contributions/community_tags/community_tags_taxonomy/community_tags_taxonomy.module

community_tags_taxonomy.module

Implements hooks to take over standard taxonomy pages.

Functions & methods

NameDescription
community_tags_taxonomy_form_taxonomy_form_vocabulary_alter
community_tags_taxonomy_menu_alter
community_tags_taxonomy_select_nodesA community tags version of taxonomy_select_nodes. Adds a tag count and default popularity ordering.
community_tags_taxonomy_vocabulary_form_submit
_community_tags_taxonomy_get_settings

File

View source
  1. <?php
  2. // $Id$
  3. /**
  4. * @file community_tags_taxonomy.module
  5. *
  6. * Implements hooks to take over standard taxonomy pages.
  7. */
  8. function community_tags_taxonomy_menu_alter(&$items) {
  9. $settings = _community_tags_taxonomy_get_settings();
  10. foreach($settings as $vocab_settings) {
  11. if($vocab_settings['override_term_page']) {
  12. $items['taxonomy/term/%']['page callback'] = 'community_tags_taxonomy_term_page';
  13. $items['taxonomy/term/%']['file'] = 'community_tags_taxonomy.pages.inc';
  14. $items['taxonomy/term/%']['module'] = 'community_tags_taxonomy';
  15. break;
  16. }
  17. }
  18. }
  19. function _community_tags_taxonomy_get_settings($vid = NULL) {
  20. $settings = variable_get('community_tags_taxonomy_settings', array());
  21. if($vid) {
  22. return !empty($settings[$vid]) ? $settings[$vid] : array('override_term_page' => FALSE);
  23. }
  24. else {
  25. return $settings;
  26. }
  27. }
  28. function community_tags_taxonomy_form_taxonomy_form_vocabulary_alter(&$form, $form_state) {
  29. if(isset($form['vid'])) {
  30. $settings = _community_tags_taxonomy_get_settings($form['vid']['#value']);
  31. }
  32. else {
  33. $settings = array('override_term_page' => FALSE);
  34. }
  35. $form['community_tags'] = array(
  36. '#type' => 'fieldset',
  37. '#title' => t('Community tags overrides'),
  38. '#tree' => TRUE,
  39. '#collapsible' => TRUE,
  40. );
  41. $form['submit']['#weight'] = 10;
  42. $form['delete']['#weight'] = 10;
  43. $form['community_tags']['override_term_page'] = array(
  44. '#type' => 'checkbox',
  45. '#title' => t('Override standard term pages with community tags pages'),
  46. '#default_value' => $settings['override_term_page'],
  47. '#description' => t('Useful if full community tag/node term synchronisation is not enabled and you wish to display all content on the term page that has been community tagged with that term. Node listings are ordered sticky first then by tag popularity.'),
  48. );
  49. $form['#submit'][] = 'community_tags_taxonomy_vocabulary_form_submit';
  50. }
  51. function community_tags_taxonomy_vocabulary_form_submit($form, &$form_state) {
  52. $settings = _community_tags_taxonomy_get_settings();
  53. $settings[$form_state['vid']] = $form_state['values']['community_tags'];
  54. variable_set('community_tags_taxonomy_settings', $settings);
  55. menu_rebuild();
  56. }
  57. /**
  58. * A community tags version of taxonomy_select_nodes. Adds a tag count and
  59. * default popularity ordering.
  60. *
  61. * Finds all nodes that match selected taxonomy conditions.
  62. *
  63. * @param $tids
  64. * An array of term IDs to match.
  65. * @param $operator
  66. * How to interpret multiple IDs in the array. Can be "or" or "and".
  67. * @param $depth
  68. * How many levels deep to traverse the taxonomy tree. Can be a nonnegative
  69. * integer or "all".
  70. * @param $pager
  71. * Whether the nodes are to be used with a pager (the case on most Drupal
  72. * pages) or not (in an XML feed, for example).
  73. * @param $order
  74. * The order clause for the query that retrieve the nodes.
  75. * @return
  76. * A resource identifier pointing to the query results.
  77. */
  78. function community_tags_taxonomy_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE, $order = 'n.sticky DESC, tag_count DESC, n.created DESC') {
  79. if (count($tids) > 0) {
  80. // For each term ID, generate an array of descendant term IDs to the right depth.
  81. $descendant_tids = array();
  82. if ($depth === 'all') {
  83. $depth = NULL;
  84. }
  85. foreach ($tids as $index => $tid) {
  86. $term = taxonomy_get_term($tid);
  87. $tree = taxonomy_get_tree($term->vid, $tid, -1, $depth);
  88. $descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree));
  89. }
  90. if ($operator == 'or') {
  91. $args = call_user_func_array('array_merge', $descendant_tids);
  92. $placeholders = db_placeholders($args, 'int');
  93. $sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created, count(n.nid) tag_count FROM {node} n INNER JOIN {community_tags} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $placeholders .') AND n.status = 1 GROUP BY n.nid ORDER BY '. $order;
  94. $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n INNER JOIN {community_tags} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $placeholders .') AND n.status = 1';
  95. }
  96. else {
  97. $joins = '';
  98. $wheres = '';
  99. $args = array();
  100. foreach ($descendant_tids as $index => $tids) {
  101. $joins .= ' INNER JOIN {community_tags} tn'. $index .' ON n.nid = tn'. $index .'.nid';
  102. $wheres .= ' AND tn'. $index .'.tid IN ('. db_placeholders($tids, 'int') .')';
  103. $args = array_merge($args, $tids);
  104. }
  105. $sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created, count(n.nid) tag_count FROM {node} n '. $joins .' WHERE n.status = 1 '. $wheres .' GROUP BY n.nid ORDER BY '. $order;
  106. $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n '. $joins .' WHERE n.status = 1 '. $wheres;
  107. }
  108. $sql = db_rewrite_sql($sql);
  109. $sql_count = db_rewrite_sql($sql_count);
  110. if ($pager) {
  111. $result = pager_query($sql, variable_get('default_nodes_main', 10), 0, $sql_count, $args);
  112. }
  113. else {
  114. $result = db_query_range($sql, $args, 0, variable_get('feed_default_items', 10));
  115. }
  116. }
  117. return $result;
  118. }