-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUserSearchHelper.php
More file actions
340 lines (302 loc) · 9.68 KB
/
UserSearchHelper.php
File metadata and controls
340 lines (302 loc) · 9.68 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
<?php
/**
* UserSearch 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');
/**
* UserSearch Helper
*
* @property LinkButtonHelper $LinkButton
* @property NetCommonsHtmlHelper $NetCommonsHtml
* @property DateHelper $Date
* @property RoomsHelper $Rooms
* @property DisplayUserHelper $DisplayUser
*
* @package NetCommons\Users\View\Helper
*/
class UserSearchHelper extends AppHelper {
/**
* 使用するヘルパー
* ただし、Roomヘルパーを使用する場合は、RoomComponentを呼び出している必要がある。
*
* @var array
*/
public $helpers = array(
'NetCommons.LinkButton',
'NetCommons.NetCommonsHtml',
'NetCommons.Date',
'Rooms.Rooms',
'Users.DisplayUser'
);
/**
* UserAttributes data
*
* @var array
*/
public $userAttributes;
/**
* 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);
//Modelの呼び出し
$this->User = ClassRegistry::init('Users.User');
$this->UsersLanguage = ClassRegistry::init('Users.UsersLanguage');
$this->userAttributes = Hash::combine(
Hash::get($this->_View->viewVars, 'userAttributes', array()),
'{n}.{n}.{n}.UserAttribute.key', '{n}.{n}.{n}'
);
}
/**
* テーブルヘッダーの出力
*
* @param bool $isEdit 編集の有無
* @param bool $isSort ソートの有無
* @return string User value
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function tableHeaders($isEdit = false, $isSort = true) {
$output = '';
$this->UserSearch = ClassRegistry::init('Users.UserSearch');
foreach ($this->_View->viewVars['displayFields'] as $fieldName) {
$userAttribute = Hash::get($this->userAttributes, $fieldName);
$dataTypeKey = $userAttribute['UserAttributeSetting']['data_type_key'];
if ($dataTypeKey === DataType::DATA_TYPE_DATETIME ||
in_array($userAttribute['UserAttribute']['key'], UserAttribute::$typeDatetime, true)) {
//日付型
$class = ' class="row-datetime"';
} else {
$class = '';
}
$output .= '<th' . $class . '>';
if ($fieldName === 'room_role_key') {
$label = __d('rooms', 'Room role');
$key = 'RoomRole.level';
} else {
$userAttribute = Hash::extract(
$this->userAttributes, '{s}.UserAttribute[key=' . $fieldName . ']'
);
$label = Hash::get($userAttribute, '0.name');
$key = $this->UserSearch->getReadableFieldOrderKey($fieldName);
}
if ($isSort) {
$output .= $this->_View->Paginator->sort($key, $label);
} else {
$output .= h($label);
}
$output .= '</th>';
if ($isEdit) {
if ($fieldName === 'handlename') {
$output .= '<th></th>';
}
if ($fieldName === 'status') {
$output .= '<th></th>';
}
}
}
return $output;
}
/**
* テーブル行の出力
*
* @param array $user ユーザデータ
* @param bool $isEdit 編集の有無
* @param array $editUrl 編集リンクURL
* @param array $tdOptions tdのオプション
* @return string 行のHTMLタグ
*/
public function tableRow($user, $isEdit, $editUrl = array(), $tdOptions = array()) {
$output = '';
foreach ($this->_View->viewVars['displayFields'] as $fieldName) {
$modelName = '';
if ($this->User->hasField($fieldName)) {
$modelName = $this->User->alias;
} elseif ($this->UsersLanguage->hasField($fieldName)) {
$modelName = $this->UsersLanguage->alias;
} elseif ($fieldName === 'room_role_key') {
$modelName = 'RolesRoom';
}
if ($modelName) {
$output .= $this->tableCell($user, $modelName, $fieldName, $isEdit, true, $tdOptions);
} else {
$output .= '<td></td>';
}
if ($isEdit) {
if ($fieldName === 'handlename') {
$output .= '<td>' . $this->__userEdit($user, $editUrl) . '</td>';
}
if ($fieldName === 'status') {
$output .= '<td>' . $this->__editUserStatus($user, $modelName, $fieldName) . '</td>';
}
}
}
return $output;
}
/**
* 状態によって、行色を変更する
*
* @param array $user ユーザデータ
* @return string 行のHTMLタグ
*/
public function userActiveClass($user) {
$output = ' id="Tr' . h($this->NetCommonsHtml->domId('User.id.' . $user['User']['id'])) . '"';
if (Hash::get($user, 'User.status') === UserAttributeChoice::STATUS_CODE_NONACTIVE) {
$output .= ' class="danger text-danger"';
} elseif (Hash::get($user, 'User.status') === UserAttributeChoice::STATUS_CODE_WAITING ||
Hash::get($user, 'User.status') === UserAttributeChoice::STATUS_CODE_APPROVED) {
$output .= ' class="warning text-danger"';
}
return $output;
}
/**
* テーブル行の出力
*
* @param array $user ユーザデータ
* @param array $url 編集リンクURL
* @return string 編集のHTMLタグ
*/
private function __userEdit($user, $url) {
$output = '';
if (Current::read('User.role_key') !== UserRole::USER_ROLE_KEY_SYSTEM_ADMINISTRATOR &&
($user['User']['role_key'] === UserRole::USER_ROLE_KEY_SYSTEM_ADMINISTRATOR)) {
return $output;
}
$url['key'] = $user['User']['id'];
$output .= $this->LinkButton->edit('', $url, ['iconSize' => 'btn-xs']);
return $output;
}
/**
* テーブルセルの出力
*
* @param array $user ユーザデータ
* @param string $modelName モデル名
* @param string $fieldName 表示フィールド
* @param bool $isEdit 編集の有無
* @param bool $tdElement tdタグの出力
* @param array $tdOptions tdのオプション
* @return string セルのHTMLタグ
*/
public function tableCell($user, $modelName, $fieldName, $isEdit, $tdElement, $tdOptions) {
$userAttribute = Hash::get($this->userAttributes, $fieldName);
$dataTypeKey = $userAttribute['UserAttributeSetting']['data_type_key'];
$value = '';
$tdOptions['escape'] = false;
if ($fieldName === 'handlename') {
//ハンドル
$value = $this->linkHandlename($user, $isEdit);
} elseif ($fieldName === 'room_role_key') {
//ルーム権限
$value = $this->Rooms->roomRoleName($user[$modelName]['role_key']);
} elseif (isset($userAttribute['UserAttributeChoice']) &&
isset($user[$modelName][$fieldName])) {
//選択肢
if ($fieldName === 'role_key') {
$values = Hash::extract(
$userAttribute['UserAttributeChoice'], '{n}[key=' . $user[$modelName][$fieldName] . ']'
);
} else {
$values = Hash::extract(
$userAttribute['UserAttributeChoice'], '{n}[code=' . $user[$modelName][$fieldName] . ']'
);
}
$value = h(Hash::get($values, '0.name'));
} elseif ($dataTypeKey === DataType::DATA_TYPE_DATETIME ||
in_array($userAttribute['UserAttribute']['key'], UserAttribute::$typeDatetime, true)) {
//日付型
$value = h($this->Date->dateFormat($user[$modelName][$fieldName]));
$tdOptions['class'] = 'row-datetime';
} else {
//その他
$value = h(Hash::get($user, $modelName . '.' . $fieldName, ''));
}
if ($tdElement) {
return $this->NetCommonsHtml->tag('td', $value, $tdOptions);
} else {
return $value;
}
}
/**
* 状態の値取得。編集可で承認待ち⇒承認、承認済みの場合、再送
*
* @param array $user ユーザデータ
* @param string $modelName モデル名
* @param string $fieldName 表示フィールド
* @return string 編集のHTMLタグ
*/
private function __editUserStatus($user, $modelName, $fieldName) {
$output = '';
if ($user[$modelName][$fieldName] === UserAttributeChoice::STATUS_CODE_WAITING) {
$confirm = __d('user_manager', 'Do you approve?');
$output .= $this->_View->element('Users.Users/edit_status_form', array(
'user' => $user,
'label' => __d('user_manager', 'Approval'),
'options' => array(
'icon' => 'glyphicon-ok',
'class' => 'btn btn-xs btn-warning',
'onclick' => 'return confirm(\'' . $confirm . '\')',
'ng-disabled' => 'sending',
),
));
} elseif ($user[$modelName][$fieldName] === UserAttributeChoice::STATUS_CODE_APPROVED) {
$output .= $this->_View->element('Users.Users/edit_status_form', array(
'user' => $user,
'label' => __d('user_manager', 'Resend'),
'options' => array(
'icon' => 'glyphicon-envelope',
'class' => 'btn btn-xs btn-info',
'ng-disabled' => 'sending',
),
));
}
return $output;
}
/**
* ハンドルの出力
*
* @param array $user ユーザデータ
* @param bool $isEdit 編集の有無
* @return string ハンドルのHTMLタグ
*/
public function linkHandlename($user, $isEdit) {
if (! $isEdit) {
return $this->DisplayUser->handleLink($user, array('avatar' => true), array(), 'User');
} else {
return $this->NetCommonsHtml->link(
$this->DisplayUser->handle($user, array('avatar' => true), 'User'), '#',
array(
'escape' => false,
'ng-controller' => 'UserManagerController',
'ng-click' => 'showUser(' . $user['User']['id'] . ')'
),
array(
'escape' => false
)
);
}
}
/**
* ユーザ選択画面でJSONでユーザを表示する
*
* @param array $user ユーザデータ
* @param array $model モデル名(TrackableCreatorやTrackableUpdaterなど)
* @return string JSON形式
*/
public function convertUserArrayByUserSelection($user, $model = 'TrackableCreator') {
$result = array(
'id' => Hash::get($user, $model . '.id'),
'handlename' => Hash::get($user, $model . '.handlename'),
'avatar' => $this->DisplayUser->avatar($user, array(), $model . '.id', false),
'link' => NetCommonsUrl::userActionUrl(array('key' => Hash::get($user, $model . '.id'))),
);
return $result;
}
}