You are here

protected function Adaptive::buildPageList in Pagerer 8

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

Return an array of pages.

Return value

array render array of pages items.

Overrides Standard::buildPageList

File

src/Plugin/pagerer/Adaptive.php, line 71

Class

Adaptive
Pager style with links to pages following an adaptive logic.

Namespace

Drupal\pagerer\Plugin\pagerer

Code

protected function buildPageList() {
  $current = $this->pager
    ->getCurrentPage();
  $last = $this->pager
    ->getLastPage();

  // Determine adaptive keys coming from query parameters.
  list($pl, $pr, $px) = [
    0,
    $last,
    NULL,
  ];
  if ($tmp = $this->pager
    ->getAdaptiveKeys()) {

    // Adaptive keys for the specific element exist.
    $tmp = explode('.', $tmp);
    $pl = isset($tmp[0]) ? $tmp[0] ? $tmp[0] : 0 : 0;
    $pr = isset($tmp[1]) ? $tmp[1] : $last;
    $px = isset($tmp[2]) ? $tmp[2] : NULL;
  }

  // First.
  $pages[0] = $this
    ->getPageItem(-$current, 'absolute', FALSE, $current == 0 ? 'page_current' : 'page', FALSE);
  $pages[0]['href'] = $this->pager
    ->getHref($this->parameters, 0, "0.{$last}");

  // Last.
  $pages[$last] = $this
    ->getPageItem($last - $current, 'absolute', FALSE, $current == $last ? 'page_current' : 'page', FALSE);
  $pages[$last]['href'] = $this->pager
    ->getHref($this->parameters, $last, "0.{$last}");

  // Neighborhood.
  $pages = $this
    ->buildNeighborhoodPageList($pages);

  // Adaptive keys left pointed page.
  if ($pl > 0 and !isset($pages[$pl])) {
    $pages[$pl] = $this
      ->getPageItem($pl - $current, $this
      ->getOption('progr_links'), TRUE);
    $pages[$pl]['outer_page'] = TRUE;
  }

  // Adaptive keys right pointed page.
  if ($pr < $last and !isset($pages[$pr])) {
    $pages[$pr] = $this
      ->getPageItem($pr - $current, $this
      ->getOption('progr_links'), TRUE);
    $pages[$pr]['outer_page'] = TRUE;
  }

  // Adaptive pages.
  $pages += $this
    ->buildAdaptivePageList($pages, $pl, $pr, $px);
  ksort($pages);

  // Enrich pages with adaptive markers.
  if ($pages) {
    $kpages = array_keys($pages);

    // Determines first adaptive pages left and right of the neighborhood,
    // if existing.
    $la = $ra = NULL;
    for ($x = 1; $x < count($kpages) - 1; $x++) {
      if (isset($pages[$kpages[$x]]['outer_page'])) {
        if ($kpages[$x] < $current) {
          $la = $kpages[$x];
        }
        if ($kpages[$x] > $current) {
          $ra = $kpages[$x];
          break;
        }
      }
    }

    // Set adaptive markers.
    for ($x = 1; $x < count($kpages) - 1; $x++) {
      $d =& $pages[$kpages[$x]];

      // Adaptive page.
      if (isset($d['outer_page'])) {
        $d['href'] = $this->pager
          ->getHref($this->parameters, $kpages[$x], $kpages[$x - 1] . '.' . $kpages[$x + 1]);
        continue;
      }

      // Else, neighborhood page.
      // Set left page and right page pointers.
      if ($px) {
        $xpl = $pl;
        $xpr = $pr;
      }
      else {
        $xpl = $pl ? $pl : 0;
        $xpr = $pr ? $pr : $last;
      }

      // Set holding marker - determine left and right offset
      // of the page vs current page.
      $off = NULL;
      $xpx = NULL;
      if ($kpages[$x] < $current) {
        $off = $la ? $kpages[$x] - $la : NULL;
      }
      elseif ($kpages[$x] > $current) {
        $off = $ra ? $ra - $kpages[$x] : NULL;
      }

      // If an offset exists, and is larger than half neighborhood,
      // then an holding marker is set. If offset is null, then
      // there are no left (or right) adaptive pointers, so we will
      // reset adaptive keys.
      if ($off) {
        $pager_middle = ceil($this
          ->getOption('quantity') / 2);
        if ($off > $pager_middle) {
          $xpx = !is_null($px) ? $px : $current;
        }
      }
      else {
        if ($kpages[$x] < $current) {
          $xpl = 0;
          $xpr = $current;
        }
        elseif ($kpages[$x] > $current) {
          $xpl = $current;
          $xpr = $last;
        }
      }
      if ($xpx) {
        $page_ak_curr = implode('.', [
          $xpl,
          $xpr,
          $xpx,
        ]);
      }
      else {
        $page_ak_curr = implode('.', [
          $xpl,
          $xpr,
        ]);
      }
      $d['href'] = $this->pager
        ->getHref($this->parameters, $kpages[$x], $page_ak_curr);
    }
  }
  return $pages;
}