forked from patricktalmadge/bootstrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaginator.php
147 lines (123 loc) · 3.98 KB
/
paginator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php namespace Bootstrapper;
use \HTML;
class Paginator extends \Laravel\Paginator {
const ALIGN_LEFT = "";
const ALIGN_CENTER = "pagination-centered";
const ALIGN_RIGHT = "pagination-right";
protected $pager_aligned = false;
/**
* The "dots" element used in the pagination slider.
*
* @var string
*/
protected $dots = '<li class="disabled"><a href="#">...</a></li>';
public function pager($align = false){
$this->pager_aligned = $align;
return '<ul class="pager">'.$this->previous().$this->next().'</ul>';
}
/**
* Create the HTML pagination links.
*
* Typically, an intelligent, "sliding" window of links will be rendered based
* on the total number of pages, the current page, and the number of adjacent
* pages that should rendered. This creates a beautiful paginator similar to
* that of Google's.
*
* Example: 1 2 ... 23 24 25 [26] 27 28 29 ... 51 52
*
* If you wish to render only certain elements of the pagination control,
* explore some of the other public methods available on the instance.
*
* <code>
* // Render the pagination links
* echo $paginator->links();
*
* // Render the pagination links using a given window size
* echo $paginator->links(5);
* </code>
*
* @param int $adjacent
* @return string
*/
public function links($adjacent = 3, $alignment = self::ALIGN_LEFT)
{
if ($this->last <= 1) return '';
// The hard-coded seven is to account for all of the constant elements in a
// sliding range, such as the current page, the two ellipses, and the two
// beginning and ending pages.
//
// If there are not enough pages to make the creation of a slider possible
// based on the adjacent pages, we will simply display all of the pages.
// Otherwise, we will create a "truncating" sliding window.
if ($this->last < 7 + ($adjacent * 2))
{
$links = $this->range(1, $this->last);
}
else
{
$links = $this->slider($adjacent);
}
$content = $this->previous().' '.$links.' '.$this->next();
$attributes = array("class" => "pagination ".$alignment);
return '<div'.HTML::attributes($attributes).'><ul>'.$content.'</ul></div>';
}
/**
* Create a chronological pagination element, such as a "previous" or "next" link.
*
* @param string $element
* @param int $page
* @param string $text
* @param Closure $disabled
* @return string
*/
protected function element($element, $page, $text, $disabled)
{
$class = $this->pager_aligned ? "{$element}" : "";
if (is_null($text))
{
$text = \Lang::line("pagination.{$element}")->get($this->language);
}
// Each consumer of this method provides a "disabled" Closure which can
// be used to determine if the element should be a span element or an
// actual link. For example, if the current page is the first page,
// the "first" element should be a span instead of a link.
if ($disabled($this->page, $this->last))
{
$class .= " disabled";
return '<li'.HTML::attributes(compact("class")).'><a href="#">'.HTML::entities($text).'</a></li>';
}
else
{
return '<li'.HTML::attributes(compact("class")).'>'.$this->link($page, $text, null).'</li>';
}
}
/**
* Build a range of numeric pagination links.
*
* For the current page, an HTML span element will be generated instead of a link.
*
* @param int $start
* @param int $end
* @return string
*/
protected function range($start, $end)
{
$pages = array();
// To generate the range of page links, we will iterate through each page
// and, if the current page matches the page, we will generate a span,
// otherwise we will generate a link for the page. The span elements
// will be assigned the "current" CSS class for convenient styling.
for ($page = $start; $page <= $end; $page++)
{
if ($this->page == $page)
{
$pages[] = '<li class="active"><a href="#">'.HTML::entities($page).'</a></li>';
}
else
{
$pages[] = '<li>'.$this->link($page, $page, null).'</li>';
}
}
return implode(' ', $pages);
}
}