-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMailQueueUser.php
More file actions
393 lines (357 loc) · 12 KB
/
MailQueueUser.php
File metadata and controls
393 lines (357 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
<?php
/**
* MailQueueUser Model
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('MailsAppModel', 'Mails.Model');
App::uses('MailSettingFixedPhrase', 'Mails.Model');
App::uses('WorkflowComponent', 'Workflow.Controller/Component');
App::uses('ComponentCollection', 'Controller');
App::uses('DefaultRolePermission', 'Roles.Model');
/**
* メールキュー送信先
*
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @package NetCommons\Mails\Model
*/
class MailQueueUser extends MailsAppModel {
/**
* Validation rules
*
* @var array
*/
public $validate = array();
/**
* beforeValidate
*
* @param array $options Options passed from Model::save().
* @return bool True if validate operation should continue, false to abort
* @link http://book.cakephp.org/2.0/ja/models/callback-methods.html#beforevalidate
* @see Model::save()
*/
public function beforeValidate($options = array()) {
$this->validate = ValidateMerge::merge($this->validate, array(
'plugin_key' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => __d('net_commons', 'Invalid request.'),
'required' => true,
),
),
//'block_key' => array(
// 'notBlank' => array(
// 'rule' => array('notBlank'),
// 'message' => __d('net_commons', 'Invalid request.'),
// 'required' => true,
// ),
//),
'mail_queue_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => __d('net_commons', 'Invalid request.'),
'required' => true,
),
),
'user_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => __d('net_commons', 'Invalid request.'),
'allowEmpty' => true,
),
),
'room_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => __d('net_commons', 'Invalid request.'),
'allowEmpty' => true,
),
),
'to_address' => array(
'email' => array(
'rule' => array('email', false, null),
'message' => __d('net_commons', 'Invalid request.'),
'allowEmpty' => true,
),
),
));
return parent::beforeValidate($options);
}
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'MailQueue' => array(
'className' => 'Mails.MailQueue',
'foreignKey' => 'mail_queue_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'Users.User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Room' => array(
'className' => 'Rooms.Room',
'foreignKey' => 'room_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* キューの配信先データ登録(複数)
*
* @param array $mailQueueUser received post data
* @param string $filed 一部だけ変更するフィールド
* @param array $values 一部だけ変更するフィールドの値(配列)
* @return void
* @throws InternalErrorException
*/
public function addMailQueueUsers($mailQueueUser, $filed, $values) {
foreach ($values as $value) {
$mailQueueUser['MailQueueUser'][$filed] = $value;
// 新規登録
$mailQueueUser = $this->create($mailQueueUser);
if (! self::saveMailQueueUser($mailQueueUser)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
}
}
/**
* キューの配信先データ保存 - (誰に)
*
* セットするパターンが3つ。いずれかをセットする
* ・room_id + ロール(block_role_permission) : 複数人パターン
* ・user_id : 個別パターン1。パスワード再発行等 (NCにいる人イメージ)
* ・to_address : 個別パターン2。メールアドレスのみで通知する (NCにいない人イメージ)
*
* @param array $data received post data
* @return mixed On success Model::$data if its not empty or true, false on failure
* @throws InternalErrorException
*/
public function saveMailQueueUser($data) {
//トランザクションBegin
$this->begin();
//バリデーション
$this->set($data);
if (! $this->validates()) {
$this->rollback();
return false;
}
try {
// 保存
if (! $mailQueueUser = $this->save(null, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//トランザクションCommit
$this->commit();
} catch (Exception $ex) {
//トランザクションRollback
$this->rollback($ex);
}
return $mailQueueUser;
}
/**
* ルーム内で該当パーミッションありのユーザ ゲット
* - MailQueueUser に 承認者達をsaveするために必要
*
* @param string $permissionKey パーミッションキー
* @param string $roomId ルームID
* @return array
*/
public function getRolesRoomsUsersByPermission($permissionKey, $roomId = null) {
$this->loadModels(array(
'RolesRoomsUser' => 'Rooms.RolesRoomsUser',
));
if ($roomId === null) {
$roomId = Current::read('Room.id');
}
$WorkflowComponent = new WorkflowComponent(new ComponentCollection());
$permissions = $WorkflowComponent->getBlockRolePermissions(array($permissionKey), $roomId);
foreach ($permissions['BlockRolePermissions'][$permissionKey] as $key => $roomRolePermission) {
if (!$roomRolePermission['value']) {
unset($permissions['BlockRolePermissions'][$permissionKey][$key]);
}
}
// $permissions = $WorkflowComponent->getRoomRolePermissions(array($permissionKey),
// DefaultRolePermission::TYPE_ROOM_ROLE,
// $roomId);
// foreach ($permissions['RoomRolePermission'][$permissionKey] as $key => $roomRolePermission) {
// if (!$roomRolePermission['value']) {
// unset($permissions['RoomRolePermission'][$permissionKey][$key]);
// }
// }
$roleKeys = array_keys($permissions['BlockRolePermissions'][$permissionKey]);
//$roleKeys = array_keys($permissions['RoomRolePermission'][$permissionKey]);
$conditions = array(
'Room.id' => $roomId,
'RolesRoom.role_key' => $roleKeys,
);
$rolesRoomsUsers = $this->RolesRoomsUser->getRolesRoomsUsers($conditions);
return $rolesRoomsUsers;
}
/**
* 登録者に配信 登録
*
* @param int $mailQueueId メールキューID
* @param Model $createdUserId 登録ユーザID
* @param string $contentKey 各プラグイン側のコンテンツのキー
* @param string $pluginKey プラグインキー
* @return void
* @throws InternalErrorException
*/
public function addMailQueueUserInCreatedUser($mailQueueId, $createdUserId, $contentKey,
$pluginKey = null) {
if (empty($createdUserId)) {
return;
}
if ($pluginKey === null) {
$pluginKey = Current::read('Plugin.key');
}
$blockKey = Current::read('Block.key');
$mailQueueUser['MailQueueUser'] = array(
'plugin_key' => $pluginKey,
'block_key' => $blockKey,
'content_key' => $contentKey,
'mail_queue_id' => $mailQueueId,
'user_id' => $createdUserId,
'room_id' => null,
'to_address' => null,
'send_room_permission' => null,
'not_send_room_user_ids' => null,
);
// 新規登録
$mailQueueUser = $this->create($mailQueueUser);
if (! self::saveMailQueueUser($mailQueueUser)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
}
/**
* ルーム内のパーミッションを持っているユーザ(例えば承認者達)に配信 登録
*
* @param int $mailQueueId メールキューID
* @param string $contentKey 各プラグイン側のコンテンツキー
* @param string $pluginKey プラグインキー
* @param string $permissionKey パーミッションキー
* @param array $notSendUserIds 送らないユーザID
* @return array ルーム配信で送らないユーザID
* @throws InternalErrorException
*/
public function addMailQueueUserInRoomByPermission($mailQueueId,
$contentKey,
$pluginKey = null,
$permissionKey = 'content_publishable',
$notSendUserIds = array()) {
if ($pluginKey === null) {
$pluginKey = Current::read('Plugin.key');
}
$blockKey = Current::read('Block.key');
$mailQueueUser['MailQueueUser'] = array(
'plugin_key' => $pluginKey,
'block_key' => $blockKey,
'content_key' => $contentKey,
'mail_queue_id' => $mailQueueId,
'user_id' => null,
'room_id' => null,
'to_address' => null,
'send_room_permission' => null,
'not_send_room_user_ids' => null,
);
$notSendRoomUserIds = array();
// 送信者データ取得
$rolesRoomsUsers = self::getRolesRoomsUsersByPermission($permissionKey);
foreach ($rolesRoomsUsers as $rolesRoomsUser) {
// 送らないユーザIDにあれば、登録しない
if (in_array($rolesRoomsUser['RolesRoomsUser']['user_id'], $notSendUserIds)) {
continue;
}
$mailQueueUser['MailQueueUser']['user_id'] = $rolesRoomsUser['RolesRoomsUser']['user_id'];
// 新規登録
$mailQueueUser = $this->create($mailQueueUser);
if (! self::saveMailQueueUser($mailQueueUser)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
// 承認完了時に2通(承認完了とルーム配信)を送らず1通にする対応
// ルーム配信で送らないユーザID を返す
$notSendRoomUserIds[] = $rolesRoomsUser['RolesRoomsUser']['user_id'];
}
// 引数の送らないユーザIDとまとめる
$notSendRoomUserIds = Hash::merge($notSendRoomUserIds, $notSendUserIds);
return $notSendRoomUserIds;
}
/**
* ルーム配信で送るパーミッション ゲット
*
* @param string $typeKey メール定型文の種類
* @return string コンテンツキー
*/
public function getSendRoomPermission($typeKey) {
if ($typeKey == MailSettingFixedPhrase::ANSWER_TYPE) {
// 回答タイプ
return 'mail_answer_receivable';
} else {
// 通常
return 'mail_content_receivable';
}
}
/**
* ルーム配信で、キューの配信先データ登録
*
* @param int $roomId ルームID
* @param array $mailQueueUser received post data
* @param string $sendTime メール送信日時
* @param array $notSendRoomUserIds ルーム配信で送らないユーザID
* @param string $sendRoomPermission ルーム配信で送るパーミッション
* @return void
* @throws InternalErrorException
*/
public function addMailQueueUserInRoom($roomId, $mailQueueUser, $sendTime, $notSendRoomUserIds,
$sendRoomPermission = 'mail_content_receivable') {
// --- ルーム配信
//$roomId = Current::read('Room.id');
$mailQueueUser['MailQueueUser']['room_id'] = $roomId;
$mailQueueUser['MailQueueUser']['send_room_permission'] = $sendRoomPermission;
// ルーム配信で送らないユーザID
$notSendRoomUserIds = $this->__getNotSendRoomUserIds($sendTime, $notSendRoomUserIds);
$mailQueueUser['MailQueueUser']['not_send_room_user_ids'] = $notSendRoomUserIds;
$mailQueueUser = $this->create($mailQueueUser);
/** @see MailQueueUser::saveMailQueueUser() */
if (! $this->saveMailQueueUser($mailQueueUser)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
// --- 追加のユーザ達に配信
// ルームIDをクリア
$mailQueueUser['MailQueueUser']['room_id'] = null;
}
/**
* ルーム配信で送らないユーザID ゲット
*
* @param string $sendTime メール送信日時
* @param array $notSendRoomUserIds ルーム配信で送らないユーザID
* @return string ルーム配信で送らないユーザID
*/
private function __getNotSendRoomUserIds($sendTime, $notSendRoomUserIds) {
// 未来日送信は2通(承認完了とルーム配信)送るため、送らないユーザIDをセットしない
$now = NetCommonsTime::getNowDatetime();
if ($sendTime > $now) {
return null;
}
// 重複登録を排除
$notSendRoomUserIds = array_unique($notSendRoomUserIds);
// 空要素を排除
$notSendRoomUserIds = Hash::filter($notSendRoomUserIds);
$notSendRoomUserIds = implode('|', $notSendRoomUserIds);
return $notSendRoomUserIds;
}
}