menu.admin.inc 27.1 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Administrative page callbacks for menu module.
6 7 8 9 10 11
 */

/**
 * Menu callback which shows an overview page of all the custom menus and their descriptions.
 */
function menu_overview_page() {
12
  $result = db_query("SELECT * FROM {menu_custom} ORDER BY title", array(), array('fetch' => PDO::FETCH_ASSOC));
13 14
  $header = array(t('Title'), array('data' => t('Operations'), 'colspan' => '3'));
  $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 18 19
    $row[] = array('data' => l(t('list links'), 'admin/structure/menu/manage/' . $menu['menu_name']));
    $row[] = array('data' => l(t('edit menu'), 'admin/structure/menu/manage/' . $menu['menu_name'] . '/edit'));
    $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 28 29 30 31 32 33 34
 * Returns HTML for a menu title and description for the menu overview page.
 *
 * @param $variables
 *   An associative array containing:
 *   - title: The menu's title.
 *   - description: The menu's description.
 *
 * @ingroup themeable
35
 */
36 37 38
function theme_menu_admin_overview($variables) {
  $output = check_plain($variables['title']);
  $output .= '<div class="description">' . filter_xss_admin($variables['description']) . '</div>';
39

40
  return $output;
41 42 43
}

/**
44 45
 * Form for editing an entire menu tree at once.
 *
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 59 60 61 62
  $links = array();
  foreach ($result as $item) {
    $links[] = $item;
  }
  $tree = menu_tree_data($links);
63 64
  $node_links = array();
  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 77 78 79 80
      '#type' => 'submit',
      '#value' => t('Save configuration'),
    );
  }
  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 90
 *
 * @param $tree
 *   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 95 96 97
  foreach ($tree as $data) {
    $title = '';
    $item = $data['link'];
    // 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']) . ($item['hidden'] ? ' (' . t('disabled') . ')' : '');
103 104
      $form[$mlid]['hidden'] = array(
        '#type' => 'checkbox',
105 106
        '#title' => t('Enable @title menu link', array('@title' => $item['title'])),
        '#title_display' => 'invisible',
107 108
        '#default_value' => !$item['hidden'],
      );
109 110
      $form[$mlid]['weight'] = array(
        '#type' => 'weight',
111
        '#delta' => 50,
112
        '#default_value' => $item['weight'],
113
        '#title_display' => 'invisible',
114
        '#title' => t('Weight for @title', array('@title' => $item['title'])),
115 116 117 118 119 120
      );
      $form[$mlid]['mlid'] = array(
        '#type' => 'hidden',
        '#value' => $item['mlid'],
      );
      $form[$mlid]['plid'] = array(
121
        '#type' => 'hidden',
122
        '#default_value' => $item['plid'],
123
      );
124
      // Build a list of operations.
125
      $operations = array();
126
      $operations['edit'] = array('#type' => 'link', '#title' => t('edit'), '#href' => 'admin/structure/menu/item/' . $item['mlid'] . '/edit');
127
      // Only items created by the menu module can be deleted.
128
      if ($item['module'] == 'menu' || $item['updated'] == 1) {
129
        $operations['delete'] = array('#type' => 'link', '#title' => t('delete'), '#href' => 'admin/structure/menu/item/' . $item['mlid'] . '/delete');
130 131 132
      }
      // Set the reset column.
      elseif ($item['module'] == 'system' && $item['customized']) {
133
        $operations['reset'] = array('#type' => 'link', '#title' => t('reset'), '#href' => 'admin/structure/menu/item/' . $item['mlid'] . '/reset');
134
      }
135
      $form[$mlid]['operations'] = $operations;
136
    }
137

138
    if ($data['below']) {
139 140 141 142 143 144
      _menu_overview_tree_form($data['below']);
    }
  }
  return $form;
}

145 146
/**
 * Submit handler for the menu overview form.
147
 *
148 149
 * This function takes great care in saving parent items first, then items
 * underneath them. Saving items in the incorrect order can break the menu tree.
150
 *
151 152 153 154 155 156 157 158 159
 * @see menu_overview_form()
 */
function menu_overview_form_submit($form, &$form_state) {
  // When dealing with saving menu items, the order in which these items are
  // saved is critical. If a changed child item is saved before its parent,
  // the child item could be saved with an invalid path past its immediate
  // parent. To prevent this, save items in the form in the same order they
  // are sent by $_POST, ensuring parents are saved first, then their children.
  // See http://drupal.org/node/181126#comment-632270
160
  $order = array_flip(array_keys($form_state['input'])); // Get the $_POST order.
161
  $form = array_intersect_key(array_merge($order, $form), $form); // Update our original form with the new order.
162 163

  $updated_items = array();
164
  $fields = array('weight', 'plid');
165
  foreach (element_children($form) as $mlid) {
166
    if (isset($form[$mlid]['#item'])) {
167
      $element = $form[$mlid];
168 169 170 171 172 173 174 175
      // Update any fields that have changed in this menu item.
      foreach ($fields as $field) {
        if ($element[$field]['#value'] != $element[$field]['#default_value']) {
          $element['#item'][$field] = $element[$field]['#value'];
          $updated_items[$mlid] = $element['#item'];
        }
      }
      // Hidden is a special case, the value needs to be reversed.
176
      if ($element['hidden']['#value'] != $element['hidden']['#default_value']) {
177 178
        // Convert to integer rather than boolean due to PDO cast to string.
        $element['#item']['hidden'] = $element['hidden']['#value'] ? 0 : 1;
179
        $updated_items[$mlid] = $element['#item'];
180
      }
181 182
    }
  }
183 184 185

  // Save all our changed items to the database.
  foreach ($updated_items as $item) {
186
    $item['customized'] = 1;
187 188
    menu_link_save($item);
  }
189
  drupal_set_message(t('Your configuration has been saved.'));
190 191 192
}

/**
193 194 195 196 197
 * Returns HTML for the menu overview form into a table.
 *
 * @param $variables
 *   An associative array containing:
 *   - form: A render element representing the form.
198 199
 *
 * @ingroup themeable
200
 */
201 202 203
function theme_menu_overview_form($variables) {
  $form = $variables['form'];

204
  drupal_add_tabledrag('menu-overview', 'match', 'parent', 'menu-plid', 'menu-plid', 'menu-mlid', TRUE, MENU_MAX_DEPTH - 1);
205 206 207
  drupal_add_tabledrag('menu-overview', 'order', 'sibling', 'menu-weight');

  $header = array(
208
    t('Menu link'),
209
    array('data' => t('Enabled'), 'class' => array('checkbox')),
210 211 212 213
    t('Weight'),
    array('data' => t('Operations'), 'colspan' => '3'),
  );

214 215 216 217 218 219 220
  $rows = array();
  foreach (element_children($form) as $mlid) {
    if (isset($form[$mlid]['hidden'])) {
      $element = &$form[$mlid];
      // Build a list of operations.
      $operations = array();
      foreach (element_children($element['operations']) as $op) {
221
        $operations[] = array('data' => drupal_render($element['operations'][$op]), 'class' => array('menu-operations'));
222 223 224 225 226
      }
      while (count($operations) < 2) {
        $operations[] = '';
      }

227
      // Add special classes to be used for tabledrag.js.
228 229 230
      $element['plid']['#attributes']['class'] = array('menu-plid');
      $element['mlid']['#attributes']['class'] = array('menu-mlid');
      $element['weight']['#attributes']['class'] = array('menu-weight');
231 232 233 234

      // Change the parent field to a hidden. This allows any value but hides the field.
      $element['plid']['#type'] = 'hidden';

235
      $row = array();
236
      $row[] = theme('indentation', array('size' => $element['#item']['depth'] - 1)) . drupal_render($element['title']);
237
      $row[] = array('data' => drupal_render($element['hidden']), 'class' => array('checkbox', 'menu-enabled'));
238
      $row[] = drupal_render($element['weight']) . drupal_render($element['plid']) . drupal_render($element['mlid']);
239
      $row = array_merge($row, $operations);
240

241
      $row = array_merge(array('data' => $row), $element['#attributes']);
242
      $row['class'][] = 'draggable';
243 244 245 246
      $rows[] = $row;
    }
  }
  $output = '';
247 248
  if (empty($rows)) {
    $rows[] = array(array('data' => $form['#empty_text'], 'colspan' => '7'));
249
  }
250
  $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'menu-overview')));
251
  $output .= drupal_render_children($form);
252
  return $output;
253 254 255 256 257
}

/**
 * Menu callback; Build the menu link editing form.
 */
258
function menu_edit_item($form, &$form_state, $type, $item, $menu) {
259 260
  if ($type == 'add' || empty($item)) {
    // This is an add form, initialize the menu link.
261
    $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);
262
  }
263
  $form['actions'] = array('#type' => 'actions');
264 265
  $form['link_title'] = array(
    '#type' => 'textfield',
266 267
    '#title' => t('Menu link title'),
    '#default_value' => $item['link_title'],
268
    '#description' => t('The text to be used for this link in the menu.'),
269 270
    '#required' => TRUE,
  );
271
  foreach (array('link_path', 'mlid', 'module', 'has_children', 'options') as $key) {
272
    $form[$key] = array('#type' => 'value', '#value' => $item[$key]);
273 274
  }
  // Any item created or edited via this interface is considered "customized".
275 276
  $form['customized'] = array('#type' => 'value', '#value' => 1);
  $form['original_item'] = array('#type' => 'value', '#value' => $item);
277

278 279
  $path = $item['link_path'];
  if (isset($item['options']['query'])) {
280
    $path .= '?' . drupal_http_build_query($item['options']['query']);
281 282
  }
  if (isset($item['options']['fragment'])) {
283
    $path .= '#' . $item['options']['fragment'];
284 285
  }
  if ($item['module'] == 'menu') {
286
    $form['link_path'] = array(
287 288
      '#type' => 'textfield',
      '#title' => t('Path'),
289
      '#maxlength' => 255,
290
      '#default_value' => $path,
291
      '#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')),
292 293
      '#required' => TRUE,
    );
294
    $form['actions']['delete'] = array(
295 296 297 298 299 300
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#access' => $item['mlid'],
      '#submit' => array('menu_item_delete_submit'),
      '#weight' => 10,
    );
301 302
  }
  else {
303
    $form['_path'] = array(
304 305 306 307
      '#type' => 'item',
      '#title' => t('Path'),
      '#description' => l($item['link_title'], $item['href'], $item['options']),
    );
308
  }
309
  $form['description'] = array(
310 311 312 313
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => isset($item['options']['attributes']['title']) ? $item['options']['attributes']['title'] : '',
    '#rows' => 1,
314
    '#description' => t('Shown when hovering over the menu link.'),
315
  );
316
  $form['enabled'] = array(
317 318 319
    '#type' => 'checkbox',
    '#title' => t('Enabled'),
    '#default_value' => !$item['hidden'],
320
    '#description' => t('Menu links that are not enabled will not be listed in any menu.'),
321
  );
322
  $form['expanded'] = array(
323
    '#type' => 'checkbox',
324
    '#title' => t('Show as expanded'),
325
    '#default_value' => $item['expanded'],
326
    '#description' => t('If selected and this menu link has children, the menu will always appear expanded.'),
327 328
  );

329
  // Generate a list of possible parents (not including this link or descendants).
330
  $options = menu_parent_options(menu_get_menus(), $item);
331
  $default = $item['menu_name'] . ':' . $item['plid'];
332
  if (!isset($options[$default])) {
333
    $default = 'navigation:0';
334
  }
335
  $form['parent'] = array(
336
    '#type' => 'select',
337
    '#title' => t('Parent link'),
338 339
    '#default_value' => $default,
    '#options' => $options,
340
    '#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)),
341
    '#attributes' => array('class' => array('menu-title-select')),
342
  );
343
  $form['weight'] = array(
344 345
    '#type' => 'weight',
    '#title' => t('Weight'),
346
    '#delta' => 50,
347
    '#default_value' => $item['weight'],
348
    '#description' => t('Optional. In the menu, the heavier links will sink and the lighter links will be positioned nearer the top.'),
349
  );
350
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
351 352

  return $form;
353 354 355 356 357 358
}

/**
 * Validate form values for a menu link being added or edited.
 */
function menu_edit_item_validate($form, &$form_state) {
359
  $item = &$form_state['values'];
360 361
  $normal_path = drupal_get_normal_path($item['link_path']);
  if ($item['link_path'] != $normal_path) {
362
    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)));
363 364
    $item['link_path'] = $normal_path;
  }
365
  if (!url_is_external($item['link_path'])) {
366 367
    $parsed_link = parse_url($item['link_path']);
    if (isset($parsed_link['query'])) {
368
      $item['options']['query'] = drupal_get_query_array($parsed_link['query']);
369
    }
370 371 372 373 374
    else {
      // Use unset() rather than setting to empty string
      // to avoid redundant serialized data being stored.
      unset($item['options']['query']);
    }
375 376 377
    if (isset($parsed_link['fragment'])) {
      $item['options']['fragment'] = $parsed_link['fragment'];
    }
378 379 380
    else {
      unset($item['options']['fragment']);
    }
381
    if (isset($parsed_link['path']) && $item['link_path'] != $parsed_link['path']) {
382 383 384
      $item['link_path'] = $parsed_link['path'];
    }
  }
385
  if (!trim($item['link_path']) || !drupal_valid_path($item['link_path'], TRUE)) {
386 387 388 389 390 391 392 393
    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'])));
  }
}

/**
 * Submit function for the delete button on the menu item editing form.
 */
function menu_item_delete_submit($form, &$form_state) {
394
  $form_state['redirect'] = 'admin/structure/menu/item/' . $form_state['values']['mlid'] . '/delete';
395 396 397 398 399 400
}

/**
 * Process menu and menu item add/edit form submissions.
 */
function menu_edit_item_submit($form, &$form_state) {
401
  $item = &$form_state['values'];
402

403
  // The value of "hidden" is the opposite of the value
404
  // supplied by the "enabled" checkbox.
405 406
  $item['hidden'] = (int) !$item['enabled'];
  unset($item['enabled']);
407

408 409 410 411 412
  $item['options']['attributes']['title'] = $item['description'];
  list($item['menu_name'], $item['plid']) = explode(':', $item['parent']);
  if (!menu_link_save($item)) {
    drupal_set_message(t('There was an error saving the menu link.'), 'error');
  }
413 414 415
  else {
    drupal_set_message(t('Your configuration has been saved.'));
  }
416
  $form_state['redirect'] = 'admin/structure/menu/manage/' . $item['menu_name'];
417 418 419 420 421
}

/**
 * Menu callback; Build the form that handles the adding/editing of a custom menu.
 */
422
function menu_edit_menu($form, &$form_state, $type, $menu = array()) {
423
  $system_menus = menu_list_system_menus();
424 425 426 427 428 429 430 431 432 433 434 435 436
  $menu += array(
    'menu_name' => '',
    'old_name' => !empty($menu['menu_name']) ? $menu['menu_name'] : '',
    'title' => '',
    'description' => '',
  );
  // Allow menu_edit_menu_submit() and other form submit handlers to determine
  // whether the menu already exists.
  $form['#insert'] = empty($menu['old_name']);
  $form['old_name'] = array(
    '#type' => 'value',
    '#value' => $menu['old_name'],
  );
437

438 439 440 441 442 443 444 445
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => $menu['title'],
    '#required' => TRUE,
    // The title of a system menu cannot be altered.
    '#access' => !isset($system_menus[$menu['menu_name']]),
  );
446

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
  $form['menu_name'] = array(
    '#type' => 'machine_name',
    '#title' => t('Menu name'),
    '#default_value' => $menu['menu_name'],
    '#maxlength' => MENU_MAX_MENU_NAME_LENGTH_UI,
    '#description' => t('A unique name to construct the URL for the menu. It must only contain lowercase letters, numbers and hyphens.'),
    '#machine_name' => array(
      'exists' => 'menu_edit_menu_name_exists',
      'source' => array('title'),
      'label' => t('URL path'),
      'replace_pattern' => '[^a-z0-9-]+',
      'replace' => '-',
    ),
    // A menu's machine name cannot be changed.
    '#disabled' => !empty($menu['old_name']) || isset($system_menus[$menu['menu_name']]),
  );
463

464 465 466 467 468
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => $menu['description'],
  );
469 470
  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
471 472 473
    '#type' => 'submit',
    '#value' => t('Save'),
  );
474
  // Only custom menus may be deleted.
475
  $form['actions']['delete'] = array(
476 477 478 479 480
    '#type' => 'submit',
    '#value' => t('Delete'),
    '#access' => $type == 'edit' && !isset($system_menus[$menu['menu_name']]),
    '#submit' => array('menu_custom_delete_submit'),
  );
481 482 483 484 485 486 487 488

  return $form;
}

/**
 * Submit function for the 'Delete' button on the menu editing form.
 */
function menu_custom_delete_submit($form, &$form_state) {
489
  $form_state['redirect'] = 'admin/structure/menu/manage/' . $form_state['values']['menu_name'] . '/delete';
490 491 492 493 494 495 496
}

/**
 * Menu callback; check access and get a confirm form for deletion of a custom menu.
 */
function menu_delete_menu_page($menu) {
  // System-defined menus may not be deleted.
497 498
  $system_menus = menu_list_system_menus();
  if (isset($system_menus[$menu['menu_name']])) {
499 500 501 502 503 504 505 506 507
    drupal_access_denied();
    return;
  }
  return drupal_get_form('menu_delete_menu_confirm', $menu);
}

/**
 * Build a confirm form for deletion of a custom menu.
 */
508
function menu_delete_menu_confirm($form, &$form_state, $menu) {
509 510
  $form['#menu'] = $menu;
  $caption = '';
511
  $num_links = db_query("SELECT COUNT(*) FROM {menu_links} WHERE menu_name = :menu", array(':menu' => $menu['menu_name']))->fetchField();
512
  if ($num_links) {
513
    $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>';
514
  }
515
  $caption .= '<p>' . t('This action cannot be undone.') . '</p>';
516
  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'));
517 518 519
}

/**
520
 * Delete a custom menu and all links in it.
521 522 523
 */
function menu_delete_menu_confirm_submit($form, &$form_state) {
  $menu = $form['#menu'];
524
  $form_state['redirect'] = 'admin/structure/menu';
525

526
  // System-defined menus may not be deleted - only menus defined by this module.
527
  $system_menus = menu_list_system_menus();
528
  if (isset($system_menus[$menu['menu_name']])  || !(db_query("SELECT 1 FROM {menu_custom} WHERE menu_name = :menu", array(':menu' => $menu['menu_name']))->fetchField())) {
529 530
    return;
  }
531 532

  // Reset all the menu links defined by the system via hook_menu().
533
  $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));
534 535
  foreach ($result as $link) {
    menu_reset_item($link);
536
  }
537

538
  // Delete all links to the overview page for this menu.
539
  $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));
540 541 542 543 544 545 546
  foreach ($result as $link) {
    menu_link_delete($link['mlid']);
  }

  // Delete the custom menu and all its menu links.
  menu_delete($menu);

547 548
  $t_args = array('%title' => $menu['title']);
  drupal_set_message(t('The custom menu %title has been deleted.', $t_args));
549
  watchdog('menu', 'Deleted custom menu %title and all its menu links.', $t_args, WATCHDOG_NOTICE);
550 551 552
}

/**
553 554 555 556
 * Returns whether a menu name already exists.
 *
 * @see menu_edit_menu()
 * @see form_validate_machine_name()
557
 */
558 559 560 561 562
function menu_edit_menu_name_exists($value) {
  // 'menu-' is added to the menu name to avoid name-space conflicts.
  $value = 'menu-' . $value;
  $custom_exists = db_query_range('SELECT 1 FROM {menu_custom} WHERE menu_name = :menu', 0, 1, array(':menu' => $value))->fetchField();
  $link_exists = db_query_range("SELECT 1 FROM {menu_links} WHERE menu_name = :menu", 0, 1, array(':menu' => $value))->fetchField();
563

564
  return $custom_exists || $link_exists;
565 566 567 568 569 570 571
}

/**
 * Submit function for adding or editing a custom menu.
 */
function menu_edit_menu_submit($form, &$form_state) {
  $menu = $form_state['values'];
572
  $path = 'admin/structure/menu/manage/';
573 574
  if ($form['#insert']) {
    // Add 'menu-' to the menu name to help avoid name-space conflicts.
575
    $menu['menu_name'] = 'menu-' . $menu['menu_name'];
576 577
    $link['link_title'] = $menu['title'];
    $link['link_path'] = $path . $menu['menu_name'];
578
    $link['router_path'] = $path . '%';
579
    $link['module'] = 'menu';
580
    $link['plid'] = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :link AND module = :module", array(
581
      ':link' => 'admin/structure/menu',
582 583 584 585
      ':module' => 'system'
    ))
    ->fetchField();

586
    menu_link_save($link);
587
    menu_save($menu);
588 589
  }
  else {
590
    menu_save($menu);
591 592
    $result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path", array(':path' => $path . $menu['menu_name']), array('fetch' => PDO::FETCH_ASSOC));
    foreach ($result as $m) {
593 594 595 596 597
      $link = menu_link_load($m['mlid']);
      $link['link_title'] = $menu['title'];
      menu_link_save($link);
    }
  }
598
  drupal_set_message(t('Your configuration has been saved.'));
599 600 601 602 603 604 605
  $form_state['redirect'] = $path . $menu['menu_name'];
}

/**
 * Menu callback; Check access and present a confirm form for deleting a menu link.
 */
function menu_item_delete_page($item) {
606 607 608
  // Links defined via hook_menu may not be deleted. Updated items are an
  // exception, as they can be broken.
  if ($item['module'] == 'system' && !$item['updated']) {
609 610 611 612 613 614 615 616 617
    drupal_access_denied();
    return;
  }
  return drupal_get_form('menu_item_delete_form', $item);
}

/**
 * Build a confirm form for deletion of a single menu link.
 */
618
function menu_item_delete_form($form, &$form_state, $item) {
619
  $form['#item'] = $item;
620
  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']);
621 622 623 624 625 626 627 628 629
}

/**
 * Process menu delete form submissions.
 */
function menu_item_delete_form_submit($form, &$form_state) {
  $item = $form['#item'];
  menu_link_delete($item['mlid']);
  $t_args = array('%title' => $item['link_title']);
630
  drupal_set_message(t('The menu link %title has been deleted.', $t_args));
631
  watchdog('menu', 'Deleted menu link %title.', $t_args, WATCHDOG_NOTICE);
632
  $form_state['redirect'] = 'admin/structure/menu/manage/' . $item['menu_name'];
633 634 635
}

/**
636
 * Menu callback; reset a single modified menu link.
637
 */
638
function menu_reset_item_confirm($form, &$form_state, $item) {
639
  $form['item'] = array('#type' => 'value', '#value' => $item);
640
  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'));
641 642 643 644 645 646 647 648
}

/**
 * Process menu reset item form submissions.
 */
function menu_reset_item_confirm_submit($form, &$form_state) {
  $item = $form_state['values']['item'];
  $new_item = menu_reset_item($item);
649
  drupal_set_message(t('The menu link was reset to its default settings.'));
650
  $form_state['redirect'] = 'admin/structure/menu/manage/' . $new_item['menu_name'];
651 652 653 654 655 656 657 658
}

/**
 * Menu callback; Build the form presenting menu configuration options.
 */
function menu_configure() {
  $form['intro'] = array(
    '#type' => 'item',
659
    '#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'))),
660 661 662 663
  );

  $menu_options = menu_get_menus();

664
  $main = variable_get('menu_main_links_source', 'main-menu');
665
  $form['menu_main_links_source'] = array(
666
    '#type' => 'select',
667
    '#title' => t('Source for the Main links'),
668
    '#default_value' => variable_get('menu_main_links_source', 'main-menu'),
669 670
    '#empty_option' => t('No Main links'),
    '#options' => $menu_options,
671
    '#tree' => FALSE,
672
    '#description' => t('Select what should be displayed as the Main links (typically at the top of the page).'),
673 674
  );

675
  $form['menu_secondary_links_source'] = array(
676
    '#type' => 'select',
677
    '#title' => t('Source for the Secondary links'),
678
    '#default_value' => variable_get('menu_secondary_links_source', 'user-menu'),
679 680
    '#empty_option' => t('No Secondary links'),
    '#options' => $menu_options,
681
    '#tree' => FALSE,
682
    '#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] : 'none')),
683 684
  );

685
  return system_settings_form($form);
686
}