-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWizardHelper.php
More file actions
300 lines (264 loc) · 7.98 KB
/
WizardHelper.php
File metadata and controls
300 lines (264 loc) · 7.98 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/**
* Wizard Helper
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('AppHelper', 'View/Helper');
/**
* ウィザードHelper
*
* #### コントローラの定義(helpersのサンプル)
* https://github.com/NetCommons3/Rooms/blob/master/Controller/RoomsAppController.php#L73-L98
* ```
* public $helpers = array(
* 'NetCommons.Wizard' => array(
* 'navibar' => array(
* self::WIZARD_ROOMS => array(
* 'url' => array(
* 'controller' => 'rooms',
* 'action' => 'add',
* ),
* 'label' => array('rooms', 'General setting'),
* ),
* self::WIZARD_ROOMS_ROLES_USERS => array(
* 'url' => array(
* 'controller' => 'rooms_roles_users',
* 'action' => 'edit',
* ),
* 'label' => array('rooms', 'Edit the members to join'),
* ),
* self::WIZARD_PLUGINS_ROOMS => array(
* 'url' => array(
* 'controller' => 'plugins_rooms',
* 'action' => 'edit',
* ),
* 'label' => array('rooms', 'Select the plugins to join'),
* ),
* ),
* 'cancelUrl' => null
* ),
* );
* ```
*
* #### viewファイルのWizardバーサンプル
* https://github.com/NetCommons3/Rooms/blob/master/View/PluginsRooms/edit.ctp#L17
* ```
* echo $this->Wizard->navibar(RoomsAppController::WIZARD_PLUGINS_ROOMS);
* ```
*
* ##### viewファイルのボタンサンプル
* (ワークフローなし)<br>
* https://github.com/NetCommons3/Rooms/blob/master/View/PluginsRooms/edit.ctp#L17
* ```
* echo $this->Wizard->buttons(RoomsAppController::WIZARD_PLUGINS_ROOMS);
* ```
* (ワークフローあり)
* ```
* echo $this->Wizard->workflowButtons('Questionnaire.status');
* ```
*
*
* @package NetCommons\NetCommons\View\Helper
*/
class WizardHelper extends AppHelper {
/**
* ヘルパー
*
* @var array
*/
public $helpers = array(
'NetCommons.Button',
'NetCommons.NetCommonsHtml',
'Workflow.Workflow',
);
/**
* Before render callback. beforeRender is called before the view file is rendered.
*
* Overridden in subclasses.
*
* @param string $viewFile The view file that is going to be rendered
* @return void
*/
public function beforeRender($viewFile) {
parent::beforeRender($viewFile);
//ウィザード
if (! isset($this->settings['navibar'])) {
$this->settings['navibar'] = array();
}
if (! isset($this->settings['cancelUrl'])) {
$this->settings['cancelUrl'] = NetCommonsUrl::backToPageUrl(null);
}
}
/**
* ウィザードバー出力
*
* @param string $activeKey アクティブのキー
* @param bool $small 小さいバーフラグ
* @return string HTML出力
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function navibar($activeKey, $small = false) {
$output = '';
$defaultStepWidth = ' style="width: ' . 100 / count($this->settings['navibar']) . '%;"';
if ($small) {
$smallCss = ' small';
} else {
$smallCss = '';
}
$output .= '<div class="progress wizard-steps">';
$count = count($this->settings['navibar']);
$index = 0;
$backLink = true;
foreach ($this->settings['navibar'] as $key => $step) {
$index++;
if (isset($step['width'])) {
$stepWidth = $step['width'];
} else {
$stepWidth = $defaultStepWidth;
}
$badge = '<span class="badge">' . $index . '</span>';
$currentClass = '';
if ($key === $activeKey) {
$currentClass = 'progress-bar ';
$badge = '<span class="btn-primary">' . $badge . '</span>';
$backLink = false;
}
$styleCss = $currentClass . 'wizard-step-item' . $smallCss . ' clearfix';
$output .= '<div class="' . $styleCss . '"' . $stepWidth . '>';
$output .= '<span class="wizard-step-item-title">' . $badge . ' ';
if ($backLink && isset($step['url'])) {
$url = $this->NetCommonsHtml->url($step['url']);
$output .= '<a href="' . $url . '">' . __d($step['label'][0], $step['label'][1]) . '</a>';
} else {
$output .= __d($step['label'][0], $step['label'][1]);
}
$output .= '</span>';
if ($count > $index) {
$output .= '<span class="glyphicon glyphicon-chevron-right pull-right"></span>';
}
$output .= '</div>';
}
$output .= '</div>';
return $output;
}
/**
* URL出力
*
* @param string $activeKey アクティブのキー
* @return string HTML出力
*/
public function naviUrl($activeKey) {
if ($activeKey === 'cancelUrl') {
return $this->settings['cancelUrl'];
} else {
return Hash::get($this->settings['navibar'], $activeKey . '.url');
}
}
/**
* ウィザードボタン
*
* @param string $actKey アクティブのキー
* @param array $cancelOpt キャンセルボタンのオプション
* @param array $prevOpt 前へボタンのオプション
* @param array $nextOpt 次へ、決定ボタンのオプション
* @param array $isBlock ブロックに関するプラグインのウィザードかどうか
* @return string HTML
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function buttons($actKey, $cancelOpt = [], $prevOpt = [], $nextOpt = [], $isBlock = false) {
$output = '';
//ウィザードの状態取得
list($prev, $next) = $this->__wizardStep($actKey);
//キャンセルボタン
if ($isBlock) {
$cancelUrl = NetCommonsUrl::blockUrl(
Hash::get($cancelOpt, 'url', $this->settings['cancelUrl'])
);
} else {
$cancelUrl = NetCommonsUrl::actionUrlAsArray(
Hash::get($cancelOpt, 'url', $this->settings['cancelUrl'])
);
}
$cancelTitle = Hash::get($cancelOpt, 'title', __d('net_commons', 'Cancel'));
$output .= $this->Button->cancel($cancelTitle, $cancelUrl, $cancelOpt);
//前へボタン
if ($prev) {
if ($isBlock) {
$prevUrl = NetCommonsUrl::blockUrl(Hash::get($prevOpt, 'url', $prev['url']));
} else {
$prevUrl = NetCommonsUrl::actionUrlAsArray(Hash::get($prevOpt, 'url', $prev['url']));
}
$prevlTitle = Hash::get($prevOpt, 'title', __d('net_commons', 'BACK'));
$prevOpt['icon'] = Hash::get($prevOpt, 'icon', 'chevron-left');
$output .= $this->Button->cancel($prevlTitle, $prevUrl, $prevOpt);
}
//次へ・決定ボタン
if ($next) {
$nextTitle = Hash::get($nextOpt, 'title', __d('net_commons', 'NEXT'));
$nextOpt['icon'] = Hash::get($nextOpt, 'icon', 'chevron-right');
} else {
$nextTitle = Hash::get($nextOpt, 'title', __d('net_commons', 'OK'));
}
$output .= $this->Button->save($nextTitle, $nextOpt);
return $output;
}
/**
* ウィザードボタン
*
* @param string $statusName ステータスのフィールド名("Modelname.fieldname")
* @param array $cancelUrl キャンセルURL
* @param array $prevUrl 前へURL
* @param array $panel panel-footerを表示するかどうか
* @return string HTML
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function workflowButtons($statusName, $cancelUrl = null, $prevUrl = null, $panel = false) {
$output = '';
$keys = array_keys($this->settings['navibar']);
$activeKey = array_pop($keys);
//ウィザードの状態取得
list($prev, ) = $this->__wizardStep($activeKey);
//キャンセルURL
if (! $cancelUrl) {
$cancelUrl = $this->settings['cancelUrl'];
}
//前へURL
if (! $prevUrl && $prev) {
$prevUrl = NetCommonsUrl::blockUrl($prev['url']);
}
$output .= $this->Workflow->buttons($statusName, $cancelUrl, $panel, $prevUrl, true);
return $output;
}
/**
* ウィザードの状態取得
*
* @param string $activeKey アクティブのキー
* @return array $activeKeyがnullの場合、$nextは最後の
*/
private function __wizardStep($activeKey) {
//ウィザードの状態取得
$current = null;
$prev = null;
$next = null;
$step = null;
foreach ($this->settings['navibar'] as $key => $navibar) {
if ($current) {
$next = $step;
break;
}
if ($key === $activeKey) {
if ($step) {
$prev = $step;
}
$current = $navibar;
}
$step = $navibar;
}
return array($prev, $next);
}
}