menu.module 28.3 KB
Newer Older
1 2 3
<?php
// $Id$

Dries's avatar
 
Dries committed
4 5 6 7 8
/**
 * @file
 * Allows administrators to customize the site navigation menu.
 */

9 10 11 12 13
/**
 * Implementation of hook_help().
 */
function menu_help($section) {
  switch ($section) {
14
    case 'admin/help#menu':
15 16
      $output = t('<p>Menus are a collection of links (menu items) used to navigate a website. The menu module provides an interface to control and customize the powerful menu system that comes with Drupal. Menus are primarily displayed as a hierarchical list of links using Drupal\'s highly flexible <a href="@admin-block">blocks</a> feature. Each menu automatically creates a block of the same name. By default, new menu items are placed inside a built-in menu labelled %navigation, but administrators can also create custom menus.</p>
<p>Drupal themes generally provide out-of-the-box support for two menus commonly labelled %primary-links and %secondary-links. These are sets of links which are usually displayed in the header or footer of each page (depending on the currently active theme). Any menu can be designated as the primary or secondary links menu via the <a href="@menu-settings">menu settings page</a>.</p>
17
Menu administration tabs:
18
<ul>
19 20 21
  <li>On the administer menu page, administrators can "edit" to change the title, description, parent or weight of a menu item. Under the "operations" column, click on "enable/disable" to toggle a menu item on or off. Only menu items which are enabled are displayed in the corresponding menu block. Note that the default menu items generated by the menu module cannot be deleted, only disabled.</li>
  <li>Use the "add menu" tab to submit a title for a new custom menu. Once submitted, the menu will appear in a list toward the bottom of the administer menu page underneath the main navigation menu. Under the menu name there will be links to edit or delete the menu, and a link to add new items to the menu.</li>
  <li>Use the "add menu item" tab to create new links in either the navigation or a custom menu (such as a primary/secondary links menu). Select the parent item to place the new link within an existing menu structure. For top level menu items, choose the name of the menu in which the link is to be added.</li>
22 23
</ul>', array('%navigation' => 'Navigation', '%primary-links' => 'primary links', '%secondary-links' => 'secondary links', '@admin-block' => url('admin/build/block'), '@menu-settings' => url('admin/build/menu/settings')));
      $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@menu">Menu page</a>.', array('@menu' => 'http://drupal.org/handbook/modules/menu/')) .'</p>';
24
      return $output;
25
    case 'admin/build/menu':
26
      return '<p>'. t('Menus are a collection of links (menu items) used to navigate a website. The list(s) below display the currently available menus along with their menu items. Select an operation from the list to manage each menu or menu item.', array('@admin-settings-menus' => url('admin/build/menu/settings'), '@admin-block' => url('admin/build/block'))) .'</p>';
27
    case 'admin/build/menu/menu/add':
28
      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>';
29
    case 'admin/build/menu/item/add':
30
      return '<p>'. t('Enter the title, path, position and the weight for your new menu item.') .'</p>';
31 32 33
  }
}

34 35 36 37 38 39 40
/**
 * Implementation of hook_perm().
 */
function menu_perm() {
  return array('administer menu');
}

41
/**
42
 * Implementation of hook_menu().
43
 */
44
function menu_menu() {
45
  $items['admin/build/menu'] = array(
46 47
    'title' => 'Menus',
    'description' => "Control your site's navigation menu, primary links and secondary links. as well as rename and reorganize menu items.",
48 49 50 51 52
    'page callback' => 'menu_overview',
    'access callback' => 'user_access',
    'access arguments' => array('administer menu'),
  );
  $items['admin/build/menu/list'] = array(
53
    'title' => 'List',
54 55 56 57
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/build/menu/item/disable'] = array(
58
    'title' => 'Disable menu item',
59 60 61 62 63
    'page callback' => 'menu_flip_item',
    'page arguments' => array('0'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/build/menu/item/enable'] = array(
64
    'title' => 'Enable menu item',
65 66 67 68 69
    'page callback' => 'menu_flip_item',
    'page arguments' => array('1'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/build/menu/item/add'] = array(
70
    'title' => 'Add menu item',
71 72 73 74 75
    'page callback' => 'drupal_get_form',
    'page arguments' => array('menu_edit_item_form', 'add'),
    'parent' => 'admin/build/menu',
    'type' => MENU_LOCAL_TASK);
  $items['admin/build/menu/item/edit'] = array(
76
    'title' => 'Edit menu item',
77 78 79 80
    'page callback' => 'drupal_get_form',
    'page arguments' => array('menu_edit_item_form', 'edit'),
    'type' => MENU_CALLBACK);
  $items['admin/build/menu/item/reset'] = array(
81
    'title' => 'Reset menu item',
82 83 84 85
    'page callback' => 'drupal_get_form',
    'page arguments' => array('menu_reset_item'),
    'type' => MENU_CALLBACK);
  $items['admin/build/menu/item/delete'] = array(
86
    'title' => 'Delete menu item',
87 88 89
    'page callback' => 'drupal_get_form',
    'page arguments' => array('menu_item_delete_form'),
    'type' => MENU_CALLBACK);
90
  $result = db_query('SELECT * FROM {menu_custom} WHERE admin = 1');
91
  while ($item = db_fetch_array($result)) {
92 93
    $item['_custom_item'] = TRUE;
    $item['_external'] = menu_path_is_external($item['path']);
94 95
    $items[$item['path']] = $item;
  }
96

97 98
  return $items;
/*
99
    $items[] = array('path' => 'admin/build/menu/menu/add',
100
      'title' => 'Add menu',
101 102
      'callback' => 'drupal_get_form',
      'callback arguments' => array('menu_edit_menu_form', 'add'),
103 104
      'access' => user_access('administer menu'),
      'type' => MENU_LOCAL_TASK);
105
    $items[] = array('path' => 'admin/build/menu/menu/edit',
106
      'title' => 'Edit menu',
107 108
      'callback' => 'drupal_get_form',
      'callback arguments' => array('menu_edit_menu_form', 'edit'),
109
      'access' => user_access('administer menu'),
110
      'type' => MENU_CALLBACK);
111
    $items[] = array('path' => 'admin/build/menu/menu/delete',
112
      'title' => 'Delete menu',
113 114
      'callback' => 'drupal_get_form',
      'callback arguments' => array('menu_item_delete_form'),
115 116
      'access' => user_access('administer menu'),
      'type' => MENU_CALLBACK);
117

118
    $items[] = array('path' => 'admin/build/menu/settings',
119
      'title' => 'Settings',
120 121
      'callback' => 'drupal_get_form',
      'callback arguments' => array('menu_configure'),
122 123 124
      'type' => MENU_LOCAL_TASK,
      'weight' => 5,
    );
125
*/
126 127 128
}


129
/**
130 131
 * Menu callback which displays every menu element accessible to the current
 * user and the relevant operations.
132
 */
133 134
function menu_overview() {
  $header = array(t('Menu item'), t('Expanded'), array('data' => t('Operations'), 'colspan' => '3'));
135 136


137
  $result = db_query('SELECT m.*, me.disabled FROM {menu} m LEFT JOIN {menu_custom} me ON m.path = me.path WHERE visible = 1 OR (disabled = 1 AND admin = 0) ORDER BY mleft');
138 139 140 141 142 143
  $map = arg();
  $rows = array();
  while ($item = db_fetch_object($result)) {
    _menu_translate($item, $map, MENU_RENDER_LINK);
    if (!$item->access) {
      continue;
144
    }
145 146 147 148 149 150 151 152 153 154
    $title = str_repeat('&nbsp;&nbsp;', $item->depth - 1) . ($item->depth > 1 ? '-&nbsp;' : '');
    $title .= l($item->title, $item->path, (array)$item);
    // Populate the operations field.
    $operations = array();
    // Set the edit column.
    $operations[] = array('data' => l(t('edit'), 'admin/build/menu/item/edit/'. $item->mid));
    if ($item->disabled) {
      $title .= ' ('. t('disabled') .')';
      $class = 'menu-disabled';
      $operations[] = array('data' => l(t('enable'), 'admin/build/menu/item/enable/'. $item->mid));
155
    }
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    else {
      $class = 'menu-enabled';
      $operations[] = array('data' => l(t('disable'), 'admin/build/menu/item/disable/'. $item->mid));
    }
    // Set the reset column.
    if ($item->type & MENU_CREATED_BY_ADMIN) {
      $operations[] = array('data' => l(t('delete'), 'admin/build/menu/item/delete/'. $item->mid));
    }
    else if ($item->type & MENU_MODIFIED_BY_ADMIN) {
      $operations[] = array('data' => l(t('reset'), 'admin/build/menu/item/reset/'. $item->mid));
    }
    else {
      $operations[] = array('data' => '');
    }
    $row = array(
      array('data' => $title, 'class' => $class),
      array('data' => ($item->has_children ? (($item->type & MENU_EXPANDED) ? t('Yes') : t('No')) : ''), 'class' => $class),
173
    );
174 175 176
    foreach ($operations as $operation) {
      $operation['class'] = $class;
      $row[] = $operation;
177
    }
178
    $rows[] = $row;
179
  }
180
  return theme('table', $header, $rows);
181
}
182

183
/**
184 185 186 187 188 189 190 191 192
 * Menu callback; enable/disable a menu item.
 *
 * @param $visible
 *   0 to remove from the menu tree. 1 to make the item and its children
 *   reappear in menu tree.
 * @param $mid
 *    mid of the menu item.
 * @param $path
 *    Optional. Path of the menu item. Only considered if mid is NULL.
193
 */
194 195
function menu_flip_item($visible, $mid, $path = NULL) {
  if (isset($mid)) {
196
    $item = menu_get_item_by_mid($mid);
197 198
  }
  elseif (isset($path)) {
199
    $item = menu_get_item($path);
200
  }
201 202
  if (isset($item) && $item->access) {
    $result = db_query('SELECT child.*, parent.path AS parent_path FROM {menu} child INNER JOIN {menu} parent ON child.pid = parent.mid WHERE %d <= child.mleft AND child.mright <= %d', $item->mleft, $item->mright);
203
    while ($item = db_fetch_object($result)) {
204
      $update_result = db_query("UPDATE {menu_custom} SET disabled = %d WHERE path = '%s'", !$visible, $item->path);
205
      if (!db_affected_rows($update_result)) {
206
        db_query("INSERT INTO {menu_custom} (parent, path, title, description, weight, type, admin, disabled) VALUES ('%s', '%s', '%s', '%s', %d, %d, %d, %d)", $item->parent_path, $item->path, $item->title, $item->description, 0, $item->type, 0, !$visible);
207 208 209 210 211 212
      }
    }
    menu_rebuild();
    if (isset($mid)) {
      drupal_set_message($visible ? t('The menu item has been enabled.') : t('The menu item has been disabled.'));
      drupal_goto('admin/build/menu');
213
    }
214 215 216 217 218 219
  }
}

/**
 * Present the menu item editing form.
 */
220 221
function menu_edit_item_form($type, $mid = 0) {
  if ($type == 'edit') {
222
    if (!($item = menu_get_item_by_mid($mid))) {
223 224 225
      drupal_not_found();
      return;
    }
226 227
  }
  else {
228
    // This is an add form.
229
    // The mid argument (if set) will be the default pid to use.
230
    $item = (object)array('mid' => 0, 'pid' => $mid ? $mid : 0, 'weight' => 0, 'type' => MENU_CUSTOM_ITEM, 'title' => '', 'description' => '', 'path' => '');
231
  }
Dries's avatar
 
Dries committed
232

233 234
  $form['title'] = array('#type' => 'textfield',
    '#title' => t('Title'),
235
    '#default_value' => $item->title,
236 237 238
    '#description' => t('The name of the menu item.'),
    '#required' => TRUE,
  );
239 240
  $form['description'] = array(
    '#type' => 'textfield',
241
    '#title' => t('Description'),
242
    '#default_value' => $item->description,
243 244
    '#description' => t('The description displayed when hovering over a menu item.'),
  );
Dries's avatar
 
Dries committed
245

246 247 248
  if ($item->type & MENU_CREATED_BY_ADMIN) {
    $form['path'] = array(
      '#type' => 'textfield',
249
      '#title' => t('Path'),
250
      '#default_value' => $item->path,
251
      '#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')),
252
      '#required' => TRUE,
253
    );
254 255
  }
  else {
256 257
    $form['_path'] = array(
      '#type' => 'item',
258
      '#title' => t('Path'),
259
      '#description' => l($item->path, $item->link_path, (array)$item),
260
    );
261
  }
262
  $form['original_path'] = array('#type' => 'value', '#value' => $item->path);
263

264 265
  $form['expanded'] = array(
    '#type' => 'checkbox',
266
    '#title' => t('Expanded'),
267
    '#default_value' => $item->type & MENU_EXPANDED ? 1 : 0,
268 269
    '#description' => t('If selected and this menu item has children, the menu will always appear expanded.'),
  );
270

271
  // Generate a list of possible parents (not including this item or descendants).
272 273 274
  $options = menu_parent_options($item->mid);
  $form['pid'] = array(
    '#type' => 'select',
275
    '#title' => t('Parent item'),
276
    '#default_value' => $item->pid,
277 278
    '#options' => $options,
  );
279 280
  $form['weight'] = array(
    '#type' => 'weight',
281
    '#title' => t('Weight'),
282
    '#default_value' => isset($item->weight) ? $item->weight : 0,
283 284
    '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
  );
Dries's avatar
 
Dries committed
285 286

  // Always enable menu items (but not menus) when editing them.
287 288
  if (!($item->type & MENU_IS_ROOT)) {
    $item->type |= MENU_VISIBLE_IN_TREE | MENU_VISIBLE_IN_BREADCRUMB;
Dries's avatar
 
Dries committed
289 290
  }

291
  $form['type'] = array('#type' => 'value', '#value' => $item->type);
292
  $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
293

294
  return $form;
295 296
}

297
function menu_edit_item_form_validate($form_values, $form, &$form_state) {
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
  if (isset($form_values['path'])) {
    $path = $form_values['path'];
    // Skip external links.
    $colonpos = strpos($path, ':');
    if ($colonpos !== FALSE && !preg_match('![/?#]!', substr($path, 0, $colonpos))) {
      return;
    }
    $item = menu_get_item($path);
    if (!$item || !$item->access) {
      form_set_error('path', t('This path is either invalid or you do not have access to it'));
    }
    elseif ($path != $form_values['original_path'] && $item->path == $path) {
      form_set_error('path', t('There is already a menu item pointing to this path.'));
    }
  }
}
Dries's avatar
 
Dries committed
314
/**
315
 * Process menu and menu item add/edit form submissions.
Dries's avatar
 
Dries committed
316
 */
317
function menu_edit_item_form_submit($form_values, $form, &$form_state) {
318
  menu_edit_item_save($form_values);
319 320
  $form_state['redirect'] = 'admin/build/menu';
  return;
Dries's avatar
 
Dries committed
321 322
}

323
/**
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
 * Return a list of menu items that are valid possible parents for the
 * given menu item. The list excludes the given item and its children.
 *
 * @param $mid
 *   The menu item id for which to generate a list of parents.
 *   If $mid == 0 then the complete tree is returned.
 * @param $pid
 *   The menu item id of the menu item at which to start the tree.
 *   If $pid > 0 then this item will be included in the tree.
 * @param $depth
 *   The current depth in the tree - used when recursing to indent the tree.
 * @return
 *   An array of menu titles keyed on the mid.
 */
function menu_parent_options($mid, $pid = 0, $depth = 0) {
  $options = array(0 => t('Root'));
  // Exclude $mid and its children from the list unless $mid is 0.
  if ($mid && $mid == $pid) {
    return $options;
  }
344
  $sql = 'SELECT m.*, me.disabled FROM {menu} m LEFT JOIN {menu_custom} me ON m.path = me.path WHERE (m.visible = 1 OR (me.disabled = 1 AND me.admin = 0))';
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
  if (!$mid) {
    $params = array();
  }
  elseif ($item = menu_get_item_by_mid($mid)) {
    $sql .=' AND (mleft < %d OR %d < mright)';
    $params = array($item->mleft, $item->mright);
  }
  else {
    return $options;
  }
  if ($parent_item = menu_get_item_by_mid($pid)) {
    $sql .= ' AND %d <= mleft AND %d <= mright';
    $params[] = $parent_item->mleft;
    $params[] = $parent_item->mright;
  }
  $sql .= ' ORDER BY mleft';
  $result = db_query($sql, $params);
  $map = arg();

  while ($item = db_fetch_object($result)) {
    _menu_translate($item, $map, MENU_RENDER_LINK);
    $title = str_repeat('--', $item->depth) .' '. $item->title;
    if ($item->disabled) {
      $title .= ' ('. t('disabled') .')';
    }
    $options[$item->mid] = $title;
  }
  return $options;
}

/**
 * Save changes to a menu item into the database.
 *
 * @return mid
 */
function menu_edit_item_save($edit) {
  if (isset($edit['expanded'])) {
    if ($edit['expanded']) {
      $edit['type'] |= MENU_EXPANDED;
    }
    else {
      $edit['type'] &= ~MENU_EXPANDED;
    }
  }

  $edit['type'] = $edit['type'] | MENU_MODIFIED_BY_ADMIN;

  $parent = $edit['pid'] ? db_result(db_query('SELECT path FROM {menu} WHERE mid = %d', $edit['pid'])) : '';
  $t_args = array('%title' => $edit['title']);
394 395
  if (!empty($edit['original_path']) && db_num_rows(db_query("SELECT * FROM {menu_custom} WHERE path='%s'", $edit['original_path']))) {
    db_query("UPDATE {menu_custom} SET parent = '%s', title = '%s', description = '%s', weight = %d, type = %d, path = '%s' WHERE path = '%s'", $parent, $edit['title'], $edit['description'], $edit['weight'], $edit['type'], isset($edit['path']) ? $edit['path'] : $edit['original_path'], $edit['original_path']);
396 397
  }
  else {
398
    db_query("INSERT INTO {menu_custom} (parent, path, title, description, weight, type, admin) VALUES ('%s', '%s', '%s', '%s', %d, %d, %d)", $parent, isset($edit['path']) ? $edit['path'] : $edit['original_path'], $edit['title'], $edit['description'], $edit['weight'], $edit['type'], isset($edit['path']));
399
  }
400
  watchdog('menu', 'Saved menu item %title.', $t_args, WATCHDOG_NOTICE, l(t('view'), 'admin/build/menu'));
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
  drupal_set_message(t('The menu item %title has been saved.', $t_args));
  menu_rebuild();
}

/**
 * Remove the menu item.
 */
function menu_node_form_delete($node) {
  menu_delete_item(array('path' => 'node/'. $node->nid));
}

/**
 * Menu callback; handle the adding/editing of a new menu.
 */
function menu_edit_menu_form($type, $mid = 0) {
  if ($type == 'edit') {
    if (!$item = menu_get_item_by_mid($mid)) {
      drupal_not_found();
      return;
    }
  }
  else {
    $item = array('mid' => 0, 'pid' => 0, 'path' => '', 'weight' => 0, 'type' => MENU_CUSTOM_MENU);
  }
  $form['title'] = array('#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => $item['title'],
    '#description' => t('The name of the menu.'),
    '#required' => TRUE,
  );
  $form['mid'] = array('#type' => 'value', '#value' => $item['mid']);
  $form['pid'] = array('#type' => 'value', '#value' => $item['pid']);
  $form['path'] = array('#type' => 'value', '#value' => $item['path']);
  $form['weight'] = array('#type' => 'value', '#value' => $item['weight']);
  $form['type'] = array('#type' => 'value', '#value' => $item['type']);
  $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
  // Reuse the submit function of menu_edit_item_form.
438 439
  $form['#submit'][] = 'menu_edit_item_form_submit';
  $form['#validate'][] = 'menu_edit_item_form_validate';
440 441 442 443 444 445 446 447 448 449
  $form['#theme'] = 'menu_edit_item_form';

  return $form;
}

/**
 * Menu callback; delete a single custom item.
 */
function menu_item_delete_form($mid) {
  if (!($menu = db_fetch_object(db_query('SELECT type, path, title FROM {menu} WHERE mid = %d', $mid)))) {
450 451 452 453
    drupal_not_found();
    return;
  }

454
  $form['path'] = array('#type' => 'value', '#value' => $menu->path);
455 456 457 458
  $form['type'] = array('#type' => 'value', '#value' => $menu->type);
  $form['title'] = array('#type' => 'value', '#value' => $menu->title);

  if ($menu->type & MENU_IS_ROOT) {
459
    $message = t('Are you sure you want to delete the menu %item?', array('%item' => $menu->title));
Dries's avatar
 
Dries committed
460 461
  }
  else {
462
    $message = t('Are you sure you want to delete the custom menu item %item?', array('%item' => $menu->title));
Dries's avatar
 
Dries committed
463 464
  }

465
  return confirm_form($form, $message, 'admin/build/menu', t('This action cannot be undone.'), t('Delete'));
466
}
467

468 469 470
/**
 * Process menu delete form submissions.
 */
471
function menu_item_delete_form_submit($form_values, $form, &$form_state) {
472
  menu_delete_item($form_values['path']);
473

474
  $t_args = array('%title' => $form_values['title']);
475 476
  if ($form_values['type'] & MENU_IS_ROOT) {
    drupal_set_message(t('The menu %title has been deleted.', $t_args));
477
    watchdog('menu', 'Deleted menu %title.', $t_args, WATCHDOG_NOTICE);
478
  }
479 480
  else {
    drupal_set_message(t('The menu item %title has been deleted.', $t_args));
481
    watchdog('menu', 'Deleted menu item %title.', $t_args, WATCHDOG_NOTICE);
482
  }
483

484 485
  $form_state['redirect'] = 'admin/build/menu';
  return;
486 487
}

488 489 490 491
/**
 * Menu callback; reset a single modified item.
 */
function menu_reset_item($mid) {
492 493 494
  if (isset($mid) && $item = db_fetch_object(db_query('SELECT path, title FROM {menu} WHERE mid = %d', $mid))) {
    $form['mid'] = array('#type' => 'value', '#value' => $item->path);
    return confirm_form($form, t('Are you sure you want to reset the item %item to its default values?', array('%item' => $item->title)), 'admin/build/menu', t('Any customizations will be lost. This action cannot be undone.'), t('Reset'));
495 496 497
  }
  else {
    drupal_not_found();
498 499 500 501
  }
}

/**
502
 * Process menu reset item form submissions.
503
 */
504
function menu_reset_item_submit($form_values, $form, &$form_state) {
505 506 507
  menu_delete_item($form_values['mid']);
  drupal_set_message(t('The menu item was reset to its default settings.'));

508 509
  $form_state['redirect'] = 'admin/build/menu';
  return;
510 511 512
}

/**
513 514 515 516 517
 * Delete a menu item from the database. If $item['mid'] is specified, then
 * this is used to find the existing item; otherwise, $item['path'] is used.
 *
 * @param $item
 *   The path to the menu item to be deleted.
518
 */
519
function menu_delete_item($path) {
520
  db_query("DELETE FROM {menu_custom} WHERE path = '%s'", $path);
521 522 523
  menu_rebuild();
}

524 525
// Conversion ends here.

526
/**
527
 * Implementation of hook_block().
528
 */
529 530 531 532 533 534 535 536 537
function _menu_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $blocks = array();
    $root_menus = menu_get_root_menus();
    foreach ($root_menus as $mid => $title) {
      // Default "Navigation" block is handled by user.module.
      if ($mid != 1) {
        $blocks[$mid]['info'] = $title;
      }
538
    }
539
    return $blocks;
540
  }
541 542 543 544 545 546 547
  else if ($op == 'view') {
    $item = menu_get_item($delta);
    $data['subject'] = check_plain($item['title']);
    $data['content'] = theme('menu_tree', $delta);
    return $data;
  }
}
548

549 550 551 552
/**
 * Implementation of hook_nodeapi().
 */
function _menu_nodeapi(&$node, $op) {
553

554 555 556 557 558 559 560 561 562 563 564 565 566 567
  if (user_access('administer menu')) {
    switch ($op) {
      case 'insert':
      case 'update':
        if ($node->menu['delete']) {
          menu_node_form_delete($node);
          menu_rebuild();
        }
        elseif ($node->menu['title']) {
          $node->menu['path'] = ($node->menu['path']) ? $node->menu['path'] : "node/$node->nid";
          menu_edit_item_save($node->menu);
          menu_rebuild();
        }
        break;
568

569 570 571 572 573
      case 'delete':
        menu_node_form_delete($node);
        menu_rebuild();
        break;
    }
574 575 576
  }
}

577
/**
578 579
 * Implementation of hook_form_alter().
 * Add menu item fields to the node form.
580
 */
581 582 583 584 585 586 587 588 589
function _menu_form_alter(&$form, $form_id) {
  if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
    $item = array();
    if ($form['nid']['#value'] > 0) {
      $item = db_fetch_array(db_query("SELECT * FROM {menu} WHERE path = 'node/%d'", $form['nid']['#value']));
      if (isset($form['#post']['menu']) && is_array($form['#post']['menu'])) {
        $item = !is_array($item) ? $form['#post']['menu'] : (($form['#post']['op'] == t('Preview')) ? array_merge($item, $form['#post']['menu']) : array_merge($form['#post']['menu'], $item));
      }
    }
590

591 592 593 594 595 596 597 598
    $form['menu'] = array('#type' => 'fieldset',
      '#title' => t('Menu settings'),
      '#access' => user_access('administer menu'),
      '#collapsible' => TRUE,
      '#collapsed' => empty($item['title']),
      '#tree' => TRUE,
      '#weight' => 30,
    );
599

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
    $form['menu']['title'] = array('#type' => 'textfield',
      '#title' => t('Title'),
      '#default_value' => $item['title'],
      '#description' => t('The name to display for this menu link.'),
    );

    $form['menu']['description'] = array('#type' => 'textfield',
      '#title' => t('Description'),
      '#default_value' => $item['description'],
      '#description' => t('The description displayed when hovering over a menu item.'),
    );

    // Generate a list of possible parents.
    $options = menu_parent_options($item['mid'], variable_get('menu_parent_items', 0));

    $form['menu']['pid'] = array('#type' => 'select',
      '#title' => t('Parent item'),
      '#default_value' => $item['pid'],
      '#options' => $options,
    );

    $form['menu']['path'] = array('#type' => 'hidden',
      '#value' => $item['path'],
    );

    $form['menu']['weight'] = array('#type' => 'weight',
      '#title' => t('Weight'),
      '#default_value' => $item['weight'],
      '#delta' => 10,
      '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
    );

    $form['menu']['mid'] = array('#type' => 'hidden',
      '#value' => $item['mid'] ? $item['mid'] : 0,
    );

    $form['menu']['type'] = array('#type' => 'hidden',
      '#value' => $item['type'] ? $item['type'] : MENU_CUSTOM_ITEM,
    );

    if ($item['mid'] > 0) {
      $form['menu']['delete'] = array('#type' => 'checkbox',
        '#title' => t('Check to delete this menu item.'),
        '#default_value' => $item['delete'],
      );

      $form['menu']['advanced'] = array('#type' => 'item',
        '#value' => t('You may also <a href="@edit">edit the advanced settings</a> for this menu item.', array('@edit' => url("admin/build/menu/item/edit/{$item['mid']}"))),
      );
649 650 651 652
    }
  }
}

653 654 655 656
function menu_get_root_menus() {
  return array();
}

657
/**
658
 * Menu callback; presents menu configuration options.
659
 */
660 661
function menu_configure() {
  $root_menus = menu_get_root_menus();
662

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
  $primary_options = $root_menus;
  $primary_options[0] = t('No primary links');

  $form['settings_links'] = array('#type' => 'fieldset',
    '#title' => t('Primary and secondary links settings'),
  );

  $form['settings_links']['intro'] = array('#type' => 'item',
    '#value' => t('Primary and secondary links provide a navigational menu system which usually (depending on your theme) appears at the top-right of the browser window. The links displayed can be generated either from a custom list created via the <a href="@menu">menu administration</a> page or from a built-in list of menu items such as the navigation menu links.', array('@menu' => url('admin/build/menu'))),
  );

  $form['settings_links']['menu_primary_menu'] = array('#type' => 'select',
    '#title' => t('Menu containing primary links'),
    '#default_value' => variable_get('menu_primary_menu', 0),
    '#options' => $primary_options,
  );

  $secondary_options = $root_menus;
  $secondary_options[0] = t('No secondary links');

  $form['settings_links']['menu_secondary_menu'] = array('#type' => 'select',
    '#title' => t('Menu containing secondary links'),
    '#default_value' => variable_get('menu_secondary_menu', 0),
    '#options' => $secondary_options,
    '#description' => t('If you select the same menu as primary links then secondary links will display the appropriate second level of your navigation hierarchy.'),
  );

  $form['settings_authoring'] = array('#type' => 'fieldset',
    '#title' => t('Content authoring form settings'),
  );

  $form['settings_authoring']['intro'] = array('#type' => 'item',
    '#value' => t('The menu module allows on-the-fly creation of menu links in the content authoring forms. The following option limits the menus in which a new link may be added. E.g., this can be used to force new menu items to be created in the primary links menu or to hide admin menu items.'),
  );

  $authoring_options = $root_menus;
  $authoring_options[0] = t('Show all menus');

  $form['settings_authoring']['menu_parent_items'] = array('#type' => 'select',
    '#title' => t('Restrict parent items to'),
    '#default_value' => variable_get('menu_parent_items', 0),
    '#options' => $authoring_options,
    '#description' => t('Choose the menu to be made available in the content authoring form. Only this menu item and its children will be shown.'),
   );

  return system_settings_form($form);
709 710
}

711 712 713
/**
 * Present the menu tree, rendered along with links to edit menu items.
 */
714
function _menu_overview_tree() {
715
  $menu = menu_get_menu();
716
  $root_menus = menu_get_root_menus();
Dries's avatar
 
Dries committed
717
  $header = array(t('Menu item'), t('Expanded'), array('data' => t('Operations'), 'colspan' => '3'));
718 719
  $output = '';

720 721
  // We reverse the root menu to show user created menus first.
  foreach (array_reverse($root_menus, true) as $mid => $title) {
722
    $operations = array();
Dries's avatar
 
Dries committed
723
    if ($menu['items'][$mid]['type'] & MENU_MODIFIABLE_BY_ADMIN) {
Steven Wittens's avatar
Steven Wittens committed
724
      $operations[] = l(t('Edit'), 'admin/build/menu/menu/edit/'. $mid);
725
    }
Dries's avatar
 
Dries committed
726
    if ($menu['items'][$mid]['type'] & MENU_CREATED_BY_ADMIN) {
Steven Wittens's avatar
Steven Wittens committed
727
      $operations[] = l(t('Delete'), 'admin/build/menu/menu/delete/'. $mid);
728
    }
Steven Wittens's avatar
Steven Wittens committed
729
    $operations[] = l(t('Add item'), 'admin/build/menu/item/add/'. $mid);
730
    $table = theme('item_list', $operations);
731 732 733 734

    $rows = menu_overview_tree_rows($mid);
    $table .= theme('table', $header, $rows ? $rows : array(array(array('data' => t('No menu items defined.'), 'colspan' => 5))));

735
    $output .= theme('box', check_plain($title), $table);
736 737 738
  }
  return $output;
}