1 <?php
2 /**
3 * @copyright Copyright © 2amigOS! Consulting Group 2013-
4 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
5 * @package foundation.components
6 * @version 1.0.0
7 */
8 namespace foundation\widgets;
9
10 use foundation\helpers\Html;
11 use foundation\helpers\Foundation;
12 use foundation\enum\Enum;
13
14 /**
15 * Pager renders a Foundation pagination component.
16 *
17 * @see http://foundation.zurb.com/docs/components/pagination.html
18 *
19 * @author Antonio Ramirez <amigo.cobos@gmail.com>
20 * @package foundation\widgets
21 */
22 class Pager extends \CLinkPager
23 {
24 const CSS_FIRST_PAGE = 'arrow';
25 const CSS_LAST_PAGE = 'arrow';
26 const CSS_PREVIOUS_PAGE = 'arrow';
27 const CSS_NEXT_PAGE = 'arrow';
28 const CSS_HIDDEN_PAGE = 'hide-for-landscape hide-for-portrait';
29 const CSS_SELECTED_PAGE = 'current';
30 const CSS_INTERNAL_PAGE = 'page';
31 /**
32 * @var string the text label for the next page button.
33 */
34 public $nextPageLabel = '›';
35 /**
36 * @var string the text label for the previous page button.
37 */
38 public $prevPageLabel = '‹';
39 /**
40 * @var string the text label for the first page button.
41 */
42 public $firstPageLabel = '«';
43 /**
44 * @var string the text label for the last page button.
45 */
46 public $lastPageLabel = '»';
47 /**
48 * @var boolean whether the "first" and "last" buttons should be hidden.
49 * Defaults to false.
50 */
51 public $hideFirstAndLast = false;
52 /**
53 * @var bool whether to center the pager or not
54 */
55 public $centered = true;
56
57 /**
58 * Initializes the widget.
59 */
60 public function init()
61 {
62 Html::addCssClass($this->htmlOptions, Enum::PAGINATION);
63 parent::init();
64 }
65
66 /**
67 * Runs the widget.
68 */
69 public function run()
70 {
71 $this->registerClientScript();
72 $buttons = $this->createPageButtons();
73
74 if (empty($buttons))
75 return true;
76 if ($this->centered) {
77 echo \CHtml::openTag('div', array('class' => Enum::PAGINATION_CENTERED));
78 }
79 echo \CHtml::tag('ul', $this->htmlOptions, implode("\n", $buttons));
80 if ($this->centered) {
81 echo \CHtml::closeTag('div');
82 }
83 }
84
85
86 /**
87 * Registers the needed CSS file.
88 * @param string $url the CSS URL. If null, a default CSS URL will be used.
89 */
90 public static function registerCssFile($url = null)
91 {
92 if ($url !== null) {
93 \Yii::app()->getClientScript()->registerCssFile($url);
94
95 } else {
96 Foundation::registerCoreCss();
97 }
98 }
99
100 /**
101 * Ported from Yii2 widget's function. Creates a widget instance and runs it. We cannot use 'widget' name as it
102 * conflicts with CBaseController component.
103 *
104 * The widget rendering result is returned by this method.
105 * @param array $config name-value pairs that will be used to initialize the object properties
106 * @return string the rendering result of the widget.
107 */
108 public static function display($config = array())
109 {
110 ob_start();
111 ob_implicit_flush(false);
112 /** @var Widget $widget */
113 $config['class'] = get_called_class();
114 $widget = \Yii::createComponent($config);
115 $widget->init();
116 $widget->run();
117 return ob_get_clean();
118 }
119 }
120