You are here

function advanced_forum_process_forum in Advanced Forum 7.2

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

Prepare an individual forum for display.

1 call to advanced_forum_process_forum()
_advanced_forum_preprocess_forum_list in includes/advanced_forum_preprocess_forum_list.inc
Preprocess forum list.

File

includes/advanced_forum_preprocess_forum_list.inc, line 299
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_process_forum($forum) {

  // Create a variable to check if the item is a container in the template.
  $forum->is_container = FALSE;

  // Create the link to the forum.
  $forum->link = url("forum/{$forum->tid}");

  // Sanitise the name and description so they can be safely printed.
  // We don't do check_plain() for subforum names because those should go
  // through l() in the advanced_forum_subforum_list theme function and l()
  // includes check_plain().
  $forum->name = empty($forum->parents[0]) ? check_plain($forum->name) : filter_xss_admin($forum->name);
  $forum->description = !empty($forum->description) ? filter_xss_admin($forum->description) : '';

  // Initialize these variables to avoid notices later since not all forums
  // have new content or even any content at all.
  $forum->total_topics = 0;
  $forum->child_total_topics = 0;
  $forum->new_topics = 0;
  $forum->new_topics_text = '';
  $forum->new_topics_link = '';
  $forum->child_new_topics = 0;
  $forum->total_posts = 0;
  $forum->child_total_posts = 0;
  $forum->new_posts = 0;
  $forum->new_posts_text = '';
  $forum->new_posts_link = '';
  $forum->child_new_posts = 0;

  // Rename these to make them more descriptive.
  if (isset($forum->num_topics)) {
    $forum->total_topics = $forum->num_topics;
    unset($forum->num_topics);
  }
  if (isset($forum->num_posts)) {
    $forum->total_posts = $forum->num_posts;
    unset($forum->num_posts);
  }

  // If the viewer is authenticated, check for new topics and posts.
  global $user;
  if ($user->uid) {

    // New topics.
    $forum->new_topics = _forum_topics_unread($forum->tid, $user->uid);
    if ($forum->new_topics) {
      $forum->new_topics_text = format_plural($forum->new_topics, '1 new', '@count new');
      $forum->new_topics_link = url("forum/{$forum->tid}", array(
        'fragment' => 'new',
      ));
    }

    // New posts are optional because the query is slow.
    if (variable_get('advanced_forum_get_new_comments', 0)) {
      $forum->new_posts = advanced_forum_unread_replies_in_forum($forum->tid, $user->uid) + $forum->new_topics;
      if ($forum->new_posts) {
        $forum->new_posts_text = format_plural($forum->new_posts, '1 new', '@count new');
        $forum->new_posts_path = "forum/{$forum->tid}";
        $forum->new_posts_link = url("forum/{$forum->tid}", array(
          'fragment' => 'new',
        ));
      }
    }
  }

  // Process the "last post" object into a printable string.
  // Trying to copy the string back into the variable directly caused odd bugs
  // so we move it to a temp variable then unset the original.
  // Before doing so, however, we make a copy in case another module implementing
  // hook_TYPE_alter() needs access to the raw data.
  $forum->last_post_obj = empty($forum->last_post) ? new stdClass() : $forum->last_post;
  $last_post = empty($forum->last_post) ? '' : $forum->last_post;
  unset($forum->last_post);
  $forum->last_post = theme('forum_submitted', array(
    'topic' => $last_post,
  ));

  // Display forum images using Field API.
  $entity = entity_load('taxonomy_term', array(
    $forum->tid,
  ));
  $field_name = variable_get('advanced_forum_forum_image_field', '');
  if (!empty($entity[$forum->tid]->{$field_name})) {
    $field = $entity[$forum->tid]->{$field_name};
    global $language;
    $langcode = isset($field[$language->language]) ? $language->language : LANGUAGE_NONE;
    if (isset($field[$langcode])) {
      $display = array(
        'label' => 'hidden',
        'type' => 'image',
        'settings' => array(
          'image_style' => variable_get('advanced_forum_forum_image_preset', ''),
          'image_link' => '',
        ),
      );
      $output = field_view_value('taxonomy_term', $entity[$forum->tid], $field_name, $field[$langcode][0], $display);
      $forum->forum_image = render($output);
    }
  }
  drupal_alter('advanced_forum', $forum);
  return $forum;
}