Overview

Packages

  • bootstrap
    • behaviors
    • components
    • form
    • gii
    • helpers
    • widgets

Classes

  • TbActiveForm
  • TbAffix
  • TbAlert
  • TbBreadcrumb
  • TbButtonColumn
  • TbCollapse
  • TbDataColumn
  • TbDetailView
  • TbGridView
  • TbHeroUnit
  • TbListView
  • TbModal
  • TbNav
  • TbNavbar
  • TbPager
  • TbScrollspy
  • TbTabs
  • TbThumbnails
  • TbTypeAhead
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * TbPager class file.
  4:  * @author Christoffer Niska <christoffer.niska@gmail.com>
  5:  * @copyright Copyright &copy; Christoffer Niska 2013-
  6:  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  7:  * @package bootstrap.widgets
  8:  */
  9: 
 10: Yii::import('bootstrap.helpers.TbHtml');
 11: Yii::import('bootstrap.behaviors.TbWidget');
 12: 
 13: /**
 14:  * Bootstrap pager widget.
 15:  * http://twitter.github.com/bootstrap/components.html#pagination
 16:  */
 17: class TbPager extends CBasePager
 18: {
 19:     /**
 20:      * @var string the pager size.
 21:      */
 22:     public $size;
 23:     /**
 24:      * @var integer maximum number of page buttons that can be displayed.
 25:      */
 26:     public $maxButtonCount = 5;
 27:     /**
 28:      * @var string the text label for the next page button.
 29:      */
 30:     public $nextPageLabel = '&rsaquo;';
 31:     /**
 32:      * @var string the text label for the previous page button.
 33:      */
 34:     public $prevPageLabel = '&lsaquo;';
 35:     /**
 36:      * @var string the text label for the first page button.
 37:      */
 38:     public $firstPageLabel = '&laquo;';
 39:     /**
 40:      * @var string the text label for the last page button.
 41:      */
 42:     public $lastPageLabel = '&raquo;';
 43: 
 44:     /**
 45:      * @var boolean whether the "first" and "last" buttons should be hidden.
 46:      * Defaults to false.
 47:      */
 48:     public $hideFirstAndLast = false;
 49:     /**
 50:      * @var array HTML attributes for the pager container tag.
 51:      */
 52:     public $htmlOptions = array();
 53: 
 54:     /**
 55:      * Initializes the widget.
 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:      * Runs the widget.
 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:      * Creates the page buttons.
 79:      * @return array a list of page buttons (in HTML code).
 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); // currentPage is calculated in getPageRange()
 90:         $links = array();
 91: 
 92:         // first page
 93:         if (!$this->hideFirstAndLast) {
 94:             $links[] = $this->createPageLink($this->firstPageLabel, 0, $currentPage <= 0, false);
 95:         }
 96: 
 97:         // prev page
 98:         if (($page = $currentPage - 1) < 0) {
 99:             $page = 0;
100:         }
101: 
102:         $links[] = $this->createPageLink($this->prevPageLabel, $page, $currentPage <= 0, false);
103: 
104:         // internal pages
105:         for ($i = $beginPage; $i <= $endPage; ++$i) {
106:             $links[] = $this->createPageLink($i + 1, $i, false, $i == $currentPage);
107:         }
108: 
109:         // next page
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:         // last page
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:      * Creates a page link.
131:      * @param string $label the link label text.
132:      * @param integer $page the page number.
133:      * @param boolean $visible whether the link is disabled.
134:      * @param boolean $active whether the link is active.
135:      * @return string the generated link.
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:      * @return array the begin and end pages that need to be displayed.
149:      */
150:     protected function getPageRange()
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: 
Yiistrap API documentation generated by ApiGen 2.8.0