1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16: 17:
18: class extends CBasePager
19: {
20: const CSS_FIRST_PAGE='first';
21: const CSS_LAST_PAGE='last';
22: const CSS_PREVIOUS_PAGE='previous';
23: const CSS_NEXT_PAGE='next';
24: const CSS_INTERNAL_PAGE='page';
25: const CSS_HIDDEN_PAGE='hidden';
26: const CSS_SELECTED_PAGE='selected';
27:
28: 29: 30: 31:
32: public $firstPageCssClass=self::CSS_FIRST_PAGE;
33: 34: 35: 36:
37: public $lastPageCssClass=self::CSS_LAST_PAGE;
38: 39: 40: 41:
42: public $previousPageCssClass=self::CSS_PREVIOUS_PAGE;
43: 44: 45: 46:
47: public $nextPageCssClass=self::CSS_NEXT_PAGE;
48: 49: 50: 51:
52: public $internalPageCssClass=self::CSS_INTERNAL_PAGE;
53: 54: 55: 56:
57: public $hiddenPageCssClass=self::CSS_HIDDEN_PAGE;
58: 59: 60: 61:
62: public $selectedPageCssClass=self::CSS_SELECTED_PAGE;
63: 64: 65:
66: public $maxButtonCount=10;
67: 68: 69: 70:
71: public $nextPageLabel;
72: 73: 74: 75:
76: public $prevPageLabel;
77: 78: 79: 80:
81: public $firstPageLabel;
82: 83: 84: 85:
86: public $lastPageLabel;
87: 88: 89:
90: public ;
91: 92: 93:
94: public ='';
95: 96: 97: 98: 99: 100:
101: public $cssFile;
102: 103: 104:
105: public $htmlOptions=array();
106:
107: 108: 109:
110: public function init()
111: {
112: if($this->nextPageLabel===null)
113: $this->nextPageLabel=Yii::t('yii','Next >');
114: if($this->prevPageLabel===null)
115: $this->prevPageLabel=Yii::t('yii','< Previous');
116: if($this->firstPageLabel===null)
117: $this->firstPageLabel=Yii::t('yii','<< First');
118: if($this->lastPageLabel===null)
119: $this->lastPageLabel=Yii::t('yii','Last >>');
120: if($this->header===null)
121: $this->header=Yii::t('yii','Go to page: ');
122:
123: if(!isset($this->htmlOptions['id']))
124: $this->htmlOptions['id']=$this->getId();
125: if(!isset($this->htmlOptions['class']))
126: $this->htmlOptions['class']='yiiPager';
127: }
128:
129: 130: 131: 132:
133: public function run()
134: {
135: $this->registerClientScript();
136: $buttons=$this->createPageButtons();
137: if(empty($buttons))
138: return;
139: echo $this->header;
140: echo CHtml::tag('ul',$this->htmlOptions,implode("\n",$buttons));
141: echo $this->footer;
142: }
143:
144: 145: 146: 147:
148: protected function createPageButtons()
149: {
150: if(($pageCount=$this->getPageCount())<=1)
151: return array();
152:
153: list($beginPage,$endPage)=$this->getPageRange();
154: $currentPage=$this->getCurrentPage(false);
155: $buttons=array();
156:
157:
158: if ($this->firstPageLabel !== false) {
159: $buttons[]=$this->createPageButton($this->firstPageLabel,0,$this->firstPageCssClass,$currentPage<=0,false);
160: }
161:
162: if ($this->prevPageLabel !== false) {
163: if(($page=$currentPage-1)<0)
164: $page=0;
165: $buttons[]=$this->createPageButton($this->prevPageLabel,$page,$this->previousPageCssClass,$currentPage<=0,false);
166: }
167:
168:
169: for($i=$beginPage;$i<=$endPage;++$i)
170: $buttons[]=$this->createPageButton($i+1,$i,$this->internalPageCssClass,false,$i==$currentPage);
171:
172:
173: if ($this->nextPageLabel !== false) {
174: if(($page=$currentPage+1)>=$pageCount-1)
175: $page=$pageCount-1;
176: $buttons[]=$this->createPageButton($this->nextPageLabel,$page,$this->nextPageCssClass,$currentPage>=$pageCount-1,false);
177: }
178:
179: if ($this->lastPageLabel !== false) {
180: $buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,$this->lastPageCssClass,$currentPage>=$pageCount-1,false);
181: }
182:
183: return $buttons;
184: }
185:
186: 187: 188: 189: 190: 191: 192: 193: 194: 195:
196: protected function createPageButton($label,$page,$class,$hidden,$selected)
197: {
198: if($hidden || $selected)
199: $class.=' '.($hidden ? $this->hiddenPageCssClass : $this->selectedPageCssClass);
200: return '<li class="'.$class.'">'.CHtml::link($label,$this->createPageUrl($page)).'</li>';
201: }
202:
203: 204: 205:
206: protected function ()
207: {
208: $currentPage=$this->getCurrentPage();
209: $pageCount=$this->getPageCount();
210:
211: $beginPage=max(0, $currentPage-(int)($this->maxButtonCount/2));
212: if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount)
213: {
214: $endPage=$pageCount-1;
215: $beginPage=max(0,$endPage-$this->maxButtonCount+1);
216: }
217: return array($beginPage,$endPage);
218: }
219:
220: 221: 222:
223: public function registerClientScript()
224: {
225: if($this->cssFile!==false)
226: self::registerCssFile($this->cssFile);
227: }
228:
229: 230: 231: 232:
233: public static function registerCssFile($url=null)
234: {
235: if($url===null)
236: $url=CHtml::asset(Yii::getPathOfAlias('system.web.widgets.pagers.pager').'.css');
237: Yii::app()->getClientScript()->registerCssFile($url);
238: }
239: }
240: