You are here

site_map.module in Site map 6

Original author: Nic Ivy Now maintained by by Fredrik Jonsson fredrik at combonet dot se

File

site_map.module
View source
<?php

/**
 * @file
 * Original author: Nic Ivy
 * Now maintained by by Fredrik Jonsson fredrik at combonet dot se
 */

/**
 * Implements hook_help().
 */
function site_map_help($path, $arg) {
  switch ($path) {
    case 'sitemap':
      $output = _sitemap_get_message();
      return $output ? '<p>' . $output . '</p>' : '';
  }
}

/**
 * Implements hook_perm().
 */
function site_map_perm() {
  return array(
    'access site map',
  );
}

/**
 * Implements hook_theme().
 */
function site_map_theme() {
  return array(
    'site_map_display' => array(
      'arguments' => array(),
    ),
    'site_map_box' => array(
      'arguments' => array(
        'title' => NULL,
        'content' => NULL,
        'class' => '',
      ),
    ),
    'site_map_feed_icon' => array(
      'arguments' => array(
        'url' => NULL,
        'type' => 'node',
      ),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function site_map_menu() {
  $items['admin/settings/sitemap'] = array(
    'title' => 'Site map',
    'description' => 'Control what should be displayed on the site map.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'site_map_admin_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'site_map.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['sitemap'] = array(
    'title' => 'Site map',
    'description' => 'Display a site map with RSS feeds.',
    'page callback' => 'site_map_page',
    'access arguments' => array(
      'access site map',
    ),
    'type' => MENU_SUGGESTED_ITEM,
  );
  return $items;
}

/**
 * Implements hook_block().
 */
function site_map_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $blocks[0]['info'] = t('Syndicate (site map)');
    return $blocks;
  }
  elseif ($op == 'view') {
    if (user_access('access content')) {
      $block['subject'] = t('Syndicate');
      if (arg(0) == 'blog') {
        $uid = arg(1);
        $feedurl = is_numeric($uid) ? "blog/{$uid}/feed" : 'blog/feed';
      }
      else {
        $feedurl = variable_get('site_map_rss_front', 'rss.xml');
      }
      $block['content'] = theme('feed_icon', url($feedurl), t('Syndicate'));
      $block['content'] .= '<div class="more-link">' . l(t('more'), 'sitemap', array(
        'title' => t('View the site map to see more RSS feeds.'),
      )) . "</div>\n";
      return $block;
    }
  }
}

/**
 * Menu callback for the site map.
 */
function site_map_page() {
  if (variable_get('site_map_css', 0) != 1) {
    drupal_add_css(drupal_get_path('module', 'site_map') . '/site_map.css');
  }
  return theme('site_map_display');
}

/**
 * Return a themed site map.
 *
 * @return string
 *   A string containing the site map output.
 */
function theme_site_map_display() {
  $output = '';
  if (variable_get('site_map_show_rss_links', 1) != 0 && module_exists('commentrss') && variable_get('commentrss_site', COMMENTRSS_SITE_FRONT_PAGE)) {
    $output .= '<p><span class="rss">' . theme('site_map_feed_icon', NULL) . '</span> ' . t('Link to a content RSS feed');
    $output .= '<br /><span class="rss">' . theme('site_map_feed_icon', NULL, 'comment') . '</span> ' . t('Link to a comment RSS feed');
    $output .= '</p>';
  }
  if (variable_get('site_map_show_front', 1)) {
    $output .= _site_map_front_page();
  }
  if (variable_get('site_map_show_blogs', 1)) {
    $output .= _site_map_blogs();
  }

  /* $output .= _site_map_audio(); */

  /* $output .= _site_map_video(); */

  // Compile the books trees.
  $output .= _site_map_books();

  // Compile the menu trees.
  $output .= _site_map_menus();
  if (variable_get('site_map_show_faq', 1)) {
    $output .= _site_map_faq();
  }

  // Compile the vocabulary trees.
  $output .= _site_map_taxonomys();

  // Invoke all modules and get themed HTML to be integrated into the site map.
  $additional = module_invoke_all('site_map');
  foreach ($additional as $themed_site_map_code) {
    $output .= $themed_site_map_code;
  }
  $output = '<div id="site-map" class="site-map">' . $output . '</div>';
  return $output;
}

/**
 * Return a themed sitemap box.
 *
 * @param string $title
 *   The subject of the box.
 * @param string $content
 *   The content of the box.
 * @param string $class
 *   Optional extra class for the box.
 *
 * @return string
 *   A string containing the box output.
 */
function theme_site_map_box($title, $content, $class = '') {
  return '<div class="sitemap-box ' . $class . '"><h2 class="title">' . $title . '</h2><div class="content">' . $content . '</div></div>';
}

/**
 * Return a themed feed icon.
 *
 * @param string $url
 *   The feed URL.
 * @param string $type
 *   The type of feed icon.
 *
 * @return string
 *   A string containing the linked image.
 */
function theme_site_map_feed_icon($url, $type = 'node') {
  $output = '';
  switch ($type) {
    case 'node':
      $output = theme('image', drupal_get_path('module', 'site_map') . '/feed-small.png', t('Syndicate content'), t('Syndicate content'));
      break;
    case 'comment':
      $output = theme('image', drupal_get_path('module', 'site_map') . '/feed-small-comment.png', t('Syndicate comments'), t('Syndicate comments'));
      break;
  }
  if ($url) {
    $output = l($output, $url, array(
      'attributes' => array(
        'class' => 'feed-link',
      ),
      'html' => TRUE,
    ));
  }
  return $output;
}

/**
 * Returns front page details.
 */
function _site_map_front_page() {
  $output = '';
  $class = '';
  $title = t('Front page');
  $output = l(t("Front page of %sn", array(
    "%sn" => variable_get('site_name', 'Drupal'),
  )), '<front>', array(
    'html' => TRUE,
  ));
  if (variable_get('site_map_show_rss_links', 1) != 0) {
    $rss_link = theme('site_map_feed_icon', variable_get('site_map_rss_front', 'rss.xml'));
    if (module_exists('commentrss') && variable_get('commentrss_site', COMMENTRSS_SITE_FRONT_PAGE)) {
      $rss_link .= ' ' . theme('site_map_feed_icon', 'crss', 'comment');
    }
    if (variable_get('site_map_show_rss_links', 1) == 1) {
      $output .= ' ' . $rss_link;
    }
    else {
      $class = ' sitemap-rss-left';
      $output = $rss_link . ' ' . $output;
    }
  }
  return theme('site_map_box', $title, $output, 'sitemap-front' . $class);
}

/**
 * Render the latest blog authors.
 */
function _site_map_blogs() {
  $output = '';
  $class = '';
  if (module_exists('blog')) {
    $title = t('Blogs');
    $output = '<div class="description">' . t("Community blog and recent blog authors at %sn.", array(
      "%sn" => variable_get('site_name', 'Drupal'),
    )) . '</div>';
    $blog_link = l(t('All blogs'), 'blog');
    if (variable_get('site_map_show_rss_links', 1) != 0) {
      $rss_link = theme('site_map_feed_icon', 'blog/feed');
      if (variable_get('site_map_show_rss_links', 1) == 1) {
        $blog_link .= ' ' . $rss_link;
      }
      else {
        $class = ' sitemap-rss-left';
        $blog_link = $rss_link . ' ' . $blog_link;
      }
    }
    $blogs = array();
    $blogs[] = $blog_link;
    $sql = "SELECT DISTINCT u.uid, u.name, count(u.uid) AS numitems\n      FROM {node} n\n      INNER JOIN {users} u ON u.uid = n.uid\n      WHERE n.type = 'blog' AND n.status = 1\n      GROUP BY u.uid, u.name\n      ORDER BY numitems DESC, u.name";
    $result = db_query_range($sql, 0, 10);
    while ($blog = db_fetch_object($result)) {
      $blog_item = l(t("!s's blog", array(
        "!s" => $blog->name,
      )), "blog/{$blog->uid}") . ' (' . $blog->numitems . ')';
      if (variable_get('site_map_show_rss_links', 1) != 0) {
        $rss_link = theme('site_map_feed_icon', "blog/{$blog->uid}/feed");
        if (variable_get('site_map_show_rss_links', 1) == 1) {
          $blog_item .= ' ' . $rss_link;
        }
        else {
          $blog_item = $rss_link . ' ' . $blog_item;
        }
      }
      $blogs[] = $blog_item;
    }
    $output .= theme('item_list', $blogs);
    $output = theme('site_map_box', $title, $output, 'sitemap-blog' . $class);
  }
  return $output;
}

/**
 * Returns audio content if audio module is enabled.
 */
function _site_map_audio() {
  $output = '';
  $class = '';
  if (module_exists('audio')) {
    $title = t('Audio');
    $output = l(t('Audio content'), 'audio');
    if (variable_get('site_map_show_rss_links', 1) != 0) {
      $rss_link = theme('site_map_feed_icon', 'audio/feed');
      if (variable_get('site_map_show_rss_links', 1) == 1) {
        $output .= ' ' . $rss_link;
      }
      else {
        $class = ' sitemap-rss-left';
        $output = $rss_link . ' ' . $output;
      }
    }
    $output = theme('site_map_box', $title, $output, 'sitemap-audio' . $class);
  }
  return $output;
}

/**
 * Returns video content if video module is enabled.
 */
function _site_map_video() {
  $output = '';
  $class = '';
  if (module_exists('video')) {
    $title = t('Video');
    $output = l(t('Video content'), 'video');
    if (variable_get('site_map_show_rss_links', 1) != 0) {
      $rss_link = theme('site_map_feed_icon', 'video/feed');
      if (variable_get('site_map_show_rss_links', 1) == 1) {
        $output .= ' ' . $rss_link;
      }
      else {
        $class = ' sitemap-rss-left';
        $output = $rss_link . ' ' . $output;
      }
    }
    $output = theme('site_map_box', $title, $output, 'sitemap-video' . $class);
  }
  return $output;
}

/**
 * Render books.
 */
function _site_map_books() {
  $output = '';
  if (module_exists('book') && ($nids = variable_get('site_map_show_books', array()))) {
    $title = t('Books');
    $description = '<div class="description">' . t("Books at %sn.", array(
      "%sn" => variable_get('site_name', 'Drupal'),
    )) . '</div>';
    $book_menus = array();
    foreach ($nids as $nid) {
      $node = node_load($nid);
      $tree = menu_tree_all_data($node->book['menu_name'], $node->book);
      $data = array_shift($tree);
      $output .= theme('book_title_link', $data['link']);
      $output .= $data['below'] ? menu_tree_output($data['below']) : '';
    }
    if ($output) {
      $output = theme('site_map_box', $title, $description . $output, 'sitemap-book');
    }
  }
  return $output;
}

/**
 * Render menus.
 */
function _site_map_menus() {
  global $language;
  if (module_exists('locale')) {
    $mids = variable_get('site_map_show_menus_' . $language->language, array());
  }
  else {
    $mids = variable_get('site_map_show_menus', array());
  }
  $output = '';
  if ($mids) {
    foreach ($mids as $mid) {
      $menu = menu_load($mid);

      // Use menu_tree_all_data to retrieve the expanded tree.
      $tree = menu_tree_all_data($mid);
      if (module_exists('i18nmenu')) {
        i18nmenu_localize_tree($tree);
      }
      $menu_display = menu_tree_output($tree);
      if (!empty($menu_display)) {
        $title = $menu['title'];
        $output .= theme('site_map_box', $title, $menu_display, 'sitemap-menu');
      }
    }
  }
  return $output;
}

/**
 * Returns FAQ if faq module is enabled.
 */
function _site_map_faq() {
  $output = '';
  if (module_exists('faq')) {
    $title = variable_get('faq_title', t('Frequently Asked Questions'));
    $output = faq_get_faq_list();
    $output = theme('site_map_box', $title, $output, 'sitemap-faq');
  }
  return $output;
}

/**
 * This function can be called from blocks or pages as desired.
 */
function _site_map_taxonomys() {
  $output = '';
  if (module_exists('taxonomy') && ($vids = variable_get('site_map_show_vocabularies', array()))) {
    $result = db_query('SELECT vid, name, description FROM {vocabulary} WHERE vid IN (' . db_placeholders($vids, 'int') . ') ORDER BY weight ASC, name', $vids);
    while ($t = db_fetch_object($result)) {
      if (module_exists('i18ntaxonomy')) {
        $t->name = tt("taxonomy:vocabulary:{$t->vid}:name", $t->name);
      }
      $output .= _site_map_taxonomy_tree($t->vid, $t->name, $t->description);
    }
  }
  return $output;
}

/**
 * Render taxonomy tree.
 *
 * @param int $vid
 *   The results of taxonomy_get_tree() with optional 'count' fields.
 * @param string $name
 *   An optional name for the tree. (Default: NULL)
 * @param string $description
 *   An optional description of the tree. (Default: NULL)
 *
 * @return string
 *   A string representing a rendered tree.
 */
function _site_map_taxonomy_tree($vid, $name = NULL, $description = NULL) {
  $output = '';
  $class = '';
  if ($vid == variable_get('forum_nav_vocabulary', '')) {
    $title = l($name, 'forum');
    $threshold = variable_get('site_map_forum_threshold', -1);
    $forum_link = TRUE;
  }
  else {
    $title = $name ? check_plain(t($name)) : '';
    $threshold = variable_get('site_map_term_threshold', 0);
    $forum_link = FALSE;
  }
  $title .= module_exists('commentrss') && variable_get('commentrss_term', FALSE) ? ' ' . theme('site_map_feed_icon', "crss/vocab/{$vid}", 'comment') : '';
  $last_depth = -1;
  $rss_depth = variable_get('site_map_rss_depth', 'all');
  if (!is_numeric($rss_depth) || $rss_depth < 0) {
    $rss_depth = 'all';
  }
  $cat_depth = variable_get('site_map_categories_depth', 'all');
  if (!is_numeric($cat_depth)) {
    $cat_depth = 'all';
  }
  $output .= !empty($description) ? '<div class="taxonomy-term-description">' . filter_xss_admin($description) . "</div>\n" : '';
  $output .= '<div class="tree">';

  // taxonomy_get_tree() honors access controls.
  $tree = taxonomy_get_tree($vid);
  foreach ($tree as $term) {
    $term->count = taxonomy_term_count_nodes($term->tid);
    if ($term->count <= $threshold) {
      continue;
    }
    if (module_exists('i18ntaxonomy')) {
      $term->name = tt("taxonomy:term:{$term->tid}:name", $term->name);
    }

    // Adjust the depth of the <ul> based on the change
    // in $term->depth since the $last_depth.
    if ($term->depth > $last_depth) {
      for ($i = 0; $i < $term->depth - $last_depth; $i++) {
        $output .= "\n<ul>";
      }
    }
    elseif ($term->depth == $last_depth) {
      $output .= '</li>';
    }
    elseif ($term->depth < $last_depth) {
      $ctr = $last_depth - $term->depth;
      for ($i = 0; $i < $ctr; $i++) {
        $output .= "</li>\n</ul>\n</li>";
      }
    }

    // Display the $term.
    $output .= "\n<li>";
    $term_item = '';
    if ($forum_link) {
      $term_item .= l($term->name, 'forum/' . $term->tid, array(
        'attributes' => array(
          'title' => $term->description,
        ),
      ));
    }
    elseif ($term->count) {
      $term_item .= l($term->name, $cat_depth < 0 ? taxonomy_term_path($term) : "taxonomy/term/{$term->tid}/{$cat_depth}", array(
        'attributes' => array(
          'title' => $term->description,
        ),
      ));
    }
    else {
      $term_item .= check_plain($term->name);
    }
    if (variable_get('site_map_show_count', 1)) {
      $term_item .= " ({$term->count})";
    }
    if (variable_get('site_map_show_rss_links', 1) != 0) {
      $rss_link = theme('site_map_feed_icon', "taxonomy/term/{$term->tid}/{$rss_depth}/feed");
      if (module_exists('commentrss') && variable_get('commentrss_term', FALSE)) {
        $rss_link .= ' ' . theme('site_map_feed_icon', "crss/term/{$term->tid}", 'comment');
      }
      if (variable_get('site_map_show_rss_links', 1) == 1) {
        $term_item .= ' ' . $rss_link;
      }
      else {
        $class = ' sitemap-rss-left';
        $term_item = $rss_link . ' ' . $term_item;
      }
    }
    $output .= $term_item;

    // Reset $last_depth in preparation for the next $term.
    $last_depth = $term->depth;
  }

  // Bring the depth back to where it began, -1.
  if ($last_depth > -1) {
    for ($i = 0; $i < $last_depth + 1; $i++) {
      $output .= "</li>\n</ul>\n";
    }
  }
  $output .= "</div>\n";
  $output = theme('site_map_box', $title, $output, 'sitemap-terms sitemap-terms-' . $vid . $class);
  return $output;
}

/**
 * Get the message which appears above the site map.
 *
 * If the locale module is enabled, an optional language code can be given.
 */
function _sitemap_get_message($lang_code = NULL) {
  if (module_exists('locale')) {
    if ($lang_code == NULL) {
      global $language;
      $lang_code = $language->language;
    }
    $message = filter_xss_admin(variable_get("site_map_message_{$lang_code}", ''));
  }
  else {
    $message = filter_xss_admin(variable_get('site_map_message', ''));
  }
  return $message;
}

Functions

Namesort descending Description
site_map_block Implements hook_block().
site_map_help Implements hook_help().
site_map_menu Implements hook_menu().
site_map_page Menu callback for the site map.
site_map_perm Implements hook_perm().
site_map_theme Implements hook_theme().
theme_site_map_box Return a themed sitemap box.
theme_site_map_display Return a themed site map.
theme_site_map_feed_icon Return a themed feed icon.
_sitemap_get_message Get the message which appears above the site map.
_site_map_audio Returns audio content if audio module is enabled.
_site_map_blogs Render the latest blog authors.
_site_map_books Render books.
_site_map_faq Returns FAQ if faq module is enabled.
_site_map_front_page Returns front page details.
_site_map_menus Render menus.
_site_map_taxonomys This function can be called from blocks or pages as desired.
_site_map_taxonomy_tree Render taxonomy tree.
_site_map_video Returns video content if video module is enabled.