BLinkPager.php
6.82 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* BlinkPager is modified CLinkPager to use with twitter bootstrap
*/
/**
* CLinkPager class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CLinkPager displays a list of hyperlinks that lead to different pages of target.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CLinkPager.php 3515 2011-12-28 12:29:24Z mdomba $
* @package system.web.widgets.pagers
* @since 1.0
*/
class BLinkPager extends CBasePager
{
const CSS_FIRST_PAGE = ''; //'first';
const CSS_LAST_PAGE = ''; //'last';
const CSS_PREVIOUS_PAGE = ''; //previous
const CSS_NEXT_PAGE = ''; //next
const CSS_INTERNAL_PAGE = ''; //'page';
const CSS_HIDDEN_PAGE = 'disabled';
const CSS_SELECTED_PAGE = 'active';
/**
* @var integer maximum number of page buttons that can be displayed. Defaults to 10.
*/
public $maxButtonCount = 10;
/**
* @var string the text label for the next page button. Defaults to 'Next >'.
*/
public $nextPageLabel;
/**
* @var string the text label for the previous page button. Defaults to '< Previous'.
*/
public $prevPageLabel;
/**
* @var string the text label for the first page button. Defaults to '<< First'.
*/
public $firstPageLabel;
/**
* @var string the text label for the last page button. Defaults to 'Last >>'.
*/
public $lastPageLabel;
/**
* @var string the text shown before page buttons. Defaults to 'Go to page: '.
*/
public $header;
/**
* @var string the text shown after page buttons.
*/
public $footer = '';
/**
* @var mixed the CSS file used for the widget. Defaults to null, meaning
* using the default CSS file included together with the widget.
* If false, no CSS file will be used. Otherwise, the specified CSS file
* will be included when using this widget.
*/
public $cssFile;
/**
* @var array HTML attributes for the pager container tag.
*/
public $htmlOptions = array();
/**
* Initializes the pager by setting some default property values.
*/
public function init()
{
if ($this->nextPageLabel === null)
$this->nextPageLabel = Yii::t('yii', 'Next >');
if ($this->prevPageLabel === null)
$this->prevPageLabel = Yii::t('yii', '< Previous');
if ($this->firstPageLabel === null)
$this->firstPageLabel = Yii::t('yii', '<< First');
if ($this->lastPageLabel === null)
$this->lastPageLabel = Yii::t('yii', 'Last >>');
if ($this->header === null)
$this->header = Yii::t('yii', 'Go to page: ');
if (!isset($this->htmlOptions['id']))
$this->htmlOptions['id'] = $this->getId();
if (!isset($this->htmlOptions['class']))
$this->htmlOptions['class'] = 'yiiPager';
}
/**
* Executes the widget.
* This overrides the parent implementation by displaying the generated page buttons.
*/
public function run()
{
$this->registerClientScript();
$buttons = $this->createPageButtons();
if (empty($buttons))
return;
echo $this->header;
echo '<div class="pagination">', CHtml::tag('ul', $this->htmlOptions, implode("\n", $buttons)), '</div>';
echo $this->footer;
}
/**
* Creates the page buttons.
* @return array a list of page buttons (in HTML code).
*/
protected function createPageButtons()
{
if (($pageCount = $this->getPageCount()) <= 1)
return array();
list($beginPage, $endPage) = $this->getPageRange();
$currentPage = $this->getCurrentPage(false); // currentPage is calculated in getPageRange()
$buttons = array();
// first page
$buttons[] = $this->createPageButton($this->firstPageLabel, 0, self::CSS_FIRST_PAGE, $currentPage <= 0, false);
// prev page
if (($page = $currentPage - 1) < 0)
$page = 0;
$buttons[] = $this->createPageButton($this->prevPageLabel, $page, self::CSS_PREVIOUS_PAGE, $currentPage <= 0, false);
// internal pages
for ($i = $beginPage; $i <= $endPage; ++$i)
$buttons[] = $this->createPageButton($i + 1, $i, self::CSS_INTERNAL_PAGE, false, $i == $currentPage);
// next page
if (($page = $currentPage + 1) >= $pageCount - 1)
$page = $pageCount - 1;
$buttons[] = $this->createPageButton($this->nextPageLabel, $page, self::CSS_NEXT_PAGE, $currentPage >= $pageCount - 1, false);
// last page
$buttons[] = $this->createPageButton($this->lastPageLabel, $pageCount - 1, self::CSS_LAST_PAGE, $currentPage >= $pageCount - 1, false);
return $buttons;
}
/**
* Creates a page button.
* You may override this method to customize the page buttons.
* @param string $label the text label for the button
* @param integer $page the page number
* @param string $class the CSS class for the page button. This could be 'page', 'first', 'last', 'next' or 'previous'.
* @param boolean $hidden whether this page button is visible
* @param boolean $selected whether this page button is selected
* @return string the generated button
*/
protected function createPageButton($label, $page, $class, $hidden, $selected)
{
if ($hidden || $selected)
$class .= ' ' . ($hidden ? self::CSS_HIDDEN_PAGE : self::CSS_SELECTED_PAGE);
return '<li class="' . $class . '">' . CHtml::link($label, $this->createPageUrl($page)) . '</li>';
}
/**
* @return array the begin and end pages that need to be displayed.
*/
protected function getPageRange()
{
$currentPage = $this->getCurrentPage();
$pageCount = $this->getPageCount();
$beginPage = max(0, $currentPage - (int)($this->maxButtonCount / 2));
if (($endPage = $beginPage + $this->maxButtonCount - 1) >= $pageCount) {
$endPage = $pageCount - 1;
$beginPage = max(0, $endPage - $this->maxButtonCount + 1);
}
return array($beginPage, $endPage);
}
/**
* Registers the needed client scripts (mainly CSS file).
*/
public function registerClientScript()
{
if ($this->cssFile !== false)
self::registerCssFile($this->cssFile);
}
/**
* Registers the needed CSS file.
* @param string $url the CSS URL. If null, a default CSS URL will be used.
*/
public static function registerCssFile($url = null)
{
// if($url===null)
// $url=CHtml::asset(Yii::getPathOfAlias('system.web.widgets.pagers.pager').'.css');
// Yii::app()->getClientScript()->registerCssFile($url);
}
}