HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
TbWizard.php
Go to the documentation of this file.
1 <?php
11 Yii::import('booster.widgets.TbMenu');
12 
21 class TbWizard extends CWidget
22 {
23  // Tab placements.
24  const PLACEMENT_ABOVE = 'above';
25  const PLACEMENT_BELOW = 'below';
26  const PLACEMENT_LEFT = 'left';
27  const PLACEMENT_RIGHT = 'right';
28 
36 
41  public $placement;
42 
46  public $tabs = array();
47 
51  public $stacked = false;
52 
56  public $encodeLabel = true;
57 
61  public $events = array();
62 
66  public $htmlOptions = array();
67 
71  public $options = array();
72 
76  public $addTabsNavBar = false;
77 
81  public $pagerContent = '<ul class="pager wizard">
82  <li class="previous first" style="display:none;"><a href="#">First</a></li>
83  <li class="previous"><a href="#">Previous</a></li>
84  <li class="next last" style="display:none;"><a href="#">Last</a></li>
85  <li class="next"><a href="#">Next</a></li>
86  </ul>';
87 
93  public function init()
94  {
95  if (!isset($this->htmlOptions['id'])) {
96  $this->htmlOptions['id'] = $this->getId();
97  }
98 
99  $classes = array();
100 
101  $validPlacements = array(
102  self::PLACEMENT_ABOVE,
103  self::PLACEMENT_BELOW,
104  self::PLACEMENT_LEFT,
105  self::PLACEMENT_RIGHT
106  );
107 
108  if (isset($this->placement) && in_array($this->placement, $validPlacements)) {
109  $classes[] = 'tabs-' . $this->placement;
110  }
111 
112  if (!empty($classes)) {
113  $classes = implode(' ', $classes);
114  if (isset($this->htmlOptions['class'])) {
115  $this->htmlOptions['class'] .= ' ' . $classes;
116  } else {
117  $this->htmlOptions['class'] = $classes;
118  }
119  }
120  }
121 
127  public function run()
128  {
129  $id = $this->id;
130  $content = array();
131  $items = $this->normalizeTabs($this->tabs, $content);
132 
133  ob_start();
134  if ($this->addTabsNavBar) {
135  echo '<div class="navbar"><div class="navbar-inner">';
136  }
137  $this->controller->widget(
138  'booster.widgets.TbMenu',
139  array(
140  'stacked' => $this->stacked,
141  'type' => $this->type,
142  'encodeLabel' => $this->encodeLabel,
143  'items' => $items,
144  )
145  );
146  if ($this->addTabsNavBar) {
147  echo '</div></div>';
148  }
149  $tabs = ob_get_clean();
150 
151  ob_start();
152  echo '<div class="tab-content">';
153  echo implode('', $content);
154  echo $this->pagerContent;
155  echo '</div>';
156  $content = ob_get_clean();
157 
158  echo CHtml::openTag('div', $this->htmlOptions);
159  echo $this->placement === self::PLACEMENT_BELOW ? $content . $tabs : $tabs . $content;
160  echo '</div>';
161 
163  $cs = Yii::app()->getClientScript();
164 
165  $cs->registerPackage('bootstrap.wizard');
166 
167  // Override options
168  if ($this->type && !isset($this->options['class'])) {
169  $this->options['class'] = $this->type;
170  }
171 
172  $options = CJavaScript::encode($this->options);
173 
174  $cs->registerScript(__CLASS__ . '#' . $id, "jQuery('#{$id}').bootstrapWizard({$options});");
175 
176  foreach ($this->events as $name => $handler) {
177  $handler = CJavaScript::encode($handler);
178  $cs->registerScript(__CLASS__ . '#' . $id . '_' . $name, "jQuery('#{$id}').on('{$name}', {$handler});");
179  }
180  }
181 
193  protected function normalizeTabs($tabs, &$panes, &$i = 0)
194  {
195  $id = $this->getId();
196  $items = array();
197 
198  foreach ($tabs as $tab) {
199  $item = $tab;
200 
201  if (isset($item['visible']) && $item['visible'] === false) {
202  continue;
203  }
204 
205  if (!isset($item['itemOptions'])) {
206  $item['itemOptions'] = array();
207  }
208 
209  $item['linkOptions']['data-toggle'] = 'tab';
210 
211  if (isset($tab['items'])) {
212  $item['items'] = $this->normalizeTabs($item['items'], $panes, $i);
213  } else {
214  if (!isset($item['id'])) {
215  $item['id'] = $id . '_tab_' . ($i + 1);
216  }
217 
218  $item['url'] = '#' . $item['id'];
219 
220  if (!isset($item['content'])) {
221  $item['content'] = '';
222  }
223 
224  $content = $item['content'];
225  unset($item['content']);
226 
227  if (!isset($item['paneOptions'])) {
228  $item['paneOptions'] = array();
229  }
230 
231  $paneOptions = $item['paneOptions'];
232  unset($item['paneOptions']);
233 
234  $paneOptions['id'] = $item['id'];
235 
236  $classes = array('tab-pane fade');
237 
238  if (isset($item['active']) && $item['active']) {
239  $classes[] = 'active in';
240  }
241 
242  $classes = implode(' ', $classes);
243  if (isset($paneOptions['class'])) {
244  $paneOptions['class'] .= ' ' . $classes;
245  } else {
246  $paneOptions['class'] = $classes;
247  }
248 
249  $panes[] = CHtml::tag('div', $paneOptions, $content);
250  $i++; // increment the tab-index
251  }
252  $items[] = $item;
253  }
254  return $items;
255  }
256 }