You are here

function _advanced_forum_preprocess_forum_list in Advanced Forum 6.2

Same name and namespace in other branches
  1. 7.2 includes/advanced_forum_preprocess_forum_list.inc \_advanced_forum_preprocess_forum_list()

@file Holds the contents of a preprocess function moved into its own file to ease memory requirements and having too much code in one file.

1 call to _advanced_forum_preprocess_forum_list()
advanced_forum_preprocess_forum_list in includes/theme.inc
Preprocesses template variables for the forum list template.

File

includes/advanced_forum_preprocess_forum_list.inc, line 9
Holds the contents of a preprocess function moved into its own file to ease memory requirements and having too much code in one file.

Code

function _advanced_forum_preprocess_forum_list(&$variables) {

  // Tell Drupal to use our forum list template file.
  advanced_forum_add_template_suggestions("forum-list", $variables['template_files']);

  // Added variable for default collapsed configuration
  $variables['collapsed_main_list'] = variable_get('advanced_forum_default_collapsed_list', '');

  // Add needed items for the collapsible containers.
  $variables['collapsible'] = variable_get('advanced_forum_collapsible_containers', 'toggle');
  if ($variables['collapsible'] != 'none') {
    drupal_add_js(drupal_get_path('module', 'advanced_forum') . '/js/advanced_forum.js', 'module');
    drupal_add_js(array(
      'advanced_forum' => array(
        'effect' => $variables['collapsible'],
      ),
    ), 'setting');

    // Add JS setting for default collapsed config so that intial display for this list will be collapsed
    drupal_add_js(array(
      'advanced_forum' => array(
        'default_collapsed_list' => $variables['collapsed_main_list'],
      ),
    ), 'setting');
  }
  else {
    $variables['collapsible'] = FALSE;
  }

  // The tid is the ID of the container or forum we are in. Assign it to
  // $parent_id for easy reference, assign it to the forum_id template variable
  // to give it a nice name for themers, then get rid of the original variable.
  $parent_id = isset($variables['tid']) ? $variables['tid'] : 0;
  $variables['forum_id'] = $parent_id;
  unset($variables['tid']);

  // Add a variable showning if we are using taxonomy image for easier theming.
  $variables['use_taxonomy_image'] = function_exists('taxonomy_image_display') && variable_get('advanced_forum_use_taxonomy_image', TRUE);

  // Process the containers and forums and move them to a new variable.
  $items = array();
  $lowest_depth = 0;
  foreach ($variables['forums'] as $id => $forum) {
    if (empty($forum->container)) {
      $items[$id] = advanced_forum_process_forum($forum);
    }
    else {
      $items[$id] = advanced_forum_process_container($forum);
    }

    // Figure out how deep the hierarchy goes for the next step.
    if ($forum->depth > $lowest_depth) {
      $lowest_depth = $forum->depth;
    }
  }

  // Calculate number of and number of new child posts for each item.
  for ($depth = $lowest_depth; $depth > 0; $depth--) {
    foreach ($items as $id => $item) {
      if ($item->depth == $depth) {
        $items[$item->parents['0']]->child_total_topics += $item->total_topics + $item->child_total_topics;
        $items[$item->parents['0']]->child_new_topics += $item->new_topics + $item->child_new_topics;
        $items[$item->parents['0']]->child_total_posts += $item->total_posts + $item->child_total_posts;
        $items[$item->parents['0']]->child_new_posts += $item->new_posts + $item->child_new_posts;
      }
    }
  }

  // Loop through all the items and fill the $tables variable that will
  // hold all the tables with the containers and forums organized, processed,
  // and ready for the template.
  if ($parent_id) {

    // On a container page. Fake the main table.
    $table_counter = 1;
    $tables[$table_counter]['table_info'] = advanced_forum_process_container($variables['parents'][0]);
  }
  else {
    $table_counter = 0;
  }
  foreach ($items as $id => $item) {

    // Get a handle on the parent of this item.
    if ($parent_id && $item->depth == 0) {

      // If we are on a container page, the parent of root items is the main
      // container that is being used for the table info.
      $parent = $tables[$table_counter]['table_info'];
    }
    elseif (!empty($variables['forums'][$item->parents[0]])) {

      // For simplicity, we assume forums/containers have only one parent.
      $parent = $variables['forums'][$item->parents[0]];
    }

    // Intentional no "else" because only root items on the main page have
    // no parent and we don't use the variable there.
    // If we aren't on the main forum page, we need to bump up the depth.
    $item_depth = $parent_id ? $item->depth + 1 : $item->depth;
    if (!empty($item->container)) {

      // CONTAINERS.
      if ($item_depth == 0) {

        // Top level container always starts a new table.
        $table_counter++;
        $tables[$table_counter]['table_info'] = $item;
        $tables[$table_counter]['items'] = array();

        // Reset the striping.
        advanced_forum_stripe(TRUE);
      }
      elseif ($item_depth == 1) {

        // Subcontainer at top level is treated like a forum.
        // We set the forum icon here, rather than in the process_forum
        // function because we need to take into account new content
        // in children.
        if ($item->new_topics || $item->new_posts || $item->child_new_topics || $item->child_new_posts) {
          $item->icon_classes = "forum-list-icon forum-list-icon-new-posts";
          $item->icon_text = t("New posts");
        }
        else {
          $item->icon_classes = "forum-list-icon forum-list-icon-default";
          $item->icon_text = t("No new");
        }

        // Set the variable to control the row striping.
        $item->zebra = advanced_forum_stripe();

        // Add the container info to the table's item list.
        $tables[$table_counter]['items'][$id] = $item;
      }
      elseif ($item_depth == 2) {

        // A container elsewhere gets added to the parent's subcontainer list.
        $tables[$table_counter]['items'][$parent->tid]->subcontainer_list[$id] = $item;
      }
    }
    else {

      // FORUMS.
      // We set the forum icon here, rather than in the process_forum
      // function because we need to take into account new content
      // in children.
      if ($item->new_topics || $item->new_posts || $item->child_new_topics || $item->child_new_posts) {
        $item->icon_classes = "forum-list-icon forum-list-icon-new-posts";
        $item->icon_text = t("New posts");
      }
      else {
        $item->icon_classes = "forum-list-icon forum-list-icon-default";
        $item->icon_text = t("No new");
      }
      if ($item_depth == 0) {

        // This is a forum at the root. If it is the first or the first
        // since the last container, make a new table. Otherwise, put it in
        // the previous table.
        if (empty($table_counter) || !empty($tables[$table_counter]['table_info']->container)) {

          // This is the first root item or the current table belongs to a
          // container. Start a new generic one. We need a tid so just grab
          // the tid of the current forum for it.
          $table_counter++;
          $tables[$table_counter]['table_info']->name = t('Forums');
          $tables[$table_counter]['table_info']->description = '';
          $tables[$table_counter]['table_info']->tid = $item->tid;

          // Reset the striping.
          advanced_forum_stripe(TRUE);
        }

        // Set the variable to control the row striping.
        $item->zebra = advanced_forum_stripe();

        // Add the forum info to the table's item list.
        $tables[$table_counter]['items'][$id] = $item;
      }
      elseif ($item_depth == 1) {

        // Main forum. Add it to the item list.
        if (empty($item->container)) {
          $item->zebra = advanced_forum_stripe();
        }
        $tables[$table_counter]['items'][$id] = $item;
      }
      elseif ($item_depth == 2) {

        // Subforum.
        $tables[$table_counter]['items'][$parent->tid]->subforum_list[$id] = $item;
      }
    }
  }

  // Theme subcontainers and subforums.
  foreach ($tables as $table_id => $table) {
    foreach ($table['items'] as $item_id => $item) {
      if (!empty($item->subforum_list)) {
        $tables[$table_id]['items'][$item_id]->subforums = theme('advanced_forum_subforum_list', $item->subforum_list);
      }
      if (!empty($item->subcontainer_list)) {
        $tables[$table_id]['items'][$item_id]->subcontainers = theme('advanced_forum_subcontainer_list', $item->subcontainer_list);
      }
    }
  }
  $variables['tables'] = $tables;

  // Remove unneeded variables.
  unset($variables['zebra']);
  unset($variables['forums']);
}