You are here

simplemenu.module in SimpleMenu 6.2

Same filename and directory in other branches
  1. 5 simplemenu.module
  2. 6 simplemenu.module
  3. 7 simplemenu.module

Creates a simplemenu.

File

simplemenu.module
View source
<?php

/**
 * @file
 * Creates a simplemenu.
 */

/**
 * Implementation of hook_menu().
 */
function simplemenu_menu() {
  $items = array();
  $items['admin/settings/simplemenu'] = array(
    'title' => 'SimpleMenu',
    'description' => 'Select the menu to display.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'simplemenu_admin_settings',
    ),
    'access arguments' => array(
      'administer simplemenu',
    ),
  );
  return $items;
}

/**
 * Is simplemenu enabled for this page request?
 */
function simplemenu_enabled() {
  static $enabled;
  if (!isset($enabled)) {
    global $theme;
    $exclusions = variable_get('simplemenu_exclusions', array());
    $enabled = user_access('view simplemenu') && (!isset($exclusions[$theme]) || !$exclusions[$theme]) && _simplemenu_page_visibility();
  }
  return $enabled;
}

/**
 * Implementation of hook_init().
 */
function simplemenu_init() {

  // do a simple access check here, since theme isn't available to check yet
  if (user_access('view simplemenu')) {
    $path = drupal_get_path('module', 'simplemenu');
    $simplemenu_theme = variable_get('simplemenu_theme', 'original');
    drupal_add_css($path . '/simplemenu.css');
    if ($simplemenu_theme != 'custom') {
      drupal_add_css($path . '/themes/' . $simplemenu_theme . '/' . $simplemenu_theme . '.css');
    }
    $settings = array(
      'effect' => variable_get('simplemenu_effect', 'opacity'),
      'effectSpeed' => variable_get('simplemenu_effect_speed', 'fast'),
      'element' => variable_get('simplemenu_element', 'body'),
      'hideDelay' => variable_get('simplemenu_hide_delay', 800),
      'placement' => variable_get('simplemenu_element_method', 'prepend'),
      'detectPopup' => variable_get('simplemenu_detect_popop', 1),
    );
    drupal_add_js(array(
      'simplemenu' => $settings,
    ), 'setting');

    //$simplemenu = drupal_to_js(simplemenu_get_menu());

    //drupal_add_js('var simplemenu = '. drupal_to_js(simplemenu_get_menu()) .';', 'inline');
    drupal_add_js($path . '/simplemenu.js');
    drupal_add_js($path . '/superfish.js');
  }
}

/**
 * Implementation of hook_footer().
 */
function simplemenu_footer() {
  $simplemenu = simplemenu_get_menu();
  return $simplemenu;
}

/**
 * Implementation of hook_perm().
 */
function simplemenu_perm() {
  return array(
    'view simplemenu',
    'administer simplemenu',
  );
}

/**
 * SimpleMenu settings page.
 */
function simplemenu_admin_settings() {
  if (module_exists('menu')) {
    $form['default_menu']['simplemenu_menu'] = array(
      '#type' => 'select',
      '#title' => t('Menu'),
      '#options' => menu_parent_options(menu_get_menus(), array(
        'mlid' => 0,
      )),
      // return complete tree;
      '#default_value' => variable_get('simplemenu_menu', 'navigation:0'),
      '#description' => t('Select the menu to display.'),
    );
  }
  if (module_exists('devel')) {
    $form['default_menu']['simplemenu_devel'] = array(
      '#type' => 'checkbox',
      '#title' => t('Add devel module links'),
      '#default_value' => variable_get('simplemenu_devel', 0),
      '#description' => t('Add devel module links for those users that can access the devel module.'),
    );
  }
  $form['default_menu']['simplemenu_theme'] = array(
    '#type' => 'select',
    '#title' => t('Theme'),
    '#options' => array(
      'original' => t('original'),
      'blackblue' => t('black & blue'),
      'custom' => t('custom'),
    ),
    '#default_value' => variable_get('simplemenu_theme', 'original'),
    '#description' => t('Select which theme to use. If you specify custom, you need to define CSS in your theme.'),
  );
  $form['default_menu']['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['default_menu']['advanced']['simplemenu_element'] = array(
    '#type' => 'textfield',
    '#title' => t('CSS selector to attach menu to'),
    '#default_value' => variable_get('simplemenu_element', 'body'),
    '#description' => t('A valid CSS selector to attach the menu to. <em>Example: body, #primary, div.my-class</em>'),
    '#required' => TRUE,
  );
  $form['default_menu']['advanced']['simplemenu_element_method'] = array(
    '#type' => 'radios',
    '#title' => t('Attach method'),
    '#options' => array(
      'prepend' => t('Prepend'),
      'append' => t('Append'),
    ),
    '#default_value' => variable_get('simplemenu_element_method', 'prepend'),
    '#description' => t('Choose how the menu should be attached to the above selector.'),
    '#required' => TRUE,
  );
  $form['default_menu']['advanced']['simplemenu_exclusions'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Theme exclusions'),
    '#options' => drupal_map_assoc(array_keys(list_themes())),
    '#default_value' => variable_get('simplemenu_exclusions', array()),
    '#description' => t('Select which themes to <strong>not</strong> display the menu. Use this when you have a theme that displays its own admin navigation.'),
  );
  $form['default_menu']['advanced']['simplemenu_hide_delay'] = array(
    '#type' => 'textfield',
    '#title' => t('Hide delay'),
    '#size' => 4,
    '#default_value' => variable_get('simplemenu_hide_delay', 800),
    '#description' => t('How long (in milliseconds) should a menu still appear after losing focus.'),
  );
  $form['default_menu']['advanced']['simplemenu_effect'] = array(
    '#type' => 'radios',
    '#title' => t('Show effect'),
    '#options' => array(
      'opacity' => t('Fade'),
      'height' => t('Slide'),
      'none' => t('None'),
    ),
    '#default_value' => variable_get('simplemenu_effect', 'opacity'),
    '#description' => t('The effect used when displaying a menu.'),
  );
  $form['default_menu']['advanced']['simplemenu_effect_speed'] = array(
    '#type' => 'radios',
    '#title' => t('Show speed'),
    '#options' => array(
      'slow' => t('Slow'),
      'medium' => t('Medium'),
      'fast' => t('Fast'),
    ),
    '#default_value' => variable_get('simplemenu_effect_speed', 'fast'),
    '#description' => t('The speed of the effect, not used when "none" is set to show effect.'),
  );
  $form['default_menu']['advanced']['simplemenu_detect_popop'] = array(
    '#type' => 'checkbox',
    '#title' => t('Detect pop-up windows'),
    '#default_value' => variable_get('simplemenu_detect_popop', 1),
    '#description' => t("Choose whether SimpleMenu should attempt to detect if it is inside of a pop-up window.  If enabled, SimpleMenu will not display if it is inside of a pop-up window."),
  );
  $form['default_menu']['advanced']['simplemenu_visibility_operator'] = array(
    '#type' => 'radios',
    '#title' => t('Show block on specific pages'),
    '#default_value' => variable_get('simplemenu_visibility_operator', 0),
    '#options' => array(
      0 => t('Show on every page except the listed pages.'),
      1 => t('Show on only the listed pages.'),
    ),
  );
  $form['default_menu']['advanced']['simplemenu_visibility_pages'] = array(
    '#type' => 'textarea',
    '#title' => t('Pages'),
    '#default_value' => variable_get('simplemenu_visibility_pages', ''),
    '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
      '%blog' => 'blog',
      '%blog-wildcard' => 'blog/*',
      '%front' => '<front>',
    )),
  );
  return system_settings_form($form);
}

/**
 * Render an HTML list of links for a given menu.
 */
function simplemenu_get_menu() {
  $output = '';

  // if a user turned off menu module but SimpleMenu was previously set
  // reset variable so a menu appears
  $menu_name = module_exists('menu') ? variable_get('simplemenu_menu', 'navigation:0') : 'navigation:0';
  $tree = simplemenu_menu_tree($menu_name);

  // allow other modules to alter the tree
  drupal_alter('simplemenu_tree', $tree);

  // if the tree is still empty, display a link to the settings.
  if (!$tree) {
    $tree[] = l(t('No menu items found. Try a different menu as the default.'), 'admin/settings/simplemenu');
  }

  // render for output
  $output = simplemenu_tree_output($tree, array(
    'id' => 'simplemenu',
  ));
  return $output;
}

/**
 * Custom implementation of menu_tree().
 * We want to retrieve the entire menu structure for a given menu,
 * regardless of whether or not the menu item is expanded or not.
 * 
 * @param $menu_name
 *   The name of the menu.
 * @return
 *   A tree array ready for simplemenu_tree_output(). 
 */
function simplemenu_menu_tree($menu_name = 'navigation:0') {
  static $menu_output = array();
  if (!isset($menu_output[$menu_name])) {
    $tree = simplemenu_tree_all_data($menu_name);
    $menu_output[$menu_name] = simplemenu_compact_menu_tree($tree);
  }
  return $menu_output[$menu_name];
}

/**
 * Recursive function to compact the menu tree into our item_list style tree
 *
 * @param $tree
 *   A menu tree array as returned from menu_tree_all_data().
 * @return 
 *   A tree array ready for simplemenu_tree_output().
 */
function simplemenu_compact_menu_tree($tree) {
  $items = array();
  foreach ($tree as $data) {
    if (!$data['link']['hidden']) {
      $link = $data['link'];
      if (empty($link['localized_options'])) {
        $link['localized_options'] = array();
      }
      unset($link['localized_options']['attributes']['title']);
      $link = l($link['title'], $link['href'], $link['localized_options']);
      if ($data['below']) {
        $items[] = array(
          'data' => $link,
          'children' => simplemenu_compact_menu_tree($data['below']),
        );
      }
      else {
        $items[] = $link;
      }
    }
  }
  return $items;
}

/**
 * Render the necessary markup from the tree array. This is largely based on
 * theme_item_list(), without the extra markup.
 * 
 * @param $tree
 *   Array of items as expected in theme_item_list().
 */
function simplemenu_tree_output($items, $attributes = array()) {
  if (!empty($items)) {
    $output .= '<ul' . drupal_attributes($attributes) . '>';
    $num_items = count($items);
    foreach ($items as $i => $item) {
      $attributes = array();
      $children = array();
      if (is_array($item)) {
        foreach ($item as $key => $value) {
          if ($key == 'data') {
            $data = $value;
          }
          elseif ($key == 'children') {
            $children = $value;
          }
          else {
            $attributes[$key] = $value;
          }
        }
      }
      else {
        $data = $item;
      }
      if (count($children) > 0) {
        $data .= simplemenu_tree_output($children);

        // Render nested list
        $attributes['class'] = empty($attributes['class']) ? 'expanded' : $attributes['class'] . ' expanded';
      }
      $output .= '<li' . drupal_attributes($attributes) . '>' . $data . "</li>\n";
    }
    $output .= "</ul>\n";
  }
  return $output;
}

/**
 * Modified menu_tree_all_data(), providing the complete menu tree below $root_menu
 * (which can be *any* menu item, not just the root of a custom menu).
 *
 * @param $root_menu
 *   root menu item of the tree to return as "menu_name:mlid" (mlid = menu link id)
 *
 * @todo we don't actually need $menu_name, $mlid would be sufficient
 */
function simplemenu_tree_all_data($root_menu = 'navigation:0') {
  static $tree = array();
  list($menu_name, $mlid) = explode(':', $root_menu);

  // Generate the cache ID.
  // "links:navigation:all:2" means "all from root to 2" (what the ...), so for "all from 2 down" we do "links:navigation:all:2:all"
  $cid = "links:{$menu_name}:all:{$mlid}" . ($mlid ? ':all' : '');
  if (!isset($tree[$cid])) {

    // If the static variable doesn't have the data, check {cache_menu}.
    $cache = cache_get($cid, 'cache_menu');
    if ($cache && isset($cache->data)) {
      $data = $cache->data;
    }
    else {

      // Build and run the query, and build the tree.
      if ($mlid > 0) {
        $item = menu_link_load($mlid);

        // The tree is a subtree of $menu_name, so we need to restrict the query to
        // this subtree.
        $px = "p{$item['depth']}";
        $where = " AND ml.{$px} = %d AND ml.mlid != %d";
        $args = array(
          $item[$px],
          $mlid,
        );
      }
      else {

        // Get all links in this menu.
        $where = '';
        $args = array();
      }
      array_unshift($args, $menu_name);

      // Select the links from the table, and recursively build the tree.  We
      // LEFT JOIN since there is no match in {menu_router} for an external
      // link.
      $data['tree'] = menu_tree_data(db_query("\n        SELECT m.load_functions, m.to_arg_functions, m.access_callback, m.access_arguments, m.page_callback, m.page_arguments, m.title, m.title_callback, m.title_arguments, m.type, m.description, ml.*\n        FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path\n        WHERE ml.menu_name = '%s'" . $where . "\n        ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args));
      $data['node_links'] = array();
      menu_tree_collect_node_links($data['tree'], $data['node_links']);

      // Cache the data.
      cache_set($cid, $data, 'cache_menu');
    }

    // Check access for the current user to each item in the tree.
    menu_tree_check_access($data['tree'], $data['node_links']);
    $tree[$cid] = $data['tree'];
  }
  return $tree[$cid];
}

/**
 * Implementation of hook_simplemenu_tree_alter().
 */
function simplemenu_simplemenu_tree_alter(&$tree) {
  if (variable_get('simplemenu_devel', 0) && module_exists('devel') && user_access('access devel information')) {
    $devel = array(
      'data' => l(t('Devel module'), 'admin/settings/devel'),
      'children' => simplemenu_menu_tree('devel:0'),
    );
    array_unshift($tree, $devel);
  }
}

/**
 * Determine if simplemenu should be displayed based on visibility settings.
 *
 * @return boolean
 */
function _simplemenu_page_visibility() {
  $operator = variable_get('simplemenu_visibility_operator', 0);
  $pages = variable_get('simplemenu_visibility_pages', '');
  if ($pages) {
    $path = drupal_get_path_alias($_GET['q']);

    // Compare with the internal and path alias (if any).
    $page_match = drupal_match_path($path, $pages);
    if ($path != $_GET['q']) {
      $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
    }

    // When $operator has a value of 0, the menu is displayed on
    // all pages except those listed in $pages. When set to 1, it
    // is displayed only on those pages listed in $pages.
    $page_match = !($operator xor $page_match);
  }
  else {
    $page_match = TRUE;
  }
  return $page_match;
}

Functions

Namesort descending Description
simplemenu_admin_settings SimpleMenu settings page.
simplemenu_compact_menu_tree Recursive function to compact the menu tree into our item_list style tree
simplemenu_enabled Is simplemenu enabled for this page request?
simplemenu_footer Implementation of hook_footer().
simplemenu_get_menu Render an HTML list of links for a given menu.
simplemenu_init Implementation of hook_init().
simplemenu_menu Implementation of hook_menu().
simplemenu_menu_tree Custom implementation of menu_tree(). We want to retrieve the entire menu structure for a given menu, regardless of whether or not the menu item is expanded or not.
simplemenu_perm Implementation of hook_perm().
simplemenu_simplemenu_tree_alter Implementation of hook_simplemenu_tree_alter().
simplemenu_tree_all_data Modified menu_tree_all_data(), providing the complete menu tree below $root_menu (which can be *any* menu item, not just the root of a custom menu).
simplemenu_tree_output Render the necessary markup from the tree array. This is largely based on theme_item_list(), without the extra markup.
_simplemenu_page_visibility Determine if simplemenu should be displayed based on visibility settings.