summaryrefslogtreecommitdiffstats
path: root/modules/menu.module
blob: b03b76edd9c6187399b1d757653b4061d749f4b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
<?php
// $Id$

/**
 * @file
 * Allows administrators to customize the site navigation menu.
 */

/**
 * Implementation of hook_help().
 */
function menu_help($section) {
  switch ($section) {
    case 'admin/help#menu':
      $output = t('<p>Menus are a collection of links (menu items) used to navigate a website. The menu module provides an interface to control and customize the powerful menu system that comes with Drupal.  Menus are primarily displayed as a hierarchical list of links using Drupal\'s highly flexible <a href="%admin-block">blocks</a> feature. Each menu automatically creates a block of the same name. By default, new menu items are placed inside a built-in menu labelled %navigation, but administrators can also create custom menus.</p>
<p>Drupal themes generally provide out-of-the-box support for two menus commonly labelled %primary-links and %secondary-links. These are sets of links which are usually displayed in the header or footer of each page (depending on the currently active theme). Any menu can be designated as the primary or secondary links menu via the <a href="%menu-settings">menu settings page</a>.</p>
Menu administration tabs:
<ul>
  <li>On the administer menu page, administrators can "edit" to change the title, description, parent or weight of a menu item. Under the "operations" column, click on "enable/disable" to toggle a menu item on or off. Only menu items which are enabled are displayed in the corresponding menu block. Note that the default menu items generated by the menu module cannot be deleted, only disabled.</li>
  <li>Use the "add menu" tab to submit a title for a new custom menu. Once submitted, the menu will appear in a list toward the bottom of the administer menu page underneath the main navigation menu. Under the menu name there will be links to edit or delete the menu, and a link to add new items to the menu.</li>
  <li>Use the "add menu item" tab to create new links in either the navigation or a custom menu (such as a primary/secondary links menu). Select the parent item to place the new link within an existing menu structure. For top level menu items, choose the name of the menu in which the link is to be added.</li>
</ul>', array('%navigation' => theme('placeholder', 'Navigation'), '%primary-links' => theme('placeholder', 'primary links'), '%secondary-links' => theme('placeholder', 'secondary links'), '%admin-block' => url('admin/block'), '%menu-settings' => url('admin/settings/menu')));
      $output .= t('<p>You can</p>
<ul>
  <li>administer menus at <a href="%admin-menu">administer &gt;&gt; menus</a>.</li>
  <li>add a menu at <a href="%admin-menu-menu-add">administer &gt;&gt; menus &gt;&gt; add menu</a>.</li>
  <li>add a menu item at <a href="%admin-menu-item-add">administer &gt;&gt; menus &gt;&gt; add menu item</a>.</li>
  <li>modify menu settings (in particular, to specify a menu to use for primary or secondary links) at <a href="%admin-settings-menus">administer &gt;&gt; settings &gt;&gt; menus</a>.</li>
  <li>manage menu blocks at <a href="%admin-block">administer &gt;&gt; blocks</a>.</li>
</ul>
', array('%admin-menu' => url('admin/menu'), '%admin-block' => url('admin/block'), '%admin-menu-menu-add' => url('admin/menu/menu/add'), '%admin-menu-item-add' => url('admin/menu/item/add'), '%admin-settings-menus' => url('admin/settings/menu')));
      $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%menu">Menu page</a>.', array('%menu' => 'http://drupal.org/handbook/modules/menu/')) .'</p>';
      return $output;
    case 'admin/modules#description':
      return t('Allows administrators to customize the site navigation menu.');
    case 'admin/menu':
      return '<p>'. t('Menus are a collection of links (menu items) used to navigate a website. The list(s) below display the currently available menus along with their menu items. Select an operation from the list to manage each menu or menu item.', array('%admin-settings-menus' => url('admin/settings/menu'), '%admin-block'=>url('admin/block'))) .'</p>';
    case 'admin/menu/menu/add':
      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/block'))) .'</p>';
    case 'admin/menu/item/add':
      return '<p>'. t('Enter the title, path, position and the weight for your new menu item.') .'</p>';
  }
}

/**
 * Implementation of hook_menu().
 */
function menu_menu($may_cache) {
  $items = array();

  if ($may_cache) {
    $items[] = array('path' => 'admin/menu',
      'title' => t('menus'),
      'callback' => 'menu_overview',
      'access' => user_access('administer menu'));
    $items[] = array('path' => 'admin/menu/list',
      'title' => t('list'),
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => -10);

    $items[] = array('path' => 'admin/menu/item/add',
      'title' => t('add menu item'),
      'callback' => 'menu_edit_item_form',
      'access' => user_access('administer menu'),
      'type' => MENU_LOCAL_TASK);
    $items[] = array('path' => 'admin/menu/item/edit',
      'title' => t('edit menu item'),
      'callback' => 'menu_edit_item_form',
      'access' => user_access('administer menu'),
      'type' => MENU_CALLBACK);
    $items[] = array('path' => 'admin/menu/item/reset',
      'title' => t('reset menu item'),
      'callback' => 'menu_reset_item',
      'access' => user_access('administer menu'),
      'type' => MENU_CALLBACK);
    $items[] = array('path' => 'admin/menu/item/disable',
      'title' => t('disable menu item'),
      'callback' => 'menu_disable_item',
      'access' => user_access('administer menu'),
      'type' => MENU_CALLBACK);
    $items[] = array('path' => 'admin/menu/item/delete',
      'title' => t('delete menu item'),
      'callback' => 'menu_item_delete_form',
      'access' => user_access('administer menu'),
      'type' => MENU_CALLBACK);

    $items[] = array('path' => 'admin/menu/menu/add',
      'title' => t('add menu'),
      'callback' => 'menu_edit_menu_form',
      'access' => user_access('administer menu'),
      'type' => MENU_LOCAL_TASK);
    $items[] = array('path' => 'admin/menu/menu/edit',
      'title' => t('edit menu'),
      'callback' => 'menu_edit_menu_form',
      'access' => user_access('administer menu'),
      'type' => MENU_CALLBACK);
    $items[] = array('path' => 'admin/menu/menu/delete',
      'title' => t('delete menu'),
      'callback' => 'menu_item_delete_form',
      'access' => user_access('administer menu'),
      'type' => MENU_CALLBACK);

    $items[] = array('path' => 'admin/settings/menu',
      'title' => t('menus'),
      'callback' => 'menu_configure');
  }

  return $items;
}

/**
 * Implementation of hook_block().
 */
function menu_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $blocks = array();
    $root_menus = menu_get_root_menus();
    foreach ($root_menus as $mid => $title) {
      // Default "Navigation" block is handled by user.module.
      if ($mid != 1) {
        $blocks[$mid]['info'] = check_plain($title);
      }
    }
    return $blocks;
  }
  else if ($op == 'view') {
    $item = menu_get_item($delta);
    $data['subject'] = check_plain($item['title']);
    $data['content'] = theme('menu_tree', $delta);
    return $data;
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function menu_nodeapi(&$node, $op) {

  if (user_access('administer menu')) {
    switch ($op) {
      case 'insert':
      case 'update':
        if ($node->menu['delete']) {
          menu_node_form_delete($node);
          menu_rebuild();
        }
        elseif ($node->menu['title']) {
          $node->menu['path'] = ($node->menu['path']) ? $node->menu['path'] : "node/$node->nid";
          menu_edit_item_save($node->menu);
          menu_rebuild();
        }
        break;

      case 'delete':
        menu_node_form_delete($node);
        menu_rebuild();
        break;
    }
  }
}

/**
 * Implementation of hook_perm().
 */
function menu_perm() {
  return array('administer menu');
}

/**
 * Implementation of hook_form_alter().
 * Add menu item fields to the node form.
 */
function menu_form_alter($form_id, &$form) {
  if (user_access('administer menu') && isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
    $edit = isset($_POST['edit']) ? $_POST['edit'] : '';
    $edit['nid'] = $form['nid']['#value'];

    $item = array();
    if ($edit['nid'] > 0) {
      $item = db_fetch_array(db_query("SELECT * FROM {menu} WHERE path = 'node/%d'", $edit['nid']));
      if (is_array($edit['menu'])) {
        $item = !is_array($item) ? $edit['menu'] : (($_POST['op'] == t('Preview')) ? array_merge($item, $edit['menu']) : array_merge($edit['menu'], $item));
      }
    }

    $form['menu'] = array('#type' => 'fieldset',
      '#title' => t('Menu settings'),
      '#collapsible' => TRUE,
      '#collapsed' => empty($item['title']),
      '#tree' => TRUE,
      '#weight' => 30,
    );

    $form['menu']['title'] = array('#type' => 'textfield',
      '#title' => t('Title'),
      '#default_value' => $item['title'],
      '#description' => t('The name to display for this link.'),
    );

    $form['menu']['description'] = array('#type' => 'textfield',
      '#title' => t('Description'),
      '#default_value' => $item['description'],
      '#description' => t('The description displayed when hovering over a menu item.'),
    );

    // Generate a list of possible parents.
    $options = menu_parent_options($item['mid'], variable_get('menu_parent_items', 0));

    $form['menu']['pid'] = array('#type' => 'select',
      '#title' => t('Parent item'),
      '#default_value' => $item['pid'],
      '#options' => $options,
    );

    $form['menu']['path'] = array('#type' => 'hidden',
      '#value' => $item['path'],
    );

    $form['menu']['weight'] = array('#type' => 'weight',
      '#title' => t('Weight'),
      '#default_value' => $item['weight'],
      '#delta' => 10,
      '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
    );

    $form['menu']['mid'] = array('#type' => 'hidden',
      '#value' => $item['mid'] ? $item['mid'] : 0,
    );

    $form['menu']['type'] = array('#type' => 'hidden',
      '#value' => $item['type'] ? $item['type'] : MENU_CUSTOM_ITEM,
    );

    if ($item['mid'] > 0) {
      $form['menu']['delete'] = array('#type' => 'checkbox',
        '#title' => t('Check to delete this menu item.'),
        '#default_value' => $item['delete'],
      );

      $form['menu']['advanced'] = array('#type' => 'item',
        '#value' => t('You may also <a href="%edit">edit the advanced settings</a> for this menu item.', array('%edit' => url("admin/menu/item/edit/{$item['mid']}"))),
      );
    }
  }
}

/**
 * Menu callback; presents menu configuration options.
 */
function menu_configure() {
  $menu = menu_get_menu();
  $root_menus = menu_get_root_menus();

  $primary_options = $root_menus;
  $primary_options[0] = t('No primary links');

  $form['settings_links'] = array('#type' => 'fieldset',
    '#title' => t('Primary and secondary links settings'),
  );

  $form['settings_links']['intro'] = array('#type' => 'item',
    '#value' => t('Primary and secondary links provide a navigational menu system which usually (depending on your theme) appears at the top-right of the browser window. The links displayed can be generated either from a custom list created via the <a href="%menu">menu administration</a> page or from a built-in list of menu items such as the navigation menu links.', array('%menu' => url('admin/menu'))),
  );

  $form['settings_links']['menu_primary_menu'] = array('#type' => 'select',
    '#title' => t('Menu containing primary links'),
    '#default_value' => variable_get('menu_primary_menu', 0),
    '#options' => $primary_options,
  );

  $secondary_options = $root_menus;
  $secondary_options[0] = t('No secondary links');

  $form['settings_links']['menu_secondary_menu'] = array('#type' => 'select',
    '#title' => t('Menu containing secondary links'),
    '#default_value' => variable_get('menu_secondary_menu', 0),
    '#options' => $secondary_options,
    '#description' => t('If you select the same menu as primary links then secondary links will display the appropriate second level of your navigation hierarchy.'),
  );

  $form['settings_authoring'] = array('#type' => 'fieldset',
    '#title' => t('Post authoring form settings'),
  );

  $form['settings_authoring']['intro'] = array('#type' => 'item',
    '#value' => t('The menu module allows on-the-fly creation of menu links in the post authoring forms. The following option limits the menus in which a new link may be added. For e.g. this can be used to force new menu items to be created in the primary links menu or to hide admin menu items.'),
  );

  $authoring_options = $root_menus;
  $authoring_options[0] = t('Show all menus');

  $form['settings_authoring']['menu_parent_items'] = array('#type' => 'select',
    '#title' => t('Restrict parent items to'),
    '#default_value' => variable_get('menu_parent_items', 0),
    '#options' => $authoring_options,
    '#description' => t('Choose the menu to be made available in the post authoring form. Only this menu item and its children will be shown.'),
   );

  return system_settings_form('menu_configure', $form);
}

/**
 * Menu callback; handle the adding/editing of a new menu.
 */
function menu_edit_menu_form($mid = 0) {
  if (arg(3) == 'edit') {
    if (!($item = db_fetch_array(db_query('SELECT * FROM {menu} WHERE mid = %d', $mid)))) {
      drupal_not_found();
      return;
    }
  }
  else {
    $item = array('mid' => 0, 'pid' => 0, 'path' => '', 'weight' => 0, 'type' => MENU_CUSTOM_MENU);
  }
  $form['title'] = array('#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => $item['title'],
    '#description' => t('The name of the menu.'),
    '#required' => TRUE,
  );
  $form['mid'] = array('#type' => 'value', '#value' => $item['mid']);
  $form['pid'] = array('#type' => 'value', '#value' => $item['pid']);
  $form['path'] = array('#type' => 'value', '#value' => $item['path']);
  $form['weight'] = array('#type' => 'value', '#value' => $item['weight']);
  $form['type'] = array('#type' => 'value', '#value' => $item['type']);
  $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));

  // Reuse the submit function of menu_edit_item_form.
  return drupal_get_form('menu_edit_menu_form', $form, 'menu_edit_item_form');
}

/**
 * Present the menu item editing form.
 */
function menu_edit_item_form($mid = 0) {
  if (arg(3) == 'edit') {
    if (!($item = db_fetch_array(db_query('SELECT * FROM {menu} WHERE mid = %d', $mid)))) {
      drupal_not_found();
      return;
    }
  }
  else {
    // This is an add form.
    // The mid argument (if set) will be the default pid to use.
    // Otherwise, we default to the "Navigation" menu (pid #1).
    $default_pid = $mid ? $mid : 1;
    $item = array('mid' => 0, 'pid' => $default_pid, 'weight' => 0, 'type' => MENU_CUSTOM_ITEM);
  }

  $form['title'] = array('#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => $item['title'],
    '#description' => t('The name of the menu item.'),
    '#required' => TRUE,
  );
  $form['description'] = array('#type' => 'textfield',
    '#title' => t('Description'),
    '#default_value' => $item['description'],
    '#description' => t('The description displayed when hovering over a menu item.'),
  );

  if ($item['type'] & MENU_CREATED_BY_ADMIN) {
    $form['path'] = array('#type' => 'textfield',
      '#title' => t('Path'),
      '#default_value' => $item['path'],
      '#description' => t('The path this menu item links to. 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' => theme('placeholder', '<front>'), '%add-node' => theme('placeholder', 'node/add'), '%drupal' => theme('placeholder', 'http://drupal.org'))),
      '#required' => TRUE,
    );
  }
  else {
    $form['_path'] = array('#type' => 'item',
      '#title' => t('Path'),
      '#description' => l($item['path'], $item['path']),
    );
    $form['path'] = array('#type' => 'value', '#value' => $item['path']);
  }

  $expanded = $item['type'] & MENU_EXPANDED ? 1 : 0;
  $form['expanded'] = array('#type' => 'checkbox',
    '#title' => t('Expanded'),
    '#default_value' => $expanded,
    '#description' => t('If selected and this menu item has children, the menu will always appear expanded.'),
  );

  // Generate a list of possible parents (not including this item or descendants).
  $options = menu_parent_options($item['mid']);
  $form['pid'] = array('#type' => 'select',
    '#title' => t('Parent item'),
    '#default_value' => $item['pid'],
    '#options' => $options,
  );
  $form['weight'] = array('#type' => 'weight',
    '#title' => t('Weight'),
    '#default_value' => $item['weight'],
    '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
  );

  // Always enable menu items (but not menus) when editing them.
  if (!($item['type'] & MENU_IS_ROOT)) {
    $item['type'] |= MENU_VISIBLE_IN_TREE | MENU_VISIBLE_IN_BREADCRUMB;
  }

  $form['type'] = array('#type' => 'value', '#value' => $item['type']);
  $form['mid'] = array('#type' => 'value', '#value' => $item['mid']);
  $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));

  return drupal_get_form('menu_edit_item_form', $form);
}

/**
 * Process menu and menu item add/edit form submissions.
 */
function menu_edit_item_form_submit($form_id, $form_values) {
  menu_edit_item_save($form_values);
  return 'admin/menu';
}

/**
 * Menu callback; delete a single custom item.
 */
function menu_item_delete_form($mid) {
  if (!($menu = db_fetch_object(db_query('SELECT type, title FROM {menu} WHERE mid = %d', $mid)))) {
    drupal_not_found();
    return;
  }

  $form['mid'] = array('#type' => 'value', '#value' => $mid);
  $form['type'] = array('#type' => 'value', '#value' => $menu->type);
  $form['title'] = array('#type' => 'value', '#value' => $menu->title);

  if ($menu->type & MENU_IS_ROOT) {
    $message = t('Are you sure you want to delete the menu %item?', array('%item' => theme('placeholder', $menu->title)));
  }
  else {
    $message = t('Are you sure you want to delete the custom menu item %item?', array('%item' => theme('placeholder', $menu->title)));
  }

  return confirm_form('menu_confirm_delete_form', $form, $message, 'admin/menu', t('This action cannot be undone.'), t('Delete'));
}

/**
 * Process menu delete form submissions.
 */
function menu_confirm_delete_form_submit($form_id, $form_values) {
  menu_delete_item($form_values['mid']);

  $t_args = array('%title' => theme('placeholder', $form_values['title']));
  if ($form_values['type'] & MENU_IS_ROOT) {
    drupal_set_message(t('The menu %title has been deleted.', $t_args));
    watchdog('menu', t('Deleted menu %title.', $t_args), WATCHDOG_NOTICE);
  }
  else {
    drupal_set_message(t('The menu item %title has been deleted.', $t_args));
    watchdog('menu', t('Deleted menu item %title.', $t_args), WATCHDOG_NOTICE);
  }

  return 'admin/menu';
}

/**
 * Menu callback; reset a single modified item.
 */
function menu_reset_item($mid) {
  if (isset($mid) && $title = db_result(db_query('SELECT title FROM {menu} WHERE mid = %d', $mid))) {
    $form['mid'] = array('#type' => 'value', '#value' => $mid);
    return confirm_form('menu_reset_item_form', $form, t('Are you sure you want to reset the item %item to its default values?', array('%item' => theme('placeholder', $title))), 'admin/menu', t('Any customizations will be lost. This action cannot be undone.'), t('Reset'));
  }
  else {
    drupal_not_found();
  }
}

/**
 * Process menu reset item form submissions.
 */
function menu_reset_item_form_submit($form_id, $form_values) {
  menu_delete_item($form_values['mid']);
  drupal_set_message(t('The menu item was reset to its default settings.'));

  return 'admin/menu';
}

/**
 * Menu callback; hide a menu item.
 */
function menu_disable_item($mid, $token = NULL) {  
  global $user;  
  $item = menu_get_item($mid);
  $form = array();
  $form['mid'] = array('#type' => 'value', '#value' => $mid);
  $form['item'] = array('#type' => 'value', '#value' => $item);
  return confirm_form('menu_disable_confirm', $form, t('Are you sure you want to disable the menu item %menu-item?', array('%menu-item' => theme('placeholder', $item['title']))), 'admin/menu', ' ', t('Disable'), t('Cancel'));  
}

function menu_disable_confirm_submit($form_id, $form_values) {
  $type = $form_values['item']['type'];
  $type &= ~MENU_VISIBLE_IN_TREE;
  $type &= ~MENU_VISIBLE_IN_BREADCRUMB;
  $type |= MENU_MODIFIED_BY_ADMIN;
  db_query('UPDATE {menu} SET type = %d WHERE mid = %d', $type, $form_values['mid']);
  drupal_set_message(t('The menu item has been disabled.'));     
  drupal_goto('admin/menu');
}

/**
 * Menu callback; present the main menu management page.
 */
function menu_overview() {
  menu_rebuild();

  return menu_overview_tree();
}

/**
 * Save changes to a menu item into the database.
 *
 * @return mid
 */
function menu_edit_item_save($edit) {
  if (isset($edit['expanded'])) {
    if ($edit['expanded']) {
      $edit['type'] |= MENU_EXPANDED;
    }
    else {
      $edit['type'] &= ~MENU_EXPANDED;
    }
  }

  $edit['type'] = $edit['type'] | MENU_MODIFIED_BY_ADMIN;

  $status = menu_save_item($edit);

  $t_args = array('%title' => theme('placeholder', $edit['title']));
  if ($status == SAVED_UPDATED) {
    drupal_set_message(t('The menu item %title has been updated.', $t_args));
  }
  elseif ($status == SAVED_NEW) {
    drupal_set_message(t('The menu item %title has been added.', $t_args));
    watchdog('menu', t('Added menu item %title.', $t_args), WATCHDOG_NOTICE, l(t('view'), 'admin/menu'));
  }
  return $edit['mid'];
}

/**
 * Save a menu item to the database.
 *
 * @param $item
 *   The menu item to be saved. This is passed by reference, so that the newly
 *   generated $item['mid'] can be accessed after an insert takes place.
 *
 * @return $status
 *   The operation that was performed in saving. Either SAVED_NEW (if a new
 *   menu item was created), or SAVED_UPDATED (if an existing menu item was
 *   updated).
 */
function menu_save_item(&$item) {
  $existing_item = NULL;

  // Check that the item already exists in the menu tree, if $item['mid'] is
  // specified.
  if (isset($item['mid'])) {
    $existing_item = menu_get_item($item['mid']);
  }

  if ($item['mid'] && !empty($existing_item)) {
    db_query("UPDATE {menu} SET pid = %d, path = '%s', title = '%s', description = '%s', weight = %d, type = %d WHERE mid = %d", $item['pid'], $item['path'], $item['title'], $item['description'], $item['weight'], $item['type'], $item['mid']);
    return SAVED_UPDATED;
  }
  else {
    $item['mid'] = db_next_id('{menu}_mid');
    // Check explicitly for mid <= 2. If the database was improperly prefixed,
    // this would cause a nasty infinite loop or duplicate mid errors.
    // TODO: have automatic prefixing through an installer to prevent this.
    while ($item['mid'] <= 2) {
      $item['mid'] = db_next_id('{menu}_mid');
    }
    db_query("INSERT INTO {menu} (mid, pid, path, title, description, weight, type) VALUES (%d, %d, '%s', '%s', '%s', %d, %d)", $item['mid'], $item['pid'], $item['path'], $item['title'], $item['description'], $item['weight'], $item['type']);
    return SAVED_NEW;
  }
}

/**
 * Delete a menu item from the database. If $item['mid'] is specified, then
 * this is used to find the existing item; otherwise, $item['path'] is used.
 *
 * @param $item
 *   The menu item to be deleted.
 */
function menu_delete_item($item) {
  if (!is_array($item)) {
    $item = array('mid' => $item);
  }

  if ($item['mid']) {
    db_query('DELETE FROM {menu} WHERE mid = %d', $item['mid']);
  }
  elseif ($item['path']) {
    db_query("DELETE FROM {menu} WHERE path = '%s'", $item['path']);
  }
}

/**
 * Present the menu tree, rendered along with links to edit menu items.
 */
function menu_overview_tree() {
  $menu = menu_get_menu();
  $root_menus = menu_get_root_menus();
  $header = array(t('Menu item'), t('Expanded'), array('data' => t('Operations'), 'colspan' => '3'));
  $output = '';

  foreach ($root_menus as $mid => $title) {
    $operations = array();
    if ($menu['items'][$mid]['type'] & MENU_MODIFIABLE_BY_ADMIN) {
      $operations[] = l(t('edit'), 'admin/menu/menu/edit/'. $mid);
    }
    if ($menu['items'][$mid]['type'] & MENU_CREATED_BY_ADMIN) {
      $operations[] = l(t('delete'), 'admin/menu/menu/delete/'. $mid);
    }
    $operations[] = l(t('add item'), 'admin/menu/item/add/'. $mid);
    $table = theme('item_list', $operations);
    $table .= theme('table', $header, menu_overview_tree_rows($mid));
    $output .= theme('box', check_plain($title), $table);
  }
  return $output;
}

function menu_overview_tree_rows($pid = 0, $depth = 0) {
  $parent_item = menu_get_item($pid);
  $rows = array();

  if (isset($parent_item) && isset($parent_item['children'])) {
    usort($parent_item['children'], '_menu_sort');
    foreach ($parent_item['children'] as $mid) {
      $item = menu_get_item($mid);
      // Populate the title field.
      $title = '';
      if ($pid == 0) {
        // Top-level items are menu names, and don't have an associated path.
        $title .= check_plain($item['title']);
      }
      else {
        $title .= l($item['title'], $item['path']);
      }
      if ($depth > 0) {
        $title = '-&nbsp;'. $title;
      }
      for ($i = 1; $i < $depth; $i++) {
        $title = '&nbsp;&nbsp;'. $title;
      }

      // Populate the operations field.
      $operations = array();
      if (!($item['type'] & MENU_MODIFIABLE_BY_ADMIN)) {
        $operations[] = array('data' => t('locked'), 'colspan' => '3', 'align' => 'center');
      }
      else {
        // Set the edit column.
        if ($item['type'] & (MENU_VISIBLE_IN_TREE | MENU_VISIBLE_IF_HAS_CHILDREN)) {
          $operations[] = array('data' => l(t('edit'), 'admin/menu/item/edit/'. $mid));
        }
        else {
          $operations[] = array('data' => '');
        }

        // Set the disable column.
        if ($item['type'] & (MENU_IS_ROOT | MENU_VISIBLE_IF_HAS_CHILDREN)) {
          // Disabling entire menus is done from block admin page.
          // MENU_VISIBLE_IF_HAS_CHILDREN menus are always enabled so hide this operation.
          $operations[] = array('data' => '');
        }
        else if ($item['type'] & MENU_VISIBLE_IN_TREE) {
          $operations[] = array('data' => l(t('disable'), 'admin/menu/item/disable/'. $mid));
        }
        else {
          $operations[] = array('data' => l(t('enable'), 'admin/menu/item/edit/'. $mid));
        }

        // Set the reset column.
        if ($item['type'] & MENU_CREATED_BY_ADMIN) {
          $operations[] = array('data' => l(t('delete'), 'admin/menu/item/delete/'. $mid));
        }
        else if ($item['type'] & MENU_MODIFIED_BY_ADMIN) {
          $operations[] = array('data' => l(t('reset'), 'admin/menu/item/reset/'. $mid));
        }
        else {
          $operations[] = array('data' => '');
        }
      }

      // Call out disabled items.
      if ($item['type'] & (MENU_VISIBLE_IN_TREE | MENU_VISIBLE_IF_HAS_CHILDREN)) {
        $class = 'menu-enabled';
      }
      else {
        $title .= ' ('. t('disabled') .')';
        $class = 'menu-disabled';
      }

      if ($item['type'] & (MENU_MODIFIABLE_BY_ADMIN | MENU_VISIBLE_IN_TREE)) {
        $row = array(array('data' => $title, 'class' => $class), array('data' => ($item['children'] ? (($item['type'] & MENU_EXPANDED) ? t('Yes') : t('No')) : ''), 'class' => $class));
        foreach ($operations as $operation) {
          $operation['class'] = $class;
          $row[] = $operation;
        }
        $rows[] = $row;
        $rows = array_merge($rows, menu_overview_tree_rows($mid, $depth + 1));
      }
      else {
        // Skip items that are hidden and locked; admins will never care about them.
        $rows = array_merge($rows, menu_overview_tree_rows($mid, $depth));
      }
    }
  }

  return $rows;
}

/**
 * Return a list of menu items that are valid possible parents for the
 * given menu item. The list excludes the given item and its children.
 *
 * @param $mid
 *   The menu item id for which to generate a list of parents.
 *   If $mid == 0 then the complete tree is returned.
 * @param $pid
 *   The menu item id of the menu item at which to start the tree.
 *   If $pid > 0 then this item will be included in the tree.
 * @param $depth
 *   The current depth in the tree - used when recursing to indent the tree.
 * @return
 *   An array of menu titles keyed on the mid.
 */
function menu_parent_options($mid, $pid = 0, $depth = 0) {
  $options = array();

  if (!($parent_item = menu_get_item($pid))) {
    return $options;
  }

  // Exclude $mid and its children from the list unless $mid is 0.
  if ($mid && $mid == $pid) {
    return $options;
  }

  // Add the current $pid to the list.
  if ($pid > 0 && ($parent_item['type'] & (MENU_MODIFIABLE_BY_ADMIN | MENU_IS_ROOT))) {
    $title = ' '. $parent_item['title'];
    for ($i = 0; $i < $depth; $i++) {
      $title = '--'. $title;
    }
    if (!($parent_item['type'] & MENU_VISIBLE_IN_TREE)) {
      $title .= ' ('. t('disabled') .')';
    }
    $options[$pid] = $title;
    $depth ++;
  }

  // Add children of $pid to the list recursively.
  if ($parent_item['children']) {
    usort($parent_item['children'], '_menu_sort');
    foreach ($parent_item['children'] as $child) {
      $options += menu_parent_options($mid, $child, $depth);
    }
  }

  return $options;
}

/**
 * Remove the menu item.
 */
function menu_node_form_delete($node) {
  menu_delete_item(array('path' => 'node/'. $node->nid));
}