You are here

protected function Progressive::buildProgressivePageList in Pagerer 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/pagerer/Progressive.php \Drupal\pagerer\Plugin\pagerer\Progressive::buildProgressivePageList()

Return an array of pages progressively more distant from current.

Parameters

array $pages: Array of pages already enlisted, to prevent override.

int $scale_factor: Scale factor to be used in the progressive series.

int $ratio: Ratio to be used in the progressive series.

int $limit: (Optional) limit the quantity of pages enlisted.

Return value

array render array of pages items.

1 call to Progressive::buildProgressivePageList()
Progressive::buildPageList in src/Plugin/pagerer/Progressive.php
Return an array of pages.

File

src/Plugin/pagerer/Progressive.php, line 109

Class

Progressive
Pager style with links to pages progressively more distant from current.

Namespace

Drupal\pagerer\Plugin\pagerer

Code

protected function buildProgressivePageList(array $pages, $scale_factor, $ratio, $limit = NULL) {
  $current = $this->pager
    ->getCurrentPage();
  $total = $this->pager
    ->getTotalPages();
  $last = $this->pager
    ->getLastPage();

  // Avoid endless loop in converging series.
  if ($ratio < 1) {
    $ratio = 1;
  }
  $offset = 0;
  for ($i = 0; TRUE; $i++) {

    // Breaks if limit reached.
    if ($limit and $i > $limit - 1) {
      break;
    }

    // Offset for this cycle.
    $offset = intval($scale_factor * pow($ratio, $i));

    // Breaks if offset > than total pages.
    if ($offset > $total) {
      break;
    }

    // Negative offset.
    $target = $current - $offset;
    if ($target > 0 && !isset($pages[$target])) {
      $pages[$target] = $this
        ->getPageItem(-$offset, $this
        ->getOption('progr_links'), TRUE);
    }

    // Positive offset.
    $target = $current + $offset;
    if ($target < $last && !isset($pages[$target])) {
      $pages[$target] = $this
        ->getPageItem($offset, $this
        ->getOption('progr_links'), TRUE);
    }
  }
  return $pages;
}