source: sipes/cord/modules/menu/menu.module @ 8a8efa8

stableversion-3.0
Last change on this file since 8a8efa8 was d7a822e, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se agrego el directorio del cord

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