| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* $Id: adv_taxonomy_menu.inc,v 1.1.2.10 2008/01/31 16:23:34 brmassa Exp $
|
| 4 |
* @file adv_taxonomy_menu.inc
|
| 5 |
* * @author Jonathan Chaffer <jchaffer@structureinteractive.com> original taxonomy_menu.module
|
| 6 |
* @author Bruno Massa <http://drupal.org/user/67164> original taxonomy_menu.module
|
| 7 |
* @author Kent Parker <kent@webdev.passingphase.co.nz> adv_taxonomy_menu.module
|
| 8 |
* It Generates menu links for all taxonomy terms
|
| 9 |
*/
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Admin area. Configure the module, setting which
|
| 13 |
* vocabularies will be converted into menus items
|
| 14 |
*
|
| 15 |
* @return
|
| 16 |
* Array. The form fields.
|
| 17 |
*/
|
| 18 |
|
| 19 |
function theme_adv_taxonomy_menu_admin_item($setting) {
|
| 20 |
return '<td>'.$setting->name.'</td><td><a href="'.url('admin/settings/adv_taxonomy_menu').'/edit/'. $setting->tmid .'">edit</a></td><td><a href="'.url('admin/settings/adv_taxonomy_menu').'/delete/'. $setting->tmid .'">delete</a></td>';
|
| 21 |
}
|
| 22 |
|
| 23 |
function _adv_taxonomy_menu_rebuild_form() {
|
| 24 |
$form['submit'] = array(
|
| 25 |
'#value' => t('Rebuild Menu'),
|
| 26 |
'#type' => 'submit'
|
| 27 |
);
|
| 28 |
return $form;
|
| 29 |
}
|
| 30 |
function _adv_taxonomy_menu_rebuild_form_submit() {
|
| 31 |
menu_rebuild();
|
| 32 |
drupal_set_message('The menu has been rebuilt');
|
| 33 |
}
|
| 34 |
function theme_adv_taxonomy_menu_admin($array) {
|
| 35 |
if(!empty($array)) {
|
| 36 |
drupal_add_css(drupal_get_path('module', 'adv_taxonomy_menu') .'/adv_taxonomy_menu.css');
|
| 37 |
$output = t('Current available menu systems');
|
| 38 |
$output .= '<table id="taxonomy_menu">';
|
| 39 |
foreach($array as $item) $output .= '<tr>'.$item.'</tr>';
|
| 40 |
$output .= '</table>';
|
| 41 |
$output .= drupal_get_form('_adv_taxonomy_menu_rebuild_form');
|
| 42 |
}
|
| 43 |
else $output = t('There are currently no taxonomy menu systems created');
|
| 44 |
return $output;
|
| 45 |
}
|
| 46 |
|
| 47 |
function _adv_taxonomy_menu_admin($op) {
|
| 48 |
if($op == 'main') {
|
| 49 |
$output = '';
|
| 50 |
if($settings = adv_taxonomy_menu_get_settings()) {
|
| 51 |
while ($setting = db_fetch_object($settings)) {
|
| 52 |
$output[] = theme('adv_taxonomy_menu_admin_item', $setting);
|
| 53 |
}
|
| 54 |
}
|
| 55 |
return theme('adv_taxonomy_menu_admin', $output);
|
| 56 |
} else {
|
| 57 |
if($op=="delete") {
|
| 58 |
if(arg(4)) {
|
| 59 |
$tmid = arg(4);
|
| 60 |
return adv_taxonomy_menu_delete_form($tmid);
|
| 61 |
}
|
| 62 |
else $error = "yes";
|
| 63 |
}
|
| 64 |
if($op=="edit") {
|
| 65 |
if(arg(4)) {
|
| 66 |
$tmid = arg(4);
|
| 67 |
if($result = adv_taxonomy_menu_get_settings($tmid)) {
|
| 68 |
$settings = db_fetch_object($result);
|
| 69 |
$order = unserialize($settings->vocab_order);
|
| 70 |
}
|
| 71 |
$form['adv_taxonomy_menu_tmid'] = array(
|
| 72 |
'#type' => 'hidden',
|
| 73 |
'#value' => $tmid,
|
| 74 |
);
|
| 75 |
}
|
| 76 |
else $error = "yes";
|
| 77 |
}
|
| 78 |
$options = array(
|
| 79 |
ADV_TAXONOMY_MENU_NORMAL => t('Normal')
|
| 80 |
);
|
| 81 |
|
| 82 |
// If the Views module is enabled, add some special
|
| 83 |
// new features
|
| 84 |
$level = array("not selected");
|
| 85 |
$i = 1;
|
| 86 |
$num_levels = 0;
|
| 87 |
foreach(taxonomy_get_vocabularies() as $vocab) {
|
| 88 |
$level[] = $i;
|
| 89 |
if($order[$vocab->vid] >0 ) $num_levels++;
|
| 90 |
$i++;
|
| 91 |
}
|
| 92 |
|
| 93 |
$form['adv_taxonomy_menu_return'] = array(
|
| 94 |
'#value' => '<a href="'.url('admin/settings/adv_taxonomy_menu').'">Return to main administration page</a>',
|
| 95 |
'#type' => 'markup',
|
| 96 |
);
|
| 97 |
// Create some options for each of the vocabularies
|
| 98 |
$form['adv_taxonomy_menu_name'] = array(
|
| 99 |
'#default_value' => $settings->name,
|
| 100 |
'#description' => t('Choose a name to identify the menu system by'),
|
| 101 |
'#title' => t('Name of menu system'),
|
| 102 |
'#type' => 'textfield',
|
| 103 |
);
|
| 104 |
foreach (taxonomy_get_vocabularies() as $vocab) {
|
| 105 |
$form[$vocab->vid] = array(
|
| 106 |
'#title' => $vocab->name,
|
| 107 |
'#tree' => FALSE,
|
| 108 |
'#type' => 'fieldset',
|
| 109 |
);
|
| 110 |
$form[$vocab->vid]['adv_taxonomy_menu_vocab_order_'. $vocab->vid] = array(
|
| 111 |
'#title' => t('Menu Level'),
|
| 112 |
'#type' => 'select',
|
| 113 |
'#options' => $level,
|
| 114 |
'#default_value' => $order[$vocab->vid],
|
| 115 |
'#description' => t('Enter a number to select the level of this vocabulary in the menu. If you do not select this vocabulary in the menu then this is ignored.'),
|
| 116 |
'#required' => FALSE,
|
| 117 |
);
|
| 118 |
|
| 119 |
}
|
| 120 |
|
| 121 |
$form[$vocab->vid]['adv_taxonomy_menu_show'] = array(
|
| 122 |
'#default_value' => $settings->show_normal,
|
| 123 |
'#options' => $options,
|
| 124 |
'#title' => t('Default or custom output. If you select view then you will need to construct a full sql to select nodes according to terms IDs shown in the url (This feature currently not available). Default applies'),
|
| 125 |
'#type' => 'radios',
|
| 126 |
);
|
| 127 |
// In case of View options selected, select Views - Currently not active
|
| 128 |
if (module_exists('viewsxx')) {
|
| 129 |
// Add the Views file with more functions
|
| 130 |
require_once(drupal_get_path('module', 'views') .'/views_cache.inc');
|
| 131 |
|
| 132 |
// Add a new options on Categories
|
| 133 |
$options[ADV_TAXONOMY_MENU_VIEW] = t('Views');
|
| 134 |
|
| 135 |
// Get the list of User generated views
|
| 136 |
$views = db_query("SELECT * FROM {view_view}");
|
| 137 |
while ($view = db_fetch_array($views)) {
|
| 138 |
//drupal_set_message(print_r($view, 1));
|
| 139 |
$views_list[$view['vid']] = $view['name'];
|
| 140 |
}
|
| 141 |
|
| 142 |
// Now get a list of default Views
|
| 143 |
foreach (_views_get_default_views() as $view => $viewdata) {
|
| 144 |
$views_list[$viewdata->vid] = $viewdata->name;
|
| 145 |
}
|
| 146 |
$form[$vocab->vid]['adv_taxonomy_menu_show_view'] = array(
|
| 147 |
'#default_value' => $settings->show_views,
|
| 148 |
'#options' => $views_list,
|
| 149 |
'#title' => t('Views available'),
|
| 150 |
'#type' => 'select',
|
| 151 |
);
|
| 152 |
}
|
| 153 |
// General options
|
| 154 |
$form['adv_taxonomy_menu_num_levels'] = array(
|
| 155 |
'#value' => t('The number of levels of this menu is currently set at '.$num_levels),
|
| 156 |
'#type' => 'markup',
|
| 157 |
);
|
| 158 |
$form['adv_taxonomy_menu_display_page'] = array(
|
| 159 |
'#default_value' => $settings->display_page,
|
| 160 |
'#description' => t('What is the label to use in the url? Example: categories/1/2/3, technology/1/2/3'),
|
| 161 |
'#title' => t('Module page'),
|
| 162 |
'#type' => 'textfield',
|
| 163 |
);
|
| 164 |
$form['adv_taxonomy_menu_display_num'] = array(
|
| 165 |
'#default_value' => $settings->display_num,
|
| 166 |
'#description' => t('If checked, number of node per term will be displayed in the menu.'),
|
| 167 |
'#title' => t('Display number of nodes per terms'),
|
| 168 |
'#type' => 'checkbox',
|
| 169 |
);
|
| 170 |
$form['adv_taxonomy_menu_hide_empty'] = array(
|
| 171 |
'#default_value' => $settings->hide_empty,
|
| 172 |
'#description' => t('If checked, only taxonomy terms with members will be shown in the menu.'),
|
| 173 |
'#title' => t('Hide Empty Terms'),
|
| 174 |
'#type' => 'checkbox',
|
| 175 |
);
|
| 176 |
$form['adv_taxonomy_menu_display_descendants'] = array(
|
| 177 |
'#default_value' => $settings->display_descendants,
|
| 178 |
'#description' => t('If checked, then when a term is selected all nodes belonging to subterms are also displayed.'),
|
| 179 |
'#title' => t('Display descendants'),
|
| 180 |
'#type' => 'checkbox',
|
| 181 |
);
|
| 182 |
|
| 183 |
$form['system_mid'] = array(
|
| 184 |
'#value' => $settings->mid,
|
| 185 |
'#type' => 'hidden',
|
| 186 |
);
|
| 187 |
$form['submit'] = array(
|
| 188 |
'#value' => t('Submit'),
|
| 189 |
'#type' => 'submit'
|
| 190 |
);
|
| 191 |
|
| 192 |
return $form;
|
| 193 |
}
|
| 194 |
}
|
| 195 |
|
| 196 |
function adv_taxonomy_menu_delete_form($tmid) {
|
| 197 |
$menu->nid = $tmid;
|
| 198 |
$form['nid'] = array('#type' => 'value', '#value' => $tmid);
|
| 199 |
$form['form_id'] = array('#type' => 'value', '#value' => 'delete_menu');
|
| 200 |
return confirm_form($form,
|
| 201 |
t('Are you sure you want to delete this menu system'),
|
| 202 |
isset($_GET['destination']) ? $_GET['destination'] : 'admin/settings/adv_taxonomy_menu',
|
| 203 |
t('This action cannot be undone.'),
|
| 204 |
t('Delete'), t('Cancel'));
|
| 205 |
}
|
| 206 |
|
| 207 |
function adv_taxonomy_menu_delete_form_submit($form_id, $form_values) {
|
| 208 |
db_query("DELETE FROM {adv_taxonomy_menu} WHERE tmid=%d", $form_values['nid']);
|
| 209 |
drupal_goto('admin/settings/adv_taxonomy_menu');
|
| 210 |
}
|
| 211 |
|
| 212 |
|
| 213 |
/**
|
| 214 |
* Generates the breadcumb for nodes that
|
| 215 |
* have a category listed as a menu
|
| 216 |
*
|
| 217 |
* @param
|
| 218 |
* Object. The node object
|
| 219 |
* @param
|
| 220 |
* Array. The list of all taxonomy vocabs and
|
| 221 |
* terms that this node have and are also
|
| 222 |
* menus
|
| 223 |
*/
|
| 224 |
function _adv_taxonomy_menu_node_view(&$node, &$vocabs) {
|
| 225 |
foreach ($vocabs as $vid => $vocab) {
|
| 226 |
$path = variable_get('adv_taxonomy_menu_display_page', 'category') .'/'. $vid;
|
| 227 |
|
| 228 |
$tree = taxonomy_get_tree($vid);
|
| 229 |
$old_depth = -1;
|
| 230 |
$old_path = $path;
|
| 231 |
|
| 232 |
// Generate the entire breadcumb
|
| 233 |
foreach ($tree as $term) {
|
| 234 |
//drupal_set_message('term '.print_r($term,1));
|
| 235 |
if ($term->depth <= $old_depth) {
|
| 236 |
$slashes_to_remove = $old_depth - $term->depth + 1;
|
| 237 |
for ($i = 0; $i < $slashes_to_remove; $i++) {
|
| 238 |
$old_path = substr($old_path, 0, strrpos($old_path, "/"));
|
| 239 |
}
|
| 240 |
}
|
| 241 |
$path = $old_path .'/'. $term->tid;
|
| 242 |
$old_depth = $term->depth;
|
| 243 |
$old_path = $path;
|
| 244 |
|
| 245 |
// When we reach the term that this node uses,
|
| 246 |
// set the breadcumb
|
| 247 |
if ($term->tid == $vocab[0]) {
|
| 248 |
menu_set_location(array(
|
| 249 |
array('path' => $path, 'title' => t($term->name)),
|
| 250 |
array('path' => 'node/'. $node->nid, 'title' => $node->title)
|
| 251 |
));
|
| 252 |
|
| 253 |
// Quit after the first match.
|
| 254 |
return;
|
| 255 |
}
|
| 256 |
}
|
| 257 |
}
|
| 258 |
}
|
| 259 |
|
| 260 |
/**
|
| 261 |
* Page callback that renders a node listing for the selected term.
|
| 262 |
*/
|
| 263 |
function _adv_taxonomy_menu_page() {
|
| 264 |
// Check if the Vocabulary ID is set
|
| 265 |
if ($vid = arg(1)) {
|
| 266 |
if($result1 = adv_taxonomy_menu_get_settings($tmid)) {
|
| 267 |
$settings = db_fetch_object($result1);
|
| 268 |
}
|
| 269 |
$view_id = 0;
|
| 270 |
if($settings->show_normal>1) $view_id = $settings->show_views;
|
| 271 |
// Depending on what Output technique is used,
|
| 272 |
// show the nodes' list
|
| 273 |
if ($view_id==0) {
|
| 274 |
if ($tid = arg(2)) {
|
| 275 |
$tid = explode('/', $_GET['q']);
|
| 276 |
// first item is category page, second is vocab id. Need all tids that follow
|
| 277 |
$tids = array();
|
| 278 |
foreach($tid as $key => $value) {
|
| 279 |
if($key>1) $tids[] = $value;
|
| 280 |
}
|
| 281 |
// allow another module to modify the result set. Note this will only process ONE module and then exit
|
| 282 |
foreach (module_implements('adv_taxonomy_menu_sql_alter') AS $module) {
|
| 283 |
$function = $module .'_adv_taxonomy_menu_sql_alter';
|
| 284 |
$result = $function($settings, $tids, 'and', variable_get('adv_taxonomy_menu_display_descendants', TRUE) ? 'all' : 0);
|
| 285 |
break;
|
| 286 |
}
|
| 287 |
|
| 288 |
if(!isset($result)) $result = taxonomy_select_nodes($tids, 'and', variable_get('adv_taxonomy_menu_display_descendants', TRUE) ? 'all' : 0);
|
| 289 |
}
|
| 290 |
else {
|
| 291 |
// If no arg(2), we're looking at just the vid. If
|
| 292 |
// display_descendants is on, grab all terms regardless
|
| 293 |
// of depth. If off, grab depth 0 terms.
|
| 294 |
$tree = taxonomy_get_tree($vid);
|
| 295 |
$descendants = variable_get('adv_taxonomy_menu_display_descendants', 1);
|
| 296 |
foreach ($tree as $term) {
|
| 297 |
if ($descendants or $term->depth == 0) {
|
| 298 |
$tids[] = $term->tid;
|
| 299 |
}
|
| 300 |
}
|
| 301 |
// allow another module to modify the result set. Note this will only process ONE module and then exit
|
| 302 |
foreach (module_implements('adv_taxonomy_menu_sql_alter') AS $module) {
|
| 303 |
$function = $module .'_adv_taxonomy_menu_sql_alter';
|
| 304 |
$result = $function($settings, $tids, 'or', 0);
|
| 305 |
break;
|
| 306 |
}
|
| 307 |
// The requested terms have already been determined,
|
| 308 |
// so don't request descendants here.
|
| 309 |
if(!isset($result)) $result = taxonomy_select_nodes($tids, 'or', 0);
|
| 310 |
}
|
| 311 |
// Render the selected nodes
|
| 312 |
$output = '';
|
| 313 |
if (db_num_rows($result) > 0) {
|
| 314 |
$nodes = array();
|
| 315 |
while ($node = db_fetch_object($result)) {
|
| 316 |
$nodes[] = $node;
|
| 317 |
}
|
| 318 |
$output .= theme('adv_taxonomy_menu_output', $nodes);
|
| 319 |
$output .= theme('pager', NULL, variable_get('default_nodes_main', 10), 0);
|
| 320 |
}
|
| 321 |
}
|
| 322 |
elseif ($view_id>0) {
|
| 323 |
// Get the last page argument
|
| 324 |
$tid = explode('/', $_GET['q']);
|
| 325 |
$tid = db_escape_string(array_pop($tid));
|
| 326 |
|
| 327 |
$arguments[] = $vid;
|
| 328 |
// Only add the Term ID if its not the Vocabulary ID
|
| 329 |
if ($vid != $tid) {
|
| 330 |
$arguments[] = $tid;
|
| 331 |
}
|
| 332 |
|
| 333 |
// Embed the views output into the page
|
| 334 |
$output = views_build_view('embed',
|
| 335 |
views_get_view($view_id),
|
| 336 |
$arguments, FALSE, NULL);
|
| 337 |
}
|
| 338 |
}
|
| 339 |
|
| 340 |
// If no content found, return a "error" message
|
| 341 |
return empty($output) ? t('No content for this category.') : $output;
|
| 342 |
}
|
| 343 |
|
| 344 |
function theme_adv_taxonomy_menu_output($nodes) {
|
| 345 |
$output = '<ul class="item-list">';
|
| 346 |
foreach($nodes as $node) {
|
| 347 |
$output .= '<li>'.l($node->title, 'node/'.$node->nid).'</li>';
|
| 348 |
|
| 349 |
}
|
| 350 |
$output .= '</ul>';
|
| 351 |
return $output;
|
| 352 |
}
|