summaryrefslogtreecommitdiff
path: root/framework/web/widgets/pagers
diff options
context:
space:
mode:
Diffstat (limited to 'framework/web/widgets/pagers')
-rw-r--r--framework/web/widgets/pagers/CBasePager.php135
-rw-r--r--framework/web/widgets/pagers/CLinkPager.php195
-rw-r--r--framework/web/widgets/pagers/CListPager.php89
-rw-r--r--framework/web/widgets/pagers/pager.css67
4 files changed, 486 insertions, 0 deletions
diff --git a/framework/web/widgets/pagers/CBasePager.php b/framework/web/widgets/pagers/CBasePager.php
new file mode 100644
index 0000000..62ee4a2
--- /dev/null
+++ b/framework/web/widgets/pagers/CBasePager.php
@@ -0,0 +1,135 @@
+<?php
+/**
+ * CBasePager class file.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2011 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+/**
+ * CBasePager is the base class for all pagers.
+ *
+ * It provides the calculation of page count and maintains the current page.
+ *
+ * @property CPagination $pages The pagination information.
+ * @property integer $pageSize Number of items in each page.
+ * @property integer $itemCount Total number of items.
+ * @property integer $pageCount Number of pages.
+ * @property integer $currentPage The zero-based index of the current page. Defaults to 0.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Id: CBasePager.php 3426 2011-10-25 00:01:09Z alexander.makarow $
+ * @package system.web.widgets.pagers
+ * @since 1.0
+ */
+abstract class CBasePager extends CWidget
+{
+ private $_pages;
+
+ /**
+ * Returns the pagination information used by this pager.
+ * @return CPagination the pagination information
+ */
+ public function getPages()
+ {
+ if($this->_pages===null)
+ $this->_pages=$this->createPages();
+ return $this->_pages;
+ }
+
+ /**
+ * Sets the pagination information used by this pager.
+ * @param CPagination $pages the pagination information
+ */
+ public function setPages($pages)
+ {
+ $this->_pages=$pages;
+ }
+
+ /**
+ * Creates the default pagination.
+ * This is called by {@link getPages} when the pagination is not set before.
+ * @return CPagination the default pagination instance.
+ */
+ protected function createPages()
+ {
+ return new CPagination;
+ }
+
+ /**
+ * @return integer number of items in each page.
+ * @see CPagination::getPageSize
+ */
+ public function getPageSize()
+ {
+ return $this->getPages()->getPageSize();
+ }
+
+ /**
+ * @param integer $value number of items in each page
+ * @see CPagination::setPageSize
+ */
+ public function setPageSize($value)
+ {
+ $this->getPages()->setPageSize($value);
+ }
+
+ /**
+ * @return integer total number of items.
+ * @see CPagination::getItemCount
+ */
+ public function getItemCount()
+ {
+ return $this->getPages()->getItemCount();
+ }
+
+ /**
+ * @param integer $value total number of items.
+ * @see CPagination::setItemCount
+ */
+ public function setItemCount($value)
+ {
+ $this->getPages()->setItemCount($value);
+ }
+
+ /**
+ * @return integer number of pages
+ * @see CPagination::getPageCount
+ */
+ public function getPageCount()
+ {
+ return $this->getPages()->getPageCount();
+ }
+
+ /**
+ * @param boolean $recalculate whether to recalculate the current page based on the page size and item count.
+ * @return integer the zero-based index of the current page. Defaults to 0.
+ * @see CPagination::getCurrentPage
+ */
+ public function getCurrentPage($recalculate=true)
+ {
+ return $this->getPages()->getCurrentPage($recalculate);
+ }
+
+ /**
+ * @param integer $value the zero-based index of the current page.
+ * @see CPagination::setCurrentPage
+ */
+ public function setCurrentPage($value)
+ {
+ $this->getPages()->setCurrentPage($value);
+ }
+
+ /**
+ * Creates the URL suitable for pagination.
+ * @param integer $page the page that the URL should point to.
+ * @return string the created URL
+ * @see CPagination::createPageUrl
+ */
+ protected function createPageUrl($page)
+ {
+ return $this->getPages()->createPageUrl($this->getController(),$page);
+ }
+}
diff --git a/framework/web/widgets/pagers/CLinkPager.php b/framework/web/widgets/pagers/CLinkPager.php
new file mode 100644
index 0000000..921dcda
--- /dev/null
+++ b/framework/web/widgets/pagers/CLinkPager.php
@@ -0,0 +1,195 @@
+<?php
+/**
+ * CLinkPager class file.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 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 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 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 &gt;'.
+ */
+ public $nextPageLabel;
+ /**
+ * @var string the text label for the previous page button. Defaults to '&lt; Previous'.
+ */
+ public $prevPageLabel;
+ /**
+ * @var string the text label for the first page button. Defaults to '&lt;&lt; First'.
+ */
+ public $firstPageLabel;
+ /**
+ * @var string the text label for the last page button. Defaults to 'Last &gt;&gt;'.
+ */
+ 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 &gt;');
+ if($this->prevPageLabel===null)
+ $this->prevPageLabel=Yii::t('yii','&lt; Previous');
+ if($this->firstPageLabel===null)
+ $this->firstPageLabel=Yii::t('yii','&lt;&lt; First');
+ if($this->lastPageLabel===null)
+ $this->lastPageLabel=Yii::t('yii','Last &gt;&gt;');
+ 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
+ $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);
+ }
+}
diff --git a/framework/web/widgets/pagers/CListPager.php b/framework/web/widgets/pagers/CListPager.php
new file mode 100644
index 0000000..0ab1f3e
--- /dev/null
+++ b/framework/web/widgets/pagers/CListPager.php
@@ -0,0 +1,89 @@
+<?php
+/**
+ * CListPager class file.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2011 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+
+/**
+ * CListPager displays a dropdown list that contains options leading to different pages of target.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Id: CListPager.php 2799 2011-01-01 19:31:13Z qiang.xue $
+ * @package system.web.widgets.pagers
+ * @since 1.0
+ */
+class CListPager extends CBasePager
+{
+ /**
+ * @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 string the text displayed as a prompt option in the dropdown list. Defaults to null, meaning no prompt.
+ */
+ public $promptText;
+ /**
+ * @var string the format string used to generate page selection text.
+ * The sprintf function will be used to perform the formatting.
+ */
+ public $pageTextFormat;
+ /**
+ * @var array HTML attributes for the enclosing 'div' tag.
+ */
+ public $htmlOptions=array();
+
+ /**
+ * Initializes the pager by setting some default property values.
+ */
+ public function init()
+ {
+ if($this->header===null)
+ $this->header=Yii::t('yii','Go to page: ');
+ if(!isset($this->htmlOptions['id']))
+ $this->htmlOptions['id']=$this->getId();
+ if($this->promptText!==null)
+ $this->htmlOptions['prompt']=$this->promptText;
+ if(!isset($this->htmlOptions['onchange']))
+ $this->htmlOptions['onchange']="if(this.value!='') {window.location=this.value;};";
+ }
+
+ /**
+ * Executes the widget.
+ * This overrides the parent implementation by displaying the generated page buttons.
+ */
+ public function run()
+ {
+ if(($pageCount=$this->getPageCount())<=1)
+ return;
+ $pages=array();
+ for($i=0;$i<$pageCount;++$i)
+ $pages[$this->createPageUrl($i)]=$this->generatePageText($i);
+ $selection=$this->createPageUrl($this->getCurrentPage());
+ echo $this->header;
+ echo CHtml::dropDownList($this->getId(),$selection,$pages,$this->htmlOptions);
+ echo $this->footer;
+ }
+
+ /**
+ * Generates the list option for the specified page number.
+ * You may override this method to customize the option display.
+ * @param integer $page zero-based page number
+ * @return string the list option for the page number
+ */
+ protected function generatePageText($page)
+ {
+ if($this->pageTextFormat!==null)
+ return sprintf($this->pageTextFormat,$page+1);
+ else
+ return $page+1;
+ }
+} \ No newline at end of file
diff --git a/framework/web/widgets/pagers/pager.css b/framework/web/widgets/pagers/pager.css
new file mode 100644
index 0000000..1c802cc
--- /dev/null
+++ b/framework/web/widgets/pagers/pager.css
@@ -0,0 +1,67 @@
+/**
+ * CSS styles for CLinkPager.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2010 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ * @version $Id: pager.css 1678 2010-01-07 21:02:00Z qiang.xue $
+ * @since 1.0
+ */
+
+ul.yiiPager
+{
+ font-size:11px;
+ border:0;
+ margin:0;
+ padding:0;
+ line-height:100%;
+ display:inline;
+}
+
+ul.yiiPager li
+{
+ display:inline;
+}
+
+ul.yiiPager a:link,
+ul.yiiPager a:visited
+{
+ border:solid 1px #9aafe5;
+ font-weight:bold;
+ color:#0e509e;
+ padding:1px 6px;
+ text-decoration:none;
+}
+
+ul.yiiPager .page a
+{
+ font-weight:normal;
+}
+
+ul.yiiPager a:hover
+{
+ border:solid 1px #0e509e;
+}
+
+ul.yiiPager .selected a
+{
+ background:#2e6ab1;
+ color:#FFFFFF;
+ font-weight:bold;
+}
+
+ul.yiiPager .hidden a
+{
+ border:solid 1px #DEDEDE;
+ color:#888888;
+}
+
+/**
+ * Hide first and last buttons by default.
+ */
+ul.yiiPager .first,
+ul.yiiPager .last
+{
+ display:none;
+} \ No newline at end of file