-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTaskCharge.php
More file actions
166 lines (148 loc) · 4.02 KB
/
TaskCharge.php
File metadata and controls
166 lines (148 loc) · 4.02 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
<?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
*/
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データ
* @param bool $isMyUser 作成者ユーザー取得フラグ
* @return array
*/
public function getSelectUsers($taskContent, $isMyUser) {
$this->loadModels(['User' => 'Users.User']);
if ($isMyUser) {
$taskContent['TaskCharge'][] = array('user_id' => Current::read('User.id'));
}
$selectUsers['selectUsers'] = array();
if (isset($taskContent['TaskCharge'])) {
$selectUsers =
Hash::extract($taskContent['TaskCharge'], '{n}.user_id');
foreach ($selectUsers as $userId) {
$user = $this->User->getUser($userId);
$taskContent['selectUsers'][] = $user;
}
}
return $taskContent;
}
/**
* 担当者ユーザのハンドル名を絞り込み選択肢として取得
*
* @param array $taskContents ToDoListデータ
* @return array
*/
public function getSelectChargeUsers($taskContents) {
$this->loadModels(['User' => 'Users.User']);
$selectChargeUsers = array();
// 一覧に表示可能なToDoで担当者として設定されているユーザidを取得(idをkeyに設定し重複を省く)
$chargeUsers = Hash::combine(
$taskContents,
'{n}.TaskContents.{n}.TaskCharge.{n}.user_id',
'{n}.TaskContents.{n}.TaskCharge.{n}.user_id'
);
if ($chargeUsers) {
foreach ($chargeUsers as $userId) {
$user = $this->User->getUser($userId);
if (isset($user['User'])) {
$selectChargeUsers['TaskContents.charge_user_id_' . $user['User']['id']] = array(
'label' => $user['User']['handlename'],
'user_id' => $user['User']['id']
);
}
}
}
return $selectChargeUsers;
}
}