-
Notifications
You must be signed in to change notification settings - Fork 2
/
TopicsHelper.php
452 lines (408 loc) · 13.4 KB
/
TopicsHelper.php
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?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('Topic', 'Topics.Model');
App::uses('Block', 'Blocks.Model');
App::uses('WorkflowComponent', 'Workflow.Controller/Component');
/**
* Workflow Helper
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Workflow\View\Helper
*/
class TopicsHelper extends AppHelper {
/**
* Other helpers used by FormHelper
*
* @var array
*/
public $helpers = array(
'NetCommons.NetCommonsForm',
'NetCommons.NetCommonsHtml',
'Rooms.RoomsForm',
'PluginManager.PluginsForm',
'Users.DisplayUser',
'Workflow.Workflow',
);
/**
* ステータス配列
*
* __constractorでセットする
*
* @var array
*/
public $statuses = array();
/**
* Default Constructor
*
* @param View $View The View this helper is being attached to.
* @param array $settings Configuration settings for the helper.
*/
public function __construct(View $View, $settings = array()) {
parent::__construct($View, $settings);
$this->statuses = array(
//一時保存
WorkflowComponent::STATUS_IN_DRAFT => array(
'key' => WorkflowComponent::STATUS_IN_DRAFT,
'class' => 'label-info',
'message' => __d('net_commons', 'Temporary'),
),
//承認待ち
WorkflowComponent::STATUS_APPROVAL_WAITING => array(
'key' => WorkflowComponent::STATUS_APPROVAL_WAITING,
'class' => 'label-warning',
'message' => __d('net_commons', 'Approving'),
),
//差し戻し
WorkflowComponent::STATUS_DISAPPROVED => array(
'key' => WorkflowComponent::STATUS_DISAPPROVED,
'class' => 'label-warning',
'message' => __d('net_commons', 'Disapproving'),
),
//公開前
Topic::STATUS_BEFORE_PUBLISH => array(
'key' => Topic::STATUS_BEFORE_PUBLISH,
'class' => 'label-default',
'message' => __d('topics', 'Before publishing'),
),
//受付終了
Topic::STATUS_ANSWER_END => array(
'key' => Topic::STATUS_ANSWER_END,
'class' => 'label-default',
'message' => __d('topics', 'Answer end'),
),
//回答済み
Topic::STATUS_ANSWERED => array(
'key' => Topic::STATUS_ANSWERED,
'class' => 'label-default',
'message' => __d('topics', 'Answered'),
),
//未回答
Topic::STATUS_UNANSWERED => array(
'key' => Topic::STATUS_UNANSWERED,
'class' => 'label-success',
'message' => __d('topics', 'Unanswered'),
),
//未確認
Topic::STATUS_UNCONFIRMED => array(
'key' => Topic::STATUS_UNCONFIRMED,
'class' => 'label-primary',
'message' => __d('topics', 'Unconfirmed'),
),
//ブロック非公開
Topic::STATUS_BLOCK_PRIVATE => array(
'key' => Topic::STATUS_BLOCK_PRIVATE,
'class' => 'label-default',
'message' => __d('blocks', 'Private'),
),
//ブロック期限付き公開(公開前)
Topic::STATUS_BLOCK_BEFORE_PUBLISH => array(
'key' => Topic::STATUS_BLOCK_BEFORE_PUBLISH,
'class' => 'label-default',
'message' => __d('blocks', 'Public before'),
),
//ブロック期限付き公開(公開中)
Topic::STATUS_BLOCK_PUBLISH => array(
'key' => Topic::STATUS_BLOCK_PUBLISH,
'class' => 'label-default',
'message' => __d('blocks', 'Limited'),
),
//ブロック期限付き公開(公開終了)
Topic::STATUS_BLOCK_END_PUBLISH => array(
'key' => Topic::STATUS_BLOCK_END_PUBLISH,
'class' => 'label-default',
'message' => __d('blocks', 'Public end'),
),
);
}
/**
* 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('/topics/css/style.css');
$this->NetCommonsHtml->script('/topics/js/topics.js');
parent::beforeRender($viewFile);
}
/**
* Camelize処理
*
* @param array $orig 変換元データ
* @return array 変換後データ
*/
public function camelizeKeyRecursive($orig) {
$newResult = [];
foreach ($orig as $key => $value) {
if (isset($value['TrackableCreator'])) {
$avatar = $this->DisplayUser->avatar($value, [], 'TrackableCreator.id');
$value['TrackableCreator']['avatar'] = $avatar;
}
if (isset($value['TrackableUpdater'])) {
$avatar = $this->DisplayUser->avatar($value, [], 'TrackableUpdater.id');
$value['TrackableUpdater']['avatar'] = $avatar;
}
$newResult[$key] = $this->__camelizeKeyRecursive($value);
$displayStatus = $this->__getStatusLabel($newResult[$key]);
$newResult[$key]['Topic']['display_status'] = $displayStatus;
}
return $newResult;
}
/**
* Camelize処理
*
* @param array $orig 変換元データ
* @return array 変換後データ
*/
private function __camelizeKeyRecursive($orig) {
$newResult = [];
foreach ($orig as $key => $value) {
if (is_array($value)) {
$newResult[$key] = $this->__camelizeKeyRecursive($value);
} else {
$newResult = $this->__parseValueForCamelize($newResult, $key, $value);
}
}
return $newResult;
}
/**
* Hash::getを独自でやる(パフォーマンス向上のため)
*
* @param array $data データ
* @param string $key キー文字列(model.fieldの形)
* @param mixed $defualt デフォルト値
* @return mixed
*/
private function __hashGet($data, $key, $defualt = null) {
list($model, $field) = explode('.', $key);
if (isset($data[$model][$field])) {
return $data[$model][$field];
} else {
return $defualt;
}
}
/**
* keyによる変換処理
*
* self::camelizeKeyRecursiveから実行される
*
* @param array $newResult keyをcamel形式に変換して戻す配列
* @param string $key key値
* @param string $value 値
* @return string 変換後の値
*/
private function __parseValueForCamelize($newResult, $key, $value) {
if ($key === 'title') {
$newResult[$key] = $value;
$key = 'display_' . $key;
$newResult[$key] = mb_strimwidth($value, 0, Topic::DISPLAY_TITLE_LENGTH, '...');
} elseif ($key === 'name') {
$newResult[$key] = $value;
$key = 'display_' . $key;
$newResult[$key] = mb_strimwidth($value, 0, Topic::DISPLAY_ROOM_NAME_LENGTH, '...');
} elseif ($key === 'summary') {
$newResult[$key] = $value;
$key = 'display_' . $key;
$newResult[$key] = mb_strimwidth($value, 0, Topic::DISPLAY_SUMMARY_LENGTH, '...');
} elseif (in_array($key, ['publish_start', 'created', 'modified'], true)) {
$newResult[$key] = $value;
$key = 'display_' . $key;
$newResult[$key] = $this->NetCommonsHtml->dateFormat($value);
} elseif ($key === 'title_icon') {
$newResult[$key] = $this->NetCommonsHtml->titleIcon($value);
} else {
$newResult[$key] = $value;
}
return $newResult;
}
/**
* keyによる変換処理
* ※self::camelizeKeyRecursiveから実行される
*
* PHPMDでエラーになるが、別関数にすると見づらくなるため、PHPMD.CyclomaticComplexityで除外する
*
* @param array $newResult keyをcamel形式に変換して戻す配列
* @return string 変換後の値
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function __getStatusLabel($newResult) {
$topicStatus = $this->__hashGet($newResult, 'Topic.status');
$blockPublicType = $this->__hashGet($newResult, 'Block.public_type');
$topicPublishStart = $this->__hashGet($newResult, 'Topic.publish_start');
$topicPublishEnd = $this->__hashGet($newResult, 'Topic.publish_end');
$answerPeriodEnd = $this->__hashGet($newResult, 'Topic.answer_period_end');
$labels = $this->statuses;
$now = gmdate('Y-m-d H:i:s');
if (in_array((int)$topicStatus, array_keys($labels), true)) {
//承認待ち、差し戻し、一時保存
} elseif ($now < $topicPublishStart) {
//公開前
$topicStatus = Topic::STATUS_BEFORE_PUBLISH;
} elseif ($this->__hashGet($newResult, 'Topic.is_answer')) {
if ($topicPublishEnd && $topicPublishEnd < $now) {
//公開終了
$topicStatus = Topic::STATUS_BLOCK_END_PUBLISH;
} elseif ($this->__hashGet($newResult, 'TopicUserStatus.answered')) {
//回答済み
$topicStatus = Topic::STATUS_ANSWERED;
} elseif ($answerPeriodEnd && $answerPeriodEnd < $now) {
//受付終了
$topicStatus = Topic::STATUS_ANSWER_END;
} elseif ($this->__hashGet($newResult, 'TopicUserStatus.read')) {
//未回答
$topicStatus = Topic::STATUS_UNANSWERED;
} elseif (Current::read('User.id')) {
//未確認
$topicStatus = Topic::STATUS_UNCONFIRMED;
}
} elseif ($blockPublicType === Block::TYPE_PRIVATE) {
//ブロック非公開
$topicStatus = Topic::STATUS_BLOCK_PRIVATE;
} elseif ($blockPublicType === Block::TYPE_LIMITED) {
if ($now <= $this->__hashGet($newResult, 'Block.publish_start', '0000-00-00 00:00:00')) {
//ブロック期限付き公開(公開前)
$topicStatus = Topic::STATUS_BLOCK_BEFORE_PUBLISH;
} elseif ($now > $this->__hashGet($newResult, 'Block.publish_end', '9999-99-99 99:99:99')) {
//ブロック期限付き公開(公開終了)
$topicStatus = Topic::STATUS_BLOCK_END_PUBLISH;
} else {
//ブロック期限付き公開(公開中)
$topicStatus = Topic::STATUS_BLOCK_PUBLISH;
}
} else {
//公開中
}
return $this->Workflow->label($topicStatus, $labels);
}
/**
* ヘルプの表示
*
* @param string $content メッセージ内容(オリジナルタグの内容)
* @param string $placement ポジション
* @return string ヘルプHTML出力
*/
public function rssSettingHelp($content = '', $placement = 'bottom') {
$html = '';
$content = __d('topics', 'Each of the embedded keywords, will be sent is converted ' .
'to the corresponding content. <br />') . $content;
$html .= __d('topics', 'Can use an embedded keyword in the channel title line and summary') . ' ';
$html .= '<a href="" data-toggle="popover" data-placement="' . $placement . '"' .
' title="' . __d('topics', 'Embedded keyword?') . '"' . ' data-content="' . $content . '">';
$html .= '<span class="glyphicon glyphicon-info-sign"></span>';
$html .= '</a>';
$html .= '<script type="text/javascript">' .
'$(function () { $(\'[data-toggle="popover"]\').popover({html: true}) });</script>';
return $html;
}
/**
* ステータスの絞り込み
*
* @return string HTML出力
*/
public function dropdownStatus() {
$named = $this->_View->Paginator->params['named'];
$named['page'] = '1';
$named['limit'] = null;
$options = [0 => __d('net_commons', 'All Statuses')];
foreach ($this->statuses as $status) {
$options[$status['key']] = $status['message'];
}
return $this->_View->element('Topics.Topics/select_status', array(
'options' => $options,
'current' => isset($named['status'])
? $named['status']
: 0,
'url' => $named,
));
}
/**
* 特定のブロックを表示する選択ボックス群の初期処理
*
* @return string HTML出力
*/
public function initSelectBlock() {
$selectBlocks = NetCommonsAppController::camelizeKeyRecursive(
$this->_View->viewVars['selectBlocks']
);
$topicFramesBlock['topicFramesBlock'] = NetCommonsAppController::camelizeKeyRecursive(
$this->_View->request->data['TopicFramesBlock']
);
$html = 'initBlocks(' .
h(json_encode($selectBlocks, true)) . ', ' .
h(json_encode($topicFramesBlock, true)) .
')';
return $html;
}
/**
* 特定のブロックを表示する選択ボックス群の初期処理
*
* @return string HTML出力
*/
public function selectBlock() {
$html = '';
if ($this->_View->viewVars['selectBlocks']) {
$html .= '<div class="form-inline form-group">';
//ルーム選択
$html .= $this->RoomsForm->selectRooms('TopicFramesBlock.room_id',
array(
'ng-model' => 'topicFramesBlock.roomId',
//'ng-init' => 'selectBlockPluginKey = \'' . Hash::get($first, '0') . '\'',
'ng-click' => 'blockOptions = optionBlocks()',
)
);
//プラグイン選択
$options = [];
foreach ($this->_View->viewVars['pluginsRoom'] as $pluginsRoom) {
if (! in_array($pluginsRoom['Plugin']['key'], TopicFrameSetting::$outPlugins)) {
$options[$pluginsRoom['Plugin']['key']] = $pluginsRoom['Plugin']['name'];
}
}
$html .= $this->PluginsForm->selectPluginsRoom('TopicFramesBlock.plugin_key',
array(
'div' => false,
'label' => false,
'options' => $options,
'ng-model' => 'topicFramesBlock.pluginKey',
//'ng-init' => 'selectBlockPluginKey = \'' . Hash::get($first, '0') . '\'',
'ng-click' => 'blockOptions = optionBlocks()',
)
);
$html .= '</div>';
}
//ブロック選択
$html .= '<div class="form-group" ng-init="blockOptions = optionBlocks()">';
if ($this->_View->viewVars['selectBlocks']) {
$selectOptions = [];
foreach ($this->_View->viewVars['selectBlocks'] as $selectBlock) {
foreach ($selectBlock as $key => $item) {
$selectOptions[$key] = $item['name'];
}
}
$html .= $this->NetCommonsForm->select('TopicFramesBlock.block_key',
$selectOptions,
array(
'size' => 10, 'class' => 'form-control', 'empty' => false,
'ng-options' => 'item as item.name for item in blockOptions track by item.key',
'ng-model' => 'topicFramesBlock.blockKey',
'ng-show' => 'blockOptions',
)
);
}
$html .= '<div ng-hide="blockOptions">';
$html .= __d('topics', 'No block found.');
$html .= '</div>';
$html .= '</div>';
return $html;
}
}