annotate includes/menu.inc @ 8:85cbd6048071

Added tag 6.3 for changeset fff6d4c8c043
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:30:28 +0100
parents fff6d4c8c043
children 589fb7c02327
rev   line source
webmaster@1 1 <?php
webmaster@7 2 // $Id: menu.inc,v 1.255.2.17 2008/07/09 15:23:50 goba Exp $
webmaster@1 3
webmaster@1 4 /**
webmaster@1 5 * @file
webmaster@1 6 * API for the Drupal menu system.
webmaster@1 7 */
webmaster@1 8
webmaster@1 9 /**
webmaster@1 10 * @defgroup menu Menu system
webmaster@1 11 * @{
webmaster@1 12 * Define the navigation menus, and route page requests to code based on URLs.
webmaster@1 13 *
webmaster@1 14 * The Drupal menu system drives both the navigation system from a user
webmaster@1 15 * perspective and the callback system that Drupal uses to respond to URLs
webmaster@1 16 * passed from the browser. For this reason, a good understanding of the
webmaster@1 17 * menu system is fundamental to the creation of complex modules.
webmaster@1 18 *
webmaster@1 19 * Drupal's menu system follows a simple hierarchy defined by paths.
webmaster@1 20 * Implementations of hook_menu() define menu items and assign them to
webmaster@1 21 * paths (which should be unique). The menu system aggregates these items
webmaster@1 22 * and determines the menu hierarchy from the paths. For example, if the
webmaster@1 23 * paths defined were a, a/b, e, a/b/c/d, f/g, and a/b/h, the menu system
webmaster@1 24 * would form the structure:
webmaster@1 25 * - a
webmaster@1 26 * - a/b
webmaster@1 27 * - a/b/c/d
webmaster@1 28 * - a/b/h
webmaster@1 29 * - e
webmaster@1 30 * - f/g
webmaster@1 31 * Note that the number of elements in the path does not necessarily
webmaster@1 32 * determine the depth of the menu item in the tree.
webmaster@1 33 *
webmaster@1 34 * When responding to a page request, the menu system looks to see if the
webmaster@1 35 * path requested by the browser is registered as a menu item with a
webmaster@1 36 * callback. If not, the system searches up the menu tree for the most
webmaster@1 37 * complete match with a callback it can find. If the path a/b/i is
webmaster@1 38 * requested in the tree above, the callback for a/b would be used.
webmaster@1 39 *
webmaster@1 40 * The found callback function is called with any arguments specified
webmaster@1 41 * in the "page arguments" attribute of its menu item. The
webmaster@1 42 * attribute must be an array. After these arguments, any remaining
webmaster@1 43 * components of the path are appended as further arguments. In this
webmaster@1 44 * way, the callback for a/b above could respond to a request for
webmaster@1 45 * a/b/i differently than a request for a/b/j.
webmaster@1 46 *
webmaster@1 47 * For an illustration of this process, see page_example.module.
webmaster@1 48 *
webmaster@1 49 * Access to the callback functions is also protected by the menu system.
webmaster@1 50 * The "access callback" with an optional "access arguments" of each menu
webmaster@1 51 * item is called before the page callback proceeds. If this returns TRUE,
webmaster@1 52 * then access is granted; if FALSE, then access is denied. Menu items may
webmaster@1 53 * omit this attribute to use the value provided by an ancestor item.
webmaster@1 54 *
webmaster@1 55 * In the default Drupal interface, you will notice many links rendered as
webmaster@1 56 * tabs. These are known in the menu system as "local tasks", and they are
webmaster@1 57 * rendered as tabs by default, though other presentations are possible.
webmaster@1 58 * Local tasks function just as other menu items in most respects. It is
webmaster@1 59 * convention that the names of these tasks should be short verbs if
webmaster@1 60 * possible. In addition, a "default" local task should be provided for
webmaster@1 61 * each set. When visiting a local task's parent menu item, the default
webmaster@1 62 * local task will be rendered as if it is selected; this provides for a
webmaster@1 63 * normal tab user experience. This default task is special in that it
webmaster@1 64 * links not to its provided path, but to its parent item's path instead.
webmaster@1 65 * The default task's path is only used to place it appropriately in the
webmaster@1 66 * menu hierarchy.
webmaster@1 67 *
webmaster@1 68 * Everything described so far is stored in the menu_router table. The
webmaster@1 69 * menu_links table holds the visible menu links. By default these are
webmaster@1 70 * derived from the same hook_menu definitions, however you are free to
webmaster@1 71 * add more with menu_link_save().
webmaster@1 72 */
webmaster@1 73
webmaster@1 74 /**
webmaster@1 75 * @name Menu flags
webmaster@1 76 * @{
webmaster@1 77 * Flags for use in the "type" attribute of menu items.
webmaster@1 78 */
webmaster@1 79
webmaster@1 80 define('MENU_IS_ROOT', 0x0001);
webmaster@1 81 define('MENU_VISIBLE_IN_TREE', 0x0002);
webmaster@1 82 define('MENU_VISIBLE_IN_BREADCRUMB', 0x0004);
webmaster@1 83 define('MENU_LINKS_TO_PARENT', 0x0008);
webmaster@1 84 define('MENU_MODIFIED_BY_ADMIN', 0x0020);
webmaster@1 85 define('MENU_CREATED_BY_ADMIN', 0x0040);
webmaster@1 86 define('MENU_IS_LOCAL_TASK', 0x0080);
webmaster@1 87
webmaster@1 88 /**
webmaster@1 89 * @} End of "Menu flags".
webmaster@1 90 */
webmaster@1 91
webmaster@1 92 /**
webmaster@1 93 * @name Menu item types
webmaster@1 94 * @{
webmaster@1 95 * Menu item definitions provide one of these constants, which are shortcuts for
webmaster@1 96 * combinations of the above flags.
webmaster@1 97 */
webmaster@1 98
webmaster@1 99 /**
webmaster@1 100 * Normal menu items show up in the menu tree and can be moved/hidden by
webmaster@1 101 * the administrator. Use this for most menu items. It is the default value if
webmaster@1 102 * no menu item type is specified.
webmaster@1 103 */
webmaster@1 104 define('MENU_NORMAL_ITEM', MENU_VISIBLE_IN_TREE | MENU_VISIBLE_IN_BREADCRUMB);
webmaster@1 105
webmaster@1 106 /**
webmaster@1 107 * Callbacks simply register a path so that the correct function is fired
webmaster@1 108 * when the URL is accessed. They are not shown in the menu.
webmaster@1 109 */
webmaster@1 110 define('MENU_CALLBACK', MENU_VISIBLE_IN_BREADCRUMB);
webmaster@1 111
webmaster@1 112 /**
webmaster@1 113 * Modules may "suggest" menu items that the administrator may enable. They act
webmaster@1 114 * just as callbacks do until enabled, at which time they act like normal items.
webmaster@1 115 * Note for the value: 0x0010 was a flag which is no longer used, but this way
webmaster@1 116 * the values of MENU_CALLBACK and MENU_SUGGESTED_ITEM are separate.
webmaster@1 117 */
webmaster@1 118 define('MENU_SUGGESTED_ITEM', MENU_VISIBLE_IN_BREADCRUMB | 0x0010);
webmaster@1 119
webmaster@1 120 /**
webmaster@1 121 * Local tasks are rendered as tabs by default. Use this for menu items that
webmaster@1 122 * describe actions to be performed on their parent item. An example is the path
webmaster@1 123 * "node/52/edit", which performs the "edit" task on "node/52".
webmaster@1 124 */
webmaster@1 125 define('MENU_LOCAL_TASK', MENU_IS_LOCAL_TASK);
webmaster@1 126
webmaster@1 127 /**
webmaster@1 128 * Every set of local tasks should provide one "default" task, that links to the
webmaster@1 129 * same path as its parent when clicked.
webmaster@1 130 */
webmaster@1 131 define('MENU_DEFAULT_LOCAL_TASK', MENU_IS_LOCAL_TASK | MENU_LINKS_TO_PARENT);
webmaster@1 132
webmaster@1 133 /**
webmaster@1 134 * @} End of "Menu item types".
webmaster@1 135 */
webmaster@1 136
webmaster@1 137 /**
webmaster@1 138 * @name Menu status codes
webmaster@1 139 * @{
webmaster@1 140 * Status codes for menu callbacks.
webmaster@1 141 */
webmaster@1 142
webmaster@1 143 define('MENU_FOUND', 1);
webmaster@1 144 define('MENU_NOT_FOUND', 2);
webmaster@1 145 define('MENU_ACCESS_DENIED', 3);
webmaster@1 146 define('MENU_SITE_OFFLINE', 4);
webmaster@1 147
webmaster@1 148 /**
webmaster@1 149 * @} End of "Menu status codes".
webmaster@1 150 */
webmaster@1 151
webmaster@1 152 /**
webmaster@1 153 * @Name Menu tree parameters
webmaster@1 154 * @{
webmaster@1 155 * Menu tree
webmaster@1 156 */
webmaster@1 157
webmaster@1 158 /**
webmaster@1 159 * The maximum number of path elements for a menu callback
webmaster@1 160 */
webmaster@1 161 define('MENU_MAX_PARTS', 7);
webmaster@1 162
webmaster@1 163
webmaster@1 164 /**
webmaster@1 165 * The maximum depth of a menu links tree - matches the number of p columns.
webmaster@1 166 */
webmaster@1 167 define('MENU_MAX_DEPTH', 9);
webmaster@1 168
webmaster@1 169
webmaster@1 170 /**
webmaster@1 171 * @} End of "Menu tree parameters".
webmaster@1 172 */
webmaster@1 173
webmaster@1 174 /**
webmaster@1 175 * Returns the ancestors (and relevant placeholders) for any given path.
webmaster@1 176 *
webmaster@1 177 * For example, the ancestors of node/12345/edit are:
webmaster@3 178 * - node/12345/edit
webmaster@3 179 * - node/12345/%
webmaster@3 180 * - node/%/edit
webmaster@3 181 * - node/%/%
webmaster@3 182 * - node/12345
webmaster@3 183 * - node/%
webmaster@3 184 * - node
webmaster@1 185 *
webmaster@1 186 * To generate these, we will use binary numbers. Each bit represents a
webmaster@1 187 * part of the path. If the bit is 1, then it represents the original
webmaster@1 188 * value while 0 means wildcard. If the path is node/12/edit/foo
webmaster@1 189 * then the 1011 bitstring represents node/%/edit/foo where % means that
webmaster@1 190 * any argument matches that part. We limit ourselves to using binary
webmaster@1 191 * numbers that correspond the patterns of wildcards of router items that
webmaster@1 192 * actually exists. This list of 'masks' is built in menu_rebuild().
webmaster@1 193 *
webmaster@1 194 * @param $parts
webmaster@1 195 * An array of path parts, for the above example
webmaster@1 196 * array('node', '12345', 'edit').
webmaster@1 197 * @return
webmaster@1 198 * An array which contains the ancestors and placeholders. Placeholders
webmaster@1 199 * simply contain as many '%s' as the ancestors.
webmaster@1 200 */
webmaster@1 201 function menu_get_ancestors($parts) {
webmaster@1 202 $number_parts = count($parts);
webmaster@1 203 $placeholders = array();
webmaster@1 204 $ancestors = array();
webmaster@1 205 $length = $number_parts - 1;
webmaster@1 206 $end = (1 << $number_parts) - 1;
webmaster@1 207 $masks = variable_get('menu_masks', array());
webmaster@1 208 // Only examine patterns that actually exist as router items (the masks).
webmaster@1 209 foreach ($masks as $i) {
webmaster@1 210 if ($i > $end) {
webmaster@1 211 // Only look at masks that are not longer than the path of interest.
webmaster@1 212 continue;
webmaster@1 213 }
webmaster@1 214 elseif ($i < (1 << $length)) {
webmaster@1 215 // We have exhausted the masks of a given length, so decrease the length.
webmaster@1 216 --$length;
webmaster@1 217 }
webmaster@1 218 $current = '';
webmaster@1 219 for ($j = $length; $j >= 0; $j--) {
webmaster@1 220 if ($i & (1 << $j)) {
webmaster@1 221 $current .= $parts[$length - $j];
webmaster@1 222 }
webmaster@1 223 else {
webmaster@1 224 $current .= '%';
webmaster@1 225 }
webmaster@1 226 if ($j) {
webmaster@1 227 $current .= '/';
webmaster@1 228 }
webmaster@1 229 }
webmaster@1 230 $placeholders[] = "'%s'";
webmaster@1 231 $ancestors[] = $current;
webmaster@1 232 }
webmaster@1 233 return array($ancestors, $placeholders);
webmaster@1 234 }
webmaster@1 235
webmaster@1 236 /**
webmaster@1 237 * The menu system uses serialized arrays stored in the database for
webmaster@1 238 * arguments. However, often these need to change according to the
webmaster@1 239 * current path. This function unserializes such an array and does the
webmaster@1 240 * necessary change.
webmaster@1 241 *
webmaster@1 242 * Integer values are mapped according to the $map parameter. For
webmaster@1 243 * example, if unserialize($data) is array('view', 1) and $map is
webmaster@1 244 * array('node', '12345') then 'view' will not be changed because
webmaster@1 245 * it is not an integer, but 1 will as it is an integer. As $map[1]
webmaster@1 246 * is '12345', 1 will be replaced with '12345'. So the result will
webmaster@1 247 * be array('node_load', '12345').
webmaster@1 248 *
webmaster@1 249 * @param @data
webmaster@1 250 * A serialized array.
webmaster@1 251 * @param @map
webmaster@1 252 * An array of potential replacements.
webmaster@1 253 * @return
webmaster@1 254 * The $data array unserialized and mapped.
webmaster@1 255 */
webmaster@1 256 function menu_unserialize($data, $map) {
webmaster@1 257 if ($data = unserialize($data)) {
webmaster@1 258 foreach ($data as $k => $v) {
webmaster@1 259 if (is_int($v)) {
webmaster@1 260 $data[$k] = isset($map[$v]) ? $map[$v] : '';
webmaster@1 261 }
webmaster@1 262 }
webmaster@1 263 return $data;
webmaster@1 264 }
webmaster@1 265 else {
webmaster@1 266 return array();
webmaster@1 267 }
webmaster@1 268 }
webmaster@1 269
webmaster@1 270
webmaster@1 271
webmaster@1 272 /**
webmaster@1 273 * Replaces the statically cached item for a given path.
webmaster@1 274 *
webmaster@1 275 * @param $path
webmaster@1 276 * The path.
webmaster@1 277 * @param $router_item
webmaster@1 278 * The router item. Usually you take a router entry from menu_get_item and
webmaster@1 279 * set it back either modified or to a different path. This lets you modify the
webmaster@1 280 * navigation block, the page title, the breadcrumb and the page help in one
webmaster@1 281 * call.
webmaster@1 282 */
webmaster@1 283 function menu_set_item($path, $router_item) {
webmaster@1 284 menu_get_item($path, $router_item);
webmaster@1 285 }
webmaster@1 286
webmaster@1 287 /**
webmaster@1 288 * Get a router item.
webmaster@1 289 *
webmaster@1 290 * @param $path
webmaster@1 291 * The path, for example node/5. The function will find the corresponding
webmaster@1 292 * node/% item and return that.
webmaster@1 293 * @param $router_item
webmaster@1 294 * Internal use only.
webmaster@1 295 * @return
webmaster@1 296 * The router item, an associate array corresponding to one row in the
webmaster@1 297 * menu_router table. The value of key map holds the loaded objects. The
webmaster@1 298 * value of key access is TRUE if the current user can access this page.
webmaster@1 299 * The values for key title, page_arguments, access_arguments will be
webmaster@1 300 * filled in based on the database values and the objects loaded.
webmaster@1 301 */
webmaster@1 302 function menu_get_item($path = NULL, $router_item = NULL) {
webmaster@1 303 static $router_items;
webmaster@1 304 if (!isset($path)) {
webmaster@1 305 $path = $_GET['q'];
webmaster@1 306 }
webmaster@1 307 if (isset($router_item)) {
webmaster@1 308 $router_items[$path] = $router_item;
webmaster@1 309 }
webmaster@1 310 if (!isset($router_items[$path])) {
webmaster@1 311 $original_map = arg(NULL, $path);
webmaster@1 312 $parts = array_slice($original_map, 0, MENU_MAX_PARTS);
webmaster@1 313 list($ancestors, $placeholders) = menu_get_ancestors($parts);
webmaster@1 314
webmaster@1 315 if ($router_item = db_fetch_array(db_query_range('SELECT * FROM {menu_router} WHERE path IN ('. implode (',', $placeholders) .') ORDER BY fit DESC', $ancestors, 0, 1))) {
webmaster@1 316 $map = _menu_translate($router_item, $original_map);
webmaster@1 317 if ($map === FALSE) {
webmaster@1 318 $router_items[$path] = FALSE;
webmaster@1 319 return FALSE;
webmaster@1 320 }
webmaster@1 321 if ($router_item['access']) {
webmaster@1 322 $router_item['map'] = $map;
webmaster@1 323 $router_item['page_arguments'] = array_merge(menu_unserialize($router_item['page_arguments'], $map), array_slice($map, $router_item['number_parts']));
webmaster@1 324 }
webmaster@1 325 }
webmaster@1 326 $router_items[$path] = $router_item;
webmaster@1 327 }
webmaster@1 328 return $router_items[$path];
webmaster@1 329 }
webmaster@1 330
webmaster@1 331 /**
webmaster@1 332 * Execute the page callback associated with the current path
webmaster@1 333 */
webmaster@1 334 function menu_execute_active_handler($path = NULL) {
webmaster@1 335 if (_menu_site_is_offline()) {
webmaster@1 336 return MENU_SITE_OFFLINE;
webmaster@1 337 }
webmaster@1 338 if (variable_get('menu_rebuild_needed', FALSE)) {
webmaster@1 339 menu_rebuild();
webmaster@1 340 }
webmaster@1 341 if ($router_item = menu_get_item($path)) {
webmaster@1 342 if ($router_item['access']) {
webmaster@1 343 if ($router_item['file']) {
webmaster@1 344 require_once($router_item['file']);
webmaster@1 345 }
webmaster@1 346 return call_user_func_array($router_item['page_callback'], $router_item['page_arguments']);
webmaster@1 347 }
webmaster@1 348 else {
webmaster@1 349 return MENU_ACCESS_DENIED;
webmaster@1 350 }
webmaster@1 351 }
webmaster@1 352 return MENU_NOT_FOUND;
webmaster@1 353 }
webmaster@1 354
webmaster@1 355 /**
webmaster@1 356 * Loads objects into the map as defined in the $item['load_functions'].
webmaster@1 357 *
webmaster@1 358 * @param $item
webmaster@1 359 * A menu router or menu link item
webmaster@1 360 * @param $map
webmaster@1 361 * An array of path arguments (ex: array('node', '5'))
webmaster@1 362 * @return
webmaster@1 363 * Returns TRUE for success, FALSE if an object cannot be loaded.
webmaster@1 364 * Names of object loading functions are placed in $item['load_functions'].
webmaster@1 365 * Loaded objects are placed in $map[]; keys are the same as keys in the
webmaster@1 366 * $item['load_functions'] array.
webmaster@1 367 * $item['access'] is set to FALSE if an object cannot be loaded.
webmaster@1 368 */
webmaster@1 369 function _menu_load_objects(&$item, &$map) {
webmaster@1 370 if ($load_functions = $item['load_functions']) {
webmaster@1 371 // If someone calls this function twice, then unserialize will fail.
webmaster@1 372 if ($load_functions_unserialized = unserialize($load_functions)) {
webmaster@1 373 $load_functions = $load_functions_unserialized;
webmaster@1 374 }
webmaster@1 375 $path_map = $map;
webmaster@1 376 foreach ($load_functions as $index => $function) {
webmaster@1 377 if ($function) {
webmaster@1 378 $value = isset($path_map[$index]) ? $path_map[$index] : '';
webmaster@1 379 if (is_array($function)) {
webmaster@1 380 // Set up arguments for the load function. These were pulled from
webmaster@1 381 // 'load arguments' in the hook_menu() entry, but they need
webmaster@1 382 // some processing. In this case the $function is the key to the
webmaster@1 383 // load_function array, and the value is the list of arguments.
webmaster@1 384 list($function, $args) = each($function);
webmaster@1 385 $load_functions[$index] = $function;
webmaster@1 386
webmaster@1 387 // Some arguments are placeholders for dynamic items to process.
webmaster@1 388 foreach ($args as $i => $arg) {
webmaster@1 389 if ($arg === '%index') {
webmaster@1 390 // Pass on argument index to the load function, so multiple
webmaster@1 391 // occurances of the same placeholder can be identified.
webmaster@1 392 $args[$i] = $index;
webmaster@1 393 }
webmaster@1 394 if ($arg === '%map') {
webmaster@1 395 // Pass on menu map by reference. The accepting function must
webmaster@1 396 // also declare this as a reference if it wants to modify
webmaster@1 397 // the map.
webmaster@1 398 $args[$i] = &$map;
webmaster@1 399 }
webmaster@1 400 if (is_int($arg)) {
webmaster@1 401 $args[$i] = isset($path_map[$arg]) ? $path_map[$arg] : '';
webmaster@1 402 }
webmaster@1 403 }
webmaster@1 404 array_unshift($args, $value);
webmaster@1 405 $return = call_user_func_array($function, $args);
webmaster@1 406 }
webmaster@1 407 else {
webmaster@1 408 $return = $function($value);
webmaster@1 409 }
webmaster@1 410 // If callback returned an error or there is no callback, trigger 404.
webmaster@1 411 if ($return === FALSE) {
webmaster@1 412 $item['access'] = FALSE;
webmaster@1 413 $map = FALSE;
webmaster@1 414 return FALSE;
webmaster@1 415 }
webmaster@1 416 $map[$index] = $return;
webmaster@1 417 }
webmaster@1 418 }
webmaster@1 419 $item['load_functions'] = $load_functions;
webmaster@1 420 }
webmaster@1 421 return TRUE;
webmaster@1 422 }
webmaster@1 423
webmaster@1 424 /**
webmaster@1 425 * Check access to a menu item using the access callback
webmaster@1 426 *
webmaster@1 427 * @param $item
webmaster@1 428 * A menu router or menu link item
webmaster@1 429 * @param $map
webmaster@1 430 * An array of path arguments (ex: array('node', '5'))
webmaster@1 431 * @return
webmaster@1 432 * $item['access'] becomes TRUE if the item is accessible, FALSE otherwise.
webmaster@1 433 */
webmaster@1 434 function _menu_check_access(&$item, $map) {
webmaster@1 435 // Determine access callback, which will decide whether or not the current
webmaster@1 436 // user has access to this path.
webmaster@1 437 $callback = empty($item['access_callback']) ? 0 : trim($item['access_callback']);
webmaster@1 438 // Check for a TRUE or FALSE value.
webmaster@1 439 if (is_numeric($callback)) {
webmaster@1 440 $item['access'] = (bool)$callback;
webmaster@1 441 }
webmaster@1 442 else {
webmaster@1 443 $arguments = menu_unserialize($item['access_arguments'], $map);
webmaster@1 444 // As call_user_func_array is quite slow and user_access is a very common
webmaster@1 445 // callback, it is worth making a special case for it.
webmaster@1 446 if ($callback == 'user_access') {
webmaster@1 447 $item['access'] = (count($arguments) == 1) ? user_access($arguments[0]) : user_access($arguments[0], $arguments[1]);
webmaster@1 448 }
webmaster@1 449 else {
webmaster@1 450 $item['access'] = call_user_func_array($callback, $arguments);
webmaster@1 451 }
webmaster@1 452 }
webmaster@1 453 }
webmaster@1 454
webmaster@1 455 /**
webmaster@1 456 * Localize the router item title using t() or another callback.
webmaster@1 457 *
webmaster@1 458 * Translate the title and description to allow storage of English title
webmaster@1 459 * strings in the database, yet display of them in the language required
webmaster@1 460 * by the current user.
webmaster@1 461 *
webmaster@1 462 * @param $item
webmaster@1 463 * A menu router item or a menu link item.
webmaster@1 464 * @param $map
webmaster@1 465 * The path as an array with objects already replaced. E.g., for path
webmaster@1 466 * node/123 $map would be array('node', $node) where $node is the node
webmaster@1 467 * object for node 123.
webmaster@1 468 * @param $link_translate
webmaster@1 469 * TRUE if we are translating a menu link item; FALSE if we are
webmaster@1 470 * translating a menu router item.
webmaster@1 471 * @return
webmaster@1 472 * No return value.
webmaster@1 473 * $item['title'] is localized according to $item['title_callback'].
webmaster@1 474 * If an item's callback is check_plain(), $item['options']['html'] becomes
webmaster@1 475 * TRUE.
webmaster@1 476 * $item['description'] is translated using t().
webmaster@1 477 * When doing link translation and the $item['options']['attributes']['title']
webmaster@1 478 * (link title attribute) matches the description, it is translated as well.
webmaster@1 479 */
webmaster@1 480 function _menu_item_localize(&$item, $map, $link_translate = FALSE) {
webmaster@1 481 $callback = $item['title_callback'];
webmaster@1 482 $item['localized_options'] = $item['options'];
webmaster@1 483 // If we are not doing link translation or if the title matches the
webmaster@1 484 // link title of its router item, localize it.
webmaster@1 485 if (!$link_translate || (!empty($item['title']) && ($item['title'] == $item['link_title']))) {
webmaster@1 486 // t() is a special case. Since it is used very close to all the time,
webmaster@1 487 // we handle it directly instead of using indirect, slower methods.
webmaster@1 488 if ($callback == 't') {
webmaster@1 489 if (empty($item['title_arguments'])) {
webmaster@1 490 $item['title'] = t($item['title']);
webmaster@1 491 }
webmaster@1 492 else {
webmaster@1 493 $item['title'] = t($item['title'], menu_unserialize($item['title_arguments'], $map));
webmaster@1 494 }
webmaster@1 495 }
webmaster@1 496 elseif ($callback) {
webmaster@1 497 if (empty($item['title_arguments'])) {
webmaster@1 498 $item['title'] = $callback($item['title']);
webmaster@1 499 }
webmaster@1 500 else {
webmaster@1 501 $item['title'] = call_user_func_array($callback, menu_unserialize($item['title_arguments'], $map));
webmaster@1 502 }
webmaster@1 503 // Avoid calling check_plain again on l() function.
webmaster@1 504 if ($callback == 'check_plain') {
webmaster@1 505 $item['localized_options']['html'] = TRUE;
webmaster@1 506 }
webmaster@1 507 }
webmaster@1 508 }
webmaster@1 509 elseif ($link_translate) {
webmaster@1 510 $item['title'] = $item['link_title'];
webmaster@1 511 }
webmaster@1 512
webmaster@1 513 // Translate description, see the motivation above.
webmaster@1 514 if (!empty($item['description'])) {
webmaster@1 515 $original_description = $item['description'];
webmaster@1 516 $item['description'] = t($item['description']);
webmaster@7 517 if ($link_translate && isset($item['options']['attributes']['title']) && $item['options']['attributes']['title'] == $original_description) {
webmaster@1 518 $item['localized_options']['attributes']['title'] = $item['description'];
webmaster@1 519 }
webmaster@1 520 }
webmaster@1 521 }
webmaster@1 522
webmaster@1 523 /**
webmaster@1 524 * Handles dynamic path translation and menu access control.
webmaster@1 525 *
webmaster@1 526 * When a user arrives on a page such as node/5, this function determines
webmaster@1 527 * what "5" corresponds to, by inspecting the page's menu path definition,
webmaster@1 528 * node/%node. This will call node_load(5) to load the corresponding node
webmaster@1 529 * object.
webmaster@1 530 *
webmaster@1 531 * It also works in reverse, to allow the display of tabs and menu items which
webmaster@1 532 * contain these dynamic arguments, translating node/%node to node/5.
webmaster@1 533 *
webmaster@1 534 * Translation of menu item titles and descriptions are done here to
webmaster@1 535 * allow for storage of English strings in the database, and translation
webmaster@1 536 * to the language required to generate the current page
webmaster@1 537 *
webmaster@1 538 * @param $router_item
webmaster@1 539 * A menu router item
webmaster@1 540 * @param $map
webmaster@1 541 * An array of path arguments (ex: array('node', '5'))
webmaster@1 542 * @param $to_arg
webmaster@1 543 * Execute $item['to_arg_functions'] or not. Use only if you want to render a
webmaster@1 544 * path from the menu table, for example tabs.
webmaster@1 545 * @return
webmaster@1 546 * Returns the map with objects loaded as defined in the
webmaster@1 547 * $item['load_functions. $item['access'] becomes TRUE if the item is
webmaster@1 548 * accessible, FALSE otherwise. $item['href'] is set according to the map.
webmaster@1 549 * If an error occurs during calling the load_functions (like trying to load
webmaster@1 550 * a non existing node) then this function return FALSE.
webmaster@1 551 */
webmaster@1 552 function _menu_translate(&$router_item, $map, $to_arg = FALSE) {
webmaster@1 553 $path_map = $map;
webmaster@1 554 if (!_menu_load_objects($router_item, $map)) {
webmaster@1 555 // An error occurred loading an object.
webmaster@1 556 $router_item['access'] = FALSE;
webmaster@1 557 return FALSE;
webmaster@1 558 }
webmaster@1 559 if ($to_arg) {
webmaster@1 560 _menu_link_map_translate($path_map, $router_item['to_arg_functions']);
webmaster@1 561 }
webmaster@1 562
webmaster@1 563 // Generate the link path for the page request or local tasks.
webmaster@1 564 $link_map = explode('/', $router_item['path']);
webmaster@1 565 for ($i = 0; $i < $router_item['number_parts']; $i++) {
webmaster@1 566 if ($link_map[$i] == '%') {
webmaster@1 567 $link_map[$i] = $path_map[$i];
webmaster@1 568 }
webmaster@1 569 }
webmaster@1 570 $router_item['href'] = implode('/', $link_map);
webmaster@1 571 $router_item['options'] = array();
webmaster@1 572 _menu_check_access($router_item, $map);
webmaster@7 573
webmaster@7 574 // For performance, don't localize an item the user can't access.
webmaster@7 575 if ($router_item['access']) {
webmaster@7 576 _menu_item_localize($router_item, $map);
webmaster@7 577 }
webmaster@1 578
webmaster@1 579 return $map;
webmaster@1 580 }
webmaster@1 581
webmaster@1 582 /**
webmaster@1 583 * This function translates the path elements in the map using any to_arg
webmaster@1 584 * helper function. These functions take an argument and return an object.
webmaster@1 585 * See http://drupal.org/node/109153 for more information.
webmaster@1 586 *
webmaster@1 587 * @param map
webmaster@1 588 * An array of path arguments (ex: array('node', '5'))
webmaster@1 589 * @param $to_arg_functions
webmaster@1 590 * An array of helper function (ex: array(2 => 'menu_tail_to_arg'))
webmaster@1 591 */
webmaster@1 592 function _menu_link_map_translate(&$map, $to_arg_functions) {
webmaster@1 593 if ($to_arg_functions) {
webmaster@1 594 $to_arg_functions = unserialize($to_arg_functions);
webmaster@1 595 foreach ($to_arg_functions as $index => $function) {
webmaster@1 596 // Translate place-holders into real values.
webmaster@1 597 $arg = $function(!empty($map[$index]) ? $map[$index] : '', $map, $index);
webmaster@1 598 if (!empty($map[$index]) || isset($arg)) {
webmaster@1 599 $map[$index] = $arg;
webmaster@1 600 }
webmaster@1 601 else {
webmaster@1 602 unset($map[$index]);
webmaster@1 603 }
webmaster@1 604 }
webmaster@1 605 }
webmaster@1 606 }
webmaster@1 607
webmaster@1 608 function menu_tail_to_arg($arg, $map, $index) {
webmaster@1 609 return implode('/', array_slice($map, $index));
webmaster@1 610 }
webmaster@1 611
webmaster@1 612 /**
webmaster@1 613 * This function is similar to _menu_translate() but does link-specific
webmaster@3 614 * preparation such as always calling to_arg functions.
webmaster@1 615 *
webmaster@1 616 * @param $item
webmaster@1 617 * A menu link
webmaster@1 618 * @return
webmaster@1 619 * Returns the map of path arguments with objects loaded as defined in the
webmaster@3 620 * $item['load_functions']:
webmaster@3 621 * - $item['access'] becomes TRUE if the item is accessible, FALSE otherwise.
webmaster@3 622 * - $item['href'] is generated from link_path, possibly by to_arg functions.
webmaster@3 623 * - $item['title'] is generated from link_title, and may be localized.
webmaster@3 624 * - $item['options'] is unserialized; it is also changed within the call
webmaster@3 625 * here to $item['localized_options'] by _menu_item_localize().
webmaster@1 626 */
webmaster@1 627 function _menu_link_translate(&$item) {
webmaster@1 628 $item['options'] = unserialize($item['options']);
webmaster@1 629 if ($item['external']) {
webmaster@1 630 $item['access'] = 1;
webmaster@1 631 $map = array();
webmaster@1 632 $item['href'] = $item['link_path'];
webmaster@1 633 $item['title'] = $item['link_title'];
webmaster@1 634 $item['localized_options'] = $item['options'];
webmaster@1 635 }
webmaster@1 636 else {
webmaster@1 637 $map = explode('/', $item['link_path']);
webmaster@1 638 _menu_link_map_translate($map, $item['to_arg_functions']);
webmaster@1 639 $item['href'] = implode('/', $map);
webmaster@1 640
webmaster@1 641 // Note - skip callbacks without real values for their arguments.
webmaster@1 642 if (strpos($item['href'], '%') !== FALSE) {
webmaster@1 643 $item['access'] = FALSE;
webmaster@1 644 return FALSE;
webmaster@1 645 }
webmaster@1 646 // menu_tree_check_access() may set this ahead of time for links to nodes.
webmaster@1 647 if (!isset($item['access'])) {
webmaster@1 648 if (!_menu_load_objects($item, $map)) {
webmaster@1 649 // An error occurred loading an object.
webmaster@1 650 $item['access'] = FALSE;
webmaster@1 651 return FALSE;
webmaster@1 652 }
webmaster@1 653 _menu_check_access($item, $map);
webmaster@1 654 }
webmaster@7 655 // For performance, don't localize a link the user can't access.
webmaster@7 656 if ($item['access']) {
webmaster@7 657 _menu_item_localize($item, $map, TRUE);
webmaster@7 658 }
webmaster@1 659 }
webmaster@1 660
webmaster@1 661 // Allow other customizations - e.g. adding a page-specific query string to the
webmaster@1 662 // options array. For performance reasons we only invoke this hook if the link
webmaster@1 663 // has the 'alter' flag set in the options array.
webmaster@1 664 if (!empty($item['options']['alter'])) {
webmaster@1 665 drupal_alter('translated_menu_link', $item, $map);
webmaster@1 666 }
webmaster@1 667
webmaster@1 668 return $map;
webmaster@1 669 }
webmaster@1 670
webmaster@1 671 /**
webmaster@1 672 * Get a loaded object from a router item.
webmaster@1 673 *
webmaster@1 674 * menu_get_object() will provide you the current node on paths like node/5,
webmaster@1 675 * node/5/revisions/48 etc. menu_get_object('user') will give you the user
webmaster@1 676 * account on user/5 etc. Note - this function should never be called within a
webmaster@1 677 * _to_arg function (like user_current_to_arg()) since this may result in an
webmaster@1 678 * infinite recursion.
webmaster@1 679 *
webmaster@1 680 * @param $type
webmaster@1 681 * Type of the object. These appear in hook_menu definitons as %type. Core
webmaster@1 682 * provides aggregator_feed, aggregator_category, contact, filter_format,
webmaster@1 683 * forum_term, menu, menu_link, node, taxonomy_vocabulary, user. See the
webmaster@1 684 * relevant {$type}_load function for more on each. Defaults to node.
webmaster@1 685 * @param $position
webmaster@1 686 * The expected position for $type object. For node/%node this is 1, for
webmaster@1 687 * comment/reply/%node this is 2. Defaults to 1.
webmaster@1 688 * @param $path
webmaster@3 689 * See menu_get_item() for more on this. Defaults to the current path.
webmaster@1 690 */
webmaster@1 691 function menu_get_object($type = 'node', $position = 1, $path = NULL) {
webmaster@1 692 $router_item = menu_get_item($path);
webmaster@1 693 if (isset($router_item['load_functions'][$position]) && !empty($router_item['map'][$position]) && $router_item['load_functions'][$position] == $type .'_load') {
webmaster@1 694 return $router_item['map'][$position];
webmaster@1 695 }
webmaster@1 696 }
webmaster@1 697
webmaster@1 698 /**
webmaster@1 699 * Render a menu tree based on the current path.
webmaster@1 700 *
webmaster@1 701 * The tree is expanded based on the current path and dynamic paths are also
webmaster@1 702 * changed according to the defined to_arg functions (for example the 'My account'
webmaster@1 703 * link is changed from user/% to a link with the current user's uid).
webmaster@1 704 *
webmaster@1 705 * @param $menu_name
webmaster@1 706 * The name of the menu.
webmaster@1 707 * @return
webmaster@1 708 * The rendered HTML of that menu on the current page.
webmaster@1 709 */
webmaster@1 710 function menu_tree($menu_name = 'navigation') {
webmaster@1 711 static $menu_output = array();
webmaster@1 712
webmaster@1 713 if (!isset($menu_output[$menu_name])) {
webmaster@1 714 $tree = menu_tree_page_data($menu_name);
webmaster@1 715 $menu_output[$menu_name] = menu_tree_output($tree);
webmaster@1 716 }
webmaster@1 717 return $menu_output[$menu_name];
webmaster@1 718 }
webmaster@1 719
webmaster@1 720 /**
webmaster@1 721 * Returns a rendered menu tree.
webmaster@1 722 *
webmaster@1 723 * @param $tree
webmaster@1 724 * A data structure representing the tree as returned from menu_tree_data.
webmaster@1 725 * @return
webmaster@1 726 * The rendered HTML of that data structure.
webmaster@1 727 */
webmaster@1 728 function menu_tree_output($tree) {
webmaster@1 729 $output = '';
webmaster@1 730 $items = array();
webmaster@1 731
webmaster@1 732 // Pull out just the menu items we are going to render so that we
webmaster@1 733 // get an accurate count for the first/last classes.
webmaster@1 734 foreach ($tree as $data) {
webmaster@1 735 if (!$data['link']['hidden']) {
webmaster@1 736 $items[] = $data;
webmaster@1 737 }
webmaster@1 738 }
webmaster@1 739
webmaster@1 740 $num_items = count($items);
webmaster@1 741 foreach ($items as $i => $data) {
webmaster@1 742 $extra_class = NULL;
webmaster@1 743 if ($i == 0) {
webmaster@1 744 $extra_class = 'first';
webmaster@1 745 }
webmaster@1 746 if ($i == $num_items - 1) {
webmaster@1 747 $extra_class = 'last';
webmaster@1 748 }
webmaster@1 749 $link = theme('menu_item_link', $data['link']);
webmaster@1 750 if ($data['below']) {
webmaster@1 751 $output .= theme('menu_item', $link, $data['link']['has_children'], menu_tree_output($data['below']), $data['link']['in_active_trail'], $extra_class);
webmaster@1 752 }
webmaster@1 753 else {
webmaster@1 754 $output .= theme('menu_item', $link, $data['link']['has_children'], '', $data['link']['in_active_trail'], $extra_class);
webmaster@1 755 }
webmaster@1 756 }
webmaster@1 757 return $output ? theme('menu_tree', $output) : '';
webmaster@1 758 }
webmaster@1 759
webmaster@1 760 /**
webmaster@1 761 * Get the data structure representing a named menu tree.
webmaster@1 762 *
webmaster@1 763 * Since this can be the full tree including hidden items, the data returned
webmaster@1 764 * may be used for generating an an admin interface or a select.
webmaster@1 765 *
webmaster@1 766 * @param $menu_name
webmaster@1 767 * The named menu links to return
webmaster@1 768 * @param $item
webmaster@1 769 * A fully loaded menu link, or NULL. If a link is supplied, only the
webmaster@1 770 * path to root will be included in the returned tree- as if this link
webmaster@1 771 * represented the current page in a visible menu.
webmaster@1 772 * @return
webmaster@1 773 * An tree of menu links in an array, in the order they should be rendered.
webmaster@1 774 */
webmaster@1 775 function menu_tree_all_data($menu_name = 'navigation', $item = NULL) {
webmaster@1 776 static $tree = array();
webmaster@1 777
webmaster@1 778 // Use $mlid as a flag for whether the data being loaded is for the whole tree.
webmaster@1 779 $mlid = isset($item['mlid']) ? $item['mlid'] : 0;
webmaster@5 780 // Generate a cache ID (cid) specific for this $menu_name and $item.
webmaster@5 781 $cid = 'links:'. $menu_name .':all-cid:'. $mlid;
webmaster@1 782
webmaster@1 783 if (!isset($tree[$cid])) {
webmaster@1 784 // If the static variable doesn't have the data, check {cache_menu}.
webmaster@1 785 $cache = cache_get($cid, 'cache_menu');
webmaster@1 786 if ($cache && isset($cache->data)) {
webmaster@5 787 // If the cache entry exists, it will just be the cid for the actual data.
webmaster@5 788 // This avoids duplication of large amounts of data.
webmaster@5 789 $cache = cache_get($cache->data, 'cache_menu');
webmaster@5 790 if ($cache && isset($cache->data)) {
webmaster@5 791 $data = $cache->data;
webmaster@5 792 }
webmaster@1 793 }
webmaster@5 794 // If the tree data was not in the cache, $data will be NULL.
webmaster@5 795 if (!isset($data)) {
webmaster@1 796 // Build and run the query, and build the tree.
webmaster@1 797 if ($mlid) {
webmaster@1 798 // The tree is for a single item, so we need to match the values in its
webmaster@1 799 // p columns and 0 (the top level) with the plid values of other links.
webmaster@1 800 $args = array(0);
webmaster@1 801 for ($i = 1; $i < MENU_MAX_DEPTH; $i++) {
webmaster@1 802 $args[] = $item["p$i"];
webmaster@1 803 }
webmaster@1 804 $args = array_unique($args);
webmaster@1 805 $placeholders = implode(', ', array_fill(0, count($args), '%d'));
webmaster@1 806 $where = ' AND ml.plid IN ('. $placeholders .')';
webmaster@1 807 $parents = $args;
webmaster@1 808 $parents[] = $item['mlid'];
webmaster@1 809 }
webmaster@1 810 else {
webmaster@1 811 // Get all links in this menu.
webmaster@1 812 $where = '';
webmaster@1 813 $args = array();
webmaster@1 814 $parents = array();
webmaster@1 815 }
webmaster@1 816 array_unshift($args, $menu_name);
webmaster@1 817 // Select the links from the table, and recursively build the tree. We
webmaster@1 818 // LEFT JOIN since there is no match in {menu_router} for an external
webmaster@1 819 // link.
webmaster@1 820 $data['tree'] = menu_tree_data(db_query("
webmaster@1 821 SELECT m.load_functions, m.to_arg_functions, m.access_callback, m.access_arguments, m.page_callback, m.page_arguments, m.title, m.title_callback, m.title_arguments, m.type, m.description, ml.*
webmaster@1 822 FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
webmaster@1 823 WHERE ml.menu_name = '%s'". $where ."
webmaster@1 824 ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args), $parents);
webmaster@1 825 $data['node_links'] = array();
webmaster@1 826 menu_tree_collect_node_links($data['tree'], $data['node_links']);
webmaster@5 827 // Cache the data, if it is not already in the cache.
webmaster@5 828 $tree_cid = _menu_tree_cid($menu_name, $data);
webmaster@5 829 if (!cache_get($tree_cid, 'cache_menu')) {
webmaster@5 830 cache_set($tree_cid, $data, 'cache_menu');
webmaster@5 831 }
webmaster@5 832 // Cache the cid of the (shared) data using the menu and item-specific cid.
webmaster@5 833 cache_set($cid, $tree_cid, 'cache_menu');
webmaster@1 834 }
webmaster@1 835 // Check access for the current user to each item in the tree.
webmaster@1 836 menu_tree_check_access($data['tree'], $data['node_links']);
webmaster@1 837 $tree[$cid] = $data['tree'];
webmaster@1 838 }
webmaster@1 839
webmaster@1 840 return $tree[$cid];
webmaster@1 841 }
webmaster@1 842
webmaster@1 843 /**
webmaster@1 844 * Get the data structure representing a named menu tree, based on the current page.
webmaster@1 845 *
webmaster@1 846 * The tree order is maintained by storing each parent in an individual
webmaster@1 847 * field, see http://drupal.org/node/141866 for more.
webmaster@1 848 *
webmaster@1 849 * @param $menu_name
webmaster@1 850 * The named menu links to return
webmaster@1 851 * @return
webmaster@1 852 * An array of menu links, in the order they should be rendered. The array
webmaster@1 853 * is a list of associative arrays -- these have two keys, link and below.
webmaster@1 854 * link is a menu item, ready for theming as a link. Below represents the
webmaster@1 855 * submenu below the link if there is one, and it is a subtree that has the
webmaster@1 856 * same structure described for the top-level array.
webmaster@1 857 */
webmaster@1 858 function menu_tree_page_data($menu_name = 'navigation') {
webmaster@1 859 static $tree = array();
webmaster@1 860
webmaster@1 861 // Load the menu item corresponding to the current page.
webmaster@1 862 if ($item = menu_get_item()) {
webmaster@5 863 // Generate a cache ID (cid) specific for this page.
webmaster@5 864 $cid = 'links:'. $menu_name .':page-cid:'. $item['href'] .':'. (int)$item['access'];
webmaster@1 865
webmaster@1 866 if (!isset($tree[$cid])) {
webmaster@1 867 // If the static variable doesn't have the data, check {cache_menu}.
webmaster@1 868 $cache = cache_get($cid, 'cache_menu');
webmaster@1 869 if ($cache && isset($cache->data)) {
webmaster@5 870 // If the cache entry exists, it will just be the cid for the actual data.
webmaster@5 871 // This avoids duplication of large amounts of data.
webmaster@5 872 $cache = cache_get($cache->data, 'cache_menu');
webmaster@5 873 if ($cache && isset($cache->data)) {
webmaster@5 874 $data = $cache->data;
webmaster@5 875 }
webmaster@1 876 }
webmaster@5 877 // If the tree data was not in the cache, $data will be NULL.
webmaster@5 878 if (!isset($data)) {
webmaster@1 879 // Build and run the query, and build the tree.
webmaster@1 880 if ($item['access']) {
webmaster@1 881 // Check whether a menu link exists that corresponds to the current path.
webmaster@1 882 $args = array($menu_name, $item['href']);
webmaster@1 883 $placeholders = "'%s'";
webmaster@1 884 if (drupal_is_front_page()) {
webmaster@1 885 $args[] = '<front>';
webmaster@1 886 $placeholders .= ", '%s'";
webmaster@1 887 }
webmaster@1 888 $parents = db_fetch_array(db_query("SELECT p1, p2, p3, p4, p5, p6, p7, p8 FROM {menu_links} WHERE menu_name = '%s' AND link_path IN (". $placeholders .")", $args));
webmaster@1 889
webmaster@1 890 if (empty($parents)) {
webmaster@1 891 // If no link exists, we may be on a local task that's not in the links.
webmaster@1 892 // TODO: Handle the case like a local task on a specific node in the menu.
webmaster@1 893 $parents = db_fetch_array(db_query("SELECT p1, p2, p3, p4, p5, p6, p7, p8 FROM {menu_links} WHERE menu_name = '%s' AND link_path = '%s'", $menu_name, $item['tab_root']));
webmaster@1 894 }
webmaster@1 895 // We always want all the top-level links with plid == 0.
webmaster@1 896 $parents[] = '0';
webmaster@1 897
webmaster@1 898 // Use array_values() so that the indices are numeric for array_merge().
webmaster@1 899 $args = $parents = array_unique(array_values($parents));
webmaster@1 900 $placeholders = implode(', ', array_fill(0, count($args), '%d'));
webmaster@1 901 $expanded = variable_get('menu_expanded', array());
webmaster@1 902 // Check whether the current menu has any links set to be expanded.
webmaster@1 903 if (in_array($menu_name, $expanded)) {
webmaster@1 904 // Collect all the links set to be expanded, and then add all of
webmaster@1 905 // their children to the list as well.
webmaster@1 906 do {
webmaster@1 907 $result = db_query("SELECT mlid FROM {menu_links} WHERE menu_name = '%s' AND expanded = 1 AND has_children = 1 AND plid IN (". $placeholders .') AND mlid NOT IN ('. $placeholders .')', array_merge(array($menu_name), $args, $args));
webmaster@1 908 $num_rows = FALSE;
webmaster@1 909 while ($item = db_fetch_array($result)) {
webmaster@1 910 $args[] = $item['mlid'];
webmaster@1 911 $num_rows = TRUE;
webmaster@1 912 }
webmaster@1 913 $placeholders = implode(', ', array_fill(0, count($args), '%d'));
webmaster@1 914 } while ($num_rows);
webmaster@1 915 }
webmaster@1 916 array_unshift($args, $menu_name);
webmaster@1 917 }
webmaster@1 918 else {
webmaster@1 919 // Show only the top-level menu items when access is denied.
webmaster@1 920 $args = array($menu_name, '0');
webmaster@1 921 $placeholders = '%d';
webmaster@1 922 $parents = array();
webmaster@1 923 }
webmaster@1 924 // Select the links from the table, and recursively build the tree. We
webmaster@1 925 // LEFT JOIN since there is no match in {menu_router} for an external
webmaster@1 926 // link.
webmaster@1 927 $data['tree'] = menu_tree_data(db_query("
webmaster@1 928 SELECT m.load_functions, m.to_arg_functions, m.access_callback, m.access_arguments, m.page_callback, m.page_arguments, m.title, m.title_callback, m.title_arguments, m.type, m.description, ml.*
webmaster@1 929 FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
webmaster@1 930 WHERE ml.menu_name = '%s' AND ml.plid IN (". $placeholders .")
webmaster@1 931 ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args), $parents);
webmaster@1 932 $data['node_links'] = array();
webmaster@1 933 menu_tree_collect_node_links($data['tree'], $data['node_links']);
webmaster@5 934 // Cache the data, if it is not already in the cache.
webmaster@5 935 $tree_cid = _menu_tree_cid($menu_name, $data);
webmaster@5 936 if (!cache_get($tree_cid, 'cache_menu')) {
webmaster@5 937 cache_set($tree_cid, $data, 'cache_menu');
webmaster@5 938 }
webmaster@5 939 // Cache the cid of the (shared) data using the page-specific cid.
webmaster@5 940 cache_set($cid, $tree_cid, 'cache_menu');
webmaster@1 941 }
webmaster@1 942 // Check access for the current user to each item in the tree.
webmaster@1 943 menu_tree_check_access($data['tree'], $data['node_links']);
webmaster@1 944 $tree[$cid] = $data['tree'];
webmaster@1 945 }
webmaster@1 946 return $tree[$cid];
webmaster@1 947 }
webmaster@1 948
webmaster@1 949 return array();
webmaster@1 950 }
webmaster@1 951
webmaster@1 952 /**
webmaster@5 953 * Helper function - compute the real cache ID for menu tree data.
webmaster@5 954 */
webmaster@5 955 function _menu_tree_cid($menu_name, $data) {
webmaster@5 956 return 'links:'. $menu_name .':tree-data:'. md5(serialize($data));
webmaster@5 957 }
webmaster@5 958
webmaster@5 959 /**
webmaster@1 960 * Recursive helper function - collect node links.
webmaster@1 961 */
webmaster@1 962 function menu_tree_collect_node_links(&$tree, &$node_links) {
webmaster@1 963 foreach ($tree as $key => $v) {
webmaster@1 964 if ($tree[$key]['link']['router_path'] == 'node/%') {
webmaster@1 965 $nid = substr($tree[$key]['link']['link_path'], 5);
webmaster@1 966 if (is_numeric($nid)) {
webmaster@1 967 $node_links[$nid][$tree[$key]['link']['mlid']] = &$tree[$key]['link'];
webmaster@1 968 $tree[$key]['link']['access'] = FALSE;
webmaster@1 969 }
webmaster@1 970 }
webmaster@1 971 if ($tree[$key]['below']) {
webmaster@1 972 menu_tree_collect_node_links($tree[$key]['below'], $node_links);
webmaster@1 973 }
webmaster@1 974 }
webmaster@1 975 }
webmaster@1 976
webmaster@1 977 /**
webmaster@1 978 * Check access and perform other dynamic operations for each link in the tree.
webmaster@1 979 */
webmaster@1 980 function menu_tree_check_access(&$tree, $node_links = array()) {
webmaster@1 981
webmaster@1 982 if ($node_links) {
webmaster@1 983 // Use db_rewrite_sql to evaluate view access without loading each full node.
webmaster@1 984 $nids = array_keys($node_links);
webmaster@1 985 $placeholders = '%d'. str_repeat(', %d', count($nids) - 1);
webmaster@1 986 $result = db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.status = 1 AND n.nid IN (". $placeholders .")"), $nids);
webmaster@1 987 while ($node = db_fetch_array($result)) {
webmaster@1 988 $nid = $node['nid'];
webmaster@1 989 foreach ($node_links[$nid] as $mlid => $link) {
webmaster@1 990 $node_links[$nid][$mlid]['access'] = TRUE;
webmaster@1 991 }
webmaster@1 992 }
webmaster@1 993 }
webmaster@1 994 _menu_tree_check_access($tree);
webmaster@1 995 return;
webmaster@1 996 }
webmaster@1 997
webmaster@1 998 /**
webmaster@1 999 * Recursive helper function for menu_tree_check_access()
webmaster@1 1000 */
webmaster@1 1001 function _menu_tree_check_access(&$tree) {
webmaster@1 1002 $new_tree = array();
webmaster@1 1003 foreach ($tree as $key => $v) {
webmaster@1 1004 $item = &$tree[$key]['link'];
webmaster@1 1005 _menu_link_translate($item);
webmaster@1 1006 if ($item['access']) {
webmaster@1 1007 if ($tree[$key]['below']) {
webmaster@1 1008 _menu_tree_check_access($tree[$key]['below']);
webmaster@1 1009 }
webmaster@1 1010 // The weights are made a uniform 5 digits by adding 50000 as an offset.
webmaster@1 1011 // After _menu_link_translate(), $item['title'] has the localized link title.
webmaster@1 1012 // Adding the mlid to the end of the index insures that it is unique.
webmaster@1 1013 $new_tree[(50000 + $item['weight']) .' '. $item['title'] .' '. $item['mlid']] = $tree[$key];
webmaster@1 1014 }
webmaster@1 1015 }
webmaster@1 1016 // Sort siblings in the tree based on the weights and localized titles.
webmaster@1 1017 ksort($new_tree);
webmaster@1 1018 $tree = $new_tree;
webmaster@1 1019 }
webmaster@1 1020
webmaster@1 1021 /**
webmaster@1 1022 * Build the data representing a menu tree.
webmaster@1 1023 *
webmaster@1 1024 * @param $result
webmaster@1 1025 * The database result.
webmaster@1 1026 * @param $parents
webmaster@1 1027 * An array of the plid values that represent the path from the current page
webmaster@1 1028 * to the root of the menu tree.
webmaster@1 1029 * @param $depth
webmaster@1 1030 * The depth of the current menu tree.
webmaster@1 1031 * @return
webmaster@1 1032 * See menu_tree_page_data for a description of the data structure.
webmaster@1 1033 */
webmaster@1 1034 function menu_tree_data($result = NULL, $parents = array(), $depth = 1) {
webmaster@1 1035 list(, $tree) = _menu_tree_data($result, $parents, $depth);
webmaster@1 1036 return $tree;
webmaster@1 1037 }
webmaster@1 1038
webmaster@1 1039 /**
webmaster@1 1040 * Recursive helper function to build the data representing a menu tree.
webmaster@1 1041 *
webmaster@1 1042 * The function is a bit complex because the rendering of an item depends on
webmaster@1 1043 * the next menu item. So we are always rendering the element previously
webmaster@1 1044 * processed not the current one.
webmaster@1 1045 */
webmaster@1 1046 function _menu_tree_data($result, $parents, $depth, $previous_element = '') {
webmaster@1 1047 $remnant = NULL;
webmaster@1 1048 $tree = array();
webmaster@1 1049 while ($item = db_fetch_array($result)) {
webmaster@1 1050 // We need to determine if we're on the path to root so we can later build
webmaster@1 1051 // the correct active trail and breadcrumb.
webmaster@1 1052 $item['in_active_trail'] = in_array($item['mlid'], $parents);
webmaster@1 1053 // The current item is the first in a new submenu.
webmaster@1 1054 if ($item['depth'] > $depth) {
webmaster@1 1055 // _menu_tree returns an item and the menu tree structure.
webmaster@1 1056 list($item, $below) = _menu_tree_data($result, $parents, $item['depth'], $item);
webmaster@1 1057 if ($previous_element) {
webmaster@1 1058 $tree[$previous_element['mlid']] = array(
webmaster@1 1059 'link' => $previous_element,
webmaster@1 1060 'below' => $below,
webmaster@1 1061 );
webmaster@1 1062 }
webmaster@1 1063 else {
webmaster@1 1064 $tree = $below;
webmaster@1 1065 }
webmaster@1 1066 // We need to fall back one level.
webmaster@1 1067 if (!isset($item) || $item['depth'] < $depth) {
webmaster@1 1068 return array($item, $tree);
webmaster@1 1069 }
webmaster@1 1070 // This will be the link to be output in the next iteration.
webmaster@1 1071 $previous_element = $item;
webmaster@1 1072 }
webmaster@1 1073 // We are at the same depth, so we use the previous element.
webmaster@1 1074 elseif ($item['depth'] == $depth) {
webmaster@1 1075 if ($previous_element) {
webmaster@1 1076 // Only the first time.
webmaster@1 1077 $tree[$previous_element['mlid']] = array(
webmaster@1 1078 'link' => $previous_element,
webmaster@1 1079 'below' => FALSE,
webmaster@1 1080 );
webmaster@1 1081 }
webmaster@1 1082 // This will be the link to be output in the next iteration.
webmaster@1 1083 $previous_element = $item;
webmaster@1 1084 }
webmaster@1 1085 // The submenu ended with the previous item, so pass back the current item.
webmaster@1 1086 else {
webmaster@1 1087 $remnant = $item;
webmaster@1 1088 break;
webmaster@1 1089 }
webmaster@1 1090 }
webmaster@1 1091 if ($previous_element) {
webmaster@1 1092 // We have one more link dangling.
webmaster@1 1093 $tree[$previous_element['mlid']] = array(
webmaster@1 1094 'link' => $previous_element,
webmaster@1 1095 'below' => FALSE,
webmaster@1 1096 );
webmaster@1 1097 }
webmaster@1 1098 return array($remnant, $tree);
webmaster@1 1099 }
webmaster@1 1100
webmaster@1 1101 /**
webmaster@1 1102 * Generate the HTML output for a single menu link.
webmaster@1 1103 *
webmaster@1 1104 * @ingroup themeable
webmaster@1 1105 */
webmaster@1 1106 function theme_menu_item_link($link) {
webmaster@1 1107 if (empty($link['localized_options'])) {
webmaster@1 1108 $link['localized_options'] = array();
webmaster@1 1109 }
webmaster@1 1110
webmaster@1 1111 return l($link['title'], $link['href'], $link['localized_options']);
webmaster@1 1112 }
webmaster@1 1113
webmaster@1 1114 /**
webmaster@1 1115 * Generate the HTML output for a menu tree
webmaster@1 1116 *
webmaster@1 1117 * @ingroup themeable
webmaster@1 1118 */
webmaster@1 1119 function theme_menu_tree($tree) {
webmaster@1 1120 return '<ul class="menu">'. $tree .'</ul>';
webmaster@1 1121 }
webmaster@1 1122
webmaster@1 1123 /**
webmaster@1 1124 * Generate the HTML output for a menu item and submenu.
webmaster@1 1125 *
webmaster@1 1126 * @ingroup themeable
webmaster@1 1127 */
webmaster@1 1128 function theme_menu_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {
webmaster@1 1129 $class = ($menu ? 'expanded' : ($has_children ? 'collapsed' : 'leaf'));
webmaster@1 1130 if (!empty($extra_class)) {
webmaster@1 1131 $class .= ' '. $extra_class;
webmaster@1 1132 }
webmaster@1 1133 if ($in_active_trail) {
webmaster@1 1134 $class .= ' active-trail';
webmaster@1 1135 }
webmaster@1 1136 return '<li class="'. $class .'">'. $link . $menu ."</li>\n";
webmaster@1 1137 }
webmaster@1 1138
webmaster@1 1139 /**
webmaster@1 1140 * Generate the HTML output for a single local task link.
webmaster@1 1141 *
webmaster@1 1142 * @ingroup themeable
webmaster@1 1143 */
webmaster@1 1144 function theme_menu_local_task($link, $active = FALSE) {
webmaster@1 1145 return '<li '. ($active ? 'class="active" ' : '') .'>'. $link ."</li>\n";
webmaster@1 1146 }
webmaster@1 1147
webmaster@1 1148 /**
webmaster@1 1149 * Generates elements for the $arg array in the help hook.
webmaster@1 1150 */
webmaster@1 1151 function drupal_help_arg($arg = array()) {
webmaster@1 1152 // Note - the number of empty elements should be > MENU_MAX_PARTS.
webmaster@1 1153 return $arg + array('', '', '', '', '', '', '', '', '', '', '', '');
webmaster@1 1154 }
webmaster@1 1155
webmaster@1 1156 /**
webmaster@1 1157 * Returns the help associated with the active menu item.
webmaster@1 1158 */
webmaster@1 1159 function menu_get_active_help() {
webmaster@1 1160 $output = '';
webmaster@1 1161 $router_path = menu_tab_root_path();
webmaster@1 1162
webmaster@1 1163 $arg = drupal_help_arg(arg(NULL));
webmaster@1 1164 $empty_arg = drupal_help_arg();
webmaster@1 1165
webmaster@1 1166 foreach (module_list() as $name) {
webmaster@1 1167 if (module_hook($name, 'help')) {
webmaster@1 1168 // Lookup help for this path.
webmaster@1 1169 if ($help = module_invoke($name, 'help', $router_path, $arg)) {
webmaster@1 1170 $output .= $help ."\n";
webmaster@1 1171 }
webmaster@1 1172 // Add "more help" link on admin pages if the module provides a
webmaster@1 1173 // standalone help page.
webmaster@1 1174 if ($arg[0] == "admin" && module_exists('help') && module_invoke($name, 'help', 'admin/help#'. $arg[2], $empty_arg) && $help) {
webmaster@1 1175 $output .= theme("more_help_link", url('admin/help/'. $arg[2]));
webmaster@1 1176 }
webmaster@1 1177 }
webmaster@1 1178 }
webmaster@1 1179 return $output;
webmaster@1 1180 }
webmaster@1 1181
webmaster@1 1182 /**
webmaster@1 1183 * Build a list of named menus.
webmaster@1 1184 */
webmaster@1 1185 function menu_get_names($reset = FALSE) {
webmaster@1 1186 static $names;
webmaster@1 1187
webmaster@1 1188 if ($reset || empty($names)) {
webmaster@1 1189 $names = array();
webmaster@1 1190 $result = db_query("SELECT DISTINCT(menu_name) FROM {menu_links} ORDER BY menu_name");
webmaster@1 1191 while ($name = db_fetch_array($result)) {
webmaster@1 1192 $names[] = $name['menu_name'];
webmaster@1 1193 }
webmaster@1 1194 }
webmaster@1 1195 return $names;
webmaster@1 1196 }
webmaster@1 1197
webmaster@1 1198 /**
webmaster@1 1199 * Return an array containing the names of system-defined (default) menus.
webmaster@1 1200 */
webmaster@1 1201 function menu_list_system_menus() {
webmaster@1 1202 return array('navigation', 'primary-links', 'secondary-links');
webmaster@1 1203 }
webmaster@1 1204
webmaster@1 1205 /**
webmaster@1 1206 * Return an array of links to be rendered as the Primary links.
webmaster@1 1207 */
webmaster@1 1208 function menu_primary_links() {
webmaster@1 1209 return menu_navigation_links(variable_get('menu_primary_links_source', 'primary-links'));
webmaster@1 1210 }
webmaster@1 1211
webmaster@1 1212 /**
webmaster@1 1213 * Return an array of links to be rendered as the Secondary links.
webmaster@1 1214 */
webmaster@1 1215 function menu_secondary_links() {
webmaster@1 1216
webmaster@1 1217 // If the secondary menu source is set as the primary menu, we display the
webmaster@1 1218 // second level of the primary menu.
webmaster@1 1219 if (variable_get('menu_secondary_links_source', 'secondary-links') == variable_get('menu_primary_links_source', 'primary-links')) {
webmaster@1 1220 return menu_navigation_links(variable_get('menu_primary_links_source', 'primary-links'), 1);
webmaster@1 1221 }
webmaster@1 1222 else {
webmaster@1 1223 return menu_navigation_links(variable_get('menu_secondary_links_source', 'secondary-links'), 0);
webmaster@1 1224 }
webmaster@1 1225 }
webmaster@1 1226
webmaster@1 1227 /**
webmaster@1 1228 * Return an array of links for a navigation menu.
webmaster@1 1229 *
webmaster@1 1230 * @param $menu_name
webmaster@1 1231 * The name of the menu.
webmaster@1 1232 * @param $level
webmaster@1 1233 * Optional, the depth of the menu to be returned.
webmaster@1 1234 * @return
webmaster@1 1235 * An array of links of the specified menu and level.
webmaster@1 1236 */
webmaster@1 1237 function menu_navigation_links($menu_name, $level = 0) {
webmaster@1 1238 // Don't even bother querying the menu table if no menu is specified.
webmaster@1 1239 if (empty($menu_name)) {
webmaster@1 1240 return array();
webmaster@1 1241 }
webmaster@1 1242
webmaster@1 1243 // Get the menu hierarchy for the current page.
webmaster@1 1244 $tree = menu_tree_page_data($menu_name);
webmaster@1 1245
webmaster@1 1246 // Go down the active trail until the right level is reached.
webmaster@1 1247 while ($level-- > 0 && $tree) {
webmaster@1 1248 // Loop through the current level's items until we find one that is in trail.
webmaster@1 1249 while ($item = array_shift($tree)) {
webmaster@1 1250 if ($item['link']['in_active_trail']) {
webmaster@1 1251 // If the item is in the active trail, we continue in the subtree.
webmaster@1 1252 $tree = empty($item['below']) ? array() : $item['below'];
webmaster@1 1253 break;
webmaster@1 1254 }
webmaster@1 1255 }
webmaster@1 1256 }
webmaster@1 1257
webmaster@1 1258 // Create a single level of links.
webmaster@1 1259 $links = array();
webmaster@1 1260 foreach ($tree as $item) {
webmaster@1 1261 if (!$item['link']['hidden']) {
webmaster@1 1262 $l = $item['link']['localized_options'];
webmaster@1 1263 $l['href'] = $item['link']['href'];
webmaster@1 1264 $l['title'] = $item['link']['title'];
webmaster@7 1265 if ($item['link']['in_active_trail']) {
webmaster@7 1266 if (empty($l['attributes']['class'])) {
webmaster@7 1267 $l['attributes']['class'] = 'active-trail';
webmaster@7 1268 }
webmaster@7 1269 else {
webmaster@7 1270 $l['attributes']['class'] .= ' active-trail';
webmaster@7 1271 }
webmaster@7 1272 }
webmaster@1 1273 // Keyed with unique menu id to generate classes from theme_links().
webmaster@1 1274 $links['menu-'. $item['link']['mlid']] = $l;
webmaster@1 1275 }
webmaster@1 1276 }
webmaster@1 1277 return $links;
webmaster@1 1278 }
webmaster@1 1279
webmaster@1 1280 /**
webmaster@1 1281 * Collects the local tasks (tabs) for a given level.
webmaster@1 1282 *
webmaster@1 1283 * @param $level
webmaster@1 1284 * The level of tasks you ask for. Primary tasks are 0, secondary are 1.
webmaster@1 1285 * @param $return_root
webmaster@1 1286 * Whether to return the root path for the current page.
webmaster@1 1287 * @return
webmaster@1 1288 * Themed output corresponding to the tabs of the requested level, or
webmaster@1 1289 * router path if $return_root == TRUE. This router path corresponds to
webmaster@1 1290 * a parent tab, if the current page is a default local task.
webmaster@1 1291 */
webmaster@1 1292 function menu_local_tasks($level = 0, $return_root = FALSE) {
webmaster@1 1293 static $tabs;
webmaster@1 1294 static $root_path;
webmaster@1 1295
webmaster@1 1296 if (!isset($tabs)) {
webmaster@1 1297 $tabs = array();
webmaster@1 1298
webmaster@1 1299 $router_item = menu_get_item();
webmaster@1 1300 if (!$router_item || !$router_item['access']) {
webmaster@1 1301 return '';
webmaster@1 1302 }
webmaster@1 1303 // Get all tabs and the root page.
webmaster@1 1304 $result = db_query("SELECT * FROM {menu_router} WHERE tab_root = '%s' ORDER BY weight, title", $router_item['tab_root']);
webmaster@1 1305 $map = arg();
webmaster@1 1306 $children = array();
webmaster@1 1307 $tasks = array();
webmaster@1 1308 $root_path = $router_item['path'];
webmaster@1 1309
webmaster@1 1310 while ($item = db_fetch_array($result)) {
webmaster@1 1311 _menu_translate($item, $map, TRUE);
webmaster@1 1312 if ($item['tab_parent']) {
webmaster@1 1313 // All tabs, but not the root page.
webmaster@1 1314 $children[$item['tab_parent']][$item['path']] = $item;
webmaster@1 1315 }
webmaster@1 1316 // Store the translated item for later use.
webmaster@1 1317 $tasks[$item['path']] = $item;
webmaster@1 1318 }
webmaster@1 1319
webmaster@1 1320 // Find all tabs below the current path.
webmaster@1 1321 $path = $router_item['path'];
webmaster@1 1322 // Tab parenting may skip levels, so the number of parts in the path may not
webmaster@1 1323 // equal the depth. Thus we use the $depth counter (offset by 1000 for ksort).
webmaster@1 1324 $depth = 1001;
webmaster@1 1325 while (isset($children[$path])) {
webmaster@1 1326 $tabs_current = '';
webmaster@1 1327 $next_path = '';
webmaster@1 1328 $count = 0;
webmaster@1 1329 foreach ($children[$path] as $item) {
webmaster@1 1330 if ($item['access']) {
webmaster@1 1331 $count++;
webmaster@1 1332 // The default task is always active.
webmaster@1 1333 if ($item['type'] == MENU_DEFAULT_LOCAL_TASK) {
webmaster@1 1334 // Find the first parent which is not a default local task.
webmaster@1 1335 for ($p = $item['tab_parent']; $tasks[$p]['type'] == MENU_DEFAULT_LOCAL_TASK; $p = $tasks[$p]['tab_parent']);
webmaster@1 1336 $link = theme('menu_item_link', array('href' => $tasks[$p]['href']) + $item);
webmaster@1 1337 $tabs_current .= theme('menu_local_task', $link, TRUE);
webmaster@1 1338 $next_path = $item['path'];
webmaster@1 1339 }
webmaster@1 1340 else {
webmaster@1 1341 $link = theme('menu_item_link', $item);
webmaster@1 1342 $tabs_current .= theme('menu_local_task', $link);
webmaster@1 1343 }
webmaster@1 1344 }
webmaster@1 1345 }
webmaster@1 1346 $path = $next_path;
webmaster@1 1347 $tabs[$depth]['count'] = $count;
webmaster@1 1348 $tabs[$depth]['output'] = $tabs_current;
webmaster@1 1349 $depth++;
webmaster@1 1350 }
webmaster@1 1351
webmaster@1 1352 // Find all tabs at the same level or above the current one.
webmaster@1 1353 $parent = $router_item['tab_parent'];
webmaster@1 1354 $path = $router_item['path'];
webmaster@1 1355 $current = $router_item;
webmaster@1 1356 $depth = 1000;
webmaster@1 1357 while (isset($children[$parent])) {
webmaster@1 1358 $tabs_current = '';
webmaster@1 1359 $next_path = '';
webmaster@1 1360 $next_parent = '';
webmaster@1 1361 $count = 0;
webmaster@1 1362 foreach ($children[$parent] as $item) {
webmaster@1 1363 if ($item['access']) {
webmaster@1 1364 $count++;
webmaster@1 1365 if ($item['type'] == MENU_DEFAULT_LOCAL_TASK) {
webmaster@1 1366 // Find the first parent which is not a default local task.
webmaster@1 1367 for ($p = $item['tab_parent']; $tasks[$p]['type'] == MENU_DEFAULT_LOCAL_TASK; $p = $tasks[$p]['tab_parent']);
webmaster@1 1368 $link = theme('menu_item_link', array('href' => $tasks[$p]['href']) + $item);
webmaster@1 1369 if ($item['path'] == $router_item['path']) {
webmaster@1 1370 $root_path = $tasks[$p]['path'];
webmaster@1 1371 }
webmaster@1 1372 }
webmaster@1 1373 else {
webmaster@1 1374 $link = theme('menu_item_link', $item);
webmaster@1 1375 }
webmaster@1 1376 // We check for the active tab.
webmaster@1 1377 if ($item['path'] == $path) {
webmaster@1 1378 $tabs_current .= theme('menu_local_task', $link, TRUE);
webmaster@1 1379 $next_path = $item['tab_parent'];
webmaster@1 1380 if (isset($tasks[$next_path])) {
webmaster@1 1381 $next_parent = $tasks[$next_path]['tab_parent'];
webmaster@1 1382 }
webmaster@1 1383 }
webmaster@1 1384 else {
webmaster@1 1385 $tabs_current .= theme('menu_local_task', $link);
webmaster@1 1386 }
webmaster@1 1387 }
webmaster@1 1388 }
webmaster@1 1389 $path = $next_path;
webmaster@1 1390 $parent = $next_parent;
webmaster@1 1391 $tabs[$depth]['count'] = $count;
webmaster@1 1392 $tabs[$depth]['output'] = $tabs_current;
webmaster@1 1393 $depth--;
webmaster@1 1394 }
webmaster@1 1395 // Sort by depth.
webmaster@1 1396 ksort($tabs);
webmaster@1 1397 // Remove the depth, we are interested only in their relative placement.
webmaster@1 1398 $tabs = array_values($tabs);
webmaster@1 1399 }
webmaster@1 1400
webmaster@1 1401 if ($return_root) {
webmaster@1 1402 return $root_path;
webmaster@1 1403 }
webmaster@1 1404 else {
webmaster@1 1405 // We do not display single tabs.
webmaster@1 1406 return (isset($tabs[$level]) && $tabs[$level]['count'] > 1) ? $tabs[$level]['output'] : '';
webmaster@1 1407 }
webmaster@1 1408 }
webmaster@1 1409
webmaster@1 1410 /**
webmaster@1 1411 * Returns the rendered local tasks at the top level.
webmaster@1 1412 */
webmaster@1 1413 function menu_primary_local_tasks() {
webmaster@1 1414 return menu_local_tasks(0);
webmaster@1 1415 }
webmaster@1 1416
webmaster@1 1417 /**
webmaster@1 1418 * Returns the rendered local tasks at the second level.
webmaster@1 1419 */
webmaster@1 1420 function menu_secondary_local_tasks() {
webmaster@1 1421 return menu_local_tasks(1);
webmaster@1 1422 }
webmaster@1 1423
webmaster@1 1424 /**
webmaster@1 1425 * Returns the router path, or the path of the parent tab of a default local task.
webmaster@1 1426 */
webmaster@1 1427 function menu_tab_root_path() {
webmaster@1 1428 return menu_local_tasks(0, TRUE);
webmaster@1 1429 }
webmaster@1 1430
webmaster@1 1431 /**
webmaster@1 1432 * Returns the rendered local tasks. The default implementation renders them as tabs.
webmaster@1 1433 *
webmaster@1 1434 * @ingroup themeable
webmaster@1 1435 */
webmaster@1 1436 function theme_menu_local_tasks() {
webmaster@1 1437 $output = '';
webmaster@1 1438
webmaster@1 1439 if ($primary = menu_primary_local_tasks()) {
webmaster@1 1440 $output .= "<ul class=\"tabs primary\">\n". $primary ."</ul>\n";
webmaster@1 1441 }
webmaster@1 1442 if ($secondary = menu_secondary_local_tasks()) {
webmaster@1 1443 $output .= "<ul class=\"tabs secondary\">\n". $secondary ."</ul>\n";
webmaster@1 1444 }
webmaster@1 1445
webmaster@1 1446 return $output;
webmaster@1 1447 }
webmaster@1 1448
webmaster@1 1449 /**
webmaster@1 1450 * Set (or get) the active menu for the current page - determines the active trail.
webmaster@1 1451 */
webmaster@1 1452 function menu_set_active_menu_name($menu_name = NULL) {
webmaster@1 1453 static $active;
webmaster@1 1454
webmaster@1 1455 if (isset($menu_name)) {
webmaster@1 1456 $active = $menu_name;
webmaster@1 1457 }
webmaster@1 1458 elseif (!isset($active)) {
webmaster@1 1459 $active = 'navigation';
webmaster@1 1460 }
webmaster@1 1461 return $active;
webmaster@1 1462 }
webmaster@1 1463
webmaster@1 1464 /**
webmaster@1 1465 * Get the active menu for the current page - determines the active trail.
webmaster@1 1466 */
webmaster@1 1467 function menu_get_active_menu_name() {
webmaster@1 1468 return menu_set_active_menu_name();
webmaster@1 1469 }
webmaster@1 1470
webmaster@1 1471 /**
webmaster@1 1472 * Set the active path, which determines which page is loaded.
webmaster@1 1473 *
webmaster@1 1474 * @param $path
webmaster@1 1475 * A Drupal path - not a path alias.
webmaster@1 1476 *
webmaster@1 1477 * Note that this may not have the desired effect unless invoked very early
webmaster@1 1478 * in the page load, such as during hook_boot, or unless you call
webmaster@1 1479 * menu_execute_active_handler() to generate your page output.
webmaster@1 1480 */
webmaster@1 1481 function menu_set_active_item($path) {
webmaster@1 1482 $_GET['q'] = $path;
webmaster@1 1483 }
webmaster@1 1484
webmaster@1 1485 /**
webmaster@1 1486 * Set (or get) the active trail for the current page - the path to root in the menu tree.
webmaster@1 1487 */
webmaster@1 1488 function menu_set_active_trail($new_trail = NULL) {
webmaster@1 1489 static $trail;
webmaster@1 1490
webmaster@1 1491 if (isset($new_trail)) {
webmaster@1 1492 $trail = $new_trail;
webmaster@1 1493 }
webmaster@1 1494 elseif (!isset($trail)) {
webmaster@1 1495 $trail = array();
webmaster@1 1496 $trail[] = array('title' => t('Home'), 'href' => '<front>', 'localized_options' => array(), 'type' => 0);
webmaster@1 1497 $item = menu_get_item();
webmaster@1 1498
webmaster@1 1499 // Check whether the current item is a local task (displayed as a tab).
webmaster@1 1500 if ($item['tab_parent']) {
webmaster@1 1501 // The title of a local task is used for the tab, never the page title.
webmaster@1 1502 // Thus, replace it with the item corresponding to the root path to get
webmaster@1 1503 // the relevant href and title. For example, the menu item corresponding
webmaster@1 1504 // to 'admin' is used when on the 'By module' tab at 'admin/by-module'.
webmaster@1 1505 $parts = explode('/', $item['tab_root']);
webmaster@1 1506 $args = arg();
webmaster@1 1507 // Replace wildcards in the root path using the current path.
webmaster@1 1508 foreach ($parts as $index => $part) {
webmaster@1 1509 if ($part == '%') {
webmaster@1 1510 $parts[$index] = $args[$index];
webmaster@1 1511 }
webmaster@1 1512 }
webmaster@1 1513 // Retrieve the menu item using the root path after wildcard replacement.
webmaster@1 1514 $root_item = menu_get_item(implode('/', $parts));
webmaster@1 1515 if ($root_item && $root_item['access']) {
webmaster@1 1516 $item = $root_item;
webmaster@1 1517 }
webmaster@1 1518 }
webmaster@1 1519
webmaster@1 1520 $tree = menu_tree_page_data(menu_get_active_menu_name());
webmaster@1 1521 list($key, $curr) = each($tree);
webmaster@1 1522
webmaster@1 1523 while ($curr) {
webmaster@1 1524 // Terminate the loop when we find the current path in the active trail.
webmaster@1 1525 if ($curr['link']['href'] == $item['href']) {
webmaster@1 1526 $trail[] = $curr['link'];
webmaster@1 1527 $curr = FALSE;
webmaster@1 1528 }
webmaster@1 1529 else {
webmaster@7 1530 // Add the link if it's in the active trail, then move to the link below.
webmaster@7 1531 if ($curr['link']['in_active_trail']) {
webmaster@1 1532 $trail[] = $curr['link'];
webmaster@7 1533 $tree = $curr['below'] ? $curr['below'] : array();
webmaster@1 1534 }
webmaster@1 1535 list($key, $curr) = each($tree);
webmaster@1 1536 }
webmaster@1 1537 }
webmaster@1 1538 // Make sure the current page is in the trail (needed for the page title),
webmaster@1 1539 // but exclude tabs and the front page.
webmaster@1 1540 $last = count($trail) - 1;
webmaster@1 1541 if ($trail[$last]['href'] != $item['href'] && !(bool)($item['type'] & MENU_IS_LOCAL_TASK) && !drupal_is_front_page()) {
webmaster@1 1542 $trail[] = $item;
webmaster@1 1543 }
webmaster@1 1544 }
webmaster@1 1545 return $trail;
webmaster@1 1546 }
webmaster@1 1547
webmaster@1 1548 /**
webmaster@1 1549 * Get the active trail for the current page - the path to root in the menu tree.
webmaster@1 1550 */
webmaster@1 1551 function menu_get_active_trail() {
webmaster@1 1552 return menu_set_active_trail();
webmaster@1 1553 }
webmaster@1 1554
webmaster@1 1555 /**
webmaster@1 1556 * Get the breadcrumb for the current page, as determined by the active trail.
webmaster@1 1557 */
webmaster@1 1558 function menu_get_active_breadcrumb() {
webmaster@1 1559 $breadcrumb = array();
webmaster@1 1560
webmaster@1 1561 // No breadcrumb for the front page.
webmaster@1 1562 if (drupal_is_front_page()) {
webmaster@1 1563 return $breadcrumb;
webmaster@1 1564 }
webmaster@1 1565
webmaster@1 1566 $item = menu_get_item();
webmaster@1 1567 if ($item && $item['access']) {
webmaster@1 1568 $active_trail = menu_get_active_trail();
webmaster@1 1569
webmaster@1 1570 foreach ($active_trail as $parent) {
webmaster@1 1571 $breadcrumb[] = l($parent['title'], $parent['href'], $parent['localized_options']);
webmaster@1 1572 }
webmaster@1 1573 $end = end($active_trail);
webmaster@1 1574
webmaster@1 1575 // Don't show a link to the current page in the breadcrumb trail.
webmaster@1 1576 if ($item['href'] == $end['href'] || ($item['type'] == MENU_DEFAULT_LOCAL_TASK && $end['href'] != '<front>')) {
webmaster@1 1577 array_pop($breadcrumb);
webmaster@1 1578 }
webmaster@1 1579 }
webmaster@1 1580 return $breadcrumb;
webmaster@1 1581 }
webmaster@1 1582
webmaster@1 1583 /**
webmaster@1 1584 * Get the title of the current page, as determined by the active trail.
webmaster@1 1585 */
webmaster@1 1586 function menu_get_active_title() {
webmaster@1 1587 $active_trail = menu_get_active_trail();
webmaster@1 1588
webmaster@1 1589 foreach (array_reverse($active_trail) as $item) {
webmaster@1 1590 if (!(bool)($item['type'] & MENU_IS_LOCAL_TASK)) {
webmaster@1 1591 return $item['title'];
webmaster@1 1592 }
webmaster@1 1593 }
webmaster@1 1594 }
webmaster@1 1595
webmaster@1 1596 /**
webmaster@1 1597 * Get a menu link by its mlid, access checked and link translated for rendering.
webmaster@1 1598 *
webmaster@1 1599 * This function should never be called from within node_load() or any other
webmaster@1 1600 * function used as a menu object load function since an infinite recursion may
webmaster@1 1601 * occur.
webmaster@1 1602 *
webmaster@1 1603 * @param $mlid
webmaster@1 1604 * The mlid of the menu item.
webmaster@1 1605 * @return
webmaster@1 1606 * A menu link, with $item['access'] filled and link translated for
webmaster@1 1607 * rendering.
webmaster@1 1608 */
webmaster@1 1609 function menu_link_load($mlid) {
webmaster@1 1610 if (is_numeric($mlid) && $item = db_fetch_array(db_query("SELECT m.*, ml.* FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path WHERE ml.mlid = %d", $mlid))) {
webmaster@1 1611 _menu_link_translate($item);
webmaster@1 1612 return $item;
webmaster@1 1613 }
webmaster@1 1614 return FALSE;
webmaster@1 1615 }
webmaster@1 1616
webmaster@1 1617 /**
webmaster@1 1618 * Clears the cached cached data for a single named menu.
webmaster@1 1619 */
webmaster@1 1620 function menu_cache_clear($menu_name = 'navigation') {
webmaster@1 1621 static $cache_cleared = array();
webmaster@1 1622
webmaster@1 1623 if (empty($cache_cleared[$menu_name])) {
webmaster@1 1624 cache_clear_all('links:'. $menu_name .':', 'cache_menu', TRUE);
webmaster@1 1625 $cache_cleared[$menu_name] = 1;
webmaster@1 1626 }
webmaster@1 1627 elseif ($cache_cleared[$menu_name] == 1) {
webmaster@1 1628 register_shutdown_function('cache_clear_all', 'links:'. $menu_name .':', 'cache_menu', TRUE);
webmaster@1 1629 $cache_cleared[$menu_name] = 2;
webmaster@1 1630 }
webmaster@1 1631 }
webmaster@1 1632
webmaster@1 1633 /**
webmaster@1 1634 * Clears all cached menu data. This should be called any time broad changes
webmaster@1 1635 * might have been made to the router items or menu links.
webmaster@1 1636 */
webmaster@1 1637 function menu_cache_clear_all() {
webmaster@1 1638 cache_clear_all('*', 'cache_menu', TRUE);
webmaster@1 1639 }
webmaster@1 1640
webmaster@1 1641 /**
webmaster@1 1642 * (Re)populate the database tables used by various menu functions.
webmaster@1 1643 *
webmaster@1 1644 * This function will clear and populate the {menu_router} table, add entries
webmaster@1 1645 * to {menu_links} for new router items, then remove stale items from
webmaster@1 1646 * {menu_links}. If called from update.php or install.php, it will also
webmaster@1 1647 * schedule a call to itself on the first real page load from
webmaster@1 1648 * menu_execute_active_handler(), because the maintenance page environment
webmaster@1 1649 * is different and leaves stale data in the menu tables.
webmaster@1 1650 */
webmaster@1 1651 function menu_rebuild() {
webmaster@1 1652 variable_del('menu_rebuild_needed');
webmaster@1 1653 menu_cache_clear_all();
webmaster@1 1654 $menu = menu_router_build(TRUE);
webmaster@1 1655 _menu_navigation_links_rebuild($menu);
webmaster@1 1656 // Clear the page and block caches.
webmaster@1 1657 _menu_clear_page_cache();
webmaster@1 1658 if (defined('MAINTENANCE_MODE')) {
webmaster@1 1659 variable_set('menu_rebuild_needed', TRUE);
webmaster@1 1660 }
webmaster@1 1661 }
webmaster@1 1662
webmaster@1 1663 /**
webmaster@1 1664 * Collect, alter and store the menu definitions.
webmaster@1 1665 */
webmaster@1 1666 function menu_router_build($reset = FALSE) {
webmaster@1 1667 static $menu;
webmaster@1 1668
webmaster@1 1669 if (!isset($menu) || $reset) {
webmaster@1 1670 if (!$reset && ($cache = cache_get('router:', 'cache_menu')) && isset($cache->data)) {
webmaster@1 1671 $menu = $cache->data;
webmaster@1 1672 }
webmaster@1 1673 else {
webmaster@1 1674 // We need to manually call each module so that we can know which module
webmaster@1 1675 // a given item came from.
webmaster@1 1676 $callbacks = array();
webmaster@1 1677 foreach (module_implements('menu') as $module) {
webmaster@1 1678 $router_items = call_user_func($module .'_menu');
webmaster@1 1679 if (isset($router_items) && is_array($router_items)) {
webmaster@1 1680 foreach (array_keys($router_items) as $path) {
webmaster@1 1681 $router_items[$path]['module'] = $module;
webmaster@1 1682 }
webmaster@1 1683 $callbacks = array_merge($callbacks, $router_items);
webmaster@1 1684 }
webmaster@1 1685 }
webmaster@1 1686 // Alter the menu as defined in modules, keys are like user/%user.
webmaster@1 1687 drupal_alter('menu', $callbacks);
webmaster@1 1688 $menu = _menu_router_build($callbacks);
webmaster@1 1689 }
webmaster@1 1690 }
webmaster@1 1691 return $menu;
webmaster@1 1692 }
webmaster@1 1693
webmaster@1 1694 /**
webmaster@1 1695 * Builds a link from a router item.
webmaster@1 1696 */
webmaster@1 1697 function _menu_link_build($item) {
webmaster@1 1698 if ($item['type'] == MENU_CALLBACK) {
webmaster@1 1699 $item['hidden'] = -1;
webmaster@1 1700 }
webmaster@1 1701 elseif ($item['type'] == MENU_SUGGESTED_ITEM) {
webmaster@1 1702 $item['hidden'] = 1;
webmaster@1 1703 }
webmaster@1 1704 // Note, we set this as 'system', so that we can be sure to distinguish all
webmaster@1 1705 // the menu links generated automatically from entries in {menu_router}.
webmaster@1 1706 $item['module'] = 'system';
webmaster@1 1707 $item += array(
webmaster@1 1708 'menu_name' => 'navigation',
webmaster@1 1709 'link_title' => $item['title'],
webmaster@1 1710 'link_path' => $item['path'],
webmaster@1 1711 'hidden' => 0,
webmaster@1 1712 'options' => empty($item['description']) ? array() : array('attributes' => array('title' => $item['description'])),
webmaster@1 1713 );
webmaster@1 1714 return $item;
webmaster@1 1715 }
webmaster@1 1716
webmaster@1 1717 /**
webmaster@1 1718 * Helper function to build menu links for the items in the menu router.
webmaster@1 1719 */
webmaster@1 1720 function _menu_navigation_links_rebuild($menu) {
webmaster@1 1721 // Add normal and suggested items as links.
webmaster@1 1722 $menu_links = array();
webmaster@1 1723 foreach ($menu as $path => $item) {
webmaster@1 1724 if ($item['_visible']) {
webmaster@1 1725 $item = _menu_link_build($item);
webmaster@1 1726 $menu_links[$path] = $item;
webmaster@1 1727 $sort[$path] = $item['_number_parts'];
webmaster@1 1728 }
webmaster@1 1729 }
webmaster@1 1730 if ($menu_links) {
webmaster@1 1731 // Make sure no child comes before its parent.
webmaster@1 1732 array_multisort($sort, SORT_NUMERIC, $menu_links);
webmaster@1 1733
webmaster@1 1734 foreach ($menu_links as $item) {
webmaster@1 1735 $existing_item = db_fetch_array(db_query("SELECT mlid, menu_name, plid, customized, has_children, updated FROM {menu_links} WHERE link_path = '%s' AND module = '%s'", $item['link_path'], 'system'));
webmaster@1 1736 if ($existing_item) {
webmaster@1 1737 $item['mlid'] = $existing_item['mlid'];
webmaster@1 1738 $item['menu_name'] = $existing_item['menu_name'];
webmaster@1 1739 $item['plid'] = $existing_item['plid'];
webmaster@1 1740 $item['has_children'] = $existing_item['has_children'];
webmaster@1 1741 $item['updated'] = $existing_item['updated'];
webmaster@1 1742 }
webmaster@1 1743 if (!$existing_item || !$existing_item['customized']) {
webmaster@1 1744 menu_link_save($item);
webmaster@1 1745 }
webmaster@1 1746 }
webmaster@1 1747 }
webmaster@1 1748 $placeholders = db_placeholders($menu, 'varchar');
webmaster@1 1749 $paths = array_keys($menu);
webmaster@3 1750 // Updated and customized items whose router paths are gone need new ones.
webmaster@1 1751 $result = db_query("SELECT ml.link_path, ml.mlid, ml.router_path, ml.updated FROM {menu_links} ml WHERE ml.updated = 1 OR (router_path NOT IN ($placeholders) AND external = 0 AND customized = 1)", $paths);
webmaster@1 1752 while ($item = db_fetch_array($result)) {
webmaster@1 1753 $router_path = _menu_find_router_path($menu, $item['link_path']);
webmaster@1 1754 if (!empty($router_path) && ($router_path != $item['router_path'] || $item['updated'])) {
webmaster@1 1755 // If the router path and the link path matches, it's surely a working
webmaster@1 1756 // item, so we clear the updated flag.
webmaster@1 1757 $updated = $item['updated'] && $router_path != $item['link_path'];
webmaster@1 1758 db_query("UPDATE {menu_links} SET router_path = '%s', updated = %d WHERE mlid = %d", $router_path, $updated, $item['mlid']);
webmaster@1 1759 }
webmaster@1 1760 }
webmaster@3 1761 // Find any item whose router path does not exist any more.
webmaster@1 1762 $result = db_query("SELECT * FROM {menu_links} WHERE router_path NOT IN ($placeholders) AND external = 0 AND updated = 0 AND customized = 0 ORDER BY depth DESC", $paths);
webmaster@1 1763 // Remove all such items. Starting from those with the greatest depth will
webmaster@1 1764 // minimize the amount of re-parenting done by menu_link_delete().
webmaster@1 1765 while ($item = db_fetch_array($result)) {
webmaster@1 1766 _menu_delete_item($item, TRUE);
webmaster@1 1767 }
webmaster@1 1768 }
webmaster@1 1769
webmaster@1 1770 /**
webmaster@1 1771 * Delete one or several menu links.
webmaster@1 1772 *
webmaster@1 1773 * @param $mlid
webmaster@1 1774 * A valid menu link mlid or NULL. If NULL, $path is used.
webmaster@1 1775 * @param $path
webmaster@1 1776 * The path to the menu items to be deleted. $mlid must be NULL.
webmaster@1 1777 */
webmaster@1 1778 function menu_link_delete($mlid, $path = NULL) {
webmaster@1 1779 if (isset($mlid)) {
webmaster@1 1780 _menu_delete_item(db_fetch_array(db_query("SELECT * FROM {menu_links} WHERE mlid = %d", $mlid)));
webmaster@1 1781 }
webmaster@1 1782 else {
webmaster@1 1783 $result = db_query("SELECT * FROM {menu_links} WHERE link_path = '%s'", $path);
webmaster@1 1784 while ($link = db_fetch_array($result)) {
webmaster@1 1785 _menu_delete_item($link);
webmaster@1 1786 }
webmaster@1 1787 }
webmaster@1 1788 }
webmaster@1 1789
webmaster@1 1790 /**
webmaster@1 1791 * Helper function for menu_link_delete; deletes a single menu link.
webmaster@1 1792 *
webmaster@1 1793 * @param $item
webmaster@1 1794 * Item to be deleted.
webmaster@1 1795 * @param $force
webmaster@1 1796 * Forces deletion. Internal use only, setting to TRUE is discouraged.
webmaster@1 1797 */
webmaster@1 1798 function _menu_delete_item($item, $force = FALSE) {
webmaster@1 1799 if ($item && ($item['module'] != 'system' || $item['updated'] || $force)) {
webmaster@1 1800 // Children get re-attached to the item's parent.
webmaster@1 1801 if ($item['has_children']) {
webmaster@1 1802 $result = db_query("SELECT mlid FROM {menu_links} WHERE plid = %d", $item['mlid']);
webmaster@1 1803 while ($m = db_fetch_array($result)) {
webmaster@1 1804 $child = menu_link_load($m['mlid']);
webmaster@1 1805 $child['plid'] = $item['plid'];
webmaster@1 1806 menu_link_save($child);
webmaster@1 1807 }
webmaster@1 1808 }
webmaster@1 1809 db_query('DELETE FROM {menu_links} WHERE mlid = %d', $item['mlid']);
webmaster@1 1810
webmaster@1 1811 // Update the has_children status of the parent.
webmaster@1 1812 _menu_update_parental_status($item);
webmaster@1 1813 menu_cache_clear($item['menu_name']);
webmaster@1 1814 _menu_clear_page_cache();
webmaster@1 1815 }
webmaster@1 1816 }
webmaster@1 1817
webmaster@1 1818 /**
webmaster@1 1819 * Save a menu link.
webmaster@1 1820 *
webmaster@1 1821 * @param $item
webmaster@1 1822 * An array representing a menu link item. The only mandatory keys are
webmaster@3 1823 * link_path and link_title. Possible keys are:
webmaster@3 1824 * - menu_name default is navigation
webmaster@3 1825 * - weight default is 0
webmaster@3 1826 * - expanded whether the item is expanded.
webmaster@3 1827 * - options An array of options, @see l for more.
webmaster@3 1828 * - mlid Set to an existing value, or 0 or NULL to insert a new link.
webmaster@3 1829 * - plid The mlid of the parent.
webmaster@3 1830 * - router_path The path of the relevant router item.
webmaster@1 1831 */
webmaster@1 1832 function menu_link_save(&$item) {
webmaster@1 1833 $menu = menu_router_build();
webmaster@1 1834
webmaster@1 1835 drupal_alter('menu_link', $item, $menu);
webmaster@1 1836
webmaster@1 1837 // This is the easiest way to handle the unique internal path '<front>',
webmaster@1 1838 // since a path marked as external does not need to match a router path.
webmaster@1 1839 $item['_external'] = menu_path_is_external($item['link_path']) || $item['link_path'] == '<front>';
webmaster@1 1840 // Load defaults.
webmaster@1 1841 $item += array(
webmaster@1 1842 'menu_name' => 'navigation',
webmaster@1 1843 'weight' => 0,
webmaster@1 1844 'link_title' => '',
webmaster@1 1845 'hidden' => 0,
webmaster@1 1846 'has_children' => 0,
webmaster@1 1847 'expanded' => 0,
webmaster@1 1848 'options' => array(),
webmaster@1 1849 'module' => 'menu',
webmaster@1 1850 'customized' => 0,
webmaster@1 1851 'updated' => 0,
webmaster@1 1852 );
webmaster@1 1853 $existing_item = FALSE;
webmaster@1 1854 if (isset($item['mlid'])) {
webmaster@1 1855 $existing_item = db_fetch_array(db_query("SELECT * FROM {menu_links} WHERE mlid = %d", $item['mlid']));
webmaster@1 1856 }
webmaster@1 1857
webmaster@1 1858 if (isset($item['plid'])) {
webmaster@1 1859 $parent = db_fetch_array(db_query("SELECT * FROM {menu_links} WHERE mlid = %d", $item['plid']));
webmaster@1 1860 }
webmaster@1 1861 else {
webmaster@1 1862 // Find the parent - it must be unique.
webmaster@1 1863 $parent_path = $item['link_path'];
webmaster@1 1864 $where = "WHERE link_path = '%s'";
webmaster@1 1865 // Only links derived from router items should have module == 'system', and
webmaster@1 1866 // we want to find the parent even if it's in a different menu.
webmaster@1 1867 if ($item['module'] == 'system') {
webmaster@1 1868 $where .= " AND module = '%s'";
webmaster@1 1869 $arg2 = 'system';
webmaster@1 1870 }
webmaster@1 1871 else {
webmaster@1 1872 // If not derived from a router item, we respect the specified menu name.
webmaster@1 1873 $where .= " AND menu_name = '%s'";
webmaster@1 1874 $arg2 = $item['menu_name'];
webmaster@1 1875 }
webmaster@1 1876 do {
webmaster@1 1877 $parent = FALSE;
webmaster@1 1878 $parent_path = substr($parent_path, 0, strrpos($parent_path, '/'));
webmaster@1 1879 $result = db_query("SELECT COUNT(*) FROM {menu_links} ". $where, $parent_path, $arg2);
webmaster@1 1880 // Only valid if we get a unique result.
webmaster@1 1881 if (db_result($result) == 1) {
webmaster@1 1882 $parent = db_fetch_array(db_query("SELECT * FROM {menu_links} ". $where, $parent_path, $arg2));
webmaster@1 1883 }
webmaster@1 1884 } while ($parent === FALSE && $parent_path);
webmaster@1 1885 }
webmaster@1 1886 if ($parent !== FALSE) {
webmaster@1 1887 $item['menu_name'] = $parent['menu_name'];
webmaster@1 1888 }
webmaster@1 1889 $menu_name = $item['menu_name'];
webmaster@1 1890 // Menu callbacks need to be in the links table for breadcrumbs, but can't
webmaster@1 1891 // be parents if they are generated directly from a router item.
webmaster@1 1892 if (empty($parent['mlid']) || $parent['hidden'] < 0) {
webmaster@1 1893 $item['plid'] = 0;
webmaster@1 1894 }
webmaster@1 1895 else {
webmaster@1 1896 $item['plid'] = $parent['mlid'];
webmaster@1 1897 }
webmaster@1 1898
webmaster@1 1899 if (!$existing_item) {
webmaster@1 1900 db_query("INSERT INTO {menu_links} (
webmaster@1 1901 menu_name, plid, link_path,
webmaster@1 1902 hidden, external, has_children,
webmaster@1 1903 expanded, weight,
webmaster@1 1904 module, link_title, options,
webmaster@1 1905 customized, updated) VALUES (
webmaster@1 1906 '%s', %d, '%s',
webmaster@1 1907 %d, %d, %d,
webmaster@1 1908 %d, %d,
webmaster@1 1909 '%s', '%s', '%s', %d, %d)",
webmaster@1 1910 $item['menu_name'], $item['plid'], $item['link_path'],
webmaster@1 1911 $item['hidden'], $item['_external'], $item['has_children'],
webmaster@1 1912 $item['expanded'], $item['weight'],
webmaster@1 1913 $item['module'], $item['link_title'], serialize($item['options']),
webmaster@1 1914 $item['customized'], $item['updated']);
webmaster@1 1915 $item['mlid'] = db_last_insert_id('menu_links', 'mlid');
webmaster@1 1916 }
webmaster@1 1917
webmaster@1 1918 if (!$item['plid']) {
webmaster@1 1919 $item['p1'] = $item['mlid'];
webmaster@1 1920 for ($i = 2; $i <= MENU_MAX_DEPTH; $i++) {
webmaster@1 1921 $item["p$i"] = 0;
webmaster@1 1922 }
webmaster@1 1923 $item['depth'] = 1;
webmaster@1 1924 }
webmaster@1 1925 else {
webmaster@1 1926 // Cannot add beyond the maximum depth.
webmaster@1 1927 if ($item['has_children'] && $existing_item) {
webmaster@1 1928 $limit = MENU_MAX_DEPTH - menu_link_children_relative_depth($existing_item) - 1;
webmaster@1 1929 }
webmaster@1 1930 else {
webmaster@1 1931 $limit = MENU_MAX_DEPTH - 1;
webmaster@1 1932 }
webmaster@1 1933 if ($parent['depth'] > $limit) {
webmaster@1 1934 return FALSE;
webmaster@1 1935 }
webmaster@1 1936 $item['depth'] = $parent['depth'] + 1;
webmaster@1 1937 _menu_link_parents_set($item, $parent);
webmaster@1 1938 }
webmaster@1 1939 // Need to check both plid and menu_name, since plid can be 0 in any menu.
webmaster@1 1940 if ($existing_item && ($item['plid'] != $existing_item['plid'] || $menu_name != $existing_item['menu_name'])) {
webmaster@1 1941 _menu_link_move_children($item, $existing_item);
webmaster@1 1942 }
webmaster@1 1943 // Find the callback. During the menu update we store empty paths to be
webmaster@1 1944 // fixed later, so we skip this.
webmaster@1 1945 if (!isset($_SESSION['system_update_6021']) && (empty($item['router_path']) || !$existing_item || ($existing_item['link_path'] != $item['link_path']))) {
webmaster@1 1946 if ($item['_external']) {
webmaster@1 1947 $item['router_path'] = '';
webmaster@1 1948 }
webmaster@1 1949 else {
webmaster@1 1950 // Find the router path which will serve this path.
webmaster@1 1951 $item['parts'] = explode('/', $item['link_path'], MENU_MAX_PARTS);
webmaster@1 1952 $item['router_path'] = _menu_find_router_path($menu, $item['link_path']);
webmaster@1 1953 }
webmaster@1 1954 }
webmaster@1 1955 db_query("UPDATE {menu_links} SET menu_name = '%s', plid = %d, link_path = '%s',
webmaster@1 1956 router_path = '%s', hidden = %d, external = %d, has_children = %d,
webmaster@1 1957 expanded = %d, weight = %d, depth = %d,
webmaster@1 1958 p1 = %d, p2 = %d, p3 = %d, p4 = %d, p5 = %d, p6 = %d, p7 = %d, p8 = %d, p9 = %d,
webmaster@1 1959 module = '%s', link_title = '%s', options = '%s', customized = %d WHERE mlid = %d",
webmaster@1 1960 $item['menu_name'], $item['plid'], $item['link_path'],
webmaster@1 1961 $item['router_path'], $item['hidden'], $item['_external'], $item['has_children'],
webmaster@1 1962 $item['expanded'], $item['weight'], $item['depth'],
webmaster@1 1963 $item['p1'], $item['p2'], $item['p3'], $item['p4'], $item['p5'], $item['p6'], $item['p7'], $item['p8'], $item['p9'],
webmaster@1 1964 $item['module'], $item['link_title'], serialize($item['options']), $item['customized'], $item['mlid']);
webmaster@1 1965 // Check the has_children status of the parent.
webmaster@1 1966 _menu_update_parental_status($item);
webmaster@1 1967 menu_cache_clear($menu_name);
webmaster@1 1968 if ($existing_item && $menu_name != $existing_item['menu_name']) {
webmaster@1 1969 menu_cache_clear($existing_item['menu_name']);
webmaster@1 1970 }
webmaster@1 1971
webmaster@1 1972 _menu_clear_page_cache();
webmaster@1 1973 return $item['mlid'];
webmaster@1 1974 }
webmaster@1 1975
webmaster@1 1976 /**
webmaster@1 1977 * Helper function to clear the page and block caches at most twice per page load.
webmaster@1 1978 */
webmaster@1 1979 function _menu_clear_page_cache() {
webmaster@1 1980 static $cache_cleared = 0;
webmaster@1 1981
webmaster@1 1982 // Clear the page and block caches, but at most twice, including at
webmaster@1 1983 // the end of the page load when there are multple links saved or deleted.
webmaster@1 1984 if (empty($cache_cleared)) {
webmaster@1 1985 cache_clear_all();
webmaster@1 1986 // Keep track of which menus have expanded items.
webmaster@1 1987 _menu_set_expanded_menus();
webmaster@1 1988 $cache_cleared = 1;
webmaster@1 1989 }
webmaster@1 1990 elseif ($cache_cleared == 1) {
webmaster@1 1991 register_shutdown_function('cache_clear_all');
webmaster@1 1992 // Keep track of which menus have expanded items.
webmaster@1 1993 register_shutdown_function('_menu_set_expanded_menus');
webmaster@1 1994 $cache_cleared = 2;
webmaster@1 1995 }
webmaster@1 1996 }
webmaster@1 1997
webmaster@1 1998 /**
webmaster@1 1999 * Helper function to update a list of menus with expanded items
webmaster@1 2000 */
webmaster@1 2001 function _menu_set_expanded_menus() {
webmaster@1 2002 $names = array();
webmaster@1 2003 $result = db_query("SELECT menu_name FROM {menu_links} WHERE expanded != 0 GROUP BY menu_name");
webmaster@1 2004 while ($n = db_fetch_array($result)) {
webmaster@1 2005 $names[] = $n['menu_name'];
webmaster@1 2006 }
webmaster@1 2007 variable_set('menu_expanded', $names);
webmaster@1 2008 }
webmaster@1 2009
webmaster@1 2010 /**
webmaster@1 2011 * Find the router path which will serve this path.
webmaster@1 2012 *
webmaster@1 2013 * @param $menu
webmaster@1 2014 * The full built menu.
webmaster@1 2015 * @param $link_path
webmaster@1 2016 * The path for we are looking up its router path.
webmaster@1 2017 * @return
webmaster@1 2018 * A path from $menu keys or empty if $link_path points to a nonexisting
webmaster@1 2019 * place.
webmaster@1 2020 */
webmaster@1 2021 function _menu_find_router_path($menu, $link_path) {
webmaster@1 2022 $parts = explode('/', $link_path, MENU_MAX_PARTS);
webmaster@1 2023 $router_path = $link_path;
webmaster@1 2024 if (!isset($menu[$router_path])) {
webmaster@1 2025 list($ancestors) = menu_get_ancestors($parts);
webmaster@1 2026 $ancestors[] = '';
webmaster@1 2027 foreach ($ancestors as $key => $router_path) {
webmaster@1 2028 if (isset($menu[$router_path])) {
webmaster@1 2029 break;
webmaster@1 2030 }
webmaster@1 2031 }
webmaster@1 2032 }
webmaster@1 2033 return $router_path;
webmaster@1 2034 }
webmaster@1 2035
webmaster@1 2036 /**
webmaster@1 2037 * Insert, update or delete an uncustomized menu link related to a module.
webmaster@1 2038 *
webmaster@1 2039 * @param $module
webmaster@1 2040 * The name of the module.
webmaster@1 2041 * @param $op
webmaster@1 2042 * Operation to perform: insert, update or delete.
webmaster@1 2043 * @param $link_path
webmaster@1 2044 * The path this link points to.
webmaster@1 2045 * @param $link_title
webmaster@1 2046 * Title of the link to insert or new title to update the link to.
webmaster@1 2047 * Unused for delete.
webmaster@1 2048 * @return
webmaster@1 2049 * The insert op returns the mlid of the new item. Others op return NULL.
webmaster@1 2050 */
webmaster@1 2051 function menu_link_maintain($module, $op, $link_path, $link_title) {
webmaster@1 2052 switch ($op) {
webmaster@1 2053 case 'insert':
webmaster@1 2054 $menu_link = array(
webmaster@1 2055 'link_title' => $link_title,
webmaster@1 2056 'link_path' => $link_path,
webmaster@1 2057 'module' => $module,
webmaster@1 2058 );
webmaster@1 2059 return menu_link_save($menu_link);
webmaster@1 2060 break;
webmaster@1 2061 case 'update':
webmaster@1 2062 db_query("UPDATE {menu_links} SET link_title = '%s' WHERE link_path = '%s' AND customized = 0 AND module = '%s'", $link_title, $link_path, $module);
webmaster@1 2063 menu_cache_clear();
webmaster@1 2064 break;
webmaster@1 2065 case 'delete':
webmaster@1 2066 menu_link_delete(NULL, $link_path);
webmaster@1 2067 break;
webmaster@1 2068 }
webmaster@1 2069 }
webmaster@1 2070
webmaster@1 2071 /**
webmaster@1 2072 * Find the depth of an item's children relative to its depth.
webmaster@1 2073 *
webmaster@1 2074 * For example, if the item has a depth of 2, and the maximum of any child in
webmaster@1 2075 * the menu link tree is 5, the relative depth is 3.
webmaster@1 2076 *
webmaster@1 2077 * @param $item
webmaster@1 2078 * An array representing a menu link item.
webmaster@1 2079 * @return
webmaster@1 2080 * The relative depth, or zero.
webmaster@1 2081 *
webmaster@1 2082 */
webmaster@1 2083 function menu_link_children_relative_depth($item) {
webmaster@1 2084 $i = 1;
webmaster@1 2085 $match = '';
webmaster@1 2086 $args[] = $item['menu_name'];
webmaster@1 2087 $p = 'p1';
webmaster@1 2088 while ($i <= MENU_MAX_DEPTH && $item[$p]) {
webmaster@1 2089 $match .= " AND $p = %d";
webmaster@1 2090 $args[] = $item[$p];
webmaster@1 2091 $p = 'p'. ++$i;
webmaster@1 2092 }
webmaster@1 2093
webmaster@1 2094 $max_depth = db_result(db_query_range("SELECT depth FROM {menu_links} WHERE menu_name = '%s'". $match ." ORDER BY depth DESC", $args, 0, 1));
webmaster@1 2095
webmaster@1 2096 return ($max_depth > $item['depth']) ? $max_depth - $item['depth'] : 0;
webmaster@1 2097 }
webmaster@1 2098
webmaster@1 2099 /**
webmaster@1 2100 * Update the children of a menu link that's being moved.
webmaster@1 2101 *
webmaster@1 2102 * The menu name, parents (p1 - p6), and depth are updated for all children of
webmaster@1 2103 * the link, and the has_children status of the previous parent is updated.
webmaster@1 2104 */
webmaster@1 2105 function _menu_link_move_children($item, $existing_item) {
webmaster@1 2106
webmaster@1 2107 $args[] = $item['menu_name'];
webmaster@1 2108 $set[] = "menu_name = '%s'";
webmaster@1 2109
webmaster@1 2110 $i = 1;
webmaster@1 2111 while ($i <= $item['depth']) {
webmaster@1 2112 $p = 'p'. $i++;
webmaster@1 2113 $set[] = "$p = %d";
webmaster@1 2114 $args[] = $item[$p];
webmaster@1 2115 }
webmaster@1 2116 $j = $existing_item['depth'] + 1;
webmaster@1 2117 while ($i <= MENU_MAX_DEPTH && $j <= MENU_MAX_DEPTH) {
webmaster@1 2118 $set[] = 'p'. $i++ .' = p'. $j++;
webmaster@1 2119 }
webmaster@1 2120 while ($i <= MENU_MAX_DEPTH) {
webmaster@1 2121 $set[] = 'p'. $i++ .' = 0';
webmaster@1 2122 }
webmaster@1 2123
webmaster@1 2124 $shift = $item['depth'] - $existing_item['depth'];
webmaster@1 2125 if ($shift < 0) {
webmaster@1 2126 $args[] = -$shift;
webmaster@1 2127 $set[] = 'depth = depth - %d';
webmaster@1 2128 }
webmaster@1 2129 elseif ($shift > 0) {
webmaster@1 2130 // The order of $set must be reversed so the new values don't overwrite the
webmaster@1 2131 // old ones before they can be used because "Single-table UPDATE
webmaster@1 2132 // assignments are generally evaluated from left to right"
webmaster@1 2133 // see: http://dev.mysql.com/doc/refman/5.0/en/update.html
webmaster@1 2134 $set = array_reverse($set);
webmaster@1 2135 $args = array_reverse($args);
webmaster@1 2136
webmaster@1 2137 $args[] = $shift;
webmaster@1 2138 $set[] = 'depth = depth + %d';
webmaster@1 2139 }
webmaster@1 2140 $where[] = "menu_name = '%s'";
webmaster@1 2141 $args[] = $existing_item['menu_name'];
webmaster@1 2142 $p = 'p1';
webmaster@1 2143 for ($i = 1; $i <= MENU_MAX_DEPTH && $existing_item[$p]; $p = 'p'. ++$i) {
webmaster@1 2144 $where[] = "$p = %d";
webmaster@1 2145 $args[] = $existing_item[$p];
webmaster@1 2146 }
webmaster@1 2147
webmaster@1 2148 db_query("UPDATE {menu_links} SET ". implode(', ', $set) ." WHERE ". implode(' AND ', $where), $args);
webmaster@1 2149 // Check the has_children status of the parent, while excluding this item.
webmaster@1 2150 _menu_update_parental_status($existing_item, TRUE);
webmaster@1 2151 }
webmaster@1 2152
webmaster@1 2153 /**
webmaster@1 2154 * Check and update the has_children status for the parent of a link.
webmaster@1 2155 */
webmaster@1 2156 function _menu_update_parental_status($item, $exclude = FALSE) {
webmaster@1 2157 // If plid == 0, there is nothing to update.
webmaster@1 2158 if ($item['plid']) {
webmaster@1 2159 // We may want to exclude the passed link as a possible child.
webmaster@1 2160 $where = $exclude ? " AND mlid != %d" : '';
webmaster@1 2161 // Check if at least one visible child exists in the table.
webmaster@1 2162 $parent_has_children = (bool)db_result(db_query_range("SELECT mlid FROM {menu_links} WHERE menu_name = '%s' AND plid = %d AND hidden = 0". $where, $item['menu_name'], $item['plid'], $item['mlid'], 0, 1));
webmaster@1 2163 db_query("UPDATE {menu_links} SET has_children = %d WHERE mlid = %d", $parent_has_children, $item['plid']);
webmaster@1 2164 }
webmaster@1 2165 }
webmaster@1 2166
webmaster@1 2167 /**
webmaster@1 2168 * Helper function that sets the p1..p9 values for a menu link being saved.
webmaster@1 2169 */
webmaster@1 2170 function _menu_link_parents_set(&$item, $parent) {
webmaster@1 2171 $i = 1;
webmaster@1 2172 while ($i < $item['depth']) {
webmaster@1 2173 $p = 'p'. $i++;
webmaster@1 2174 $item[$p] = $parent[$p];
webmaster@1 2175 }
webmaster@1 2176 $p = 'p'. $i++;
webmaster@1 2177 // The parent (p1 - p9) corresponding to the depth always equals the mlid.
webmaster@1 2178 $item[$p] = $item['mlid'];
webmaster@1 2179 while ($i <= MENU_MAX_DEPTH) {
webmaster@1 2180 $p = 'p'. $i++;
webmaster@1 2181 $item[$p] = 0;
webmaster@1 2182 }
webmaster@1 2183 }
webmaster@1 2184
webmaster@1 2185 /**
webmaster@1 2186 * Helper function to build the router table based on the data from hook_menu.
webmaster@1 2187 */
webmaster@1 2188 function _menu_router_build($callbacks) {
webmaster@1 2189 // First pass: separate callbacks from paths, making paths ready for
webmaster@1 2190 // matching. Calculate fitness, and fill some default values.
webmaster@1 2191 $menu = array();
webmaster@1 2192 foreach ($callbacks as $path => $item) {
webmaster@1 2193 $load_functions = array();
webmaster@1 2194 $to_arg_functions = array();
webmaster@1 2195 $fit = 0;
webmaster@1 2196 $move = FALSE;
webmaster@1 2197
webmaster@1 2198 $parts = explode('/', $path, MENU_MAX_PARTS);
webmaster@1 2199 $number_parts = count($parts);
webmaster@1 2200 // We store the highest index of parts here to save some work in the fit
webmaster@1 2201 // calculation loop.
webmaster@1 2202 $slashes = $number_parts - 1;
webmaster@1 2203 // Extract load and to_arg functions.
webmaster@1 2204 foreach ($parts as $k => $part) {
webmaster@1 2205 $match = FALSE;
webmaster@1 2206 if (preg_match('/^%([a-z_]*)$/', $part, $matches)) {
webmaster@1 2207 if (empty($matches[1])) {
webmaster@1 2208 $match = TRUE;
webmaster@1 2209 $load_functions[$k] = NULL;
webmaster@1 2210 }
webmaster@1 2211 else {
webmaster@1 2212 if (function_exists($matches[1] .'_to_arg')) {
webmaster@1 2213 $to_arg_functions[$k] = $matches[1] .'_to_arg';
webmaster@1 2214 $load_functions[$k] = NULL;
webmaster@1 2215 $match = TRUE;
webmaster@1 2216 }
webmaster@1 2217 if (function_exists($matches[1] .'_load')) {
webmaster@1 2218 $function = $matches[1] .'_load';
webmaster@1 2219 // Create an array of arguments that will be passed to the _load
webmaster@1 2220 // function when this menu path is checked, if 'load arguments'
webmaster@1 2221 // exists.
webmaster@1 2222 $load_functions[$k] = isset($item['load arguments']) ? array($function => $item['load arguments']) : $function;
webmaster@1 2223 $match = TRUE;
webmaster@1 2224 }
webmaster@1 2225 }
webmaster@1 2226 }
webmaster@1 2227 if ($match) {
webmaster@1 2228 $parts[$k] = '%';
webmaster@1 2229 }
webmaster@1 2230 else {
webmaster@1 2231 $fit |= 1 << ($slashes - $k);
webmaster@1 2232 }
webmaster@1 2233 }
webmaster@1 2234 if ($fit) {
webmaster@1 2235 $move = TRUE;
webmaster@1 2236 }
webmaster@1 2237 else {
webmaster@1 2238 // If there is no %, it fits maximally.
webmaster@1 2239 $fit = (1 << $number_parts) - 1;
webmaster@1 2240 }
webmaster@1 2241 $masks[$fit] = 1;
webmaster@1 2242 $item['load_functions'] = empty($load_functions) ? '' : serialize($load_functions);
webmaster@1 2243 $item['to_arg_functions'] = empty($to_arg_functions) ? '' : serialize($to_arg_functions);
webmaster@1 2244 $item += array(
webmaster@1 2245 'title' => '',
webmaster@1 2246 'weight' => 0,
webmaster@1 2247 'type' => MENU_NORMAL_ITEM,
webmaster@1 2248 '_number_parts' => $number_parts,
webmaster@1 2249 '_parts' => $parts,
webmaster@1 2250 '_fit' => $fit,
webmaster@1 2251 );
webmaster@1 2252 $item += array(
webmaster@1 2253 '_visible' => (bool)($item['type'] & MENU_VISIBLE_IN_BREADCRUMB),
webmaster@1 2254 '_tab' => (bool)($item['type'] & MENU_IS_LOCAL_TASK),
webmaster@1 2255 );
webmaster@1 2256 if ($move) {
webmaster@1 2257 $new_path = implode('/', $item['_parts']);
webmaster@1 2258 $menu[$new_path] = $item;
webmaster@1 2259 $sort[$new_path] = $number_parts;
webmaster@1 2260 }
webmaster@1 2261 else {
webmaster@1 2262 $menu[$path] = $item;
webmaster@1 2263 $sort[$path] = $number_parts;
webmaster@1 2264 }
webmaster@1 2265 }
webmaster@1 2266 array_multisort($sort, SORT_NUMERIC, $menu);
webmaster@1 2267
webmaster@7 2268 if (!$menu) {
webmaster@7 2269 // We must have a serious error - there is no data to save.
webmaster@7 2270 watchdog('php', 'Menu router rebuild failed - some paths may not work correctly.', array(), WATCHDOG_ERROR);
webmaster@7 2271 return array();
webmaster@7 2272 }
webmaster@7 2273 // Delete the existing router since we have some data to replace it.
webmaster@7 2274 db_query('DELETE FROM {menu_router}');
webmaster@1 2275 // Apply inheritance rules.
webmaster@1 2276 foreach ($menu as $path => $v) {
webmaster@1 2277 $item = &$menu[$path];
webmaster@1 2278 if (!$item['_tab']) {
webmaster@1 2279 // Non-tab items.
webmaster@1 2280 $item['tab_parent'] = '';
webmaster@1 2281 $item['tab_root'] = $path;
webmaster@1 2282 }
webmaster@1 2283 for ($i = $item['_number_parts'] - 1; $i; $i--) {
webmaster@1 2284 $parent_path = implode('/', array_slice($item['_parts'], 0, $i));
webmaster@1 2285 if (isset($menu[$parent_path])) {
webmaster@1 2286
webmaster@1 2287 $parent = $menu[$parent_path];
webmaster@1 2288
webmaster@1 2289 if (!isset($item['tab_parent'])) {
webmaster@1 2290 // Parent stores the parent of the path.
webmaster@1 2291 $item['tab_parent'] = $parent_path;
webmaster@1 2292 }
webmaster@1 2293 if (!isset($item['tab_root']) && !$parent['_tab']) {
webmaster@1 2294 $item['tab_root'] = $parent_path;
webmaster@1 2295 }
webmaster@5 2296 // If an access callback is not found for a default local task we use
webmaster@5 2297 // the callback from the parent, since we expect them to be identical.
webmaster@5 2298 // In all other cases, the access parameters must be specified.
webmaster@5 2299 if (($item['type'] == MENU_DEFAULT_LOCAL_TASK) && !isset($item['access callback']) && isset($parent['access callback'])) {
webmaster@1 2300 $item['access callback'] = $parent['access callback'];
webmaster@1 2301 if (!isset($item['access arguments']) && isset($parent['access arguments'])) {
webmaster@1 2302 $item['access arguments'] = $parent['access arguments'];
webmaster@1 2303 }
webmaster@1 2304 }
webmaster@1 2305 // Same for page callbacks.
webmaster@1 2306 if (!isset($item['page callback']) && isset($parent['page callback'])) {
webmaster@1 2307 $item['page callback'] = $parent['page callback'];
webmaster@1 2308 if (!isset($item['page arguments']) && isset($parent['page arguments'])) {
webmaster@1 2309 $item['page arguments'] = $parent['page arguments'];
webmaster@1 2310 }
webmaster@1 2311 if (!isset($item['file']) && isset($parent['file'])) {
webmaster@1 2312 $item['file'] = $parent['file'];
webmaster@1 2313 }
webmaster@1 2314 if (!isset($item['file path']) && isset($parent['file path'])) {
webmaster@1 2315 $item['file path'] = $parent['file path'];
webmaster@1 2316 }
webmaster@1 2317 }
webmaster@1 2318 }
webmaster@1 2319 }
webmaster@1 2320 if (!isset($item['access callback']) && isset($item['access arguments'])) {
webmaster@1 2321 // Default callback.
webmaster@1 2322 $item['access callback'] = 'user_access';
webmaster@1 2323 }
webmaster@1 2324 if (!isset($item['access callback']) || empty($item['page callback'])) {
webmaster@1 2325 $item['access callback'] = 0;
webmaster@1 2326 }
webmaster@1 2327 if (is_bool($item['access callback'])) {
webmaster@1 2328 $item['access callback'] = intval($item['access callback']);
webmaster@1 2329 }
webmaster@1 2330
webmaster@1 2331 $item += array(
webmaster@1 2332 'access arguments' => array(),
webmaster@1 2333 'access callback' => '',
webmaster@1 2334 'page arguments' => array(),
webmaster@1 2335 'page callback' => '',
webmaster@1 2336 'block callback' => '',
webmaster@1 2337 'title arguments' => array(),
webmaster@1 2338 'title callback' => 't',
webmaster@1 2339 'description' => '',
webmaster@1 2340 'position' => '',
webmaster@1 2341 'tab_parent' => '',
webmaster@1 2342 'tab_root' => $path,
webmaster@1 2343 'path' => $path,
webmaster@1 2344 'file' => '',
webmaster@1 2345 'file path' => '',
webmaster@1 2346 'include file' => '',
webmaster@1 2347 );
webmaster@1 2348
webmaster@1 2349 // Calculate out the file to be included for each callback, if any.
webmaster@1 2350 if ($item['file']) {
webmaster@1 2351 $file_path = $item['file path'] ? $item['file path'] : drupal_get_path('module', $item['module']);
webmaster@1 2352 $item['include file'] = $file_path .'/'. $item['file'];
webmaster@1 2353 }
webmaster@1 2354
webmaster@1 2355 $title_arguments = $item['title arguments'] ? serialize($item['title arguments']) : '';
webmaster@1 2356 db_query("INSERT INTO {menu_router}
webmaster@1 2357 (path, load_functions, to_arg_functions, access_callback,
webmaster@1 2358 access_arguments, page_callback, page_arguments, fit,
webmaster@1 2359 number_parts, tab_parent, tab_root,
webmaster@1 2360 title, title_callback, title_arguments,
webmaster@1 2361 type, block_callback, description, position, weight, file)
webmaster@1 2362 VALUES ('%s', '%s', '%s', '%s',
webmaster@1 2363 '%s', '%s', '%s', %d,
webmaster@1 2364 %d, '%s', '%s',
webmaster@1 2365 '%s', '%s', '%s',
webmaster@1 2366 %d, '%s', '%s', '%s', %d, '%s')",
webmaster@1 2367 $path, $item['load_functions'], $item['to_arg_functions'], $item['access callback'],
webmaster@1 2368 serialize($item['access arguments']), $item['page callback'], serialize($item['page arguments']), $item['_fit'],
webmaster@1 2369 $item['_number_parts'], $item['tab_parent'], $item['tab_root'],
webmaster@1 2370 $item['title'], $item['title callback'], $title_arguments,
webmaster@1 2371 $item['type'], $item['block callback'], $item['description'], $item['position'], $item['weight'], $item['include file']);
webmaster@1 2372 }
webmaster@1 2373 // Sort the masks so they are in order of descending fit, and store them.
webmaster@1 2374 $masks = array_keys($masks);
webmaster@1 2375 rsort($masks);
webmaster@1 2376 variable_set('menu_masks', $masks);
webmaster@7 2377 cache_set('router:', $menu, 'cache_menu');
webmaster@1 2378 return $menu;
webmaster@1 2379 }
webmaster@1 2380
webmaster@1 2381 /**
webmaster@1 2382 * Returns TRUE if a path is external (e.g. http://example.com).
webmaster@1 2383 */
webmaster@1 2384 function menu_path_is_external($path) {
webmaster@1 2385 $colonpos = strpos($path, ':');
webmaster@1 2386 return $colonpos !== FALSE && !preg_match('![/?#]!', substr($path, 0, $colonpos)) && filter_xss_bad_protocol($path, FALSE) == check_plain($path);
webmaster@1 2387 }
webmaster@1 2388
webmaster@1 2389 /**
webmaster@1 2390 * Checks whether the site is off-line for maintenance.
webmaster@1 2391 *
webmaster@1 2392 * This function will log the current user out and redirect to front page
webmaster@1 2393 * if the current user has no 'administer site configuration' permission.
webmaster@1 2394 *
webmaster@1 2395 * @return
webmaster@1 2396 * FALSE if the site is not off-line or its the login page or the user has
webmaster@1 2397 * 'administer site configuration' permission.
webmaster@1 2398 * TRUE for anonymous users not on the login page if the site is off-line.
webmaster@1 2399 */
webmaster@1 2400 function _menu_site_is_offline() {
webmaster@1 2401 // Check if site is set to off-line mode.
webmaster@1 2402 if (variable_get('site_offline', 0)) {
webmaster@1 2403 // Check if the user has administration privileges.
webmaster@1 2404 if (user_access('administer site configuration')) {
webmaster@1 2405 // Ensure that the off-line message is displayed only once [allowing for
webmaster@1 2406 // page redirects], and specifically suppress its display on the site
webmaster@1 2407 // maintenance page.
webmaster@1 2408 if (drupal_get_normal_path($_GET['q']) != 'admin/settings/site-maintenance') {
webmaster@1 2409 drupal_set_message(t('Operating in off-line mode.'), 'status', FALSE);
webmaster@1 2410 }
webmaster@1 2411 }
webmaster@1 2412 else {
webmaster@1 2413 // Anonymous users get a FALSE at the login prompt, TRUE otherwise.
webmaster@1 2414 if (user_is_anonymous()) {
webmaster@1 2415 return $_GET['q'] != 'user' && $_GET['q'] != 'user/login';
webmaster@1 2416 }
webmaster@1 2417 // Logged in users are unprivileged here, so they are logged out.
webmaster@1 2418 require_once drupal_get_path('module', 'user') .'/user.pages.inc';
webmaster@1 2419 user_logout();
webmaster@1 2420 }
webmaster@1 2421 }
webmaster@1 2422 return FALSE;
webmaster@1 2423 }
webmaster@1 2424
webmaster@1 2425 /**
webmaster@1 2426 * Validates the path of a menu link being created or edited.
webmaster@1 2427 *
webmaster@1 2428 * @return
webmaster@1 2429 * TRUE if it is a valid path AND the current user has access permission,
webmaster@1 2430 * FALSE otherwise.
webmaster@1 2431 */
webmaster@1 2432 function menu_valid_path($form_item) {
webmaster@1 2433 global $menu_admin;
webmaster@1 2434 $item = array();
webmaster@1 2435 $path = $form_item['link_path'];
webmaster@1 2436 // We indicate that a menu administrator is running the menu access check.
webmaster@1 2437 $menu_admin = TRUE;
webmaster@1 2438 if ($path == '<front>' || menu_path_is_external($path)) {
webmaster@1 2439 $item = array('access' => TRUE);
webmaster@1 2440 }
webmaster@1 2441 elseif (preg_match('/\/\%/', $path)) {
webmaster@1 2442 // Path is dynamic (ie 'user/%'), so check directly against menu_router table.
webmaster@1 2443 if ($item = db_fetch_array(db_query("SELECT * FROM {menu_router} where path = '%s' ", $path))) {
webmaster@1 2444 $item['link_path'] = $form_item['link_path'];
webmaster@1 2445 $item['link_title'] = $form_item['link_title'];
webmaster@1 2446 $item['external'] = FALSE;
webmaster@1 2447 $item['options'] = '';
webmaster@1 2448 _menu_link_translate($item);
webmaster@1 2449 }
webmaster@1 2450 }
webmaster@1 2451 else {
webmaster@1 2452 $item = menu_get_item($path);
webmaster@1 2453 }
webmaster@1 2454 $menu_admin = FALSE;
webmaster@1 2455 return $item && $item['access'];
webmaster@1 2456 }
webmaster@1 2457
webmaster@1 2458 /**
webmaster@1 2459 * @} End of "defgroup menu".
webmaster@1 2460 */