|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
*## TbPager class file. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Christoffer Niska <[email protected]> |
|
6
|
|
|
* @copyright Copyright © Christoffer Niska 2011- |
|
7
|
|
|
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
*## Bootstrap pager. |
|
12
|
|
|
* |
|
13
|
|
|
* @see <http://twitter.github.com/bootstrap/components.html#pagination> |
|
14
|
|
|
* |
|
15
|
|
|
* @package booster.widgets.supplementary |
|
16
|
|
|
*/ |
|
17
|
|
|
class TbPager extends CLinkPager { |
|
18
|
|
|
|
|
19
|
|
|
// Pager alignments. |
|
20
|
|
|
const ALIGNMENT_CENTER = 'centered'; |
|
21
|
|
|
const ALIGNMENT_RIGHT = 'right'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string attributes for the pager container tag. |
|
25
|
|
|
*/ |
|
26
|
|
|
public $containerTag = 'div'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array HTML attributes for the pager container tag. |
|
30
|
|
|
*/ |
|
31
|
|
|
public $containerHtmlOptions = array(); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string the pager alignment. |
|
35
|
|
|
* Valid values are 'centered' and 'right'. |
|
36
|
|
|
*/ |
|
37
|
|
|
public $alignment = self::ALIGNMENT_RIGHT; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string the text shown before page buttons. |
|
41
|
|
|
* Defaults to an empty string, meaning that no header will be displayed. |
|
42
|
|
|
*/ |
|
43
|
|
|
public $header = ''; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string the URL of the CSS file used by this pager. |
|
47
|
|
|
* Defaults to false, meaning that no CSS will be included. |
|
48
|
|
|
*/ |
|
49
|
|
|
public $cssFile = false; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var boolean whether to display the first and last items. |
|
53
|
|
|
*/ |
|
54
|
|
|
public $displayFirstAndLast = false; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
*### .init() |
|
58
|
|
|
* |
|
59
|
|
|
* Initializes the pager by setting some default property values. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function init() { |
|
62
|
|
|
|
|
63
|
|
|
if ($this->nextPageLabel === null) { |
|
64
|
|
|
$this->nextPageLabel = '»'; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($this->prevPageLabel === null) { |
|
68
|
|
|
$this->prevPageLabel = '«'; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$classes = array('pagination'); |
|
72
|
|
|
|
|
73
|
|
|
/* TODO: move these to styles files! */ |
|
74
|
|
|
$style = ''; |
|
75
|
|
|
$containerStyle = ''; |
|
76
|
|
|
|
|
77
|
|
|
$validAlignments = array(self::ALIGNMENT_CENTER, self::ALIGNMENT_RIGHT); |
|
78
|
|
|
|
|
79
|
|
|
if (in_array($this->alignment, $validAlignments)) { |
|
80
|
|
|
if($this->alignment == self::ALIGNMENT_RIGHT) |
|
81
|
|
|
$classes[] = 'pull-right'; |
|
82
|
|
|
|
|
83
|
|
|
if($this->alignment == self::ALIGNMENT_CENTER) { |
|
84
|
|
|
// $style = 'margin-left: auto; margin-right: auto;'; // not needed! |
|
85
|
|
|
$containerStyle = 'text-align: center;'; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (!empty($classes)) { |
|
90
|
|
|
$classes = implode(' ', $classes); |
|
91
|
|
|
if (isset($this->htmlOptions['class'])) { |
|
92
|
|
|
$this->htmlOptions['class'] = ' ' . $classes; |
|
93
|
|
|
} else { |
|
94
|
|
|
$this->htmlOptions['class'] = $classes; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if(!empty($style)) { |
|
99
|
|
|
if(isset($this->htmlOptions['style']) && !empty($this->htmlOptions['style'])) |
|
100
|
|
|
$this->htmlOptions['style'] .= ' '.$style; |
|
101
|
|
|
else |
|
102
|
|
|
$this->htmlOptions['style'] = $style; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if(!empty($containerStyle)) { |
|
106
|
|
|
if(isset($this->containerHtmlOptions['style']) && !empty($this->containerHtmlOptions['style'])) |
|
107
|
|
|
$this->containerHtmlOptions['style'] .= ' '.$containerStyle; |
|
108
|
|
|
else |
|
109
|
|
|
$this->containerHtmlOptions['style'] = $containerStyle; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
parent::init(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Executes the widget. |
|
117
|
|
|
* This overrides the parent implementation by displaying the generated page buttons. |
|
118
|
|
|
*/ |
|
119
|
|
|
public function run() { |
|
120
|
|
|
|
|
121
|
|
|
$this->registerClientScript(); |
|
122
|
|
|
$buttons=$this->createPageButtons(); |
|
123
|
|
|
if(empty($buttons)) |
|
124
|
|
|
return; |
|
125
|
|
|
echo CHtml::openTag($this->containerTag, $this->containerHtmlOptions); |
|
126
|
|
|
echo $this->header; |
|
127
|
|
|
echo CHtml::tag('ul',$this->htmlOptions,implode("\n",$buttons)); |
|
128
|
|
|
echo '<div style="clear: both;"></div>'; |
|
129
|
|
|
echo $this->footer; |
|
130
|
|
|
echo CHtml::closeTag($this->containerTag); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
*### .createPageButtons() |
|
135
|
|
|
* |
|
136
|
|
|
* Creates the page buttons. |
|
137
|
|
|
* @return array a list of page buttons (in HTML code). |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function createPageButtons() { |
|
140
|
|
|
|
|
141
|
|
|
if (($pageCount = $this->getPageCount()) <= 1) { |
|
142
|
|
|
return array(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
list ($beginPage, $endPage) = $this->getPageRange(); |
|
146
|
|
|
|
|
147
|
|
|
$currentPage = $this->getCurrentPage(false); // currentPage is calculated in getPageRange() |
|
148
|
|
|
|
|
149
|
|
|
$buttons = array(); |
|
150
|
|
|
|
|
151
|
|
|
// first page |
|
152
|
|
|
if ($this->displayFirstAndLast) { |
|
153
|
|
|
$buttons[] = $this->createPageButton($this->firstPageLabel, 0, 'first', $currentPage <= 0, false); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
// prev page |
|
157
|
|
|
if (($page = $currentPage - 1) < 0) { |
|
158
|
|
|
$page = 0; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$buttons[] = $this->createPageButton($this->prevPageLabel, $page, 'previous', $currentPage <= 0, false); |
|
162
|
|
|
|
|
163
|
|
|
// internal pages |
|
164
|
|
|
for ($i = $beginPage; $i <= $endPage; ++$i) { |
|
165
|
|
|
$buttons[] = $this->createPageButton($i + 1, $i, '', false, $i == $currentPage); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
// next page |
|
169
|
|
|
if (($page = $currentPage + 1) >= $pageCount - 1) { |
|
170
|
|
|
$page = $pageCount - 1; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$buttons[] = $this->createPageButton( |
|
174
|
|
|
$this->nextPageLabel, |
|
175
|
|
|
$page, |
|
176
|
|
|
'next', |
|
177
|
|
|
$currentPage >= ($pageCount - 1), |
|
178
|
|
|
false |
|
179
|
|
|
); |
|
180
|
|
|
|
|
181
|
|
|
// last page |
|
182
|
|
|
if ($this->displayFirstAndLast) { |
|
183
|
|
|
$buttons[] = $this->createPageButton( |
|
184
|
|
|
$this->lastPageLabel, |
|
185
|
|
|
$pageCount - 1, |
|
186
|
|
|
'last', |
|
187
|
|
|
$currentPage >= ($pageCount - 1), |
|
188
|
|
|
false |
|
189
|
|
|
); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $buttons; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
*### .createPageButton() |
|
197
|
|
|
* |
|
198
|
|
|
* Creates a page button. |
|
199
|
|
|
* You may override this method to customize the page buttons. |
|
200
|
|
|
* |
|
201
|
|
|
* @param string $label the text label for the button |
|
202
|
|
|
* @param integer $page the page number |
|
203
|
|
|
* @param string $class the CSS class for the page button. This could be 'page', 'first', 'last', 'next' or 'previous'. |
|
204
|
|
|
* @param boolean $hidden whether this page button is visible |
|
205
|
|
|
* @param boolean $selected whether this page button is selected |
|
206
|
|
|
* |
|
207
|
|
|
* @return string the generated button |
|
208
|
|
|
*/ |
|
209
|
|
|
protected function createPageButton($label, $page, $class, $hidden, $selected) |
|
210
|
|
|
{ |
|
211
|
|
|
if ($hidden || $selected) { |
|
212
|
|
|
$class .= ' ' . ($hidden ? 'disabled' : 'active'); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
return CHtml::tag('li', array('class' => $class), CHtml::link($label, $this->createPageUrl($page))); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|