PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/admincrud/extensions/bootstrap/widgets/TbWizard.php

https://github.com/max-rautkin/yii-admincrud
PHP | 250 lines | 139 code | 41 blank | 70 comment | 22 complexity | 9d09b6c112c02df5f8860855df1b9279 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /*## TbWizard class file.
  3. *
  4. * @author Christoffer Niska <ChristofferNiska@gmail.com>
  5. * @copyright Copyright &copy; Christoffer Niska 2011-
  6. * @copyright Copyright &copy; Vincent Gabriel 2012
  7. * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
  8. * @package bootstrap.widgets
  9. */
  10. Yii::import('bootstrap.widgets.TbMenu');
  11. /**
  12. * Bootstrap Javascript tabs widget.
  13. * @see <http://twitter.github.com/bootstrap/javascript.html#tabs>
  14. */
  15. class TbWizard extends CWidget
  16. {
  17. // Tab placements.
  18. const PLACEMENT_ABOVE = 'above';
  19. const PLACEMENT_BELOW = 'below';
  20. const PLACEMENT_LEFT = 'left';
  21. const PLACEMENT_RIGHT = 'right';
  22. /**
  23. * @var string the type of tabs to display.
  24. * Defaults to 'tabs'. Valid values are 'tabs' and 'pills'.
  25. * Please not that Javascript pills are not fully supported in Bootstrap yet!
  26. * @see TbMenu::$type
  27. */
  28. public $type = TbMenu::TYPE_TABS;
  29. /**
  30. * @var string the placement of the tabs.
  31. * Valid values are 'above', 'below', 'left' and 'right'.
  32. */
  33. public $placement;
  34. /**
  35. * @var array the tab configuration.
  36. */
  37. public $tabs = array();
  38. /**
  39. * @var boolean indicates whether to stack navigation items.
  40. */
  41. public $stacked = false;
  42. /**
  43. * @var boolean whether to encode item labels.
  44. */
  45. public $encodeLabel = true;
  46. /**
  47. * @var string[] the Javascript event handlers.
  48. */
  49. public $events = array();
  50. /**
  51. * @var array the HTML attributes for the widget container.
  52. */
  53. public $htmlOptions = array();
  54. /**
  55. * @var array the JS options for the bootstrap wizard plugin
  56. */
  57. public $options = array();
  58. /**
  59. * @var boolean Add tabs navbar to the main tab navigation
  60. */
  61. public $addTabsNavBar = false;
  62. /**
  63. * @var string Pager HTML code
  64. */
  65. public $pagerContent = '<ul class="pager wizard">
  66. <li class="previous first" style="display:none;"><a href="#">First</a></li>
  67. <li class="previous"><a href="#">Previous</a></li>
  68. <li class="next last" style="display:none;"><a href="#">Last</a></li>
  69. <li class="next"><a href="#">Next</a></li>
  70. </ul>';
  71. /**
  72. *### .init()
  73. *
  74. * Initializes the widget.
  75. */
  76. public function init()
  77. {
  78. if (!isset($this->htmlOptions['id'])) {
  79. $this->htmlOptions['id'] = $this->getId();
  80. }
  81. $classes = array();
  82. $validPlacements = array(
  83. self::PLACEMENT_ABOVE,
  84. self::PLACEMENT_BELOW,
  85. self::PLACEMENT_LEFT,
  86. self::PLACEMENT_RIGHT
  87. );
  88. if (isset($this->placement) && in_array($this->placement, $validPlacements)) {
  89. $classes[] = 'tabs-' . $this->placement;
  90. }
  91. if (!empty($classes)) {
  92. $classes = implode(' ', $classes);
  93. if (isset($this->htmlOptions['class'])) {
  94. $this->htmlOptions['class'] .= ' ' . $classes;
  95. } else {
  96. $this->htmlOptions['class'] = $classes;
  97. }
  98. }
  99. }
  100. /**
  101. *### .run()
  102. *
  103. * Run this widget.
  104. */
  105. public function run()
  106. {
  107. $id = $this->id;
  108. $content = array();
  109. $items = $this->normalizeTabs($this->tabs, $content);
  110. ob_start();
  111. if ($this->addTabsNavBar) {
  112. echo '<div class="navbar"><div class="navbar-inner">';
  113. }
  114. $this->controller->widget(
  115. 'bootstrap.widgets.TbMenu',
  116. array(
  117. 'stacked' => $this->stacked,
  118. 'type' => $this->type,
  119. 'encodeLabel' => $this->encodeLabel,
  120. 'items' => $items,
  121. )
  122. );
  123. if ($this->addTabsNavBar) {
  124. echo '</div></div>';
  125. }
  126. $tabs = ob_get_clean();
  127. ob_start();
  128. echo '<div class="tab-content">';
  129. echo implode('', $content);
  130. echo $this->pagerContent;
  131. echo '</div>';
  132. $content = ob_get_clean();
  133. echo CHtml::openTag('div', $this->htmlOptions);
  134. echo $this->placement === self::PLACEMENT_BELOW ? $content . $tabs : $tabs . $content;
  135. echo '</div>';
  136. /** @var CClientScript $cs */
  137. $cs = Yii::app()->getClientScript();
  138. Yii::app()->bootstrap->registerAssetJs('jquery.bootstrap.wizard.js');
  139. // Override options
  140. if ($this->type && !isset($this->options['class'])) {
  141. $this->options['class'] = $this->type;
  142. }
  143. $options = CJavaScript::encode($this->options);
  144. $cs->registerScript(__CLASS__ . '#' . $id, "jQuery('#{$id}').bootstrapWizard({$options});");
  145. foreach ($this->events as $name => $handler) {
  146. $handler = CJavaScript::encode($handler);
  147. $cs->registerScript(__CLASS__ . '#' . $id . '_' . $name, "jQuery('#{$id}').on('{$name}', {$handler});");
  148. }
  149. }
  150. /**
  151. *### .normalizeTabs()
  152. *
  153. * Normalizes the tab configuration.
  154. *
  155. * @param array $tabs the tab configuration
  156. * @param array $panes a reference to the panes array
  157. * @param integer $i the current index
  158. *
  159. * @return array the items
  160. */
  161. protected function normalizeTabs($tabs, &$panes, &$i = 0)
  162. {
  163. $id = $this->getId();
  164. $items = array();
  165. foreach ($tabs as $tab) {
  166. $item = $tab;
  167. if (isset($item['visible']) && $item['visible'] === false) {
  168. continue;
  169. }
  170. if (!isset($item['itemOptions'])) {
  171. $item['itemOptions'] = array();
  172. }
  173. $item['linkOptions']['data-toggle'] = 'tab';
  174. if (isset($tab['items'])) {
  175. $item['items'] = $this->normalizeTabs($item['items'], $panes, $i);
  176. } else {
  177. if (!isset($item['id'])) {
  178. $item['id'] = $id . '_tab_' . ($i + 1);
  179. }
  180. $item['url'] = '#' . $item['id'];
  181. if (!isset($item['content'])) {
  182. $item['content'] = '';
  183. }
  184. $content = $item['content'];
  185. unset($item['content']);
  186. if (!isset($item['paneOptions'])) {
  187. $item['paneOptions'] = array();
  188. }
  189. $paneOptions = $item['paneOptions'];
  190. unset($item['paneOptions']);
  191. $paneOptions['id'] = $item['id'];
  192. $classes = array('tab-pane fade');
  193. if (isset($item['active']) && $item['active']) {
  194. $classes[] = 'active in';
  195. }
  196. $classes = implode(' ', $classes);
  197. if (isset($paneOptions['class'])) {
  198. $paneOptions['class'] .= ' ' . $classes;
  199. } else {
  200. $paneOptions['class'] = $classes;
  201. }
  202. $panes[] = CHtml::tag('div', $paneOptions, $content);
  203. $i++; // increment the tab-index
  204. }
  205. $items[] = $item;
  206. }
  207. return $items;
  208. }
  209. }