You are here

function advanced_forum_create_topic_pager in Advanced Forum 7.2

Same name and namespace in other branches
  1. 6.2 advanced_forum.module \advanced_forum_create_topic_pager()

Creates a pager to place on each multi-page topic of the topic listing page.

Parameters

int $max_pages_to_display: Number of pages to include on the pager.

object $topic: Topic object to create a pager for.

Return value

object Object containing the linked pages ready assembly by the theme function.

1 call to advanced_forum_create_topic_pager()
advanced_forum_preprocess_advanced_forum_topic_pager in includes/theme.inc
Preprocess forum topic pager.

File

./advanced_forum.module, line 1275
Enables the look and feel of other popular forum software.

Code

function advanced_forum_create_topic_pager($max_pages_to_display, $topic) {

  // Find the number of comments per page for the node type of the topic.
  $comments_per_page = variable_get('comment_default_per_page_' . $topic->type, 50);
  if ($max_pages_to_display > 0 && $topic->comment_count > $comments_per_page) {

    // Topic has more than one page and a pager is wanted. Start off the
    // first page because that doesn't have a query.
    $pager_array = array();
    $current_display_page = 1;

    // @codingStandardsIgnoreStart
    $pager_array[0] = l('1', "node/{$topic->nid}");

    // @codingStandardsIgnoreEnd
    // Find the ending point. The pager URL is always 1 less than
    // the number being displayed because the first page is 0.
    $last_display_page = ceil($topic->comment_count / $comments_per_page);
    $last_pager_page = $last_display_page - 1;

    // Add pages until we run out or until we hit the max to show.
    while ($current_display_page < $last_display_page && $current_display_page < $max_pages_to_display) {

      // Move to the next page.
      $current_display_page++;

      // The page number we link to is 1 less than what's displayed.
      $link_to_page = $current_display_page - 1;

      // Add the link to the array.
      $pager_array[$link_to_page] = l($current_display_page, "node/{$topic->nid}", array(
        'query' => array(
          'page' => $link_to_page,
        ),
      ));
    }

    // Move to the next page.
    $current_display_page++;
    if ($current_display_page == $last_display_page) {

      // We are one past the max to display, but it's the last page,
      // so putting the ...last is silly. Just display it normally.
      $link_to_page = $current_display_page - 1;
      $pager_array[$link_to_page] = l($current_display_page, "node/{$topic->nid}", array(
        'query' => array(
          'page' => $link_to_page,
        ),
      ));
    }
    if ($current_display_page < $last_display_page) {

      // We are one past the max to display and still aren't
      // on the last page, so put in ... Last Page(N)
      $text = t('Last Page');
      $pager_last_text = l($text, "node/{$topic->nid}", array(
        'query' => array(
          'page' => $last_pager_page,
        ),
      ));
      $pager_last_number = l($last_display_page, "node/{$topic->nid}", array(
        'query' => array(
          'page' => $last_pager_page,
        ),
      ));

      // Create last page array to enable more customization for themers.
      $pager_last = array(
        'number' => $last_display_page,
        'link' => "node/{$topic->nid}",
        'options' => array(
          'query' => array(
            'page' => $last_pager_page,
          ),
        ),
      );
    }
    $topic_pager = new stdClass();
    $topic_pager->initial_pages = empty($pager_array) ? array() : $pager_array;
    $topic_pager->last_page_text = empty($pager_last_text) ? '' : $pager_last_text;
    $topic_pager->last_page_number = empty($pager_last_number) ? '' : $pager_last_number;
    $topic_pager->last_page = empty($pager_last) ? array() : $pager_last;
    return $topic_pager;
  }
}