-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUserSearchFormHelper.php
More file actions
500 lines (431 loc) · 14.2 KB
/
UserSearchFormHelper.php
File metadata and controls
500 lines (431 loc) · 14.2 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
<?php
/**
* UserSearchForm Helper
*
* @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('AppHelper', 'View/Helper');
/**
* UserSearchForm Helper
*
* @property FormHelper $Form
* @property ButtonHelper $Button
* @property NetCommonsFormHelper $NetCommonsForm
* @property NetCommonsHtmlHelper $NetCommonsHtml
*
* @package NetCommons\Users\View\Helper
*/
class UserSearchFormHelper extends AppHelper {
/**
* Other helpers used by FormHelper
*
* @var array
*/
public $helpers = array(
'Form',
'NetCommons.Button',
'NetCommons.NetCommonsForm',
'NetCommons.NetCommonsHtml',
);
/**
* 会員検索の入力フォームHTMLを生成する
*
* @param array $userAttribute ユーザ項目属性データ
* @return string inputのHTML
*/
public function userSearchInput($userAttribute) {
$html = '';
$dataTypeKey = $userAttribute['UserAttributeSetting']['data_type_key'];
//以下の場合、条件のinputを表示させない
// * パスワードは項目表示しない
// * 前回ログイン日時は項目表示しない
// * 他人の項目が読めない && 他人の項目が編集できない
if ($dataTypeKey === DataType::DATA_TYPE_PASSWORD ||
$userAttribute['UserAttribute']['key'] === UserAttribute::PREVIOUS_LOGIN_FIELD ||
! $userAttribute['UserAttributesRole']['other_readable'] &&
! $userAttribute['UserAttributesRole']['other_editable']) {
return $html;
}
if (in_array($userAttribute['UserAttribute']['key'], UserAttribute::$typeDatetime, true)) {
$dataTypeKey = DataType::DATA_TYPE_DATETIME;
}
$html .= $this->__input($dataTypeKey, $userAttribute);
return $html;
}
/**
* 会員検索の入力フォームHTMLを生成する
*
* @param string $dataTypeKey inputタイプ
* @param array $userAttribute ユーザ項目属性データ
* @return string 入力フォームHTML
*/
private function __input($dataTypeKey, $userAttribute) {
$html = '';
$options = null;
$choiceInArray = array(
DataType::DATA_TYPE_RADIO,
DataType::DATA_TYPE_CHECKBOX,
DataType::DATA_TYPE_SELECT,
);
if ($dataTypeKey === DataType::DATA_TYPE_IMG) {
//あり、なし、指定なしのラジオボタン
$dataTypeKey = DataType::DATA_TYPE_RADIO;
$options = array(
'0' => __d('user_manager', 'No avatar.'),
'1' => __d('user_manager', 'Has avatar.')
);
} elseif (in_array($dataTypeKey, $choiceInArray, true)) {
$keyPath = '{n}.key';
//ラジオボタン、チェックボタン、セレクトボタン
$options = Hash::combine(
$userAttribute, 'UserAttributeChoice.' . $keyPath, 'UserAttributeChoice.{n}.name'
);
}
$html .= '<div class="form-group row user-search-conditions-row">';
//ラベル
$html .= '<div class="col-xs-3">';
$html .= '<label class="control-label">' .
h($userAttribute['UserAttribute']['name']) .
'</label>';
$html .= '</div>';
switch ($dataTypeKey) {
case DataType::DATA_TYPE_RADIO:
$html .= $this->__inputRadio($dataTypeKey, $userAttribute, $options);
break;
case DataType::DATA_TYPE_CHECKBOX:
$html .= $this->__inputCheckbox($dataTypeKey, $userAttribute, $options);
break;
case DataType::DATA_TYPE_SELECT:
$html .= $this->__inputSelect($dataTypeKey, $userAttribute, $options);
break;
case DataType::DATA_TYPE_DATETIME:
$html .= $this->__inputDatetime($dataTypeKey, $userAttribute);
break;
default:
$html .= $this->__inputText($dataTypeKey, $userAttribute);
}
$html .= '</div>';
return $html;
}
/**
* 会員検索の入力フォームHTMLを生成する
*
* @param string $dataTypeKey inputタイプ
* @param array $userAttribute ユーザ項目属性データ
* @param array $options オプションデータ(radio, checkbox, select)
* @return string 入力フォームHTML
*/
private function __inputRadio($dataTypeKey, $userAttribute, $options) {
$html = '';
$options = array('' => __d('net_commons', 'Not condition specified')) + $options;
$input = $this->NetCommonsForm->input($userAttribute['UserAttribute']['key'], array(
'type' => 'radio',
'label' => false,
'options' => $options,
'hiddenField' => false,
'error' => false,
'inline' => true,
'div' => false,
'default' => ''
));
$html .= $this->NetCommonsHtml->div(null, $input, array('class' => 'col-xs-9'));
return $html;
}
/**
* 会員検索の入力フォームHTMLを生成する
* 後でやる
*
* @param string $dataTypeKey inputタイプ
* @param array $userAttribute ユーザ項目属性データ
* @param array $options オプションデータ(radio, checkbox, select)
* @return string 入力フォームHTML
*/
private function __inputCheckbox($dataTypeKey, $userAttribute, $options) {
$html = '';
$html .= '<div class="user-search-conditions-checkbox col-xs-9">';
$index = 0;
$inputOptions = [
'hiddenField' => false,
'type' => 'checkbox',
'error' => false,
'legend' => false,
'label' => false,
'inline' => true,
];
$default =
$this->_View->request->data['UserSearch'][$userAttribute['UserAttribute']['key']] ?? [];
foreach ($options as $key => $label) {
$domId = $this->domId($userAttribute['UserAttribute']['key'] . $index);
$inputOptions['id'] = $domId;
$inputOptions['value'] = $key;
$inputOptions['checked'] = in_array($key, $default, true);
$html .= '<div class="checkbox checkbox-inline">';
$html .= '<label class="control-label" for="' . $domId . '">';
$html .= $this->Form->checkbox(
$userAttribute['UserAttribute']['key'] . '[' . $index . ']',
$inputOptions
);
$html .= h($label);
$html .= '</label>';
$html .= '</div>';
$index++;
}
$html .= '</div>';
return $html;
}
/**
* 会員検索の入力フォームHTMLを生成する
*
* @param string $dataTypeKey inputタイプ
* @param array $userAttribute ユーザ項目属性データ
* @param array $options オプションデータ(radio, checkbox, select)
* @return string 入力フォームHTML
*/
private function __inputSelect($dataTypeKey, $userAttribute, $options) {
$html = '';
//入力部品
if ($options) {
$options = array('' => __d('net_commons', '-- Not specified --')) + $options;
}
$html .= $this->NetCommonsForm->input($userAttribute['UserAttribute']['key'], array(
'type' => 'select',
'options' => $options,
'label' => false,
'div' => array('class' => 'col-xs-9'),
'error' => false,
'class' => 'form-control input-sm',
));
return $html;
}
/**
* 会員検索の入力フォームHTMLを生成する
*
* @param string $dataTypeKey inputタイプ
* @param array $userAttribute ユーザ項目属性データ
* @return string 入力フォームHTML
*/
private function __inputDatetime($dataTypeKey, $userAttribute) {
$html = '';
//入力部品
$html .= '<div class="col-xs-9">';
if (in_array($userAttribute['UserAttribute']['key'], ['last_login', 'previous_login'], true)) {
//最終ログイン日時の場合、ラベル変更(○日以上ログインしていない、○日以内ログインしている)
$moreThanDays =
__d('user_manager', 'Not logged more than <span style="color:#ff0000;">X</span>days ago');
$withinDays =
__d('user_manager', 'Have logged in within <span style="color:#ff0000;">X</span>days');
$html .= '<div class="user-search-conditions-datetime">';
} else {
//○日以上前、○日以内
$moreThanDays = __d('user_manager', 'more than <span style="color:#ff0000;">X</span>days ago');
$withinDays = __d('user_manager', 'within <span style="color:#ff0000;">X</span>days');
$html .= '<div class="user-search-conditions-datetime">';
}
//○日以上前(○日以上ログインしていない)の出力
$fieldKey = $userAttribute['UserAttribute']['key'] . '_' . UserSearch::MORE_THAN_DAYS;
$html .= '<div class="input-group">';
$html .= $this->NetCommonsForm->input(
$fieldKey,
array(
'name' => $fieldKey,
'type' => 'number',
'class' => 'form-control input-sm user-search-conditions-datetime-top',
'label' => false,
'div' => false,
'error' => false,
'placeholder' => false,
)
);
$html .= $this->NetCommonsForm->label(
$fieldKey, $moreThanDays,
array('class' => 'input-group-addon user-search-conditions-datetime-top')
);
$html .= '</div>';
//○日以内(○日以内ログインしている)の出力
$fieldKey = $userAttribute['UserAttribute']['key'] . '_' . UserSearch::WITHIN_DAYS;
$html .= '<div class="input-group">';
$html .= $this->NetCommonsForm->input(
$fieldKey,
array(
'name' => $fieldKey,
'type' => 'number',
'class' => 'form-control input-sm user-search-conditions-datetime-bottom',
'label' => false,
'div' => false,
'error' => false,
'placeholder' => false,
)
);
$html .= $this->NetCommonsForm->label(
$fieldKey, $withinDays,
array('class' => 'input-group-addon user-search-conditions-datetime-bottom')
);
$html .= '</div>';
$html .= '</div>';
$html .= '</div>';
return $html;
}
/**
* 会員検索の入力フォームHTMLを生成する
*
* @param string $dataTypeKey inputタイプ
* @param array $userAttribute ユーザ項目属性データ
* @return string 入力フォームHTML
*/
private function __inputText($dataTypeKey, $userAttribute) {
$html = '';
$html .= $this->NetCommonsForm->input($userAttribute['UserAttribute']['key'], array(
'type' => DataType::DATA_TYPE_TEXT,
'label' => false,
'div' => array('class' => 'col-xs-9'),
'error' => false,
'class' => 'form-control input-sm',
));
return $html;
}
/**
* 会員検索の入力フォームHTMLを生成する
*
* @return string inputのHTML
*/
public function userSearchRoomsSelect() {
$html = '';
$options = ['' => __d('net_commons', '-- Not specified --')] + $this->_View->viewVars['rooms'];
$html .= '<div class="form-group row user-search-conditions-row">';
//ラベル
$html .= '<div class="col-xs-3">';
$html .= '<label class="control-label">' . __d('user_manager', 'Rooms') . '</label>';
$html .= '</div>';
$html .= $this->NetCommonsForm->input('room_id', array(
'type' => 'select',
'options' => $options,
'label' => false,
'div' => array('class' => 'col-xs-9'),
'error' => false,
'class' => 'form-control input-sm',
));
$html .= '</div>';
return $html;
}
/**
* 会員検索の入力フォームHTMLを生成する
*
* @return string inputのHTML
*/
public function userSearchGroupsSelect() {
$html = '';
$options = ['' => __d('net_commons', '-- Not specified --')] + $this->_View->viewVars['groups'];
$html .= '<div class="form-group row user-search-conditions-row">';
//ラベル
$html .= '<div class="col-xs-3">';
$html .= '<label class="control-label">' . __d('user_manager', 'Groups') . '</label>';
$html .= '</div>';
$html .= $this->NetCommonsForm->input('group_id', array(
'type' => 'select',
'options' => $options,
'label' => false,
'div' => array('class' => 'col-xs-9'),
'error' => false,
'class' => 'form-control input-sm',
));
$html .= '</div>';
return $html;
}
/**
* 対象会員の絞り込みボタン表示
*
* @param string $label ボタンラベル
* @param array $params URLのパラメータ
* @param bool $displayExport エクスポートボタンの表示
* @return string HTML
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function displaySearchButton($label, $params = array(), $displayExport = false) {
$UserSearch = ClassRegistry::init('Users.UserSearch');
$html = '';
$html .= $this->NetCommonsHtml->script(array(
'/users/js/user_search.js'
));
if ($this->_View->request->query &&
$this->_View->request->query !== ['room_id' => null]) {
$conditions = '';
foreach ($this->_View->request->query as $key => $value) {
if ($UserSearch->getOriginalField($key)) {
$conditions .= '<div class="pull-left">';
$conditions .=
$this->NetCommonsForm->label('', $UserSearch->getReadableFieldName($key));
$conditions .= ': ';
$conditions .= $UserSearch->getSearchFieldValue($key, $value);
$conditions .= '</div>';
}
}
if (! $conditions) {
$conditions .= '<div class="pull-left">';
$conditions .= __d('users', 'Not search condition.');
$conditions .= '</div>';
}
if ($displayExport) {
$conditions .= '<div class="pull-right">';
$conditions .= $this->NetCommonsHtml->link(
'<span class="glyphicon glyphicon-export"></span> ' . __d('user_manager', 'Export'),
array('action' => 'export', '?' => $this->_View->request->query),
array('name' => 'import', 'class' => 'btn btn-default btn-sm', 'escapeTitle' => false)
);
$conditions .= '</div>';
}
$html .= '<div class="user-search-conditions-frame clearfix well well-sm">';
$html .= $conditions;
$html .= '</div>';
}
$html .= '<div class="text-center" ng-controller="UserSearch.controller">';
$html .= $this->Button->search($label, array(
'type' => 'button',
'ng-click' => 'showUserSearch(' .
h(json_encode($this->__makeQueryBySearchConditions(), JSON_FORCE_OBJECT)) . ', ' .
'\'' . h($this->_View->request->params['plugin']) . '\', ' .
'\'' . h($this->_View->request->params['controller']) . '\', ' .
'\'' . h($this->_View->request->params['action']) . '\', ' .
'\'' . implode('/', array_map('h', $params)) . '\')'
));
if ($this->_View->request->query) {
$html .= $this->NetCommonsHtml->link(
__d('users', 'Search condition clear'),
Hash::merge(
array(
'plugin' => $this->_View->request->params['plugin'],
'controller' => $this->_View->request->params['controller'],
'action' => $this->_View->request->params['action']
),
$params
),
array(
'class' => 'btn btn-default btn-workflow',
)
);
}
$html .= '</div>';
return $html;
}
/**
* 検索条件画面のクエリパラメータ生成
*
* @return array
*/
private function __makeQueryBySearchConditions() {
$query = [];
foreach ($this->_View->request->query as $key => $value) {
if (is_array($value)) {
foreach ($value as $k => $v) {
$query[$key . '[' . $k . ']'] = $v;
}
} else {
$query[$key] = $value;
}
}
return $query;
}
}