48 lines
1.9 KiB
PHP
48 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
class UserHelper extends CommonHelper
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
|
|
{
|
|
switch ($field) {
|
|
case 'passwd':
|
|
case 'confirmpassword':
|
|
$form = form_password($field, "", [...$extras]);
|
|
break;
|
|
case 'role':
|
|
// 💡 $value는 수정 폼 로드시 현재 Entity의 'role' 배열입니다.
|
|
// (old() 값이 있다면 old() 배열이 됩니다.)
|
|
$currentRoles = is_array($value) ? $value : [];
|
|
// 사용 가능한 모든 역할 목록 (Service 등에서 가져온다고 가정)
|
|
$allRoles = $viewDatas['control']['formOptions']['role'] ?? ROLE['USER'];
|
|
$form = '';
|
|
// 2. 각 역할에 대한 체크박스를 순회하며 생성
|
|
foreach ($allRoles as $roleValue => $roleLabel) {
|
|
|
|
// $roleLabel이 배열이 아닌 문자열만 있는 경우를 대비
|
|
if (is_int($roleValue)) {
|
|
$roleValue = $roleLabel;
|
|
$roleLabel = ucfirst($roleValue);
|
|
}
|
|
$checked = in_array($roleValue, $currentRoles);
|
|
// 3. name="role[]" 형태로 배열 데이터 전송 준비
|
|
$form .= '<label class="me-3">';
|
|
$form .= form_checkbox('role[]', $roleValue, $checked, array_merge(['id' => "role_{$roleValue}"], $extras));
|
|
$form .= " {$roleLabel}";
|
|
$form .= '</label>';
|
|
}
|
|
break;
|
|
default:
|
|
$form = parent::getFieldForm($field, $value, $viewDatas, $extras);
|
|
break;
|
|
}
|
|
return $form;
|
|
} //
|
|
}
|