-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTaskCharge.php
More file actions
183 lines (165 loc) · 4.6 KB
/
TaskCharge.php
File metadata and controls
183 lines (165 loc) · 4.6 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
<?php
/**
* TaskCharge Model
*
* @author Yuto Kitatsuji <kitatsuji.yuto@withone.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
App::uses('TasksAppModel', 'Tasks.Model');
/**
* TaskCharge Model
*
* @author Yuto Kitatsuji <kitatsuji.yuto@withone.co.jp>
* @package NetCommons\Tasks\Model
* @property User $User
*/
class TaskCharge extends TasksAppModel {
/**
* @var int recursiveはデフォルトアソシエーションなしに
*/
public $recursive = -1;
/**
* Validate this model
*
* @return bool
*/
public function validateTaskCharge() {
$this->validates = $this->_getValidateSpecification();
return $this->validationErrors ? false : true;
}
/**
* バリデーションルールを返す
*
* @return array
*/
protected function _getValidateSpecification() {
$validate = array(
'task_content_id' => array(
'naturalNumber' => array(
'rule' => array('naturalNumber'),
'message' => __d('net_commons', 'Invalid request.'),
),
),
'user_id' => array(
'naturalNumber' => array(
'rule' => array('naturalNumber'),
'message' => __d('net_commons', 'Invalid request.'),
),
),
);
return $validate;
}
/**
* 担当者に設定したユーザーの存在確認
*
* @param int $userId ユーザーID
* @return bool
* @throws InternalErrorException
*/
public function searchChargeUser($userId) {
// 必要なモデル読み込み
$this->loadModels([
'User' => 'Users.User',
]);
if (! $this->User->findById($userId)) {
return false;
}
return true;
}
/**
* ToDoの担当者を登録
*
* @param array $data 登録データ
* @return bool
* @throws InternalErrorException
*/
public function setCharges($data) {
$taskId = $data['TaskContent']['id'];
// すべてDelete
if (! $this->deleteAll(array('TaskCharge.task_content_id' => $taskId), false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
// 1件ずつ保存
if (isset($data['TaskCharges']) && count($data['TaskCharges']) > 0) {
foreach ($data['TaskCharges'] as $charge) {
$charge['TaskCharge']['task_content_id'] = $taskId;
if (! $this->validateTaskCharge($charge)) {
return false;
}
$this->create($charge);
if (! $this->save(null, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
}
}
return true;
}
/**
* 担当者情報を取得する
*
* @param array $taskContent Todo情報
* @return array
*/
public function getSelectUsers($taskContent) {
$this->loadModels(['User' => 'Users.User']);
$setSelectUsers = [];
if (isset($taskContent['TaskCharge'])) {
$selectUserIdArr = [];
foreach ($taskContent['TaskCharge'] as $item) {
$selectUserIdArr[] = $item['user_id'];
}
// 不要なイベントを発生させないためにBehaviorを除去
$this->User->Behaviors->unload('Files.Attachment');
$setSelectUsers = $this->User->find('all', [
'recursive' => -1,
'fields' => ['User.id', 'User.handlename'],
'conditions' => [
$this->User->alias . '.id' => $selectUserIdArr
],
]);
}
return $setSelectUsers;
}
/**
* 担当者ユーザのハンドル名を絞り込み選択肢として取得
*
* @param array $taskContents ToDoListデータ
* @return array
*/
public function getSelectChargeUsers($taskContents) {
$this->loadModels(['User' => 'Users.User']);
$selectChargeUsers = [];
// 一覧に表示可能なToDoで担当者として設定されているユーザidを取得(idをkeyに設定し重複を省く)
$chargeUserIdArr = [];
foreach ($taskContents as $taskContent) {
foreach ($taskContent['TaskContents'] as $item) {
if (isset($item['TaskCharge'])) {
$tmpIdArr = array_keys($item['TaskCharge']);
$chargeUserIdArr = array_merge($chargeUserIdArr, $tmpIdArr);
}
}
}
$chargeUserIdArr = array_unique($chargeUserIdArr);
if (! empty($chargeUserIdArr)) {
// 不要なイベントを発生させないためにBehaviorを除去
$this->User->Behaviors->unload('Files.Attachment');
$chargeUsers = $this->User->find('all', [
'recursive' => -1,
'fields' => ['User.id', 'User.handlename'],
'conditions' => [
$this->User->alias . '.id' => $chargeUserIdArr
],
]);
foreach ($chargeUsers as $chargeUser) {
if (isset($chargeUser['User'])) {
$selectChargeUsers['TaskContents.charge_user_id_' . $chargeUser['User']['id']] = [
'label' => $chargeUser['User']['handlename'],
'user_id' => $chargeUser['User']['id']
];
}
}
}
return $selectChargeUsers;
}
}