cfmgrv4/app/Controllers/Admin/UserController.php
2024-10-02 21:33:13 +09:00

161 lines
6.2 KiB
PHP

<?php
namespace App\Controllers\Admin;
use App\Models\UserModel;
use CodeIgniter\HTTP\DownloadResponse;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
class UserController extends AdminController
{
private $_model = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->class_name = "User";
$this->class_path = $this->root . $this->class_name;
$this->title = lang("{$this->class_path}.title");
helper($this->class_path);
}
protected function getModel(): UserModel
{
if ($this->_model === null) {
$this->_model = new UserModel();
}
return $this->_model;
}
protected function getFormFieldOption(string $field, array $options = []): array
{
switch ($field) {
default:
$options = parent::getFormFieldOption($field, $options);
break;
}
return $options;
}
protected function getFormData(string $field, array $formDatas): array
{
switch ($field) {
case 'role':
$roles = $this->request->getVar($field);
if (!count($roles)) {
throw new \Exception("권한이 지정되지 않았습니다.");
}
$formDatas[$field] = implode(DEFAULTS["DELIMITER_ROLE"],);
break;
default:
$formDatas = parent::getFormData($field, $formDatas);
break;
}
return $formDatas;
}
private function init(string $action): void
{
$this->action = $action;
$this->fields = ['id', 'passwd', $this->getModel()::TITLE, 'email', 'mobile', 'role'];
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
$this->filter_fields = ['role', 'status'];
$this->field_options = $this->getFormFieldOptions($this->filter_fields);
}
//생성
protected function create_validate(string $action, array $fields): void
{
//변경할 값 확인 : Upload된 파일 검증시 $this->request->getVar()보다 먼처 체크필요
$this->validation = service('validation');
foreach ($fields as $field) {
switch ($field) {
case 'role':
//아래 Rule Array는 필드명.* checkbox를 사용
$this->validation->setRule($field . "*", $field, $this->getModel()->getFieldRule($action, $field));
break;
default:
$this->validation->setRule($field, $field, $this->getModel()->getFieldRule($action, $field));
break;
}
}
if (!$this->validation->withRequest($this->request)->run()) {
throw new \Exception("{$this->class_name} 작업 데이터 검증 오류발생\n" . implode(
"\n",
$this->validation->getErrors()
));
}
}
public function create_form(): RedirectResponse|string
{
$this->init('create');
return $this->create_form_procedure();
}
public function create(): RedirectResponse
{
$this->init(__FUNCTION__);
return $this->create_procedure();
}
//수정
protected function modify_validate(string $action, array $fields): void
{
//변경할 값 확인 : Upload된 파일 검증시 $this->request->getVar()보다 먼처 체크필요
$this->validation = service('validation');
foreach ($fields as $field) {
switch ($field) {
case 'role':
//아래 Rule Array는 필드명.* checkbox를 사용
$this->validation->setRule($field . "*", $field, $this->getModel()->getFieldRule($action, $field));
break;
default:
$this->validation->setRule($field, $field, $this->getModel()->getFieldRule($action, $field));
break;
}
}
if (!$this->validation->withRequest($this->request)->run()) {
throw new \Exception("{$this->class_name} 작업 데이터 검증 오류발생\n" . implode(
"\n",
$this->validation->getErrors()
));
}
}
public function modify_form(string $uid): RedirectResponse|string
{
$this->init('modify');
return $this->modify_form_procedure($uid);
}
public function modify(string $uid): RedirectResponse
{
$this->init(__FUNCTION__);
return $this->modify_procedure($uid);
}
//일괄작업
public function batcjob(): RedirectResponse
{
$this->action = __FUNCTION__;
$this->fields = ['status'];
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
return $this->batcjob_procedure();
}
// 리스트
public function index(): string
{
$this->action = __FUNCTION__;
$this->fields = ['id', $this->getModel()::TITLE, 'email', 'mobile', 'role', 'status'];
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
$this->filter_fields = ['role', 'status'];
$this->field_options = $this->getFormFieldOptions($this->filter_fields);
$this->batchjob_fields = ['status'];
return $this->list_procedure();
}
// Download
public function download(string $output_type, $uid = false): DownloadResponse|string
{
$this->action = __FUNCTION__;
$this->fields = ['id', $this->getModel()::TITLE, 'email', 'mobile', 'role', 'status'];
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
$this->filter_fields = ['role', 'status'];
$this->field_options = $this->getFormFieldOptions($this->filter_fields);
$this->batchjob_fields = ['status'];
return $this->download_procedure($output_type, $uid);
}
}