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