-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCircularNoticeTargetUserBehavior.php
More file actions
79 lines (70 loc) · 2.73 KB
/
CircularNoticeTargetUserBehavior.php
File metadata and controls
79 lines (70 loc) · 2.73 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
<?php
/**
* CircularNoticeTargetUser Behavior
*
* @author Masaki Goto <go8ogle@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2016, NetCommons Project
*/
App::uses('ModelBehavior', 'Model');
/**
* CircularNoticeTargetUser Behavior
*
* 選択したユーザを登録
*
* @author Masaki Goto <go8ogle@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
class CircularNoticeTargetUserBehavior extends ModelBehavior {
/**
* beforeValidate is called before a model is validated, you can use this callback to
* add behavior validation rules into a models validate array. Returning false
* will allow you to make the validation fail.
*
* @param Model $model Model using this behavior
* @param array $options Options passed from Model::save().
* @return mixed False or null will abort the operation. Any other result will continue.
* @see Model::save()
*/
public function beforeValidate(Model $model, $options = array()) {
$model->loadModels(array(
'CircularNoticeContent' => 'CircularNotices.CircularNoticeContent',
'CircularNoticeTargetUser' => 'CircularNotices.CircularNoticeTargetUser',
'User' => 'Users.User',
));
if (empty($model->data['CircularNoticeContent']['is_room_target'])) {
// 回覧先ユーザのバリデーション処理
if (! isset($model->data['CircularNoticeTargetUser'])) {
$model->data['CircularNoticeTargetUser'] = array();
}
$model->CircularNoticeTargetUser->set($model->data['CircularNoticeTargetUser']);
// ユーザ選択チェック
$targetUsers = [];
foreach ($model->data['CircularNoticeTargetUser'] as $datum) {
$targetUsers[] = $datum['user_id'];
}
if (! $model->CircularNoticeTargetUser->isUserSelected($targetUsers)) {
$model->CircularNoticeTargetUser->validationErrors['user_id'] =
sprintf(__d('circular_notices', 'Select user'));
$model->validationErrors =
array_merge($model->validationErrors, $model->CircularNoticeTargetUser->validationErrors);
return false;
}
if (! $model->CircularNoticeTargetUser->validates()) {
$model->validationErrors =
array_merge($model->validationErrors, $model->CircularNoticeTargetUser->validationErrors);
return false;
}
if (! $model->User->existsUser($targetUsers)) {
$model->CircularNoticeTargetUser->validationErrors['user_id'][] =
sprintf(__d('net_commons', 'Failed on validation errors. Please check the input data.'));
$model->validationErrors =
array_merge($model->validationErrors, $model->CircularNoticeTargetUser->validationErrors);
return false;
}
}
return true;
}
}