Automation/app/Controllers/MVController.php
2024-09-23 17:21:44 +09:00

157 lines
5.8 KiB
PHP

<?php
namespace App\Controllers;
use Psr\Log\LoggerInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\RedirectResponse;
abstract class MVController extends CommonController
{
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
helper('common');
}
abstract protected function create_init(): void;
abstract protected function getModel(): mixed;
//Field별 Form Option용
protected function getFormFieldOption(string $field, array $options = []): array
{
switch ($field) {
default:
$temps = lang($this->class_name . '.' . strtoupper($field));
if (!is_array($temps)) {
throw new \Exception(__FUNCTION__ . "에서 {$field}의 데이터가 array가 아닙니다.\n" . var_export($temps, true));
}
$options = [
["" => lang($this->class_name . '.label.' . $field) . ' 선택'],
lang($this->class_name . '.' . strtoupper($field)),
];
break;
}
return $options;
}
protected function getFormFieldInput(string $field, string $value, array $inputs = []): array
{
switch ($field) {
case 'status':
$inputs[$field] = form_dropdown(
$field,
$this->getFormFieldOption($field),
$value
);
break;
case 'updated_at':
case 'created_at':
$inputs[$field] = form_input($field, $value, ["class" => " calender"]);
break;
default:
$inputs[$field] = form_input($field, $value);
break;
}
return $inputs;
}
final public function getFormFieldInputs(array $inputs = []): array
{
foreach ($this->fields as $field) {
if (is_array($field)) {
throw new \Exception(__FUNCTION__ . "에서 field array 입니다.\n" . var_export($field, true));
}
$inputs = $this->getFormFieldInput($field, old($field) ?? DEFAULTS['EMPTY'], $inputs);
}
return $inputs;
}
protected function getFormFieldRule(string $field, array $rules): array
{
if (is_array($field)) {
throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true));
}
switch ($field) {
default:
$rules[$field] = $this->_model->getFieldRule($field, $rules);
;
break;
}
return $rules;
}
final public function getFormFieldRules(array $rules = []): array
{
foreach ($this->fields as $field) {
$rules = $this->getFormFieldRule($field, $rules);
}
return $rules;
}
//전송된 데이터 검증
protected function validateFormData(): void
{
//변경할 값 확인 : Upload된 파일 검증시 $this->request->getVar()보다 먼처 체크필요
$validation = \Config\Services::validation();
$validation->setRules($this->getModel()->getFieldRules($this->fields));
if (!$validation->withRequest($this->request)->run()) {
throw new \Exception("데이터 검증 오류발생\n" . implode("\n", $this->validator->getErrors()));
}
}
//전송된 데이터
protected function getFormData(string $field): void
{
switch ($field) {
default:
$this->formDatas[$field] = rtrim($this->request->getVar($field));
break;
}
}
//전송된 데이터 재정의
protected function convertFormData(string $field): void
{
switch ($field) {
default:
break;
}
}
final public function create_form_process(): RedirectResponse|string
{
helper(['form']);
try {
$this->create_init();
$this->forminputs = $this->getFormFieldInputs();
$this->session->keepFlashdata(SESSION_NAMES['RETURN_URL']);
$this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []];
return view(
strtolower($this->class_name) . "/create",
['viewDatas' => $this->getAttributes()]
);
} catch (\Exception $e) {
log_message("error", $e->getMessage());
return redirect()->to($this->session->getFlashdata(SESSION_NAMES['RETURN_URL']) ?: "/")->with(SESSION_NAMES['RETURN_MSG'], $e->getMessage());
}
}
abstract protected function create_process_submit(): mixed;
final protected function create_process(): mixed
{
$this->getModel()->transStart();
try {
$this->create_init();
$this->validateFormData();
foreach ($this->fields as $field) {
$this->getFormData($field);
}
foreach ($this->fields as $field) {
$this->convertFormData($field);
}
$entity = $this->create_process_submit();
log_message("notice", __FUNCTION__ . "=>{$entity->getTitle()} 작업을 완료하였습니다.");
return redirect()->to($this->session->getFlashdata(SESSION_NAMES['RETURN_URL']) ?: "/");
} catch (\Exception $e) {
//Transaction Rollback
$this->getModel()->transRollback();
log_message("error", $e->getMessage());
$this->session->setFlashdata(SESSION_NAMES['RETURN_MSG'], __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());
$this->session->keepFlashdata(SESSION_NAMES['RETURN_URL']);
return redirect()->back()->withInput();
}
}
}