-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWizard.php
More file actions
225 lines (203 loc) · 7.15 KB
/
Copy pathWizard.php
File metadata and controls
225 lines (203 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/**
* @link https://github.com/Chofoteddy/yii2-bootstrap-wizard
* @copyright Copyright (c) 2015 Chofoteddy
* @license https://raw.githubusercontent.com/Chofoteddy/yii2-bootstrap-wizard/master/LICENSE
*/
namespace chofoteddy\wizard;
use Yii;
use yii\base\InvalidConfigException;
use yii\bootstrap\Nav;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/**
* Wizard renders a wizard bootstrap javascript component.
*
* For example:
*
* ```php
* echo Wizard::widget([
* 'items' => [
* // equivalent to the above
* [
* 'label' => 'Collapsible Group Item #1',
* 'content' => 'Anim pariatur cliche...',
* // open its content by default
* 'contentOptions' => ['class' => 'in']
* ],
* // another wizard step
* [
* 'label' => 'Collapsible Group Item #1',
* 'content' => 'Anim pariatur cliche...',
* 'contentOptions' => [...],
* 'options' => [...],
* ],
* // short cut to label => content
* 'Finished' => 'Thanks for filling your information'
* ]
* ]);
* ```
*
* @see http://vadimg.com/twitter-bootstrap-wizard-example/
* @author Christopher Castaneida <chofoteddy@gmail.com>
* @author Angel Guevara <angeldelcaos@gmail.com>
*/
class Wizard extends \yii\bootstrap\Widget
{
/**
* @var array list of groups in the wizard widget. Each array element
* represents a single group with the following structure:
*
* - label: string, required, the group header label.
* - url: string|array, optional, an external URL. When this is specified,
* clicking on this tab will bring
* - encode: boolean, optional, whether this label should be HTML-encoded.
* This param will override global `$this->encodeLabels` param.
* - content: array|string|object, required, the content (HTML) of the group
* - options: array, optional, the HTML attributes of the group
* - contentOptions: optional, the HTML attributes of the group's content
* - visible: boolean, optional, whether the item should be visible or not.
* Defaults to true.
*/
public $items = [];
/**
* @var array list of HTML attributes for the item container tags. This will be overwritten
* by the "options" set in individual [[items]]. The following special options are recognized:
*
* - tag: string, defaults to "div", the tag name of the item container tags.
*
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $itemOptions = [];
/**
* @var array list of HTML attributes for the item container tags. This will be overwritten
* by the "options" set in individual [[items]]. The following special options are recognized:
*
* - tag: string, defaults to "li", the tag name of the item container tags.
*
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $labelOptions = [];
/**
* @var array list of HTML attributes for the tab header link tags. This will be overwritten
* by the "linkOptions" set in individual [[items]].
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $linkOptions = [];
/**
* @var array list of HTML attributes for the header container tags.
* The following special options are recognized:
*
* - tag: string, defaults to "ul", the tag name of the item container tags.
*
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $headerOptions = [];
/**
* @var array options to get passed to the \yii\bootstrap\Nav widget
*
* @see \yii\bootstrap\Widget::$options for details.
*/
public $navOptions = [];
/**
* @var boolean whether the labels for header items should be HTML-encoded.
*/
public $encodeLabels = true;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
Html::addCssClass($this->itemOptions, ['widget' => 'tab-pane']);
}
/**
* @inheritdoc
*/
public function run()
{
WizardAsset::register($this->view);
$this->registerPlugin('bootstrapWizard');
return implode("\n", [
Html::beginTag('div', $this->options),
$this->renderItems(),
Html::endTag('div')
]);
}
/**
* Renders wizard items as specified on [[items]].
* @return string the rendering result.
* @throws InvalidConfigException.
*/
public function renderItems()
{
$labels = [];
$contents = [];
$n = 0;
foreach ($this->items as $key => $item) {
if (!ArrayHelper::remove($item, 'visible', true)) {
continue;
}
if (!is_string($key) && !array_key_exists('label', $item)) {
throw new InvalidConfigException(
"The 'label' option is required."
);
}
if (is_string($item)) {
$item = ['content' => $item];
}
$label = is_string($key) ? $key : $item['label'];
$encodeLabel = isset($item['encode'])
? $item['encode']
: $this->encodeLabels;
$label = $encodeLabel
? Html::encode($label)
: $label;
$itemOptions = array_merge(
$this->itemOptions,
ArrayHelper::getValue($item, 'options', [])
);
$labelOptions = array_merge(
$this->labelOptions,
ArrayHelper::getValue($item, 'labelOptions', [])
);
$linkOptions = array_merge(
$this->linkOptions,
ArrayHelper::getValue($item, 'linkOptions', [])
);
$itemOptions['id'] = ArrayHelper::getValue(
$itemOptions,
'id',
$this->options['id'] . '-wizard' . $n
);
if (isset($item['url'])) {
$labels[] = [
'label' => $label,
'url' => $item['url'],
'linkOptions' => $linkOptions,
'options' => $labelOptions,
];
} else {
$linkOptions['data-toggle'] = 'tab';
$labels[] = [
'label' => $label,
'url' => '#' . $itemOptions['id'],
'linkOptions' => $linkOptions,
'options' => $labelOptions,
];
}
$contents[] = Html::tag(
ArrayHelper::getValue($itemOptions, 'tag', 'div'),
ArrayHelper::getValue($item, 'content', ''),
$itemOptions
);
$n++;
}
return Nav::widget(['items' => $labels, 'options' => $this->navOptions])
. Html::tag(
'div',
implode("\n", $contents),
['class' => 'tab-content']
);
}
}