[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/nice_menus/ -> nice_menus.module (source)

   1  <?php
   2  // $Id: nice_menus.module,v 1.35.2.15 2008/11/30 19:47:53 add1sun Exp $
   3  /**
   4   * @file
   5   *  Module to enable CSS dropdown and flyout menus.
   6   *  
   7   * Maintainer: Addison Berry (add1sun)
   8   * Originally written by Jake Gordon (jakeg)
   9   */
  10  
  11  /**
  12   * Implementation of hook_help().
  13   */
  14  function nice_menus_help($path, $arg) {
  15    $output = '';
  16    switch ($path) {
  17      case 'admin/settings/modules#description':
  18        $output .= t('Make drop down/flyout CSS menus for site and admin menus.');
  19        break;
  20      case 'admin/settings/nice_menus':
  21        $output .= t('<p>This is a simple module that enables the site to have drop down/flyout CSS menus for site and admin navigation.</p><p>Remember to activate and configure the menu blocks in !link</p>', array('!link' => l('admin/build/block', 'admin/build/block')));
  22        break;
  23    }
  24    return $output;
  25  }
  26  
  27  /**
  28   * Implementation of hook_form_alter().
  29   */
  30  function nice_menus_form_alter(&$form, $form_state, $form_id) {
  31    switch ($form_id) {
  32      case 'system_theme_settings':
  33  
  34        // This is a global setting, so only insert the field
  35        // on the global settings page.
  36        if (arg(4)) {
  37          return;
  38        }
  39  
  40        // Have to add a custom submit handler since this form doesn't use
  41        // the standard system submit handler.
  42        $form['#submit'][] = 'nice_menus_system_theme_settings_submit';
  43  
  44        // Add global theme setting for a custom CSS file.
  45        $form['nice_menus_custom_css'] = array(
  46          '#type' => 'textfield',
  47          '#title' => t('Path to custom Nice Menus CSS file'),
  48          '#description' => t('To override the default Nice Menus CSS layout, enter the path to your custom CSS file.  It should be a relative path from the root of your Drupal install (e.g. sites/all/themes/example/mymenu.css).'),
  49          '#default_value' => variable_get('nice_menus_custom_css', ''),
  50          // Field appears below submit buttons without this -- yucky.
  51          '#weight' => 0,
  52        );
  53        break;
  54    }
  55  }
  56  
  57  /**
  58   * Records the nice menu custom CSS file per theme.
  59   */
  60  function nice_menus_system_theme_settings_submit($form, &$form_state) {
  61    variable_set('nice_menus_custom_css', $form_state['values']['nice_menus_custom_css']);
  62  }
  63  
  64  /**
  65   * Implemention of hook_menu().
  66   */
  67  function nice_menus_menu() {
  68    $items['admin/settings/nice_menus'] = array(
  69      'title' => 'Nice Menus',
  70      'description' => 'Configure Nice Menus.',
  71      'page callback' => 'drupal_get_form',
  72      'page arguments' => array('nice_menus_admin_settings'),
  73      'access arguments' => array('administer site configuration'),
  74      'type' => MENU_NORMAL_ITEM,
  75    );
  76  
  77    return $items;
  78  }
  79  
  80  
  81  /**
  82   * Settings form as implemented by hook_menu
  83   */
  84  function nice_menus_admin_settings() {
  85    $form['nice_menus_number'] = array(
  86      '#type' => 'textfield',
  87      '#description' => t('The total number of independent Nice menus blocks you want. Enter a number between 0 and 99. If you set this to 0, you will have no blocks created but you can still use the Nice menus theme functions directly in your theme.'),
  88      '#default_value' => variable_get('nice_menus_number', '2'),
  89      '#size' => 2,
  90    );
  91    $form['nice_menus_ie'] = array(
  92      '#type' => 'checkbox',
  93      '#title' => t('Enable IE support'),
  94      '#description' => t('This will add necessary JavaScript for Nice Menus to work properly in Internet Explorer.'),
  95      '#default_value' => variable_get('nice_menus_ie', 1),
  96    );
  97    
  98    // Custom validation to make sure the user is entering numbers.
  99    $form['#validate'][] = 'nice_menus_settings_validate';
 100  
 101    return system_settings_form($form);
 102  }
 103  
 104  /**
 105   * Custom validation for the settings form.
 106   */
 107  function nice_menus_settings_validate($form, &$form_state) {
 108    $number = $form_state['values']['nice_menus_number'];
 109    // Check to make sure it is a number and that is a maximum of 2 digits.
 110    if (!is_numeric($number) || strlen($number) > 2) {
 111        form_set_error('nice_menus_number', t('You must enter a number from 0 to 99.'));
 112    }
 113  }
 114  
 115  /**
 116   * Implementation of hook_block().
 117   */
 118  function nice_menus_block($op = 'list', $delta = 0, $edit = array()) {
 119    global $user;
 120  
 121    switch ($op) {
 122      case 'list':
 123        for ($i = 1; $i <= variable_get('nice_menus_number', '2'); $i++) {
 124          $blocks[$i]['info'] = variable_get('nice_menus_name_'. $i, 'Nice Menu '. $i) .' (Nice Menu)';
 125          // We have too many things changing per user, per page to cache.
 126          $blocks[$i]['cache'] = BLOCK_NO_CACHE;
 127        }
 128        return $blocks;
 129      break;
 130  
 131      case 'configure':
 132        $form['nice_menus_name_'. $delta] = array(
 133          '#type' => 'textfield',
 134          '#title' => t('Menu Name'),
 135          '#default_value' => variable_get('nice_menus_name_'. $delta, 'Nice Menu '. $delta),
 136        );
 137        $form['nice_menus_menu_'. $delta] = array(
 138          '#type' => 'select',
 139          '#title' => t('Source Menu Tree'),
 140          '#description' => t('The menu tree from which to show a nice menu.'),
 141          '#default_value' => variable_get('nice_menus_menu_'. $delta, 'navigation:0'),
 142          '#options' => menu_parent_options(menu_get_menus(), 0),
 143        );
 144        $form['nice_menus_type_'. $delta] = array(
 145          '#type' => 'select',
 146          '#title' => t('Menu Style'),
 147          '#description' => t('right: menu items are listed on top of each other and expand to the right') .'<br />'. t('left: menu items are listed on top of each other and expand to the left') .'<br />'. t('down: menu items are listed side by side and expand down'),
 148          '#default_value' => variable_get('nice_menus_type_'. $delta, 'right'),
 149          '#options' => drupal_map_assoc(array('right', 'left', 'down')),
 150        );
 151        return $form;
 152      break;
 153  
 154      case 'save':
 155        variable_set('nice_menus_name_'. $delta, $edit['nice_menus_name_'. $delta]);
 156        variable_set('nice_menus_menu_'. $delta, $edit['nice_menus_menu_'. $delta]);
 157        variable_set('nice_menus_type_'. $delta, $edit['nice_menus_type_'. $delta]);
 158      break;
 159  
 160      case 'view':
 161        // Build the nice menu for the block.
 162        list($menu_name, $mlid) = explode(':', variable_get('nice_menus_menu_'. $delta, 'navigation:0'));
 163        $direction = variable_get('nice_menus_type_'. $delta, 'right');
 164        if ($output = theme('nice_menu', $delta, $menu_name, $mlid, $direction)) {
 165          $block['content'] = $output['content'];
 166          if (variable_get('nice_menus_type_'. $delta, 'right') == 'down') {
 167            $class = 'nice-menu-hide-title';
 168          }
 169          else {
 170            $class = 'nice-menu-show-title';
 171          }
 172          // If we're building the navigation block
 173          // use the same block title logic as menu module.
 174          if ($output['subject'] == t('Navigation') && $user->uid) {
 175            $subject = $user->name;
 176          }
 177          else {
 178            $subject = $output['subject'];
 179          }
 180          $block['subject'] = '<span class="'. $class .'">'. check_plain($subject) .'</span>';
 181        }
 182        else {
 183          $block['content'] = false;
 184        }
 185        return $block;
 186      break;
 187    }
 188  }
 189  
 190  /**
 191   * Implmentation of hook_theme().
 192   */
 193  function nice_menus_theme() {
 194    return array(
 195      'nice_menu_tree' => array(
 196        'arguments' => array('menu_name' => NULL, 'mlid' => NULL, 'menu' => NULL),
 197      ),
 198      'nice_menu_build' => array(
 199        'arguments' => array('menu' => NULL),
 200      ),
 201      'nice_menu' => array(
 202        'arguments' => array('id' => NULL, 'menu_name' => NULL, 'mlid' => NULL, 'direction' => 'right', 'menu' => NULL),
 203      ),
 204      'nice_menu_primary_links' => array(
 205        'arguments' => array('direction' => 'down', 'menu' => NULL),
 206      ),
 207      'nice_menu' => array(
 208        'arguments' => array('id' => NULL, 'pid' => NULL, 'direction' => 'right', 'menu' => NULL),
 209      ),
 210      'nice_menu_primary_links' => array(
 211        'arguments' => array('direction' => 'down', 'menu' => NULL),
 212      ),
 213    );
 214  }
 215  
 216  /**
 217   * Implementation of hook_init().
 218   *
 219   * We are adding the JavaScript and CSS here rather than theme_nice_menu
 220   * because when block caching is enabled none of it would get fired
 221   * and the menus are unstyled.
 222   */
 223  function nice_menus_init() {
 224    // Add JavaScript, if enabled.
 225    if (variable_get('nice_menus_ie', 1) == 1) {
 226      drupal_add_js(drupal_get_path('module', 'nice_menus') .'/nice_menus.js');
 227    }
 228  
 229    // Add main CSS functionality.
 230    drupal_add_css(drupal_get_path('module', 'nice_menus') .'/nice_menus.css');
 231    // Add custom CSS layout if specified.
 232    if ($custom = variable_get('nice_menus_custom_css', '')) {
 233      drupal_add_css($custom);
 234    }
 235    // Fall back to default layout.
 236    else {
 237      drupal_add_css(drupal_get_path('module', 'nice_menus') .'/nice_menus_default.css');
 238    }
 239  }
 240  
 241  /**
 242   * Builds the final nice menu.
 243   *
 244   * @param $menu_name
 245   *   The top-level menu name that contains the menu to use (e.g. navigation
 246   *   or primary-links) for Drupal menus. For custom $menus this is just the
 247   *   name for menu display.
 248   * @param $mlid
 249   *   The menu ID from which to start building the items, i.e. the parent
 250   *   of the displayed menu.
 251   * @param $menu
 252   *   Optional. A custom menu array to use for theming -- it should have
 253   *   the same structure as that returned by menu_tree_all_data().
 254   * @return
 255   *   An HTML string of properly nested nice menu lists.
 256   */
 257  function theme_nice_menu_tree($menu_name, $mlid = NULL, $menu = NULL) {
 258    // Load the full menu array.
 259    $menu = isset($menu) ? $menu : menu_tree_all_data($menu_name);
 260  
 261    // For custom $menus and menus built all the way from the top-level we
 262    // don't need to "create" the specific sub-menu and we need to get the title
 263    // from the $menu_name since there is no "parent item" array.
 264  
 265    // Create the specific menu if we have a mlid.
 266    if (!empty($mlid)) {
 267      // Load the parent menu item.
 268      $item = menu_link_load($mlid);
 269      $title = check_plain($item['title']);
 270      // Narrow down the full menu to the specific sub-tree we need.
 271      for ($p = 1; $p < 10; $p++) {
 272        if ($sub_mlid = $item["p$p"]) {
 273          $subitem = menu_link_load($sub_mlid);
 274          // Menu sets these ghetto-ass keys in _menu_tree_check_access().
 275          $menu = $menu[(50000 + $subitem['weight']) .' '. $subitem['title'] .' '. $subitem['mlid']]['below'];
 276        }
 277      }
 278    }
 279    // Otherwise just set a title and move on.
 280    else {
 281      // Get the title from the DB since we don't have it in the $menu.
 282      $result = db_result(db_query("SELECT title FROM {menu_custom} WHERE menu_name = '%s'", $menu_name));
 283      $title = check_plain($result);
 284    }
 285  
 286    $output['content'] = '';
 287    $output['subject'] = $title;
 288  
 289    if ($menu) {
 290      $output['content'] .= theme('nice_menu_build', $menu);
 291    }
 292  
 293    return $output;
 294  }
 295  
 296  /**
 297   * Helper function that builds the nested lists of a nice menu.
 298   *
 299   * @param $menu
 300   *   Menu array from which to build the nested lists.
 301   */
 302  function theme_nice_menu_build($menu) {
 303    $output = '';
 304  
 305    foreach ($menu as $menu_item) {
 306      $mlid = $menu_item['link']['mlid'];
 307      // Check to see if it is a visible menu item.
 308      if ($menu_item['link']['hidden'] == 0) {
 309        // Build class name based on menu path
 310        // e.g. to give each menu item individual style.
 311        // Strip funny symbols.
 312        $clean_path = str_replace(array('http://', '<', '>', '&', '=', '?', ':'), '', $menu_item['link']['href']);
 313        // Convert slashes to dashes.
 314        $clean_path = str_replace('/', '-', $clean_path);
 315        $path_class = 'menu-path-'. $clean_path;
 316        // If it has children build a nice little tree under it.
 317        if ((!empty($menu_item['link']['has_children'])) && (!empty($menu_item['below']))) {
 318          // Keep passing children into the function 'til we get them all.
 319          $children = theme('nice_menu_build', $menu_item['below']);
 320          // Set the class to parent only of children are displayed.
 321          $parent_class = $children ? 'menuparent ' : '';
 322          $output .= '<li id="menu-'. $mlid .'" class="'. $parent_class . $path_class .'">'. theme('menu_item_link', $menu_item['link']);
 323          // Build the child UL only if children are displayed for the user.
 324          if ($children) {
 325            $output .= '<ul>';
 326            $output .= $children;
 327            $output .= "</ul>\n";
 328          }
 329          $output .= "</li>\n";
 330        }
 331        else {
 332          $output .= '<li id="menu-'. $mlid .'" class="'. $path_class .'">'. theme('menu_item_link', $menu_item['link']) .'</li>'."\n";
 333        }
 334      }
 335    }
 336    return $output;
 337  }
 338  
 339  /**
 340   * General theming function to allow any menu tree to be themed
 341   * as a nice menu.
 342   *
 343   * @param $id
 344   *   The nice menu ID.
 345   * @param $menu_name
 346   *   The top parent menu name from which to build the full menu.
 347   * @param $mlid
 348   *   The menu ID from which to build the displayed menu.
 349   * @param $direction
 350   *   Optional. The direction the menu expands. Default is 'right'.
 351   * @param $menu
 352   *   Optional. A custom menu array to use for theming --
 353   *   it should have the same structure as that returned
 354   *  by menu_tree_all_data(). Default is the standard menu tree.
 355   * @return
 356   *   An HTML string of nice menu links.
 357   */
 358  function theme_nice_menu($id, $menu_name, $mlid, $direction = 'right', $menu = NULL) {
 359    $output = array();
 360  
 361    if ($menu_tree = theme('nice_menu_tree', $menu_name, $mlid, $menu)) {
 362      if ($menu_tree['content']) {
 363        $output['content'] = '<ul class="nice-menu nice-menu-'. $direction .'" id="nice-menu-'. $id .'">'. $menu_tree['content'] .'</ul>'."\n";
 364        $output['subject'] = $menu_tree['subject'];
 365      }
 366    }
 367    return $output;
 368  }
 369  
 370  /**
 371   * Theme primary links as nice menus
 372   *
 373   * @param $direction
 374   *   Optional. The direction the menu expands. Default is 'down'.
 375   * @param $menu
 376   *   Optional. A custom menu array to use for theming --
 377   *   it should have the same structure as that returned
 378   *   by menu_tree_all_data(). Default is the standard menu tree.
 379   * @return
 380   *   An HTML string of nice menu primary links.
 381   */
 382  function theme_nice_menu_primary_links($direction = 'down', $menu = NULL) {
 383    $menu_name = variable_get('menu_primary_links_source', 'primary-links');
 384    $output = theme('nice_menu', 0, $menu_name, 0, $direction, $menu);
 385    return $output['content'];
 386  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7