HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
TbPager.php
Go to the documentation of this file.
1 <?php
17 class TbPager extends CLinkPager {
18 
19  // Pager alignments.
20  const ALIGNMENT_CENTER = 'centered';
21  const ALIGNMENT_RIGHT = 'right';
22 
26  public $containerTag = 'div';
27 
31  public $containerHtmlOptions = array();
32 
38 
43  public $header = '';
44 
49  public $cssFile = false;
50 
54  public $displayFirstAndLast = false;
55 
61  public function init() {
62 
63  if ($this->nextPageLabel === null) {
64  $this->nextPageLabel = '&raquo;';
65  }
66 
67  if ($this->prevPageLabel === null) {
68  $this->prevPageLabel = '&laquo;';
69  }
70 
71  $classes = array('pagination');
72 
73  /* TODO: move these to styles files! */
74  $style = '';
75  $containerStyle = '';
76 
77  $validAlignments = array(self::ALIGNMENT_CENTER, self::ALIGNMENT_RIGHT);
78 
79  if (in_array($this->alignment, $validAlignments)) {
80  if($this->alignment == self::ALIGNMENT_RIGHT)
81  $classes[] = 'pull-right';
82 
83  if($this->alignment == self::ALIGNMENT_CENTER) {
84  // $style = 'margin-left: auto; margin-right: auto;'; // not needed!
85  $containerStyle = 'text-align: center;';
86  }
87  }
88 
89  if (!empty($classes)) {
90  $classes = implode(' ', $classes);
91  if (isset($this->htmlOptions['class'])) {
92  $this->htmlOptions['class'] = ' ' . $classes;
93  } else {
94  $this->htmlOptions['class'] = $classes;
95  }
96  }
97 
98  if(!empty($style)) {
99  if(isset($this->htmlOptions['style']) && !empty($this->htmlOptions['style']))
100  $this->htmlOptions['style'] .= ' '.$style;
101  else
102  $this->htmlOptions['style'] = $style;
103  }
104 
105  if(!empty($containerStyle)) {
106  if(isset($this->containerHtmlOptions['style']) && !empty($this->containerHtmlOptions['style']))
107  $this->containerHtmlOptions['style'] .= ' '.$containerStyle;
108  else
109  $this->containerHtmlOptions['style'] = $containerStyle;
110  }
111 
112  parent::init();
113  }
114 
119  public function run() {
120 
121  $this->registerClientScript();
122  $buttons=$this->createPageButtons();
123  if(empty($buttons))
124  return;
125  echo CHtml::openTag($this->containerTag, $this->containerHtmlOptions);
126  echo $this->header;
127  echo CHtml::tag('ul',$this->htmlOptions,implode("\n",$buttons));
128  echo '<div style="clear: both;"></div>';
129  echo $this->footer;
130  echo CHtml::closeTag($this->containerTag);
131  }
132 
139  protected function createPageButtons() {
140 
141  if (($pageCount = $this->getPageCount()) <= 1) {
142  return array();
143  }
144 
145  list ($beginPage, $endPage) = $this->getPageRange();
146 
147  $currentPage = $this->getCurrentPage(false); // currentPage is calculated in getPageRange()
148 
149  $buttons = array();
150 
151  // first page
152  if ($this->displayFirstAndLast) {
153  $buttons[] = $this->createPageButton($this->firstPageLabel, 0, 'first', $currentPage <= 0, false);
154  }
155 
156  // prev page
157  if (($page = $currentPage - 1) < 0) {
158  $page = 0;
159  }
160 
161  $buttons[] = $this->createPageButton($this->prevPageLabel, $page, 'previous', $currentPage <= 0, false);
162 
163  // internal pages
164  for ($i = $beginPage; $i <= $endPage; ++$i) {
165  $buttons[] = $this->createPageButton($i + 1, $i, '', false, $i == $currentPage);
166  }
167 
168  // next page
169  if (($page = $currentPage + 1) >= $pageCount - 1) {
170  $page = $pageCount - 1;
171  }
172 
173  $buttons[] = $this->createPageButton(
174  $this->nextPageLabel,
175  $page,
176  'next',
177  $currentPage >= ($pageCount - 1),
178  false
179  );
180 
181  // last page
182  if ($this->displayFirstAndLast) {
183  $buttons[] = $this->createPageButton(
184  $this->lastPageLabel,
185  $pageCount - 1,
186  'last',
187  $currentPage >= ($pageCount - 1),
188  false
189  );
190  }
191 
192  return $buttons;
193  }
194 
209  protected function createPageButton($label, $page, $class, $hidden, $selected)
210  {
211  if ($hidden || $selected) {
212  $class .= ' ' . ($hidden ? 'disabled' : 'active');
213  }
214 
215  return CHtml::tag('li', array('class' => $class), CHtml::link($label, $this->createPageUrl($page)));
216  }
217 }