-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGroupsUserBehavior.php
More file actions
executable file
·62 lines (55 loc) · 1.81 KB
/
GroupsUserBehavior.php
File metadata and controls
executable file
·62 lines (55 loc) · 1.81 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
<?php
/**
* GroupsUser 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');
/**
* GroupsUser Behavior
*
* 選択したユーザを登録
*
* @author Masaki Goto <go8ogle@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
class GroupsUserBehavior 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(
'Group' => 'Groups.Group',
'GroupsUser' => 'Groups.GroupsUser',
));
$model->Group->set($model->Group->data['Group']);
if (! isset($model->data['GroupsUser']['user_id'])) {
$model->data['GroupsUser']['user_id'] = array();
}
$validUsers = array();
foreach ($model->data['GroupsUser']['user_id'] as $userId) {
if (! $model->GroupsUser->isExists($userId)) {
continue;
}
$validUsers[] = $userId;
}
$model->data['GroupsUser']['user_id'] = $validUsers;
$model->GroupsUser->set($model->data['GroupsUser']);
if (! $model->GroupsUser->validates()) {
$model->validationErrors = Hash::merge($model->validationErrors, $model->GroupsUser->validationErrors);
return false;
}
return true;
}
}