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