cfmgrv4/app/Controllers/Admin/UserController.php
2024-10-01 18:25:10 +09:00

116 lines
4.0 KiB
PHP

<?php
namespace App\Controllers\Admin;
use App\Models\UserModel;
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) {
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);
}
//생성
public function create_form(): RedirectResponse|string
{
$this->init('create');
return $this->create_form_procedure();
}
protected function create_process(): void
{
$this->create_validate($this->action, $this->fields);
$this->formDatas = $this->getFormDatas();
parent::create_process();
$this->entity = $this->getModel()->create(formDatas: $this->formDatas);
}
public function create(): RedirectResponse
{
$this->init(__FUNCTION__);
return $this->create_procedure();
}
//수정
protected function modify_form_process(string $uid): void
{
$this->entity = $this->getModel()->getEntityByPK(intval($uid));
if ($this->entity === null) {
throw new \Exception("해당 정보를 찾을수 없습니다.");
}
parent::modify_form_process($uid);
}
public function modify_form(string $uid): RedirectResponse|string
{
$this->init('modify');
return $this->modify_form_procedure($uid);
}
protected function modify_process(string $uid): void
{
$this->entity = $this->getModel()->getEntityByPK(intval($uid));
if ($this->entity === null) {
throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
}
$this->modify_validate($this->action, $this->fields);
$this->formDatas = $this->getFormDatas();
parent::modify_process($uid);
$this->entity = $this->getModel()->modify($this->entity, $this->formDatas);
}
public function modify(string $uid): RedirectResponse
{
$this->init(__FUNCTION__);
return $this->modify_procedure($uid);
}
// 리스트
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 = ['role', 'status'];
return $this->list_procedure();
}
}