123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- <?php namespace Laravel;
- class Paginator {
-
- public $results;
-
- public $page;
-
- public $last;
-
- public $total;
-
- public $per_page;
-
- protected $appends;
-
- protected $appendage;
-
- protected $language;
-
- protected $dots = '<span class="dots">...</span>';
-
- protected function __construct($results, $page, $total, $per_page, $last)
- {
- $this->page = $page;
- $this->last = $last;
- $this->total = $total;
- $this->results = $results;
- $this->per_page = $per_page;
- }
-
- public static function make($results, $total, $per_page)
- {
- $page = static::page($total, $per_page);
- $last = ceil($total / $per_page);
- return new static($results, $page, $total, $per_page, $last);
- }
-
- public static function page($total, $per_page)
- {
- $page = Input::get('page', 1);
-
-
-
-
- if (is_numeric($page) and $page > $last = ceil($total / $per_page))
- {
- return ($last > 0) ? $last : 1;
- }
- return (static::valid($page)) ? $page : 1;
- }
-
- protected static function valid($page)
- {
- return $page >= 1 and filter_var($page, FILTER_VALIDATE_INT) !== false;
- }
-
- public function links($adjacent = 3)
- {
- if ($this->last <= 1) return '';
-
-
-
-
-
-
-
- if ($this->last < 7 + ($adjacent * 2))
- {
- $links = $this->range(1, $this->last);
- }
- else
- {
- $links = $this->slider($adjacent);
- }
- $content = $this->previous().' '.$links.' '.$this->next();
- return '<div class="pagination">'.$content.'</div>';
- }
-
- public function slider($adjacent = 3)
- {
- $window = $adjacent * 2;
-
-
-
-
-
-
-
-
-
- if ($this->page <= $window)
- {
- return $this->range(1, $window + 2).' '.$this->ending();
- }
-
- elseif ($this->page >= $this->last - $window)
- {
- return $this->beginning().' '.$this->range($this->last - $window - 2, $this->last);
- }
-
- $content = $this->range($this->page - $adjacent, $this->page + $adjacent);
- return $this->beginning().' '.$content.' '.$this->ending();
- }
-
- public function previous($text = null)
- {
- $disabled = function($page) { return $page <= 1; };
- return $this->element(__FUNCTION__, $this->page - 1, $text, $disabled);
- }
-
- public function next($text = null)
- {
- $disabled = function($page, $last) { return $page >= $last; };
- return $this->element(__FUNCTION__, $this->page + 1, $text, $disabled);
- }
-
- protected function element($element, $page, $text, $disabled)
- {
- $class = "{$element}_page";
- if (is_null($text))
- {
- $text = Lang::line("pagination.{$element}")->get($this->language);
- }
-
-
-
-
- if ($disabled($this->page, $this->last))
- {
- return HTML::span($text, array('class' => "{$class} disabled"));
- }
- else
- {
- return $this->link($page, $text, $class);
- }
- }
-
- protected function beginning()
- {
- return $this->range(1, 2).' '.$this->dots;
- }
-
- protected function ending()
- {
- return $this->dots.' '.$this->range($this->last - 1, $this->last);
- }
-
- protected function range($start, $end)
- {
- $pages = array();
-
-
-
-
- for ($page = $start; $page <= $end; $page++)
- {
- if ($this->page == $page)
- {
- $pages[] = HTML::span($page, array('class' => 'current'));
- }
- else
- {
- $pages[] = $this->link($page, $page, null);
- }
- }
- return implode(' ', $pages);
- }
-
- protected function link($page, $text, $class)
- {
- $query = '?page='.$page.$this->appendage($this->appends);
- return HTML::link(URI::current().$query, $text, compact('class'), Request::secure());
- }
-
- protected function appendage($appends)
- {
-
-
-
- if ( ! is_null($this->appendage)) return $this->appendage;
- if (count($appends) <= 0)
- {
- return $this->appendage = '';
- }
- return $this->appendage = '&'.http_build_query($appends);
- }
-
- public function appends($values)
- {
- $this->appends = $values;
- return $this;
- }
-
- public function speaks($language)
- {
- $this->language = $language;
- return $this;
- }
- }
|