CLinkPager.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * CLinkPager class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CLinkPager displays a list of hyperlinks that lead to different pages of target.
  12. *
  13. * @author Qiang Xue <qiang.xue@gmail.com>
  14. * @package system.web.widgets.pagers
  15. * @since 1.0
  16. */
  17. class CLinkPager extends CBasePager
  18. {
  19. const CSS_FIRST_PAGE='first';
  20. const CSS_LAST_PAGE='last';
  21. const CSS_PREVIOUS_PAGE='previous';
  22. const CSS_NEXT_PAGE='next';
  23. const CSS_INTERNAL_PAGE='page';
  24. const CSS_HIDDEN_PAGE='hidden';
  25. const CSS_SELECTED_PAGE='selected';
  26. /**
  27. * @var string the CSS class for the first page button. Defaults to 'first'.
  28. * @since 1.1.11
  29. */
  30. public $firstPageCssClass=self::CSS_FIRST_PAGE;
  31. /**
  32. * @var string the CSS class for the last page button. Defaults to 'last'.
  33. * @since 1.1.11
  34. */
  35. public $lastPageCssClass=self::CSS_LAST_PAGE;
  36. /**
  37. * @var string the CSS class for the previous page button. Defaults to 'previous'.
  38. * @since 1.1.11
  39. */
  40. public $previousPageCssClass=self::CSS_PREVIOUS_PAGE;
  41. /**
  42. * @var string the CSS class for the next page button. Defaults to 'next'.
  43. * @since 1.1.11
  44. */
  45. public $nextPageCssClass=self::CSS_NEXT_PAGE;
  46. /**
  47. * @var string the CSS class for the internal page buttons. Defaults to 'page'.
  48. * @since 1.1.11
  49. */
  50. public $internalPageCssClass=self::CSS_INTERNAL_PAGE;
  51. /**
  52. * @var string the CSS class for the hidden page buttons. Defaults to 'hidden'.
  53. * @since 1.1.11
  54. */
  55. public $hiddenPageCssClass=self::CSS_HIDDEN_PAGE;
  56. /**
  57. * @var string the CSS class for the selected page buttons. Defaults to 'selected'.
  58. * @since 1.1.11
  59. */
  60. public $selectedPageCssClass=self::CSS_SELECTED_PAGE;
  61. /**
  62. * @var integer maximum number of page buttons that can be displayed. Defaults to 10.
  63. */
  64. public $maxButtonCount=10;
  65. /**
  66. * @var string the text label for the next page button. Defaults to 'Next &gt;'.
  67. * Setting this to false will disable this button.
  68. */
  69. public $nextPageLabel;
  70. /**
  71. * @var string the text label for the previous page button. Defaults to '&lt; Previous'.
  72. * Setting this to false will disable this button.
  73. */
  74. public $prevPageLabel;
  75. /**
  76. * @var string the text label for the first page button. Defaults to '&lt;&lt; First'.
  77. * Setting this to false will disable this button.
  78. */
  79. public $firstPageLabel;
  80. /**
  81. * @var string the text label for the last page button. Defaults to 'Last &gt;&gt;'.
  82. * Setting this to false will disable this button.
  83. */
  84. public $lastPageLabel;
  85. /**
  86. * @var string the text shown before page buttons. Defaults to 'Go to page: '.
  87. */
  88. public $header;
  89. /**
  90. * @var string the text shown after page buttons.
  91. */
  92. public $footer='';
  93. /**
  94. * @var mixed the CSS file used for the widget. Defaults to null, meaning
  95. * using the default CSS file included together with the widget.
  96. * If false, no CSS file will be used. Otherwise, the specified CSS file
  97. * will be included when using this widget.
  98. */
  99. public $cssFile;
  100. /**
  101. * @var array HTML attributes for the pager container tag.
  102. */
  103. public $htmlOptions=array();
  104. /**
  105. * Initializes the pager by setting some default property values.
  106. */
  107. public function init()
  108. {
  109. if($this->nextPageLabel===null)
  110. $this->nextPageLabel=Yii::t('yii','Next &gt;');
  111. if($this->prevPageLabel===null)
  112. $this->prevPageLabel=Yii::t('yii','&lt; Previous');
  113. if($this->firstPageLabel===null)
  114. $this->firstPageLabel=Yii::t('yii','&lt;&lt; First');
  115. if($this->lastPageLabel===null)
  116. $this->lastPageLabel=Yii::t('yii','Last &gt;&gt;');
  117. if($this->header===null)
  118. $this->header=Yii::t('yii','Go to page: ');
  119. if(!isset($this->htmlOptions['id']))
  120. $this->htmlOptions['id']=$this->getId();
  121. if(!isset($this->htmlOptions['class']))
  122. $this->htmlOptions['class']='yiiPager';
  123. }
  124. /**
  125. * Executes the widget.
  126. * This overrides the parent implementation by displaying the generated page buttons.
  127. */
  128. public function run()
  129. {
  130. $this->registerClientScript();
  131. $buttons=$this->createPageButtons();
  132. if(empty($buttons))
  133. return;
  134. echo $this->header;
  135. echo CHtml::tag('ul',$this->htmlOptions,implode("\n",$buttons));
  136. echo $this->footer;
  137. }
  138. /**
  139. * Creates the page buttons.
  140. * @return array a list of page buttons (in HTML code).
  141. */
  142. protected function createPageButtons()
  143. {
  144. if(($pageCount=$this->getPageCount())<=1)
  145. return array();
  146. list($beginPage,$endPage)=$this->getPageRange();
  147. $currentPage=$this->getCurrentPage(false); // currentPage is calculated in getPageRange()
  148. $buttons=array();
  149. // first page
  150. if ($this->firstPageLabel !== false) {
  151. $buttons[]=$this->createPageButton($this->firstPageLabel,0,$this->firstPageCssClass,$currentPage<=0,false);
  152. }
  153. // prev page
  154. if ($this->prevPageLabel !== false) {
  155. if(($page=$currentPage-1)<0)
  156. $page=0;
  157. $buttons[]=$this->createPageButton($this->prevPageLabel,$page,$this->previousPageCssClass,$currentPage<=0,false);
  158. }
  159. // internal pages
  160. for($i=$beginPage;$i<=$endPage;++$i)
  161. $buttons[]=$this->createPageButton($i+1,$i,$this->internalPageCssClass,false,$i==$currentPage);
  162. // next page
  163. if ($this->nextPageLabel !== false) {
  164. if(($page=$currentPage+1)>=$pageCount-1)
  165. $page=$pageCount-1;
  166. $buttons[]=$this->createPageButton($this->nextPageLabel,$page,$this->nextPageCssClass,$currentPage>=$pageCount-1,false);
  167. }
  168. // last page
  169. if ($this->lastPageLabel !== false) {
  170. $buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,$this->lastPageCssClass,$currentPage>=$pageCount-1,false);
  171. }
  172. return $buttons;
  173. }
  174. /**
  175. * Creates a page button.
  176. * You may override this method to customize the page buttons.
  177. * @param string $label the text label for the button
  178. * @param integer $page the page number
  179. * @param string $class the CSS class for the page button.
  180. * @param boolean $hidden whether this page button is visible
  181. * @param boolean $selected whether this page button is selected
  182. * @return string the generated button
  183. */
  184. protected function createPageButton($label,$page,$class,$hidden,$selected)
  185. {
  186. if($hidden || $selected)
  187. $class.=' '.($hidden ? $this->hiddenPageCssClass : $this->selectedPageCssClass);
  188. return '<li class="'.$class.'">'.CHtml::link($label,$this->createPageUrl($page)).'</li>';
  189. }
  190. /**
  191. * @return array the begin and end pages that need to be displayed.
  192. */
  193. protected function getPageRange()
  194. {
  195. $currentPage=$this->getCurrentPage();
  196. $pageCount=$this->getPageCount();
  197. $beginPage=max(0, $currentPage-(int)($this->maxButtonCount/2));
  198. if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount)
  199. {
  200. $endPage=$pageCount-1;
  201. $beginPage=max(0,$endPage-$this->maxButtonCount+1);
  202. }
  203. return array($beginPage,$endPage);
  204. }
  205. /**
  206. * Registers the needed client scripts (mainly CSS file).
  207. */
  208. public function registerClientScript()
  209. {
  210. if($this->cssFile!==false)
  211. self::registerCssFile($this->cssFile);
  212. }
  213. /**
  214. * Registers the needed CSS file.
  215. * @param string $url the CSS URL. If null, a default CSS URL will be used.
  216. */
  217. public static function registerCssFile($url=null)
  218. {
  219. if($url===null)
  220. $url=CHtml::asset(Yii::getPathOfAlias('system.web.widgets.pagers.pager').'.css');
  221. Yii::app()->getClientScript()->registerCssFile($url);
  222. }
  223. }