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

Line #Times calledCode
1
<?php
2
// $Id: menu.module,v 1.172 2008/10/12 04:30:06 webchick 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
 */
132366
define('MENU_MAX_MENU_NAME_LENGTH_UI', 27);
14
15
/**
16
 * Implementation of hook_help().
17
 */
182366
function menu_help($path, $arg) {
19
  switch ($path) {
201678
    case 'admin/help#menu':
2134
      $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>';
2234
      $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>';
2334
      $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>';
2434
      return $output;
251676
    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>';
271674
    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>';
291672
    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>';
311672
    case 'admin/build/menu/item/add':
320
      return '<p>' . t('Enter the title and path for your new menu item.')
. '</p>';
330
  }
341672
}
35
36
/**
37
 * Implementation of hook_perm().
38
 */
392366
function menu_perm() {
40
  return array(
41
    'administer menu' => array(
42172
      'title' => t('Administer menu'),
43172
      'description' => t('Manage menus and menu items.'),
44172
    ),
45172
  );
460
}
47
48
/**
49
 * Implementation of hook_menu().
50
 */
512366
function menu_menu() {
52161
  $items['admin/build/menu'] = array(
53161
    'title' => 'Menus',
54161
    'description' => "Control your site's Navigation menu, Main menu and
Secondary menu. as well as rename and reorganize menu items.",
55161
    'page callback' => 'menu_overview_page',
56161
    'access callback' => 'user_access',
57161
    'access arguments' => array('administer menu'),
58
  );
59161
  $items['admin/build/menu/list'] = array(
60161
    'title' => 'List menus',
61161
    'type' => MENU_DEFAULT_LOCAL_TASK,
62161
    'weight' => -10,
63
  );
64161
  $items['admin/build/menu/add'] = array(
65161
    'title' => 'Add menu',
66161
    'page callback' => 'drupal_get_form',
67161
    'page arguments' => array('menu_edit_menu', 'add'),
68161
    'access arguments' => array('administer menu'),
69161
    'type' => MENU_LOCAL_TASK,
70
  );
71161
  $items['admin/build/menu/settings'] = array(
72161
    'title' => 'Settings',
73161
    'page callback' => 'drupal_get_form',
74161
    'page arguments' => array('menu_configure'),
75161
    'access arguments' => array('administer menu'),
76161
    'type' => MENU_LOCAL_TASK,
77161
    'weight' => 5,
78
  );
79161
  $items['admin/build/menu-customize/%menu'] = array(
80161
    'title' => 'Customize menu',
81161
    'page callback' => 'drupal_get_form',
82161
    'page arguments' => array('menu_overview_form', 3),
83161
    'title callback' => 'menu_overview_title',
84161
    'title arguments' => array(3),
85161
    'access arguments' => array('administer menu'),
86161
    'type' => MENU_CALLBACK,
87
  );
88161
  $items['admin/build/menu-customize/%menu/list'] = array(
89161
    'title' => 'List items',
90161
    'weight' => -10,
91161
    'type' => MENU_DEFAULT_LOCAL_TASK,
92
  );
93161
  $items['admin/build/menu-customize/%menu/add'] = array(
94161
    'title' => 'Add item',
95161
    'page callback' => 'drupal_get_form',
96161
    'page arguments' => array('menu_edit_item', 'add', NULL, 3),
97161
    'access arguments' => array('administer menu'),
98161
    'type' => MENU_LOCAL_TASK,
99
  );
100161
  $items['admin/build/menu-customize/%menu/edit'] = array(
101161
    'title' => 'Edit menu',
102161
    'page callback' => 'drupal_get_form',
103161
    'page arguments' => array('menu_edit_menu', 'edit', 3),
104161
    'access arguments' => array('administer menu'),
105161
    'type' => MENU_LOCAL_TASK,
106
  );
107161
  $items['admin/build/menu-customize/%menu/delete'] = array(
108161
    'title' => 'Delete menu',
109161
    'page callback' => 'menu_delete_menu_page',
110161
    'page arguments' => array(3),
111161
    'access arguments' => array('administer menu'),
112161
    'type' => MENU_CALLBACK,
113
  );
114161
  $items['admin/build/menu/item/%menu_link/edit'] = array(
115161
    'title' => 'Edit menu item',
116161
    'page callback' => 'drupal_get_form',
117161
    'page arguments' => array('menu_edit_item', 'edit', 4, NULL),
118161
    'access arguments' => array('administer menu'),
119161
    'type' => MENU_CALLBACK,
120
  );
121161
  $items['admin/build/menu/item/%menu_link/reset'] = array(
122161
    'title' => 'Reset menu item',
123161
    'page callback' => 'drupal_get_form',
124161
    'page arguments' => array('menu_reset_item_confirm', 4),
125161
    'access arguments' => array('administer menu'),
126161
    'type' => MENU_CALLBACK,
127
  );
128161
  $items['admin/build/menu/item/%menu_link/delete'] = array(
129161
    'title' => 'Delete menu item',
130161
    'page callback' => 'menu_item_delete_page',
131161
    'page arguments' => array(4),
132161
    'access arguments' => array('administer menu'),
133161
    'type' => MENU_CALLBACK,
134
  );
135161
  return $items;
1360
}
137
138
/**
139
 * Implemenation of hook_theme().
140
 */
1412366
function menu_theme() {
142
  return array(
143
    'menu_overview_form' => array(
144178
      'file' => 'menu.admin.inc',
145178
      'arguments' => array('form' => NULL),
146178
    ),
147178
  );
1480
}
149
150
/**
151
 * Implementation of hook_enable().
152
 *
153
 *  Add a link for each custom menu.
154
 */
1552366
function menu_enable() {
156134
  menu_rebuild();
157134
  $link = db_fetch_array(db_query("SELECT mlid AS plid, menu_name from
{menu_links} WHERE link_path = 'admin/build/menu' AND module = 'system'"));
158134
  $link['router_path'] = 'admin/build/menu-customize/%';
159134
  $link['module'] = 'menu';
160134
  $result = db_query("SELECT * FROM {menu_custom}");
161134
  while ($menu = db_fetch_array($result)) {
162134
    $link['mlid'] = 0;
163134
    $link['link_title'] = $menu['title'];
164134
    $link['link_path'] = 'admin/build/menu-customize/' .
$menu['menu_name'];
165134
    if (!db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_path
= '%s' AND plid = %d", $link['link_path'], $link['plid']))) {
166134
      menu_link_save($link);
167134
    }
168134
  }
169134
  menu_cache_clear_all();
170134
}
171
172
/**
173
 * Title callback for the menu overview page and links.
174
 */
1752366
function menu_overview_title($menu) {
17647
  return $menu['title'];
1770
}
178
179
/**
180
 * Load the data for a single custom menu.
181
 */
1822366
function menu_load($menu_name) {
1831747
  return db_fetch_array(db_query("SELECT * FROM {menu_custom} WHERE
menu_name = '%s'", $menu_name));
1840
}
185
186
/**
187
 * Return a list of menu items that are valid possible parents for the
given menu item.
188
 *
189
 * @param $menus
190
 *   An array of menu names and titles, such as from menu_get_menus().
191
 * @param $item
192
 *   The menu item for which to generate a list of parents.
193
 *   If $item['mlid'] == 0 then the complete tree is returned.
194
 * @return
195
 *   An array of menu link titles keyed on the a string containing the menu
name
196
 *   and mlid. The list excludes the given item and its children.
197
 */
1982366
function menu_parent_options($menus, $item) {
199
  // The menu_links table can be practically any size and we need a way to
200
  // allow contrib modules to provide more scalable pattern choosers.
201
  // hook_form_alter is too late in itself because all the possible parents
are
202
  // retrieved here, unless menu_override_parent_selector is set to TRUE.
203135
  if (variable_get('menu_override_parent_selector', FALSE)) {
2040
    return array();
2050
  }
206
  // If the item has children, there is an added limit to the depth of
valid parents.
207135
  if (isset($item['parent_depth_limit'])) {
20889
    $limit = $item['parent_depth_limit'];
20989
  }
210
  else {
21146
    $limit = _menu_parent_depth_limit($item);
212
  }
213
214135
  foreach ($menus as $menu_name => $title) {
215135
    $tree = menu_tree_all_data($menu_name, NULL);
216135
    $options[$menu_name . ':0'] = '<' . $title . '>';
217135
    _menu_parents_recurse($tree, $menu_name, '--', $options, $item['mlid'],
$limit);
218135
  }
219135
  return $options;
2200
}
221
222
/**
223
 * Recursive helper function for menu_parent_options().
224
 */
2252366
function _menu_parents_recurse($tree, $menu_name, $indent, &$options,
$exclude, $depth_limit) {
226135
  foreach ($tree as $data) {
227135
    if ($data['link']['depth'] > $depth_limit) {
228
      // Don't iterate through any links on this level.
2290
      break;
2300
    }
231135
    if ($data['link']['mlid'] != $exclude && $data['link']['hidden'] >= 0)
{
232135
      $title = $indent . ' ' . truncate_utf8($data['link']['title'], 30,
TRUE, FALSE);
233135
      if ($data['link']['hidden']) {
234135
        $title .= ' (' . t('disabled') . ')';
235135
      }
236135
      $options[$menu_name . ':' . $data['link']['mlid']] = $title;
237135
      if ($data['below']) {
238128
        _menu_parents_recurse($data['below'], $menu_name, $indent . '--',
$options, $exclude, $depth_limit);
239128
      }
240135
    }
241135
  }
242135
}
243
244
/**
245
 * Reset a system-defined menu item.
246
 */
2472366
function menu_reset_item($item) {
2481
  $router = menu_router_build();
2491
  $new_item = _menu_link_build($router[$item['router_path']]);
2501
  foreach (array('mlid', 'has_children') as $key) {
2511
    $new_item[$key] = $item[$key];
2521
  }
2531
  menu_link_save($new_item);
2541
  return $new_item;
2550
}
256
257
/**
258
 * Implementation of hook_block().
259
 */
2602366
function menu_block($op = 'list', $delta = '') {
26197
  $menus = menu_get_menus();
262
  // The Navigation menu is handled by the user module.
26397
  unset($menus['navigation']);
26497
  if ($op == 'list') {
26529
    $blocks = array();
26629
    foreach ($menus as $name => $title) {
267
      // Default "Navigation" block is handled by user.module.
26829
      $blocks[$name]['info'] = check_plain($title);
269
      // Menu blocks can't be cached because each menu item can have
270
      // a custom access callback. menu.inc manages its own caching.
27129
      $blocks[$name]['cache'] = BLOCK_NO_CACHE;
27229
    }
27329
    return $blocks;
2740
  }
27569
  elseif ($op == 'view') {
27669
    $data['subject'] = check_plain($menus[$delta]);
27769
    $data['content'] = menu_tree($delta);
27869
    return $data;
2790
  }
2800
}
281
282
/**
283
 * Implementation of hook_nodeapi_insert().
284
 */
2852366
function menu_nodeapi_insert(&$node) {
28658
  if (isset($node->menu)) {
28740
    $item = $node->menu;
28840
    if (!empty($item['delete'])) {
2890
      menu_link_delete($item['mlid']);
2900
    }
29140
    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
    }
30140
  }
30258
}
303
304
/**
305
 * Implementation of hook_nodeapi_update().
306
 */
3072366
function menu_nodeapi_update(&$node) {
30831
  if (isset($node->menu)) {
30927
    $item = $node->menu;
31027
    if (!empty($item['delete'])) {
3110
      menu_link_delete($item['mlid']);
3120
    }
31327
    elseif (trim($item['link_title'])) {
3140
      $item['link_title'] = trim($item['link_title']);
3150
      $item['link_path'] = "node/$node->nid";
3160
      if (!$item['customized']) {
3170
        $item['options']['attributes']['title'] = trim($node->title);
3180
      }
3190
      if (!menu_link_save($item)) {
3200
        drupal_set_message(t('There was an error saving the menu link.'),
'error');
3210
      }
3220
    }
32327
  }
32431
}
325
326
/**
327
 * Implementation of hook_nodeapi_delete().
328
 */
3292366
function menu_nodeapi_delete(&$node) {
330
  // Delete all menu module links that point to this node.
33112
  $result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path
AND module = 'menu'", array(':path' => 'node/'. $node->nid));
33212
  while ($m = db_fetch_array($result)) {
3330
    menu_link_delete($m['mlid']);
3340
  }
33512
}
336
337
/**
338
 * Implementation of hook_nodeapi_prepare().
339
 */
3402366
function menu_nodeapi_prepare(&$node) {
34186
  if (empty($node->menu)) {
342
    // Prepare the node for the edit form so that $node->menu always
exists.
34382
    $menu_name = variable_get('menu_default_node_menu', 'main-menu');
34482
    $item = array();
34582
    if (isset($node->nid)) {
346
      // Give priority to the default menu
34737
      $mlid = db_result(db_query_range("SELECT mlid FROM {menu_links} WHERE
link_path = :path AND menu_name = :menu_name AND module = 'menu' ORDER BY
mlid ASC", array(
34837
        ':path' => 'node/'. $node->nid,
34937
        ':menu_name' => $menu_name,
35037
      ), 0, 1));
351
      // Check all menus if a link does not exist in the default menu.
35237
      if (!$mlid) {
35337
        $mlid = db_result(db_query_range("SELECT mlid FROM {menu_links}
WHERE link_path = :path AND module = 'menu' ORDER BY mlid ASC", array(
35437
          ':path' => 'node/'. $node->nid,
35537
        ), 0, 1));
35637
      }
35737
      if ($mlid) {
3580
        $item = menu_link_load($mlid);
3590
      }
36037
    }
361
    // Set default values.
36282
    $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);
36382
  }
364
  // Find the depth limit for the parent select.
36586
  if (!isset($node->menu['parent_depth_limit'])) {
36682
    $node->menu['parent_depth_limit'] =
_menu_parent_depth_limit($node->menu);
36782
  }
36886
}
369
370
/**
371
 * Find the depth limit for items in the parent select.
372
 */
3732366
function _menu_parent_depth_limit($item) {
374128
  return MENU_MAX_DEPTH - 1 - (($item['mlid'] && $item['has_children']) ?
menu_link_children_relative_depth($item) : 0);
3750
}
376
377
/**
378
 * Implementation of hook_form_alter(). Adds menu item fields to the node
form.
379
 */
3802366
function menu_form_alter(&$form, $form_state, $form_id) {
3811575
  if (!empty($form['#node_edit_form'])) {
382
    // Note - doing this to make sure the delete checkbox stays in the
form.
38389
    $form['#cache'] = TRUE;
384
38589
    $form['menu'] = array(
38689
      '#type' => 'fieldset',
38789
      '#title' => t('Menu settings'),
38889
      '#access' => user_access('administer menu'),
38989
      '#collapsible' => TRUE,
39089
      '#collapsed' => FALSE,
39189
      '#tree' => TRUE,
39289
      '#weight' => -2,
39389
      '#attributes' => array('class' => 'menu-item-form'),
394
    );
39589
    $item = $form['#node']->menu;
396
39789
    if ($item['mlid']) {
398
      // There is an existing link.
3990
      $form['menu']['delete'] = array(
4000
        '#type' => 'checkbox',
4010
        '#title' => t('Delete this menu item.'),
402
      );
4030
    }
40489
    if (!$item['link_title']) {
40589
      $form['menu']['#collapsed'] = TRUE;
40689
    }
407
40889
    foreach (array('mlid', 'module', 'hidden', 'has_children',
'customized', 'options', 'expanded', 'hidden', 'parent_depth_limit') as
$key) {
40989
      $form['menu'][$key] = array('#type' => 'value', '#value' =>
$item[$key]);
41089
    }
41189
    $form['menu']['#item'] = $item;
412
41389
    $form['menu']['link_title'] = array('#type' => 'textfield',
41489
      '#title' => t('Menu link title'),
41589
      '#default_value' => $item['link_title'],
41689
      '#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.'),
41789
      '#required' => FALSE,
418
    );
419
    // Generate a list of possible parents (not including this item or
descendants).
42089
    $options = menu_parent_options(menu_get_menus(), $item);
42189
    $default = $item['menu_name'] . ':' . $item['plid'];
42289
    if (!isset($options[$default])) {
4230
      $default = 'main-menu:0';
4240
    }
42589
    $form['menu']['parent'] = array(
42689
      '#type' => 'select',
42789
      '#title' => t('Parent item'),
42889
      '#default_value' => $default,
42989
      '#options' => $options,
43089
      '#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)),
43189
      '#attributes' => array('class' => 'menu-title-select'),
432
    );
43389
    $form['#submit'][] = 'menu_node_form_submit';
434
43589
    $form['menu']['weight'] = array(
43689
      '#type' => 'weight',
43789
      '#title' => t('Weight'),
43889
      '#delta' => 50,
43989
      '#default_value' => $item['weight'],
44089
      '#description' => t('Optional. In the menu, the heavier items will
sink and the lighter items will be positioned nearer the top.'),
441
    );
44289
  }
4431575
}
444
445
/**
446
 * Decompose the selected menu parent option into the menu_name and plid.
447
 */
4482366
function menu_node_form_submit($form, &$form_state) {
44974
  list($form_state['values']['menu']['menu_name'],
$form_state['values']['menu']['plid']) = explode(':',
$form_state['values']['menu']['parent']);
45074
}
451
452
/**
453
 * Return an associative array of the custom menus names.
454
 *
455
 * @param $all
456
 *   If FALSE return only user-added menus, or if TRUE also include
457
 *   the menus defined by the system.
458
 * @return
459
 *   An array with the machine-readable names as the keys, and
human-readable
460
 *   titles as the values.
461
 */
4622366
function menu_get_menus($all = TRUE) {
463218
  $system_menus = menu_list_system_menus();
464218
  $query = db_select('menu_custom');
465218
  $query->addField('menu_custom', 'menu_name', 'menu_name');
466218
  $query->addField('menu_custom', 'title', 'title');
467218
  if (!$all) {
4680
    $query->condition('menu_name', $system_menus, 'NOT IN');
4690
  }
470218
  $query->orderBy('title');
471
472218
  return $query->execute()->fetchAllKeyed();
4730
}
4742366