web/drupal/modules/menu/menu.module
branchdrupal
changeset 74 0ff3ba646492
equal deleted inserted replaced
73:fcf75e232c5b 74:0ff3ba646492
       
     1 <?php
       
     2 // $Id: menu.module,v 1.157.2.6 2009/04/27 12:50:13 goba 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  */
       
    13 define('MENU_MAX_MENU_NAME_LENGTH_UI', 27);
       
    14 
       
    15 /**
       
    16  * Implementation of hook_help().
       
    17  */
       
    18 function menu_help($path, $arg) {
       
    19   switch ($path) {
       
    20     case 'admin/help#menu':
       
    21       $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>Primary links</em>, and <em>Secondary links</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 <em>Primary links</em> and <em>Secondary links</em>, by displaying them in either the header or footer of each page. By default, <em>Primary links</em> and <em>Secondary links</em> contain no menu items but may be configured to contain custom menu items specific to your site.") .'</p>';
       
    22       $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>';
       
    23       $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>';
       
    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 menus currently available on your site are displayed below. Select a menu from this list to manage its menu items.') .'</p>';
       
    27     case 'admin/build/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-customize/%':
       
    30       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>';
       
    31     case 'admin/build/menu/item/add':
       
    32       return '<p>'. t('Enter the title and path for your new menu item.') .'</p>';
       
    33   }
       
    34 }
       
    35 
       
    36 /**
       
    37  * Implementation of hook_perm().
       
    38  */
       
    39 function menu_perm() {
       
    40   return array('administer menu');
       
    41 }
       
    42 
       
    43 /**
       
    44  * Implementation of hook_menu().
       
    45  */
       
    46 function menu_menu() {
       
    47   $items['admin/build/menu'] = array(
       
    48     'title' => 'Menus',
       
    49     'description' => "Control your site's navigation menu, primary links and secondary links. as well as rename and reorganize menu items.",
       
    50     'page callback' => 'menu_overview_page',
       
    51     'access callback' => 'user_access',
       
    52     'access arguments' => array('administer menu'),
       
    53     'file' => 'menu.admin.inc',
       
    54   );
       
    55 
       
    56   $items['admin/build/menu/list'] = array(
       
    57     'title' => 'List menus',
       
    58     'type' => MENU_DEFAULT_LOCAL_TASK,
       
    59     'weight' => -10,
       
    60     'file' => 'menu.admin.inc',
       
    61   );
       
    62   $items['admin/build/menu/add'] = array(
       
    63     'title' => 'Add menu',
       
    64     'page callback' => 'drupal_get_form',
       
    65     'page arguments' => array('menu_edit_menu', 'add'),
       
    66     'access arguments' => array('administer menu'),
       
    67     'type' => MENU_LOCAL_TASK,
       
    68     'file' => 'menu.admin.inc',
       
    69   );
       
    70   $items['admin/build/menu/settings'] = array(
       
    71     'title' => 'Settings',
       
    72     'page callback' => 'drupal_get_form',
       
    73     'page arguments' => array('menu_configure'),
       
    74     'access arguments' => array('administer menu'),
       
    75     'type' => MENU_LOCAL_TASK,
       
    76     'weight' => 5,
       
    77     'file' => 'menu.admin.inc',
       
    78   );
       
    79   $items['admin/build/menu-customize/%menu'] = array(
       
    80     'title' => 'Customize menu',
       
    81     'page callback' => 'drupal_get_form',
       
    82     'page arguments' => array('menu_overview_form', 3),
       
    83     'title callback' => 'menu_overview_title',
       
    84     'title arguments' => array(3),
       
    85     'access arguments' => array('administer menu'),
       
    86     'type' => MENU_CALLBACK,
       
    87     'file' => 'menu.admin.inc',
       
    88   );
       
    89   $items['admin/build/menu-customize/%menu/list'] = array(
       
    90     'title' => 'List items',
       
    91     'weight' => -10,
       
    92     'type' => MENU_DEFAULT_LOCAL_TASK,
       
    93     'file' => 'menu.admin.inc',
       
    94   );
       
    95   $items['admin/build/menu-customize/%menu/add'] = array(
       
    96     'title' => 'Add item',
       
    97     'page callback' => 'drupal_get_form',
       
    98     'page arguments' => array('menu_edit_item', 'add', NULL, 3),
       
    99     'access arguments' => array('administer menu'),
       
   100     'type' => MENU_LOCAL_TASK,
       
   101     'file' => 'menu.admin.inc',
       
   102   );
       
   103   $items['admin/build/menu-customize/%menu/edit'] = array(
       
   104     'title' => 'Edit menu',
       
   105     'page callback' => 'drupal_get_form',
       
   106     'page arguments' => array('menu_edit_menu', 'edit', 3),
       
   107     'access arguments' => array('administer menu'),
       
   108     'type' => MENU_LOCAL_TASK,
       
   109     'file' => 'menu.admin.inc',
       
   110   );
       
   111   $items['admin/build/menu-customize/%menu/delete'] = array(
       
   112     'title' => 'Delete menu',
       
   113     'page callback' => 'menu_delete_menu_page',
       
   114     'page arguments' => array(3),
       
   115     'access arguments' => array('administer menu'),
       
   116     'type' => MENU_CALLBACK,
       
   117     'file' => 'menu.admin.inc',
       
   118   );
       
   119   $items['admin/build/menu/item/%menu_link/edit'] = array(
       
   120     'title' => 'Edit menu item',
       
   121     'page callback' => 'drupal_get_form',
       
   122     'page arguments' => array('menu_edit_item', 'edit', 4, NULL),
       
   123     'access arguments' => array('administer menu'),
       
   124     'type' => MENU_CALLBACK,
       
   125     'file' => 'menu.admin.inc',
       
   126   );
       
   127   $items['admin/build/menu/item/%menu_link/reset'] = array(
       
   128     'title' => 'Reset menu item',
       
   129     'page callback' => 'drupal_get_form',
       
   130     'page arguments' => array('menu_reset_item_confirm', 4),
       
   131     'access arguments' => array('administer menu'),
       
   132     'type' => MENU_CALLBACK,
       
   133     'file' => 'menu.admin.inc',
       
   134   );
       
   135   $items['admin/build/menu/item/%menu_link/delete'] = array(
       
   136     'title' => 'Delete menu item',
       
   137     'page callback' => 'menu_item_delete_page',
       
   138     'page arguments' => array(4),
       
   139     'access arguments' => array('administer menu'),
       
   140     'type' => MENU_CALLBACK,
       
   141     'file' => 'menu.admin.inc',
       
   142   );
       
   143 
       
   144   return $items;
       
   145 }
       
   146 
       
   147 /**
       
   148  * Implemenation of hook_theme().
       
   149  */
       
   150 function menu_theme() {
       
   151   return array(
       
   152     'menu_overview_form' => array(
       
   153       'file' => 'menu.admin.inc',
       
   154       'arguments' => array('form' => NULL),
       
   155     ),
       
   156   );
       
   157 }
       
   158 
       
   159 /**
       
   160  * Implementation of hook_enable()
       
   161  *
       
   162  *  Add a link for each custom menu.
       
   163  */
       
   164 function menu_enable() {
       
   165   menu_rebuild();
       
   166   $base_link = db_fetch_array(db_query("SELECT mlid AS plid, menu_name from {menu_links} WHERE link_path = 'admin/build/menu' AND module = 'system'"));
       
   167   $base_link['router_path'] = 'admin/build/menu-customize/%';
       
   168   $base_link['module'] = 'menu';
       
   169   $result = db_query("SELECT * FROM {menu_custom}");
       
   170   while ($menu = db_fetch_array($result)) {
       
   171     // $link is passed by reference to menu_link_save(), so we make a copy of $base_link.
       
   172     $link = $base_link;
       
   173     $link['mlid'] = 0;
       
   174     $link['link_title'] = $menu['title'];
       
   175     $link['link_path'] = 'admin/build/menu-customize/'. $menu['menu_name'];
       
   176     if (!db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_path = '%s' AND plid = %d", $link['link_path'], $link['plid']))) {
       
   177       menu_link_save($link);
       
   178     }
       
   179   }
       
   180   menu_cache_clear_all();
       
   181 }
       
   182 
       
   183 /**
       
   184  * Title callback for the menu overview page and links.
       
   185  */
       
   186 function menu_overview_title($menu) {
       
   187   return $menu['title'];
       
   188 }
       
   189 
       
   190 /**
       
   191  * Load the data for a single custom menu.
       
   192  */
       
   193 function menu_load($menu_name) {
       
   194   return db_fetch_array(db_query("SELECT * FROM {menu_custom} WHERE menu_name = '%s'", $menu_name));
       
   195 }
       
   196 
       
   197 /**
       
   198  * Return a list of menu items that are valid possible parents for the given menu item.
       
   199  *
       
   200  * @param $menus
       
   201  *   An array of menu names and titles, such as from menu_get_menus().
       
   202  * @param $item
       
   203  *   The menu item for which to generate a list of parents.
       
   204  *   If $item['mlid'] == 0 then the complete tree is returned.
       
   205  * @return
       
   206  *   An array of menu link titles keyed on the a string containing the menu name
       
   207  *   and mlid. The list excludes the given item and its children.
       
   208  */
       
   209 function menu_parent_options($menus, $item) {
       
   210   // The menu_links table can be practically any size and we need a way to
       
   211   // allow contrib modules to provide more scalable pattern choosers.
       
   212   // hook_form_alter is too late in itself because all the possible parents are
       
   213   // retrieved here, unless menu_override_parent_selector is set to TRUE.
       
   214   if (variable_get('menu_override_parent_selector', FALSE)) {
       
   215     return array();
       
   216   }
       
   217   // If the item has children, there is an added limit to the depth of valid parents.
       
   218   if (isset($item['parent_depth_limit'])) {
       
   219     $limit = $item['parent_depth_limit'];
       
   220   }
       
   221   else {
       
   222     $limit = _menu_parent_depth_limit($item);
       
   223   }
       
   224 
       
   225   foreach ($menus as $menu_name => $title) {
       
   226     $tree = menu_tree_all_data($menu_name, NULL);
       
   227     $options[$menu_name .':0'] = '<'. $title .'>';
       
   228     _menu_parents_recurse($tree, $menu_name, '--', $options, $item['mlid'], $limit);
       
   229   }
       
   230   return $options;
       
   231 }
       
   232 
       
   233 /**
       
   234  * Recursive helper function for menu_parent_options().
       
   235  */
       
   236 function _menu_parents_recurse($tree, $menu_name, $indent, &$options, $exclude, $depth_limit) {
       
   237   foreach ($tree as $data) {
       
   238     if ($data['link']['depth'] > $depth_limit) {
       
   239       // Don't iterate through any links on this level.
       
   240       break;
       
   241     }
       
   242     if ($data['link']['mlid'] != $exclude && $data['link']['hidden'] >= 0) {
       
   243       $title = $indent .' '. truncate_utf8($data['link']['title'], 30, TRUE, FALSE);
       
   244       if ($data['link']['hidden']) {
       
   245         $title .= ' ('. t('disabled') .')';
       
   246       }
       
   247       $options[$menu_name .':'. $data['link']['mlid']] = $title;
       
   248       if ($data['below']) {
       
   249         _menu_parents_recurse($data['below'], $menu_name, $indent .'--', $options, $exclude, $depth_limit);
       
   250       }
       
   251     }
       
   252   }
       
   253 }
       
   254 
       
   255 /**
       
   256  * Reset a system-defined menu item.
       
   257  */
       
   258 function menu_reset_item($item) {
       
   259   $new_item = _menu_link_build(menu_get_item($item['router_path']));
       
   260   foreach (array('mlid', 'has_children') as $key) {
       
   261     $new_item[$key] = $item[$key];
       
   262   }
       
   263   menu_link_save($new_item);
       
   264   return $new_item;
       
   265 }
       
   266 
       
   267 /**
       
   268  * Implementation of hook_block().
       
   269  */
       
   270 function menu_block($op = 'list', $delta = 0) {
       
   271   $menus = menu_get_menus();
       
   272   // The Navigation menu is handled by the user module.
       
   273   unset($menus['navigation']);
       
   274   if ($op == 'list') {
       
   275     $blocks = array();
       
   276     foreach ($menus as $name => $title) {
       
   277       // Default "Navigation" block is handled by user.module.
       
   278       $blocks[$name]['info'] = check_plain($title);
       
   279       // Menu blocks can't be cached because each menu item can have
       
   280       // a custom access callback. menu.inc manages its own caching.
       
   281       $blocks[$name]['cache'] = BLOCK_NO_CACHE;
       
   282     }
       
   283     return $blocks;
       
   284   }
       
   285   else if ($op == 'view') {
       
   286     $data['subject'] = check_plain($menus[$delta]);
       
   287     $data['content'] = menu_tree($delta);
       
   288     return $data;
       
   289   }
       
   290 }
       
   291 
       
   292 /**
       
   293  * Implementation of hook_nodeapi().
       
   294  */
       
   295 function menu_nodeapi(&$node, $op) {
       
   296   switch ($op) {
       
   297     case 'insert':
       
   298     case 'update':
       
   299       if (isset($node->menu)) {
       
   300         $item = &$node->menu;
       
   301         if (!empty($item['delete'])) {
       
   302           menu_link_delete($item['mlid']);
       
   303         }
       
   304         elseif (trim($item['link_title'])) {
       
   305           $item['link_title'] = trim($item['link_title']);
       
   306           $item['link_path'] = "node/$node->nid";
       
   307           if (!$item['customized']) {
       
   308             $item['options']['attributes']['title'] = trim($node->title);
       
   309           }
       
   310           if (!menu_link_save($item)) {
       
   311             drupal_set_message(t('There was an error saving the menu link.'), 'error');
       
   312           }
       
   313         }
       
   314       }
       
   315       break;
       
   316     case 'delete':
       
   317       // Delete all menu module links that point to this node.
       
   318       $result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = 'node/%d' AND module = 'menu'", $node->nid);
       
   319       while ($m = db_fetch_array($result)) {
       
   320         menu_link_delete($m['mlid']);
       
   321       }
       
   322       break;
       
   323     case 'prepare':
       
   324       if (empty($node->menu)) {
       
   325         // Prepare the node for the edit form so that $node->menu always exists.
       
   326         $menu_name = variable_get('menu_default_node_menu', 'primary-links');
       
   327         $item = array();
       
   328         if (isset($node->nid)) {
       
   329           // Give priority to the default menu
       
   330           $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));
       
   331           // Check all menus if a link does not exist in the default menu.
       
   332           if (!$mlid) {
       
   333             $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));
       
   334           }
       
   335           if ($mlid) {
       
   336             $item = menu_link_load($mlid);
       
   337           }
       
   338         }
       
   339         // Set default values.
       
   340         $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);
       
   341       }
       
   342       // Find the depth limit for the parent select.
       
   343       if (!isset($node->menu['parent_depth_limit'])) {
       
   344         $node->menu['parent_depth_limit'] = _menu_parent_depth_limit($node->menu);
       
   345       }
       
   346       break;
       
   347   }
       
   348 }
       
   349 
       
   350 /**
       
   351  * Find the depth limit for items in the parent select.
       
   352  */
       
   353 function _menu_parent_depth_limit($item) {
       
   354   return MENU_MAX_DEPTH - 1 - (($item['mlid'] && $item['has_children']) ? menu_link_children_relative_depth($item) : 0);
       
   355 }
       
   356 
       
   357 /**
       
   358  * Implementation of hook_form_alter(). Adds menu item fields to the node form.
       
   359  */
       
   360 function menu_form_alter(&$form, $form_state, $form_id) {
       
   361   if (isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) {
       
   362     // Note - doing this to make sure the delete checkbox stays in the form.
       
   363     $form['#cache'] = TRUE;
       
   364 
       
   365     $form['menu'] = array(
       
   366       '#type' => 'fieldset',
       
   367       '#title' => t('Menu settings'),
       
   368       '#access' => user_access('administer menu'),
       
   369       '#collapsible' => TRUE,
       
   370       '#collapsed' => FALSE,
       
   371       '#tree' => TRUE,
       
   372       '#weight' => -2,
       
   373       '#attributes' => array('class' => 'menu-item-form'),
       
   374     );
       
   375     $item = $form['#node']->menu;
       
   376 
       
   377     if ($item['mlid']) {
       
   378       // There is an existing link.
       
   379       $form['menu']['delete'] = array(
       
   380         '#type' => 'checkbox',
       
   381         '#title' => t('Delete this menu item.'),
       
   382       );
       
   383     }
       
   384     if (!$item['link_title']) {
       
   385       $form['menu']['#collapsed'] = TRUE;
       
   386     }
       
   387 
       
   388     foreach (array('mlid', 'module', 'hidden', 'has_children', 'customized', 'options', 'expanded', 'hidden', 'parent_depth_limit') as $key) {
       
   389       $form['menu'][$key] = array('#type' => 'value', '#value' => $item[$key]);
       
   390     }
       
   391     $form['menu']['#item'] = $item;
       
   392 
       
   393     $form['menu']['link_title'] = array('#type' => 'textfield',
       
   394       '#title' => t('Menu link title'),
       
   395       '#default_value' => $item['link_title'],
       
   396       '#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.'),
       
   397       '#required' => FALSE,
       
   398     );
       
   399     // Generate a list of possible parents (not including this item or descendants).
       
   400     $options = menu_parent_options(menu_get_menus(), $item);
       
   401     $default = $item['menu_name'] .':'. $item['plid'];
       
   402     if (!isset($options[$default])) {
       
   403       $default = 'primary-links:0';
       
   404     }
       
   405     $form['menu']['parent'] = array(
       
   406       '#type' => 'select',
       
   407       '#title' => t('Parent item'),
       
   408       '#default_value' => $default,
       
   409       '#options' => $options,
       
   410       '#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)),
       
   411       '#attributes' => array('class' => 'menu-title-select'),
       
   412     );
       
   413     $form['#submit'][] = 'menu_node_form_submit';
       
   414 
       
   415     $form['menu']['weight'] = array(
       
   416       '#type' => 'weight',
       
   417       '#title' => t('Weight'),
       
   418       '#delta' => 50,
       
   419       '#default_value' => $item['weight'],
       
   420       '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
       
   421     );
       
   422   }
       
   423 }
       
   424 
       
   425 /**
       
   426  * Decompose the selected menu parent option into the menu_name and plid.
       
   427  */
       
   428 function menu_node_form_submit($form, &$form_state) {
       
   429   list($form_state['values']['menu']['menu_name'], $form_state['values']['menu']['plid']) = explode(':', $form_state['values']['menu']['parent']);
       
   430 }
       
   431 
       
   432 /**
       
   433  * Return an associative array of the custom menus names.
       
   434  *
       
   435  * @param $all
       
   436  *   If FALSE return only user-added menus, or if TRUE also include
       
   437  *   the menus defined by the system.
       
   438  * @return
       
   439  *   An array with the machine-readable names as the keys, and human-readable
       
   440  *   titles as the values.
       
   441  */
       
   442 function menu_get_menus($all = TRUE) {
       
   443   $system_menus = menu_list_system_menus();
       
   444   $sql = 'SELECT * FROM {menu_custom}';
       
   445   if (!$all) {
       
   446     $sql .= ' WHERE menu_name NOT IN ('. implode(',', array_fill(0, count($system_menus), "'%s'")) .')';
       
   447   }
       
   448   $sql .= ' ORDER BY title';
       
   449   $result = db_query($sql, $system_menus);
       
   450   $rows = array();
       
   451   while ($r = db_fetch_array($result)) {
       
   452     $rows[$r['menu_name']] = $r['title'];
       
   453   }
       
   454   return $rows;
       
   455 }