Code coverage for /20080809/modules/menu/menu.module

Line #Times calledCode
1
<?php
2
// $Id: menu.module,v 1.166 2008/07/24 16:25:18 dries Exp $
3
4
/**
5
 * @file
6
 * Allows administrators to customize the site navigation menu.
7
 */
8
9
/**
10
 * Maximum length of menu name as entered by the user. Database length is
32
11
 * and we add a menu- prefix.
12
 */
132027
define('MENU_MAX_MENU_NAME_LENGTH_UI', 27);
14
15
/**
16
 * Implementation of hook_help().
17
 */
182027
function menu_help($path, $arg) {
19
  switch ($path) {
201491
    case 'admin/help#menu':
2138
      $output = '<p>' . t("The menu module provides an interface to control
and customize Drupal's powerful menu system. Menus are a hierarchical
collection of links, or menu items, used to navigate a website, and are
positioned and displayed using Drupal's flexible block system. By default,
three menus are created during installation: <em>Navigation</em>, <em>Main
menu</em>, and <em>Secondary menu</em>. The <em>Navigation</em> menu
contains most links necessary for working with and navigating your site,
and is often displayed in either the left or right sidebar. Most Drupal
themes also provide support for the <em>Main menu</em> and <em>Secondary
menu</em>, by displaying them in either the header or footer of each page.
By default, the <em>Main menu</em> and <em>Secondary menu</em> contain no
menu items but may be configured to contain custom menu items specific to
your site.") . '</p>';
2238
      $output .= '<p>' . t('The <a href="@menu">menus page</a> displays all
menus currently available on your site. Select a menu from this list to add
or edit a menu item, or to rearrange items within the menu. Create new
menus using the <a href="@add-menu">add menu page</a> (the block containing
a new menu must also be enabled on the <a href="@blocks">blocks
administration page</a>).', array('@menu' => url('admin/build/menu'),
'@add-menu' => url('admin/build/menu/add'), '@blocks' =>
url('admin/build/block'))) . '</p>';
2338
      $output .= '<p>' . t('For more information, see the online handbook
entry for <a href="@menu">Menu module</a>.', array('@menu' =>
'http://drupal.org/handbook/modules/menu/')) . '</p>';
2438
      return $output;
251489
    case 'admin/build/menu':
262
      return '<p>' . t('Menus are a collection of links (menu items) used
to navigate a website. The menus currently available on your site are
displayed below. Select a menu from this list to manage its menu items.') .
'</p>';
271487
    case 'admin/build/menu/add':
282
      return '<p>' . t('Enter the name for your new menu. Remember to
enable the newly created block in the <a href="@blocks">blocks
administration page</a>.', array('@blocks' => url('admin/build/block'))) .
'</p>';
291485
    case 'admin/build/menu-customize/%':
3030
      return '<p>' . t('To rearrange menu items, grab a drag-and-drop
handle under the <em>Menu item</em> column and drag the items (or group of
items) to a new location in the list. (Grab a handle by clicking and
holding the mouse while hovering over a handle icon.) Remember that your
changes will not be saved until you click the <em>Save configuration</em>
button at the bottom of the page.') . '</p>';
311485
    case 'admin/build/menu/item/add':
320
      return '<p>' . t('Enter the title and path for your new menu item.')
. '</p>';
330
  }
341485
}
35
36
/**
37
 * Implementation of hook_perm().
38
 */
392027
function menu_perm() {
40
  return array(
4187
    'administer menu' => t('Manage menus and menu items.'),
4287
  );
430
}
44
45
/**
46
 * Implementation of hook_menu().
47
 */
482027
function menu_menu() {
4985
  $items['admin/build/menu'] = array(
5085
    'title' => 'Menus',
5185
    'description' => "Control your site's Navigation menu, Main menu and
Secondary menu. as well as rename and reorganize menu items.",
5285
    'page callback' => 'menu_overview_page',
5385
    'access callback' => 'user_access',
5485
    'access arguments' => array('administer menu'),
55
  );
5685
  $items['admin/build/menu/list'] = array(
5785
    'title' => 'List menus',
5885
    'type' => MENU_DEFAULT_LOCAL_TASK,
5985
    'weight' => -10,
60
  );
6185
  $items['admin/build/menu/add'] = array(
6285
    'title' => 'Add menu',
6385
    'page callback' => 'drupal_get_form',
6485
    'page arguments' => array('menu_edit_menu', 'add'),
6585
    'access arguments' => array('administer menu'),
6685
    'type' => MENU_LOCAL_TASK,
67
  );
6885
  $items['admin/build/menu/settings'] = array(
6985
    'title' => 'Settings',
7085
    'page callback' => 'drupal_get_form',
7185
    'page arguments' => array('menu_configure'),
7285
    'access arguments' => array('administer menu'),
7385
    'type' => MENU_LOCAL_TASK,
7485
    'weight' => 5,
75
  );
7685
  $items['admin/build/menu-customize/%menu'] = array(
7785
    'title' => 'Customize menu',
7885
    'page callback' => 'drupal_get_form',
7985
    'page arguments' => array('menu_overview_form', 3),
8085
    'title callback' => 'menu_overview_title',
8185
    'title arguments' => array(3),
8285
    'access arguments' => array('administer menu'),
8385
    'type' => MENU_CALLBACK,
84
  );
8585
  $items['admin/build/menu-customize/%menu/list'] = array(
8685
    'title' => 'List items',
8785
    'weight' => -10,
8885
    'type' => MENU_DEFAULT_LOCAL_TASK,
89
  );
9085
  $items['admin/build/menu-customize/%menu/add'] = array(
9185
    'title' => 'Add item',
9285
    'page callback' => 'drupal_get_form',
9385
    'page arguments' => array('menu_edit_item', 'add', NULL, 3),
9485
    'access arguments' => array('administer menu'),
9585
    'type' => MENU_LOCAL_TASK,
96
  );
9785
  $items['admin/build/menu-customize/%menu/edit'] = array(
9885
    'title' => 'Edit menu',
9985
    'page callback' => 'drupal_get_form',
10085
    'page arguments' => array('menu_edit_menu', 'edit', 3),
10185
    'access arguments' => array('administer menu'),
10285
    'type' => MENU_LOCAL_TASK,
103
  );
10485
  $items['admin/build/menu-customize/%menu/delete'] = array(
10585
    'title' => 'Delete menu',
10685
    'page callback' => 'menu_delete_menu_page',
10785
    'page arguments' => array(3),
10885
    'access arguments' => array('administer menu'),
10985
    'type' => MENU_CALLBACK,
110
  );
11185
  $items['admin/build/menu/item/%menu_link/edit'] = array(
11285
    'title' => 'Edit menu item',
11385
    'page callback' => 'drupal_get_form',
11485
    'page arguments' => array('menu_edit_item', 'edit', 4, NULL),
11585
    'access arguments' => array('administer menu'),
11685
    'type' => MENU_CALLBACK,
117
  );
11885
  $items['admin/build/menu/item/%menu_link/reset'] = array(
11985
    'title' => 'Reset menu item',
12085
    'page callback' => 'drupal_get_form',
12185
    'page arguments' => array('menu_reset_item_confirm', 4),
12285
    'access arguments' => array('administer menu'),
12385
    'type' => MENU_CALLBACK,
124
  );
12585
  $items['admin/build/menu/item/%menu_link/delete'] = array(
12685
    'title' => 'Delete menu item',
12785
    'page callback' => 'menu_item_delete_page',
12885
    'page arguments' => array(4),
12985
    'access arguments' => array('administer menu'),
13085
    'type' => MENU_CALLBACK,
131
  );
13285
  return $items;
1330
}
134
135
/**
136
 * Implemenation of hook_theme().
137
 */
1382027
function menu_theme() {
139
  return array(
140
    'menu_overview_form' => array(
14191
      'file' => 'menu.admin.inc',
14291
      'arguments' => array('form' => NULL),
14391
    ),
14491
  );
1450
}
146
147
/**
148
 * Implementation of hook_enable().
149
 *
150
 *  Add a link for each custom menu.
151
 */
1522027
function menu_enable() {
15362
  menu_rebuild();
15462
  $link = db_fetch_array(db_query("SELECT mlid AS plid, menu_name from
{menu_links} WHERE link_path = 'admin/build/menu' AND module =
'system'"));
15562
  $link['router_path'] = 'admin/build/menu-customize/%';
15662
  $link['module'] = 'menu';
15762
  $result = db_query("SELECT * FROM {menu_custom}");
15862
  while ($menu = db_fetch_array($result)) {
15962
    $link['mlid'] = 0;
16062
    $link['link_title'] = $menu['title'];
16162
    $link['link_path'] = 'admin/build/menu-customize/' .
$menu['menu_name'];
16262
    if (!db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_path
= '%s' AND plid = %d", $link['link_path'], $link['plid']))) {
16362
      menu_link_save($link);
16462
    }
16562
  }
16662
  menu_cache_clear_all();
16762
}
168
169
/**
170
 * Title callback for the menu overview page and links.
171
 */
1722027
function menu_overview_title($menu) {
17347
  return $menu['title'];
1740
}
175
176
/**
177
 * Load the data for a single custom menu.
178
 */
1792027
function menu_load($menu_name) {
18055
  return db_fetch_array(db_query("SELECT * FROM {menu_custom} WHERE
menu_name = '%s'", $menu_name));
1810
}
182
183
/**
184
 * Return a list of menu items that are valid possible parents for the
given menu item.
185
 *
186
 * @param $menus
187
 *   An array of menu names and titles, such as from menu_get_menus().
188
 * @param $item
189
 *   The menu item for which to generate a list of parents.
190
 *   If $item['mlid'] == 0 then the complete tree is returned.
191
 * @return
192
 *   An array of menu link titles keyed on the a string containing the menu
name
193
 *   and mlid. The list excludes the given item and its children.
194
 */
1952027
function menu_parent_options($menus, $item) {
196
  // The menu_links table can be practically any size and we need a way to
197
  // allow contrib modules to provide more scalable pattern choosers.
198
  // hook_form_alter is too late in itself because all the possible parents
are
199
  // retrieved here, unless menu_override_parent_selector is set to TRUE.
200129
  if (variable_get('menu_override_parent_selector', FALSE)) {
2010
    return array();
2020
  }
203
  // If the item has children, there is an added limit to the depth of
valid parents.
204129
  if (isset($item['parent_depth_limit'])) {
20583
    $limit = $item['parent_depth_limit'];
20683
  }
207
  else {
20846
    $limit = _menu_parent_depth_limit($item);
209
  }
210
211129
  foreach ($menus as $menu_name => $title) {
212129
    $tree = menu_tree_all_data($menu_name, NULL);
213129
    $options[$menu_name . ':0'] = '<' . $title . '>';
214129
    _menu_parents_recurse($tree, $menu_name, '--', $options, $item['mlid'],
$limit);
215129
  }
216129
  return $options;
2170
}
218
219
/**
220
 * Recursive helper function for menu_parent_options().
221
 */
2222027
function _menu_parents_recurse($tree, $menu_name, $indent, &$options,
$exclude, $depth_limit) {
223129
  foreach ($tree as $data) {
224129
    if ($data['link']['depth'] > $depth_limit) {
225
      // Don't iterate through any links on this level.
2260
      break;
2270
    }
228129
    if ($data['link']['mlid'] != $exclude && $data['link']['hidden'] >= 0)
{
229129
      $title = $indent . ' ' . truncate_utf8($data['link']['title'], 30,
TRUE, FALSE);
230129
      if ($data['link']['hidden']) {
231129
        $title .= ' (' . t('disabled') . ')';
232129
      }
233129
      $options[$menu_name . ':' . $data['link']['mlid']] = $title;
234129
      if ($data['below']) {
235122
        _menu_parents_recurse($data['below'], $menu_name, $indent . '--',
$options, $exclude, $depth_limit);
236122
      }
237129
    }
238129
  }
239129
}
240
241
/**
242
 * Reset a system-defined menu item.
243
 */
2442027
function menu_reset_item($item) {
2451
  $router = menu_router_build();
2461
  $new_item = _menu_link_build($router[$item['router_path']]);
2471
  foreach (array('mlid', 'has_children') as $key) {
2481
    $new_item[$key] = $item[$key];
2491
  }
2501
  menu_link_save($new_item);
2511
  return $new_item;
2520
}
253
254
/**
255
 * Implementation of hook_block().
256
 */
2572027
function menu_block($op = 'list', $delta = '') {
25896
  $menus = menu_get_menus();
259
  // The Navigation menu is handled by the user module.
26096
  unset($menus['navigation']);
26196
  if ($op == 'list') {
26228
    $blocks = array();
26328
    foreach ($menus as $name => $title) {
264
      // Default "Navigation" block is handled by user.module.
26528
      $blocks[$name]['info'] = check_plain($title);
266
      // Menu blocks can't be cached because each menu item can have
267
      // a custom access callback. menu.inc manages its own caching.
26828
      $blocks[$name]['cache'] = BLOCK_NO_CACHE;
26928
    }
27028
    return $blocks;
2710
  }
27269
  else if ($op == 'view') {
27369
    $data['subject'] = check_plain($menus[$delta]);
27469
    $data['content'] = menu_tree($delta);
27569
    return $data;
2760
  }
2770
}
278
279
/**
280
 * Implementation of hook_nodeapi().
281
 */
2822027
function menu_nodeapi(&$node, $op) {
283
  switch ($op) {
284496
    case 'insert':
285496
    case 'update':
28679
      if (isset($node->menu)) {
28764
        $item = $node->menu;
28864
        if (!empty($item['delete'])) {
2890
          menu_link_delete($item['mlid']);
2900
        }
29164
        elseif (trim($item['link_title'])) {
2920
          $item['link_title'] = trim($item['link_title']);
2930
          $item['link_path'] = "node/$node->nid";
2940
          if (!$item['customized']) {
2950
            $item['options']['attributes']['title'] = trim($node->title);
2960
          }
2970
          if (!menu_link_save($item)) {
2980
            drupal_set_message(t('There was an error saving the menu
link.'), 'error');
2990
          }
3000
        }
30164
      }
30279
      break;
303496
    case 'delete':
304
      // Delete all menu module links that point to this node.
30513
      $result = db_query("SELECT mlid FROM {menu_links} WHERE link_path =
'node/%d' AND module = 'menu'", $node->nid);
30613
      while ($m = db_fetch_array($result)) {
3070
        menu_link_delete($m['mlid']);
3080
      }
30913
      break;
310496
    case 'prepare':
31181
      if (empty($node->menu)) {
312
        // Prepare the node for the edit form so that $node->menu always
exists.
31377
        $menu_name = variable_get('menu_default_node_menu', 'main-menu');
31477
        $item = array();
31577
        if (isset($node->nid)) {
316
          // Give priority to the default menu
31736
          $mlid = db_result(db_query_range("SELECT mlid FROM {menu_links}
WHERE link_path = 'node/%d' AND menu_name = '%s' AND module = 'menu' ORDER
BY mlid ASC", $node->nid, $menu_name, 0, 1));
318
          // Check all menus if a link does not exist in the default menu.
31936
          if (!$mlid) {
32036
            $mlid = db_result(db_query_range("SELECT mlid FROM {menu_links}
WHERE link_path = 'node/%d' AND module = 'menu' ORDER BY mlid ASC",
$node->nid, 0, 1));
32136
          }
32236
          if ($mlid) {
3230
            $item = menu_link_load($mlid);
3240
          }
32536
        }
326
        // Set default values.
32777
        $node->menu = $item + array('link_title' => '', 'mlid' => 0, 'plid'
=> 0, 'menu_name' => $menu_name, 'weight' => 0, 'options' => array(),
'module' => 'menu', 'expanded' => 0, 'hidden' => 0, 'has_children' => 0,
'customized' => 0);
32877
      }
329
      // Find the depth limit for the parent select.
33081
      if (!isset($node->menu['parent_depth_limit'])) {
33177
        $node->menu['parent_depth_limit'] =
_menu_parent_depth_limit($node->menu);
33277
      }
33381
      break;
3340
  }
335496
}
336
337
/**
338
 * Find the depth limit for items in the parent select.
339
 */
3402027
function _menu_parent_depth_limit($item) {
341123
  return MENU_MAX_DEPTH - 1 - (($item['mlid'] && $item['has_children']) ?
menu_link_children_relative_depth($item) : 0);
3420
}
343
344
/**
345
 * Implementation of hook_form_alter(). Adds menu item fields to the node
form.
346
 */
3472027
function menu_form_alter(&$form, $form_state, $form_id) {
3481338
  if (isset($form['#node']) && $form['#node']->type . '_node_form' ==
$form_id) {
349
    // Note - doing this to make sure the delete checkbox stays in the
form.
35083
    $form['#cache'] = TRUE;
351
35283
    $form['menu'] = array(
35383
      '#type' => 'fieldset',
35483
      '#title' => t('Menu settings'),
35583
      '#access' => user_access('administer menu'),
35683
      '#collapsible' => TRUE,
35783
      '#collapsed' => FALSE,
35883
      '#tree' => TRUE,
35983
      '#weight' => -2,
36083
      '#attributes' => array('class' => 'menu-item-form'),
361
    );
36283
    $item = $form['#node']->menu;
363
36483
    if ($item['mlid']) {
365
      // There is an existing link.
3660
      $form['menu']['delete'] = array(
3670
        '#type' => 'checkbox',
3680
        '#title' => t('Delete this menu item.'),
369
      );
3700
    }
37183
    if (!$item['link_title']) {
37283
      $form['menu']['#collapsed'] = TRUE;
37383
    }
374
37583
    foreach (array('mlid', 'module', 'hidden', 'has_children',
'customized', 'options', 'expanded', 'hidden', 'parent_depth_limit') as
$key) {
37683
      $form['menu'][$key] = array('#type' => 'value', '#value' =>
$item[$key]);
37783
    }
37883
    $form['menu']['#item'] = $item;
379
38083
    $form['menu']['link_title'] = array('#type' => 'textfield',
38183
      '#title' => t('Menu link title'),
38283
      '#default_value' => $item['link_title'],
38383
      '#description' => t('The link text corresponding to this item that
should appear in the menu. Leave blank if you do not wish to add this post
to the menu.'),
38483
      '#required' => FALSE,
385
    );
386
    // Generate a list of possible parents (not including this item or
descendants).
38783
    $options = menu_parent_options(menu_get_menus(), $item);
38883
    $default = $item['menu_name'] . ':' . $item['plid'];
38983
    if (!isset($options[$default])) {
3900
      $default = 'main-menu:0';
3910
    }
39283
    $form['menu']['parent'] = array(
39383
      '#type' => 'select',
39483
      '#title' => t('Parent item'),
39583
      '#default_value' => $default,
39683
      '#options' => $options,
39783
      '#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)),
39883
      '#attributes' => array('class' => 'menu-title-select'),
399
    );
40083
    $form['#submit'][] = 'menu_node_form_submit';
401
40283
    $form['menu']['weight'] = array(
40383
      '#type' => 'weight',
40483
      '#title' => t('Weight'),
40583
      '#delta' => 50,
40683
      '#default_value' => $item['weight'],
40783
      '#description' => t('Optional. In the menu, the heavier items will
sink and the lighter items will be positioned nearer the top.'),
408
    );
40983
  }
4101338
}
411
412
/**
413
 * Decompose the selected menu parent option into the menu_name and plid.
414
 */
4152027
function menu_node_form_submit($form, &$form_state) {
41670
  list($form_state['values']['menu']['menu_name'],
$form_state['values']['menu']['plid']) = explode(':',
$form_state['values']['menu']['parent']);
41770
}
418
419
/**
420
 * Return an associative array of the custom menus names.
421
 *
422
 * @param $all
423
 *   If FALSE return only user-added menus, or if TRUE also include
424
 *   the menus defined by the system.
425
 * @return
426
 *   An array with the machine-readable names as the keys, and
human-readable
427
 *   titles as the values.
428
 */
4292027
function menu_get_menus($all = TRUE) {
430211
  $system_menus = menu_list_system_menus();
431211
  $sql = 'SELECT * FROM {menu_custom}';
432211
  if (!$all) {
4330
    $sql .= ' WHERE menu_name NOT IN (' . implode(',', array_fill(0,
count($system_menus), "'%s'")) . ')';
4340
  }
435211
  $sql .= ' ORDER BY title';
436211
  $result = db_query($sql, $system_menus);
437211
  $rows = array();
438211
  while ($r = db_fetch_array($result)) {
439211
    $rows[$r['menu_name']] = $r['title'];
440211
  }
441211
  return $rows;
4420
}
4432027