Automation/app/Controllers/MVController.php
2024-09-21 11:33:14 +09:00

132 lines
5.1 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);
}
abstract protected function create_init(): void;
abstract protected function getModel(): mixed;
//Field별 Form Option용
protected function getFormFilterOption(string $filter_field, array $filter_options): array
{
switch ($filter_field) {
default:
$temps = lang($this->class_name . '.' . strtoupper($filter_field));
if (!is_array($temps)) {
throw new \Exception(__FUNCTION__ . "에서 {$filter_field}의 데이터가 array가 아닙니다.\n" . var_export($temps, true));
}
$filter_options[$filter_field] = [
["" => lang($this->class_name . '.label.' . $filter_field) . ' 선택'],
lang($this->class_name . '.' . strtoupper($filter_field))
];
break;
}
return $filter_options;
}
//Field별 Form Option용
final public function getFormFilterOptions(array $filter_options = []): array
{
foreach ($this->filter_fields as $filter_field) {
if (is_array($filter_field)) {
throw new \Exception(__FUNCTION__ . "에서 filter_field가 array 입니다.\n" . var_export($filter_field, true));
}
$filter_options = $this->getFormFilterOption($filter_field, $filter_options);
}
return $filter_options;
}
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->create_form_process();
$this->session->keepFlashdata(SESSION_NAMES['RETURN_URL']);
return view("/{$this->class_name}/insert", ['attributes' => $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();
}
}
}