-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUserEditFormHelper.php
More file actions
529 lines (455 loc) · 16.2 KB
/
UserEditFormHelper.php
File metadata and controls
529 lines (455 loc) · 16.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
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
<?php
/**
* UserEditForm 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');
/**
* UserEditForm Helper
*
* ※PHPMのSuppressWarningsは暫定
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Users\View\Helper
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
class UserEditFormHelper extends AppHelper {
/**
* Other helpers used by FormHelper
*
* @var array
*/
public $helpers = array(
'DataTypes.DataTypeForm',
'M17n.SwitchLanguage',
'NetCommons.Date',
'NetCommons.NetCommonsHtml',
'NetCommons.NetCommonsForm',
);
/**
* Default Constructor
*
* @param View $View The View this helper is being attached to.
* @param array $settings Configuration settings for the helper.
*/
public function __construct(View $View, $settings = array()) {
parent::__construct($View, $settings);
$this->User = ClassRegistry::init('Users.User');
$this->UsersLanguage = ClassRegistry::init('Users.UsersLanguage');
}
/**
* Before render callback. beforeRender is called before the view file is rendered.
*
* Overridden in subclasses.
*
* @param string $viewFile The view file that is going to be rendered
* @return void
*/
public function beforeRender($viewFile) {
$this->NetCommonsHtml->css('/users/css/style.css');
$this->NetCommonsHtml->script('/data_types/js/data_types.jquery.js');
parent::beforeRender($viewFile);
}
/**
* 会員の入力フォームの表示
*
* @param array $userAttribute UserAttributeデータ
* @return string HTMLタグ
*/
public function userInput($userAttribute) {
$html = '';
$userAttributeKey = $userAttribute['UserAttribute']['key'];
if ($userAttributeKey === 'created_user') {
$html .= $this->__input('TrackableCreator.handlename', $userAttribute);
} elseif ($userAttributeKey === 'modified_user') {
$html .= $this->__input('TrackableUpdater.handlename', $userAttribute);
} elseif ($userAttribute['UserAttributeSetting']['data_type_key'] === DataType::DATA_TYPE_IMG) {
$html .= $this->__input('User.' . $userAttributeKey, $userAttribute);
} elseif ($this->User->hasField($userAttributeKey)) {
$html .= $this->__input('User.' . $userAttributeKey, $userAttribute);
} elseif ($this->UsersLanguage->hasField($userAttributeKey)) {
$index = 0;
foreach (array_keys($this->_View->viewVars['languages']) as $langId) {
$html .= '<div ng-show="activeLangId === \'' . $langId . '\'" ng-cloak>';
$html .= $this->__input(
'UsersLanguage.' . $index . '.' . $userAttributeKey, $userAttribute, $langId
);
$html .= '</div>';
$index++;
}
} else {
$html .= h($userAttribute['UserAttribute']['name']);
return $html;
}
return $html;
}
/**
* 会員の入力フォームの表示(自分)
*
* @param array $userAttribute UserAttributeデータ
* @return string HTMLタグ
*/
public function userInputForSelf($userAttribute) {
$html = '';
//以下の条件の場合、何も表示しない
// * 他人の場合
// * 非表示(display=OFF)項目
// * 自分が読めない && パスワード以外
// * 自分自身が書けない && ラベルタイプ以外
if (Current::read('User.id') !== $this->_View->viewVars['user']['User']['id'] ||
! $userAttribute['UserAttributeSetting']['display'] ||
! $userAttribute['UserAttributesRole']['self_readable'] &&
$userAttribute['UserAttribute']['key'] !== UserAttribute::PASSWORD_FIELD ||
! $userAttribute['UserAttributesRole']['self_editable'] &&
$userAttribute['UserAttributeSetting']['data_type_key'] !== DataType::DATA_TYPE_LABEL) {
return $html;
}
$html .= $this->userInput($userAttribute);
return $html;
}
/**
* 会員の公開非公開の有無ラジオボタンの表示
*
* @param array $userAttribute UserAttributeデータ
* @return string HTMLタグ
*/
public function userPublicForSelf($userAttribute) {
$html = '';
//以下の条件の場合、何も表示しない
// * 各自で公開非公開の設定ができない
// * 他人の場合
// * 非表示(display=OFF)項目
// * 自分が読めない
// * 自分自身が書けない && ラベルタイプ以外
if (! $userAttribute['UserAttributeSetting']['self_public_setting'] ||
Current::read('User.id') !== Hash::get($this->_View->viewVars, 'user.User.id') ||
! $userAttribute['UserAttributeSetting']['display'] ||
! $userAttribute['UserAttributesRole']['self_readable'] ||
! $userAttribute['UserAttributesRole']['self_editable'] &&
$userAttribute['UserAttributeSetting']['data_type_key'] !== DataType::DATA_TYPE_LABEL) {
return $html;
}
$html .= $this->userPublic($userAttribute);
return $html;
}
/**
* 会員の公開非公開の有無ラジオボタンの表示
*
* @param array $userAttribute UserAttributeデータ
* @return string HTMLタグ
*/
public function userPublic($userAttribute) {
$html = '';
//以下の条件の場合、何も表示しない
// * 各自で公開非公開の設定ができない
if (! $userAttribute['UserAttributeSetting']['self_public_setting']) {
return $html;
}
$fieldName =
'User.' . sprintf(UserAttribute::PUBLIC_FIELD_FORMAT, $userAttribute['UserAttribute']['key']);
$html .= '<div class="col-xs-12 col-sm-offset-3 col-sm-offset-9 user-edit-choice-public">';
$html .= $this->NetCommonsForm->radio($fieldName, User::$publicTypes, array(
'div' => array('class' => 'form-inline'),
));
$html .= '</div>';
return $html;
}
/**
* 会員のメールの受信可否のチェックボックスボタンの表示
*
* @param array $userAttribute UserAttributeデータ
* @return string HTMLタグ
*/
public function userMailReceptionForSelf($userAttribute) {
$html = '';
//以下の条件の場合、何も表示しない
// * メール項目でない
// * 各自でメール受信可否の設定ができない
// * 他人の場合
// * 非表示(display=OFF)項目
// * 自分自身が書けない
if ($userAttribute['UserAttributeSetting']['data_type_key'] !== DataType::DATA_TYPE_EMAIL ||
! $userAttribute['UserAttributeSetting']['self_email_setting'] ||
Current::read('User.id') !== Hash::get($this->_View->viewVars, 'user.User.id') ||
! $userAttribute['UserAttributeSetting']['display'] ||
! $userAttribute['UserAttributesRole']['self_editable']) {
return $html;
}
$html .= $this->userMailReception($userAttribute);
return $html;
}
/**
* 会員のメールの受信可否のチェックボックスボタンの表示
*
* @param array $userAttribute UserAttributeデータ
* @return string HTMLタグ
*/
public function userMailReception($userAttribute) {
$html = '';
//以下の条件の場合、何も表示しない
// * メール項目でない
// * 各自でメール受信可否の設定ができない
if ($userAttribute['UserAttributeSetting']['data_type_key'] !== DataType::DATA_TYPE_EMAIL ||
! $userAttribute['UserAttributeSetting']['self_email_setting']) {
return $html;
}
$fieldName =
'User.' .
sprintf(UserAttribute::MAIL_RECEPTION_FIELD_FORMAT, $userAttribute['UserAttribute']['key']);
$html .= '<div class="col-xs-12 col-sm-offset-3 col-sm-offset-9 user-edit-choice-mail">';
$html .= $this->NetCommonsForm->checkbox($fieldName, array(
'label' => __d('users', 'Yes, I receive by e-mail.'),
'div' => array('class' => 'form-inline'),
'inline' => true,
));
$html .= '</div>';
return $html;
}
/**
* inputタグの生成
*
* @param string $fieldName フィールド名("Modelname.fieldname"形式)
* @param array $userAttribute UserAttributeデータ
* @param int $languageId 言語ID
* @return string HTMLタグ
*/
private function __input($fieldName, $userAttribute, $languageId = null) {
$html = '';
$dataTypeKey = $userAttribute['UserAttributeSetting']['data_type_key'];
$userAttributeKey = $userAttribute['UserAttribute']['key'];
if ($dataTypeKey === DataType::DATA_TYPE_LABEL &&
! Hash::get($this->_View->request->data, $fieldName)) {
return $html;
}
$attributes = array();
//ラベル
$attributes['label'] = $this->SwitchLanguage->inputLabel(
$userAttribute['UserAttribute']['name'], $languageId
);
//入力タイプ
$attributes['type'] = $dataTypeKey;
//必須項目ラベルの設定
$attributes['required'] = (bool)$userAttribute['UserAttributeSetting']['required'];
//選択肢の設定
if (isset($userAttribute['UserAttributeChoice'])) {
$attributes['options'] = $this->__makeOptions(
$userAttributeKey,
$userAttribute['UserAttributeChoice']
);
if (! $userAttribute['UserAttributeSetting']['required']) {
$attributes['empty'] = !(bool)$userAttribute['UserAttributeSetting']['required'];
}
}
if (in_array($userAttributeKey, UserAttribute::$typeDatetime, true)) {
$attributes['value'] = $this->Date->dateFormat(
Hash::get($this->_View->request->data, $fieldName), UserAttribute::DISPLAY_DATETIME_FORMAT
);
}
$attributes['help'] = h(Hash::get($userAttribute, 'UserAttribute.description', ''));
$noneDivOuters = [DataType::DATA_TYPE_IMG, DataType::DATA_TYPE_TEXTAREA];
if (in_array($attributes['type'], $noneDivOuters, true)) {
$divOuterStart = '';
$divOuterEnd = '';
} else {
$divOuterStart = '<div class="form-group row">';
$divOuterEnd = '</div>';
}
$html .= $this->__inputDataType(
$fieldName, $userAttribute, $attributes, array('start' => $divOuterStart, 'end' => $divOuterEnd)
);
return $html;
}
/**
* 選択肢オプションを生成する。
*
* @param string $attributeKey 会員項目キー
* @param array $choices 選択肢データ
* @return array
*/
private function __makeOptions($attributeKey, $choices) {
//選択肢の設定
if ($attributeKey === 'role_key') {
$field = 'key';
} else {
$field = 'code';
}
$options = [];
foreach ($choices as $choice) {
$options[(string)$choice[$field]] = $choice['name'];
}
return $options;
}
/**
* データタイプに対するinputタグのHTML出力
* ※PHPMのSuppressWarningsは暫定
*
* @param string $fieldName フィールド名("Modelname.fieldname"形式)
* @param array $userAttribute UserAttributeデータ
* @param array $attributes HTMLタグ属性
* @param array $divOuter 外側のDIVタグ
* @return string HTML 入力HTML
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
private function __inputDataType($fieldName, $userAttribute, $attributes, $divOuter) {
$output = '';
$output .= $divOuter['start'];
switch ($attributes['type']) {
case DataType::DATA_TYPE_IMG:
$output .= $this->__image($fieldName, $userAttribute, $attributes);
break;
case DataType::DATA_TYPE_PASSWORD:
$output .= '<div class="col-xs-12 col-sm-3 user-edit-label">';
$output .= $this->NetCommonsForm->label(
$fieldName,
$attributes['label'],
['required' => Hash::get($attributes, 'required'), 'error' => true]
);
$output .= '</div>';
$attributes = Hash::insert($attributes, 'label', false);
$attributes = Hash::insert($attributes, 'div', false);
if ($this->_View->request->params['plugin'] === 'users') {
$attributes = Hash::merge(['again' => true, 'current' => true], $attributes);
} else {
$attributes = Hash::merge(['again' => true], $attributes);
}
if (! isset($attributes['autocomplete'])) {
$attributes['autocomplete'] = 'new-password';
}
$output .= '<div class="col-xs-12 col-sm-9">';
$output .= $this->NetCommonsForm->input($fieldName, $attributes);
$output .= '</div>';
break;
case DataType::DATA_TYPE_RADIO:
case DataType::DATA_TYPE_CHECKBOX:
if ($attributes['type'] === DataType::DATA_TYPE_CHECKBOX) {
$default = Hash::get($this->_View->request->data, $fieldName, '');
if (is_string($default)) {
$attributes['default'] = explode(DataType::CHECKBOX_SEPARATOR, $default);
} else {
$attributes['default'] = $default;
}
$output .= $this->NetCommonsForm->hidden($fieldName, array(
'value' => false
));
$attributes['hiddenField'] = false;
} else {
$attributes = Hash::insert($attributes, 'empty', null);
}
$output .= '<div class="col-xs-12 col-sm-3 user-edit-label">';
$output .= $this->NetCommonsForm->label(
$fieldName,
$attributes['label'],
['required' => Hash::get($attributes, 'required'), 'error' => true]
);
$output .= '</div>';
$attributes = Hash::insert($attributes, 'label', false);
$attributes = Hash::insert($attributes, 'div', false);
$attributes = Hash::merge(['inline' => true], $attributes);
$output .= '<div class="col-xs-12 col-sm-9 user-edit-choice">';
$output .= $this->NetCommonsForm->input($fieldName, $attributes);
$output .= '</div>';
break;
case DataType::DATA_TYPE_TEXTAREA:
$attributes['rows'] = 3;
$output .= $this->NetCommonsForm->input($fieldName, $attributes);
break;
default:
$output .= '<div class="col-xs-12 col-sm-3 user-edit-label">';
$output .= $this->NetCommonsForm->label(
$fieldName,
$attributes['label'],
['required' => Hash::get($attributes, 'required'), 'error' => true]
);
$output .= '</div>';
$attributes = Hash::insert($attributes, 'label', false);
$attributes = Hash::insert($attributes, 'div', false);
$output .= '<div class="col-xs-12 col-sm-9">';
$output .= $this->NetCommonsForm->input($fieldName, $attributes);
$output .= '</div>';
}
if ($this->_View->request->params['plugin'] === 'user_manager') {
$output .= $this->userPublic($userAttribute);
$output .= $this->userMailReception($userAttribute);
} else {
$output .= $this->userPublicForSelf($userAttribute);
$output .= $this->userMailReceptionForSelf($userAttribute);
}
$output .= $divOuter['end'];
return $output;
}
/**
* イメージ(アバター)の表示処理
*
* @param string $fieldName フィールド名("Modelname.fieldname"形式)
* @param array $userAttribute UserAttributeデータ
* @param array $attributes HTMLタグ属性
* @return string imageタグ
*/
private function __image($fieldName, $userAttribute, $attributes = array()) {
$output = '';
$userAttributeKey = $userAttribute['UserAttribute']['key'];
if ($this->_View->request->params['plugin'] === 'user_manager') {
if ($this->_View->request->params['controller'] === 'user_add') {
$attributes['url'] = array(
'plugin' => 'user_manager',
'controller' => 'user_add',
'action' => 'download',
'key' => $userAttributeKey,
'medium',
);
} else {
$attributes['url'] = array(
'plugin' => 'user_manager',
'controller' => 'user_manager',
'action' => 'download',
'key' => Hash::get($this->_View->request->data, 'User.id'),
'key2' => $userAttributeKey,
'medium',
);
}
} else {
$attributes['url'] = array(
'plugin' => 'users',
'controller' => 'users',
'action' => 'download',
'key' => Hash::get($this->_View->request->data, 'User.id'),
'key2' => $userAttributeKey,
'medium',
);
}
if (Hash::get($this->_View->request->data, 'User.is_avatar_auto_created') &&
$userAttributeKey === UserAttribute::AVATAR_FIELD) {
$attributes['remove'] = false;
$attributes['filename'] = false;
}
if (! isset($attributes['url'])) {
return $output;
}
$label = $attributes['label'];
if (Hash::get($attributes, 'required')) {
$label .= $this->_View->element('NetCommons.required');
}
$attributes = Hash::remove($attributes, 'label');
$attributes = Hash::remove($attributes, 'required');
$output .= $this->NetCommonsForm->label($fieldName, $label);
$output .= '<div class="thumbnail user-thumbnail user-edit-thumbnail">';
$output .= $this->NetCommonsHtml->image($attributes['url'], array(
'class' => 'img-responsive img-rounded',
'alt' => Hash::get($attributes, 'alt'),
'id' => $this->domId($fieldName . '_image')
));
$output .= '</div>';
$output .= $this->NetCommonsForm->uploadFile($fieldName, Hash::merge(array(
'label' => false,
'data-type-key' => 'image',
//'class' => false,
), $attributes));
return $output;
}
}