Projet

Général

Profil

Paste
Télécharger (27,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / menu / menu.admin.inc @ bfb52287

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