You are here

function theme_tableofcontents_toc in Table of Contents 7

Same name and namespace in other branches
  1. 5.2 tableofcontents.module \theme_tableofcontents_toc()
  2. 6.3 tableofcontents.pages.inc \theme_tableofcontents_toc()
  3. 6.2 tableofcontents.module \theme_tableofcontents_toc()
  4. 7.2 tableofcontents.themes.inc \theme_tableofcontents_toc()

Theme the output of a table of contents.

Parameters

$toc A TOC object with the options, table of contents, headers, and files.:

Return value

Rendered HTML to be displayed.

1 theme call to theme_tableofcontents_toc()
_tableofcontents_replace_toc_tags in ./tableofcontents.pages.inc
This function replaces one [toc ...] tag.

File

./tableofcontents.pages.inc, line 857
Applies the filter functions.

Code

function theme_tableofcontents_toc($variables) {
  $toc = $variables['toc'];

  // TODO: at this time, the hideshow works with all TOCs at once
  if ($toc->hideshow && !isset($toc->on_print_pages)) {

    // We add the link with JavaScript so if someone does not have JavaScript, they do
    // not see a link that doesn nothing for them.
    $hs = '<span class="toc-toggle-message">&nbsp;</span>';
    $js = '<script type="text/javascript">toc_collapse=' . ($toc->collapsed ? 1 : 0) . ';</script>';
  }
  else {
    $hs = '';
    $js = '';
  }

  // TODO: we should only define the id="toc" for the first TOC
  if ($toc->id) {
    $id = ' id="' . check_plain($toc->id) . '"';
  }
  else {
    $id = '';
  }
  $output = $js . "<div class=\"toc\"" . $id . ">\n";
  if ($toc->title != '<none>') {
    $output .= "<div class=\"toc-title\">" . t($toc->title) . $hs . "</div>\n";
  }
  elseif ($hs) {
    $output .= $hs;
  }
  $list = $toc->numbering == 4 ? 'ol' : 'ul';
  $output .= "<div class=\"toc-list\">\n" . "<" . $list . ">\n";
  $has_content = FALSE;

  // Process nested lists.
  $level = $toc->minlevel;
  $first = TRUE;
  foreach ($toc->headers as $h) {
    $cur_level = $h->level;
    if ($cur_level >= $toc->minlevel && $cur_level <= $toc->maxlevel) {
      $has_content = TRUE;

      // Be sure to deal with skipping between non-adjacent h levels.
      if ($cur_level > $level) {
        do {
          $output .= "\n<" . $list . ">\n";
          ++$level;
        } while ($cur_level > $level);
      }
      elseif ($cur_level < $level) {
        do {
          $output .= "</li>\n</" . $list . ">\n";
          --$level;
        } while ($cur_level < $level);
        $output .= "</li>\n";
      }
      elseif (!$first) {

        // we know that $cur_level == $level here
        // Close list items at the same level (except the very first time)
        $output .= "</li>\n";
      }
      if ($h->number) {
        $number = '<span class="toc-number">' . $h->number . '</span>';
      }
      else {
        $number = '';
      }
      $list_class = 'toc-level-' . ($level - $toc->minlevel + 1);
      $title = strip_tags($h->title, $toc->allowed_tags);

      // insert the li element
      $output .= "\t<li class=\"{$list_class}\">";
      if (isset($toc->on_print_pages)) {
        $output .= $number . $title;
      }
      else {
        $output .= "<a href=\"#" . $h->identifier . "\">" . $number . $title . "</a>";
      }

      // First element processed after first iteration, write it down.
      $first = FALSE;
    }
  }

  // Did we recurse back out? If not, close open lists.
  for (; $level > $toc->minlevel; --$level) {
    $output .= "</li>\n</" . $list . ">\n";
  }
  $output .= "</li>\n";

  // If we've been passed an array of files, add them to the table of contents.
  if (!empty($toc->files)) {
    $has_files = FALSE;
    $files_output = '<li><a href="#attachments">' . t("Attachments") . "</a>";
    $files_output .= "<" . $list . ">";
    foreach ($toc->files as $file) {
      if ($file->list && !$file->remove) {
        $has_files = TRUE;
        $url = strpos($file->fid, 'upload') === FALSE ? $file->filepath : file_create_filename($file->filename, file_create_path());
        $file_href = file_create_url($url);
        $file_text = $file->description ? $file->description : $file->filename;

        // If there is no function providing phptemplate_file_icon in
        // template.php or similar then the theme file_icon will fail gracefully.
        $files_output .= '<li>' . theme('file_icon', $file) . l($file_text, $file_href) . "</li>\n";
      }
    }
    if ($has_files) {
      $output .= $files_output . "</" . $list . "></li>\n";
      $has_content = TRUE;
    }
  }
  if (!$has_content) {

    // this happens when all the header levels are either too small or too large
    // and if there is no attachment either
    return '';
  }
  $output .= "</" . $list . ">\n</div>\n</div>";
  return theme('tableofcontents_toc_text', array(
    $output,
  ));
}