You are here

footermap.module in footermap: a footer site map 5.2

This module queries the menu for pages and makes a dynamic sitemap at the bottom of the page.

copyright Matthew Radcliffe, Kosada Inc.

File

footermap.module
View source
<?php

/* $Id */

/**
 * @file
 * This module queries the menu for pages and makes a dynamic
 * sitemap at the bottom of the page.
 *
 * copyright Matthew Radcliffe, Kosada Inc.
 *
 */

/**
 * Display help and module information
 * @param section which sectino of the site we're displaying
 * @return help text for section
 */
function footermap_help($section = '') {
  $output = '';
  switch ($section) {
    case "admin/modules#description":
      $output = t("Displays a sitemap at the bottom of the page");
      break;
  }
  return $output;
}

// function footermap_help

/**
 * Set settings path
 * @return array an array of menu items
 */
function footermap_menu() {
  $items = array();
  $items[] = array(
    'path' => 'admin/settings/footermap',
    'title' => t('Footermap'),
    'description' => t('Change recurse limit or specify top menu id'),
    'callback' => 'drupal_get_form',
    'callback arguments' => array(
      'footermap_settings',
    ),
    'access' => user_access('administer site configuration'),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

// function footermap_menu

/**
 * Setup settings form for footermap
 * @return array an array of forms
 */
function footermap_settings() {
  $form['recurse_limit'] = array(
    '#type' => 'textfield',
    '#title' => t('Recurse Limit'),
    '#default_value' => variable_get('recurse_limit', 0),
    '#size' => 3,
    '#maxlength' => 3,
    '#description' => t('Set the # of times to recurse through a particular menu.  Default is 0, unlimited.'),
  );
  $topmenu = variable_get('top_menu', variable_get('menu_primary_menu', 0));
  $form['top_menu'] = array(
    '#type' => 'textfield',
    '#title' => t('Top Menu'),
    '#default_value' => $topmenu,
    '#size' => 3,
    '#maxlength' => 3,
    '#description' => t('Set the menu id to use as the top level.  Default is to start at 0 i.e. primary links which should be set by the menu_primary_menu variable. If you are seeing odd behavior try setting this to 0.'),
  );
  $primary_menus = footermap_get_primary_menus($topmenu);
  $form['avail_menus'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Enabled Menus'),
    '#default_value' => variable_get('avail_menus', array(
      'Primary links',
    )),
    '#options' => $primary_menus,
    '#description' => t('Select the menus you want visible.'),
  );
  $form['menu_headers'] = array(
    '#type' => 'radios',
    '#title' => t('Menu Headers'),
    '#default_value' => variable_get('menu_headers', 0),
    '#options' => array(
      t('No'),
      t('Yes'),
    ),
    '#required' => '1',
    '#description' => t('Display menu block headers in the footer site map.'),
  );
  $form['footer_legacy'] = array(
    '#type' => 'radios',
    '#title' => t('Enable Legacy Footermap'),
    '#default_value' => variable_get('footer_legacy', 0),
    '#options' => array(
      t('No'),
      t('Yes'),
    ),
    '#description' => t('Enable footeramp to be inserted in the closure as well as in a block. You probably do not want to enable this unless you\'re upgrading from an older version.'),
  );
  $form['footer_cache'] = array(
    '#type' => 'radios',
    '#title' => t('Enable Caching'),
    '#default_value' => variable_get('footer_cache', 0),
    '#options' => array(
      t('Yes'),
      t('No'),
    ),
    '#required' => '1',
    '#description' => t('Enable caching of footermap. Recommended.'),
  );
  return system_settings_form($form);
}

// function footermap_settings

/**
 * Declare footer hook
 * @return string A string containing HTML to be inserted
 */
function footermap_footer($main = 0) {
  if (variable_get('footer_legacy', 0) == 0) {
    return;
  }
  else {
    return _footermap_render();
  }
}

// function footermap_footer

/**
 * Implementation of hook_block
 */
function footermap_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks = array();
    $blocks[] = array(
      'info' => t('Footermap'),
      'weight' => 0,
      'status' => 1,
      'region' => 'footer',
    );
    return $blocks;
  }
  else {
    if ($op == 'view') {
      switch ($delta) {
        case 0:
          $block = array(
            'subject' => t(''),
            'content' => _footermap_render(),
          );
          break;
      }
      return $block;
    }
  }
}

// function footermap_block

/**
 * Render function for footermap
 * @return string A string containing HTML to be inserted
 */
function _footermap_render() {

  /* base mid changed to '0' in 5.x? */

  /* is there a way of getting this dynamically in 4.x and 5.x consistently? */
  $mapref = array();
  $avail_menus = array();
  if (variable_get('footer_cache', 0) == 0) {
    $vartime = cache_get('variables', 'cache')->created;
    $o = cache_get('footermap', 'cache');
  }
  if (isset($o->data) && $o->created >= $vartime) {
    return $o->data;
  }
  unset($o);
  $tmp_menus = variable_get('avail_menus', array(
    'Primary links',
  ));
  foreach ($tmp_menus as $key => $val) {
    if (!is_numeric($val)) {
      $avail_menus[$val] = $val;
    }
  }
  footermap_get_menu(variable_get('top_menu', variable_get('menu_primary_menu', 0)), 1, variable_get('recurse_limit', 0), $mapref, 0, $avail_menus);

  //     $x = footermap_get_menu(variable_get('top_menu',variable_get('menu_primary_menu', 0)),0,1,$mapref,variable_get('recurse_limit',0));
  foreach ($mapref as $block) {

    // we need to do this because theme() can do links or not links, not a mix of either for menu_headers...
    if (variable_get('menu_headers', 0) == 1) {

      // menu_headers enabled?
      foreach ($block as $item) {
        if ($item['href'] == '') {

          // find an empty path
          $o .= footermap_theme_map($block, array(
            'class' => 'footermap-submenu',
          ), array(
            'class' => 'links footermap-item',
          ));
          break;
        }
      }
    }
    else {
      $o .= theme('links', $block, array(
        'class' => 'links footermap-item',
      ));
    }
  }
  $o = "<div class=\"footermap\">\n" . $o . "\n</div>\n";
  if (variable_get('footer_cache', 0) == 0) {
    cache_set('footermap', 'cache', $o, CACHE_TEMPORARY);
  }
  return $o;
}

// function _footermap_render

/**
 * Retrieve menu recursively
 * @param mid menu_id
 * @param level what level we are in the tree
 * @param limit recurse limit
 * @param mapref array pointer
 * @param arrcnt block counter
 * @param avail_menus visible menus
 */
function footermap_get_menu($mid, $level, $limit, &$mapref, $arrcnt, $avail_menus) {
  $x = 0;
  $a = 0;
  if ($limit != 0 && $level > $limit) {
    return 0;
  }
  if (module_exists('path')) {
    $r = db_query("SELECT menu.*, (SELECT dst FROM {url_alias} WHERE src = menu.path) AS alias FROM {menu} WHERE pid = {$mid} ORDER BY pid,weight") or die(db_error());
  }
  else {
    $r = db_query("SELECT menu.* FROM {menu} WHERE pid = {$mid} ORDER BY pid,weight") or die(db_error());
  }
  while ($h = db_fetch_object($r)) {
    if (preg_match("/node\\/(\\d+)/", $h->path, $matches) == 1) {
      $node = node_load($matches[1]);
    }
    $g = 0;
    if ($level == 1) {

      // in drupal 5 menu block headers are the first level!
      foreach ($avail_menus as $key => $mn) {
        print "<!-- " . $mn . " = " . $h->title . " -->\n";
        if ($mn == $h->title) {
          $g = 1;
          end;
        }
      }
    }
    else {
      $g = 1;
    }
    unset($cur);

    // let's make sure we unset things first
    $cur = 'menu' . $h->mid;
    if (dechex($h->type) % 10 != 0 && $g == 1 && ($node->status == 1 || !isset($node))) {
      if ($h->path != '') {
        $a = 1;
      }
      else {
        if (variable_get('menu_headers', 0) == 1 && $h->path == '') {
          $a = 1;
        }
        else {
          $a = 2;
        }
      }
      if ($a == 1) {

        // menu_headers enabled
        if ($h->alias) {
          $mapref[$arrcnt][$cur][href] = $h->alias;
        }
        else {
          $mapref[$arrcnt][$cur][href] = $h->path;
        }
        if ($h->path == "/") {
          $mapref[$arrcnt][$cur][href] = $base_url;
        }
        $mapref[$arrcnt][$cur][title] = $h->title;
      }
      footermap_get_menu($h->mid, $level + 1, $limit, $mapref, $arrcnt, array());
      if ($level == 1) {
        $arrcnt++;
      }
    }
  }
}

// function footermap_get_menu()

/**
 * Theme a given menu block if menu_headers are enabled and the path is empty.
 * @param menu array containing links under  a certain tree
 * @param menuattributes css attributes
 * @param linkattributes css attributes
 * @return string html-formatted string
 */
function footermap_theme_map($menu, $menuattributes, $linkattributes) {
  $arrcnt = 0;
  $ret = '';
  foreach ($menu as $path) {
    if ($path['href'] == '' && $arrcnt == 0) {

      // this should always be the first element in the array... hopefully...  nasty bug?
      $ret .= "<ul>\t<li " . drupal_attributes($menuattributes) . ">" . $path['title'] . "</li>\n";
    }
    else {
      $ret .= "\t<li " . drupal_attributes($linkattributes) . ">" . l($path['title'], $path['href'], $attributes) . "</li>\n";
    }
    $arrcnt++;
  }
  $ret .= "</ul>\n";
  return $ret;
}

// function footermap_theme_map

/**
 * Get all the primary menus with pid = $topmenu
 * @param topmenu pid integer value
 * @return array an array of menu names
 */
function footermap_get_primary_menus($topmenu = 0) {
  $ret = array();
  if (!is_numeric($topmenu)) {

    // let's be careful
    $topmenu = 0;
  }
  $res = db_query("select distinct title from {menu} where pid = %d", $topmenu);
  while ($menu = db_fetch_object($res)) {
    $ret[$menu->title] = t($menu->title);
  }
  return $ret;
}

// function footermap_get_primary_menus

Functions

Namesort descending Description
footermap_block Implementation of hook_block
footermap_footer Declare footer hook
footermap_get_menu Retrieve menu recursively
footermap_get_primary_menus Get all the primary menus with pid = $topmenu
footermap_help Display help and module information
footermap_menu Set settings path
footermap_settings Setup settings form for footermap
footermap_theme_map Theme a given menu block if menu_headers are enabled and the path is empty.
_footermap_render Render function for footermap