menu.inc

API for the Drupal menu system.

Archivo

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

Functions

Nombreorden descendente Descripción
drupal_help_arg Generates elements for the $arg array in the help hook.
menu_cache_clear Clears the cached cached data for a single named menu.
menu_cache_clear_all Clears all cached menu data. This should be called any time broad changes might have been made to the router items or menu links.
menu_execute_active_handler Execute the page callback associated with the current path
menu_get_active_breadcrumb Get the breadcrumb for the current page, as determined by the active trail.
menu_get_active_help Returns the help associated with the active menu item.
menu_get_active_menu_name Get the active menu for the current page - determines the active trail.
menu_get_active_title Get the title of the current page, as determined by the active trail.
menu_get_active_trail Gets the active trail (path to root menu root) of the current page.
menu_get_ancestors Returns the ancestors (and relevant placeholders) for any given path.
menu_get_item Get a router item.
menu_get_names Build a list of named menus.
menu_get_object Get a loaded object from a router item.
menu_link_children_relative_depth Find the depth of an item's children relative to its depth.
menu_link_delete Delete one or several menu links.
menu_link_load Get a menu link by its mlid, access checked and link translated for rendering.
menu_link_maintain Insert, update or delete an uncustomized menu link related to a module.
menu_link_save Save a menu link.
menu_list_system_menus Return an array containing the names of system-defined (default) menus.
menu_local_tasks Collects the local tasks (tabs) for a given level.
menu_navigation_links Return an array of links for a navigation menu.
menu_path_is_external Returns TRUE if a path is external (e.g. http://example.com).
menu_primary_links Return an array of links to be rendered as the Primary links.
menu_primary_local_tasks Returns the rendered local tasks at the top level.
menu_rebuild (Re)populate the database tables used by various menu functions.
menu_router_build Collect, alter and store the menu definitions.
menu_secondary_links Return an array of links to be rendered as the Secondary links.
menu_secondary_local_tasks Returns the rendered local tasks at the second level.
menu_set_active_item Set the active path, which determines which page is loaded.
menu_set_active_menu_name Set (or get) the active menu for the current page - determines the active trail.
menu_set_active_trail Sets or gets the active trail (path to root menu root) of the current page.
menu_set_item Replaces the statically cached item for a given path.
menu_tab_root_path Returns the router path, or the path of the parent tab of a default local task.
menu_tail_to_arg
menu_tree Render a menu tree based on the current path.
menu_tree_all_data Get the data structure representing a named menu tree.
menu_tree_check_access Check access and perform other dynamic operations for each link in the tree.
menu_tree_collect_node_links Recursive helper function - collect node links.
menu_tree_data Build the data representing a menu tree.
menu_tree_output Returns a rendered menu tree.
menu_tree_page_data Get the data structure representing a named menu tree, based on the current page.
menu_unserialize The menu system uses serialized arrays stored in the database for arguments. However, often these need to change according to the current path. This function unserializes such an array and does the necessary change.
menu_valid_path Validates the path of a menu link being created or edited.
theme_menu_item Generate the HTML output for a menu item and submenu.
theme_menu_item_link Generate the HTML output for a single menu link.
theme_menu_local_task Generate the HTML output for a single local task link.
theme_menu_local_tasks Returns the rendered local tasks. The default implementation renders them as tabs.
theme_menu_tree Generate the HTML output for a menu tree
_menu_check_access Check access to a menu item using the access callback
_menu_clear_page_cache Helper function to clear the page and block caches at most twice per page load.
_menu_delete_item Helper function for menu_link_delete; deletes a single menu link.
_menu_find_router_path Find the router path which will serve this path.
_menu_item_localize Localize the router item title using t() or another callback.
_menu_link_build Builds a link from a router item.
_menu_link_map_translate This function translates the path elements in the map using any to_arg helper function. These functions take an argument and return an object. See http://drupal.org/node/109153 for more information.
_menu_link_move_children Update the children of a menu link that's being moved.
_menu_link_parents_set Helper function that sets the p1..p9 values for a menu link being saved.
_menu_link_translate This function is similar to _menu_translate() but does link-specific preparation such as always calling to_arg functions.
_menu_load_objects Loads objects into the map as defined in the $item['load_functions'].
_menu_navigation_links_rebuild Helper function to build menu links for the items in the menu router.
_menu_router_build Helper function to build the router table based on the data from hook_menu.
_menu_router_cache Helper function to store the menu router if we have it in memory.
_menu_set_expanded_menus Helper function to update a list of menus with expanded items
_menu_site_is_offline Checks whether the site is off-line for maintenance.
_menu_translate Handles dynamic path translation and menu access control.
_menu_tree_check_access Recursive helper function for menu_tree_check_access()
_menu_tree_cid Helper function - compute the real cache ID for menu tree data.
_menu_tree_data Recursive helper function to build the data representing a menu tree.
_menu_update_parental_status Check and update the has_children status for the parent of a link.

Constants

Nombreorden descendente Descripción
MENU_ACCESS_DENIED
MENU_CALLBACK Callbacks simply register a path so that the correct function is fired when the URL is accessed. They are not shown in the menu.
MENU_CREATED_BY_ADMIN
MENU_DEFAULT_LOCAL_TASK Every set of local tasks should provide one "default" task, that links to the same path as its parent when clicked.
MENU_FOUND
MENU_IS_LOCAL_TASK
MENU_IS_ROOT
MENU_LINKS_TO_PARENT
MENU_LOCAL_TASK Local tasks are rendered as tabs by default. Use this for menu items that describe actions to be performed on their parent item. An example is the path "node/52/edit", which performs the "edit" task on "node/52".
MENU_MAX_DEPTH The maximum depth of a menu links tree - matches the number of p columns.
MENU_MAX_PARTS The maximum number of path elements for a menu callback
MENU_MODIFIED_BY_ADMIN
MENU_NORMAL_ITEM Normal menu items show up in the menu tree and can be moved/hidden by the administrator. Use this for most menu items. It is the default value if no menu item type is specified.
MENU_NOT_FOUND
MENU_SITE_OFFLINE
MENU_SUGGESTED_ITEM Modules may "suggest" menu items that the administrator may enable. They act just as callbacks do until enabled, at which time they act like normal items. Note for the value: 0x0010 was a flag which is no longer used, but this way the values…
MENU_VISIBLE_IN_BREADCRUMB
MENU_VISIBLE_IN_TREE