-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUserAttributeChoice.php
More file actions
269 lines (242 loc) · 7.08 KB
/
UserAttributeChoice.php
File metadata and controls
269 lines (242 loc) · 7.08 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
<?php
/**
* UserAttributeChoice Model
*
* @property Language $Language
* @property UserAttribute $UserAttribute
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('UsersAppModel', 'Users.Model');
App::uses('DataType', 'DataTypes.Model');
/**
* UserAttributeChoice Model
*/
class UserAttributeChoice extends UsersAppModel {
/**
* 状態のステータス(利用可能)
*
* @var const
*/
const STATUS_KEY_ACTIVE = 'status_1',
STATUS_CODE_ACTIVE = '1';
/**
* 状態のステータス(利用不可)
*
* @var const
*/
const STATUS_KEY_NONACTIVE = 'status_0',
STATUS_CODE_NONACTIVE = '0';
/**
* 状態のステータス(承認待ち)
*
* @var const
*/
const STATUS_KEY_WAITING = 'status_2',
STATUS_CODE_WAITING = '2';
/**
* 状態のステータス(承認済み⇒本人の確認待ち)
*
* @var const
*/
const STATUS_KEY_APPROVED = 'status_3',
STATUS_CODE_APPROVED = '3';
/**
* 言語の自動
*
* @var const
*/
const LANGUAGE_KEY_AUTO = 'auto';
/**
* use behaviors
*
* @var array
*/
public $actsAs = array(
'NetCommons.OriginalKey',
);
/**
* Validation rules
*
* @var array
*/
public $validate = array();
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Language' => array(
'className' => 'M17n.Language',
'foreignKey' => 'language_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'UserAttribute' => array(
'className' => 'UserAttributes.UserAttribute',
'foreignKey' => 'user_attribute_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* Called during validation operations, before validation. Please note that custom
* validation rules can be defined in $validate.
*
* @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/en/models/callback-methods.html#beforevalidate
* @see Model::save()
*/
public function beforeValidate($options = array()) {
$this->validate = Hash::merge($this->validate, array(
'language_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => __d('net_commons', 'Invalid request.'),
'required' => true,
),
),
'user_attribute_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => __d('net_commons', 'Invalid request.'),
'required' => true,
'on' => 'update', // Limit validation to 'create' or 'update' operations
),
),
'name' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => sprintf(
__d('net_commons', 'Please input %s.'), __d('user_attributes', 'Item choice name')
),
'required' => true,
),
),
'key' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => __d('net_commons', 'Invalid request.'),
'required' => true,
'on' => 'update', // Limit validation to 'create' or 'update' operations
),
),
'weight' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => __d('net_commons', 'Invalid request.'),
),
),
));
return parent::beforeValidate($options);
}
/**
* Called before each save operation, after validation. Return a non-true result
* to halt the save.
*
* @param array $options Options passed from Model::save().
* @return bool True if the operation should continue, false if it should abort
* @link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforesave
* @see Model::save()
*/
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['key']) &&
isset($this->data[$this->alias]['code']) && ! $this->data[$this->alias]['code']) {
$this->data[$this->alias]['code'] = $this->data[$this->alias]['key'];
}
return parent::beforeSave($options);
}
/**
* 会員項目の選択肢の登録
* トランザクションは呼び出し元のUserAttribute->saveUserAttribute()で行う。
*
* @param array $data リクエストデータ
* @return bool Trueは成功。Falseはバリデーションエラー
* @throws InternalErrorException
*/
public function saveUserAttributeChoices($data) {
//システム項目、ラジオボタン・チェックボタン・セレクトボックス以外、処理を抜ける
if (! ($data['UserAttributeSetting']['data_type_key'] === DataType::DATA_TYPE_RADIO ||
$data['UserAttributeSetting']['data_type_key'] === DataType::DATA_TYPE_CHECKBOX ||
$data['UserAttributeSetting']['data_type_key'] === DataType::DATA_TYPE_SELECT)
) {
return true;
}
//登録処理
$userAttributeIds = Hash::combine(
$data['UserAttribute'], '{n}.UserAttribute.language_id', '{n}.UserAttribute.id'
);
$useChoiceKeys = array();
foreach ($data['UserAttributeChoice'] as $choiceByWeight) {
$choiceKey = null;
foreach ($choiceByWeight as $languageId => $choice) {
if (! isset($choiceKey)) {
$choiceKey = $choice['key'];
}
$choice['user_attribute_id'] = $userAttributeIds[$languageId];
$choice['key'] = $choiceKey;
$this->create();
if (! $result = $this->save($choice)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$choiceKey = $result['UserAttributeChoice']['key'];
}
$useChoiceKeys[] = $choiceKey;
}
//不要データの削除
$conditions = array(
$this->alias . '.user_attribute_id' => $userAttributeIds,
$this->alias . '.key NOT' => $useChoiceKeys,
);
if (! $this->deleteAll($conditions, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
return true;
}
/**
* リクエストのバリデーション処理
* UserAttributeChoiceとUserAttributeChoiceMapが必要
*
* @param array $data received post data
* @return bool True on success, false on validation errors
* @throws BadRequestException
*/
public function validateRequestData($data) {
$result = array();
if (! is_array(Hash::get($data, 'UserAttributeChoice'))) {
return $result;
}
$weight = 0;
foreach ($data['UserAttributeChoice'] as $choiceByWeight) {
foreach ($choiceByWeight as $langId => $choice) {
if (! Hash::get($choice, 'id')) {
$created = $this->create(array(
'id' => null,
'language_id' => $choice['language_id'],
'user_attribute_id' => $choice['user_attribute_id'],
'weight' => $weight + 1,
'name' => $choice['name'],
));
$result[$weight][$langId] = $created[$this->alias];
} elseif (isset($data['UserAttributeChoiceMap'][$choice['id']])) {
$result[$weight][$langId] = $data['UserAttributeChoiceMap'][$choice['id']];
$result[$weight][$langId]['weight'] = $weight + 1;
$result[$weight][$langId]['name'] = $choice['name'];
} else {
//不正なリクエスト
return false;
}
}
$weight++;
}
return $result;
}
}