| 1 |
<?php
|
| 2 |
// $Id: taxonomy_menu.module,v 1.19.2.2.2.32 2009/03/26 02:52:28 indytechcook Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* It Generates menu links for all selected taxonomy terms
|
| 7 |
*
|
| 8 |
* @author Neil Hastings <http://drupal.org/user/245817>
|
| 9 |
* @author Mark Theunissen <http://drupal.org/user/108606>
|
| 10 |
* @author Afief Halumi <http://drupal.org/user/237472>
|
| 11 |
*/
|
| 12 |
|
| 13 |
//include the database layer
|
| 14 |
require_once(drupal_get_path('module', 'taxonomy_menu') .'/taxonomy_menu.database.inc');
|
| 15 |
|
| 16 |
/**
|
| 17 |
* Implementation of hook_form_alter().
|
| 18 |
*
|
| 19 |
* Modify the form at admin/content/taxonomy/edit/vocabulary/xx. We add
|
| 20 |
* our taxonomy_menu options in here on a per-vocab basis.
|
| 21 |
*/
|
| 22 |
function taxonomy_menu_form_alter(&$form, $form_state, $form_id) {
|
| 23 |
if ($form_id == 'taxonomy_form_vocabulary') {
|
| 24 |
// choose a menu to add link items to.
|
| 25 |
$menus = menu_get_menus();
|
| 26 |
array_unshift($menus, '= DISABLED =');
|
| 27 |
|
| 28 |
//options for path if tokens are not enabled
|
| 29 |
$paths = _taxonomy_menu_get_paths();
|
| 30 |
|
| 31 |
$form['taxonomy_menu'] = array(
|
| 32 |
'#type' => 'fieldset',
|
| 33 |
'#collapsible' => TRUE,
|
| 34 |
'#title' => t('Taxonomy menu'),
|
| 35 |
'#weight' => 10,
|
| 36 |
'#tree' => TRUE,
|
| 37 |
);
|
| 38 |
//this turns the vocab terms into menu items
|
| 39 |
$form['taxonomy_menu']['vocab_menu'] = array(
|
| 40 |
'#type' => 'select',
|
| 41 |
'#title' => t('Menu'),
|
| 42 |
'#default_value' => variable_get('taxonomy_menu_vocab_menu_'. $form['vid']['#value'], FALSE),
|
| 43 |
'#options' => $menus,
|
| 44 |
'#description' => t('With this option enabled, an entry will be created in the menu system for this vocabulary.')
|
| 45 |
);
|
| 46 |
|
| 47 |
$form['taxonomy_menu']['path'] = array(
|
| 48 |
'#type' => 'select',
|
| 49 |
'#title' => t('Menu Path Type'),
|
| 50 |
'#default_value' => variable_get('taxonomy_menu_path_'. $form['vid']['#value'], 0),
|
| 51 |
'#options' => $paths,
|
| 52 |
'#description' => t('If Default is selected then the path is taxonomy/term/tid <br />
|
| 53 |
The menu path will be passed through drupal_get_path_alias() function so all aliases will be applied'
|
| 54 |
),
|
| 55 |
);
|
| 56 |
|
| 57 |
//get taxonomy menu form options
|
| 58 |
$form['taxonomy_menu']['options'] = _taxonomy_menu_create_options($form['vid']['#value']);
|
| 59 |
|
| 60 |
//rebuild the menu
|
| 61 |
$form['taxonomy_menu']['options']['rebuild'] = array(
|
| 62 |
'#type' => 'checkbox',
|
| 63 |
'#title' => t('Select to rebuild the menu on submit.'),
|
| 64 |
'#default_value' => 0,
|
| 65 |
'#weight' => 20,
|
| 66 |
'#description' => t('Rebuild the menu on submit. Warning: This will delete then re-create all of the menu items. Only use this option if you are experiencing issues.'),
|
| 67 |
);
|
| 68 |
// move the buttons to the bottom of the form
|
| 69 |
$form['submit']['#weight'] = 49;
|
| 70 |
$form['delete']['#weight'] = 50;
|
| 71 |
|
| 72 |
// add an extra submit handler to save these settings
|
| 73 |
$form['#submit'][] = 'taxonomy_menu_vocab_submit';
|
| 74 |
|
| 75 |
}
|
| 76 |
}
|
| 77 |
|
| 78 |
/**
|
| 79 |
* Submit handler for the extra settings added to the taxonomy vocab form.
|
| 80 |
*
|
| 81 |
* Check to see if the user has selected a different menu, and only rebuild
|
| 82 |
* if this is the case.
|
| 83 |
*/
|
| 84 |
function taxonomy_menu_vocab_submit($form, &$form_state) {
|
| 85 |
$vid = $form_state['values']['vid'];
|
| 86 |
$changed = FALSE;
|
| 87 |
|
| 88 |
//set the menu name and check for changes
|
| 89 |
$variable_name = _taxonomy_menu_build_variable('vocab_menu', $vid);
|
| 90 |
if (_taxonomy_menu_check_variable($variable_name, $form_state['values']['taxonomy_menu']['vocab_menu'])) {
|
| 91 |
$changed_menu = TRUE;
|
| 92 |
}
|
| 93 |
variable_set($variable_name, $form_state['values']['taxonomy_menu']['vocab_menu']);
|
| 94 |
|
| 95 |
//set the path and check for changes
|
| 96 |
$variable_name = _taxonomy_menu_build_variable('path', $vid);
|
| 97 |
if (_taxonomy_menu_check_variable($variable_name, $form_state['values']['taxonomy_menu']['path'])) {
|
| 98 |
$changed_path = TRUE;
|
| 99 |
}
|
| 100 |
variable_set($variable_name, $form_state['values']['taxonomy_menu']['path']);
|
| 101 |
|
| 102 |
foreach ($form_state['values']['taxonomy_menu']['options'] as $key => $value) {
|
| 103 |
//create the variable name
|
| 104 |
$variable_name = _taxonomy_menu_build_variable($key, $vid);
|
| 105 |
|
| 106 |
//check to see if the vocab enable options has changed
|
| 107 |
if ($key = 'voc_item') {
|
| 108 |
if (_taxonomy_menu_check_variable($variable_name, $value)) {
|
| 109 |
$change_vocab_item = TRUE;
|
| 110 |
}
|
| 111 |
}
|
| 112 |
|
| 113 |
//if $changed is alreayd set to true, then don't bother checking any others
|
| 114 |
if (!$changed) {
|
| 115 |
//check to see if the variable has changed
|
| 116 |
if (_taxonomy_menu_check_variable($variable_name, $value)) {
|
| 117 |
$changed = TRUE;
|
| 118 |
}
|
| 119 |
}
|
| 120 |
//save variable
|
| 121 |
variable_set($variable_name, $value);
|
| 122 |
}
|
| 123 |
|
| 124 |
//if the menu hasn't changed and the menu is disabled then do not do anything else
|
| 125 |
if ($form_state['values']['taxonomy_menu']['options']['rebuild'] ||
|
| 126 |
$changed_menu ||
|
| 127 |
(!$changed_menu && variable_get('taxonomy_menu_vocab_menu_'. $vid, FALSE) == 0)) {
|
| 128 |
//rebuild if rebuild is selected, menu has changed or vocabulary option changed
|
| 129 |
if ($form_state['values']['taxonomy_menu']['options']['rebuild'] || $changed_menu || $change_vocab_item || $changed_path) {
|
| 130 |
$message = _taxonomy_menu_rebuild($vid);
|
| 131 |
}
|
| 132 |
//if setting has changed and a menu item is enabled, then update all of the menu items
|
| 133 |
elseif ($changed && variable_get('taxonomy_menu_vocab_menu_'. $vid, FALSE)) {
|
| 134 |
$message = _taxonomy_menu_update_link_items($vid);
|
| 135 |
}
|
| 136 |
|
| 137 |
//do a full menu rebuild incase we have removed the menu or moved it between menus
|
| 138 |
variable_set('menu_rebuild_needed', TRUE);
|
| 139 |
drupal_set_message($message, 'status');
|
| 140 |
}
|
| 141 |
}
|
| 142 |
|
| 143 |
/**
|
| 144 |
* rebuilds a menu
|
| 145 |
*
|
| 146 |
* @param $vid
|
| 147 |
* @return $message
|
| 148 |
* message that is displayed
|
| 149 |
*/
|
| 150 |
function _taxonomy_menu_rebuild($vid) {
|
| 151 |
//remove all of the menu items for this vocabulary
|
| 152 |
_taxonomy_menu_delete_all($vid);
|
| 153 |
|
| 154 |
//only insert the links if a menu is set
|
| 155 |
if (variable_get('taxonomy_menu_vocab_menu_'. $vid, FALSE)) {
|
| 156 |
_taxonomy_menu_insert_link_items($vid);
|
| 157 |
return t('The Taxonomy Menu has been rebuilt');
|
| 158 |
}
|
| 159 |
|
| 160 |
return t('The Taxonomy Menu has been removed');
|
| 161 |
}
|
| 162 |
|
| 163 |
/**
|
| 164 |
* Checks to see if the variable has changed.
|
| 165 |
*
|
| 166 |
* @param $variable
|
| 167 |
* name of variable
|
| 168 |
* @return Boolean
|
| 169 |
* TRUE if it has changed
|
| 170 |
*/
|
| 171 |
function _taxonomy_menu_check_variable($variable, $new_value) {
|
| 172 |
if ($new_value != variable_get($variable, FALSE)) {
|
| 173 |
return TRUE;
|
| 174 |
}
|
| 175 |
return FALSE;
|
| 176 |
}
|
| 177 |
|
| 178 |
/**
|
| 179 |
* Update the menu items
|
| 180 |
*
|
| 181 |
* @param $vid
|
| 182 |
* vocab id
|
| 183 |
*/
|
| 184 |
function _taxonomy_menu_update_link_items($vid) {
|
| 185 |
// rebuild vocab 'taxonomy_menu_voc_item_' . $vid
|
| 186 |
$menu_name = variable_get('taxonomy_menu_vocab_menu_'. $vid, FALSE);
|
| 187 |
|
| 188 |
//get a list of the current menu links
|
| 189 |
$menu_links = _taxonomy_menu_get_terms($vid);
|
| 190 |
|
| 191 |
//cycle through the
|
| 192 |
foreach ($menu_links as $menu_link) {
|
| 193 |
if ($menu_link->tid == 0) {
|
| 194 |
$args['vid'] = $menu_link->vid;
|
| 195 |
}
|
| 196 |
else {
|
| 197 |
$args['term'] = taxonomy_get_term($menu_link->tid);
|
| 198 |
}
|
| 199 |
|
| 200 |
//set the mneu name
|
| 201 |
$args['menu_name'] = $menu_name;
|
| 202 |
|
| 203 |
//update the menu link
|
| 204 |
taxonomy_menu_handler('update', $args);
|
| 205 |
}
|
| 206 |
|
| 207 |
return t("The Taxonomy Menu $menu_name has been updated.");
|
| 208 |
}
|
| 209 |
|
| 210 |
/**
|
| 211 |
* Creates new link items for the vocabulary
|
| 212 |
*
|
| 213 |
* @param $vid
|
| 214 |
*/
|
| 215 |
function _taxonomy_menu_insert_link_items($vid) {
|
| 216 |
$menu_name = variable_get('taxonomy_menu_vocab_menu_'. $vid, FALSE);
|
| 217 |
|
| 218 |
//check to see if we should had a vocab item
|
| 219 |
if (variable_get('taxonomy_menu_voc_item_'. $vid, FALSE)) {
|
| 220 |
$args = array(
|
| 221 |
'vid' => $vid,
|
| 222 |
'menu_name' => $menu_name,
|
| 223 |
);
|
| 224 |
|
| 225 |
$mlid = taxonomy_menu_handler('insert', $args);
|
| 226 |
}
|
| 227 |
//cycle through terms for the vocab
|
| 228 |
foreach (taxonomy_get_tree($vid) as $term) {
|
| 229 |
$args = array(
|
| 230 |
'term' => $term,
|
| 231 |
'menu_name' => $menu_name,
|
| 232 |
);
|
| 233 |
|
| 234 |
$mlid = taxonomy_menu_handler('insert', $args);
|
| 235 |
}
|
| 236 |
}
|
| 237 |
/**
|
| 238 |
* Implementation of hook_taxonomy().
|
| 239 |
*
|
| 240 |
* When a user inserts, alters or deletes taxonomy terms, we can keep
|
| 241 |
* the related menu synchronised to the changes without rebuilding the entire
|
| 242 |
* menu (which would delete all other customisations the user may have done).
|
| 243 |
*/
|
| 244 |
function taxonomy_menu_taxonomy($op, $type, $args = NULL) {
|
| 245 |
//if submiting vocab, set new preferences
|
| 246 |
|
| 247 |
if ($type == 'vocabulary') {
|
| 248 |
if ($op == 'delete') {
|
| 249 |
//delete the menu items
|
| 250 |
_taxonomy_menu_delete_all($vid);
|
| 251 |
$menu_name = variable_get('taxonomy_menu_vocab_menu_'. $args['vid'], 0);
|
| 252 |
menu_cache_clear($menu_name);
|
| 253 |
}
|
| 254 |
}
|
| 255 |
else {
|
| 256 |
// only sync if taxonomy_menu is enabled for this vocab and the 'sync'
|
| 257 |
// option has been checked.
|
| 258 |
$menu_name = variable_get('taxonomy_menu_vocab_menu_'. $args['vid'], 0);
|
| 259 |
$sync = variable_get('taxonomy_menu_sync_'. $args['vid'], 0);
|
| 260 |
|
| 261 |
if ($type == 'term' && $menu_name && $sync) {
|
| 262 |
//build arguments
|
| 263 |
switch ($op) {
|
| 264 |
case 'insert':
|
| 265 |
//we have to pull from the args because using a taxonomy function pulls from the cache
|
| 266 |
$term->name = $args['name'];
|
| 267 |
$term->description = $args['description'];
|
| 268 |
$term->parents = $args['parent'];
|
| 269 |
$term->weight = $args['weight'];
|
| 270 |
$term->vid = $args['vid'];
|
| 271 |
$term->tid = $args['tid'];
|
| 272 |
$item = array(
|
| 273 |
'term' => $term,
|
| 274 |
'menu_name' => $menu_name,
|
| 275 |
);
|
| 276 |
$message = "Term '@term' has been added to taxonomy menu '@menuname'";
|
| 277 |
break;
|
| 278 |
|
| 279 |
case 'update':
|
| 280 |
//we have to pull from the args because using a taxonomy function pulls from the cache
|
| 281 |
$term->name = $args['name'];
|
| 282 |
$term->description = $args['description'];
|
| 283 |
$term->parents = $args['parent'];
|
| 284 |
$term->weight = $args['weight'];
|
| 285 |
$term->vid = $args['vid'];
|
| 286 |
$term->tid = $args['tid'];
|
| 287 |
$item = array(
|
| 288 |
'term' => $term,
|
| 289 |
'menu_name' => $menu_name,
|
| 290 |
'mlid' => _taxonomy_menu_get_mlid($args['tid'], $args['vid']),
|
| 291 |
);
|
| 292 |
$message = "Term '@term' has been updated in taxonomy menu '@menuname'";
|
| 293 |
break;
|
| 294 |
|
| 295 |
case 'delete':
|
| 296 |
$item = array(
|
| 297 |
'tid' => taxonomy_get_term($args['tid']),
|
| 298 |
'mlid' => _taxonomy_menu_get_mlid($args['tid'], $args['vid']),
|
| 299 |
);
|
| 300 |
$message = "Term '@term' has been deleted from taxonomy menu '@menuname'";
|
| 301 |
break;
|
| 302 |
}
|
| 303 |
// run function
|
| 304 |
taxonomy_menu_handler($op, $item);
|
| 305 |
// report status
|
| 306 |
$message = t($message, array('@term' => $args['name'], '@menuname' => $menu_name));
|
| 307 |
drupal_set_message($message, 'status');
|
| 308 |
|
| 309 |
// rebuild the menu
|
| 310 |
menu_cache_clear($menu_name);
|
| 311 |
}
|
| 312 |
}
|
| 313 |
}
|
| 314 |
|
| 315 |
/**
|
| 316 |
* Implementation of hook_nodeapi().
|
| 317 |
*
|
| 318 |
* This hook enables the menu to be displayed in context during node views.
|
| 319 |
*/
|
| 320 |
function taxonomy_menu_nodeapi(&$node, $op, $a3, $a4) {
|
| 321 |
static $terms_old;
|
| 322 |
|
| 323 |
//if display numbers is on, update the menu item with the new name
|
| 324 |
if ($op == 'update' || $op == 'insert' || $op == 'delete') {
|
| 325 |
$terms_new = _taxonomy_menu_get_node_terms($node->nid);
|
| 326 |
|
| 327 |
//merge current terms and previous terms to update both menu items.
|
| 328 |
$terms = array_unique(array_merge((array)$terms_new, (array)$terms_old));
|
| 329 |
foreach ($terms as $key => $tid) {
|
| 330 |
$term = taxonomy_get_term($tid);
|
| 331 |
//update the menu for each term if necessary
|
| 332 |
$menu_name = variable_get('taxonomy_menu_vocab_menu_'. $term->vid, FALSE);
|
| 333 |
$vocb_sync = variable_get('taxonomy_menu_sync_'. $term->vid, TRUE);
|
| 334 |
$menu_num = variable_get('taxonomy_menu_display_num_' , $term->vid, FALSE);
|
| 335 |
|
| 336 |
if ($menu_name && $vocb_sync && $menu_num) {
|
| 337 |
switch ($op) {
|
| 338 |
case 'update':
|
| 339 |
//build argument array to save menu_item
|
| 340 |
$args = array(
|
| 341 |
'term' => $term,
|
| 342 |
'menu_name' => $menu_name,
|
| 343 |
'mlid' => _taxonomy_menu_get_mlid($term->tid, $term->vid),
|
| 344 |
);
|
| 345 |
//since taxonomy_get_term does not return the parents, fetch them now
|
| 346 |
$args['term']->parents = _taxonomy_menu_get_parents($term->tid);
|
| 347 |
break;
|
| 348 |
|
| 349 |
case 'insert':
|
| 350 |
//build argument array to save menu_item
|
| 351 |
$args = array(
|
| 352 |
'term' => $term,
|
| 353 |
'menu_name' => $menu_name,
|
| 354 |
'mlid' => _taxonomy_menu_get_mlid($term->tid, $term->vid),
|
| 355 |
);
|
| 356 |
//since taxonomy_get_term does not return the parents, fetch them now
|
| 357 |
$args['term']->parents = _taxonomy_menu_get_parents($term->tid);
|
| 358 |
break;
|
| 359 |
|
| 360 |
case 'delete':
|
| 361 |
$args = array(
|
| 362 |
'tid' => $term->tid,
|
| 363 |
'mlid' => _taxonomy_menu_get_mlid($term->tid, $term->vid),
|
| 364 |
);
|
| 365 |
}
|
| 366 |
taxonomy_menu_handler($op, $args);
|
| 367 |
}
|
| 368 |
}
|
| 369 |
}
|
| 370 |
elseif ($op == 'presave') {
|
| 371 |
//get the terms from the database before the changes are made.
|
| 372 |
//these will be used to update the menu item's name if needed
|
| 373 |
//we go directly to the db to bypass any caches
|
| 374 |
$terms_old = _taxonomy_menu_get_node_terms($node->nid);
|
| 375 |
}
|
| 376 |
}
|
| 377 |
|
| 378 |
/**
|
| 379 |
* HANDLING
|
| 380 |
*
|
| 381 |
* @param $op
|
| 382 |
* options are 'insert', 'update', 'delete' or path
|
| 383 |
*
|
| 384 |
* @param $args
|
| 385 |
* if $op == 'insert' then
|
| 386 |
* array with the following key/value pairs:
|
| 387 |
* 'term' => term object,
|
| 388 |
* 'menu_name' => menu that the item is set to apply to
|
| 389 |
* if $op == 'delete' then
|
| 390 |
* array(
|
| 391 |
* 'tid' => TermID
|
| 392 |
* 'mlid => Menu ID
|
| 393 |
* )
|
| 394 |
* if $op == 'update' then
|
| 395 |
* 'term' => term object,
|
| 396 |
* 'menu_name' => menu that the item is set to apply to
|
| 397 |
* 'mlid' => Menu ID
|
| 398 |
*/
|
| 399 |
function taxonomy_menu_handler($op, $args = array(), $item = array()) {
|
| 400 |
|
| 401 |
//get the initial $item
|
| 402 |
if (empty($item)) {
|
| 403 |
$item = _taxonomy_menu_create_item($args);
|
| 404 |
}
|
| 405 |
|
| 406 |
//let other modules make edits
|
| 407 |
$item = module_invoke_all('taxonomy_menu_'. $op, $item);
|
| 408 |
|
| 409 |
//update the menu and return the mlid if the remove element is not true
|
| 410 |
// if (!$item['remove']) {
|
| 411 |
return _taxonomy_menu_save($item);
|
| 412 |
// }
|
| 413 |
}
|
| 414 |
|
| 415 |
/**
|
| 416 |
* Add/Update a taxonomy menu item.
|
| 417 |
*
|
| 418 |
* We use a custom data array $item as a parameter, instead of using a
|
| 419 |
* standard taxonomy $term object. This is because this function is also
|
| 420 |
* called from hook_taxonomy(), which doesn't have a $term object. Rather
|
| 421 |
* have one consistent method of passing the data.
|
| 422 |
*
|
| 423 |
* @param $item
|
| 424 |
* array with the following key/value pairs:
|
| 425 |
* 'tid' => the term id (if 0 then adding the vocab as an item)
|
| 426 |
* 'name' => the term's name
|
| 427 |
* 'description' => term description, used as to build the title attribute
|
| 428 |
* 'weight' => term weight
|
| 429 |
* 'vid' => the vocabulary's id
|
| 430 |
* 'ptid' => the term's parent's term id
|
| 431 |
* 'menu_name' => the menu that the link item will be inserted into
|
| 432 |
* 'mlid' => if this is filled in then the mlid will be updated
|
| 433 |
*/
|
| 434 |
function _taxonomy_menu_save($item) {
|
| 435 |
$insert = TRUE;
|
| 436 |
|
| 437 |
//creat the path.
|
| 438 |
//use url to create he inital path
|
| 439 |
//we need to remove the first '/' so menu_link_save will work correctly
|
| 440 |
$path = taxonomy_menu_create_path($item['vid'], $item['tid']);
|
| 441 |
|
| 442 |
$link = array(
|
| 443 |
'link_title' => t($item['name']),
|
| 444 |
'menu_name' => $item['menu_name'],
|
| 445 |
'plid' => _taxonomy_menu_get_mlid($item['ptid'], $item['vid']),
|
| 446 |
'options' => array('attributes' => array('title' => t($item['description']))),
|
| 447 |
'weight' => $item['weight'],
|
| 448 |
'module' => 'taxonomy_menu',
|
| 449 |
'expanded' => variable_get('taxonomy_menu_expanded_'. $item['vid'], TRUE),
|
| 450 |
'link_path' => $path,
|
| 451 |
);
|
| 452 |
|
| 453 |
//if passed a mlid then add it
|
| 454 |
if ($item['mlid']) {
|
| 455 |
$link['mlid'] = $item['mlid'];
|
| 456 |
$insert = FALSE;
|
| 457 |
}
|
| 458 |
|
| 459 |
//set the has_children property
|
| 460 |
//if tid=0 then adding a vocab item and had children
|
| 461 |
//if the term has any children then set it to true
|
| 462 |
if ($item['tid'] == 0) {
|
| 463 |
$link['has_children'] = 1;
|
| 464 |
}
|
| 465 |
else {
|
| 466 |
$children = taxonomy_get_children($item['tid']);
|
| 467 |
if (!empty($children)) {
|
| 468 |
$link['has_children'] = 1;
|
| 469 |
}
|
| 470 |
}
|
| 471 |
|
| 472 |
//if remove is true then set hidden to 1
|
| 473 |
if ($item['remove']) {
|
| 474 |
$link['hidden'] = 1;
|
| 475 |
}
|
| 476 |
//save the menu item
|
| 477 |
if ($mlid = menu_link_save($link)) {
|
| 478 |
//if inserting a new menu item then insert a record into the table
|
| 479 |
if ($insert) {
|
| 480 |
_taxonomy_menu_insert_menu_item($mlid, $item['tid'], $item['vid']);
|
| 481 |
}
|
| 482 |
return $mlid;
|
| 483 |
}
|
| 484 |
else {
|
| 485 |
drupal_set_message(t('Could not save the menu link for the taxonomy menu'), 'error');
|
| 486 |
return FALSE;
|
| 487 |
}
|
| 488 |
}
|
| 489 |
|
| 490 |
/**
|
| 491 |
* Create the path to use in the menu item
|
| 492 |
*
|
| 493 |
* @return array
|
| 494 |
* path selections
|
| 495 |
*/
|
| 496 |
function _taxonomy_menu_get_paths() {
|
| 497 |
return module_invoke_all('taxonomy_menu_path');
|
| 498 |
}
|
| 499 |
|
| 500 |
/**
|
| 501 |
* Create the path for the vid/tid combination.
|
| 502 |
*
|
| 503 |
* @param $vid
|
| 504 |
* @param $tid
|
| 505 |
* @return string
|
| 506 |
* path
|
| 507 |
*/
|
| 508 |
function taxonomy_menu_create_path($vid, $tid) {
|
| 509 |
//get the path function for this vocabulary
|
| 510 |
$function = variable_get('taxonomy_menu_path_'. $vid, 'taxonomy_menu_path_default');
|
| 511 |
//run the function
|
| 512 |
return $function($vid, $tid);
|
| 513 |
}
|
| 514 |
|
| 515 |
/**
|
| 516 |
* hook_taxonomy_menu_path. Invoked from _taxonomy_menu_get_paths.
|
| 517 |
*
|
| 518 |
* @return array
|
| 519 |
* function name => Display Title
|
| 520 |
* a list of the path options.
|
| 521 |
*/
|
| 522 |
function taxonomy_menu_taxonomy_menu_path() {
|
| 523 |
$output = array(
|
| 524 |
'taxonomy_menu_path_default' => t('Default'),
|
| 525 |
);
|
| 526 |
|
| 527 |
return $output;
|
| 528 |
}
|
| 529 |
|
| 530 |
/**
|
| 531 |
* Callback for hook_taxonomy_menu_path
|
| 532 |
*/
|
| 533 |
function taxonomy_menu_path_default($vid, $tid) {
|
| 534 |
//if tid = 0 then we are creating the vocab menu item format will be taxonomy/term/$tid+$tid+$tid....
|
| 535 |
if ($tid == 0) {
|
| 536 |
//get all of the terms for the vocab
|
| 537 |
$vtids = _taxonomy_menu_get_terms($vid);
|
| 538 |
$end = implode(' ', $vtids);
|
| 539 |
$path = "taxonomy/term/$end";
|
| 540 |
}
|
| 541 |
else {
|
| 542 |
$path = taxonomy_term_path(taxonomy_get_term($tid));
|
| 543 |
if (variable_get('taxonomy_menu_display_descendants_'. $vid, FALSE)) {
|
| 544 |
//we wait to run this instead of durning the if above
|
| 545 |
//because we only wan to run it once.
|
| 546 |
$terms = taxonomy_get_tree($vid, $tid);
|
| 547 |
foreach ($terms as $term) {
|
| 548 |
$tids[] = $term->tid;
|
| 549 |
}
|
| 550 |
if ($tids) {
|
| 551 |
$end = implode(' ', $tids);
|
| 552 |
$path .= ' '. $end;
|
| 553 |
}
|
| 554 |
}
|
| 555 |
}
|
| 556 |
|
| 557 |
return $path;
|
| 558 |
}
|
| 559 |
|
| 560 |
/**
|
| 561 |
* hook_taxonomy_menu_delete
|
| 562 |
*
|
| 563 |
* @param $args
|
| 564 |
* array(
|
| 565 |
* 'vid' => Vocab ID
|
| 566 |
* 'tid' => TermID
|
| 567 |
* 'mlid' => Menu ID
|
| 568 |
* )
|
| 569 |
*
|
| 570 |
*/
|
| 571 |
function taxonomy_menu_taxonomy_menu_delete($args = array()) {
|
| 572 |
menu_link_delete($args['mlid']);
|
| 573 |
_taxonomy_menu_delete_item($args['vid'], $args['tid']);
|
| 574 |
}
|
| 575 |
|
| 576 |
/**
|
| 577 |
* Create the inital $item array
|
| 578 |
*
|
| 579 |
* @param $args
|
| 580 |
* array with the following key/value pairs:
|
| 581 |
* 'term' => term object, if updating a term
|
| 582 |
* 'menu_name' => menu that the item is set to apply to
|
| 583 |
* 'vid' => vocab id. if editing vocab item
|
| 584 |
* 'mlid' => menu id
|
| 585 |
*
|
| 586 |
* @param $item
|
| 587 |
* array with the following key/value pairs:
|
| 588 |
* 'tid' => the term id (if 0 then updating the vocab as an item)
|
| 589 |
* 'name' => new menu name
|
| 590 |
* 'description' => new menu description, used as to build the title attribute
|
| 591 |
* 'weight' => new menu weight
|
| 592 |
* 'vid' => the new vocabulary's id
|
| 593 |
* 'ptid' => the new parent tid
|
| 594 |
* 'mlid' => mlid that needs to be edited
|
| 595 |
* 'path_type' => either term, tid or vid. This is what will be pased to the path function This must be a key of the array also.
|
| 596 |
*/
|
| 597 |
function _taxonomy_menu_create_item($args = array()) {
|
| 598 |
|
| 599 |
//if tid = 0 then we are creating a vocab item
|
| 600 |
if ($args['tid'] == 0 && variable_get('taxonomy_menu_voc_item_'. $args['vid'], 0)) {
|
| 601 |
|
| 602 |
$vocab = taxonomy_vocabulary_load($args['vid']);
|
| 603 |
$item = array(
|
| 604 |
'tid' => 0,
|
| 605 |
'name' => $vocab->name,
|
| 606 |
'description' => $vocab->description,
|
| 607 |
'weight' => $vocab->weight,
|
| 608 |
'vid' => $args['vid'],
|
| 609 |
'ptid' => 0,
|
| 610 |
'menu_name' => $args['menu_name'],
|
| 611 |
);
|
| 612 |
|
| 613 |
return $item;
|
| 614 |
}
|
| 615 |
else {
|
| 616 |
$term = $args['term'];
|
| 617 |
}
|
| 618 |
|
| 619 |
// get the first parent
|
| 620 |
if (is_array($term->parents)) {
|
| 621 |
foreach ($term->parents as $key => $val) {
|
| 622 |
$ptid = $val;
|
| 623 |
break;
|
| 624 |
}
|
| 625 |
}
|
| 626 |
else {
|
| 627 |
$ptid = $term->parents;
|
| 628 |
}
|
| 629 |
|
| 630 |
//if ptid is empty, then set it to 0
|
| 631 |
if (empty($ptid)) {
|
| 632 |
$ptid = 0;
|
| 633 |
}
|
| 634 |
|
| 635 |
// turn the term into the correct $item array form
|
| 636 |
$item = array(
|
| 637 |
'tid' => $term->tid,
|
| 638 |
'name' => t($term->name),
|
| 639 |
'description' => t($term->description),
|
| 640 |
'weight' => $term->weight,
|
| 641 |
'vid' => $term->vid,
|
| 642 |
'ptid' => $ptid,
|
| 643 |
'menu_name' => $args['menu_name'],
|
| 644 |
);
|
| 645 |
|
| 646 |
if ($args['mlid']) {
|
| 647 |
$item['mlid'] = $args['mlid'];
|
| 648 |
}
|
| 649 |
|
| 650 |
return $item;
|
| 651 |
}
|
| 652 |
|
| 653 |
/**
|
| 654 |
* Helper function to see if any of the children have any nodes
|
| 655 |
*
|
| 656 |
* @param $tid
|
| 657 |
* @param $vid
|
| 658 |
* @return boolean
|
| 659 |
*/
|
| 660 |
function _taxonomy_menu_children_has_nodes($tid, $vid) {
|
| 661 |
$children = taxonomy_get_children($tid, $vid);
|
| 662 |
foreach ($children as $tid => $term) {
|
| 663 |
if (_taxonomy_menu_term_count($tid) > 0) {
|
| 664 |
return FALSE;
|
| 665 |
}
|
| 666 |
}
|
| 667 |
return TRUE;
|
| 668 |
}
|
| 669 |
|
| 670 |
/**
|
| 671 |
* Helper function for insert and update hooks
|
| 672 |
* @param $item
|
| 673 |
* @return unknown_type
|
| 674 |
*/
|
| 675 |
function _taxonomy_menu_item($item) {
|
| 676 |
//if tid is 0 then do not chagne any settings
|
| 677 |
if ($item['tid'] > 0) {
|
| 678 |
//get the number of node attached to this term
|
| 679 |
$num = _taxonomy_menu_term_count($item['tid']);
|
| 680 |
|
| 681 |
//if hide menu is selected and the term count is 0 and the term has no children then do not create the menu item
|
| 682 |
if ($num == 0 &&
|
| 683 |
variable_get('taxonomy_menu_hide_empty_terms_'. $item['vid'], FALSE) &&
|
| 684 |
_taxonomy_menu_children_has_nodes($item['tid'], $item['vid'])) {
|
| 685 |
|
| 686 |
$item['remove'] = TRUE;
|
| 687 |
return $item;
|
| 688 |
}
|
| 689 |
|
| 690 |
//if display number is selected and $num > 0 then change the title
|
| 691 |
if (variable_get('taxonomy_menu_display_num_'. $item['vid'], FALSE)) {
|
| 692 |
//if number > 0 and display decendants, then count all of the children
|
| 693 |
if (variable_get('taxonomy_menu_display_descendants_'. $item['vid'], FALSE)) {
|
| 694 |
$num = taxonomy_term_count_nodes($item['tid']);
|
| 695 |
}
|
| 696 |
$item['name'] .= " ($num)";
|
| 697 |
}
|
| 698 |
}
|
| 699 |
|
| 700 |
return $item;
|
| 701 |
}
|
| 702 |
|
| 703 |
/**
|
| 704 |
* Implementation of hook_taxonomy_menu_insert().
|
| 705 |
*
|
| 706 |
* @param $item
|
| 707 |
* array with the following key/value pairs:
|
| 708 |
* 'tid' => the term id (if 0 then updating the vocab as an item)
|
| 709 |
* 'name' => new menu name
|
| 710 |
* 'description' => new menu description, used as to build the title attribute
|
| 711 |
* 'weight' => new menu weight
|
| 712 |
* 'vid' => the new vocabulary's id
|
| 713 |
* 'ptid' => the new parent tid
|
| 714 |
* 'remove' => if this is set to TRUE then the $item is not added as a menu
|
| 715 |
*
|
| 716 |
* @return $item
|
| 717 |
*/
|
| 718 |
function taxonomy_menu_taxonomy_menu_insert($item) {
|
| 719 |
return _taxonomy_menu_item($item);
|
| 720 |
}
|
| 721 |
|
| 722 |
/**
|
| 723 |
* Implementation of hook_taxonomy_menu_update().
|
| 724 |
*
|
| 725 |
* @param $item
|
| 726 |
* array with the following key/value pairs:
|
| 727 |
* 'tid' => the term id (if 0 then updating the vocab as an item)
|
| 728 |
* 'name' => new menu name
|
| 729 |
* 'description' => new menu description, used as to build the title attribute
|
| 730 |
* 'weight' => new menu weight
|
| 731 |
* 'vid' => the new vocabulary's id
|
| 732 |
* 'ptid' => the new parent tid
|
| 733 |
* 'mlid' => mlid that needs to be edited
|
| 734 |
* 'remove' => if this is set to TRUE then the $item is not added as a menu
|
| 735 |
*
|
| 736 |
* @return $item
|
| 737 |
*/
|
| 738 |
function taxonomy_menu_taxonomy_menu_update($item) {
|
| 739 |
return _taxonomy_menu_item($item);
|
| 740 |
}
|
| 741 |
|
| 742 |
/**
|
| 743 |
* Used to create a form array of taxonomy menu options
|
| 744 |
* invokes hook_taxonomy_menu_options().
|
| 745 |
*
|
| 746 |
* @return $form array
|
| 747 |
*/
|
| 748 |
function _taxonomy_menu_create_options($vid) {
|
| 749 |
$options = module_invoke_all('taxonomy_menu_options');
|
| 750 |
|
| 751 |
//cycle through field
|
| 752 |
foreach ($options as $field_name => $field_elements) {
|
| 753 |
//cycle through each value of the field
|
| 754 |
$variable_name = _taxonomy_menu_build_variable($field_name, $vid);
|
| 755 |
|
| 756 |
//if the variable is set then use that, if the default key is set then use that, otherwise use false
|
| 757 |
$options[$field_name]['#default_value'] =
|
| 758 |
variable_get($variable_name,
|
| 759 |
!empty($options[$field_name]['default']) ? $options[$field_name]['default'] : FALSE);
|
| 760 |
|
| 761 |
//set the type to checkbox if it is empty
|
| 762 |
if (empty($options[$field_name]['#type'])) {
|
| 763 |
$options[$field_name]['#type'] = 'checkbox';
|
| 764 |
}
|
| 765 |
|
| 766 |
//set the option feildset values
|
| 767 |
$options['#type'] = 'fieldset';
|
| 768 |
$options['#title'] = t('Options');
|
| 769 |
$options['#collapsible'] = TRUE;
|
| 770 |
|
| 771 |
//remove the default value from the array so we don't pass it to the form
|
| 772 |
unset($options[$field_name]['default']);
|
| 773 |
}
|
| 774 |
|
| 775 |
return $options;
|
| 776 |
}
|
| 777 |
|
| 778 |
function _taxonomy_menu_build_variable($name, $vid) {
|
| 779 |
$base_string = "taxonomy_menu_%s_$vid";
|
| 780 |
|
| 781 |
return sprintf($base_string, $name);
|
| 782 |
}
|
| 783 |
|
| 784 |
/**
|
| 785 |
* Implementation of hook_taxonomy_menu_options().
|
| 786 |
*
|
| 787 |
* @return array
|
| 788 |
* Uses the value to set the variable taxonomy_menu_<value>_$vid
|
| 789 |
* $options[value]
|
| 790 |
* default - optional. this is what will be used if the varialbe is not set. if empty then FALSE is used
|
| 791 |
* #title - required.
|
| 792 |
* any other form element
|
| 793 |
*/
|
| 794 |
function taxonomy_menu_taxonomy_menu_options() {
|
| 795 |
|
| 796 |
$options['sync'] = array(
|
| 797 |
'#title' => t('Syncronise changes to this vocabulary'),
|
| 798 |
'#description' => t('Every time a term is added/deleted/modified, the corresponding menu link will be altered too.'),
|
| 799 |
'default' => TRUE,
|
| 800 |
);
|
| 801 |
|
| 802 |
$options['display_num'] = array(
|
| 803 |
'#title' => t('Display Number of Nodes'),
|
| 804 |
'#description' => t('Display the number of Items per taxonomy Terms. Will not show up for vocabulary menu items.'),
|
| 805 |
'default' => FALSE,
|
| 806 |
);
|
| 807 |
|
| 808 |
$options['hide_empty_terms'] = array(
|
| 809 |
'#title' => t('Hide Empty Terms'),
|
| 810 |
'#description' => t('Hide terms with no nodes attached to them.'),
|
| 811 |
'default' => FALSE,
|
| 812 |
);
|
| 813 |
|
| 814 |
$options['voc_item'] = array(
|
| 815 |
'#title' => t('Item for Vocabulary'),
|
| 816 |
'#description' => t("Shall the vocabulary have it's own item."),
|
| 817 |
'default' => TRUE,
|
| 818 |
);
|
| 819 |
|
| 820 |
$options['expanded'] = array(
|
| 821 |
'#title' => t('Auto Expand Menu Item'),
|
| 822 |
'#description' => t('Sets the expand setting to TRUE'),
|
| 823 |
'default' => TRUE,
|
| 824 |
);
|
| 825 |
|
| 826 |
$options['display_descendants'] = array(
|
| 827 |
'#title' => t('Display Descendants'),
|
| 828 |
'default' => FALSE,
|
| 829 |
);
|
| 830 |
|
| 831 |
return $options;
|
| 832 |
}
|