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