Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Pagination
- {
- private $last;
- private $next;
- private $prev;
- private $first;
- private $items;
- private $current;
- private $options = array(
- 'separator' => '...',
- 'sideItemsCount' => 1,
- 'edgeItemsCount' => 1,
- 'activeClass' => 'active',
- 'paginationClass' => '',
- 'showNextPrevButtons' => false,
- 'renderOnePage' => false
- );
- public function __construct(array $options = array())
- {
- $this->setOptions($options);
- }
- public function __toString()
- {
- return '';
- }
- public function last()
- {
- return $this->last;
- }
- public function next()
- {
- return $this->next;
- }
- public function prev()
- {
- return $this->prev;
- }
- public function first()
- {
- return $this->first;
- }
- public function items()
- {
- return $this->items;
- }
- public function current()
- {
- return $this->current;
- }
- public function paginate($current, $pages)
- {
- $this->current = $current;
- $this->pages = $pages;
- $this->last = $pages;
- $this->first = 1;
- $this->prev = ($this->current > 1) ? $this->current - 1 : null;
- $this->next = ($this->current) < $this->pages ? $this->current + 1 : null;
- $this->setItems();
- return $this;
- }
- public function setOption(string $option, $value)
- {
- $this->options[$option] = $value;
- return $this;
- }
- public function setOptions(array $options)
- {
- foreach ($this->options as $option => $value) {
- $this->options[$option] = isset($options[$option]) ? $options[$option] : $this->options[$option];
- }
- return $this;
- }
- private function visibleEdgeItemsCount()
- {
- $separators = $this->options['separator'] ? 2 : 1;
- return 2 * $this->options['sideItemsCount'] + $this->options['edgeItemsCount'] + $separators;
- }
- private function edgeItems()
- {
- $edgeItems = array();
- for ($i = 1; $i <= $this->options['edgeItemsCount']; $i++) {
- $edgeItems[] = $i;
- }
- for ($i = 0; $i < $this->options['edgeItemsCount']; $i++) {
- $edgeItems[] = $this->last - $i;
- }
- return $edgeItems;
- }
- private function sideItems()
- {
- $sideItems = array();
- // when current page is in start edge
- if ($this->current + $this->options['sideItemsCount'] <= $this->visibleEdgeItemsCount()) {
- $start = $this->first;
- $end = $this->visibleEdgeItemsCount();
- }
- // when current page is in end edge
- elseif ($this->current - $this->options['sideItemsCount'] > $this->last - $this->visibleEdgeItemsCount()) {
- $start = $this->last - $this->visibleEdgeItemsCount() + 1;
- $end = $this->last;
- }
- else {
- $start = $this->current - $this->options['sideItemsCount'];
- $end = $this->current + $this->options['sideItemsCount'];
- }
- for ($i = $start; $i <= $end; $i++) {
- if ($i > 0 && $i <= $this->last) {
- $sideItems[] = $i;
- }
- }
- return $sideItems;
- }
- private function setItems()
- {
- $visiblePages = array_unique(array_merge($this->edgeItems(), $this->sideItems()));
- $this->items = array();
- for ($page = 1; $page <= $this->pages; $page++) {
- if (in_array($page, $visiblePages)) {
- $this->items[] = $page;
- }
- elseif ($page == ($this->first + $this->options['edgeItemsCount'])) {
- if ($this->options['separator']) {
- $this->items[] = $this->options['separator'];
- }
- }
- elseif ($page == ($this->last - $this->options['edgeItemsCount'])) {
- if ($this->options['separator']) {
- $this->items[] = $this->options['separator'];
- }
- }
- }
- return $this->items;
- }
- public function checkIfPaginationIsNeeded()
- {
- return $this->options['renderOnePage'] || $this->last > 1;
- }
- public function fresh()
- {
- $this->setItems();
- return $this;
- }
- public function render($echo = false, $prevText = 'prev', $nextText = 'next')
- {
- if (!$this->checkIfPaginationIsNeeded()) {
- return false;
- }
- $content = '';
- if ($this->options['showNextPrevButtons'] && $this->prev) {
- $content .= '<li class="pagination-prev-page"><a class="pagination-prev-link" href="?page=' . $this->prev . '">'. $prevText .'</a></li>';
- }
- foreach ($this->items as $item) {
- if ($item == $this->options['separator']) {
- $content .= '<li class="pagination-list-item pagination-separator-item"><span class="pagination-separator">' . $item . '</span></li>';
- } else {
- $activeClass = ($this->current == $item) ? ' ' . $this->options['activeClass'] : '';
- $link = '<a class="pagination-page-link'. $activeClass .'" href="?page=' . $item . '">' . $item . "</a>";
- $content .= '<li class="pagination-list-item">' . $link .'</li>';
- }
- }
- if ($this->options['showNextPrevButtons'] && $this->prev) {
- $content .= '<li class="pagination-next-page"><a class="pagination-next-link" href="?page=' . $this->next . '">' . $nextText . '</a></li>';
- }
- // $html = '<ul class="' . $this->options['paginationClass'] . '">' . $content . '</ul>';
- $html = $content;
- if ($echo) {
- echo $html;
- }
- return $html;
- }
- }
Add Comment
Please, Sign In to add comment