/[drupal]/drupal/modules/menu/menu.admin.inc
ViewVC logotype

Contents of /drupal/modules/menu/menu.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.68 - (show annotations) (download) (as text)
Wed Nov 4 04:43:58 2009 UTC (3 weeks, 1 day ago) by webchick
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10, HEAD
Changes since 1.67: +4 -4 lines
File MIME type: text/x-php
#576916 follow-up by mooffie: Fixed some mistaken variable renaming.
1 <?php
2 // $Id: menu.admin.inc,v 1.67 2009/11/03 05:27:18 webchick Exp $
3
4 /**
5 * @file
6 * Administrative page callbacks for menu module.
7 */
8
9 /**
10 * Menu callback which shows an overview page of all the custom menus and their descriptions.
11 */
12 function menu_overview_page() {
13 $result = db_query("SELECT * FROM {menu_custom} ORDER BY title", array(), array('fetch' => PDO::FETCH_ASSOC));
14 $header = array(t('Title'), array('data' => t('Operations'), 'colspan' => '3'));
15 $rows = array();
16 foreach ($result as $menu) {
17 $row = array(theme('menu_admin_overview', array('title' => $menu['title'], 'name' => $menu['menu_name'], 'description' => $menu['description'])));
18 $row[] = array('data' => l(t('list links'), 'admin/structure/menu/manage/' . $menu['menu_name']));
19 $row[] = array('data' => l(t('edit menu'), 'admin/structure/menu/manage/' . $menu['menu_name'] . '/edit'));
20 $row[] = array('data' => l(t('add link'), 'admin/structure/menu/manage/' . $menu['menu_name'] . '/add'));
21 $rows[] = $row;
22 }
23
24 return theme('table', array('header' => $header, 'rows' => $rows));
25 }
26
27 /**
28 * Theme the menu title and description for admin page
29 */
30 function theme_menu_admin_overview($variables) {
31 $output = check_plain($variables['title']);
32 $output .= '<div class="description">' . filter_xss_admin($variables['description']) . '</div>';
33
34 return $output;
35 }
36
37 /**
38 * Form for editing an entire menu tree at once.
39 *
40 * Shows for one menu the menu links accessible to the current user and
41 * relevant operations.
42 */
43 function menu_overview_form($form, &$form_state, $menu) {
44 global $menu_admin;
45 $form['#attached']['css'] = array(drupal_get_path('module', 'menu') . '/menu.css');
46 $sql = "
47 SELECT m.load_functions, m.to_arg_functions, m.access_callback, m.access_arguments, m.page_callback, m.page_arguments, m.delivery_callback, m.title, m.title_callback, m.title_arguments, m.type, m.description, ml.*
48 FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
49 WHERE ml.menu_name = :menu
50 ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC";
51 $result = db_query($sql, array(':menu' => $menu['menu_name']), array('fetch' => PDO::FETCH_ASSOC));
52 $links = array();
53 foreach ($result as $item) {
54 $links[] = $item;
55 }
56 $tree = menu_tree_data($links);
57 $node_links = array();
58 menu_tree_collect_node_links($tree, $node_links);
59 // We indicate that a menu administrator is running the menu access check.
60 $menu_admin = TRUE;
61 menu_tree_check_access($tree, $node_links);
62 $menu_admin = FALSE;
63
64 $form = array_merge($form, _menu_overview_tree_form($tree));
65 $form['#menu'] = $menu;
66 if (element_children($form)) {
67 $form['submit'] = array(
68 '#type' => 'submit',
69 '#value' => t('Save configuration'),
70 );
71 }
72 else {
73 $form['#empty_text'] = t('There are no menu links yet. <a href="@link">Add link</a>.', array('@link' => url('admin/structure/menu/manage/'. $form['#menu']['menu_name'] .'/add')));
74 }
75 return $form;
76 }
77
78 /**
79 * Recursive helper function for menu_overview_form().
80 */
81 function _menu_overview_tree_form($tree) {
82 $form = &drupal_static(__FUNCTION__, array('#tree' => TRUE));
83 foreach ($tree as $data) {
84 $title = '';
85 $item = $data['link'];
86 // Don't show callbacks; these have $item['hidden'] < 0.
87 if ($item && $item['hidden'] >= 0) {
88 $mlid = 'mlid:' . $item['mlid'];
89 $form[$mlid]['#item'] = $item;
90 $form[$mlid]['#attributes'] = $item['hidden'] ? array('class' => array('menu-disabled')) : array('class' => array('menu-enabled'));
91 $form[$mlid]['title']['#markup'] = l($item['title'], $item['href'], $item['localized_options']) . ($item['hidden'] ? ' (' . t('disabled') . ')' : '');
92 $form[$mlid]['hidden'] = array(
93 '#type' => 'checkbox',
94 '#default_value' => !$item['hidden'],
95 );
96 $form[$mlid]['weight'] = array(
97 '#type' => 'weight',
98 '#delta' => 50,
99 '#default_value' => isset($form_state[$mlid]['weight']) ? $form_state[$mlid]['weight'] : $item['weight'],
100 );
101 $form[$mlid]['mlid'] = array(
102 '#type' => 'hidden',
103 '#value' => $item['mlid'],
104 );
105 $form[$mlid]['plid'] = array(
106 '#type' => 'textfield',
107 '#default_value' => isset($form_state[$mlid]['plid']) ? $form_state[$mlid]['plid'] : $item['plid'],
108 '#size' => 6,
109 );
110 // Build a list of operations.
111 $operations = array();
112 $operations['edit'] = array('#type' => 'link', '#title' => t('edit'), '#href' => 'admin/structure/menu/item/' . $item['mlid'] . '/edit');
113 // Only items created by the menu module can be deleted.
114 if ($item['module'] == 'menu' || $item['updated'] == 1) {
115 $operations['delete'] = array('#type' => 'link', '#title' => t('delete'), '#href' => 'admin/structure/menu/item/' . $item['mlid'] . '/delete');
116 }
117 // Set the reset column.
118 elseif ($item['module'] == 'system' && $item['customized']) {
119 $operations['reset'] = array('#type' => 'link', '#title' => t('reset'), '#href' => 'admin/structure/menu/item/' . $item['mlid'] . '/reset');
120 }
121 $form[$mlid]['operations'] = $operations;
122 }
123
124 if ($data['below']) {
125 _menu_overview_tree_form($data['below']);
126 }
127 }
128 return $form;
129 }
130
131 /**
132 * Submit handler for the menu overview form.
133 *
134 * This function takes great care in saving parent items first, then items
135 * underneath them. Saving items in the incorrect order can break the menu tree.
136 *
137 * @see menu_overview_form()
138 */
139 function menu_overview_form_submit($form, &$form_state) {
140 // When dealing with saving menu items, the order in which these items are
141 // saved is critical. If a changed child item is saved before its parent,
142 // the child item could be saved with an invalid path past its immediate
143 // parent. To prevent this, save items in the form in the same order they
144 // are sent by $_POST, ensuring parents are saved first, then their children.
145 // See http://drupal.org/node/181126#comment-632270
146 $order = array_flip(array_keys($form_state['input'])); // Get the $_POST order.
147 $form = array_merge($order, $form); // Update our original form with the new order.
148
149 $updated_items = array();
150 $fields = array('weight', 'plid');
151 foreach (element_children($form) as $mlid) {
152 if (isset($form[$mlid]['#item'])) {
153 $element = $form[$mlid];
154 // Update any fields that have changed in this menu item.
155 foreach ($fields as $field) {
156 if ($element[$field]['#value'] != $element[$field]['#default_value']) {
157 $element['#item'][$field] = $element[$field]['#value'];
158 $updated_items[$mlid] = $element['#item'];
159 }
160 }
161 // Hidden is a special case, the value needs to be reversed.
162 if ($element['hidden']['#value'] != $element['hidden']['#default_value']) {
163 // Convert to integer rather than boolean due to PDO cast to string.
164 $element['#item']['hidden'] = $element['hidden']['#value'] ? 0 : 1;
165 $updated_items[$mlid] = $element['#item'];
166 }
167 }
168 }
169
170 // Save all our changed items to the database.
171 foreach ($updated_items as $item) {
172 $item['customized'] = 1;
173 menu_link_save($item);
174 }
175 }
176
177 /**
178 * Theme the menu overview form into a table.
179 *
180 * @ingroup themeable
181 */
182 function theme_menu_overview_form($variables) {
183 $form = $variables['form'];
184
185 drupal_add_tabledrag('menu-overview', 'match', 'parent', 'menu-plid', 'menu-plid', 'menu-mlid', TRUE, MENU_MAX_DEPTH - 1);
186 drupal_add_tabledrag('menu-overview', 'order', 'sibling', 'menu-weight');
187
188 $header = array(
189 t('Menu link'),
190 array('data' => t('Enabled'), 'class' => array('checkbox')),
191 t('Weight'),
192 array('data' => t('Operations'), 'colspan' => '3'),
193 );
194
195 $rows = array();
196 foreach (element_children($form) as $mlid) {
197 if (isset($form[$mlid]['hidden'])) {
198 $element = &$form[$mlid];
199 // Build a list of operations.
200 $operations = array();
201 foreach (element_children($element['operations']) as $op) {
202 $operations[] = array('data' => drupal_render($element['operations'][$op]), 'class' => array('menu-operations'));
203 }
204 while (count($operations) < 2) {
205 $operations[] = '';
206 }
207
208 // Add special classes to be used for tabledrag.js.
209 $element['plid']['#attributes']['class'] = array('menu-plid');
210 $element['mlid']['#attributes']['class'] = array('menu-mlid');
211 $element['weight']['#attributes']['class'] = array('menu-weight');
212
213 // Change the parent field to a hidden. This allows any value but hides the field.
214 $element['plid']['#type'] = 'hidden';
215
216 $row = array();
217 $row[] = theme('indentation', array('size' => $element['#item']['depth'] - 1)) . drupal_render($element['title']);
218 $row[] = array('data' => drupal_render($element['hidden']), 'class' => array('checkbox', 'menu-enabled'));
219 $row[] = drupal_render($element['weight']) . drupal_render($element['plid']) . drupal_render($element['mlid']);
220 $row = array_merge($row, $operations);
221
222 $row = array_merge(array('data' => $row), $element['#attributes']);
223 $row['class'][] = 'draggable';
224 $rows[] = $row;
225 }
226 }
227 $output = '';
228 if (empty($rows)) {
229 $rows[] = array(array('data' => $form['#empty_text'], 'colspan' => '7'));
230 }
231 $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'menu-overview')));
232 $output .= drupal_render_children($form);
233 return $output;
234 }
235
236 /**
237 * Menu callback; Build the menu link editing form.
238 */
239 function menu_edit_item($form, &$form_state, $type, $item, $menu) {
240
241 $form['menu'] = array(
242 '#type' => 'fieldset',
243 '#title' => t('Menu settings'),
244 '#collapsible' => FALSE,
245 '#tree' => TRUE,
246 '#weight' => -2,
247 '#attributes' => array('class' => array('menu-item-form')),
248 '#item' => $item,
249 );
250 if ($type == 'add' || empty($item)) {
251 // This is an add form, initialize the menu link.
252 $item = array('link_title' => '', 'mlid' => 0, 'plid' => 0, 'menu_name' => $menu['menu_name'], 'weight' => 0, 'link_path' => '', 'options' => array(), 'module' => 'menu', 'expanded' => 0, 'hidden' => 0, 'has_children' => 0);
253 }
254 $form['menu']['link_title'] = array('#type' => 'textfield',
255 '#title' => t('Menu link title'),
256 '#default_value' => $item['link_title'],
257 '#description' => t('The text to be used for this link in the menu.'),
258 '#required' => TRUE,
259 );
260 foreach (array('link_path', 'mlid', 'module', 'has_children', 'options') as $key) {
261 $form['menu'][$key] = array('#type' => 'value', '#value' => $item[$key]);
262 }
263 // Any item created or edited via this interface is considered "customized".
264 $form['menu']['customized'] = array('#type' => 'value', '#value' => 1);
265 $form['menu']['original_item'] = array('#type' => 'value', '#value' => $item);
266
267 $path = $item['link_path'];
268 if (isset($item['options']['query'])) {
269 $path .= '?' . $item['options']['query'];
270 }
271 if (isset($item['options']['fragment'])) {
272 $path .= '#' . $item['options']['fragment'];
273 }
274 if ($item['module'] == 'menu') {
275 $form['menu']['link_path'] = array(
276 '#type' => 'textfield',
277 '#title' => t('Path'),
278 '#default_value' => $path,
279 '#description' => t('The path for this menu link. This can be an internal Drupal path such as %add-node or an external URL such as %drupal. Enter %front to link to the front page.', array('%front' => '<front>', '%add-node' => 'node/add', '%drupal' => 'http://drupal.org')),
280 '#required' => TRUE,
281 );
282 $form['delete'] = array(
283 '#type' => 'submit',
284 '#value' => t('Delete'),
285 '#access' => $item['mlid'],
286 '#submit' => array('menu_item_delete_submit'),
287 '#weight' => 10,
288 );
289 }
290 else {
291 $form['menu']['_path'] = array(
292 '#type' => 'item',
293 '#title' => t('Path'),
294 '#description' => l($item['link_title'], $item['href'], $item['options']),
295 );
296 }
297 $form['menu']['description'] = array(
298 '#type' => 'textarea',
299 '#title' => t('Description'),
300 '#default_value' => isset($item['options']['attributes']['title']) ? $item['options']['attributes']['title'] : '',
301 '#rows' => 1,
302 '#description' => t('The description displayed when hovering over a menu link.'),
303 );
304 $form['menu']['enabled'] = array(
305 '#type' => 'checkbox',
306 '#title' => t('Enabled'),
307 '#default_value' => !$item['hidden'],
308 '#description' => t('Menu links that are not enabled will not be listed in any menu.'),
309 );
310 $form['menu']['expanded'] = array(
311 '#type' => 'checkbox',
312 '#title' => t('Show as expanded'),
313 '#default_value' => $item['expanded'],
314 '#description' => t('If selected and this menu link has children, the menu will always appear expanded.'),
315 );
316
317 // Generate a list of possible parents (not including this link or descendants).
318 $options = menu_parent_options(menu_get_menus(), $item);
319 $default = $item['menu_name'] . ':' . $item['plid'];
320 if (!isset($options[$default])) {
321 $default = 'navigation:0';
322 }
323 $form['menu']['parent'] = array(
324 '#type' => 'select',
325 '#title' => t('Parent link'),
326 '#default_value' => $default,
327 '#options' => $options,
328 '#description' => t('The maximum depth for a link and all its children is fixed at !maxdepth. Some menu links may not be available as parents if selecting them would exceed this limit.', array('!maxdepth' => MENU_MAX_DEPTH)),
329 '#attributes' => array('class' => array('menu-title-select')),
330 );
331 $form['menu']['weight'] = array(
332 '#type' => 'weight',
333 '#title' => t('Weight'),
334 '#delta' => 50,
335 '#default_value' => $item['weight'],
336 '#description' => t('Optional. In the menu, the heavier links will sink and the lighter links will be positioned nearer the top.'),
337 );
338 $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
339
340
341 return $form;
342 }
343
344 /**
345 * Validate form values for a menu link being added or edited.
346 */
347 function menu_edit_item_validate($form, &$form_state) {
348 $item = &$form_state['values']['menu'];
349 $normal_path = drupal_get_normal_path($item['link_path']);
350 if ($item['link_path'] != $normal_path) {
351 drupal_set_message(t('The menu system stores system paths only, but will use the URL alias for display. %link_path has been stored as %normal_path', array('%link_path' => $item['link_path'], '%normal_path' => $normal_path)));
352 $item['link_path'] = $normal_path;
353 }
354 if (!url_is_external($item['link_path'])) {
355 $parsed_link = parse_url($item['link_path']);
356 if (isset($parsed_link['query'])) {
357 $item['options']['query'] = $parsed_link['query'];
358 }
359 if (isset($parsed_link['fragment'])) {
360 $item['options']['fragment'] = $parsed_link['fragment'];
361 }
362 if ($item['link_path'] != $parsed_link['path']) {
363 $item['link_path'] = $parsed_link['path'];
364 }
365 }
366 if (!trim($item['link_path']) || !menu_valid_path($item)) {
367 form_set_error('link_path', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $item['link_path'])));
368 }
369 }
370
371 /**
372 * Submit function for the delete button on the menu item editing form.
373 */
374 function menu_item_delete_submit($form, &$form_state) {
375 $form_state['redirect'] = 'admin/structure/menu/item/' . $form_state['values']['menu']['mlid'] . '/delete';
376 }
377
378 /**
379 * Process menu and menu item add/edit form submissions.
380 */
381 function menu_edit_item_submit($form, &$form_state) {
382 $item = &$form_state['values']['menu'];
383
384 // The value of "hidden" is the opposite of the value
385 // supplied by the "enabled" checkbox.
386 $item['hidden'] = (int) !$item['enabled'];
387 unset($item['enabled']);
388
389 $item['options']['attributes']['title'] = $item['description'];
390 list($item['menu_name'], $item['plid']) = explode(':', $item['parent']);
391 if (!menu_link_save($item)) {
392 drupal_set_message(t('There was an error saving the menu link.'), 'error');
393 }
394 $form_state['redirect'] = 'admin/structure/menu/manage/' . $item['menu_name'];
395 }
396
397 /**
398 * Menu callback; Build the form that handles the adding/editing of a custom menu.
399 */
400 function menu_edit_menu($form, &$form_state, $type, $menu = array()) {
401 $system_menus = menu_list_system_menus();
402 $menu += array('menu_name' => '', 'old_name' => '', 'title' => '', 'description' => '');
403 if (!empty($menu['menu_name'])) {
404 $menu['old_name'] = $menu['menu_name'];
405 }
406 $form['old_name'] = array('#type' => 'value', '#value' => $menu['old_name']);
407
408 // The title of a system menu cannot be altered.
409 if (isset($system_menus[$menu['menu_name']])) {
410 $form['title'] = array('#type' => 'value', '#value' => $menu['title']);
411 }
412 else {
413 $form['title'] = array(
414 '#type' => 'textfield',
415 '#title' => t('Title'),
416 '#default_value' => $menu['title'],
417 '#required' => TRUE,
418 '#field_suffix' => ' <small id="edit-title-suffix">&nbsp;</small>',
419 );
420 }
421
422 // The internal menu name can only be defined during initial menu creation.
423 if (!empty($menu['old_name'])) {
424 $form['#insert'] = FALSE;
425 $form['menu_name'] = array('#type' => 'value', '#value' => $menu['menu_name']);
426 }
427 else {
428 $form['#insert'] = TRUE;
429 $js_settings = array(
430 'type' => 'setting',
431 'data' => array(
432 'machineReadableValue' => array(
433 'title' => array(
434 'text' => t('URL path'),
435 'target' => 'menu-name',
436 'searchPattern' => '[^a-z0-9]+',
437 'replaceToken' => '-',
438 ),
439 ),
440 ),
441 );
442 $form['menu_name'] = array(
443 '#type' => 'textfield',
444 '#title' => t('Menu name'),
445 '#maxsize' => MENU_MAX_MENU_NAME_LENGTH_UI,
446 '#description' => t('This text will be used to construct the URL for the menu. The name must contain only lowercase letters, numbers and hyphens, and must be unique.'),
447 '#required' => TRUE,
448 '#attached' => array(
449 'js' => array(drupal_get_path('module', 'system') . '/system.js', $js_settings),
450 ),
451 );
452 }
453
454 $form['description'] = array(
455 '#type' => 'textarea',
456 '#title' => t('Description'),
457 '#default_value' => $menu['description'],
458 );
459 $form['submit'] = array(
460 '#type' => 'submit',
461 '#value' => t('Save'),
462 );
463 // Only custom menus may be deleted.
464 $form['delete'] = array(
465 '#type' => 'submit',
466 '#value' => t('Delete'),
467 '#access' => $type == 'edit' && !isset($system_menus[$menu['menu_name']]),
468 '#submit' => array('menu_custom_delete_submit'),
469 );
470
471 return $form;
472 }
473
474 /**
475 * Submit function for the 'Delete' button on the menu editing form.
476 */
477 function menu_custom_delete_submit($form, &$form_state) {
478 $form_state['redirect'] = 'admin/structure/menu/manage/' . $form_state['values']['menu_name'] . '/delete';
479 }
480
481 /**
482 * Menu callback; check access and get a confirm form for deletion of a custom menu.
483 */
484 function menu_delete_menu_page($menu) {
485 // System-defined menus may not be deleted.
486 $system_menus = menu_list_system_menus();
487 if (isset($system_menus[$menu['menu_name']])) {
488 drupal_access_denied();
489 return;
490 }
491 return drupal_get_form('menu_delete_menu_confirm', $menu);
492 }
493
494 /**
495 * Build a confirm form for deletion of a custom menu.
496 */
497 function menu_delete_menu_confirm($form, &$form_state, $menu) {
498 $form['#menu'] = $menu;
499 $caption = '';
500 $num_links = db_query("SELECT COUNT(*) FROM {menu_links} WHERE menu_name = :menu", array(':menu' => $menu['menu_name']))->fetchField();
501 if ($num_links) {
502 $caption .= '<p>' . format_plural($num_links, '<strong>Warning:</strong> There is currently 1 menu link in %title. It will be deleted (system-defined items will be reset).', '<strong>Warning:</strong> There are currently @count menu links in %title. They will be deleted (system-defined links will be reset).', array('%title' => $menu['title'])) . '</p>';
503 }
504 $caption .= '<p>' . t('This action cannot be undone.') . '</p>';
505 return confirm_form($form, t('Are you sure you want to delete the custom menu %title?', array('%title' => $menu['title'])), 'admin/structure/menu/manage/' . $menu['menu_name'], $caption, t('Delete'));
506 }
507
508 /**
509 * Delete a custom menu and all links in it.
510 */
511 function menu_delete_menu_confirm_submit($form, &$form_state) {
512 $menu = $form['#menu'];
513 $form_state['redirect'] = 'admin/structure/menu';
514
515 // System-defined menus may not be deleted - only menus defined by this module.
516 $system_menus = menu_list_system_menus();
517 if (isset($system_menus[$menu['menu_name']]) || !(db_query("SELECT 1 FROM {menu_custom} WHERE menu_name = :menu", array(':menu' => $menu['menu_name']))->fetchField())) {
518 return;
519 }
520
521 // Reset all the menu links defined by the system via hook_menu().
522 $result = db_query("SELECT * FROM {menu_links} ml INNER JOIN {menu_router} m ON ml.router_path = m.path WHERE ml.menu_name = :menu AND ml.module = 'system' ORDER BY m.number_parts ASC", array(':menu' => $menu['menu_name']), array('fetch' => PDO::FETCH_ASSOC));
523 foreach ($result as $link) {
524 menu_reset_item($link);
525 }
526
527 // Delete all links to the overview page for this menu.
528 $result = db_query("SELECT mlid FROM {menu_links} ml WHERE ml.link_path = :link", array(':link' => 'admin/structure/menu/manage/' . $menu['menu_name']), array('fetch' => PDO::FETCH_ASSOC));
529 foreach ($result as $link) {
530 menu_link_delete($link['mlid']);
531 }
532
533 // Delete the custom menu and all its menu links.
534 menu_delete($menu);
535
536 $t_args = array('%title' => $menu['title']);
537 drupal_set_message(t('The custom menu %title has been deleted.', $t_args));
538 watchdog('menu', 'Deleted custom menu %title and all its menu links.', $t_args, WATCHDOG_NOTICE);
539 }
540
541 /**
542 * Validates the human and machine-readable names when adding or editing a menu.
543 */
544 function menu_edit_menu_validate($form, &$form_state) {
545 $item = $form_state['values'];
546 if (preg_match('/[^a-z0-9-]/', $item['menu_name'])) {
547 form_set_error('menu_name', t('The menu name may only consist of lowercase letters, numbers, and hyphens.'));
548 }
549 if ($form['#insert']) {
550 if (strlen($item['menu_name']) > MENU_MAX_MENU_NAME_LENGTH_UI) {
551 form_set_error('menu_name', format_plural(MENU_MAX_MENU_NAME_LENGTH_UI, "The menu name can't be longer than 1 character.", "The menu name can't be longer than @count characters."));
552 }
553
554 // We will add 'menu-' to the menu name to help avoid name-space conflicts.
555 $item['menu_name'] = 'menu-' . $item['menu_name'];
556 $custom_exists = db_query_range('SELECT 1 FROM {menu_custom} WHERE menu_name = :menu', 0, 1, array(':menu' => $item['menu_name']))->fetchField();
557 $link_exists = db_query_range("SELECT 1 FROM {menu_links} WHERE menu_name = :menu", 0, 1, array(':menu' => $item['menu_name']))->fetchField();
558 if ($custom_exists || $link_exists) {
559 form_set_error('menu_name', t('The menu already exists.'));
560 }
561 }
562 }
563
564 /**
565 * Submit function for adding or editing a custom menu.
566 */
567 function menu_edit_menu_submit($form, &$form_state) {
568 $menu = $form_state['values'];
569 $path = 'admin/structure/menu/manage/';
570 if ($form['#insert']) {
571 // Add 'menu-' to the menu name to help avoid name-space conflicts.
572 $menu['menu_name'] = 'menu-' . $menu['menu_name'];
573 $link['link_title'] = $menu['title'];
574 $link['link_path'] = $path . $menu['menu_name'];
575 $link['router_path'] = $path . '%';
576 $link['module'] = 'menu';
577 $link['plid'] = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :link AND module = :module", array(
578 ':link' => 'admin/structure/menu',
579 ':module' => 'system'
580 ))
581 ->fetchField();
582
583 menu_link_save($link);
584 menu_save($menu);
585 }
586 else {
587 menu_save($menu);
588 $result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path", array(':path' => $path . $menu['menu_name']), array('fetch' => PDO::FETCH_ASSOC));
589 foreach ($result as $m) {
590 $link = menu_link_load($m['mlid']);
591 $link['link_title'] = $menu['title'];
592 menu_link_save($link);
593 }
594 }
595 $form_state['redirect'] = $path . $menu['menu_name'];
596 }
597
598 /**
599 * Menu callback; Check access and present a confirm form for deleting a menu link.
600 */
601 function menu_item_delete_page($item) {
602 // Links defined via hook_menu may not be deleted. Updated items are an
603 // exception, as they can be broken.
604 if ($item['module'] == 'system' && !$item['updated']) {
605 drupal_access_denied();
606 return;
607 }
608 return drupal_get_form('menu_item_delete_form', $item);
609 }
610
611 /**
612 * Build a confirm form for deletion of a single menu link.
613 */
614 function menu_item_delete_form($form, &$form_state, $item) {
615 $form['#item'] = $item;
616 return confirm_form($form, t('Are you sure you want to delete the custom menu link %item?', array('%item' => $item['link_title'])), 'admin/structure/menu/manage/' . $item['menu_name']);
617 }
618
619 /**
620 * Process menu delete form submissions.
621 */
622 function menu_item_delete_form_submit($form, &$form_state) {
623 $item = $form['#item'];
624 menu_link_delete($item['mlid']);
625 $t_args = array('%title' => $item['link_title']);
626 drupal_set_message(t('The menu link %title has been deleted.', $t_args));
627 watchdog('menu', 'Deleted menu link %title.', $t_args, WATCHDOG_NOTICE);
628 $form_state['redirect'] = 'admin/structure/menu/manage/' . $item['menu_name'];
629 }
630
631 /**
632 * Menu callback; reset a single modified menu link.
633 */
634 function menu_reset_item_confirm($form, &$form_state, $item) {
635 $form['item'] = array('#type' => 'value', '#value' => $item);
636 return confirm_form($form, t('Are you sure you want to reset the link %item to its default values?', array('%item' => $item['link_title'])), 'admin/structure/menu/manage/' . $item['menu_name'], t('Any customizations will be lost. This action cannot be undone.'), t('Reset'));
637 }
638
639 /**
640 * Process menu reset item form submissions.
641 */
642 function menu_reset_item_confirm_submit($form, &$form_state) {
643 $item = $form_state['values']['item'];
644 $new_item = menu_reset_item($item);
645 drupal_set_message(t('The menu link was reset to its default settings.'));
646 $form_state['redirect'] = 'admin/structure/menu/manage/' . $new_item['menu_name'];
647 }
648
649 /**
650 * Menu callback; Build the form presenting menu configuration options.
651 */
652 function menu_configure() {
653 $form['intro'] = array(
654 '#type' => 'item',
655 '#markup' => t('The menu module allows on-the-fly creation of menu links in the content authoring forms. The following option sets the default menu in which a new link will be added.'),
656 );
657
658 $menu_options = menu_get_menus();
659 $form['menu_default_node_menu'] = array(
660 '#type' => 'select',
661 '#title' => t('Default menu for content'),
662 '#default_value' => 'navigation',
663 '#options' => $menu_options,
664 '#description' => t('Choose the menu to be the default in the menu options in the content authoring form.'),
665 );
666
667 $main = variable_get('menu_main_links_source', 'main-menu');
668 $main_options = array_merge($menu_options, array('' => t('No Main links')));
669 $form['menu_main_links_source'] = array(
670 '#type' => 'select',
671 '#title' => t('Source for the Main links'),
672 '#default_value' => 'main-menu',
673 '#options' => $main_options,
674 '#tree' => FALSE,
675 '#description' => t('Select what should be displayed as the Main links (typically at the top of the page).'),
676 );
677
678 $secondary_options = array_merge($menu_options, array('' => t('No Secondary links')));
679 $form['menu_secondary_links_source'] = array(
680 '#type' => 'select',
681 '#title' => t('Source for the Secondary links'),
682 '#default_value' => 'user-menu',
683 '#options' => $secondary_options,
684 '#tree' => FALSE,
685 '#description' => t("Select the source for the Secondary links. An advanced option allows you to use the same source for both Main links (currently %main) and Secondary links: if your source menu has two levels of hierarchy, the top level menu links will appear in the Main links, and the children of the active link will appear in the Secondary links." , array('%main' => $main_options[$main])),
686 );
687
688 return system_settings_form($form, TRUE);
689 }

  ViewVC Help
Powered by ViewVC 1.1.2