-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTaskContentsController.php
More file actions
434 lines (388 loc) · 12 KB
/
TaskContentsController.php
File metadata and controls
434 lines (388 loc) · 12 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
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
<?php
/**
* TaskContents Controller
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Yuto Kitatsuji <kitatsuji.yuto@wihtone.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('TasksAppController', 'Tasks.Controller');
/**
* TaskContents Controller
*
* @author Yuto Kitatsuji <kitatsuji.yuto@wihtone.co.jp>
* @package NetCommons\Tasks\Controller
* @property TaskContent $TaskContent
* @property TaskCharge $TaskCharge
* @property User $User
* @property Category $Category
* @property ContentCommentsComponent $ContentComments
*/
class TaskContentsController extends TasksAppController {
/**
* use models
*
* @var array
*/
public $uses = array(
'Tasks.TaskContent',
'Tasks.TaskCharge',
'Mails.MailSetting',
);
/**
* use components
*
* @var array
*/
public $components = array(
'NetCommons.Permission' => array(
//アクセスの権限
'allow' => array(
'add,edit,delete' => 'content_creatable',
),
),
'Categories.Categories',
'ContentComments.ContentComments' => array(
'viewVarsKey' => array(
'contentKey' => 'taskContent.TaskContent.key',
'useComment' => 'taskSetting.use_comment'
),
'allow' => array('view')
),
'NetCommons.NetCommonsTime',
);
/**
* beforeFilters
*
* @return void
*/
public function beforeFilter() {
parent::beforeFilter();
}
/**
* use helpers
*
* @var array
*/
public $helpers = array(
'Workflow.Workflow',
'NetCommons.NetCommonsForm',
'NetCommons.TitleIcon',
'ContentComments.ContentComment' => array(
'viewVarsKey' => array(
'contentKey' => 'taskContent.TaskContent.key',
'contentTitleForMail' => 'taskContent.TaskContent.title',
'useComment' => 'taskSetting.use_comment',
'useCommentApproval' => 'taskSetting.use_comment_approval'
)
),
'Categories.Category',
'Users.DisplayUser',
'Users.UserSearch',
'Groups.GroupUserList',
);
/**
* index action
*
* @return void
*/
public function index() {
if (! Current::read('Block.id')) {
$this->autoRender = false;
return;
}
$this->_prepare();
$this->set('listTitle', $this->_taskTitle);
$this->set('categoryLabel', h(__d('tasks', 'No category assignment')));
$conditions = $this->request->params['named'];
if (isset($conditions['category_id'])) {
$category = $this->Category->find('first', [
'recursive' => 0,
'fields' => ['CategoriesLanguage.name'],
'conditions' => ['Category.id' => $conditions['category_id']],
]);
if (! $category) {
return $this->throwBadRequest();
}
$this->set('categoryLabel', $category['CategoriesLanguage']['name']);
}
$this->__list($conditions);
}
/**
* index view
*
* @return void
*
* 速度改善の修正に伴って発生したため抑制
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function view() {
if (! Current::read('Block.id')) {
$this->autoRender = false;
return;
}
$key = $this->params['key'];
$this->_prepare();
$this->set('listTitle', $this->_taskTitle);
$taskContent = $this->TaskContent->getTask($key);
if ($taskContent) {
$this->set('taskContent', $taskContent);
$this->request->data['selectUsers'] = $this->TaskCharge->getSelectUsers($taskContent);
// コメントを利用する
if ($this->_taskSetting['TaskSetting']['use_comment']) {
if ($this->request->is('post')) {
// コメントする
$taskContentKey = $taskContent['TaskContent']['key'];
$useCommentApproval = $this->_taskSetting['TaskSetting']['use_comment_approval'];
if (! $this->ContentComments->comment('tasks', $taskContentKey,
$useCommentApproval)
) {
return $this->throwBadRequest();
}
}
}
// リマインダーメールを設定
$conditions = $this->request->params['named'];
if (isset($conditions['is_makeReminder']) && $conditions['is_makeReminder'] === '1') {
if ($result = $this->TaskContent->setReminderMail($taskContent)) {
$url = NetCommonsUrl::actionUrl(array(
'controller' => 'task_contents',
'action' => 'view',
'frame_id' => Current::read('Frame.id'),
'block_id' => Current::read('Block.id'),
'key' => $result['TaskContent']['key'],
));
return $this->redirect($url);
} else {
// セーブに失敗しているのでBadRequestを返す
return $this->throwBadRequest();
}
}
} else {
// 表示できないToDoへのアクセスならBadRequest
return $this->throwBadRequest();
}
}
/**
* 一覧
*
* @param array $conditions ソート絞り込み条件
* @return void
*/
private function __list($conditions) {
$this->TaskContent->recursive = 0;
$this->TaskContent->Behaviors->load('ContentComments.ContentComment');
$params = array();
$afterOrder = array(
'TaskContent.task_end_date' => 'asc',
'TaskContent.priority' => 'desc',
'TaskContent.progress_rate' => 'desc',
'TaskContent.title' => 'asc',
'TaskContent.modified' => 'desc'
);
$userParam = array();
// カテゴリ絞り込み
if (isset($conditions['category_id'])) {
$params[] = array('TaskContent.category_id' => $conditions['category_id']);
}
// 担当者絞り込み
if (isset($conditions['user_id'])
&& $this->TaskCharge->searchChargeUser($conditions['user_id'])) {
$userParam = array('TaskCharge.user_id' => $conditions['user_id']);
$currentUserId = $conditions['user_id'];
} else {
$currentUserId = 'all';
}
// 完了未完了option取得
$isCompletionOptions = $this->__getSelectOptions('is_completion');
// 完了未完了絞り込み
$currentIsCompletion = '';
if (isset($conditions['is_completion'])) {
if ($conditions['is_completion'] === 'all') {
$currentIsCompletion = $conditions['is_completion'];
} elseif (isset(
$isCompletionOptions['TaskContents.is_completion.' . $conditions['is_completion']]
)) {
$params[] = array('TaskContent.is_completion' => $conditions['is_completion']);
$currentIsCompletion = $conditions['is_completion'];
}
} else {
$params[] = array('TaskContent.is_completion' => TasksComponent::TASK_CONTENT_INCOMPLETE_TASK);
}
// 並べ替えoption取得
$sortOptions = $this->__getSelectOptions('sort');
// 並べ替え絞り込み
$sort = $this->__getSortParam($conditions, $sortOptions);
// order情報を整理
$order = array_merge($sort['order'], $afterOrder);
$params = $this->__setTaskChargeContents($params, $userParam);
// ToDo一覧を取得
$results = $this->TaskContent->getTaskContentList($params, $order);
$taskContents = [];
if (isset($results['tasks'])) {
$taskContents = $results['tasks'];
}
// 期限間近のToDo一覧を取得
$deadLineTasks = [];
if (isset($results['deadLineTasks'])) {
$deadLineTasks = $results['deadLineTasks'];
}
// 期限間近のToDo一覧
$this->set('deadLineTasks', $deadLineTasks);
// ToDo一覧
$this->set('taskContents', $taskContents);
// デフォルトの担当者選択肢を取得
$defaultOptions = $this->__getSelectOptions('default');
$selectChargeUsers = $this->TaskCharge->getSelectChargeUsers($taskContents);
// 担当者絞り込み条件をマージする
$userOptions = array_merge($defaultOptions, $selectChargeUsers);
$this->set('currentUserId', $currentUserId);
$this->set('userOptions', $userOptions);
$this->set('currentIsCompletion', $currentIsCompletion);
$this->set('currentSort', $sort['currentSort']);
$this->TaskContent->Behaviors->unload('ContentComments.ContentComment');
}
/**
* Get Select Options
*
* 絞り込み及びソートのoption取得
*
* @param void $selectTarget 絞り込み及びソート対象名
* @return array selectOptions
*/
private function __getSelectOptions($selectTarget = '') {
$selectOptions = array();
if ($selectTarget === 'default') {
// 自身のユーザーデータを取得
// 担当者絞り込みデフォルト値
$defaultOptions = array(
'TaskContents.charge_user_id_all' => array(
'label' => __d('tasks', 'No person in charge'),
'user_id' => 'all',
),
);
$myUserOptions = array();
$myUser = array(Current::read('User'));
if ($myUser[0]) {
$myUserOptions = array(
'TaskContents.charge_user_id_' . $myUser[0]['id'] => array(
'label' => $myUser[0]['handlename'],
'user_id' => $myUser[0]['id'],
),
);
}
$selectOptions = array_merge($defaultOptions, $myUserOptions);
}
if ($selectTarget === 'is_completion') {
$selectOptions = array(
'TaskContents.is_completion.' . TasksComponent::TASK_CONTENT_INCOMPLETE_TASK => array(
'label' => __d('tasks', 'Incomplete task'),
'is_completion' => TasksComponent::TASK_CONTENT_INCOMPLETE_TASK,
),
'TaskContents.is_completion.' . TasksComponent::TASK_CONTENT_IS_COMPLETION => array(
'label' => __d('tasks', 'Completed task'),
'is_completion' => TasksComponent::TASK_CONTENT_IS_COMPLETION,
),
'TaskContents.is_completion.' . 'all' => array(
'label' => __d('tasks', 'All task'),
'is_completion' => 'all',
),
);
$this->set('isCompletionOptions', $selectOptions);
}
if ($selectTarget === 'sort') {
$selectOptions = array(
'TaskContent.task_end_date.asc' => array(
'label' => __d('tasks', 'Close of the deadline order'),
'sort' => 'TaskContent.task_end_date',
'direction' => 'asc'
),
'TaskContent.title.asc' => array(
'label' => __d('tasks', 'Title order'),
'sort' => 'TaskContent.title',
'direction' => 'asc'
),
'TaskContent.priority.desc' => array(
'label' => __d('tasks', 'Priority order'),
'sort' => 'TaskContent.priority',
'direction' => 'desc'
),
'TaskContent.progress_rate.desc' => array(
'label' => __d('tasks', 'High progress rate order'),
'sort' => 'TaskContent.progress_rate',
'direction' => 'desc'
),
'TaskContent.progress_rate.asc' => array(
'label' => __d('tasks', 'Low progress rate order'),
'sort' => 'TaskContent.progress_rate',
'direction' => 'asc'
),
);
$this->set('sortOptions', $selectOptions);
}
return $selectOptions;
}
/**
* Get Sort Param
*
* 並び替えのorderパラメーターとcurrentSortの値を取得
*
* @param array $conditions POSTされた絞り込み及びソートデータ
* @param array $sortOptions 並び替え選択肢
* @return array sortパラメーター
*/
private function __getSortParam($conditions = array(), $sortOptions = array()) {
$sortPram = '';
$afterOrder = array(
'TaskContent.is_date_set' => 'desc',
'TaskContent.task_end_date is null',
'TaskContent.task_end_date' => 'asc'
);
if (isset($conditions['sort']) && isset($conditions['direction'])) {
$sortPram = $conditions['sort'] . '.' . $conditions['direction'];
}
if (isset($sortOptions[$sortPram])
&& $sortPram !== 'TaskContent.task_end_date.asc'
) {
$order = array($conditions['sort'] => $conditions['direction']);
$order = array_merge($order, $afterOrder);
$currentSort = $conditions['sort'] . '.' . $conditions['direction'];
} else {
$order = $afterOrder;
$currentSort = 'TaskContent.task_end_date.asc';
}
$sort = array(
'order' => $order,
'currentSort' => $currentSort,
);
return $sort;
}
/**
* Get Task Charge Content
*
* 絞り込み条件に担当者IDをセットする
*
* @param array $params 絞り込み条件
* @param array $userParam 担当者絞り込み条件
* @return array
*/
private function __setTaskChargeContents($params, $userParam) {
if ($userParam) {
// 絞り込み条件に指定した担当者データを全て取得
$taskChargeContents = $this->TaskCharge->find('threaded', [
'recursive' => 1,
'conditions' => $userParam
]);
// 担当者として設定されているToDoのcontent_idのみ取得
$taskContentIdArr = [];
foreach ($taskChargeContents as $taskChargeContent) {
$taskContentIdArr[] = $taskChargeContent['TaskCharge']['task_content_id'];
}
// 絞り込み条件に加える
$params[] = array('TaskContent.id' => $taskContentIdArr);
}
return $params;
}
}