Frontend\Core\Engine\Base\Block::parsePagination PHP Method

parsePagination() protected method

Parse pagination
protected parsePagination ( string $query_parameter = 'page' )
$query_parameter string
    protected function parsePagination($query_parameter = 'page')
    {
        $pagination = null;
        $showFirstPages = false;
        $showLastPages = false;
        $useQuestionMark = true;
        // validate pagination array
        if (!isset($this->pagination['limit'])) {
            throw new Exception('no limit in the pagination-property.');
        }
        if (!isset($this->pagination['offset'])) {
            throw new Exception('no offset in the pagination-property.');
        }
        if (!isset($this->pagination['requested_page'])) {
            throw new Exception('no requested_page available in the pagination-property.');
        }
        if (!isset($this->pagination['num_items'])) {
            throw new Exception('no num_items available in the pagination-property.');
        }
        if (!isset($this->pagination['num_pages'])) {
            throw new Exception('no num_pages available in the pagination-property.');
        }
        if (!isset($this->pagination['url'])) {
            throw new Exception('no URL available in the pagination-property.');
        }
        // should we use a questionmark or an ampersand
        if (mb_strpos($this->pagination['url'], '?') !== false) {
            $useQuestionMark = false;
        }
        // no pagination needed
        if ($this->pagination['num_pages'] < 1) {
            return;
        }
        // populate count fields
        $pagination['num_pages'] = $this->pagination['num_pages'];
        $pagination['current_page'] = $this->pagination['requested_page'];
        // define anchor
        $anchor = isset($this->pagination['anchor']) ? '#' . $this->pagination['anchor'] : '';
        // as long as we have more then 5 pages and are 5 pages from the end we should show all pages till the end
        if ($this->pagination['requested_page'] > 5 && $this->pagination['requested_page'] >= $this->pagination['num_pages'] - 4) {
            // init vars
            $pagesStart = $this->pagination['num_pages'] > 7 ? $this->pagination['num_pages'] - 5 : $this->pagination['num_pages'] - 6;
            $pagesEnd = $this->pagination['num_pages'];
            // fix for page 6
            if ($this->pagination['num_pages'] == 6) {
                $pagesStart = 1;
            }
            // show first pages
            if ($this->pagination['num_pages'] > 7) {
                $showFirstPages = true;
            }
        } elseif ($this->pagination['requested_page'] <= 5) {
            // as long as we are below page 5 and below 5 from the end we should show all pages starting from 1
            // init vars
            $pagesStart = 1;
            $pagesEnd = 6;
            // when we have 7 pages, show 7 as end
            if ($this->pagination['num_pages'] == 7) {
                $pagesEnd = 7;
            } elseif ($this->pagination['num_pages'] <= 6) {
                // when we have less then 6 pages, show the maximum page
                $pagesEnd = $this->pagination['num_pages'];
            }
            // show last pages
            if ($this->pagination['num_pages'] > 7) {
                $showLastPages = true;
            }
        } else {
            // page 6
            // init vars
            $pagesStart = $this->pagination['requested_page'] - 2;
            $pagesEnd = $this->pagination['requested_page'] + 2;
            $showFirstPages = true;
            $showLastPages = true;
        }
        // show previous
        if ($this->pagination['requested_page'] > 1) {
            // build URL
            if ($useQuestionMark) {
                $url = $this->pagination['url'] . '?' . $query_parameter . '=' . ($this->pagination['requested_page'] - 1);
            } else {
                $url = $this->pagination['url'] . '&' . $query_parameter . '=' . ($this->pagination['requested_page'] - 1);
            }
            // set
            $pagination['show_previous'] = true;
            $pagination['previous_url'] = $url . $anchor;
            // flip ahead
            $this->header->addLink(array('rel' => 'prev', 'href' => SITE_URL . $url . $anchor));
        }
        // show first pages?
        if ($showFirstPages) {
            // init var
            $pagesFirstStart = 1;
            $pagesFirstEnd = 1;
            // loop pages
            for ($i = $pagesFirstStart; $i <= $pagesFirstEnd; ++$i) {
                // build URL
                if ($useQuestionMark) {
                    $url = $this->pagination['url'] . '?' . $query_parameter . '=' . $i;
                } else {
                    $url = $this->pagination['url'] . '&' . $query_parameter . '=' . $i;
                }
                // add
                $pagination['first'][] = array('url' => $url . $anchor, 'label' => $i);
            }
        }
        // build array
        for ($i = $pagesStart; $i <= $pagesEnd; ++$i) {
            // init var
            $current = $i == $this->pagination['requested_page'];
            // build URL
            if ($useQuestionMark) {
                $url = $this->pagination['url'] . '?' . $query_parameter . '=' . $i;
            } else {
                $url = $this->pagination['url'] . '&' . $query_parameter . '=' . $i;
            }
            // add
            $pagination['pages'][] = array('url' => $url . $anchor, 'label' => $i, 'current' => $current);
        }
        // show last pages?
        if ($showLastPages) {
            // init var
            $pagesLastStart = $this->pagination['num_pages'];
            $pagesLastEnd = $this->pagination['num_pages'];
            // loop pages
            for ($i = $pagesLastStart; $i <= $pagesLastEnd; ++$i) {
                // build URL
                if ($useQuestionMark) {
                    $url = $this->pagination['url'] . '?' . $query_parameter . '=' . $i;
                } else {
                    $url = $this->pagination['url'] . '&' . $query_parameter . '=' . $i;
                }
                // add
                $pagination['last'][] = array('url' => $url . $anchor, 'label' => $i);
            }
        }
        // show next
        if ($this->pagination['requested_page'] < $this->pagination['num_pages']) {
            // build URL
            if ($useQuestionMark) {
                $url = $this->pagination['url'] . '?' . $query_parameter . '=' . ($this->pagination['requested_page'] + 1);
            } else {
                $url = $this->pagination['url'] . '&' . $query_parameter . '=' . ($this->pagination['requested_page'] + 1);
            }
            // set
            $pagination['show_next'] = true;
            $pagination['next_url'] = $url . $anchor;
            // flip ahead
            $this->header->addLink(array('rel' => 'next', 'href' => SITE_URL . $url . $anchor));
        }
        // multiple pages
        $pagination['multiple_pages'] = $pagination['num_pages'] == 1 ? false : true;
        // assign pagination
        $this->tpl->assign('pagination', $pagination);
    }