annotate includes/menu.inc @ 3:165d43f946a8 6.1

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