| 1 |
<?php
|
| 2 |
// $Id: taxonomy_navigation.module,v 1.1.2.5 2009/02/02 20:24:16 hanoii Exp $
|
| 3 |
|
| 4 |
define('TAXONOMY_NAVIGATION_DEFAULT_NUMBER_OF_NODES', 1);
|
| 5 |
|
| 6 |
/**
|
| 7 |
* Helper function to decide if a vocabulary should be supported by this module
|
| 8 |
*
|
| 9 |
* @param object $vocabulary
|
| 10 |
* taxonomy vocabulary object
|
| 11 |
* @return bool
|
| 12 |
*/
|
| 13 |
function _taxonomy_navigation_is_supported($vocabulary) {
|
| 14 |
$supported = FALSE;
|
| 15 |
|
| 16 |
if ($vocabulary->hierarchy < 2) {
|
| 17 |
$supported = TRUE;
|
| 18 |
}
|
| 19 |
|
| 20 |
return $supported;
|
| 21 |
}
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Count the number of published nodes classified by a term.
|
| 25 |
*
|
| 26 |
* @param $tid
|
| 27 |
* The term's ID
|
| 28 |
*
|
| 29 |
* @param $depth
|
| 30 |
* The $depth. Take depth into consideration when counting
|
| 31 |
*
|
| 32 |
* @return int
|
| 33 |
* An integer representing a number of nodes.
|
| 34 |
*/
|
| 35 |
function _taxonomy_navigation_count($tid, $depth) {
|
| 36 |
static $count;
|
| 37 |
|
| 38 |
if ( $depth == 'all' ) {
|
| 39 |
$cnt = taxonomy_term_count_nodes($tid);
|
| 40 |
} else {
|
| 41 |
|
| 42 |
if (!isset($count[$tid]) ) {
|
| 43 |
$result = db_query(db_rewrite_sql('SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 GROUP BY t.tid'));
|
| 44 |
while ($term = db_fetch_object($result)) {
|
| 45 |
$count[$term->tid] = $term->c;
|
| 46 |
}
|
| 47 |
}
|
| 48 |
|
| 49 |
if ( $depth != 0 ) {
|
| 50 |
foreach (_taxonomy_term_children($tid) as $c) {
|
| 51 |
$children_count += _taxonomy_navigation_count($c, $depth-1);
|
| 52 |
}
|
| 53 |
}
|
| 54 |
|
| 55 |
$cnt = $count[$tid] + $children_count ;
|
| 56 |
}
|
| 57 |
|
| 58 |
return $cnt;
|
| 59 |
}
|
| 60 |
|
| 61 |
function taxonomy_navigation_menu($may_cache) {
|
| 62 |
|
| 63 |
if ( $may_cache ) {
|
| 64 |
$items[]= array (
|
| 65 |
'path' => 'admin/settings/taxonomy_navigation',
|
| 66 |
'title' => t('Taxonomy Navigation'),
|
| 67 |
'callback' => 'drupal_get_form',
|
| 68 |
'callback arguments' => array('taxonomy_navigation_settings'),
|
| 69 |
'access' => user_access('administer site configuration'),
|
| 70 |
'description' => t('Select which, where and how vocabularies should be included in navigation.'),
|
| 71 |
'type' => MENU_NORMAL_ITEM,
|
| 72 |
);
|
| 73 |
} else {
|
| 74 |
foreach (taxonomy_get_vocabularies() as $vocabulary) {
|
| 75 |
if (_taxonomy_navigation_is_supported($vocabulary) && variable_get('taxonomy_navigation_show_' . $vocabulary->vid, 0)) {
|
| 76 |
// get parent settings
|
| 77 |
$pid = variable_get('taxonomy_navigation_pid_' . $vocabulary->vid, 0);
|
| 78 |
|
| 79 |
$tree = taxonomy_get_tree($vocabulary->vid);
|
| 80 |
// build an array which holds all children of current term. necessary to build a proper 'or' value in the HREF
|
| 81 |
foreach ($tree as $term) {
|
| 82 |
$count = 0;
|
| 83 |
if (variable_get('taxonomy_navigation_nodes_' . $vocabulary->vid, TAXONOMY_NAVIGATION_DEFAULT_NUMBER_OF_NODES)) {
|
| 84 |
$count = _taxonomy_navigation_count($term->tid, variable_get('taxonomy_navigation_depth_' . $vocabulary->vid, 'all'));
|
| 85 |
}
|
| 86 |
|
| 87 |
if ( ( $alias = drupal_get_path_alias('taxonomy/term/' . $term->tid) ) != 'taxonomy/term/' . $term->tid ) {
|
| 88 |
$item = array(
|
| 89 |
'path' => drupal_get_path_alias('taxonomy/term/' . $term->tid),
|
| 90 |
'title' => theme('taxonomy_navigation_menu_title', $term, $count),
|
| 91 |
'access' => TRUE,
|
| 92 |
'type' => MENU_NORMAL_ITEM,
|
| 93 |
'weight' => variable_get('taxonomy_navigation_weight_' . $vocabulary->vid, -20) + $term->weight,
|
| 94 |
'tid' => $term->tid,
|
| 95 |
);
|
| 96 |
|
| 97 |
if ( $pid != 0 ) {
|
| 98 |
// If term has no parent, it means it's a root term
|
| 99 |
if (array_sum($term->parents) == 0) {
|
| 100 |
$item['pid'] = $pid;
|
| 101 |
}
|
| 102 |
}
|
| 103 |
$items[] = $item;
|
| 104 |
|
| 105 |
$items[] = array(
|
| 106 |
'path' => 'taxonomy/term/' . $term->tid,
|
| 107 |
'title' => t($term->name).$out,
|
| 108 |
'callback' => 'taxonomy_navigation_page',
|
| 109 |
'callback arguments' => array($term->tid, variable_get('taxonomy_navigation_depth_' . $vocabulary->vid, 'all')),
|
| 110 |
'access' => user_access('access content'),
|
| 111 |
'type' => MENU_CALLBACK,
|
| 112 |
'tid' => $term->tid,
|
| 113 |
);
|
| 114 |
}
|
| 115 |
|
| 116 |
}
|
| 117 |
}
|
| 118 |
}
|
| 119 |
}
|
| 120 |
|
| 121 |
return $items;
|
| 122 |
}
|
| 123 |
|
| 124 |
function taxonomy_navigation_page($str_tids = '', $depth = 0, $op = 'page') {
|
| 125 |
menu_set_active_item(drupal_get_path_alias($_GET['q']));
|
| 126 |
|
| 127 |
if (module_exists('views') && ($view_name = db_result(db_query("SELECT name FROM {view_view} WHERE url = '%s'", 'taxonomy/term')))) {
|
| 128 |
$q = drupal_get_normal_path($_GET['q']);
|
| 129 |
$views_args = array();
|
| 130 |
$views_args[] = $view_name;
|
| 131 |
$args = split('/', $q);
|
| 132 |
for ($i = 2 ; $i < count($args) ; $i++) {
|
| 133 |
$views_args[] = $args[$i];
|
| 134 |
}
|
| 135 |
$output = call_user_func_array('views_view_page', $views_args);
|
| 136 |
}
|
| 137 |
else {
|
| 138 |
$output = taxonomy_term_page($str_tids, $depth, $op);
|
| 139 |
}
|
| 140 |
|
| 141 |
return $output;
|
| 142 |
}
|
| 143 |
|
| 144 |
/**
|
| 145 |
* Menu callback; manage settings for taxonomy menu.
|
| 146 |
*/
|
| 147 |
function taxonomy_navigation_settings() {
|
| 148 |
|
| 149 |
$form['taxonomy_navigation_help'] = array(
|
| 150 |
'#type' => 'markup',
|
| 151 |
'#value' => t('<p>Only vocabularies with <i>Disabled</i> or <i>Single</i> hierarchy can be handled by this module.</p>'),
|
| 152 |
);
|
| 153 |
|
| 154 |
if (module_exists('views') && ($view_name = db_result(db_query("SELECT name FROM {view_view} WHERE url = '%s'", 'taxonomy/term')))) {
|
| 155 |
$form['taxonomy_navigation_views'] = array(
|
| 156 |
'#type' => 'fieldset',
|
| 157 |
'#title' => t('Views'),
|
| 158 |
);
|
| 159 |
$form['taxonomy_navigation_views']['text'] = array(
|
| 160 |
'#type' => 'markup',
|
| 161 |
'#value' => t('<p>A views overriding !view was found. This module will use that view instead of the regular taxonomy page. All the elements of the URL after taxonomy/term will be passed as arguments to the view. <b>Default depth</b> settings below will not have any effect as it should be set on the view itself.', array('!view' => l('taxonomy/term', 'admin/build/views/' . $view_name . '/edit'))),
|
| 162 |
);
|
| 163 |
}
|
| 164 |
|
| 165 |
foreach (taxonomy_get_vocabularies() as $vocabulary) {
|
| 166 |
if (_taxonomy_navigation_is_supported($vocabulary)) {
|
| 167 |
$enable = variable_get('taxonomy_navigation_show_' . $vocabulary->vid, 0);
|
| 168 |
$form['taxonomy_navigation_show'][$vocabulary->vid] = array(
|
| 169 |
'#type' => 'fieldset',
|
| 170 |
'#title' => t('!vocab - !enable', array('!vocab' => t($vocabulary->name), '!enable' => ($enable ? t('Enabled') : t('Disabled')))),
|
| 171 |
'#collapsible' => TRUE,
|
| 172 |
'#collapsed' => TRUE,
|
| 173 |
);
|
| 174 |
$form['taxonomy_navigation_show'][$vocabulary->vid]['taxonomy_navigation_show_' . $vocabulary->vid] = array(
|
| 175 |
'#type' => 'checkbox',
|
| 176 |
'#title' => t('Show "%vocab" in menu', array('%vocab' => t($vocabulary->name))),
|
| 177 |
'#default_value' => variable_get('taxonomy_navigation_show_' . $vocabulary->vid, 0),
|
| 178 |
);
|
| 179 |
$form['taxonomy_navigation_show'][$vocabulary->vid]['taxonomy_navigation_depth_' . $vocabulary->vid] = array(
|
| 180 |
'#type' => 'textfield',
|
| 181 |
'#title' => t('Default depth'),
|
| 182 |
'#description' => t('Depth of hierarchy terms to display on each navigation item. You can use "all" for a complete list.'),
|
| 183 |
'#default_value' => variable_get('taxonomy_navigation_depth_' . $vocabulary->vid, 'all'),
|
| 184 |
);
|
| 185 |
$form['taxonomy_navigation_show'][$vocabulary->vid]['taxonomy_navigation_weight_' . $vocabulary->vid] = array(
|
| 186 |
'#type' => 'textfield',
|
| 187 |
'#title' => t('Weight offset'),
|
| 188 |
'#description' => t('Weight offset that will be added to the term weight. Useful to move navigation items alltogether. It can be positive or negative.'),
|
| 189 |
'#default_value' => variable_get('taxonomy_navigation_weight_' . $vocabulary->vid, -20),
|
| 190 |
);
|
| 191 |
$options = array();
|
| 192 |
$options[0] = t('Unset');
|
| 193 |
$root_menues = menu_get_root_menus();
|
| 194 |
foreach ( $root_menues as $mid => $title ) {
|
| 195 |
$options[$mid] = $title;
|
| 196 |
}
|
| 197 |
$form['taxonomy_navigation_show'][$vocabulary->vid]['taxonomy_navigation_pid_' . $vocabulary->vid] = array(
|
| 198 |
'#type' => 'select',
|
| 199 |
'#title' => t('Menu parent'),
|
| 200 |
'#options' => $options,
|
| 201 |
'#description' => t('Choose one of your root menues to be the parent of the dynamic menu items of this vocabulary. Unset means default parent will be used which is generally Navigation.'),
|
| 202 |
'#default_value' => variable_get('taxonomy_navigation_pid_' . $vocabulary->vid, 0),
|
| 203 |
);
|
| 204 |
$form['taxonomy_navigation_show'][$vocabulary->vid]['taxonomy_navigation_nodes_' . $vocabulary->vid] = array(
|
| 205 |
'#type' => 'checkbox',
|
| 206 |
'#title' => t('Display number of nodes'),
|
| 207 |
'#description' => t('Choose whether to display the amount of nodes in each term.'),
|
| 208 |
'#default_value' => variable_get('taxonomy_navigation_nodes_' . $vocabulary->vid, TAXONOMY_NAVIGATION_DEFAULT_NUMBER_OF_NODES),
|
| 209 |
);
|
| 210 |
}
|
| 211 |
}
|
| 212 |
|
| 213 |
return system_settings_form($form);
|
| 214 |
}
|
| 215 |
|
| 216 |
function taxonomy_navigation_nodeapi($node, $op, $teaser, $page) {
|
| 217 |
if ($op == 'view' && !$teaser && $page) {
|
| 218 |
foreach ( $node->taxonomy as $term ) {
|
| 219 |
if ( variable_get('taxonomy_navigation_show_' . $term->vid, 0) ) {
|
| 220 |
if ( ( $alias = drupal_get_path_alias('taxonomy/term/' . $term->tid) ) != 'taxonomy/term/' . $term->tid ) {
|
| 221 |
$items[] = array(
|
| 222 |
'path' => $alias,
|
| 223 |
);
|
| 224 |
}
|
| 225 |
}
|
| 226 |
}
|
| 227 |
if ( count($items) ) {
|
| 228 |
$items[] = array(
|
| 229 |
'path' => 'node/'.$node->nid,
|
| 230 |
);
|
| 231 |
menu_set_location($items);
|
| 232 |
}
|
| 233 |
}
|
| 234 |
}
|
| 235 |
|
| 236 |
function theme_taxonomy_navigation_nodes($qty) {
|
| 237 |
if ($qty) {
|
| 238 |
$output = ' (' . $qty . ')';
|
| 239 |
}
|
| 240 |
return $output;
|
| 241 |
}
|
| 242 |
|
| 243 |
function theme_taxonomy_navigation_menu_title($term, $qty) {
|
| 244 |
$title .= t($term->name);
|
| 245 |
if (variable_get('taxonomy_navigation_nodes_' . $term->vid, TAXONOMY_NAVIGATION_DEFAULT_NUMBER_OF_NODES)) {
|
| 246 |
$title .= theme('taxonomy_navigation_nodes', $qty);
|
| 247 |
}
|
| 248 |
|
| 249 |
return $title;
|
| 250 |
}
|