-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUserSearchAppModel.php
More file actions
566 lines (505 loc) · 16.7 KB
/
UserSearchAppModel.php
File metadata and controls
566 lines (505 loc) · 16.7 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
<?php
/**
* User Model
*
* @author Noriko Arai <arai@nii.ac.jp>
* @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('NetCommonsTime', 'NetCommons.Utility');
/**
* UserSearchAppModel
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Users\Model
*
* 速度改善の修正に伴って発生したため抑制
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class UserSearchAppModel extends UsersAppModel {
/**
* more_than_days定数
* ○日以上前(○日以上ログインしていない)
*
* @var const
*/
const MORE_THAN_DAYS = 'more_than_days';
/**
* within_days定数
* ○日以内(○日以内ログインしている)
*
* @var const
*/
const WITHIN_DAYS = 'within_days';
/**
* Custom database table name, or null/false if no table association is desired.
*
* @var string
* @link http://book.cakephp.org/2.0/ja/models/model-attributes.html#usetable
*/
public $useTable = false;
/**
* 閲覧可のフィールドセット
* self::__prepare()から実行される
*
* @param string $attrKey 会員項目キー
* @param array $userAttributes 会員項目データ
* @return void
*
* 速度改善の修正に伴って発生したため抑制
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
protected function _setReadableField($attrKey, $userAttributes) {
$dataTypeKey = '';
$userAttr = [];
foreach ($userAttributes as $arr) {
foreach ($arr as $item) {
foreach ($item as $userAttribute) {
if ($userAttribute['UserAttribute']['key'] === $attrKey) {
$dataTypeKey = $userAttribute['UserAttributeSetting']['data_type_key'];
$userAttr = $userAttribute['UserAttribute'];
break;
}
}
}
}
if (empty($userAttr)) {
return;
}
$label = $userAttr['name'];
//Fieldのチェック
if ($dataTypeKey === DataType::DATA_TYPE_IMG) {
$this->readableFields[$attrKey]['field'] =
$this->UploadFile->alias . Inflector::classify($attrKey) . '.field_name';
$this->readableFields[$attrKey]['label'] = $label;
$this->readableFields[$attrKey]['options'] = array(
'0' => __d('user_manager', 'No avatar.'),
'1' => __d('user_manager', 'Has avatar.')
);
$this->readableFields[$attrKey]['data_type'] = $dataTypeKey;
} elseif (in_array($attrKey, UserAttribute::$typeDatetime, true) ||
$dataTypeKey === DataType::DATA_TYPE_DATETIME) {
if (in_array($attrKey, ['last_login', 'previous_login'], true)) {
//最終ログイン日時の場合、ラベル変更(○日以上ログインしていない、○日以内ログインしている)
$moreThanDays =
__d('user_manager', 'Not logged more than <span style="color:#ff0000;">%s</span>days ago');
$withinDays =
__d('user_manager', 'Have logged in within <span style="color:#ff0000;">%s</span>days');
} else {
//○日以上前、○日以内
$moreThanDays =
__d('user_manager', 'more than <span style="color:#ff0000;">%s</span>days ago');
$withinDays =
__d('user_manager', 'within <span style="color:#ff0000;">%s</span>days');
}
//日時型の場合
$this->readableFields[$attrKey]['field'] = $this->alias . '.' . $attrKey;
$this->readableFields[$attrKey]['data_type'] = $dataTypeKey;
$fieldKey = $attrKey . '_' . self::MORE_THAN_DAYS;
$this->readableFields[$fieldKey]['field'] = $this->alias . '.' . $attrKey;
$this->readableFields[$fieldKey]['label'] = $label;
$this->readableFields[$fieldKey]['format'] = $moreThanDays;
$this->readableFields[$fieldKey]['data_type'] = $dataTypeKey;
$fieldKey = $attrKey . '_' . self::WITHIN_DAYS;
$this->readableFields[$fieldKey]['field'] = $this->alias . '.' . $attrKey;
$this->readableFields[$fieldKey]['label'] = $label;
$this->readableFields[$fieldKey]['format'] = $withinDays;
$this->readableFields[$fieldKey]['data_type'] = $dataTypeKey;
} elseif ($this->hasField($attrKey)) {
//Userモデル
$this->readableFields[$attrKey]['field'] = $this->alias . '.' . $attrKey;
$this->readableFields[$attrKey]['label'] = $label;
$this->readableFields[$attrKey]['data_type'] = $dataTypeKey;
} elseif ($this->UsersLanguage->hasField($attrKey)) {
//UsersLanguageモデル
$this->readableFields[$attrKey]['field'] = $this->UsersLanguage->alias . '.' . $attrKey;
$this->readableFields[$attrKey]['label'] = $label;
$this->readableFields[$attrKey]['data_type'] = $dataTypeKey;
}
$userAttrChoices = [];
foreach ($userAttributes as $arr) {
foreach ($arr as $item) {
foreach ($item as $userAttribute) {
if (isset($userAttribute['UserAttributeChoice'])) {
foreach ($userAttribute['UserAttributeChoice'] as $choice) {
if ($choice['user_attribute_id'] == $userAttr['id']) {
$userAttrChoices[$choice['key']] = $choice['name'];
}
}
}
}
}
}
if ($userAttrChoices) {
$this->readableFields[$attrKey]['options'] = $userAttrChoices;
if ($attrKey === 'role_key') {
$this->readableFields[$attrKey]['option_field'] = 'key';
} else {
$this->readableFields[$attrKey]['option_field'] = 'code';
}
}
////Field(is_xxxx_public)のチェック
//$fieldKey = sprintf(UserAttribute::PUBLIC_FIELD_FORMAT, $field);
//if ($this->hasField($fieldKey)) {
// $this->readableFields[$fieldKey] = $this->alias . '.' . $fieldKey;
//}
}
/**
* リクエストキーのパース処理
*
* @param string $requestKey リクエストキー
* @return array array(フィールド名、setting, 符号)
*/
protected function _parseRequestKey($requestKey) {
$setting = null;
$sign = null;
if (preg_match('/' . self::MORE_THAN_DAYS . '$/', $requestKey)) {
$field = substr($requestKey, 0, (strlen(self::MORE_THAN_DAYS) + 1) * -1);
$setting = self::MORE_THAN_DAYS;
} elseif (preg_match('/' . self::WITHIN_DAYS . '$/', $requestKey)) {
$field = substr($requestKey, 0, (strlen(self::WITHIN_DAYS) + 1) * -1);
$setting = self::WITHIN_DAYS;
} elseif (preg_match('/ NOT$/', $requestKey)) {
$field = substr($requestKey, 0, -4);
$sign = ' NOT';
} else {
$field = $requestKey;
}
if (isset($this->convRealToFieldKey[$field])) {
$field = $this->convRealToFieldKey[$field]['key'];
}
return array($field, $setting, $sign);
}
/**
* JOINテーブルを取得
*
* @param array $conditions 条件(Conditions)リスト
* @return array Findで使用するJOIN配列
*/
protected function _getSearchJoinTablesByConditions($conditions) {
$joinModels = array();
$fieldKeys = array_keys($conditions);
if (in_array('group_id', $fieldKeys, true)) {
$joinModels = Hash::merge(array('Group' => true), $joinModels);
}
if (in_array('created_user', $fieldKeys, true)) {
$joinModels = Hash::merge(array('TrackableCreator' => true), $joinModels);
}
if (in_array('modified_user', $fieldKeys, true)) {
$joinModels = Hash::merge(array('TrackableUpdater' => true), $joinModels);
}
foreach ($fieldKeys as $field) {
$modelName = $this->UploadFile->alias . Inflector::classify($field);
if ($this->getOriginalField($field) === $modelName . '.field_name') {
$joinModels = Hash::merge(array($modelName => array(
'table' => $this->UploadFile->table,
'alias' => $modelName,
'type' => 'LEFT',
'conditions' => array(
$modelName . '.content_key' . ' = ' . $this->alias . '.id',
$modelName . '.plugin_key' => 'users',
$modelName . '.field_name' => $field,
),
)), $joinModels);
}
}
return $joinModels;
}
/**
* 検索可能のフィールドをチェックして、検索不可なフィールドは削除する
*
* @param array $fields 表示するフィールドリスト
* @return array 実際に表示できるフィールドリスト
*/
public function cleanSearchFields($fields) {
$fieldKeys = array_keys($fields);
foreach ($fieldKeys as $key) {
list($field, ) = $this->_parseRequestKey($key);
if (! isset($this->readableFields[$field])) {
unset($fields[$key]);
}
}
if (! $fields) {
$fields = array();
}
return $fields;
}
/**
* 検索フィールドから実際のテーブルフィールドを取得する
*
* @param string $field 表示するフィールドリスト
* @return string 実際のフィールド
*/
public function getOriginalField($field) {
return Hash::get($this->readableFields, $field . '.' . 'field');
}
/**
* 検索フィールド名(ラベル)を取得する
*
* @param string $field 表示するフィールド
* @return string フィールド名(ラベル)
*/
public function getReadableFieldName($field) {
return Hash::get($this->readableFields, $field . '.' . 'label');
}
/**
* 検索フィールドのオプションを取得する
*
* @param string $field 表示するフィールド
* @return string オプション
*/
public function getReadableFieldOptions($field) {
return Hash::get($this->readableFields, $field . '.' . 'options');
}
/**
* 検索フィールドのソートキーを取得する
*
* @param string $field 表示するフィールド
* @return string ソートキー
*/
public function getReadableFieldOrderKey($field) {
$key = 'order';
if (! Hash::get($this->readableFields, $field . '.' . $key)) {
$key = 'field';
}
return Hash::get($this->readableFields, $field . '.' . $key);
}
/**
* 検索フィールドの値をフォーマットに当てはめて出力する。
*
* @param string $field 表示するフィールドリスト
* @param string $value 値
* @return string 値
*/
public function getSearchFieldValue($field, $value) {
if (Hash::get($this->readableFields, $field . '.' . 'format')) {
return sprintf(Hash::get($this->readableFields, $field . '.' . 'format'), h($value));
} elseif (Hash::get($this->readableFields, $field . '.' . 'options')) {
$options = Hash::get($this->readableFields, $field . '.' . 'options', array());
if (is_array($value)) {
$ret = '';
foreach ($value as $key) {
$ret .= ',' . ($options[$key] ?? '');
}
return h(mb_substr($ret, 1));
} else {
return h($options[$value] ?? '');
}
} else {
return h($value);
}
}
/**
* フィールド値の会員項目属性データを取得する
*
* @param array $field フィールド
* @return array
*/
protected function _getUserAttribute($field) {
$attributesByLayout = $this->UserAttribute->getUserAttributesForLayout();
$userAttribute = null;
foreach ($attributesByLayout as $userAttributesByRow) {
foreach ($userAttributesByRow as $userAttributesByCol) {
foreach ($userAttributesByCol as $userAttributeCell) {
if ($userAttributeCell['UserAttribute']['key'] === $field) {
$userAttribute = $userAttributeCell;
break;
}
}
}
}
return $userAttribute;
}
/**
* 検索可能のフィールドをチェックして、検索不可なフィールドは削除する
*
* @param array $field フィールド
* @param array $setting セッティングモード(日時型のみ使用)
* @param array $value 値
* @param string $defaultSign デフォルトの符号
* @return array array(符号, SQL値)
*/
protected function _creanSearchCondition($field, $setting, $value, $defaultSign = null) {
$userAttribute = $this->_getUserAttribute($field);
if (is_null($userAttribute)) {
return [$defaultSign, $value];
}
$dataTypeKey = $userAttribute['UserAttributeSetting']['data_type_key'] ?? '';
$forwardTypes = array(
DataType::DATA_TYPE_TEXT, DataType::DATA_TYPE_TEXTAREA, DataType::DATA_TYPE_EMAIL
);
$optionTypes = array(
DataType::DATA_TYPE_RADIO, DataType::DATA_TYPE_SELECT, DataType::DATA_TYPE_CHECKBOX,
DataType::DATA_TYPE_PREFECTURE, DataType::DATA_TYPE_TIMEZONE, DataType::DATA_TYPE_MULTIPLE_SELECT
);
if ($dataTypeKey === DataType::DATA_TYPE_IMG) {
list($sign, $value) =
$this->_makeSearchConditionByImage($value, $defaultSign);
} elseif (in_array($field, UserAttribute::$typeDatetime, true) ||
$dataTypeKey === DataType::DATA_TYPE_DATETIME) {
//日付型の場合
list($sign, $value) =
$this->_makeSearchConditionByDate($value, $setting);
} elseif (in_array($dataTypeKey, $forwardTypes, true) ||
in_array($field, ['created_user', 'modified_user'], true)) {
// テキスト型、テキストエリア型、メールアドレス型、作成者、更新者の場合
// ->あいまい検索※今後、MatchAgainstもしくは、前方一致にする必要あり。
$sign = ' LIKE';
$value = '%' . $value . '%';
} elseif (in_array($dataTypeKey, $optionTypes, true)) {
list($sign, $value) =
$this->_makeSearchConditionByChoice($userAttribute, $field, $value, $defaultSign);
} else {
$sign = $defaultSign;
}
return array($sign, $value);
}
/**
* Imageの検索条件を作成する
*
* @param array $value 値
* @param string $defaultSign デフォルトの符号
* @return array
*/
protected function _makeSearchConditionByImage($value, $defaultSign) {
if ($value) {
$sign = ' NOT';
} else {
$sign = $defaultSign;
}
$value = null;
return [$sign, $value];
}
/**
* Dateの検索条件を作成する
*
* @param array $value 値
* @param string $setting 日付の条件設定
* @return array
*/
protected function _makeSearchConditionByDate($value, $setting) {
//日付型の場合
if ($setting === self::MORE_THAN_DAYS) {
//○日以上前(○日以上ログインしていない)
$sign = ' <=';
} else {
//○日以内(○日以内ログインしている)
$sign = ' >=';
}
$date = new DateTime(NetCommonsTime::getNowDatetime());
$date->sub(new DateInterval(sprintf('P%dD', (int)$value)));
$value = $date->format('Y-m-d H:i:s');
return [$sign, $value];
}
/**
* 選択肢の検索条件を作成する
*
* @param array $userAttribute 会員項目データ
* @param array $field フィールド
* @param array $value 値
* @param string $defaultSign デフォルトの符号
* @return array
*/
protected function _makeSearchConditionByChoice(
$userAttribute, $field, $value, $defaultSign
) {
$options = [];
$choiceFiled = $this->readableFields[$field]['option_field'] ?? 'code';
foreach ($userAttribute['UserAttributeChoice'] as $attributeChoice) {
$options[$attributeChoice['key']] = $attributeChoice[$choiceFiled];
}
if (is_array($value)) {
$retValue = [];
foreach ($value as $k => $v) {
$retValue[$k] = $options[$v] ?? $v;
}
} else {
$retValue = $options[$value] ?? $value;
}
return [$defaultSign, $retValue];
}
/**
* 検索フィールドを取得する
*
* @param array $fields フィールド配列
* @return array 実際に検索できるフィールドリスト
*/
protected function _getSearchFields($fields) {
$originalFields = array(
'User.id'
);
foreach ($fields as $field) {
$originalFields[] = $this->getOriginalField($field);
}
if (in_array('room_role_key', $fields, true)) {
$originalFields = array_merge(
$originalFields,
array(
$this->RolesRoomsUser->alias . '.id',
$this->RolesRoomsUser->alias . '.roles_room_id',
$this->RolesRoomsUser->alias . '.user_id',
$this->RolesRoomsUser->alias . '.room_id',
),
array(
$this->RolesRoom->alias . '.id',
$this->RolesRoom->alias . '.room_id',
$this->RolesRoom->alias . '.role_key',
)
);
}
$originalFields = array_unique($originalFields);
return $originalFields;
}
/**
* 検索フィールドを取得する
*
* @param array $fields フィールド配列
* @return array 実際に検索できるフィールドリスト
*/
protected function _getSearchFieldsByRoomRoleKey($fields) {
$fields = Hash::merge(array(
'user_id',
'role_id',
'roles_room_id',
'roles_room_room_id',
'roles_room_role_key',
'roles_rooms_user_id',
'roles_rooms_user_roles_room_id',
'roles_rooms_user_user_id',
'roles_rooms_user_room_id'
), $fields);
$originalFields = array();
foreach ($fields as $field) {
$originalField = $this->getOriginalField($field);
if ($originalField) {
$originalFields[$field] = $originalField . ' AS ' . $field;
}
}
$originalFields = array_unique($originalFields);
return $originalFields;
}
/**
* 検索取得するためのrolesリスト取得
*
* @param array $extra findのオプション
* @return array 検索取得するためのrolesリスト
*/
protected function _getRolesByRoomRoleKey($extra) {
$roles = array(
Role::ROOM_ROLE_KEY_ROOM_ADMINISTRATOR,
Role::ROOM_ROLE_KEY_CHIEF_EDITOR,
Role::ROOM_ROLE_KEY_EDITOR,
Role::ROOM_ROLE_KEY_GENERAL_USER,
Role::ROOM_ROLE_KEY_VISITOR,
);
if (Hash::get($extra, 'extra.search', false)) {
$roles[] = null;
}
return $roles;
}
}