-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWorkflowHelper.php
More file actions
322 lines (289 loc) · 9.08 KB
/
WorkflowHelper.php
File metadata and controls
322 lines (289 loc) · 9.08 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/**
* Workflow 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');
App::uses('WorkflowComponent', 'Workflow.Controller/Component');
/**
* Workflow Helper
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Workflow\View\Helper
*/
class WorkflowHelper extends AppHelper {
/**
* Other helpers used by FormHelper
*
* @var array
*/
public $helpers = array(
'Form',
'Html',
'NetCommons.Button',
'NetCommons.LinkButton',
'NetCommons.NetCommonsForm',
'NetCommons.NetCommonsHtml',
);
/**
* 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) {
$this->NetCommonsHtml->css('/workflow/css/style.css');
parent::beforeRender($viewFile);
}
/**
* ステータスのラベル
*
* @param int $status スータス
* @param array $labels Overwrite Status labels
* @return string ラベルHTML
*/
public function label($status, $labels = array()) {
if (empty($labels)) {
$labels = array(
WorkflowComponent::STATUS_IN_DRAFT => array(
'class' => 'label-info',
'message' => __d('net_commons', 'Temporary'),
),
WorkflowComponent::STATUS_APPROVAL_WAITING => array(
'class' => 'label-warning',
'message' => __d('net_commons', 'Approving'),
),
WorkflowComponent::STATUS_DISAPPROVED => array(
'class' => 'label-warning',
'message' => __d('net_commons', 'Disapproving'),
),
);
}
$output = '';
if (isset($labels[$status])) {
$output .= '<span class="workflow-label label ' . $labels[$status]['class'] . '">' .
$labels[$status]['message'] .
'</span>';
}
return $output;
}
/**
* ワークフローのボタン表示
*
* @param string $statusFieldName ステータスのフィールド名("Modelname.fieldname")
* @param string|null $cancelUrl キャンセルURL
* @param bool $panel panel-footerを表示するかどうか
* @param string|null $backUrl 前へのURL
* @return string ボタンHTML
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function buttons($statusFieldName, $cancelUrl = null, $panel = true, $backUrl = null) {
$output = '';
if ($panel) {
$output .= '<div class="panel-footer text-center">';
}
$status = Hash::get($this->_View->request->data, $statusFieldName . '_');
if (! $status) {
$status = Hash::get($this->_View->request->data, $statusFieldName);
}
//変更前のstatusを保持する
$output .= $this->NetCommonsForm->hidden('status_', array('value' => $status));
if (! isset($cancelUrl)) {
$cancelUrl = NetCommonsUrl::backToIndexUrl();
}
$cancelOptions = array(
'ng-class' => '{disabled: sending}',
'ng-click' => 'sending=true',
);
if (Current::permission('content_publishable') &&
$status === WorkflowComponent::STATUS_APPROVAL_WAITING) {
$saveTempOptions = array(
'label' => __d('net_commons', 'Disapproval'),
'class' => 'btn btn-warning' . $this->Button->getButtonSize() . ' btn-workflow',
'name' => 'save_' . WorkflowComponent::STATUS_DISAPPROVED,
'ng-class' => '{disabled: sending}'
);
} else {
$saveTempOptions = array(
'label' => __d('net_commons', 'Save temporally'),
'class' => 'btn btn-info' . $this->Button->getButtonSize() . ' btn-workflow',
'name' => 'save_' . WorkflowComponent::STATUS_IN_DRAFT,
'ng-class' => '{disabled: sending}'
);
}
if (Current::permission('content_publishable')) {
$saveOptions = array(
'label' => __d('net_commons', 'OK'),
'class' => 'btn btn-primary' . $this->Button->getButtonSize() . ' btn-workflow',
'name' => 'save_' . WorkflowComponent::STATUS_PUBLISHED,
'ng-class' => '{disabled: sending}'
);
} else {
$saveOptions = array(
'label' => __d('net_commons', 'OK'),
'class' => 'btn btn-primary' . $this->Button->getButtonSize() . ' btn-workflow',
'name' => 'save_' . WorkflowComponent::STATUS_APPROVAL_WAITING,
'ng-class' => '{disabled: sending}'
);
}
$output .= $this->Button->cancelAndSaveAndSaveTemp(
$cancelUrl, $cancelOptions, $saveTempOptions, $saveOptions, $backUrl
);
if ($panel) {
$output .= '</div>';
}
return $output;
}
/**
* Output workflow input comment
*
* @param string $statusFieldName ステータスのフィールド名
* @param bool $displayBlockKey block_keyを含めるかどうか
* @return string Html
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function inputComment($statusFieldName, $displayBlockKey = true) {
$status = Hash::get($this->_View->data, $statusFieldName);
$output = $this->_View->element('Workflow.form', array(
'contentPublishable' => Current::permission('content_publishable'),
'contentStatus' => $status,
));
if ($displayBlockKey) {
$output .= $this->NetCommonsForm->hidden('Block.key', ['value' => Current::read('Block.key')]);
}
return $output;
}
/**
* Output workflow input comment
*
* @return string Cancel url
*/
public function comments() {
if (! isset($this->_View->viewVars['comments'])) {
$this->_View->viewVars['comments'] = array();
}
return $this->_View->element('Workflow.index', array(
'comments' => $this->_View->viewVars['comments']
));
}
/**
* Check editable permission
*
* @param string $model This should be "Pluginname.Modelname"
* @param array $data Model data
* @return bool True is editable data
*/
public function canEdit($model, $data) {
list($plugin, $model) = pluginSplit($model);
if (! $plugin) {
$plugin = Inflector::pluralize(Inflector::classify($this->request->params['plugin']));
}
${$model} = ClassRegistry::init($plugin . '.' . $model);
return ${$model}->canEditWorkflowContent($data);
}
/**
* Check deletable permission
*
* @param string $model This should be "Pluginname.Modelname"
* @param array $data Model data
* @return bool True is editable data
*/
public function canDelete($model, $data) {
list($plugin, $model) = pluginSplit($model);
if (! $plugin) {
$plugin = Inflector::pluralize(Inflector::classify($this->request->params['plugin']));
}
${$model} = ClassRegistry::init($plugin . '.' . $model);
return ${$model}->canDeleteWorkflowContent($data);
}
/**
* Creates a `<a>` tag for publish link link. The type attribute defaults
*
* @param string $title The button's caption. Not automatically HTML encoded
* @param array $options Array of options and HTML attributes.
* @return string A HTML button tag.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::button
*/
public function publishLinkButton($title = '', $options = array()) {
$output = '';
//ボタンサイズ
$sizeAttr = Hash::get($options, 'iconSize', '');
if ($sizeAttr) {
$sizeAttr = ' ' . $sizeAttr;
}
$options = Hash::remove($options, 'iconSize');
//Linkオプションの設定
$inputOptions = Hash::merge(
array(
'icon' => 'ok',
'name' => 'save_' . WorkflowComponent::STATUS_PUBLISHED,
'class' => 'btn btn-warning' . $sizeAttr
),
$options,
array(
'escapeTitle' => false,
)
);
if (Hash::get($options, 'escapeTitle', true)) {
$title = h($title);
}
//iconの有無
$icon = '<span class="glyphicon glyphicon-' . h($inputOptions['icon']) . '" aria-hidden="true">' .
'</span> ';
unset($options['icon']);
//span tooltipタグの出力
if (isset($options['tooltip']) && $options['tooltip']) {
if (is_string($options['tooltip'])) {
$tooltip = $options['tooltip'];
} else {
$tooltip = __d('net_commons', 'Accept');
}
$output .= '<span class="nc-tooltip" tooltip="' . $tooltip . '">';
unset($inputOptions['tooltip']);
}
$output .= $this->Form->button($icon . $title, $inputOptions);
if (isset($options['tooltip']) && $options['tooltip']) {
$output .= '</span>';
}
return $output;
}
/**
* Creates a `<a>` tag for add link. The type attribute defaults
*
* @param string $title The button's caption. Not automatically HTML encoded
* @param mixed $url Link url
* @param array $options Array of options and HTML attributes.
* @return string A HTML button tag.
*/
public function addLinkButton($title = '', $url = null, $options = array()) {
$output = '';
if (! Current::permission('content_creatable')) {
return $output;
}
//URLの設定
$defaultUrl = array(
'plugin' => $this->_View->request->params['plugin'],
'controller' => $this->_View->request->params['controller'],
);
if (! isset($url)) {
$url = array(
'action' => 'add',
'block_id' => Current::read('Block.id'),
'frame_id' => Current::read('Frame.id'),
);
if (isset($this->_View->viewVars['addActionController'])) {
$url['controller'] = $this->_View->viewVars['addActionController'];
}
}
$url = Hash::merge($defaultUrl, $url);
$output = $this->LinkButton->add($title, $url, $options);
return $output;
}
}