<?php
namespace App\Mvc\View\Listing\Members; class Pagination { protected $currentPage = 0; protected $totalItems = 0; protected $itemsPerPage = 0; protected $itemsPerPageList = [5=>5, 10=>10, 25=>25, 50=>50]; protected $format = ''; protected $totalItemsIsCached; public function setCurrentPage($currentPage) { $this->currentPage = $currentPage; return $this; } public function getCurrentPage() { return $this->currentPage; } public function setTotalItems($totalItems) { $this->totalItems = $totalItems; return $this; } public function getTotalItems() { return $this->totalItems; } public function getTotalItemsForDisplay() { if ($this->totalItemsIsCached) { if ($this->totalItems==0) { return '0'; } elseif ($this->totalItems<50) { return $this->totalItems; } elseif ($this->totalItems<900) { return sprintf("~%d", 10*ceil($this->totalItems/10)); } elseif ($this->totalItems<1100) { return sprintf("~%.0fk", round($this->totalItems/1000, 2)); } elseif ($this->totalItems<9000) { return sprintf("~%.1fk", round($this->totalItems/1000, 2)); } elseif ($this->totalItems<11000) { return sprintf("~%.0fk", round($this->totalItems/1000, 2)); } elseif ($this->totalItems<900000) { return sprintf("~%.0fk", round($this->totalItems/1000, 2)); } else { return sprintf("~%.2fM", round($this->totalItems/(1000*1000), 3)); } } else { return $this->totalItems; } } public function setTotalItemsIsCached($isCached = true) { $this->totalItemsIsCached = $isCached; return $this; } public function getTotalItemsIsCached() { return $this->totalItemsIsCached; } public function setItemsPerPage($itemsPerPage) { if ($itemsPerPage==0) { $itemsPerPage = 1; \Log::getInstance()->error(sprintf('[%s:%s] Check why the itemsPerPage is 0 in pagination class.', __METHOD__, __LINE__)); } $this->itemsPerPage = $itemsPerPage; return $this; } public function getItemsPerPage() { return $this->itemsPerPage; } public function getTotalPages() { return (int)ceil($this->totalItems / $this->itemsPerPage); } public function getFirstDisplayedItem() { $index = min( $this->currentPage*$this->itemsPerPage, $this->totalItems - $this->totalItems % $this->itemsPerPage ); return $index; } public function getLastDisplayedItem() { $index = min( $this->currentPage*$this->itemsPerPage+$this->itemsPerPage, $this->totalItems ); return $index; } public function getItemsPerPageList() { return $this->itemsPerPageList; } public function setItemsPerPageList($itemsPerPageList) { $this->itemsPerPageList = $itemsPerPageList; return $this; } public function setFormat($format) { $this->format = $format; return $this; } public function getFormat() { return $this->format; } public function getPagesList() { $pagination = []; $maxItemsPerInterval = 5; $distanceMultiplier = 10; $maxInterval = PHP_INT_MAX/100; $itemsMarginWidth = 2; $items = 0; $distance = 1; $totalPages = $this->getTotalPages(); $currentPage = $this->getCurrentPage(); for ($i=$this->currentPage; $i<$totalPages; $i++) { $add = false; if ($items>$maxItemsPerInterval && $distance<$maxInterval) { $items = 0; $distance*=$distanceMultiplier; } if ($i % $distance===($distance-1)) { $add = true; } if ($i >= ($totalPages - $itemsMarginWidth)) { $add = true; } if($add){ $items++; $pagination[$i] = [ 'index' => $i, 'distance' => $distance, 'isCurrent' => ($i===$currentPage) ]; } } $items = 0; $distance = 1; for ($i=$currentPage-1; $i>=0; $i--) { $add = false; if ($items>=$maxItemsPerInterval && $distance<$maxInterval) { $items = 0; $distance*=$distanceMultiplier; } if ($i % $distance===($distance-1)) { $add = true; } if ($i < ($itemsMarginWidth)) { $add = true; } if ($add) { $items++; $pagination[$i] = [ 'index' => $i, 'distance' => $distance, 'isCurrent' => ($i===$currentPage) ]; } } ksort($pagination); return array_values($pagination); } } 