Newer
Older
Gábor Hojtsy
committed
<?php
// $Id$
/**
* @file
* Administrative page callbacks for menu module.
Gábor Hojtsy
committed
*/
/**
* Menu callback which shows an overview page of all the custom menus and their descriptions.
*/
function menu_overview_page() {
Dries Buytaert
committed
$result = db_query("SELECT * FROM {menu_custom} ORDER BY title", array(), array('fetch' => PDO::FETCH_ASSOC));
Dries Buytaert
committed
$header = array(t('Title'), array('data' => t('Operations'), 'colspan' => '3'));
$rows = array();
Dries Buytaert
committed
foreach ($result as $menu) {
Dries Buytaert
committed
$row = array(theme('menu_admin_overview', array('title' => $menu['title'], 'name' => $menu['menu_name'], 'description' => $menu['description'])));
Dries Buytaert
committed
$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'));
Dries Buytaert
committed
$rows[] = $row;
Gábor Hojtsy
committed
}
Dries Buytaert
committed
Dries Buytaert
committed
return theme('table', array('header' => $header, 'rows' => $rows));
Dries Buytaert
committed
}
/**
* Theme the menu title and description for admin page
*/
Dries Buytaert
committed
function theme_menu_admin_overview($variables) {
$output = check_plain($variables['title']);
$output .= '<div class="description">' . filter_xss_admin($variables['description']) . '</div>';
Dries Buytaert
committed
return $output;
Gábor Hojtsy
committed
}
/**
Gábor Hojtsy
committed
* Form for editing an entire menu tree at once.
*
* Shows for one menu the menu links accessible to the current user and
Gábor Hojtsy
committed
* relevant operations.
Gábor Hojtsy
committed
*/
function menu_overview_form($form, &$form_state, $menu) {
Dries Buytaert
committed
global $menu_admin;
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.*
Gábor Hojtsy
committed
FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
Dries Buytaert
committed
WHERE ml.menu_name = :menu
Gábor Hojtsy
committed
ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC";
Dries Buytaert
committed
$result = db_query($sql, array(':menu' => $menu['menu_name']), array('fetch' => PDO::FETCH_ASSOC));
Angie Byron
committed
$links = array();
foreach ($result as $item) {
$links[] = $item;
}
$tree = menu_tree_data($links);
Gábor Hojtsy
committed
$node_links = array();
menu_tree_collect_node_links($tree, $node_links);
Gábor Hojtsy
committed
// We indicate that a menu administrator is running the menu access check.
Dries Buytaert
committed
$menu_admin = TRUE;
Gábor Hojtsy
committed
menu_tree_check_access($tree, $node_links);
Dries Buytaert
committed
$menu_admin = FALSE;
Gábor Hojtsy
committed
$form = array_merge($form, _menu_overview_tree_form($tree));
Gábor Hojtsy
committed
$form['#menu'] = $menu;
Gábor Hojtsy
committed
if (element_children($form)) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
}
else {
Dries Buytaert
committed
$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')));
Gábor Hojtsy
committed
}
return $form;
Gábor Hojtsy
committed
}
/**
Gábor Hojtsy
committed
* Recursive helper function for menu_overview_form().
Gábor Hojtsy
committed
*/
Gábor Hojtsy
committed
function _menu_overview_tree_form($tree) {
$form = &drupal_static(__FUNCTION__, array('#tree' => TRUE));
Gábor Hojtsy
committed
foreach ($tree as $data) {
$title = '';
$item = $data['link'];
// Don't show callbacks; these have $item['hidden'] < 0.
Gábor Hojtsy
committed
if ($item && $item['hidden'] >= 0) {
$mlid = 'mlid:' . $item['mlid'];
Gábor Hojtsy
committed
$form[$mlid]['#item'] = $item;
$form[$mlid]['#attributes'] = $item['hidden'] ? array('class' => array('menu-disabled')) : array('class' => array('menu-enabled'));
$form[$mlid]['title']['#markup'] = l($item['title'], $item['href'], $item['localized_options']) . ($item['hidden'] ? ' (' . t('disabled') . ')' : '');
Gábor Hojtsy
committed
$form[$mlid]['hidden'] = array(
'#type' => 'checkbox',
'#default_value' => !$item['hidden'],
);
$form[$mlid]['expanded'] = array(
'#type' => 'checkbox',
'#default_value' => $item['expanded'],
Gábor Hojtsy
committed
);
$form[$mlid]['weight'] = array(
'#type' => 'weight',
Dries Buytaert
committed
'#delta' => 50,
'#default_value' => isset($form_state[$mlid]['weight']) ? $form_state[$mlid]['weight'] : $item['weight'],
);
$form[$mlid]['mlid'] = array(
'#type' => 'hidden',
'#value' => $item['mlid'],
);
$form[$mlid]['plid'] = array(
'#type' => 'textfield',
'#default_value' => isset($form_state[$mlid]['plid']) ? $form_state[$mlid]['plid'] : $item['plid'],
'#size' => 6,
);
Gábor Hojtsy
committed
// Build a list of operations.
Gábor Hojtsy
committed
$operations = array();
Dries Buytaert
committed
$operations['edit'] = l(t('edit'), 'admin/structure/menu/item/' . $item['mlid'] . '/edit');
Gábor Hojtsy
committed
// Only items created by the menu module can be deleted.
Gábor Hojtsy
committed
if ($item['module'] == 'menu' || $item['updated'] == 1) {
Dries Buytaert
committed
$operations['delete'] = l(t('delete'), 'admin/structure/menu/item/' . $item['mlid'] . '/delete');
Gábor Hojtsy
committed
}
// Set the reset column.
elseif ($item['module'] == 'system' && $item['customized']) {
Dries Buytaert
committed
$operations['reset'] = l(t('reset'), 'admin/structure/menu/item/' . $item['mlid'] . '/reset');
Gábor Hojtsy
committed
}
Gábor Hojtsy
committed
$form[$mlid]['operations'] = array();
foreach ($operations as $op => $value) {
$form[$mlid]['operations'][$op] = array('#markup' => $value);
Gábor Hojtsy
committed
}
}
Gábor Hojtsy
committed
Gábor Hojtsy
committed
if ($data['below']) {
Gábor Hojtsy
committed
_menu_overview_tree_form($data['below']);
}
}
return $form;
}
/**
* Submit handler for the menu overview form.
Dries Buytaert
committed
*
* 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.
Dries Buytaert
committed
*
* @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
Dries Buytaert
committed
$order = array_flip(array_keys($form_state['input'])); // Get the $_POST order.
$form = array_merge($order, $form); // Update our original form with the new order.
$updated_items = array();
$fields = array('expanded', 'weight', 'plid');
Gábor Hojtsy
committed
foreach (element_children($form) as $mlid) {
if (isset($form[$mlid]['#item'])) {
Gábor Hojtsy
committed
$element = $form[$mlid];
// 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.
Gábor Hojtsy
committed
if ($element['hidden']['#value'] != $element['hidden']['#default_value']) {
// Convert to integer rather than boolean due to PDO cast to string.
$element['#item']['hidden'] = $element['hidden']['#value'] ? 0 : 1;
$updated_items[$mlid] = $element['#item'];
Gábor Hojtsy
committed
}
Gábor Hojtsy
committed
}
}
// Save all our changed items to the database.
foreach ($updated_items as $item) {
Gábor Hojtsy
committed
$item['customized'] = 1;
menu_link_save($item);
}
Gábor Hojtsy
committed
}
/**
Gábor Hojtsy
committed
* Theme the menu overview form into a table.
Gábor Hojtsy
committed
*
* @ingroup themeable
Gábor Hojtsy
committed
*/
Dries Buytaert
committed
function theme_menu_overview_form($variables) {
$form = $variables['form'];
drupal_add_tabledrag('menu-overview', 'match', 'parent', 'menu-plid', 'menu-plid', 'menu-mlid', TRUE, MENU_MAX_DEPTH - 1);
drupal_add_tabledrag('menu-overview', 'order', 'sibling', 'menu-weight');
$header = array(
Dries Buytaert
committed
t('Menu link'),
array('data' => t('Enabled'), 'class' => array('checkbox')),
array('data' => t('Show as expanded'), 'class' => array('checkbox')),
t('Weight'),
array('data' => t('Operations'), 'colspan' => '3'),
);
Gábor Hojtsy
committed
$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) {
$operations[] = drupal_render($element['operations'][$op]);
}
while (count($operations) < 2) {
$operations[] = '';
}
// Add special classes to be used for tabledrag.js.
$element['plid']['#attributes']['class'] = array('menu-plid');
$element['mlid']['#attributes']['class'] = array('menu-mlid');
$element['weight']['#attributes']['class'] = array('menu-weight');
// Change the parent field to a hidden. This allows any value but hides the field.
$element['plid']['#type'] = 'hidden';
Gábor Hojtsy
committed
$row = array();
Dries Buytaert
committed
$row[] = theme('indentation', array('size' => $element['#item']['depth'] - 1)) . drupal_render($element['title']);
$row[] = array('data' => drupal_render($element['hidden']), 'class' => array('checkbox'));
$row[] = array('data' => drupal_render($element['expanded']), 'class' => array('checkbox'));
$row[] = drupal_render($element['weight']) . drupal_render($element['plid']) . drupal_render($element['mlid']);
Gábor Hojtsy
committed
$row = array_merge($row, $operations);
Gábor Hojtsy
committed
Gábor Hojtsy
committed
$row = array_merge(array('data' => $row), $element['#attributes']);
$row['class'][] = 'draggable';
Gábor Hojtsy
committed
$rows[] = $row;
}
}
$output = '';
if (empty($rows)) {
$rows[] = array(array('data' => $form['#empty_text'], 'colspan' => '7'));
Gábor Hojtsy
committed
}
Dries Buytaert
committed
$output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'menu-overview')));
$output .= drupal_render_children($form);
Gábor Hojtsy
committed
return $output;
Gábor Hojtsy
committed
}
/**
* Menu callback; Build the menu link editing form.
*/
function menu_edit_item($form, &$form_state, $type, $item, $menu) {
Dries Buytaert
committed
Gábor Hojtsy
committed
$form['menu'] = array(
'#type' => 'fieldset',
'#title' => t('Menu settings'),
'#collapsible' => FALSE,
'#tree' => TRUE,
'#weight' => -2,
'#attributes' => array('class' => array('menu-item-form')),
Gábor Hojtsy
committed
'#item' => $item,
);
if ($type == 'add' || empty($item)) {
// This is an add form, initialize the menu link.
$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);
Gábor Hojtsy
committed
}
Dries Buytaert
committed
$form['menu']['link_title'] = array('#type' => 'textfield',
'#title' => t('Menu link title'),
'#default_value' => $item['link_title'],
'#description' => t('The text to be used for this link in the menu.'),
Dries Buytaert
committed
'#required' => TRUE,
);
Gábor Hojtsy
committed
foreach (array('link_path', 'mlid', 'module', 'has_children', 'options') as $key) {
Gábor Hojtsy
committed
$form['menu'][$key] = array('#type' => 'value', '#value' => $item[$key]);
}
// Any item created or edited via this interface is considered "customized".
$form['menu']['customized'] = array('#type' => 'value', '#value' => 1);
$form['menu']['original_item'] = array('#type' => 'value', '#value' => $item);
Dries Buytaert
committed
Gábor Hojtsy
committed
$path = $item['link_path'];
if (isset($item['options']['query'])) {
$path .= '?' . $item['options']['query'];
Gábor Hojtsy
committed
}
if (isset($item['options']['fragment'])) {
$path .= '#' . $item['options']['fragment'];
Gábor Hojtsy
committed
}
if ($item['module'] == 'menu') {
$form['menu']['link_path'] = array(
'#type' => 'textfield',
'#title' => t('Path'),
'#default_value' => $path,
'#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')),
Gábor Hojtsy
committed
'#required' => TRUE,
);
$form['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#access' => $item['mlid'],
'#submit' => array('menu_item_delete_submit'),
'#weight' => 10,
);
Gábor Hojtsy
committed
}
else {
Gábor Hojtsy
committed
$form['menu']['_path'] = array(
'#type' => 'item',
'#title' => t('Path'),
'#description' => l($item['link_title'], $item['href'], $item['options']),
);
Gábor Hojtsy
committed
}
Gábor Hojtsy
committed
$form['menu']['description'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#default_value' => isset($item['options']['attributes']['title']) ? $item['options']['attributes']['title'] : '',
'#rows' => 1,
'#description' => t('The description displayed when hovering over a menu link.'),
Gábor Hojtsy
committed
);
Gábor Hojtsy
committed
$form['menu']['enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enabled'),
'#default_value' => !$item['hidden'],
Dries Buytaert
committed
'#description' => t('Menu links that are not enabled will not be listed in any menu.'),
Gábor Hojtsy
committed
);
Gábor Hojtsy
committed
$form['menu']['expanded'] = array(
'#type' => 'checkbox',
Dries Buytaert
committed
'#title' => t('Show as expanded'),
Gábor Hojtsy
committed
'#default_value' => $item['expanded'],
Dries Buytaert
committed
'#description' => t('If selected and this menu link has children, the menu will always appear expanded.'),
Gábor Hojtsy
committed
);
// Generate a list of possible parents (not including this link or descendants).
Gábor Hojtsy
committed
$options = menu_parent_options(menu_get_menus(), $item);
$default = $item['menu_name'] . ':' . $item['plid'];
Gábor Hojtsy
committed
if (!isset($options[$default])) {
Dries Buytaert
committed
$default = 'navigation:0';
Gábor Hojtsy
committed
}
$form['menu']['parent'] = array(
'#type' => 'select',
Dries Buytaert
committed
'#title' => t('Parent link'),
Gábor Hojtsy
committed
'#default_value' => $default,
'#options' => $options,
Dries Buytaert
committed
'#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)),
'#attributes' => array('class' => array('menu-title-select')),
Gábor Hojtsy
committed
);
$form['menu']['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight'),
Dries Buytaert
committed
'#delta' => 50,
Gábor Hojtsy
committed
'#default_value' => $item['weight'],
'#description' => t('Optional. In the menu, the heavier links will sink and the lighter links will be positioned nearer the top.'),
Gábor Hojtsy
committed
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
return $form;
Gábor Hojtsy
committed
}
/**
* Validate form values for a menu link being added or edited.
*/
function menu_edit_item_validate($form, &$form_state) {
$item = &$form_state['values']['menu'];
Gábor Hojtsy
committed
$normal_path = drupal_get_normal_path($item['link_path']);
if ($item['link_path'] != $normal_path) {
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)));
Gábor Hojtsy
committed
$item['link_path'] = $normal_path;
}
if (!menu_path_is_external($item['link_path'])) {
$parsed_link = parse_url($item['link_path']);
if (isset($parsed_link['query'])) {
$item['options']['query'] = $parsed_link['query'];
}
if (isset($parsed_link['fragment'])) {
$item['options']['fragment'] = $parsed_link['fragment'];
}
if ($item['link_path'] != $parsed_link['path']) {
$item['link_path'] = $parsed_link['path'];
}
}
Gábor Hojtsy
committed
if (!trim($item['link_path']) || !menu_valid_path($item)) {
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) {
Dries Buytaert
committed
$form_state['redirect'] = 'admin/structure/menu/item/' . $form_state['values']['menu']['mlid'] . '/delete';
Gábor Hojtsy
committed
}
/**
* Process menu and menu item add/edit form submissions.
*/
function menu_edit_item_submit($form, &$form_state) {
Angie Byron
committed
$item = &$form_state['values']['menu'];
Dries Buytaert
committed
Gábor Hojtsy
committed
// The value of "hidden" is the opposite of the value
Dries Buytaert
committed
// supplied by the "enabled" checkbox.
Gábor Hojtsy
committed
$item['hidden'] = (int) !$item['enabled'];
unset($item['enabled']);
Dries Buytaert
committed
Gábor Hojtsy
committed
$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');
}
Dries Buytaert
committed
$form_state['redirect'] = 'admin/structure/menu/manage/' . $item['menu_name'];
Gábor Hojtsy
committed
}
/**
* Menu callback; Build the form that handles the adding/editing of a custom menu.
*/
function menu_edit_menu($form, &$form_state, $type, $menu = array()) {
Dries Buytaert
committed
$system_menus = menu_list_system_menus();
$menu += array('menu_name' => '', 'old_name' => '', 'title' => '', 'description' => '');
if (!empty($menu['menu_name'])) {
$menu['old_name'] = $menu['menu_name'];
}
$form['old_name'] = array('#type' => 'value', '#value' => $menu['old_name']);
// The title of a system menu cannot be altered.
if (isset($system_menus[$menu['menu_name']])) {
$form['title'] = array('#type' => 'value', '#value' => $menu['title']);
Gábor Hojtsy
committed
}
else {
$form['title'] = array(
Gábor Hojtsy
committed
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => $menu['title'],
Gábor Hojtsy
committed
'#required' => TRUE,
'#field_suffix' => ' <small id="edit-title-suffix"> </small>',
Gábor Hojtsy
committed
);
}
// The internal menu name can only be defined during initial menu creation.
if (!empty($menu['old_name'])) {
$form['#insert'] = FALSE;
$form['menu_name'] = array('#type' => 'value', '#value' => $menu['menu_name']);
Dries Buytaert
committed
}
else {
$form['#insert'] = TRUE;
$js_settings = array(
'type' => 'setting',
'data' => array(
'machineReadableValue' => array(
'title' => array(
'text' => t('URL path'),
'target' => 'menu-name',
'searchPattern' => '[^a-z0-9]+',
'replaceToken' => '-',
),
),
),
);
$form['menu_name'] = array(
Dries Buytaert
committed
'#type' => 'textfield',
'#title' => t('Menu name'),
'#maxsize' => MENU_MAX_MENU_NAME_LENGTH_UI,
'#description' => t('This text will be used to construct the URL for the menu. The name must contain only lowercase letters, numbers and hyphens, and must be unique.'),
Dries Buytaert
committed
'#required' => TRUE,
Dries Buytaert
committed
'#attached' => array(
'js' => array(drupal_get_path('module', 'system') . '/system.js', $js_settings),
),
Dries Buytaert
committed
);
}
Gábor Hojtsy
committed
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#default_value' => $menu['description'],
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
// Only custom menus may be deleted.
$form['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#access' => $type == 'edit' && !isset($system_menus[$menu['menu_name']]),
'#submit' => array('menu_custom_delete_submit'),
);
Gábor Hojtsy
committed
return $form;
}
/**
* Submit function for the 'Delete' button on the menu editing form.
*/
function menu_custom_delete_submit($form, &$form_state) {
Dries Buytaert
committed
$form_state['redirect'] = 'admin/structure/menu/manage/' . $form_state['values']['menu_name'] . '/delete';
Gábor Hojtsy
committed
}
/**
* 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.
Dries Buytaert
committed
$system_menus = menu_list_system_menus();
if (isset($system_menus[$menu['menu_name']])) {
Gábor Hojtsy
committed
drupal_access_denied();
return;
}
return drupal_get_form('menu_delete_menu_confirm', $menu);
}
/**
* Build a confirm form for deletion of a custom menu.
*/
function menu_delete_menu_confirm($form, &$form_state, $menu) {
Gábor Hojtsy
committed
$form['#menu'] = $menu;
$caption = '';
Dries Buytaert
committed
$num_links = db_query("SELECT COUNT(*) FROM {menu_links} WHERE menu_name = :menu", array(':menu' => $menu['menu_name']))->fetchField();
Gábor Hojtsy
committed
if ($num_links) {
$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>';
Gábor Hojtsy
committed
}
$caption .= '<p>' . t('This action cannot be undone.') . '</p>';
Dries Buytaert
committed
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'));
Gábor Hojtsy
committed
}
/**
* Delete a custom menu and all links in it.
Gábor Hojtsy
committed
*/
function menu_delete_menu_confirm_submit($form, &$form_state) {
$menu = $form['#menu'];
Dries Buytaert
committed
$form_state['redirect'] = 'admin/structure/menu';
Gábor Hojtsy
committed
// System-defined menus may not be deleted - only menus defined by this module.
Dries Buytaert
committed
$system_menus = menu_list_system_menus();
Dries Buytaert
committed
if (isset($system_menus[$menu['menu_name']]) || !(db_query("SELECT 1 FROM {menu_custom} WHERE menu_name = :menu", array(':menu' => $menu['menu_name']))->fetchField())) {
Gábor Hojtsy
committed
return;
}
// Reset all the menu links defined by the system via hook_menu().
Dries Buytaert
committed
$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));
foreach ($result as $link) {
menu_reset_item($link);
Gábor Hojtsy
committed
}
Gábor Hojtsy
committed
// Delete all links to the overview page for this menu.
Dries Buytaert
committed
$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));
foreach ($result as $link) {
menu_link_delete($link['mlid']);
}
// Delete the custom menu and all its menu links.
menu_delete($menu);
Gábor Hojtsy
committed
$t_args = array('%title' => $menu['title']);
drupal_set_message(t('The custom menu %title has been deleted.', $t_args));
watchdog('menu', 'Deleted custom menu %title and all its menu links.', $t_args, WATCHDOG_NOTICE);
Gábor Hojtsy
committed
}
/**
* Validates the human and machine-readable names when adding or editing a menu.
*/
function menu_edit_menu_validate($form, &$form_state) {
$item = $form_state['values'];
if (preg_match('/[^a-z0-9-]/', $item['menu_name'])) {
Gábor Hojtsy
committed
form_set_error('menu_name', t('The menu name may only consist of lowercase letters, numbers, and hyphens.'));
Gábor Hojtsy
committed
}
if ($form['#insert']) {
if (strlen($item['menu_name']) > MENU_MAX_MENU_NAME_LENGTH_UI) {
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."));
}
Gábor Hojtsy
committed
// We will add 'menu-' to the menu name to help avoid name-space conflicts.
$item['menu_name'] = 'menu-' . $item['menu_name'];
$custom_exists = db_query_range('SELECT 1 FROM {menu_custom} WHERE menu_name = :menu', 0, 1, array(':menu' => $item['menu_name']))->fetchField();
$link_exists = db_query_range("SELECT 1 FROM {menu_links} WHERE menu_name = :menu", 0, 1, array(':menu' => $item['menu_name']))->fetchField();
Dries Buytaert
committed
if ($custom_exists || $link_exists) {
Gábor Hojtsy
committed
form_set_error('menu_name', t('The menu already exists.'));
Gábor Hojtsy
committed
}
}
}
/**
* Submit function for adding or editing a custom menu.
*/
function menu_edit_menu_submit($form, &$form_state) {
$menu = $form_state['values'];
Dries Buytaert
committed
$path = 'admin/structure/menu/manage/';
Gábor Hojtsy
committed
if ($form['#insert']) {
// Add 'menu-' to the menu name to help avoid name-space conflicts.
$menu['menu_name'] = 'menu-' . $menu['menu_name'];
Gábor Hojtsy
committed
$link['link_title'] = $menu['title'];
$link['link_path'] = $path . $menu['menu_name'];
$link['router_path'] = $path . '%';
Gábor Hojtsy
committed
$link['module'] = 'menu';
Dries Buytaert
committed
$link['plid'] = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :link AND module = :module", array(
Dries Buytaert
committed
':link' => 'admin/structure/menu',
Dries Buytaert
committed
':module' => 'system'
))
->fetchField();
Gábor Hojtsy
committed
menu_link_save($link);
menu_save($menu);
Gábor Hojtsy
committed
}
else {
menu_save($menu);
Dries Buytaert
committed
$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) {
Gábor Hojtsy
committed
$link = menu_link_load($m['mlid']);
$link['link_title'] = $menu['title'];
menu_link_save($link);
}
}
$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) {
Gábor Hojtsy
committed
// 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']) {
Gábor Hojtsy
committed
drupal_access_denied();
return;
}
return drupal_get_form('menu_item_delete_form', $item);
}
/**
* Build a confirm form for deletion of a single menu link.
*/
function menu_item_delete_form($form, &$form_state, $item) {
Gábor Hojtsy
committed
$form['#item'] = $item;
Dries Buytaert
committed
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']);
Gábor Hojtsy
committed
}
/**
* 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']);
drupal_set_message(t('The menu link %title has been deleted.', $t_args));
watchdog('menu', 'Deleted menu link %title.', $t_args, WATCHDOG_NOTICE);
Dries Buytaert
committed
$form_state['redirect'] = 'admin/structure/menu/manage/' . $item['menu_name'];
Gábor Hojtsy
committed
}
/**
* Menu callback; reset a single modified menu link.
Gábor Hojtsy
committed
*/
function menu_reset_item_confirm($form, &$form_state, $item) {
Gábor Hojtsy
committed
$form['item'] = array('#type' => 'value', '#value' => $item);
Dries Buytaert
committed
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'));
Gábor Hojtsy
committed
}
/**
* 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);
drupal_set_message(t('The menu link was reset to its default settings.'));
Dries Buytaert
committed
$form_state['redirect'] = 'admin/structure/menu/manage/' . $new_item['menu_name'];
Gábor Hojtsy
committed
}
/**
* Menu callback; Build the form presenting menu configuration options.
*/
function menu_configure() {
$form['intro'] = array(
'#type' => 'item',
'#markup' => t('The menu module allows on-the-fly creation of menu links in the content authoring forms. The following option sets the default menu in which a new link will be added.'),
Gábor Hojtsy
committed
);
$menu_options = menu_get_menus();
Dries Buytaert
committed
$form['menu_default_node_menu'] = array(
'#type' => 'select',
Gábor Hojtsy
committed
'#title' => t('Default menu for content'),
Dries Buytaert
committed
'#default_value' => 'navigation',
Gábor Hojtsy
committed
'#options' => $menu_options,
'#description' => t('Choose the menu to be the default in the menu options in the content authoring form.'),
);
Dries Buytaert
committed
$main = variable_get('menu_main_links_source', 'navigation');
Dries Buytaert
committed
$main_options = array_merge($menu_options, array('' => t('No Main links')));
$form['menu_main_links_source'] = array(
Dries Buytaert
committed
'#type' => 'select',
Dries Buytaert
committed
'#title' => t('Source for the Main links'),
Dries Buytaert
committed
'#default_value' => 'navigation',
'#options' => $main_options,
Dries Buytaert
committed
'#tree' => FALSE,
Dries Buytaert
committed
'#description' => t('Select what should be displayed as the Main links (typically at the top of the page).'),
Dries Buytaert
committed
);
Dries Buytaert
committed
$secondary_options = array_merge($menu_options, array('' => t('No Secondary links')));
$form["menu_secondary_links_source"] = array(
Dries Buytaert
committed
'#type' => 'select',
Dries Buytaert
committed
'#title' => t('Source for the Secondary links'),
Dries Buytaert
committed
'#default_value' => 'user-menu',
Gábor Hojtsy
committed
'#options' => $secondary_options,
'#tree' => FALSE,
Dries Buytaert
committed
'#description' => t("Select the source for the Secondary links. An advanced option allows you to use the same source for both Main links (currently %main) and Secondary links: if your source menu has two levels of hierarchy, the top level menu links will appear in the Main links, and the children of the active link will appear in the Secondary links." , array('%main' => $main_options[$main])),
Gábor Hojtsy
committed
);
Dries Buytaert
committed
return system_settings_form($form, TRUE);