1: <?php
2: 3: 4: 5: 6: 7: 8:
9:
10: Yii::import('bootstrap.helpers.TbHtml');
11: Yii::import('bootstrap.behaviors.TbWidget');
12:
13: 14: 15: 16:
17: class extends CBasePager
18: {
19: 20: 21:
22: public $size;
23: 24: 25:
26: public $maxButtonCount = 5;
27: 28: 29:
30: public $nextPageLabel = '›';
31: 32: 33:
34: public $prevPageLabel = '‹';
35: 36: 37:
38: public $firstPageLabel = '«';
39: 40: 41:
42: public $lastPageLabel = '»';
43:
44: 45: 46: 47:
48: public $hideFirstAndLast = false;
49: 50: 51:
52: public $htmlOptions = array();
53:
54: 55: 56:
57: public function init()
58: {
59: $this->attachBehavior('TbWidget', new TbWidget);
60: $this->copyId();
61: if (isset($this->size)) {
62: TbArray::defaultValue('size', $this->size, $this->htmlOptions);
63: }
64: }
65:
66: 67: 68:
69: public function run()
70: {
71: $links = $this->createPageLinks();
72: if (!empty($links)) {
73: echo TbHtml::pagination($links, $this->htmlOptions);
74: }
75: }
76:
77: 78: 79: 80:
81: protected function createPageLinks()
82: {
83: if (($pageCount = $this->getPageCount()) <= 1) {
84: return array();
85: }
86:
87: list($beginPage, $endPage) = $this->getPageRange();
88:
89: $currentPage = $this->getCurrentPage(false);
90: $links = array();
91:
92:
93: if (!$this->hideFirstAndLast) {
94: $links[] = $this->createPageLink($this->firstPageLabel, 0, $currentPage <= 0, false);
95: }
96:
97:
98: if (($page = $currentPage - 1) < 0) {
99: $page = 0;
100: }
101:
102: $links[] = $this->createPageLink($this->prevPageLabel, $page, $currentPage <= 0, false);
103:
104:
105: for ($i = $beginPage; $i <= $endPage; ++$i) {
106: $links[] = $this->createPageLink($i + 1, $i, false, $i == $currentPage);
107: }
108:
109:
110: if (($page = $currentPage + 1) >= $pageCount - 1) {
111: $page = $pageCount - 1;
112: }
113:
114: $links[] = $this->createPageLink($this->nextPageLabel, $page, $currentPage >= $pageCount - 1, false);
115:
116:
117: if (!$this->hideFirstAndLast) {
118: $links[] = $this->createPageLink(
119: $this->lastPageLabel,
120: $pageCount - 1,
121: $currentPage >= $pageCount - 1,
122: false
123: );
124: }
125:
126: return $links;
127: }
128:
129: 130: 131: 132: 133: 134: 135: 136:
137: protected function createPageLink($label, $page, $disabled, $active)
138: {
139: return array(
140: 'label' => $label,
141: 'url' => $this->createPageUrl($page),
142: 'disabled' => $disabled,
143: 'active' => $active,
144: );
145: }
146:
147: 148: 149:
150: protected function ()
151: {
152: $currentPage = $this->getCurrentPage();
153: $pageCount = $this->getPageCount();
154: $beginPage = max(0, $currentPage - (int)($this->maxButtonCount / 2));
155: if (($endPage = $beginPage + $this->maxButtonCount - 1) >= $pageCount) {
156: $endPage = $pageCount - 1;
157: $beginPage = max(0, $endPage - $this->maxButtonCount + 1);
158: }
159: return array($beginPage, $endPage);
160: }
161: }
162: