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\ArrayHelper;
11 use foundation\helpers\Html;
12 use foundation\enum\Enum;
13
14 /**
15 * Section renders Foundation sections, which are similar to tabs as a way to selectively show a single panel of content
16 * at a time. Sections replace Accordion, Tabs, Vertical Nav & Horizontal Nav.
17 *
18 * For example:
19 *
20 * <pre>
21 * echo Tabs::widget(array(
22 * 'items' => array(
23 * array(
24 * 'label' => 'One',
25 * 'content' => 'Anim pariatur cliche...',
26 * 'active' => true
27 * ),
28 * array(
29 * 'label' => 'Two',
30 * 'content' => 'Anim pariatur cliche...',
31 * 'options' => array('id' => 'myveryownID'),
32 * ),
33 * ),
34 * ));
35 * </pre>
36 *
37 * @author Antonio Ramirez <amigo.cobos@gmail.com>
38 * @package foundation\helpers
39 */
40 class Section extends base\Widget
41 {
42
43 /**
44 * @var array list of tabs in the tabs widget. Each array element represents a single
45 * tab with the following structure:
46 *
47 * - label: string, required, the tab header label.
48 * - headerOptions: array, optional, the HTML attributes of the tab header.
49 * - content: array, required if `items` is not set. The content (HTML) of the section pane.
50 * - options: array, optional, the HTML attributes of the content pane container.
51 * - active: boolean, optional, whether the item section header and pane should be visible or not.
52 */
53 public $items = array();
54 /**
55 * @var string style of the sections. Defaults to 'auto', which means that will switch between tabs and
56 * accordion based on the resolution of the device.
57 */
58 public $style = Enum::SECTION_STYLE_AUTO;
59 /**
60 * @var array the plugin options. The following is the list of supported options and their default values:
61 *
62 * ´´´php
63 * array(
64 * 'deep_linking' => 'false',
65 * 'one_up' => 'true',
66 * 'rtl' => 'false',
67 * 'callback' => 'js:function(){}',
68 * )
69 * ´´´
70 */
71 public $pluginOptions = array();
72
73 /**
74 * Initilizes the widget
75 */
76 public function init()
77 {
78 $this->assets = array(
79 'js' => YII_DEBUG ? 'foundation/foundation.section.js' : 'foundation.min.js'
80 );
81
82 Html::addCssClass($this->htmlOptions, Enum::SECTION_CONTAINER);
83 Html::addCssClass($this->htmlOptions, $this->style);
84 ArrayHelper::addValue('data-section', $this->style, $this->htmlOptions);
85 $this->registerClientScript();
86 parent::init();
87 }
88
89 /**
90 * Renders the widget
91 */
92 public function run()
93 {
94 echo $this->renderSection();
95 }
96
97 /**
98 * Renders the section
99 * @return string the rendering result
100 */
101 public function renderSection()
102 {
103 $sections = array();
104 foreach ($this->items as $item) {
105 $sections[] = $this->renderItem($item);
106 }
107 return \CHtml::tag('div', $this->htmlOptions, implode("\n", $sections));
108 }
109
110 /**
111 * Renders a section item
112 * @param array $item the section item
113 * @return string the section result
114 */
115 public function renderItem($item)
116 {
117 $sectionItem = array();
118 $sectionItem[] = \CHtml::tag(
119 'p',
120 array('class' => 'title', 'data-section-title' => 'data-section-title'),
121 \CHtml::link(ArrayHelper::getValue($item, 'label', 'Section Title'), '#')
122 );
123 $options = ArrayHelper::getValue($item, 'options', array());
124 Html::addCssClass($options, 'content');
125 ArrayHelper::addValue('data-section-content', 'data-section-content', $options);
126 $sectionOptions = array();
127 if (ArrayHelper::getValue($item, 'active')) {
128 ArrayHelper::addValue('class', 'active', $sectionOptions);
129 }
130 $sectionItem[] = \CHtml::tag('div', $options, ArrayHelper::getValue($item, 'content', 'Section Content'));
131 return \CHtml::tag('section', $sectionOptions, implode("\n", $sectionItem));
132 }
133
134 /**
135 * Registers the client options to initialize the plugin -if set.
136 */
137 public function registerClientScript()
138 {
139 if (!empty($this->pluginOptions)) {
140 $options = \CJavaScript::encode($this->pluginOptions);
141 \Yii::app()->clientScript
142 ->registerScript('Section#' . $this->getId(), "$(document).foundation('section', {$options}");
143 }
144 }
145 }