annotate includes/menu.inc @ 5:2427550111ae 6.2

Drupal 6.2
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:30:08 +0100
parents 165d43f946a8
children fff6d4c8c043
rev   line source
webmaster@1 1 <?php
webmaster@5 2 // $Id: menu.inc,v 1.255.2.11 2008/04/09 21:11:44 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@1 517 if ($link_translate && $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@1 573
webmaster@1 574 _menu_item_localize($router_item, $map);
webmaster@1 575
webmaster@1 576 return $map;
webmaster@1 577 }
webmaster@1 578
webmaster@1 579 /**
webmaster@1 580 * This function translates the path elements in the map using any to_arg
webmaster@1 581 * helper function. These functions take an argument and return an object.
webmaster@1 582 * See http://drupal.org/node/109153 for more information.
webmaster@1 583 *
webmaster@1 584 * @param map
webmaster@1 585 * An array of path arguments (ex: array('node', '5'))
webmaster@1 586 * @param $to_arg_functions
webmaster@1 587 * An array of helper function (ex: array(2 => 'menu_tail_to_arg'))
webmaster@1 588 */
webmaster@1 589 function _menu_link_map_translate(&$map, $to_arg_functions) {
webmaster@1 590 if ($to_arg_functions) {
webmaster@1 591 $to_arg_functions = unserialize($to_arg_functions);
webmaster@1 592 foreach ($to_arg_functions as $index => $function) {
webmaster@1 593 // Translate place-holders into real values.
webmaster@1 594 $arg = $function(!empty($map[$index]) ? $map[$index] : '', $map, $index);
webmaster@1 595 if (!empty($map[$index]) || isset($arg)) {
webmaster@1 596 $map[$index] = $arg;
webmaster@1 597 }
webmaster@1 598 else {
webmaster@1 599 unset($map[$index]);
webmaster@1 600 }
webmaster@1 601 }
webmaster@1 602 }
webmaster@1 603 }
webmaster@1 604
webmaster@1 605 function menu_tail_to_arg($arg, $map, $index) {
webmaster@1 606 return implode('/', array_slice($map, $index));
webmaster@1 607 }
webmaster@1 608
webmaster@1 609 /**
webmaster@1 610 * This function is similar to _menu_translate() but does link-specific
webmaster@3 611 * preparation such as always calling to_arg functions.
webmaster@1 612 *
webmaster@1 613 * @param $item
webmaster@1 614 * A menu link
webmaster@1 615 * @return
webmaster@1 616 * Returns the map of path arguments with objects loaded as defined in the
webmaster@3 617 * $item['load_functions']:
webmaster@3 618 * - $item['access'] becomes TRUE if the item is accessible, FALSE otherwise.
webmaster@3 619 * - $item['href'] is generated from link_path, possibly by to_arg functions.
webmaster@3 620 * - $item['title'] is generated from link_title, and may be localized.
webmaster@3 621 * - $item['options'] is unserialized; it is also changed within the call
webmaster@3 622 * here to $item['localized_options'] by _menu_item_localize().
webmaster@1 623 */
webmaster@1 624 function _menu_link_translate(&$item) {
webmaster@1 625 $item['options'] = unserialize($item['options']);
webmaster@1 626 if ($item['external']) {
webmaster@1 627 $item['access'] = 1;
webmaster@1 628 $map = array();
webmaster@1 629 $item['href'] = $item['link_path'];
webmaster@1 630 $item['title'] = $item['link_title'];
webmaster@1 631 $item['localized_options'] = $item['options'];
webmaster@1 632 }
webmaster@1 633 else {
webmaster@1 634 $map = explode('/', $item['link_path']);
webmaster@1 635 _menu_link_map_translate($map, $item['to_arg_functions']);
webmaster@1 636 $item['href'] = implode('/', $map);
webmaster@1 637
webmaster@1 638 // Note - skip callbacks without real values for their arguments.
webmaster@1 639 if (strpos($item['href'], '%') !== FALSE) {
webmaster@1 640 $item['access'] = FALSE;
webmaster@1 641 return FALSE;
webmaster@1 642 }
webmaster@1 643 // menu_tree_check_access() may set this ahead of time for links to nodes.
webmaster@1 644 if (!isset($item['access'])) {
webmaster@1 645 if (!_menu_load_objects($item, $map)) {
webmaster@1 646 // An error occurred loading an object.
webmaster@1 647 $item['access'] = FALSE;
webmaster@1 648 return FALSE;
webmaster@1 649 }
webmaster@1 650 _menu_check_access($item, $map);
webmaster@1 651 }
webmaster@1 652
webmaster@1 653 _menu_item_localize($item, $map, TRUE);
webmaster@1 654 }
webmaster@1 655
webmaster@1 656 // Allow other customizations - e.g. adding a page-specific query string to the
webmaster@1 657 // options array. For performance reasons we only invoke this hook if the link
webmaster@1 658 // has the 'alter' flag set in the options array.
webmaster@1 659 if (!empty($item['options']['alter'])) {
webmaster@1 660 drupal_alter('translated_menu_link', $item, $map);
webmaster@1 661 }
webmaster@1 662
webmaster@1 663 return $map;
webmaster@1 664 }
webmaster@1 665
webmaster@1 666 /**
webmaster@1 667 * Get a loaded object from a router item.
webmaster@1 668 *
webmaster@1 669 * menu_get_object() will provide you the current node on paths like node/5,
webmaster@1 670 * node/5/revisions/48 etc. menu_get_object('user') will give you the user
webmaster@1 671 * account on user/5 etc. Note - this function should never be called within a
webmaster@1 672 * _to_arg function (like user_current_to_arg()) since this may result in an
webmaster@1 673 * infinite recursion.
webmaster@1 674 *
webmaster@1 675 * @param $type
webmaster@1 676 * Type of the object. These appear in hook_menu definitons as %type. Core
webmaster@1 677 * provides aggregator_feed, aggregator_category, contact, filter_format,
webmaster@1 678 * forum_term, menu, menu_link, node, taxonomy_vocabulary, user. See the
webmaster@1 679 * relevant {$type}_load function for more on each. Defaults to node.
webmaster@1 680 * @param $position
webmaster@1 681 * The expected position for $type object. For node/%node this is 1, for
webmaster@1 682 * comment/reply/%node this is 2. Defaults to 1.
webmaster@1 683 * @param $path
webmaster@3 684 * See menu_get_item() for more on this. Defaults to the current path.
webmaster@1 685 */
webmaster@1 686 function menu_get_object($type = 'node', $position = 1, $path = NULL) {
webmaster@1 687 $router_item = menu_get_item($path);
webmaster@1 688 if (isset($router_item['load_functions'][$position]) && !empty($router_item['map'][$position]) && $router_item['load_functions'][$position] == $type .'_load') {
webmaster@1 689 return $router_item['map'][$position];
webmaster@1 690 }
webmaster@1 691 }
webmaster@1 692
webmaster@1 693 /**
webmaster@1 694 * Render a menu tree based on the current path.
webmaster@1 695 *
webmaster@1 696 * The tree is expanded based on the current path and dynamic paths are also
webmaster@1 697 * changed according to the defined to_arg functions (for example the 'My account'
webmaster@1 698 * link is changed from user/% to a link with the current user's uid).
webmaster@1 699 *
webmaster@1 700 * @param $menu_name
webmaster@1 701 * The name of the menu.
webmaster@1 702 * @return
webmaster@1 703 * The rendered HTML of that menu on the current page.
webmaster@1 704 */
webmaster@1 705 function menu_tree($menu_name = 'navigation') {
webmaster@1 706 static $menu_output = array();
webmaster@1 707
webmaster@1 708 if (!isset($menu_output[$menu_name])) {
webmaster@1 709 $tree = menu_tree_page_data($menu_name);
webmaster@1 710 $menu_output[$menu_name] = menu_tree_output($tree);
webmaster@1 711 }
webmaster@1 712 return $menu_output[$menu_name];
webmaster@1 713 }
webmaster@1 714
webmaster@1 715 /**
webmaster@1 716 * Returns a rendered menu tree.
webmaster@1 717 *
webmaster@1 718 * @param $tree
webmaster@1 719 * A data structure representing the tree as returned from menu_tree_data.
webmaster@1 720 * @return
webmaster@1 721 * The rendered HTML of that data structure.
webmaster@1 722 */
webmaster@1 723 function menu_tree_output($tree) {
webmaster@1 724 $output = '';
webmaster@1 725 $items = array();
webmaster@1 726
webmaster@1 727 // Pull out just the menu items we are going to render so that we
webmaster@1 728 // get an accurate count for the first/last classes.
webmaster@1 729 foreach ($tree as $data) {
webmaster@1 730 if (!$data['link']['hidden']) {
webmaster@1 731 $items[] = $data;
webmaster@1 732 }
webmaster@1 733 }
webmaster@1 734
webmaster@1 735 $num_items = count($items);
webmaster@1 736 foreach ($items as $i => $data) {
webmaster@1 737 $extra_class = NULL;
webmaster@1 738 if ($i == 0) {
webmaster@1 739 $extra_class = 'first';
webmaster@1 740 }
webmaster@1 741 if ($i == $num_items - 1) {
webmaster@1 742 $extra_class = 'last';
webmaster@1 743 }
webmaster@1 744 $link = theme('menu_item_link', $data['link']);
webmaster@1 745 if ($data['below']) {
webmaster@1 746 $output .= theme('menu_item', $link, $data['link']['has_children'], menu_tree_output($data['below']), $data['link']['in_active_trail'], $extra_class);
webmaster@1 747 }
webmaster@1 748 else {
webmaster@1 749 $output .= theme('menu_item', $link, $data['link']['has_children'], '', $data['link']['in_active_trail'], $extra_class);
webmaster@1 750 }
webmaster@1 751 }
webmaster@1 752 return $output ? theme('menu_tree', $output) : '';
webmaster@1 753 }
webmaster@1 754
webmaster@1 755 /**
webmaster@1 756 * Get the data structure representing a named menu tree.
webmaster@1 757 *
webmaster@1 758 * Since this can be the full tree including hidden items, the data returned
webmaster@1 759 * may be used for generating an an admin interface or a select.
webmaster@1 760 *
webmaster@1 761 * @param $menu_name
webmaster@1 762 * The named menu links to return
webmaster@1 763 * @param $item
webmaster@1 764 * A fully loaded menu link, or NULL. If a link is supplied, only the
webmaster@1 765 * path to root will be included in the returned tree- as if this link
webmaster@1 766 * represented the current page in a visible menu.
webmaster@1 767 * @return
webmaster@1 768 * An tree of menu links in an array, in the order they should be rendered.
webmaster@1 769 */
webmaster@1 770 function menu_tree_all_data($menu_name = 'navigation', $item = NULL) {
webmaster@1 771 static $tree = array();
webmaster@1 772
webmaster@1 773 // Use $mlid as a flag for whether the data being loaded is for the whole tree.
webmaster@1 774 $mlid = isset($item['mlid']) ? $item['mlid'] : 0;
webmaster@5 775 // Generate a cache ID (cid) specific for this $menu_name and $item.
webmaster@5 776 $cid = 'links:'. $menu_name .':all-cid:'. $mlid;
webmaster@1 777
webmaster@1 778 if (!isset($tree[$cid])) {
webmaster@1 779 // If the static variable doesn't have the data, check {cache_menu}.
webmaster@1 780 $cache = cache_get($cid, 'cache_menu');
webmaster@1 781 if ($cache && isset($cache->data)) {
webmaster@5 782 // If the cache entry exists, it will just be the cid for the actual data.
webmaster@5 783 // This avoids duplication of large amounts of data.
webmaster@5 784 $cache = cache_get($cache->data, 'cache_menu');
webmaster@5 785 if ($cache && isset($cache->data)) {
webmaster@5 786 $data = $cache->data;
webmaster@5 787 }
webmaster@1 788 }
webmaster@5 789 // If the tree data was not in the cache, $data will be NULL.
webmaster@5 790 if (!isset($data)) {
webmaster@1 791 // Build and run the query, and build the tree.
webmaster@1 792 if ($mlid) {
webmaster@1 793 // The tree is for a single item, so we need to match the values in its
webmaster@1 794 // p columns and 0 (the top level) with the plid values of other links.
webmaster@1 795 $args = array(0);
webmaster@1 796 for ($i = 1; $i < MENU_MAX_DEPTH; $i++) {
webmaster@1 797 $args[] = $item["p$i"];
webmaster@1 798 }
webmaster@1 799 $args = array_unique($args);
webmaster@1 800 $placeholders = implode(', ', array_fill(0, count($args), '%d'));
webmaster@1 801 $where = ' AND ml.plid IN ('. $placeholders .')';
webmaster@1 802 $parents = $args;
webmaster@1 803 $parents[] = $item['mlid'];
webmaster@1 804 }
webmaster@1 805 else {
webmaster@1 806 // Get all links in this menu.
webmaster@1 807 $where = '';
webmaster@1 808 $args = array();
webmaster@1 809 $parents = array();
webmaster@1 810 }
webmaster@1 811 array_unshift($args, $menu_name);
webmaster@1 812 // Select the links from the table, and recursively build the tree. We
webmaster@1 813 // LEFT JOIN since there is no match in {menu_router} for an external
webmaster@1 814 // link.
webmaster@1 815 $data['tree'] = menu_tree_data(db_query("
webmaster@1 816 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 817 FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
webmaster@1 818 WHERE ml.menu_name = '%s'". $where ."
webmaster@1 819 ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args), $parents);
webmaster@1 820 $data['node_links'] = array();
webmaster@1 821 menu_tree_collect_node_links($data['tree'], $data['node_links']);
webmaster@5 822 // Cache the data, if it is not already in the cache.
webmaster@5 823 $tree_cid = _menu_tree_cid($menu_name, $data);
webmaster@5 824 if (!cache_get($tree_cid, 'cache_menu')) {
webmaster@5 825 cache_set($tree_cid, $data, 'cache_menu');
webmaster@5 826 }
webmaster@5 827 // Cache the cid of the (shared) data using the menu and item-specific cid.
webmaster@5 828 cache_set($cid, $tree_cid, 'cache_menu');
webmaster@1 829 }
webmaster@1 830 // Check access for the current user to each item in the tree.
webmaster@1 831 menu_tree_check_access($data['tree'], $data['node_links']);
webmaster@1 832 $tree[$cid] = $data['tree'];
webmaster@1 833 }
webmaster@1 834
webmaster@1 835 return $tree[$cid];
webmaster@1 836 }
webmaster@1 837
webmaster@1 838 /**
webmaster@1 839 * Get the data structure representing a named menu tree, based on the current page.
webmaster@1 840 *
webmaster@1 841 * The tree order is maintained by storing each parent in an individual
webmaster@1 842 * field, see http://drupal.org/node/141866 for more.
webmaster@1 843 *
webmaster@1 844 * @param $menu_name
webmaster@1 845 * The named menu links to return
webmaster@1 846 * @return
webmaster@1 847 * An array of menu links, in the order they should be rendered. The array
webmaster@1 848 * is a list of associative arrays -- these have two keys, link and below.
webmaster@1 849 * link is a menu item, ready for theming as a link. Below represents the
webmaster@1 850 * submenu below the link if there is one, and it is a subtree that has the
webmaster@1 851 * same structure described for the top-level array.
webmaster@1 852 */
webmaster@1 853 function menu_tree_page_data($menu_name = 'navigation') {
webmaster@1 854 static $tree = array();
webmaster@1 855
webmaster@1 856 // Load the menu item corresponding to the current page.
webmaster@1 857 if ($item = menu_get_item()) {
webmaster@5 858 // Generate a cache ID (cid) specific for this page.
webmaster@5 859 $cid = 'links:'. $menu_name .':page-cid:'. $item['href'] .':'. (int)$item['access'];
webmaster@1 860
webmaster@1 861 if (!isset($tree[$cid])) {
webmaster@1 862 // If the static variable doesn't have the data, check {cache_menu}.
webmaster@1 863 $cache = cache_get($cid, 'cache_menu');
webmaster@1 864 if ($cache && isset($cache->data)) {
webmaster@5 865 // If the cache entry exists, it will just be the cid for the actual data.
webmaster@5 866 // This avoids duplication of large amounts of data.
webmaster@5 867 $cache = cache_get($cache->data, 'cache_menu');
webmaster@5 868 if ($cache && isset($cache->data)) {
webmaster@5 869 $data = $cache->data;
webmaster@5 870 }
webmaster@1 871 }
webmaster@5 872 // If the tree data was not in the cache, $data will be NULL.
webmaster@5 873 if (!isset($data)) {
webmaster@1 874 // Build and run the query, and build the tree.
webmaster@1 875 if ($item['access']) {
webmaster@1 876 // Check whether a menu link exists that corresponds to the current path.
webmaster@1 877 $args = array($menu_name, $item['href']);
webmaster@1 878 $placeholders = "'%s'";
webmaster@1 879 if (drupal_is_front_page()) {
webmaster@1 880 $args[] = '<front>';
webmaster@1 881 $placeholders .= ", '%s'";
webmaster@1 882 }
webmaster@1 883 $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 884
webmaster@1 885 if (empty($parents)) {
webmaster@1 886 // If no link exists, we may be on a local task that's not in the links.
webmaster@1 887 // TODO: Handle the case like a local task on a specific node in the menu.
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 = '%s'", $menu_name, $item['tab_root']));
webmaster@1 889 }
webmaster@1 890 // We always want all the top-level links with plid == 0.
webmaster@1 891 $parents[] = '0';
webmaster@1 892
webmaster@1 893 // Use array_values() so that the indices are numeric for array_merge().
webmaster@1 894 $args = $parents = array_unique(array_values($parents));
webmaster@1 895 $placeholders = implode(', ', array_fill(0, count($args), '%d'));
webmaster@1 896 $expanded = variable_get('menu_expanded', array());
webmaster@1 897 // Check whether the current menu has any links set to be expanded.
webmaster@1 898 if (in_array($menu_name, $expanded)) {
webmaster@1 899 // Collect all the links set to be expanded, and then add all of
webmaster@1 900 // their children to the list as well.
webmaster@1 901 do {
webmaster@1 902 $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 903 $num_rows = FALSE;
webmaster@1 904 while ($item = db_fetch_array($result)) {
webmaster@1 905 $args[] = $item['mlid'];
webmaster@1 906 $num_rows = TRUE;
webmaster@1 907 }
webmaster@1 908 $placeholders = implode(', ', array_fill(0, count($args), '%d'));
webmaster@1 909 } while ($num_rows);
webmaster@1 910 }
webmaster@1 911 array_unshift($args, $menu_name);
webmaster@1 912 }
webmaster@1 913 else {
webmaster@1 914 // Show only the top-level menu items when access is denied.
webmaster@1 915 $args = array($menu_name, '0');
webmaster@1 916 $placeholders = '%d';
webmaster@1 917 $parents = array();
webmaster@1 918 }
webmaster@1 919 // Select the links from the table, and recursively build the tree. We
webmaster@1 920 // LEFT JOIN since there is no match in {menu_router} for an external
webmaster@1 921 // link.
webmaster@1 922 $data['tree'] = menu_tree_data(db_query("
webmaster@1 923 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 924 FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
webmaster@1 925 WHERE ml.menu_name = '%s' AND ml.plid IN (". $placeholders .")
webmaster@1 926 ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args), $parents);
webmaster@1 927 $data['node_links'] = array();
webmaster@1 928 menu_tree_collect_node_links($data['tree'], $data['node_links']);
webmaster@5 929 // Cache the data, if it is not already in the cache.
webmaster@5 930 $tree_cid = _menu_tree_cid($menu_name, $data);
webmaster@5 931 if (!cache_get($tree_cid, 'cache_menu')) {
webmaster@5 932 cache_set($tree_cid, $data, 'cache_menu');
webmaster@5 933 }
webmaster@5 934 // Cache the cid of the (shared) data using the page-specific cid.
webmaster@5 935 cache_set($cid, $tree_cid, 'cache_menu');
webmaster@1 936 }
webmaster@1 937 // Check access for the current user to each item in the tree.
webmaster@1 938 menu_tree_check_access($data['tree'], $data['node_links']);
webmaster@1 939 $tree[$cid] = $data['tree'];
webmaster@1 940 }
webmaster@1 941 return $tree[$cid];
webmaster@1 942 }
webmaster@1 943
webmaster@1 944 return array();
webmaster@1 945 }
webmaster@1 946
webmaster@1 947 /**
webmaster@5 948 * Helper function - compute the real cache ID for menu tree data.
webmaster@5 949 */
webmaster@5 950 function _menu_tree_cid($menu_name, $data) {
webmaster@5 951 return 'links:'. $menu_name .':tree-data:'. md5(serialize($data));
webmaster@5 952 }
webmaster@5 953
webmaster@5 954 /**
webmaster@1 955 * Recursive helper function - collect node links.
webmaster@1 956 */
webmaster@1 957 function menu_tree_collect_node_links(&$tree, &$node_links) {
webmaster@1 958 foreach ($tree as $key => $v) {
webmaster@1 959 if ($tree[$key]['link']['router_path'] == 'node/%') {
webmaster@1 960 $nid = substr($tree[$key]['link']['link_path'], 5);
webmaster@1 961 if (is_numeric($nid)) {
webmaster@1 962 $node_links[$nid][$tree[$key]['link']['mlid']] = &$tree[$key]['link'];
webmaster@1 963 $tree[$key]['link']['access'] = FALSE;
webmaster@1 964 }
webmaster@1 965 }
webmaster@1 966 if ($tree[$key]['below']) {
webmaster@1 967 menu_tree_collect_node_links($tree[$key]['below'], $node_links);
webmaster@1 968 }
webmaster@1 969 }
webmaster@1 970 }
webmaster@1 971
webmaster@1 972 /**
webmaster@1 973 * Check access and perform other dynamic operations for each link in the tree.
webmaster@1 974 */
webmaster@1 975 function menu_tree_check_access(&$tree, $node_links = array()) {
webmaster@1 976
webmaster@1 977 if ($node_links) {
webmaster@1 978 // Use db_rewrite_sql to evaluate view access without loading each full node.
webmaster@1 979 $nids = array_keys($node_links);
webmaster@1 980 $placeholders = '%d'. str_repeat(', %d', count($nids) - 1);
webmaster@1 981 $result = db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.status = 1 AND n.nid IN (". $placeholders .")"), $nids);
webmaster@1 982 while ($node = db_fetch_array($result)) {
webmaster@1 983 $nid = $node['nid'];
webmaster@1 984 foreach ($node_links[$nid] as $mlid => $link) {
webmaster@1 985 $node_links[$nid][$mlid]['access'] = TRUE;
webmaster@1 986 }
webmaster@1 987 }
webmaster@1 988 }
webmaster@1 989 _menu_tree_check_access($tree);
webmaster@1 990 return;
webmaster@1 991 }
webmaster@1 992
webmaster@1 993 /**
webmaster@1 994 * Recursive helper function for menu_tree_check_access()
webmaster@1 995 */
webmaster@1 996 function _menu_tree_check_access(&$tree) {
webmaster@1 997 $new_tree = array();
webmaster@1 998 foreach ($tree as $key => $v) {
webmaster@1 999 $item = &$tree[$key]['link'];
webmaster@1 1000 _menu_link_translate($item);
webmaster@1 1001 if ($item['access']) {
webmaster@1 1002 if ($tree[$key]['below']) {
webmaster@1 1003 _menu_tree_check_access($tree[$key]['below']);
webmaster@1 1004 }
webmaster@1 1005 // The weights are made a uniform 5 digits by adding 50000 as an offset.
webmaster@1 1006 // After _menu_link_translate(), $item['title'] has the localized link title.
webmaster@1 1007 // Adding the mlid to the end of the index insures that it is unique.
webmaster@1 1008 $new_tree[(50000 + $item['weight']) .' '. $item['title'] .' '. $item['mlid']] = $tree[$key];
webmaster@1 1009 }
webmaster@1 1010 }
webmaster@1 1011 // Sort siblings in the tree based on the weights and localized titles.
webmaster@1 1012 ksort($new_tree);
webmaster@1 1013 $tree = $new_tree;
webmaster@1 1014 }
webmaster@1 1015
webmaster@1 1016 /**
webmaster@1 1017 * Build the data representing a menu tree.
webmaster@1 1018 *
webmaster@1 1019 * @param $result
webmaster@1 1020 * The database result.
webmaster@1 1021 * @param $parents
webmaster@1 1022 * An array of the plid values that represent the path from the current page
webmaster@1 1023 * to the root of the menu tree.
webmaster@1 1024 * @param $depth
webmaster@1 1025 * The depth of the current menu tree.
webmaster@1 1026 * @return
webmaster@1 1027 * See menu_tree_page_data for a description of the data structure.
webmaster@1 1028 */
webmaster@1 1029 function menu_tree_data($result = NULL, $parents = array(), $depth = 1) {
webmaster@1 1030 list(, $tree) = _menu_tree_data($result, $parents, $depth);
webmaster@1 1031 return $tree;
webmaster@1 1032 }
webmaster@1 1033
webmaster@1 1034 /**
webmaster@1 1035 * Recursive helper function to build the data representing a menu tree.
webmaster@1 1036 *
webmaster@1 1037 * The function is a bit complex because the rendering of an item depends on
webmaster@1 1038 * the next menu item. So we are always rendering the element previously
webmaster@1 1039 * processed not the current one.
webmaster@1 1040 */
webmaster@1 1041 function _menu_tree_data($result, $parents, $depth, $previous_element = '') {
webmaster@1 1042 $remnant = NULL;
webmaster@1 1043 $tree = array();
webmaster@1 1044 while ($item = db_fetch_array($result)) {
webmaster@1 1045 // We need to determine if we're on the path to root so we can later build
webmaster@1 1046 // the correct active trail and breadcrumb.
webmaster@1 1047 $item['in_active_trail'] = in_array($item['mlid'], $parents);
webmaster@1 1048 // The current item is the first in a new submenu.
webmaster@1 1049 if ($item['depth'] > $depth) {
webmaster@1 1050 // _menu_tree returns an item and the menu tree structure.
webmaster@1 1051 list($item, $below) = _menu_tree_data($result, $parents, $item['depth'], $item);
webmaster@1 1052 if ($previous_element) {
webmaster@1 1053 $tree[$previous_element['mlid']] = array(
webmaster@1 1054 'link' => $previous_element,
webmaster@1 1055 'below' => $below,
webmaster@1 1056 );
webmaster@1 1057 }
webmaster@1 1058 else {
webmaster@1 1059 $tree = $below;
webmaster@1 1060 }
webmaster@1 1061 // We need to fall back one level.
webmaster@1 1062 if (!isset($item) || $item['depth'] < $depth) {
webmaster@1 1063 return array($item, $tree);
webmaster@1 1064 }
webmaster@1 1065 // This will be the link to be output in the next iteration.
webmaster@1 1066 $previous_element = $item;
webmaster@1 1067 }
webmaster@1 1068 // We are at the same depth, so we use the previous element.
webmaster@1 1069 elseif ($item['depth'] == $depth) {
webmaster@1 1070 if ($previous_element) {
webmaster@1 1071 // Only the first time.
webmaster@1 1072 $tree[$previous_element['mlid']] = array(
webmaster@1 1073 'link' => $previous_element,
webmaster@1 1074 'below' => FALSE,
webmaster@1 1075 );
webmaster@1 1076 }
webmaster@1 1077 // This will be the link to be output in the next iteration.
webmaster@1 1078 $previous_element = $item;
webmaster@1 1079 }
webmaster@1 1080 // The submenu ended with the previous item, so pass back the current item.
webmaster@1 1081 else {
webmaster@1 1082 $remnant = $item;
webmaster@1 1083 break;
webmaster@1 1084 }
webmaster@1 1085 }
webmaster@1 1086 if ($previous_element) {
webmaster@1 1087 // We have one more link dangling.
webmaster@1 1088 $tree[$previous_element['mlid']] = array(
webmaster@1 1089 'link' => $previous_element,
webmaster@1 1090 'below' => FALSE,
webmaster@1 1091 );
webmaster@1 1092 }
webmaster@1 1093 return array($remnant, $tree);
webmaster@1 1094 }
webmaster@1 1095
webmaster@1 1096 /**
webmaster@1 1097 * Generate the HTML output for a single menu link.
webmaster@1 1098 *
webmaster@1 1099 * @ingroup themeable
webmaster@1 1100 */
webmaster@1 1101 function theme_menu_item_link($link) {
webmaster@1 1102 if (empty($link['localized_options'])) {
webmaster@1 1103 $link['localized_options'] = array();
webmaster@1 1104 }
webmaster@1 1105
webmaster@1 1106 return l($link['title'], $link['href'], $link['localized_options']);
webmaster@1 1107 }
webmaster@1 1108
webmaster@1 1109 /**
webmaster@1 1110 * Generate the HTML output for a menu tree
webmaster@1 1111 *
webmaster@1 1112 * @ingroup themeable
webmaster@1 1113 */
webmaster@1 1114 function theme_menu_tree($tree) {
webmaster@1 1115 return '<ul class="menu">'. $tree .'</ul>';
webmaster@1 1116 }
webmaster@1 1117
webmaster@1 1118 /**
webmaster@1 1119 * Generate the HTML output for a menu item and submenu.
webmaster@1 1120 *
webmaster@1 1121 * @ingroup themeable
webmaster@1 1122 */
webmaster@1 1123 function theme_menu_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {
webmaster@1 1124 $class = ($menu ? 'expanded' : ($has_children ? 'collapsed' : 'leaf'));
webmaster@1 1125 if (!empty($extra_class)) {
webmaster@1 1126 $class .= ' '. $extra_class;
webmaster@1 1127 }
webmaster@1 1128 if ($in_active_trail) {
webmaster@1 1129 $class .= ' active-trail';
webmaster@1 1130 }
webmaster@1 1131 return '<li class="'. $class .'">'. $link . $menu ."</li>\n";
webmaster@1 1132 }
webmaster@1 1133
webmaster@1 1134 /**
webmaster@1 1135 * Generate the HTML output for a single local task link.
webmaster@1 1136 *
webmaster@1 1137 * @ingroup themeable
webmaster@1 1138 */
webmaster@1 1139 function theme_menu_local_task($link, $active = FALSE) {
webmaster@1 1140 return '<li '. ($active ? 'class="active" ' : '') .'>'. $link ."</li>\n";
webmaster@1 1141 }
webmaster@1 1142
webmaster@1 1143 /**
webmaster@1 1144 * Generates elements for the $arg array in the help hook.
webmaster@1 1145 */
webmaster@1 1146 function drupal_help_arg($arg = array()) {
webmaster@1 1147 // Note - the number of empty elements should be > MENU_MAX_PARTS.
webmaster@1 1148 return $arg + array('', '', '', '', '', '', '', '', '', '', '', '');
webmaster@1 1149 }
webmaster@1 1150
webmaster@1 1151 /**
webmaster@1 1152 * Returns the help associated with the active menu item.
webmaster@1 1153 */
webmaster@1 1154 function menu_get_active_help() {
webmaster@1 1155 $output = '';
webmaster@1 1156 $router_path = menu_tab_root_path();
webmaster@1 1157
webmaster@1 1158 $arg = drupal_help_arg(arg(NULL));
webmaster@1 1159 $empty_arg = drupal_help_arg();
webmaster@1 1160
webmaster@1 1161 foreach (module_list() as $name) {
webmaster@1 1162 if (module_hook($name, 'help')) {
webmaster@1 1163 // Lookup help for this path.
webmaster@1 1164 if ($help = module_invoke($name, 'help', $router_path, $arg)) {
webmaster@1 1165 $output .= $help ."\n";
webmaster@1 1166 }
webmaster@1 1167 // Add "more help" link on admin pages if the module provides a
webmaster@1 1168 // standalone help page.
webmaster@1 1169 if ($arg[0] == "admin" && module_exists('help') && module_invoke($name, 'help', 'admin/help#'. $arg[2], $empty_arg) && $help) {
webmaster@1 1170 $output .= theme("more_help_link", url('admin/help/'. $arg[2]));
webmaster@1 1171 }
webmaster@1 1172 }
webmaster@1 1173 }
webmaster@1 1174 return $output;
webmaster@1 1175 }
webmaster@1 1176
webmaster@1 1177 /**
webmaster@1 1178 * Build a list of named menus.
webmaster@1 1179 */
webmaster@1 1180 function menu_get_names($reset = FALSE) {
webmaster@1 1181 static $names;
webmaster@1 1182
webmaster@1 1183 if ($reset || empty($names)) {
webmaster@1 1184 $names = array();
webmaster@1 1185 $result = db_query("SELECT DISTINCT(menu_name) FROM {menu_links} ORDER BY menu_name");
webmaster@1 1186 while ($name = db_fetch_array($result)) {
webmaster@1 1187 $names[] = $name['menu_name'];
webmaster@1 1188 }
webmaster@1 1189 }
webmaster@1 1190 return $names;
webmaster@1 1191 }
webmaster@1 1192
webmaster@1 1193 /**
webmaster@1 1194 * Return an array containing the names of system-defined (default) menus.
webmaster@1 1195 */
webmaster@1 1196 function menu_list_system_menus() {
webmaster@1 1197 return array('navigation', 'primary-links', 'secondary-links');
webmaster@1 1198 }
webmaster@1 1199
webmaster@1 1200 /**
webmaster@1 1201 * Return an array of links to be rendered as the Primary links.
webmaster@1 1202 */
webmaster@1 1203 function menu_primary_links() {
webmaster@1 1204 return menu_navigation_links(variable_get('menu_primary_links_source', 'primary-links'));
webmaster@1 1205 }
webmaster@1 1206
webmaster@1 1207 /**
webmaster@1 1208 * Return an array of links to be rendered as the Secondary links.
webmaster@1 1209 */
webmaster@1 1210 function menu_secondary_links() {
webmaster@1 1211
webmaster@1 1212 // If the secondary menu source is set as the primary menu, we display the
webmaster@1 1213 // second level of the primary menu.
webmaster@1 1214 if (variable_get('menu_secondary_links_source', 'secondary-links') == variable_get('menu_primary_links_source', 'primary-links')) {
webmaster@1 1215 return menu_navigation_links(variable_get('menu_primary_links_source', 'primary-links'), 1);
webmaster@1 1216 }
webmaster@1 1217 else {
webmaster@1 1218 return menu_navigation_links(variable_get('menu_secondary_links_source', 'secondary-links'), 0);
webmaster@1 1219 }
webmaster@1 1220 }
webmaster@1 1221
webmaster@1 1222 /**
webmaster@1 1223 * Return an array of links for a navigation menu.
webmaster@1 1224 *
webmaster@1 1225 * @param $menu_name
webmaster@1 1226 * The name of the menu.
webmaster@1 1227 * @param $level
webmaster@1 1228 * Optional, the depth of the menu to be returned.
webmaster@1 1229 * @return
webmaster@1 1230 * An array of links of the specified menu and level.
webmaster@1 1231 */
webmaster@1 1232 function menu_navigation_links($menu_name, $level = 0) {
webmaster@1 1233 // Don't even bother querying the menu table if no menu is specified.
webmaster@1 1234 if (empty($menu_name)) {
webmaster@1 1235 return array();
webmaster@1 1236 }
webmaster@1 1237
webmaster@1 1238 // Get the menu hierarchy for the current page.
webmaster@1 1239 $tree = menu_tree_page_data($menu_name);
webmaster@1 1240
webmaster@1 1241 // Go down the active trail until the right level is reached.
webmaster@1 1242 while ($level-- > 0 && $tree) {
webmaster@1 1243 // Loop through the current level's items until we find one that is in trail.
webmaster@1 1244 while ($item = array_shift($tree)) {
webmaster@1 1245 if ($item['link']['in_active_trail']) {
webmaster@1 1246 // If the item is in the active trail, we continue in the subtree.
webmaster@1 1247 $tree = empty($item['below']) ? array() : $item['below'];
webmaster@1 1248 break;
webmaster@1 1249 }
webmaster@1 1250 }
webmaster@1 1251 }
webmaster@1 1252
webmaster@1 1253 // Create a single level of links.
webmaster@1 1254 $links = array();
webmaster@1 1255 foreach ($tree as $item) {
webmaster@1 1256 if (!$item['link']['hidden']) {
webmaster@1 1257 $l = $item['link']['localized_options'];
webmaster@1 1258 $l['href'] = $item['link']['href'];
webmaster@1 1259 $l['title'] = $item['link']['title'];
webmaster@1 1260 // Keyed with unique menu id to generate classes from theme_links().
webmaster@1 1261 $links['menu-'. $item['link']['mlid']] = $l;
webmaster@1 1262 }
webmaster@1 1263 }
webmaster@1 1264 return $links;
webmaster@1 1265 }
webmaster@1 1266
webmaster@1 1267 /**
webmaster@1 1268 * Collects the local tasks (tabs) for a given level.
webmaster@1 1269 *
webmaster@1 1270 * @param $level
webmaster@1 1271 * The level of tasks you ask for. Primary tasks are 0, secondary are 1.
webmaster@1 1272 * @param $return_root
webmaster@1 1273 * Whether to return the root path for the current page.
webmaster@1 1274 * @return
webmaster@1 1275 * Themed output corresponding to the tabs of the requested level, or
webmaster@1 1276 * router path if $return_root == TRUE. This router path corresponds to
webmaster@1 1277 * a parent tab, if the current page is a default local task.
webmaster@1 1278 */
webmaster@1 1279 function menu_local_tasks($level = 0, $return_root = FALSE) {
webmaster@1 1280 static $tabs;
webmaster@1 1281 static $root_path;
webmaster@1 1282
webmaster@1 1283 if (!isset($tabs)) {
webmaster@1 1284 $tabs = array();
webmaster@1 1285
webmaster@1 1286 $router_item = menu_get_item();
webmaster@1 1287 if (!$router_item || !$router_item['access']) {
webmaster@1 1288 return '';
webmaster@1 1289 }
webmaster@1 1290 // Get all tabs and the root page.
webmaster@1 1291 $result = db_query("SELECT * FROM {menu_router} WHERE tab_root = '%s' ORDER BY weight, title", $router_item['tab_root']);
webmaster@1 1292 $map = arg();
webmaster@1 1293 $children = array();
webmaster@1 1294 $tasks = array();
webmaster@1 1295 $root_path = $router_item['path'];
webmaster@1 1296
webmaster@1 1297 while ($item = db_fetch_array($result)) {
webmaster@1 1298 _menu_translate($item, $map, TRUE);
webmaster@1 1299 if ($item['tab_parent']) {
webmaster@1 1300 // All tabs, but not the root page.
webmaster@1 1301 $children[$item['tab_parent']][$item['path']] = $item;
webmaster@1 1302 }
webmaster@1 1303 // Store the translated item for later use.
webmaster@1 1304 $tasks[$item['path']] = $item;
webmaster@1 1305 }
webmaster@1 1306
webmaster@1 1307 // Find all tabs below the current path.
webmaster@1 1308 $path = $router_item['path'];
webmaster@1 1309 // Tab parenting may skip levels, so the number of parts in the path may not
webmaster@1 1310 // equal the depth. Thus we use the $depth counter (offset by 1000 for ksort).
webmaster@1 1311 $depth = 1001;
webmaster@1 1312 while (isset($children[$path])) {
webmaster@1 1313 $tabs_current = '';
webmaster@1 1314 $next_path = '';
webmaster@1 1315 $count = 0;
webmaster@1 1316 foreach ($children[$path] as $item) {
webmaster@1 1317 if ($item['access']) {
webmaster@1 1318 $count++;
webmaster@1 1319 // The default task is always active.
webmaster@1 1320 if ($item['type'] == MENU_DEFAULT_LOCAL_TASK) {
webmaster@1 1321 // Find the first parent which is not a default local task.
webmaster@1 1322 for ($p = $item['tab_parent']; $tasks[$p]['type'] == MENU_DEFAULT_LOCAL_TASK; $p = $tasks[$p]['tab_parent']);
webmaster@1 1323 $link = theme('menu_item_link', array('href' => $tasks[$p]['href']) + $item);
webmaster@1 1324 $tabs_current .= theme('menu_local_task', $link, TRUE);
webmaster@1 1325 $next_path = $item['path'];
webmaster@1 1326 }
webmaster@1 1327 else {
webmaster@1 1328 $link = theme('menu_item_link', $item);
webmaster@1 1329 $tabs_current .= theme('menu_local_task', $link);
webmaster@1 1330 }
webmaster@1 1331 }
webmaster@1 1332 }
webmaster@1 1333 $path = $next_path;
webmaster@1 1334 $tabs[$depth]['count'] = $count;
webmaster@1 1335 $tabs[$depth]['output'] = $tabs_current;
webmaster@1 1336 $depth++;
webmaster@1 1337 }
webmaster@1 1338
webmaster@1 1339 // Find all tabs at the same level or above the current one.
webmaster@1 1340 $parent = $router_item['tab_parent'];
webmaster@1 1341 $path = $router_item['path'];
webmaster@1 1342 $current = $router_item;
webmaster@1 1343 $depth = 1000;
webmaster@1 1344 while (isset($children[$parent])) {
webmaster@1 1345 $tabs_current = '';
webmaster@1 1346 $next_path = '';
webmaster@1 1347 $next_parent = '';
webmaster@1 1348 $count = 0;
webmaster@1 1349 foreach ($children[$parent] as $item) {
webmaster@1 1350 if ($item['access']) {
webmaster@1 1351 $count++;
webmaster@1 1352 if ($item['type'] == MENU_DEFAULT_LOCAL_TASK) {
webmaster@1 1353 // Find the first parent which is not a default local task.
webmaster@1 1354 for ($p = $item['tab_parent']; $tasks[$p]['type'] == MENU_DEFAULT_LOCAL_TASK; $p = $tasks[$p]['tab_parent']);
webmaster@1 1355 $link = theme('menu_item_link', array('href' => $tasks[$p]['href']) + $item);
webmaster@1 1356 if ($item['path'] == $router_item['path']) {
webmaster@1 1357 $root_path = $tasks[$p]['path'];
webmaster@1 1358 }
webmaster@1 1359 }
webmaster@1 1360 else {
webmaster@1 1361 $link = theme('menu_item_link', $item);
webmaster@1 1362 }
webmaster@1 1363 // We check for the active tab.
webmaster@1 1364 if ($item['path'] == $path) {
webmaster@1 1365 $tabs_current .= theme('menu_local_task', $link, TRUE);
webmaster@1 1366 $next_path = $item['tab_parent'];
webmaster@1 1367 if (isset($tasks[$next_path])) {
webmaster@1 1368 $next_parent = $tasks[$next_path]['tab_parent'];
webmaster@1 1369 }
webmaster@1 1370 }
webmaster@1 1371 else {
webmaster@1 1372 $tabs_current .= theme('menu_local_task', $link);
webmaster@1 1373 }
webmaster@1 1374 }
webmaster@1 1375 }
webmaster@1 1376 $path = $next_path;
webmaster@1 1377 $parent = $next_parent;
webmaster@1 1378 $tabs[$depth]['count'] = $count;
webmaster@1 1379 $tabs[$depth]['output'] = $tabs_current;
webmaster@1 1380 $depth--;
webmaster@1 1381 }
webmaster@1 1382 // Sort by depth.
webmaster@1 1383 ksort($tabs);
webmaster@1 1384 // Remove the depth, we are interested only in their relative placement.
webmaster@1 1385 $tabs = array_values($tabs);
webmaster@1 1386 }
webmaster@1 1387
webmaster@1 1388 if ($return_root) {
webmaster@1 1389 return $root_path;
webmaster@1 1390 }
webmaster@1 1391 else {
webmaster@1 1392 // We do not display single tabs.
webmaster@1 1393 return (isset($tabs[$level]) && $tabs[$level]['count'] > 1) ? $tabs[$level]['output'] : '';
webmaster@1 1394 }
webmaster@1 1395 }
webmaster@1 1396
webmaster@1 1397 /**
webmaster@1 1398 * Returns the rendered local tasks at the top level.
webmaster@1 1399 */
webmaster@1 1400 function menu_primary_local_tasks() {
webmaster@1 1401 return menu_local_tasks(0);
webmaster@1 1402 }
webmaster@1 1403
webmaster@1 1404 /**
webmaster@1 1405 * Returns the rendered local tasks at the second level.
webmaster@1 1406 */
webmaster@1 1407 function menu_secondary_local_tasks() {
webmaster@1 1408 return menu_local_tasks(1);
webmaster@1 1409 }
webmaster@1 1410
webmaster@1 1411 /**
webmaster@1 1412 * Returns the router path, or the path of the parent tab of a default local task.
webmaster@1 1413 */
webmaster@1 1414 function menu_tab_root_path() {
webmaster@1 1415 return menu_local_tasks(0, TRUE);
webmaster@1 1416 }
webmaster@1 1417
webmaster@1 1418 /**
webmaster@1 1419 * Returns the rendered local tasks. The default implementation renders them as tabs.
webmaster@1 1420 *
webmaster@1 1421 * @ingroup themeable
webmaster@1 1422 */
webmaster@1 1423 function theme_menu_local_tasks() {
webmaster@1 1424 $output = '';
webmaster@1 1425
webmaster@1 1426 if ($primary = menu_primary_local_tasks()) {
webmaster@1 1427 $output .= "<ul class=\"tabs primary\">\n". $primary ."</ul>\n";
webmaster@1 1428 }
webmaster@1 1429 if ($secondary = menu_secondary_local_tasks()) {
webmaster@1 1430 $output .= "<ul class=\"tabs secondary\">\n". $secondary ."</ul>\n";
webmaster@1 1431 }
webmaster@1 1432
webmaster@1 1433 return $output;
webmaster@1 1434 }
webmaster@1 1435
webmaster@1 1436 /**
webmaster@1 1437 * Set (or get) the active menu for the current page - determines the active trail.
webmaster@1 1438 */
webmaster@1 1439 function menu_set_active_menu_name($menu_name = NULL) {
webmaster@1 1440 static $active;
webmaster@1 1441
webmaster@1 1442 if (isset($menu_name)) {
webmaster@1 1443 $active = $menu_name;
webmaster@1 1444 }
webmaster@1 1445 elseif (!isset($active)) {
webmaster@1 1446 $active = 'navigation';
webmaster@1 1447 }
webmaster@1 1448 return $active;
webmaster@1 1449 }
webmaster@1 1450
webmaster@1 1451 /**
webmaster@1 1452 * Get the active menu for the current page - determines the active trail.
webmaster@1 1453 */
webmaster@1 1454 function menu_get_active_menu_name() {
webmaster@1 1455 return menu_set_active_menu_name();
webmaster@1 1456 }
webmaster@1 1457
webmaster@1 1458 /**
webmaster@1 1459 * Set the active path, which determines which page is loaded.
webmaster@1 1460 *
webmaster@1 1461 * @param $path
webmaster@1 1462 * A Drupal path - not a path alias.
webmaster@1 1463 *
webmaster@1 1464 * Note that this may not have the desired effect unless invoked very early
webmaster@1 1465 * in the page load, such as during hook_boot, or unless you call
webmaster@1 1466 * menu_execute_active_handler() to generate your page output.
webmaster@1 1467 */
webmaster@1 1468 function menu_set_active_item($path) {
webmaster@1 1469 $_GET['q'] = $path;
webmaster@1 1470 }
webmaster@1 1471
webmaster@1 1472 /**
webmaster@1 1473 * Set (or get) the active trail for the current page - the path to root in the menu tree.
webmaster@1 1474 */
webmaster@1 1475 function menu_set_active_trail($new_trail = NULL) {
webmaster@1 1476 static $trail;
webmaster@1 1477
webmaster@1 1478 if (isset($new_trail)) {
webmaster@1 1479 $trail = $new_trail;
webmaster@1 1480 }
webmaster@1 1481 elseif (!isset($trail)) {
webmaster@1 1482 $trail = array();
webmaster@1 1483 $trail[] = array('title' => t('Home'), 'href' => '<front>', 'localized_options' => array(), 'type' => 0);
webmaster@1 1484 $item = menu_get_item();
webmaster@1 1485
webmaster@1 1486 // Check whether the current item is a local task (displayed as a tab).
webmaster@1 1487 if ($item['tab_parent']) {
webmaster@1 1488 // The title of a local task is used for the tab, never the page title.
webmaster@1 1489 // Thus, replace it with the item corresponding to the root path to get
webmaster@1 1490 // the relevant href and title. For example, the menu item corresponding
webmaster@1 1491 // to 'admin' is used when on the 'By module' tab at 'admin/by-module'.
webmaster@1 1492 $parts = explode('/', $item['tab_root']);
webmaster@1 1493 $args = arg();
webmaster@1 1494 // Replace wildcards in the root path using the current path.
webmaster@1 1495 foreach ($parts as $index => $part) {
webmaster@1 1496 if ($part == '%') {
webmaster@1 1497 $parts[$index] = $args[$index];
webmaster@1 1498 }
webmaster@1 1499 }
webmaster@1 1500 // Retrieve the menu item using the root path after wildcard replacement.
webmaster@1 1501 $root_item = menu_get_item(implode('/', $parts));
webmaster@1 1502 if ($root_item && $root_item['access']) {
webmaster@1 1503 $item = $root_item;
webmaster@1 1504 }
webmaster@1 1505 }
webmaster@1 1506
webmaster@1 1507 $tree = menu_tree_page_data(menu_get_active_menu_name());
webmaster@1 1508 list($key, $curr) = each($tree);
webmaster@1 1509
webmaster@1 1510 while ($curr) {
webmaster@1 1511 // Terminate the loop when we find the current path in the active trail.
webmaster@1 1512 if ($curr['link']['href'] == $item['href']) {
webmaster@1 1513 $trail[] = $curr['link'];
webmaster@1 1514 $curr = FALSE;
webmaster@1 1515 }
webmaster@1 1516 else {
webmaster@1 1517 // Move to the child link if it's in the active trail.
webmaster@1 1518 if ($curr['below'] && $curr['link']['in_active_trail']) {
webmaster@1 1519 $trail[] = $curr['link'];
webmaster@1 1520 $tree = $curr['below'];
webmaster@1 1521 }
webmaster@1 1522 list($key, $curr) = each($tree);
webmaster@1 1523 }
webmaster@1 1524 }
webmaster@1 1525 // Make sure the current page is in the trail (needed for the page title),
webmaster@1 1526 // but exclude tabs and the front page.
webmaster@1 1527 $last = count($trail) - 1;
webmaster@1 1528 if ($trail[$last]['href'] != $item['href'] && !(bool)($item['type'] & MENU_IS_LOCAL_TASK) && !drupal_is_front_page()) {
webmaster@1 1529 $trail[] = $item;
webmaster@1 1530 }
webmaster@1 1531 }
webmaster@1 1532 return $trail;
webmaster@1 1533 }
webmaster@1 1534
webmaster@1 1535 /**
webmaster@1 1536 * Get the active trail for the current page - the path to root in the menu tree.
webmaster@1 1537 */
webmaster@1 1538 function menu_get_active_trail() {
webmaster@1 1539 return menu_set_active_trail();
webmaster@1 1540 }
webmaster@1 1541
webmaster@1 1542 /**
webmaster@1 1543 * Get the breadcrumb for the current page, as determined by the active trail.
webmaster@1 1544 */
webmaster@1 1545 function menu_get_active_breadcrumb() {
webmaster@1 1546 $breadcrumb = array();
webmaster@1 1547
webmaster@1 1548 // No breadcrumb for the front page.
webmaster@1 1549 if (drupal_is_front_page()) {
webmaster@1 1550 return $breadcrumb;
webmaster@1 1551 }
webmaster@1 1552
webmaster@1 1553 $item = menu_get_item();
webmaster@1 1554 if ($item && $item['access']) {
webmaster@1 1555 $active_trail = menu_get_active_trail();
webmaster@1 1556
webmaster@1 1557 foreach ($active_trail as $parent) {
webmaster@1 1558 $breadcrumb[] = l($parent['title'], $parent['href'], $parent['localized_options']);
webmaster@1 1559 }
webmaster@1 1560 $end = end($active_trail);
webmaster@1 1561
webmaster@1 1562 // Don't show a link to the current page in the breadcrumb trail.
webmaster@1 1563 if ($item['href'] == $end['href'] || ($item['type'] == MENU_DEFAULT_LOCAL_TASK && $end['href'] != '<front>')) {
webmaster@1 1564 array_pop($breadcrumb);
webmaster@1 1565 }
webmaster@1 1566 }
webmaster@1 1567 return $breadcrumb;
webmaster@1 1568 }
webmaster@1 1569
webmaster@1 1570 /**
webmaster@1 1571 * Get the title of the current page, as determined by the active trail.
webmaster@1 1572 */
webmaster@1 1573 function menu_get_active_title() {
webmaster@1 1574 $active_trail = menu_get_active_trail();
webmaster@1 1575
webmaster@1 1576 foreach (array_reverse($active_trail) as $item) {
webmaster@1 1577 if (!(bool)($item['type'] & MENU_IS_LOCAL_TASK)) {
webmaster@1 1578 return $item['title'];
webmaster@1 1579 }
webmaster@1 1580 }
webmaster@1 1581 }
webmaster@1 1582
webmaster@1 1583 /**
webmaster@1 1584 * Get a menu link by its mlid, access checked and link translated for rendering.
webmaster@1 1585 *
webmaster@1 1586 * This function should never be called from within node_load() or any other
webmaster@1 1587 * function used as a menu object load function since an infinite recursion may
webmaster@1 1588 * occur.
webmaster@1 1589 *
webmaster@1 1590 * @param $mlid
webmaster@1 1591 * The mlid of the menu item.
webmaster@1 1592 * @return
webmaster@1 1593 * A menu link, with $item['access'] filled and link translated for
webmaster@1 1594 * rendering.
webmaster@1 1595 */
webmaster@1 1596 function menu_link_load($mlid) {
webmaster@1 1597 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 1598 _menu_link_translate($item);
webmaster@1 1599 return $item;
webmaster@1 1600 }
webmaster@1 1601 return FALSE;
webmaster@1 1602 }
webmaster@1 1603
webmaster@1 1604 /**
webmaster@1 1605 * Clears the cached cached data for a single named menu.
webmaster@1 1606 */
webmaster@1 1607 function menu_cache_clear($menu_name = 'navigation') {
webmaster@1 1608 static $cache_cleared = array();
webmaster@1 1609
webmaster@1 1610 if (empty($cache_cleared[$menu_name])) {
webmaster@1 1611 cache_clear_all('links:'. $menu_name .':', 'cache_menu', TRUE);
webmaster@1 1612 $cache_cleared[$menu_name] = 1;
webmaster@1 1613 }
webmaster@1 1614 elseif ($cache_cleared[$menu_name] == 1) {
webmaster@1 1615 register_shutdown_function('cache_clear_all', 'links:'. $menu_name .':', 'cache_menu', TRUE);
webmaster@1 1616 $cache_cleared[$menu_name] = 2;
webmaster@1 1617 }
webmaster@1 1618 }
webmaster@1 1619
webmaster@1 1620 /**
webmaster@1 1621 * Clears all cached menu data. This should be called any time broad changes
webmaster@1 1622 * might have been made to the router items or menu links.
webmaster@1 1623 */
webmaster@1 1624 function menu_cache_clear_all() {
webmaster@1 1625 cache_clear_all('*', 'cache_menu', TRUE);
webmaster@1 1626 }
webmaster@1 1627
webmaster@1 1628 /**
webmaster@1 1629 * (Re)populate the database tables used by various menu functions.
webmaster@1 1630 *
webmaster@1 1631 * This function will clear and populate the {menu_router} table, add entries
webmaster@1 1632 * to {menu_links} for new router items, then remove stale items from
webmaster@1 1633 * {menu_links}. If called from update.php or install.php, it will also
webmaster@1 1634 * schedule a call to itself on the first real page load from
webmaster@1 1635 * menu_execute_active_handler(), because the maintenance page environment
webmaster@1 1636 * is different and leaves stale data in the menu tables.
webmaster@1 1637 */
webmaster@1 1638 function menu_rebuild() {
webmaster@1 1639 variable_del('menu_rebuild_needed');
webmaster@1 1640 menu_cache_clear_all();
webmaster@1 1641 $menu = menu_router_build(TRUE);
webmaster@1 1642 _menu_navigation_links_rebuild($menu);
webmaster@1 1643 // Clear the page and block caches.
webmaster@1 1644 _menu_clear_page_cache();
webmaster@1 1645 if (defined('MAINTENANCE_MODE')) {
webmaster@1 1646 variable_set('menu_rebuild_needed', TRUE);
webmaster@1 1647 }
webmaster@1 1648 }
webmaster@1 1649
webmaster@1 1650 /**
webmaster@1 1651 * Collect, alter and store the menu definitions.
webmaster@1 1652 */
webmaster@1 1653 function menu_router_build($reset = FALSE) {
webmaster@1 1654 static $menu;
webmaster@1 1655
webmaster@1 1656 if (!isset($menu) || $reset) {
webmaster@1 1657 if (!$reset && ($cache = cache_get('router:', 'cache_menu')) && isset($cache->data)) {
webmaster@1 1658 $menu = $cache->data;
webmaster@1 1659 }
webmaster@1 1660 else {
webmaster@1 1661 db_query('DELETE FROM {menu_router}');
webmaster@1 1662 // We need to manually call each module so that we can know which module
webmaster@1 1663 // a given item came from.
webmaster@1 1664 $callbacks = array();
webmaster@1 1665 foreach (module_implements('menu') as $module) {
webmaster@1 1666 $router_items = call_user_func($module .'_menu');
webmaster@1 1667 if (isset($router_items) && is_array($router_items)) {
webmaster@1 1668 foreach (array_keys($router_items) as $path) {
webmaster@1 1669 $router_items[$path]['module'] = $module;
webmaster@1 1670 }
webmaster@1 1671 $callbacks = array_merge($callbacks, $router_items);
webmaster@1 1672 }
webmaster@1 1673 }
webmaster@1 1674 // Alter the menu as defined in modules, keys are like user/%user.
webmaster@1 1675 drupal_alter('menu', $callbacks);
webmaster@1 1676 $menu = _menu_router_build($callbacks);
webmaster@1 1677 cache_set('router:', $menu, 'cache_menu');
webmaster@1 1678 }
webmaster@1 1679 }
webmaster@1 1680 return $menu;
webmaster@1 1681 }
webmaster@1 1682
webmaster@1 1683 /**
webmaster@1 1684 * Builds a link from a router item.
webmaster@1 1685 */
webmaster@1 1686 function _menu_link_build($item) {
webmaster@1 1687 if ($item['type'] == MENU_CALLBACK) {
webmaster@1 1688 $item['hidden'] = -1;
webmaster@1 1689 }
webmaster@1 1690 elseif ($item['type'] == MENU_SUGGESTED_ITEM) {
webmaster@1 1691 $item['hidden'] = 1;
webmaster@1 1692 }
webmaster@1 1693 // Note, we set this as 'system', so that we can be sure to distinguish all
webmaster@1 1694 // the menu links generated automatically from entries in {menu_router}.
webmaster@1 1695 $item['module'] = 'system';
webmaster@1 1696 $item += array(
webmaster@1 1697 'menu_name' => 'navigation',
webmaster@1 1698 'link_title' => $item['title'],
webmaster@1 1699 'link_path' => $item['path'],
webmaster@1 1700 'hidden' => 0,
webmaster@1 1701 'options' => empty($item['description']) ? array() : array('attributes' => array('title' => $item['description'])),
webmaster@1 1702 );
webmaster@1 1703 return $item;
webmaster@1 1704 }
webmaster@1 1705
webmaster@1 1706 /**
webmaster@1 1707 * Helper function to build menu links for the items in the menu router.
webmaster@1 1708 */
webmaster@1 1709 function _menu_navigation_links_rebuild($menu) {
webmaster@1 1710 // Add normal and suggested items as links.
webmaster@1 1711 $menu_links = array();
webmaster@1 1712 foreach ($menu as $path => $item) {
webmaster@1 1713 if ($item['_visible']) {
webmaster@1 1714 $item = _menu_link_build($item);
webmaster@1 1715 $menu_links[$path] = $item;
webmaster@1 1716 $sort[$path] = $item['_number_parts'];
webmaster@1 1717 }
webmaster@1 1718 }
webmaster@1 1719 if ($menu_links) {
webmaster@1 1720 // Make sure no child comes before its parent.
webmaster@1 1721 array_multisort($sort, SORT_NUMERIC, $menu_links);
webmaster@1 1722
webmaster@1 1723 foreach ($menu_links as $item) {
webmaster@1 1724 $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 1725 if ($existing_item) {
webmaster@1 1726 $item['mlid'] = $existing_item['mlid'];
webmaster@1 1727 $item['menu_name'] = $existing_item['menu_name'];
webmaster@1 1728 $item['plid'] = $existing_item['plid'];
webmaster@1 1729 $item['has_children'] = $existing_item['has_children'];
webmaster@1 1730 $item['updated'] = $existing_item['updated'];
webmaster@1 1731 }
webmaster@1 1732 if (!$existing_item || !$existing_item['customized']) {
webmaster@1 1733 menu_link_save($item);
webmaster@1 1734 }
webmaster@1 1735 }
webmaster@1 1736 }
webmaster@1 1737 $placeholders = db_placeholders($menu, 'varchar');
webmaster@1 1738 $paths = array_keys($menu);
webmaster@3 1739 // Updated and customized items whose router paths are gone need new ones.
webmaster@1 1740 $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 1741 while ($item = db_fetch_array($result)) {
webmaster@1 1742 $router_path = _menu_find_router_path($menu, $item['link_path']);
webmaster@1 1743 if (!empty($router_path) && ($router_path != $item['router_path'] || $item['updated'])) {
webmaster@1 1744 // If the router path and the link path matches, it's surely a working
webmaster@1 1745 // item, so we clear the updated flag.
webmaster@1 1746 $updated = $item['updated'] && $router_path != $item['link_path'];
webmaster@1 1747 db_query("UPDATE {menu_links} SET router_path = '%s', updated = %d WHERE mlid = %d", $router_path, $updated, $item['mlid']);
webmaster@1 1748 }
webmaster@1 1749 }
webmaster@3 1750 // Find any item whose router path does not exist any more.
webmaster@1 1751 $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 1752 // Remove all such items. Starting from those with the greatest depth will
webmaster@1 1753 // minimize the amount of re-parenting done by menu_link_delete().
webmaster@1 1754 while ($item = db_fetch_array($result)) {
webmaster@1 1755 _menu_delete_item($item, TRUE);
webmaster@1 1756 }
webmaster@1 1757 }
webmaster@1 1758
webmaster@1 1759 /**
webmaster@1 1760 * Delete one or several menu links.
webmaster@1 1761 *
webmaster@1 1762 * @param $mlid
webmaster@1 1763 * A valid menu link mlid or NULL. If NULL, $path is used.
webmaster@1 1764 * @param $path
webmaster@1 1765 * The path to the menu items to be deleted. $mlid must be NULL.
webmaster@1 1766 */
webmaster@1 1767 function menu_link_delete($mlid, $path = NULL) {
webmaster@1 1768 if (isset($mlid)) {
webmaster@1 1769 _menu_delete_item(db_fetch_array(db_query("SELECT * FROM {menu_links} WHERE mlid = %d", $mlid)));
webmaster@1 1770 }
webmaster@1 1771 else {
webmaster@1 1772 $result = db_query("SELECT * FROM {menu_links} WHERE link_path = '%s'", $path);
webmaster@1 1773 while ($link = db_fetch_array($result)) {
webmaster@1 1774 _menu_delete_item($link);
webmaster@1 1775 }
webmaster@1 1776 }
webmaster@1 1777 }
webmaster@1 1778
webmaster@1 1779 /**
webmaster@1 1780 * Helper function for menu_link_delete; deletes a single menu link.
webmaster@1 1781 *
webmaster@1 1782 * @param $item
webmaster@1 1783 * Item to be deleted.
webmaster@1 1784 * @param $force
webmaster@1 1785 * Forces deletion. Internal use only, setting to TRUE is discouraged.
webmaster@1 1786 */
webmaster@1 1787 function _menu_delete_item($item, $force = FALSE) {
webmaster@1 1788 if ($item && ($item['module'] != 'system' || $item['updated'] || $force)) {
webmaster@1 1789 // Children get re-attached to the item's parent.
webmaster@1 1790 if ($item['has_children']) {
webmaster@1 1791 $result = db_query("SELECT mlid FROM {menu_links} WHERE plid = %d", $item['mlid']);
webmaster@1 1792 while ($m = db_fetch_array($result)) {
webmaster@1 1793 $child = menu_link_load($m['mlid']);
webmaster@1 1794 $child['plid'] = $item['plid'];
webmaster@1 1795 menu_link_save($child);
webmaster@1 1796 }
webmaster@1 1797 }
webmaster@1 1798 db_query('DELETE FROM {menu_links} WHERE mlid = %d', $item['mlid']);
webmaster@1 1799
webmaster@1 1800 // Update the has_children status of the parent.
webmaster@1 1801 _menu_update_parental_status($item);
webmaster@1 1802 menu_cache_clear($item['menu_name']);
webmaster@1 1803 _menu_clear_page_cache();
webmaster@1 1804 }
webmaster@1 1805 }
webmaster@1 1806
webmaster@1 1807 /**
webmaster@1 1808 * Save a menu link.
webmaster@1 1809 *
webmaster@1 1810 * @param $item
webmaster@1 1811 * An array representing a menu link item. The only mandatory keys are
webmaster@3 1812 * link_path and link_title. Possible keys are:
webmaster@3 1813 * - menu_name default is navigation
webmaster@3 1814 * - weight default is 0
webmaster@3 1815 * - expanded whether the item is expanded.
webmaster@3 1816 * - options An array of options, @see l for more.
webmaster@3 1817 * - mlid Set to an existing value, or 0 or NULL to insert a new link.
webmaster@3 1818 * - plid The mlid of the parent.
webmaster@3 1819 * - router_path The path of the relevant router item.
webmaster@1 1820 */
webmaster@1 1821 function menu_link_save(&$item) {
webmaster@1 1822 $menu = menu_router_build();
webmaster@1 1823
webmaster@1 1824 drupal_alter('menu_link', $item, $menu);
webmaster@1 1825
webmaster@1 1826 // This is the easiest way to handle the unique internal path '<front>',
webmaster@1 1827 // since a path marked as external does not need to match a router path.
webmaster@1 1828 $item['_external'] = menu_path_is_external($item['link_path']) || $item['link_path'] == '<front>';
webmaster@1 1829 // Load defaults.
webmaster@1 1830 $item += array(
webmaster@1 1831 'menu_name' => 'navigation',
webmaster@1 1832 'weight' => 0,
webmaster@1 1833 'link_title' => '',
webmaster@1 1834 'hidden' => 0,
webmaster@1 1835 'has_children' => 0,
webmaster@1 1836 'expanded' => 0,
webmaster@1 1837 'options' => array(),
webmaster@1 1838 'module' => 'menu',
webmaster@1 1839 'customized' => 0,
webmaster@1 1840 'updated' => 0,
webmaster@1 1841 );
webmaster@1 1842 $existing_item = FALSE;
webmaster@1 1843 if (isset($item['mlid'])) {
webmaster@1 1844 $existing_item = db_fetch_array(db_query("SELECT * FROM {menu_links} WHERE mlid = %d", $item['mlid']));
webmaster@1 1845 }
webmaster@1 1846
webmaster@1 1847 if (isset($item['plid'])) {
webmaster@1 1848 $parent = db_fetch_array(db_query("SELECT * FROM {menu_links} WHERE mlid = %d", $item['plid']));
webmaster@1 1849 }
webmaster@1 1850 else {
webmaster@1 1851 // Find the parent - it must be unique.
webmaster@1 1852 $parent_path = $item['link_path'];
webmaster@1 1853 $where = "WHERE link_path = '%s'";
webmaster@1 1854 // Only links derived from router items should have module == 'system', and
webmaster@1 1855 // we want to find the parent even if it's in a different menu.
webmaster@1 1856 if ($item['module'] == 'system') {
webmaster@1 1857 $where .= " AND module = '%s'";
webmaster@1 1858 $arg2 = 'system';
webmaster@1 1859 }
webmaster@1 1860 else {
webmaster@1 1861 // If not derived from a router item, we respect the specified menu name.
webmaster@1 1862 $where .= " AND menu_name = '%s'";
webmaster@1 1863 $arg2 = $item['menu_name'];
webmaster@1 1864 }
webmaster@1 1865 do {
webmaster@1 1866 $parent = FALSE;
webmaster@1 1867 $parent_path = substr($parent_path, 0, strrpos($parent_path, '/'));
webmaster@1 1868 $result = db_query("SELECT COUNT(*) FROM {menu_links} ". $where, $parent_path, $arg2);
webmaster@1 1869 // Only valid if we get a unique result.
webmaster@1 1870 if (db_result($result) == 1) {
webmaster@1 1871 $parent = db_fetch_array(db_query("SELECT * FROM {menu_links} ". $where, $parent_path, $arg2));
webmaster@1 1872 }
webmaster@1 1873 } while ($parent === FALSE && $parent_path);
webmaster@1 1874 }
webmaster@1 1875 if ($parent !== FALSE) {
webmaster@1 1876 $item['menu_name'] = $parent['menu_name'];
webmaster@1 1877 }
webmaster@1 1878 $menu_name = $item['menu_name'];
webmaster@1 1879 // Menu callbacks need to be in the links table for breadcrumbs, but can't
webmaster@1 1880 // be parents if they are generated directly from a router item.
webmaster@1 1881 if (empty($parent['mlid']) || $parent['hidden'] < 0) {
webmaster@1 1882 $item['plid'] = 0;
webmaster@1 1883 }
webmaster@1 1884 else {
webmaster@1 1885 $item['plid'] = $parent['mlid'];
webmaster@1 1886 }
webmaster@1 1887
webmaster@1 1888 if (!$existing_item) {
webmaster@1 1889 db_query("INSERT INTO {menu_links} (
webmaster@1 1890 menu_name, plid, link_path,
webmaster@1 1891 hidden, external, has_children,
webmaster@1 1892 expanded, weight,
webmaster@1 1893 module, link_title, options,
webmaster@1 1894 customized, updated) VALUES (
webmaster@1 1895 '%s', %d, '%s',
webmaster@1 1896 %d, %d, %d,
webmaster@1 1897 %d, %d,
webmaster@1 1898 '%s', '%s', '%s', %d, %d)",
webmaster@1 1899 $item['menu_name'], $item['plid'], $item['link_path'],
webmaster@1 1900 $item['hidden'], $item['_external'], $item['has_children'],
webmaster@1 1901 $item['expanded'], $item['weight'],
webmaster@1 1902 $item['module'], $item['link_title'], serialize($item['options']),
webmaster@1 1903 $item['customized'], $item['updated']);
webmaster@1 1904 $item['mlid'] = db_last_insert_id('menu_links', 'mlid');
webmaster@1 1905 }
webmaster@1 1906
webmaster@1 1907 if (!$item['plid']) {
webmaster@1 1908 $item['p1'] = $item['mlid'];
webmaster@1 1909 for ($i = 2; $i <= MENU_MAX_DEPTH; $i++) {
webmaster@1 1910 $item["p$i"] = 0;
webmaster@1 1911 }
webmaster@1 1912 $item['depth'] = 1;
webmaster@1 1913 }
webmaster@1 1914 else {
webmaster@1 1915 // Cannot add beyond the maximum depth.
webmaster@1 1916 if ($item['has_children'] && $existing_item) {
webmaster@1 1917 $limit = MENU_MAX_DEPTH - menu_link_children_relative_depth($existing_item) - 1;
webmaster@1 1918 }
webmaster@1 1919 else {
webmaster@1 1920 $limit = MENU_MAX_DEPTH - 1;
webmaster@1 1921 }
webmaster@1 1922 if ($parent['depth'] > $limit) {
webmaster@1 1923 return FALSE;
webmaster@1 1924 }
webmaster@1 1925 $item['depth'] = $parent['depth'] + 1;
webmaster@1 1926 _menu_link_parents_set($item, $parent);
webmaster@1 1927 }
webmaster@1 1928 // Need to check both plid and menu_name, since plid can be 0 in any menu.
webmaster@1 1929 if ($existing_item && ($item['plid'] != $existing_item['plid'] || $menu_name != $existing_item['menu_name'])) {
webmaster@1 1930 _menu_link_move_children($item, $existing_item);
webmaster@1 1931 }
webmaster@1 1932 // Find the callback. During the menu update we store empty paths to be
webmaster@1 1933 // fixed later, so we skip this.
webmaster@1 1934 if (!isset($_SESSION['system_update_6021']) && (empty($item['router_path']) || !$existing_item || ($existing_item['link_path'] != $item['link_path']))) {
webmaster@1 1935 if ($item['_external']) {
webmaster@1 1936 $item['router_path'] = '';
webmaster@1 1937 }
webmaster@1 1938 else {
webmaster@1 1939 // Find the router path which will serve this path.
webmaster@1 1940 $item['parts'] = explode('/', $item['link_path'], MENU_MAX_PARTS);
webmaster@1 1941 $item['router_path'] = _menu_find_router_path($menu, $item['link_path']);
webmaster@1 1942 }
webmaster@1 1943 }
webmaster@1 1944 db_query("UPDATE {menu_links} SET menu_name = '%s', plid = %d, link_path = '%s',
webmaster@1 1945 router_path = '%s', hidden = %d, external = %d, has_children = %d,
webmaster@1 1946 expanded = %d, weight = %d, depth = %d,
webmaster@1 1947 p1 = %d, p2 = %d, p3 = %d, p4 = %d, p5 = %d, p6 = %d, p7 = %d, p8 = %d, p9 = %d,
webmaster@1 1948 module = '%s', link_title = '%s', options = '%s', customized = %d WHERE mlid = %d",
webmaster@1 1949 $item['menu_name'], $item['plid'], $item['link_path'],
webmaster@1 1950 $item['router_path'], $item['hidden'], $item['_external'], $item['has_children'],
webmaster@1 1951 $item['expanded'], $item['weight'], $item['depth'],
webmaster@1 1952 $item['p1'], $item['p2'], $item['p3'], $item['p4'], $item['p5'], $item['p6'], $item['p7'], $item['p8'], $item['p9'],
webmaster@1 1953 $item['module'], $item['link_title'], serialize($item['options']), $item['customized'], $item['mlid']);
webmaster@1 1954 // Check the has_children status of the parent.
webmaster@1 1955 _menu_update_parental_status($item);
webmaster@1 1956 menu_cache_clear($menu_name);
webmaster@1 1957 if ($existing_item && $menu_name != $existing_item['menu_name']) {
webmaster@1 1958 menu_cache_clear($existing_item['menu_name']);
webmaster@1 1959 }
webmaster@1 1960
webmaster@1 1961 _menu_clear_page_cache();
webmaster@1 1962 return $item['mlid'];
webmaster@1 1963 }
webmaster@1 1964
webmaster@1 1965 /**
webmaster@1 1966 * Helper function to clear the page and block caches at most twice per page load.
webmaster@1 1967 */
webmaster@1 1968 function _menu_clear_page_cache() {
webmaster@1 1969 static $cache_cleared = 0;
webmaster@1 1970
webmaster@1 1971 // Clear the page and block caches, but at most twice, including at
webmaster@1 1972 // the end of the page load when there are multple links saved or deleted.
webmaster@1 1973 if (empty($cache_cleared)) {
webmaster@1 1974 cache_clear_all();
webmaster@1 1975 // Keep track of which menus have expanded items.
webmaster@1 1976 _menu_set_expanded_menus();
webmaster@1 1977 $cache_cleared = 1;
webmaster@1 1978 }
webmaster@1 1979 elseif ($cache_cleared == 1) {
webmaster@1 1980 register_shutdown_function('cache_clear_all');
webmaster@1 1981 // Keep track of which menus have expanded items.
webmaster@1 1982 register_shutdown_function('_menu_set_expanded_menus');
webmaster@1 1983 $cache_cleared = 2;
webmaster@1 1984 }
webmaster@1 1985 }
webmaster@1 1986
webmaster@1 1987 /**
webmaster@1 1988 * Helper function to update a list of menus with expanded items
webmaster@1 1989 */
webmaster@1 1990 function _menu_set_expanded_menus() {
webmaster@1 1991 $names = array();
webmaster@1 1992 $result = db_query("SELECT menu_name FROM {menu_links} WHERE expanded != 0 GROUP BY menu_name");
webmaster@1 1993 while ($n = db_fetch_array($result)) {
webmaster@1 1994 $names[] = $n['menu_name'];
webmaster@1 1995 }
webmaster@1 1996 variable_set('menu_expanded', $names);
webmaster@1 1997 }
webmaster@1 1998
webmaster@1 1999 /**
webmaster@1 2000 * Find the router path which will serve this path.
webmaster@1 2001 *
webmaster@1 2002 * @param $menu
webmaster@1 2003 * The full built menu.
webmaster@1 2004 * @param $link_path
webmaster@1 2005 * The path for we are looking up its router path.
webmaster@1 2006 * @return
webmaster@1 2007 * A path from $menu keys or empty if $link_path points to a nonexisting
webmaster@1 2008 * place.
webmaster@1 2009 */
webmaster@1 2010 function _menu_find_router_path($menu, $link_path) {
webmaster@1 2011 $parts = explode('/', $link_path, MENU_MAX_PARTS);
webmaster@1 2012 $router_path = $link_path;
webmaster@1 2013 if (!isset($menu[$router_path])) {
webmaster@1 2014 list($ancestors) = menu_get_ancestors($parts);
webmaster@1 2015 $ancestors[] = '';
webmaster@1 2016 foreach ($ancestors as $key => $router_path) {
webmaster@1 2017 if (isset($menu[$router_path])) {
webmaster@1 2018 break;
webmaster@1 2019 }
webmaster@1 2020 }
webmaster@1 2021 }
webmaster@1 2022 return $router_path;
webmaster@1 2023 }
webmaster@1 2024
webmaster@1 2025 /**
webmaster@1 2026 * Insert, update or delete an uncustomized menu link related to a module.
webmaster@1 2027 *
webmaster@1 2028 * @param $module
webmaster@1 2029 * The name of the module.
webmaster@1 2030 * @param $op
webmaster@1 2031 * Operation to perform: insert, update or delete.
webmaster@1 2032 * @param $link_path
webmaster@1 2033 * The path this link points to.
webmaster@1 2034 * @param $link_title
webmaster@1 2035 * Title of the link to insert or new title to update the link to.
webmaster@1 2036 * Unused for delete.
webmaster@1 2037 * @return
webmaster@1 2038 * The insert op returns the mlid of the new item. Others op return NULL.
webmaster@1 2039 */
webmaster@1 2040 function menu_link_maintain($module, $op, $link_path, $link_title) {
webmaster@1 2041 switch ($op) {
webmaster@1 2042 case 'insert':
webmaster@1 2043 $menu_link = array(
webmaster@1 2044 'link_title' => $link_title,
webmaster@1 2045 'link_path' => $link_path,
webmaster@1 2046 'module' => $module,
webmaster@1 2047 );
webmaster@1 2048 return menu_link_save($menu_link);
webmaster@1 2049 break;
webmaster@1 2050 case 'update':
webmaster@1 2051 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 2052 menu_cache_clear();
webmaster@1 2053 break;
webmaster@1 2054 case 'delete':
webmaster@1 2055 menu_link_delete(NULL, $link_path);
webmaster@1 2056 break;
webmaster@1 2057 }
webmaster@1 2058 }
webmaster@1 2059
webmaster@1 2060 /**
webmaster@1 2061 * Find the depth of an item's children relative to its depth.
webmaster@1 2062 *
webmaster@1 2063 * For example, if the item has a depth of 2, and the maximum of any child in
webmaster@1 2064 * the menu link tree is 5, the relative depth is 3.
webmaster@1 2065 *
webmaster@1 2066 * @param $item
webmaster@1 2067 * An array representing a menu link item.
webmaster@1 2068 * @return
webmaster@1 2069 * The relative depth, or zero.
webmaster@1 2070 *
webmaster@1 2071 */
webmaster@1 2072 function menu_link_children_relative_depth($item) {
webmaster@1 2073 $i = 1;
webmaster@1 2074 $match = '';
webmaster@1 2075 $args[] = $item['menu_name'];
webmaster@1 2076 $p = 'p1';
webmaster@1 2077 while ($i <= MENU_MAX_DEPTH && $item[$p]) {
webmaster@1 2078 $match .= " AND $p = %d";
webmaster@1 2079 $args[] = $item[$p];
webmaster@1 2080 $p = 'p'. ++$i;
webmaster@1 2081 }
webmaster@1 2082
webmaster@1 2083 $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 2084
webmaster@1 2085 return ($max_depth > $item['depth']) ? $max_depth - $item['depth'] : 0;
webmaster@1 2086 }
webmaster@1 2087
webmaster@1 2088 /**
webmaster@1 2089 * Update the children of a menu link that's being moved.
webmaster@1 2090 *
webmaster@1 2091 * The menu name, parents (p1 - p6), and depth are updated for all children of
webmaster@1 2092 * the link, and the has_children status of the previous parent is updated.
webmaster@1 2093 */
webmaster@1 2094 function _menu_link_move_children($item, $existing_item) {
webmaster@1 2095
webmaster@1 2096 $args[] = $item['menu_name'];
webmaster@1 2097 $set[] = "menu_name = '%s'";
webmaster@1 2098
webmaster@1 2099 $i = 1;
webmaster@1 2100 while ($i <= $item['depth']) {
webmaster@1 2101 $p = 'p'. $i++;
webmaster@1 2102 $set[] = "$p = %d";
webmaster@1 2103 $args[] = $item[$p];
webmaster@1 2104 }
webmaster@1 2105 $j = $existing_item['depth'] + 1;
webmaster@1 2106 while ($i <= MENU_MAX_DEPTH && $j <= MENU_MAX_DEPTH) {
webmaster@1 2107 $set[] = 'p'. $i++ .' = p'. $j++;
webmaster@1 2108 }
webmaster@1 2109 while ($i <= MENU_MAX_DEPTH) {
webmaster@1 2110 $set[] = 'p'. $i++ .' = 0';
webmaster@1 2111 }
webmaster@1 2112
webmaster@1 2113 $shift = $item['depth'] - $existing_item['depth'];
webmaster@1 2114 if ($shift < 0) {
webmaster@1 2115 $args[] = -$shift;
webmaster@1 2116 $set[] = 'depth = depth - %d';
webmaster@1 2117 }
webmaster@1 2118 elseif ($shift > 0) {
webmaster@1 2119 // The order of $set must be reversed so the new values don't overwrite the
webmaster@1 2120 // old ones before they can be used because "Single-table UPDATE
webmaster@1 2121 // assignments are generally evaluated from left to right"
webmaster@1 2122 // see: http://dev.mysql.com/doc/refman/5.0/en/update.html
webmaster@1 2123 $set = array_reverse($set);
webmaster@1 2124 $args = array_reverse($args);
webmaster@1 2125
webmaster@1 2126 $args[] = $shift;
webmaster@1 2127 $set[] = 'depth = depth + %d';
webmaster@1 2128 }
webmaster@1 2129 $where[] = "menu_name = '%s'";
webmaster@1 2130 $args[] = $existing_item['menu_name'];
webmaster@1 2131 $p = 'p1';
webmaster@1 2132 for ($i = 1; $i <= MENU_MAX_DEPTH && $existing_item[$p]; $p = 'p'. ++$i) {
webmaster@1 2133 $where[] = "$p = %d";
webmaster@1 2134 $args[] = $existing_item[$p];
webmaster@1 2135 }
webmaster@1 2136
webmaster@1 2137 db_query("UPDATE {menu_links} SET ". implode(', ', $set) ." WHERE ". implode(' AND ', $where), $args);
webmaster@1 2138 // Check the has_children status of the parent, while excluding this item.
webmaster@1 2139 _menu_update_parental_status($existing_item, TRUE);
webmaster@1 2140 }
webmaster@1 2141
webmaster@1 2142 /**
webmaster@1 2143 * Check and update the has_children status for the parent of a link.
webmaster@1 2144 */
webmaster@1 2145 function _menu_update_parental_status($item, $exclude = FALSE) {
webmaster@1 2146 // If plid == 0, there is nothing to update.
webmaster@1 2147 if ($item['plid']) {
webmaster@1 2148 // We may want to exclude the passed link as a possible child.
webmaster@1 2149 $where = $exclude ? " AND mlid != %d" : '';
webmaster@1 2150 // Check if at least one visible child exists in the table.
webmaster@1 2151 $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 2152 db_query("UPDATE {menu_links} SET has_children = %d WHERE mlid = %d", $parent_has_children, $item['plid']);
webmaster@1 2153 }
webmaster@1 2154 }
webmaster@1 2155
webmaster@1 2156 /**
webmaster@1 2157 * Helper function that sets the p1..p9 values for a menu link being saved.
webmaster@1 2158 */
webmaster@1 2159 function _menu_link_parents_set(&$item, $parent) {
webmaster@1 2160 $i = 1;
webmaster@1 2161 while ($i < $item['depth']) {
webmaster@1 2162 $p = 'p'. $i++;
webmaster@1 2163 $item[$p] = $parent[$p];
webmaster@1 2164 }
webmaster@1 2165 $p = 'p'. $i++;
webmaster@1 2166 // The parent (p1 - p9) corresponding to the depth always equals the mlid.
webmaster@1 2167 $item[$p] = $item['mlid'];
webmaster@1 2168 while ($i <= MENU_MAX_DEPTH) {
webmaster@1 2169 $p = 'p'. $i++;
webmaster@1 2170 $item[$p] = 0;
webmaster@1 2171 }
webmaster@1 2172 }
webmaster@1 2173
webmaster@1 2174 /**
webmaster@1 2175 * Helper function to build the router table based on the data from hook_menu.
webmaster@1 2176 */
webmaster@1 2177 function _menu_router_build($callbacks) {
webmaster@1 2178 // First pass: separate callbacks from paths, making paths ready for
webmaster@1 2179 // matching. Calculate fitness, and fill some default values.
webmaster@1 2180 $menu = array();
webmaster@1 2181 foreach ($callbacks as $path => $item) {
webmaster@1 2182 $load_functions = array();
webmaster@1 2183 $to_arg_functions = array();
webmaster@1 2184 $fit = 0;
webmaster@1 2185 $move = FALSE;
webmaster@1 2186
webmaster@1 2187 $parts = explode('/', $path, MENU_MAX_PARTS);
webmaster@1 2188 $number_parts = count($parts);
webmaster@1 2189 // We store the highest index of parts here to save some work in the fit
webmaster@1 2190 // calculation loop.
webmaster@1 2191 $slashes = $number_parts - 1;
webmaster@1 2192 // Extract load and to_arg functions.
webmaster@1 2193 foreach ($parts as $k => $part) {
webmaster@1 2194 $match = FALSE;
webmaster@1 2195 if (preg_match('/^%([a-z_]*)$/', $part, $matches)) {
webmaster@1 2196 if (empty($matches[1])) {
webmaster@1 2197 $match = TRUE;
webmaster@1 2198 $load_functions[$k] = NULL;
webmaster@1 2199 }
webmaster@1 2200 else {
webmaster@1 2201 if (function_exists($matches[1] .'_to_arg')) {
webmaster@1 2202 $to_arg_functions[$k] = $matches[1] .'_to_arg';
webmaster@1 2203 $load_functions[$k] = NULL;
webmaster@1 2204 $match = TRUE;
webmaster@1 2205 }
webmaster@1 2206 if (function_exists($matches[1] .'_load')) {
webmaster@1 2207 $function = $matches[1] .'_load';
webmaster@1 2208 // Create an array of arguments that will be passed to the _load
webmaster@1 2209 // function when this menu path is checked, if 'load arguments'
webmaster@1 2210 // exists.
webmaster@1 2211 $load_functions[$k] = isset($item['load arguments']) ? array($function => $item['load arguments']) : $function;
webmaster@1 2212 $match = TRUE;
webmaster@1 2213 }
webmaster@1 2214 }
webmaster@1 2215 }
webmaster@1 2216 if ($match) {
webmaster@1 2217 $parts[$k] = '%';
webmaster@1 2218 }
webmaster@1 2219 else {
webmaster@1 2220 $fit |= 1 << ($slashes - $k);
webmaster@1 2221 }
webmaster@1 2222 }
webmaster@1 2223 if ($fit) {
webmaster@1 2224 $move = TRUE;
webmaster@1 2225 }
webmaster@1 2226 else {
webmaster@1 2227 // If there is no %, it fits maximally.
webmaster@1 2228 $fit = (1 << $number_parts) - 1;
webmaster@1 2229 }
webmaster@1 2230 $masks[$fit] = 1;
webmaster@1 2231 $item['load_functions'] = empty($load_functions) ? '' : serialize($load_functions);
webmaster@1 2232 $item['to_arg_functions'] = empty($to_arg_functions) ? '' : serialize($to_arg_functions);
webmaster@1 2233 $item += array(
webmaster@1 2234 'title' => '',
webmaster@1 2235 'weight' => 0,
webmaster@1 2236 'type' => MENU_NORMAL_ITEM,
webmaster@1 2237 '_number_parts' => $number_parts,
webmaster@1 2238 '_parts' => $parts,
webmaster@1 2239 '_fit' => $fit,
webmaster@1 2240 );
webmaster@1 2241 $item += array(
webmaster@1 2242 '_visible' => (bool)($item['type'] & MENU_VISIBLE_IN_BREADCRUMB),
webmaster@1 2243 '_tab' => (bool)($item['type'] & MENU_IS_LOCAL_TASK),
webmaster@1 2244 );
webmaster@1 2245 if ($move) {
webmaster@1 2246 $new_path = implode('/', $item['_parts']);
webmaster@1 2247 $menu[$new_path] = $item;
webmaster@1 2248 $sort[$new_path] = $number_parts;
webmaster@1 2249 }
webmaster@1 2250 else {
webmaster@1 2251 $menu[$path] = $item;
webmaster@1 2252 $sort[$path] = $number_parts;
webmaster@1 2253 }
webmaster@1 2254 }
webmaster@1 2255 array_multisort($sort, SORT_NUMERIC, $menu);
webmaster@1 2256
webmaster@1 2257 // Apply inheritance rules.
webmaster@1 2258 foreach ($menu as $path => $v) {
webmaster@1 2259 $item = &$menu[$path];
webmaster@1 2260 if (!$item['_tab']) {
webmaster@1 2261 // Non-tab items.
webmaster@1 2262 $item['tab_parent'] = '';
webmaster@1 2263 $item['tab_root'] = $path;
webmaster@1 2264 }
webmaster@1 2265 for ($i = $item['_number_parts'] - 1; $i; $i--) {
webmaster@1 2266 $parent_path = implode('/', array_slice($item['_parts'], 0, $i));
webmaster@1 2267 if (isset($menu[$parent_path])) {
webmaster@1 2268
webmaster@1 2269 $parent = $menu[$parent_path];
webmaster@1 2270
webmaster@1 2271 if (!isset($item['tab_parent'])) {
webmaster@1 2272 // Parent stores the parent of the path.
webmaster@1 2273 $item['tab_parent'] = $parent_path;
webmaster@1 2274 }
webmaster@1 2275 if (!isset($item['tab_root']) && !$parent['_tab']) {
webmaster@1 2276 $item['tab_root'] = $parent_path;
webmaster@1 2277 }
webmaster@5 2278 // If an access callback is not found for a default local task we use
webmaster@5 2279 // the callback from the parent, since we expect them to be identical.
webmaster@5 2280 // In all other cases, the access parameters must be specified.
webmaster@5 2281 if (($item['type'] == MENU_DEFAULT_LOCAL_TASK) && !isset($item['access callback']) && isset($parent['access callback'])) {
webmaster@1 2282 $item['access callback'] = $parent['access callback'];
webmaster@1 2283 if (!isset($item['access arguments']) && isset($parent['access arguments'])) {
webmaster@1 2284 $item['access arguments'] = $parent['access arguments'];
webmaster@1 2285 }
webmaster@1 2286 }
webmaster@1 2287 // Same for page callbacks.
webmaster@1 2288 if (!isset($item['page callback']) && isset($parent['page callback'])) {
webmaster@1 2289 $item['page callback'] = $parent['page callback'];
webmaster@1 2290 if (!isset($item['page arguments']) && isset($parent['page arguments'])) {
webmaster@1 2291 $item['page arguments'] = $parent['page arguments'];
webmaster@1 2292 }
webmaster@1 2293 if (!isset($item['file']) && isset($parent['file'])) {
webmaster@1 2294 $item['file'] = $parent['file'];
webmaster@1 2295 }
webmaster@1 2296 if (!isset($item['file path']) && isset($parent['file path'])) {
webmaster@1 2297 $item['file path'] = $parent['file path'];
webmaster@1 2298 }
webmaster@1 2299 }
webmaster@1 2300 }
webmaster@1 2301 }
webmaster@1 2302 if (!isset($item['access callback']) && isset($item['access arguments'])) {
webmaster@1 2303 // Default callback.
webmaster@1 2304 $item['access callback'] = 'user_access';
webmaster@1 2305 }
webmaster@1 2306 if (!isset($item['access callback']) || empty($item['page callback'])) {
webmaster@1 2307 $item['access callback'] = 0;
webmaster@1 2308 }
webmaster@1 2309 if (is_bool($item['access callback'])) {
webmaster@1 2310 $item['access callback'] = intval($item['access callback']);
webmaster@1 2311 }
webmaster@1 2312
webmaster@1 2313 $item += array(
webmaster@1 2314 'access arguments' => array(),
webmaster@1 2315 'access callback' => '',
webmaster@1 2316 'page arguments' => array(),
webmaster@1 2317 'page callback' => '',
webmaster@1 2318 'block callback' => '',
webmaster@1 2319 'title arguments' => array(),
webmaster@1 2320 'title callback' => 't',
webmaster@1 2321 'description' => '',
webmaster@1 2322 'position' => '',
webmaster@1 2323 'tab_parent' => '',
webmaster@1 2324 'tab_root' => $path,
webmaster@1 2325 'path' => $path,
webmaster@1 2326 'file' => '',
webmaster@1 2327 'file path' => '',
webmaster@1 2328 'include file' => '',
webmaster@1 2329 );
webmaster@1 2330
webmaster@1 2331 // Calculate out the file to be included for each callback, if any.
webmaster@1 2332 if ($item['file']) {
webmaster@1 2333 $file_path = $item['file path'] ? $item['file path'] : drupal_get_path('module', $item['module']);
webmaster@1 2334 $item['include file'] = $file_path .'/'. $item['file'];
webmaster@1 2335 }
webmaster@1 2336
webmaster@1 2337 $title_arguments = $item['title arguments'] ? serialize($item['title arguments']) : '';
webmaster@1 2338 db_query("INSERT INTO {menu_router}
webmaster@1 2339 (path, load_functions, to_arg_functions, access_callback,
webmaster@1 2340 access_arguments, page_callback, page_arguments, fit,
webmaster@1 2341 number_parts, tab_parent, tab_root,
webmaster@1 2342 title, title_callback, title_arguments,
webmaster@1 2343 type, block_callback, description, position, weight, file)
webmaster@1 2344 VALUES ('%s', '%s', '%s', '%s',
webmaster@1 2345 '%s', '%s', '%s', %d,
webmaster@1 2346 %d, '%s', '%s',
webmaster@1 2347 '%s', '%s', '%s',
webmaster@1 2348 %d, '%s', '%s', '%s', %d, '%s')",
webmaster@1 2349 $path, $item['load_functions'], $item['to_arg_functions'], $item['access callback'],
webmaster@1 2350 serialize($item['access arguments']), $item['page callback'], serialize($item['page arguments']), $item['_fit'],
webmaster@1 2351 $item['_number_parts'], $item['tab_parent'], $item['tab_root'],
webmaster@1 2352 $item['title'], $item['title callback'], $title_arguments,
webmaster@1 2353 $item['type'], $item['block callback'], $item['description'], $item['position'], $item['weight'], $item['include file']);
webmaster@1 2354 }
webmaster@1 2355 // Sort the masks so they are in order of descending fit, and store them.
webmaster@1 2356 $masks = array_keys($masks);
webmaster@1 2357 rsort($masks);
webmaster@1 2358 variable_set('menu_masks', $masks);
webmaster@1 2359 return $menu;
webmaster@1 2360 }
webmaster@1 2361
webmaster@1 2362 /**
webmaster@1 2363 * Returns TRUE if a path is external (e.g. http://example.com).
webmaster@1 2364 */
webmaster@1 2365 function menu_path_is_external($path) {
webmaster@1 2366 $colonpos = strpos($path, ':');
webmaster@1 2367 return $colonpos !== FALSE && !preg_match('![/?#]!', substr($path, 0, $colonpos)) && filter_xss_bad_protocol($path, FALSE) == check_plain($path);
webmaster@1 2368 }
webmaster@1 2369
webmaster@1 2370 /**
webmaster@1 2371 * Checks whether the site is off-line for maintenance.
webmaster@1 2372 *
webmaster@1 2373 * This function will log the current user out and redirect to front page
webmaster@1 2374 * if the current user has no 'administer site configuration' permission.
webmaster@1 2375 *
webmaster@1 2376 * @return
webmaster@1 2377 * FALSE if the site is not off-line or its the login page or the user has
webmaster@1 2378 * 'administer site configuration' permission.
webmaster@1 2379 * TRUE for anonymous users not on the login page if the site is off-line.
webmaster@1 2380 */
webmaster@1 2381 function _menu_site_is_offline() {
webmaster@1 2382 // Check if site is set to off-line mode.
webmaster@1 2383 if (variable_get('site_offline', 0)) {
webmaster@1 2384 // Check if the user has administration privileges.
webmaster@1 2385 if (user_access('administer site configuration')) {
webmaster@1 2386 // Ensure that the off-line message is displayed only once [allowing for
webmaster@1 2387 // page redirects], and specifically suppress its display on the site
webmaster@1 2388 // maintenance page.
webmaster@1 2389 if (drupal_get_normal_path($_GET['q']) != 'admin/settings/site-maintenance') {
webmaster@1 2390 drupal_set_message(t('Operating in off-line mode.'), 'status', FALSE);
webmaster@1 2391 }
webmaster@1 2392 }
webmaster@1 2393 else {
webmaster@1 2394 // Anonymous users get a FALSE at the login prompt, TRUE otherwise.
webmaster@1 2395 if (user_is_anonymous()) {
webmaster@1 2396 return $_GET['q'] != 'user' && $_GET['q'] != 'user/login';
webmaster@1 2397 }
webmaster@1 2398 // Logged in users are unprivileged here, so they are logged out.
webmaster@1 2399 require_once drupal_get_path('module', 'user') .'/user.pages.inc';
webmaster@1 2400 user_logout();
webmaster@1 2401 }
webmaster@1 2402 }
webmaster@1 2403 return FALSE;
webmaster@1 2404 }
webmaster@1 2405
webmaster@1 2406 /**
webmaster@1 2407 * Validates the path of a menu link being created or edited.
webmaster@1 2408 *
webmaster@1 2409 * @return
webmaster@1 2410 * TRUE if it is a valid path AND the current user has access permission,
webmaster@1 2411 * FALSE otherwise.
webmaster@1 2412 */
webmaster@1 2413 function menu_valid_path($form_item) {
webmaster@1 2414 global $menu_admin;
webmaster@1 2415 $item = array();
webmaster@1 2416 $path = $form_item['link_path'];
webmaster@1 2417 // We indicate that a menu administrator is running the menu access check.
webmaster@1 2418 $menu_admin = TRUE;
webmaster@1 2419 if ($path == '<front>' || menu_path_is_external($path)) {
webmaster@1 2420 $item = array('access' => TRUE);
webmaster@1 2421 }
webmaster@1 2422 elseif (preg_match('/\/\%/', $path)) {
webmaster@1 2423 // Path is dynamic (ie 'user/%'), so check directly against menu_router table.
webmaster@1 2424 if ($item = db_fetch_array(db_query("SELECT * FROM {menu_router} where path = '%s' ", $path))) {
webmaster@1 2425 $item['link_path'] = $form_item['link_path'];
webmaster@1 2426 $item['link_title'] = $form_item['link_title'];
webmaster@1 2427 $item['external'] = FALSE;
webmaster@1 2428 $item['options'] = '';
webmaster@1 2429 _menu_link_translate($item);
webmaster@1 2430 }
webmaster@1 2431 }
webmaster@1 2432 else {
webmaster@1 2433 $item = menu_get_item($path);
webmaster@1 2434 }
webmaster@1 2435 $menu_admin = FALSE;
webmaster@1 2436 return $item && $item['access'];
webmaster@1 2437 }
webmaster@1 2438
webmaster@1 2439 /**
webmaster@1 2440 * @} End of "defgroup menu".
webmaster@1 2441 */