annotate includes/menu.inc @ 2:85b5b336180c

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