123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- /**
- * CLinkPager class file.
- *
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @link http://www.yiiframework.com/
- * @copyright 2008-2013 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>
- * @package system.web.widgets.pagers
- * @since 1.0
- */
- class CLinkPager 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='hidden';
- const CSS_SELECTED_PAGE='selected';
- /**
- * @var string the CSS class for the first page button. Defaults to 'first'.
- * @since 1.1.11
- */
- public $firstPageCssClass=self::CSS_FIRST_PAGE;
- /**
- * @var string the CSS class for the last page button. Defaults to 'last'.
- * @since 1.1.11
- */
- public $lastPageCssClass=self::CSS_LAST_PAGE;
- /**
- * @var string the CSS class for the previous page button. Defaults to 'previous'.
- * @since 1.1.11
- */
- public $previousPageCssClass=self::CSS_PREVIOUS_PAGE;
- /**
- * @var string the CSS class for the next page button. Defaults to 'next'.
- * @since 1.1.11
- */
- public $nextPageCssClass=self::CSS_NEXT_PAGE;
- /**
- * @var string the CSS class for the internal page buttons. Defaults to 'page'.
- * @since 1.1.11
- */
- public $internalPageCssClass=self::CSS_INTERNAL_PAGE;
- /**
- * @var string the CSS class for the hidden page buttons. Defaults to 'hidden'.
- * @since 1.1.11
- */
- public $hiddenPageCssClass=self::CSS_HIDDEN_PAGE;
- /**
- * @var string the CSS class for the selected page buttons. Defaults to 'selected'.
- * @since 1.1.11
- */
- public $selectedPageCssClass=self::CSS_SELECTED_PAGE;
- /**
- * @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 >'.
- * Setting this to false will disable this button.
- */
- public $nextPageLabel;
- /**
- * @var string the text label for the previous page button. Defaults to '< Previous'.
- * Setting this to false will disable this button.
- */
- public $prevPageLabel;
- /**
- * @var string the text label for the first page button. Defaults to '<< First'.
- * Setting this to false will disable this button.
- */
- public $firstPageLabel;
- /**
- * @var string the text label for the last page button. Defaults to 'Last >>'.
- * Setting this to false will disable this button.
- */
- 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 CHtml::tag('ul',$this->htmlOptions,implode("\n",$buttons));
- 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
- if ($this->firstPageLabel !== false) {
- $buttons[]=$this->createPageButton($this->firstPageLabel,0,$this->firstPageCssClass,$currentPage<=0,false);
- }
- // prev page
- if ($this->prevPageLabel !== false) {
- if(($page=$currentPage-1)<0)
- $page=0;
- $buttons[]=$this->createPageButton($this->prevPageLabel,$page,$this->previousPageCssClass,$currentPage<=0,false);
- }
- // internal pages
- for($i=$beginPage;$i<=$endPage;++$i)
- $buttons[]=$this->createPageButton($i+1,$i,$this->internalPageCssClass,false,$i==$currentPage);
-
- // next page
- if ($this->nextPageLabel !== false) {
- if(($page=$currentPage+1)>=$pageCount-1)
- $page=$pageCount-1;
- $buttons[]=$this->createPageButton($this->nextPageLabel,$page,$this->nextPageCssClass,$currentPage>=$pageCount-1,false);
- }
- // last page
- if ($this->lastPageLabel !== false) {
- $buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,$this->lastPageCssClass,$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.
- * @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 ? $this->hiddenPageCssClass : $this->selectedPageCssClass);
- 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);
- }
- }
|