Mercurial > defr > drupal > core
comparison modules/menu/menu.module @ 1:c1f4ac30525a 6.0
Drupal 6.0
author | Franck Deroche <webmaster@defr.org> |
---|---|
date | Tue, 23 Dec 2008 14:28:28 +0100 |
parents | |
children | 2427550111ae |
comparison
equal
deleted
inserted
replaced
0:5a113a1c4740 | 1:c1f4ac30525a |
---|---|
1 <?php | |
2 // $Id: menu.module,v 1.157.2.1 2008/02/11 15:12:53 goba Exp $ | |
3 | |
4 /** | |
5 * @file | |
6 * Allows administrators to customize the site navigation menu. | |
7 */ | |
8 | |
9 /** | |
10 * Maximum length of menu name as entered by the user. Database length is 32 | |
11 * and we add a menu- prefix. | |
12 */ | |
13 define('MENU_MAX_MENU_NAME_LENGTH_UI', 27); | |
14 | |
15 /** | |
16 * Implementation of hook_help(). | |
17 */ | |
18 function menu_help($path, $arg) { | |
19 switch ($path) { | |
20 case 'admin/help#menu': | |
21 $output = '<p>'. t("The menu module provides an interface to control and customize Drupal's powerful menu system. Menus are a hierarchical collection of links, or menu items, used to navigate a website, and are positioned and displayed using Drupal's flexible block system. By default, three menus are created during installation: <em>Navigation</em>, <em>Primary links</em>, and <em>Secondary links</em>. The <em>Navigation</em> menu contains most links necessary for working with and navigating your site, and is often displayed in either the left or right sidebar. Most Drupal themes also provide support for <em>Primary links</em> and <em>Secondary links</em>, by displaying them in either the header or footer of each page. By default, <em>Primary links</em> and <em>Secondary links</em> contain no menu items but may be configured to contain custom menu items specific to your site.") .'</p>'; | |
22 $output .= '<p>'. t('The <a href="@menu">menus page</a> displays all menus currently available on your site. Select a menu from this list to add or edit a menu item, or to rearrange items within the menu. Create new menus using the <a href="@add-menu">add menu page</a> (the block containing a new menu must also be enabled on the <a href="@blocks">blocks administration page</a>).', array('@menu' => url('admin/build/menu'), '@add-menu' => url('admin/build/menu/add'), '@blocks' => url('admin/build/block'))) .'</p>'; | |
23 $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@menu">Menu module</a>.', array('@menu' => 'http://drupal.org/handbook/modules/menu/')) .'</p>'; | |
24 return $output; | |
25 case 'admin/build/menu': | |
26 return '<p>'. t('Menus are a collection of links (menu items) used to navigate a website. The menus currently available on your site are displayed below. Select a menu from this list to manage its menu items.') .'</p>'; | |
27 case 'admin/build/menu/add': | |
28 return '<p>'. t('Enter the name for your new menu. Remember to enable the newly created block in the <a href="@blocks">blocks administration page</a>.', array('@blocks' => url('admin/build/block'))) .'</p>'; | |
29 case 'admin/build/menu-customize/%': | |
30 return '<p>'. t('To rearrange menu items, grab a drag-and-drop handle under the <em>Menu item</em> column and drag the items (or group of items) to a new location in the list. (Grab a handle by clicking and holding the mouse while hovering over a handle icon.) Remember that your changes will not be saved until you click the <em>Save configuration</em> button at the bottom of the page.') .'<p>'; | |
31 case 'admin/build/menu/item/add': | |
32 return '<p>'. t('Enter the title and path for your new menu item.') .'</p>'; | |
33 } | |
34 } | |
35 | |
36 /** | |
37 * Implementation of hook_perm(). | |
38 */ | |
39 function menu_perm() { | |
40 return array('administer menu'); | |
41 } | |
42 | |
43 /** | |
44 * Implementation of hook_menu(). | |
45 */ | |
46 function menu_menu() { | |
47 $items['admin/build/menu'] = array( | |
48 'title' => 'Menus', | |
49 'description' => "Control your site's navigation menu, primary links and secondary links. as well as rename and reorganize menu items.", | |
50 'page callback' => 'menu_overview_page', | |
51 'access callback' => 'user_access', | |
52 'access arguments' => array('administer menu'), | |
53 'file' => 'menu.admin.inc', | |
54 ); | |
55 | |
56 $items['admin/build/menu/list'] = array( | |
57 'title' => 'List menus', | |
58 'type' => MENU_DEFAULT_LOCAL_TASK, | |
59 'weight' => -10, | |
60 'file' => 'menu.admin.inc', | |
61 ); | |
62 $items['admin/build/menu/add'] = array( | |
63 'title' => 'Add menu', | |
64 'page callback' => 'drupal_get_form', | |
65 'page arguments' => array('menu_edit_menu', 'add'), | |
66 'type' => MENU_LOCAL_TASK, | |
67 'file' => 'menu.admin.inc', | |
68 ); | |
69 $items['admin/build/menu/settings'] = array( | |
70 'title' => 'Settings', | |
71 'page callback' => 'drupal_get_form', | |
72 'page arguments' => array('menu_configure'), | |
73 'type' => MENU_LOCAL_TASK, | |
74 'weight' => 5, | |
75 'file' => 'menu.admin.inc', | |
76 ); | |
77 $items['admin/build/menu-customize/%menu'] = array( | |
78 'title' => 'Customize menu', | |
79 'page callback' => 'drupal_get_form', | |
80 'page arguments' => array('menu_overview_form', 3), | |
81 'title callback' => 'menu_overview_title', | |
82 'title arguments' => array(3), | |
83 'access arguments' => array('administer menu'), | |
84 'type' => MENU_CALLBACK, | |
85 'file' => 'menu.admin.inc', | |
86 ); | |
87 $items['admin/build/menu-customize/%menu/list'] = array( | |
88 'title' => 'List items', | |
89 'weight' => -10, | |
90 'type' => MENU_DEFAULT_LOCAL_TASK, | |
91 'file' => 'menu.admin.inc', | |
92 ); | |
93 $items['admin/build/menu-customize/%menu/add'] = array( | |
94 'title' => 'Add item', | |
95 'page callback' => 'drupal_get_form', | |
96 'page arguments' => array('menu_edit_item', 'add', NULL, 3), | |
97 'type' => MENU_LOCAL_TASK, | |
98 'file' => 'menu.admin.inc', | |
99 ); | |
100 $items['admin/build/menu-customize/%menu/edit'] = array( | |
101 'title' => 'Edit menu', | |
102 'page callback' => 'drupal_get_form', | |
103 'page arguments' => array('menu_edit_menu', 'edit', 3), | |
104 'type' => MENU_LOCAL_TASK, | |
105 'file' => 'menu.admin.inc', | |
106 ); | |
107 $items['admin/build/menu-customize/%menu/delete'] = array( | |
108 'title' => 'Delete menu', | |
109 'page callback' => 'menu_delete_menu_page', | |
110 'page arguments' => array(3), | |
111 'type' => MENU_CALLBACK, | |
112 'file' => 'menu.admin.inc', | |
113 ); | |
114 $items['admin/build/menu/item/%menu_link/edit'] = array( | |
115 'title' => 'Edit menu item', | |
116 'page callback' => 'drupal_get_form', | |
117 'page arguments' => array('menu_edit_item', 'edit', 4, NULL), | |
118 'type' => MENU_CALLBACK, | |
119 'file' => 'menu.admin.inc', | |
120 ); | |
121 $items['admin/build/menu/item/%menu_link/reset'] = array( | |
122 'title' => 'Reset menu item', | |
123 'page callback' => 'drupal_get_form', | |
124 'page arguments' => array('menu_reset_item_confirm', 4), | |
125 'type' => MENU_CALLBACK, | |
126 'file' => 'menu.admin.inc', | |
127 ); | |
128 $items['admin/build/menu/item/%menu_link/delete'] = array( | |
129 'title' => 'Delete menu item', | |
130 'page callback' => 'menu_item_delete_page', | |
131 'page arguments' => array(4), | |
132 'type' => MENU_CALLBACK, | |
133 'file' => 'menu.admin.inc', | |
134 ); | |
135 | |
136 return $items; | |
137 } | |
138 | |
139 /** | |
140 * Implemenation of hook_theme(). | |
141 */ | |
142 function menu_theme() { | |
143 return array( | |
144 'menu_overview_form' => array( | |
145 'file' => 'menu.admin.inc', | |
146 'arguments' => array('form' => NULL), | |
147 ), | |
148 ); | |
149 } | |
150 | |
151 /** | |
152 * Implementation of hook_enable() | |
153 * | |
154 * Add a link for each custom menu. | |
155 */ | |
156 function menu_enable() { | |
157 menu_rebuild(); | |
158 $link = db_fetch_array(db_query("SELECT mlid AS plid, menu_name from {menu_links} WHERE link_path = 'admin/build/menu' AND module = 'system'")); | |
159 $link['router_path'] = 'admin/build/menu-customize/%'; | |
160 $link['module'] = 'menu'; | |
161 $result = db_query("SELECT * FROM {menu_custom}"); | |
162 while ($menu = db_fetch_array($result)) { | |
163 $link['mlid'] = 0; | |
164 $link['link_title'] = $menu['title']; | |
165 $link['link_path'] = 'admin/build/menu-customize/'. $menu['menu_name']; | |
166 if (!db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_path = '%s' AND plid = %d", $link['link_path'], $link['plid']))) { | |
167 menu_link_save($link); | |
168 } | |
169 } | |
170 menu_cache_clear_all(); | |
171 } | |
172 | |
173 /** | |
174 * Title callback for the menu overview page and links. | |
175 */ | |
176 function menu_overview_title($menu) { | |
177 return $menu['title']; | |
178 } | |
179 | |
180 /** | |
181 * Load the data for a single custom menu. | |
182 */ | |
183 function menu_load($menu_name) { | |
184 return db_fetch_array(db_query("SELECT * FROM {menu_custom} WHERE menu_name = '%s'", $menu_name)); | |
185 } | |
186 | |
187 /** | |
188 * Return a list of menu items that are valid possible parents for the given menu item. | |
189 * | |
190 * @param $menus | |
191 * An array of menu names and titles, such as from menu_get_menus(). | |
192 * @param $item | |
193 * The menu item for which to generate a list of parents. | |
194 * If $item['mlid'] == 0 then the complete tree is returned. | |
195 * @return | |
196 * An array of menu link titles keyed on the a string containing the menu name | |
197 * and mlid. The list excludes the given item and its children. | |
198 */ | |
199 function menu_parent_options($menus, $item) { | |
200 // The menu_links table can be practically any size and we need a way to | |
201 // allow contrib modules to provide more scalable pattern choosers. | |
202 // hook_form_alter is too late in itself because all the possible parents are | |
203 // retrieved here, unless menu_override_parent_selector is set to TRUE. | |
204 if (variable_get('menu_override_parent_selector', FALSE)) { | |
205 return array(); | |
206 } | |
207 // If the item has children, there is an added limit to the depth of valid parents. | |
208 if (isset($item['parent_depth_limit'])) { | |
209 $limit = $item['parent_depth_limit']; | |
210 } | |
211 else { | |
212 $limit = _menu_parent_depth_limit($item); | |
213 } | |
214 | |
215 foreach ($menus as $menu_name => $title) { | |
216 $tree = menu_tree_all_data($menu_name, NULL); | |
217 $options[$menu_name .':0'] = '<'. $title .'>'; | |
218 _menu_parents_recurse($tree, $menu_name, '--', $options, $item['mlid'], $limit); | |
219 } | |
220 return $options; | |
221 } | |
222 | |
223 /** | |
224 * Recursive helper function for menu_parent_options(). | |
225 */ | |
226 function _menu_parents_recurse($tree, $menu_name, $indent, &$options, $exclude, $depth_limit) { | |
227 foreach ($tree as $data) { | |
228 if ($data['link']['depth'] > $depth_limit) { | |
229 // Don't iterate through any links on this level. | |
230 break; | |
231 } | |
232 if ($data['link']['mlid'] != $exclude && $data['link']['hidden'] >= 0) { | |
233 $title = $indent .' '. truncate_utf8($data['link']['title'], 30, TRUE, FALSE); | |
234 if ($data['link']['hidden']) { | |
235 $title .= ' ('. t('disabled') .')'; | |
236 } | |
237 $options[$menu_name .':'. $data['link']['mlid']] = $title; | |
238 if ($data['below']) { | |
239 _menu_parents_recurse($data['below'], $menu_name, $indent .'--', $options, $exclude, $depth_limit); | |
240 } | |
241 } | |
242 } | |
243 } | |
244 | |
245 /** | |
246 * Reset a system-defined menu item. | |
247 */ | |
248 function menu_reset_item($item) { | |
249 $router = menu_router_build(); | |
250 $new_item = _menu_link_build($router[$item['router_path']]); | |
251 foreach (array('mlid', 'has_children') as $key) { | |
252 $new_item[$key] = $item[$key]; | |
253 } | |
254 menu_link_save($new_item); | |
255 return $new_item; | |
256 } | |
257 | |
258 /** | |
259 * Implementation of hook_block(). | |
260 */ | |
261 function menu_block($op = 'list', $delta = 0) { | |
262 $menus = menu_get_menus(); | |
263 // The Navigation menu is handled by the user module. | |
264 unset($menus['navigation']); | |
265 if ($op == 'list') { | |
266 $blocks = array(); | |
267 foreach ($menus as $name => $title) { | |
268 // Default "Navigation" block is handled by user.module. | |
269 $blocks[$name]['info'] = check_plain($title); | |
270 // Menu blocks can't be cached because each menu item can have | |
271 // a custom access callback. menu.inc manages its own caching. | |
272 $blocks[$name]['cache'] = BLOCK_NO_CACHE; | |
273 } | |
274 return $blocks; | |
275 } | |
276 else if ($op == 'view') { | |
277 $data['subject'] = check_plain($menus[$delta]); | |
278 $data['content'] = menu_tree($delta); | |
279 return $data; | |
280 } | |
281 } | |
282 | |
283 /** | |
284 * Implementation of hook_nodeapi(). | |
285 */ | |
286 function menu_nodeapi(&$node, $op) { | |
287 switch ($op) { | |
288 case 'insert': | |
289 case 'update': | |
290 if (isset($node->menu)) { | |
291 $item = $node->menu; | |
292 if (!empty($item['delete'])) { | |
293 menu_link_delete($item['mlid']); | |
294 } | |
295 elseif (trim($item['link_title'])) { | |
296 $item['link_title'] = trim($item['link_title']); | |
297 $item['link_path'] = "node/$node->nid"; | |
298 if (!$item['customized']) { | |
299 $item['options']['attributes']['title'] = trim($node->title); | |
300 } | |
301 if (!menu_link_save($item)) { | |
302 drupal_set_message(t('There was an error saving the menu link.'), 'error'); | |
303 } | |
304 } | |
305 } | |
306 break; | |
307 case 'delete': | |
308 // Delete all menu module links that point to this node. | |
309 $result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = 'node/%d' AND module = 'menu'", $node->nid); | |
310 while ($m = db_fetch_array($result)) { | |
311 menu_link_delete($m['mlid']); | |
312 } | |
313 break; | |
314 case 'prepare': | |
315 if (empty($node->menu)) { | |
316 // Prepare the node for the edit form so that $node->menu always exists. | |
317 $menu_name = variable_get('menu_default_node_menu', 'primary-links'); | |
318 $item = array(); | |
319 if (isset($node->nid)) { | |
320 // Give priority to the default menu | |
321 $mlid = db_result(db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = 'node/%d' AND menu_name = '%s' AND module = 'menu' ORDER BY mlid ASC", $node->nid, $menu_name, 0, 1)); | |
322 // Check all menus if a link does not exist in the default menu. | |
323 if (!$mlid) { | |
324 $mlid = db_result(db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = 'node/%d' AND module = 'menu' ORDER BY mlid ASC", $node->nid, 0, 1)); | |
325 } | |
326 if ($mlid) { | |
327 $item = menu_link_load($mlid); | |
328 } | |
329 } | |
330 // Set default values. | |
331 $node->menu = $item + array('link_title' => '', 'mlid' => 0, 'plid' => 0, 'menu_name' => $menu_name, 'weight' => 0, 'options' => array(), 'module' => 'menu', 'expanded' => 0, 'hidden' => 0, 'has_children' => 0, 'customized' => 0); | |
332 } | |
333 // Find the depth limit for the parent select. | |
334 if (!isset($node->menu['parent_depth_limit'])) { | |
335 $node->menu['parent_depth_limit'] = _menu_parent_depth_limit($node->menu); | |
336 } | |
337 break; | |
338 } | |
339 } | |
340 | |
341 /** | |
342 * Find the depth limit for items in the parent select. | |
343 */ | |
344 function _menu_parent_depth_limit($item) { | |
345 return MENU_MAX_DEPTH - 1 - (($item['mlid'] && $item['has_children']) ? menu_link_children_relative_depth($item) : 0); | |
346 } | |
347 | |
348 /** | |
349 * Implementation of hook_form_alter(). Adds menu item fields to the node form. | |
350 */ | |
351 function menu_form_alter(&$form, $form_state, $form_id) { | |
352 if (isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) { | |
353 // Note - doing this to make sure the delete checkbox stays in the form. | |
354 $form['#cache'] = TRUE; | |
355 | |
356 $form['menu'] = array( | |
357 '#type' => 'fieldset', | |
358 '#title' => t('Menu settings'), | |
359 '#access' => user_access('administer menu'), | |
360 '#collapsible' => TRUE, | |
361 '#collapsed' => FALSE, | |
362 '#tree' => TRUE, | |
363 '#weight' => -2, | |
364 '#attributes' => array('class' => 'menu-item-form'), | |
365 ); | |
366 $item = $form['#node']->menu; | |
367 | |
368 if ($item['mlid']) { | |
369 // There is an existing link. | |
370 $form['menu']['delete'] = array( | |
371 '#type' => 'checkbox', | |
372 '#title' => t('Delete this menu item.'), | |
373 ); | |
374 } | |
375 if (!$item['link_title']) { | |
376 $form['menu']['#collapsed'] = TRUE; | |
377 } | |
378 | |
379 foreach (array('mlid', 'module', 'hidden', 'has_children', 'customized', 'options', 'expanded', 'hidden', 'parent_depth_limit') as $key) { | |
380 $form['menu'][$key] = array('#type' => 'value', '#value' => $item[$key]); | |
381 } | |
382 $form['menu']['#item'] = $item; | |
383 | |
384 $form['menu']['link_title'] = array('#type' => 'textfield', | |
385 '#title' => t('Menu link title'), | |
386 '#default_value' => $item['link_title'], | |
387 '#description' => t('The link text corresponding to this item that should appear in the menu. Leave blank if you do not wish to add this post to the menu.'), | |
388 '#required' => FALSE, | |
389 ); | |
390 // Generate a list of possible parents (not including this item or descendants). | |
391 $options = menu_parent_options(menu_get_menus(), $item); | |
392 $default = $item['menu_name'] .':'. $item['plid']; | |
393 if (!isset($options[$default])) { | |
394 $default = 'primary-links:0'; | |
395 } | |
396 $form['menu']['parent'] = array( | |
397 '#type' => 'select', | |
398 '#title' => t('Parent item'), | |
399 '#default_value' => $default, | |
400 '#options' => $options, | |
401 '#description' => t('The maximum depth for an item and all its children is fixed at !maxdepth. Some menu items may not be available as parents if selecting them would exceed this limit.', array('!maxdepth' => MENU_MAX_DEPTH)), | |
402 '#attributes' => array('class' => 'menu-title-select'), | |
403 ); | |
404 $form['#submit'][] = 'menu_node_form_submit'; | |
405 | |
406 $form['menu']['weight'] = array( | |
407 '#type' => 'weight', | |
408 '#title' => t('Weight'), | |
409 '#delta' => 50, | |
410 '#default_value' => $item['weight'], | |
411 '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'), | |
412 ); | |
413 } | |
414 } | |
415 | |
416 /** | |
417 * Decompose the selected menu parent option into the menu_name and plid. | |
418 */ | |
419 function menu_node_form_submit($form, &$form_state) { | |
420 list($form_state['values']['menu']['menu_name'], $form_state['values']['menu']['plid']) = explode(':', $form_state['values']['menu']['parent']); | |
421 } | |
422 | |
423 /** | |
424 * Return an associative array of the custom menus names. | |
425 * | |
426 * @param $all | |
427 * If FALSE return only user-added menus, or if TRUE also include | |
428 * the menus defined by the system. | |
429 * @return | |
430 * An array with the machine-readable names as the keys, and human-readable | |
431 * titles as the values. | |
432 */ | |
433 function menu_get_menus($all = TRUE) { | |
434 $system_menus = menu_list_system_menus(); | |
435 $sql = 'SELECT * FROM {menu_custom}'; | |
436 if (!$all) { | |
437 $sql .= ' WHERE menu_name NOT IN ('. implode(',', array_fill(0, count($system_menus), "'%s'")) .')'; | |
438 } | |
439 $sql .= ' ORDER BY title'; | |
440 $result = db_query($sql, $system_menus); | |
441 $rows = array(); | |
442 while ($r = db_fetch_array($result)) { | |
443 $rows[$r['menu_name']] = $r['title']; | |
444 } | |
445 return $rows; | |
446 } |