[ Index ]
 

Code source de Drupal 5.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/modules/menu/ -> menu.module (source)

   1  <?php
   2  // $Id: menu.module,v 1.100.2.1 2007/07/26 19:16:45 drumm Exp $
   3  
   4  /**
   5   * @file
   6   * Allows administrators to customize the site navigation menu.
   7   */
   8  
   9  /**
  10   * Implementation of hook_help().
  11   */
  12  function menu_help($section) {
  13    switch ($section) {
  14      case 'admin/help#menu':
  15        $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>
  16  <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    <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>
  20    <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>
  21    <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  </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')));
  23        $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   * Implementation of hook_menu().
  36   */
  37  function menu_menu($may_cache) {
  38    $items = array();
  39  
  40    if ($may_cache) {
  41      $items[] = array('path' => 'admin/build/menu',
  42        'title' => t('Menus'),
  43        'description' => t("Control your site's navigation menu, primary links and secondary links. as well as rename and reorganize menu items."),
  44        'callback' => 'menu_overview',
  45        'access' => user_access('administer menu'));
  46      $items[] = array('path' => 'admin/build/menu/list',
  47        'title' => t('List'),
  48        'type' => MENU_DEFAULT_LOCAL_TASK,
  49        'weight' => -10);
  50  
  51      $items[] = array('path' => 'admin/build/menu/item/add',
  52        'title' => t('Add menu item'),
  53        'callback' => 'drupal_get_form',
  54        'callback arguments' => array('menu_edit_item_form', 'add'),
  55        'access' => user_access('administer menu'),
  56        'type' => MENU_LOCAL_TASK);
  57      $items[] = array('path' => 'admin/build/menu/item/edit',
  58        'title' => t('Edit menu item'),
  59        'callback' => 'drupal_get_form',
  60        'callback arguments' => array('menu_edit_item_form', 'edit'),
  61        'access' => user_access('administer menu'),
  62        'type' => MENU_CALLBACK);
  63      $items[] = array('path' => 'admin/build/menu/item/reset',
  64        'title' => t('Reset menu item'),
  65        'callback' => 'drupal_get_form',
  66        'callback arguments' => array('menu_reset_item'),
  67        'access' => user_access('administer menu'),
  68        'type' => MENU_CALLBACK);
  69      $items[] = array('path' => 'admin/build/menu/item/disable',
  70        'title' => t('Disable menu item'),
  71        'callback' => 'drupal_get_form',
  72        'callback arguments' => array('menu_confirm_disable_item'),
  73        'access' => user_access('administer menu'),
  74        'type' => MENU_CALLBACK);
  75      $items[] = array('path' => 'admin/build/menu/item/delete',
  76        'title' => t('Delete menu item'),
  77        'callback' => 'drupal_get_form',
  78        'callback arguments' => array('menu_item_delete_form'),
  79        'access' => user_access('administer menu'),
  80        'type' => MENU_CALLBACK);
  81  
  82      $items[] = array('path' => 'admin/build/menu/menu/add',
  83        'title' => t('Add menu'),
  84        'callback' => 'drupal_get_form',
  85        'callback arguments' => array('menu_edit_menu_form', 'add'),
  86        'access' => user_access('administer menu'),
  87        'type' => MENU_LOCAL_TASK);
  88      $items[] = array('path' => 'admin/build/menu/menu/edit',
  89        'title' => t('Edit menu'),
  90        'callback' => 'drupal_get_form',
  91        'callback arguments' => array('menu_edit_menu_form', 'edit'),
  92        'access' => user_access('administer menu'),
  93        'type' => MENU_CALLBACK);
  94      $items[] = array('path' => 'admin/build/menu/menu/delete',
  95        'title' => t('Delete menu'),
  96        'callback' => 'drupal_get_form',
  97        'callback arguments' => array('menu_item_delete_form'),
  98        'access' => user_access('administer menu'),
  99        'type' => MENU_CALLBACK);
 100  
 101      $items[] = array('path' => 'admin/build/menu/settings',
 102        'title' => t('Settings'),
 103        'callback' => 'drupal_get_form',
 104        'callback arguments' => array('menu_configure'),
 105        'type' => MENU_LOCAL_TASK,
 106        'weight' => 5,
 107      );
 108    }
 109  
 110    return $items;
 111  }
 112  
 113  /**
 114   * Implementation of hook_block().
 115   */
 116  function menu_block($op = 'list', $delta = 0) {
 117    if ($op == 'list') {
 118      $blocks = array();
 119      $root_menus = menu_get_root_menus();
 120      foreach ($root_menus as $mid => $title) {
 121        // Default "Navigation" block is handled by user.module.
 122        if ($mid != 1) {
 123          $blocks[$mid]['info'] = $title;
 124        }
 125      }
 126      return $blocks;
 127    }
 128    else if ($op == 'view') {
 129      $item = menu_get_item($delta);
 130      $data['subject'] = check_plain($item['title']);
 131      $data['content'] = theme('menu_tree', $delta);
 132      return $data;
 133    }
 134  }
 135  
 136  /**
 137   * Implementation of hook_nodeapi().
 138   */
 139  function menu_nodeapi(&$node, $op) {
 140  
 141    if (user_access('administer menu')) {
 142      switch ($op) {
 143        case 'insert':
 144        case 'update':
 145          if ($node->menu['delete']) {
 146            menu_node_form_delete($node);
 147            menu_rebuild();
 148          }
 149          elseif ($node->menu['title']) {
 150            $node->menu['path'] = ($node->menu['path']) ? $node->menu['path'] : "node/$node->nid";
 151            menu_edit_item_save($node->menu);
 152            menu_rebuild();
 153          }
 154          break;
 155  
 156        case 'delete':
 157          menu_node_form_delete($node);
 158          menu_rebuild();
 159          break;
 160      }
 161    }
 162  }
 163  
 164  /**
 165   * Implementation of hook_perm().
 166   */
 167  function menu_perm() {
 168    return array('administer menu');
 169  }
 170  
 171  /**
 172   * Implementation of hook_form_alter().
 173   * Add menu item fields to the node form.
 174   */
 175  function menu_form_alter($form_id, &$form) {
 176    if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
 177      $item = array();
 178      if ($form['nid']['#value'] > 0) {
 179        $item = db_fetch_array(db_query("SELECT * FROM {menu} WHERE path = 'node/%d'", $form['nid']['#value']));
 180        if (isset($form['#post']['menu']) && is_array($form['#post']['menu'])) {
 181          $item = !is_array($item) ? $form['#post']['menu'] : (($form['#post']['op'] == t('Preview')) ? array_merge($item, $form['#post']['menu']) : array_merge($form['#post']['menu'], $item));
 182        }
 183      }
 184  
 185      $form['menu'] = array('#type' => 'fieldset',
 186        '#title' => t('Menu settings'),
 187        '#access' => user_access('administer menu'),
 188        '#collapsible' => TRUE,
 189        '#collapsed' => empty($item['title']),
 190        '#tree' => TRUE,
 191        '#weight' => 30,
 192      );
 193  
 194      $form['menu']['title'] = array('#type' => 'textfield',
 195        '#title' => t('Title'),
 196        '#default_value' => $item['title'],
 197        '#description' => t('The name to display for this menu link.'),
 198      );
 199  
 200      $form['menu']['description'] = array('#type' => 'textfield',
 201        '#title' => t('Description'),
 202        '#default_value' => $item['description'],
 203        '#description' => t('The description displayed when hovering over a menu item.'),
 204      );
 205  
 206      // Generate a list of possible parents.
 207      $options = menu_parent_options($item['mid'], variable_get('menu_parent_items', 0));
 208  
 209      $form['menu']['pid'] = array('#type' => 'select',
 210        '#title' => t('Parent item'),
 211        '#default_value' => $item['pid'],
 212        '#options' => $options,
 213      );
 214  
 215      $form['menu']['path'] = array('#type' => 'hidden',
 216        '#value' => $item['path'],
 217      );
 218  
 219      $form['menu']['weight'] = array('#type' => 'weight',
 220        '#title' => t('Weight'),
 221        '#default_value' => $item['weight'],
 222        '#delta' => 10,
 223        '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
 224      );
 225  
 226      $form['menu']['mid'] = array('#type' => 'hidden',
 227        '#value' => $item['mid'] ? $item['mid'] : 0,
 228      );
 229  
 230      $form['menu']['type'] = array('#type' => 'hidden',
 231        '#value' => $item['type'] ? $item['type'] : MENU_CUSTOM_ITEM,
 232      );
 233  
 234      if ($item['mid'] > 0) {
 235        $form['menu']['delete'] = array('#type' => 'checkbox',
 236          '#title' => t('Check to delete this menu item.'),
 237          '#default_value' => $item['delete'],
 238        );
 239  
 240        $form['menu']['advanced'] = array('#type' => 'item',
 241          '#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']}"))),
 242        );
 243      }
 244    }
 245  }
 246  
 247  /**
 248   * Menu callback; presents menu configuration options.
 249   */
 250  function menu_configure() {
 251    $root_menus = menu_get_root_menus();
 252  
 253    $primary_options = $root_menus;
 254    $primary_options[0] = t('No primary links');
 255  
 256    $form['settings_links'] = array('#type' => 'fieldset',
 257      '#title' => t('Primary and secondary links settings'),
 258    );
 259  
 260    $form['settings_links']['intro'] = array('#type' => 'item',
 261      '#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'))),
 262    );
 263  
 264    $form['settings_links']['menu_primary_menu'] = array('#type' => 'select',
 265      '#title' => t('Menu containing primary links'),
 266      '#default_value' => variable_get('menu_primary_menu', 0),
 267      '#options' => $primary_options,
 268    );
 269  
 270    $secondary_options = $root_menus;
 271    $secondary_options[0] = t('No secondary links');
 272  
 273    $form['settings_links']['menu_secondary_menu'] = array('#type' => 'select',
 274      '#title' => t('Menu containing secondary links'),
 275      '#default_value' => variable_get('menu_secondary_menu', 0),
 276      '#options' => $secondary_options,
 277      '#description' => t('If you select the same menu as primary links then secondary links will display the appropriate second level of your navigation hierarchy.'),
 278    );
 279  
 280    $form['settings_authoring'] = array('#type' => 'fieldset',
 281      '#title' => t('Content authoring form settings'),
 282    );
 283  
 284    $form['settings_authoring']['intro'] = array('#type' => 'item',
 285      '#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.'),
 286    );
 287  
 288    $authoring_options = $root_menus;
 289    $authoring_options[0] = t('Show all menus');
 290  
 291    $form['settings_authoring']['menu_parent_items'] = array('#type' => 'select',
 292      '#title' => t('Restrict parent items to'),
 293      '#default_value' => variable_get('menu_parent_items', 0),
 294      '#options' => $authoring_options,
 295      '#description' => t('Choose the menu to be made available in the content authoring form. Only this menu item and its children will be shown.'),
 296     );
 297  
 298    return system_settings_form($form);
 299  }
 300  
 301  /**
 302   * Menu callback; handle the adding/editing of a new menu.
 303   */
 304  function menu_edit_menu_form($type, $mid = 0) {
 305    if ($type == 'edit') {
 306      if (!($item = db_fetch_array(db_query('SELECT * FROM {menu} WHERE mid = %d', $mid)))) {
 307        drupal_not_found();
 308        return;
 309      }
 310    }
 311    else {
 312      $item = array('mid' => 0, 'pid' => 0, 'path' => '', 'weight' => 0, 'type' => MENU_CUSTOM_MENU);
 313    }
 314    $form['title'] = array('#type' => 'textfield',
 315      '#title' => t('Title'),
 316      '#default_value' => $item['title'],
 317      '#description' => t('The name of the menu.'),
 318      '#required' => TRUE,
 319    );
 320    $form['mid'] = array('#type' => 'value', '#value' => $item['mid']);
 321    $form['pid'] = array('#type' => 'value', '#value' => $item['pid']);
 322    $form['path'] = array('#type' => 'value', '#value' => $item['path']);
 323    $form['weight'] = array('#type' => 'value', '#value' => $item['weight']);
 324    $form['type'] = array('#type' => 'value', '#value' => $item['type']);
 325    $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
 326    // Reuse the submit function of menu_edit_item_form.
 327    $form['#base'] = 'menu_edit_item_form';
 328  
 329    return $form;
 330  }
 331  
 332  /**
 333   * Present the menu item editing form.
 334   */
 335  function menu_edit_item_form($type, $mid = 0) {
 336    if ($type == 'edit') {
 337      if (!($item = db_fetch_array(db_query('SELECT * FROM {menu} WHERE mid = %d', $mid)))) {
 338        drupal_not_found();
 339        return;
 340      }
 341    }
 342    else {
 343      // This is an add form.
 344      // The mid argument (if set) will be the default pid to use.
 345      // Otherwise, we default to the "Navigation" menu (pid #1).
 346      $default_pid = $mid ? $mid : 1;
 347      $item = array('mid' => 0, 'pid' => $default_pid, 'weight' => 0, 'type' => MENU_CUSTOM_ITEM);
 348    }
 349  
 350    $form['title'] = array('#type' => 'textfield',
 351      '#title' => t('Title'),
 352      '#default_value' => $item['title'],
 353      '#description' => t('The name of the menu item.'),
 354      '#required' => TRUE,
 355    );
 356    $form['description'] = array('#type' => 'textfield',
 357      '#title' => t('Description'),
 358      '#default_value' => $item['description'],
 359      '#description' => t('The description displayed when hovering over a menu item.'),
 360    );
 361  
 362    if ($item['type'] & MENU_CREATED_BY_ADMIN) {
 363      $form['path'] = array('#type' => 'textfield',
 364        '#title' => t('Path'),
 365        '#default_value' => $item['path'],
 366        '#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')),
 367        '#required' => TRUE,
 368      );
 369    }
 370    else {
 371      $form['_path'] = array('#type' => 'item',
 372        '#title' => t('Path'),
 373        '#description' => l($item['path'], $item['path']),
 374      );
 375      $form['path'] = array('#type' => 'value', '#value' => $item['path']);
 376    }
 377  
 378    $expanded = $item['type'] & MENU_EXPANDED ? 1 : 0;
 379    $form['expanded'] = array('#type' => 'checkbox',
 380      '#title' => t('Expanded'),
 381      '#default_value' => $expanded,
 382      '#description' => t('If selected and this menu item has children, the menu will always appear expanded.'),
 383    );
 384  
 385    // Generate a list of possible parents (not including this item or descendants).
 386    $options = menu_parent_options($item['mid']);
 387    $form['pid'] = array('#type' => 'select',
 388      '#title' => t('Parent item'),
 389      '#default_value' => $item['pid'],
 390      '#options' => $options,
 391    );
 392    $form['weight'] = array('#type' => 'weight',
 393      '#title' => t('Weight'),
 394      '#default_value' => $item['weight'],
 395      '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
 396    );
 397  
 398    // Always enable menu items (but not menus) when editing them.
 399    if (!($item['type'] & MENU_IS_ROOT)) {
 400      $item['type'] |= MENU_VISIBLE_IN_TREE | MENU_VISIBLE_IN_BREADCRUMB;
 401    }
 402  
 403    $form['type'] = array('#type' => 'value', '#value' => $item['type']);
 404    $form['mid'] = array('#type' => 'value', '#value' => $item['mid']);
 405    $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
 406  
 407    return $form;
 408  }
 409  
 410  /**
 411   * Process menu and menu item add/edit form submissions.
 412   */
 413  function menu_edit_item_form_submit($form_id, $form_values) {
 414    menu_edit_item_save($form_values);
 415    return 'admin/build/menu';
 416  }
 417  
 418  /**
 419   * Menu callback; delete a single custom item.
 420   */
 421  function menu_item_delete_form($mid) {
 422    if (!($menu = db_fetch_object(db_query('SELECT type, title FROM {menu} WHERE mid = %d', $mid)))) {
 423      drupal_not_found();
 424      return;
 425    }
 426  
 427    $form['mid'] = array('#type' => 'value', '#value' => $mid);
 428    $form['type'] = array('#type' => 'value', '#value' => $menu->type);
 429    $form['title'] = array('#type' => 'value', '#value' => $menu->title);
 430  
 431    if ($menu->type & MENU_IS_ROOT) {
 432      $message = t('Are you sure you want to delete the menu %item?', array('%item' => $menu->title));
 433    }
 434    else {
 435      $message = t('Are you sure you want to delete the custom menu item %item?', array('%item' => $menu->title));
 436    }
 437  
 438    return confirm_form($form, $message, 'admin/build/menu', t('This action cannot be undone.'), t('Delete'));
 439  }
 440  
 441  /**
 442   * Process menu delete form submissions.
 443   */
 444  function menu_item_delete_form_submit($form_id, $form_values) {
 445    menu_delete_item($form_values['mid']);
 446  
 447    $t_args = array('%title' => $form_values['title']);
 448    if ($form_values['type'] & MENU_IS_ROOT) {
 449      drupal_set_message(t('The menu %title has been deleted.', $t_args));
 450      watchdog('menu', t('Deleted menu %title.', $t_args), WATCHDOG_NOTICE);
 451    }
 452    else {
 453      drupal_set_message(t('The menu item %title has been deleted.', $t_args));
 454      watchdog('menu', t('Deleted menu item %title.', $t_args), WATCHDOG_NOTICE);
 455    }
 456  
 457    return 'admin/build/menu';
 458  }
 459  
 460  /**
 461   * Menu callback; reset a single modified item.
 462   */
 463  function menu_reset_item($mid) {
 464    if (isset($mid) && $title = db_result(db_query('SELECT title FROM {menu} WHERE mid = %d', $mid))) {
 465      $form['mid'] = array('#type' => 'value', '#value' => $mid);
 466      return confirm_form($form, t('Are you sure you want to reset the item %item to its default values?', array('%item' => $title)), 'admin/build/menu', t('Any customizations will be lost. This action cannot be undone.'), t('Reset'));
 467    }
 468    else {
 469      drupal_not_found();
 470    }
 471  }
 472  
 473  /**
 474   * Process menu reset item form submissions.
 475   */
 476  function menu_reset_item_submit($form_id, $form_values) {
 477    menu_delete_item($form_values['mid']);
 478    drupal_set_message(t('The menu item was reset to its default settings.'));
 479  
 480    return 'admin/build/menu';
 481  }
 482  
 483  /**
 484   * Menu callback; hide a menu item.
 485   *
 486   * Presents a confirmation form to protect against cross site request forgeries.
 487   */
 488  function menu_confirm_disable_item($mid, $token = NULL) {
 489    global $user;
 490    $item = menu_get_item($mid);
 491    $form = array();
 492    $form['mid'] = array('#type' => 'value', '#value' => $mid);
 493    $form['item'] = array('#type' => 'value', '#value' => $item);
 494    return confirm_form($form, t('Are you sure you want to disable the menu item %menu-item?', array('%menu-item' =>  $item['title'])), 'admin/build/menu', ' ', t('Disable'), t('Cancel'));
 495  }
 496  
 497  function menu_confirm_disable_item_submit($form_id, $form_values) {
 498    $type = $form_values['item']['type'];
 499    $type &= ~MENU_VISIBLE_IN_TREE;
 500    $type &= ~MENU_VISIBLE_IN_BREADCRUMB;
 501    $type |= MENU_MODIFIED_BY_ADMIN;
 502    db_query('UPDATE {menu} SET type = %d WHERE mid = %d', $type, $form_values['mid']);
 503    drupal_set_message(t('The menu item has been disabled.'));
 504    drupal_goto('admin/build/menu');
 505  }
 506  
 507  /**
 508   * Menu callback; present the main menu management page.
 509   */
 510  function menu_overview() {
 511    menu_rebuild();
 512  
 513    return menu_overview_tree();
 514  }
 515  
 516  /**
 517   * Save changes to a menu item into the database.
 518   *
 519   * @return mid
 520   */
 521  function menu_edit_item_save($edit) {
 522    if (isset($edit['expanded'])) {
 523      if ($edit['expanded']) {
 524        $edit['type'] |= MENU_EXPANDED;
 525      }
 526      else {
 527        $edit['type'] &= ~MENU_EXPANDED;
 528      }
 529    }
 530  
 531    $edit['type'] = $edit['type'] | MENU_MODIFIED_BY_ADMIN;
 532  
 533    $status = menu_save_item($edit);
 534  
 535    $t_args = array('%title' => $edit['title']);
 536    if ($status == SAVED_UPDATED) {
 537      drupal_set_message(t('The menu item %title has been updated.', $t_args));
 538    }
 539    elseif ($status == SAVED_NEW) {
 540      drupal_set_message(t('The menu item %title has been added.', $t_args));
 541      watchdog('menu', t('Added menu item %title.', $t_args), WATCHDOG_NOTICE, l(t('view'), 'admin/build/menu'));
 542    }
 543    return $edit['mid'];
 544  }
 545  
 546  /**
 547   * Save a menu item to the database.
 548   *
 549   * @param $item
 550   *   The menu item to be saved. This is passed by reference, so that the newly
 551   *   generated $item['mid'] can be accessed after an insert takes place.
 552   *
 553   * @return $status
 554   *   The operation that was performed in saving. Either SAVED_NEW (if a new
 555   *   menu item was created), or SAVED_UPDATED (if an existing menu item was
 556   *   updated).
 557   */
 558  function menu_save_item(&$item) {
 559    $existing_item = NULL;
 560  
 561    // Check that the item already exists in the menu tree, if $item['mid'] is
 562    // specified.
 563    if (isset($item['mid'])) {
 564      $existing_item = menu_get_item($item['mid']);
 565    }
 566  
 567    if ($item['mid'] && !empty($existing_item)) {
 568      db_query("UPDATE {menu} SET pid = %d, path = '%s', title = '%s', description = '%s', weight = %d, type = %d WHERE mid = %d", $item['pid'], $item['path'], $item['title'], $item['description'], $item['weight'], $item['type'], $item['mid']);
 569      return SAVED_UPDATED;
 570    }
 571    else {
 572      $item['mid'] = db_next_id('{menu}_mid');
 573      // Check explicitly for mid <= 2. If the database was improperly prefixed,
 574      // this would cause a nasty infinite loop or duplicate mid errors.
 575      // TODO: have automatic prefixing through an installer to prevent this.
 576      while ($item['mid'] <= 2) {
 577        $item['mid'] = db_next_id('{menu}_mid');
 578      }
 579      db_query("INSERT INTO {menu} (mid, pid, path, title, description, weight, type) VALUES (%d, %d, '%s', '%s', '%s', %d, %d)", $item['mid'], $item['pid'], $item['path'], $item['title'], $item['description'], $item['weight'], $item['type']);
 580      return SAVED_NEW;
 581    }
 582  }
 583  
 584  /**
 585   * Delete a menu item from the database. If $item['mid'] is specified, then
 586   * this is used to find the existing item; otherwise, $item['path'] is used.
 587   *
 588   * @param $item
 589   *   The menu item to be deleted.
 590   */
 591  function menu_delete_item($item) {
 592    if (!is_array($item)) {
 593      $item = array('mid' => $item);
 594    }
 595  
 596    if ($item['mid']) {
 597      db_query('DELETE FROM {menu} WHERE mid = %d', $item['mid']);
 598    }
 599    elseif ($item['path']) {
 600      db_query("DELETE FROM {menu} WHERE path = '%s'", $item['path']);
 601    }
 602  }
 603  
 604  /**
 605   * Present the menu tree, rendered along with links to edit menu items.
 606   */
 607  function menu_overview_tree() {
 608    $menu = menu_get_menu();
 609    $root_menus = menu_get_root_menus();
 610    $header = array(t('Menu item'), t('Expanded'), array('data' => t('Operations'), 'colspan' => '3'));
 611    $output = '';
 612  
 613    // We reverse the root menu to show user created menus first.
 614    foreach (array_reverse($root_menus, true) as $mid => $title) {
 615      $operations = array();
 616      if ($menu['items'][$mid]['type'] & MENU_MODIFIABLE_BY_ADMIN) {
 617        $operations[] = l(t('Edit'), 'admin/build/menu/menu/edit/'. $mid);
 618      }
 619      if ($menu['items'][$mid]['type'] & MENU_CREATED_BY_ADMIN) {
 620        $operations[] = l(t('Delete'), 'admin/build/menu/menu/delete/'. $mid);
 621      }
 622      $operations[] = l(t('Add item'), 'admin/build/menu/item/add/'. $mid);
 623      $table = theme('item_list', $operations);
 624  
 625      $rows = menu_overview_tree_rows($mid);
 626      $table .= theme('table', $header, $rows ? $rows : array(array(array('data' => t('No menu items defined.'), 'colspan' => 5))));
 627  
 628      $output .= theme('box', check_plain($title), $table);
 629    }
 630    return $output;
 631  }
 632  
 633  function menu_overview_tree_rows($pid = 0, $depth = 0) {
 634    $parent_item = menu_get_item($pid);
 635    $rows = array();
 636  
 637    if (isset($parent_item) && isset($parent_item['children'])) {
 638      usort($parent_item['children'], '_menu_sort');
 639      foreach ($parent_item['children'] as $mid) {
 640        $item = menu_get_item($mid);
 641        // Populate the title field.
 642        $title = '';
 643        if ($pid == 0) {
 644          // Top-level items are menu names, and don't have an associated path.
 645          $title .= check_plain($item['title']);
 646        }
 647        else {
 648          $title .= l($item['title'], $item['path']);
 649        }
 650        if ($depth > 0) {
 651          $title = '-&nbsp;'. $title;
 652        }
 653        for ($i = 1; $i < $depth; $i++) {
 654          $title = '&nbsp;&nbsp;'. $title;
 655        }
 656  
 657        // Populate the operations field.
 658        $operations = array();
 659        if (!($item['type'] & MENU_MODIFIABLE_BY_ADMIN)) {
 660          $operations[] = array('data' => t('locked'), 'colspan' => '3', 'align' => 'center');
 661        }
 662        else {
 663          // Set the edit column.
 664          if ($item['type'] & (MENU_VISIBLE_IN_TREE | MENU_VISIBLE_IF_HAS_CHILDREN)) {
 665            $operations[] = array('data' => l(t('edit'), 'admin/build/menu/item/edit/'. $mid));
 666          }
 667          else {
 668            $operations[] = array('data' => '');
 669          }
 670  
 671          // Set the disable column.
 672          if ($item['type'] & (MENU_IS_ROOT | MENU_VISIBLE_IF_HAS_CHILDREN)) {
 673            // Disabling entire menus is done from block admin page.
 674            // MENU_VISIBLE_IF_HAS_CHILDREN menus are always enabled so hide this operation.
 675            $operations[] = array('data' => '');
 676          }
 677          else if ($item['type'] & MENU_VISIBLE_IN_TREE) {
 678            $operations[] = array('data' => l(t('disable'), 'admin/build/menu/item/disable/'. $mid));
 679          }
 680          else {
 681            $operations[] = array('data' => l(t('enable'), 'admin/build/menu/item/edit/'. $mid));
 682          }
 683  
 684          // Set the reset column.
 685          if ($item['type'] & MENU_CREATED_BY_ADMIN) {
 686            $operations[] = array('data' => l(t('delete'), 'admin/build/menu/item/delete/'. $mid));
 687          }
 688          else if ($item['type'] & MENU_MODIFIED_BY_ADMIN) {
 689            $operations[] = array('data' => l(t('reset'), 'admin/build/menu/item/reset/'. $mid));
 690          }
 691          else {
 692            $operations[] = array('data' => '');
 693          }
 694        }
 695  
 696        // Call out disabled items.
 697        if ($item['type'] & (MENU_VISIBLE_IN_TREE | MENU_VISIBLE_IF_HAS_CHILDREN)) {
 698          $class = 'menu-enabled';
 699        }
 700        else {
 701          $title .= ' ('. t('disabled') .')';
 702          $class = 'menu-disabled';
 703        }
 704  
 705        if ($item['type'] & (MENU_MODIFIABLE_BY_ADMIN | MENU_VISIBLE_IN_TREE)) {
 706          $row = array(array('data' => $title, 'class' => $class), array('data' => ($item['children'] ? (($item['type'] & MENU_EXPANDED) ? t('Yes') : t('No')) : ''), 'class' => $class));
 707          foreach ($operations as $operation) {
 708            $operation['class'] = $class;
 709            $row[] = $operation;
 710          }
 711          $rows[] = $row;
 712          $rows = array_merge($rows, menu_overview_tree_rows($mid, $depth + 1));
 713        }
 714        else {
 715          // Skip items that are hidden and locked; admins will never care about them.
 716          $rows = array_merge($rows, menu_overview_tree_rows($mid, $depth));
 717        }
 718      }
 719    }
 720  
 721    return $rows;
 722  }
 723  
 724  /**
 725   * Return a list of menu items that are valid possible parents for the
 726   * given menu item. The list excludes the given item and its children.
 727   *
 728   * @param $mid
 729   *   The menu item id for which to generate a list of parents.
 730   *   If $mid == 0 then the complete tree is returned.
 731   * @param $pid
 732   *   The menu item id of the menu item at which to start the tree.
 733   *   If $pid > 0 then this item will be included in the tree.
 734   * @param $depth
 735   *   The current depth in the tree - used when recursing to indent the tree.
 736   * @return
 737   *   An array of menu titles keyed on the mid.
 738   */
 739  function menu_parent_options($mid, $pid = 0, $depth = 0) {
 740    $options = array();
 741  
 742    if (!($parent_item = menu_get_item($pid))) {
 743      return $options;
 744    }
 745  
 746    // Exclude $mid and its children from the list unless $mid is 0.
 747    if ($mid && $mid == $pid) {
 748      return $options;
 749    }
 750  
 751    // Add the current $pid to the list.
 752    if ($pid > 0 && ($parent_item['type'] & (MENU_MODIFIABLE_BY_ADMIN | MENU_IS_ROOT))) {
 753      $title = ' '. $parent_item['title'];
 754      for ($i = 0; $i < $depth; $i++) {
 755        $title = '--'. $title;
 756      }
 757      if (!($parent_item['type'] & MENU_VISIBLE_IN_TREE)) {
 758        $title .= ' ('. t('disabled') .')';
 759      }
 760      $options[$pid] = $title;
 761      $depth ++;
 762    }
 763  
 764    // Add children of $pid to the list recursively.
 765    if (isset($parent_item['children'])) {
 766      usort($parent_item['children'], '_menu_sort');
 767      foreach ($parent_item['children'] as $child) {
 768        $options += menu_parent_options($mid, $child, $depth);
 769      }
 770    }
 771  
 772    return $options;
 773  }
 774  
 775  /**
 776   * Remove the menu item.
 777   */
 778  function menu_node_form_delete($node) {
 779    menu_delete_item(array('path' => 'node/'. $node->nid));
 780  }


Généré le : Fri Nov 30 16:20:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics