Simpletest Coverage - modules/menu/menu.admin.inc

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

Legend

Missed
lines code that were not excersized during program execution.
Covered
lines code were excersized during program execution.
Comment/non executable
Comment or non-executable line of code.
Dead
lines of code that according to xdebug could not be executed. This is counted as coverage code because in almost all cases it is code that runnable.