From c6219c0aab497f42516afc988cdd65d87b4d25ef Mon Sep 17 00:00:00 2001 From: "choi.jh" Date: Fri, 7 Nov 2025 10:40:33 +0900 Subject: [PATCH] trafficmonitor init...2 --- app/Controllers/Admin/UserController.php | 23 ++++- app/Controllers/Auth/AuthController.php | 9 +- app/Controllers/CommonController.php | 30 +++++-- app/Forms/Auth/GoogleForm.php | 12 +-- app/Forms/Auth/LocalForm.php | 12 +-- app/Forms/CommonForm.php | 38 ++++----- app/Forms/TrafficForm.php | 103 +++-------------------- app/Forms/UserForm.php | 29 +++++-- app/Services/CommonService.php | 56 ++++-------- app/Services/UserService.php | 38 +++++++-- app/Views/admin/user/modify_form.php | 2 +- 11 files changed, 158 insertions(+), 194 deletions(-) diff --git a/app/Controllers/Admin/UserController.php b/app/Controllers/Admin/UserController.php index 510f6ae..e1d78f8 100644 --- a/app/Controllers/Admin/UserController.php +++ b/app/Controllers/Admin/UserController.php @@ -40,12 +40,12 @@ class UserController extends AdminController protected function create_process(): RedirectResponse { //요청 데이터를 DTO 객체로 변환 - $dto = new UserDTO($this->request->getPost()); - $entity = $this->service->create($dto); + $dto = new UserDTO($this->request->getPost()); + $entity = $this->service->create($dto); $redirect_url = $this->authService->popPreviousUrl() ?? implode(DIRECTORY_SEPARATOR, $this->getActionPaths()); return redirect()->to($redirect_url)->with('success', "{$entity->getTitle()} 계정 생성이 완료되었습니다."); } - protected function modify_form_process($uid): UserEntity + protected function modify_form_process($uid): void { if (!$uid) { throw new \Exception("계정 번호가 정의 되지 않았습니다."); @@ -54,6 +54,21 @@ class UserController extends AdminController if (!$entity instanceof UserEntity) { throw new \Exception("{$uid}에 해당하는 계정을 찾을수 없습니다."); } - return $entity; + $this->addViewDatas('entity', $entity); + } + protected function modify_process($uid): RedirectResponse + { + if (!$uid) { + throw new \Exception("계정 번호가 정의 되지 않았습니다."); + } + $entity = $this->service->getEntity($uid); + if (!$entity instanceof UserEntity) { + throw new \Exception("{$uid}에 해당하는 계정을 찾을수 없습니다."); + } + //요청 데이터를 DTO 객체로 변환 + $dto = new UserDTO($this->request->getPost()); + $entity = $this->service->modify($dto); + $redirect_url = $this->authService->popPreviousUrl() ?? implode(DIRECTORY_SEPARATOR, $this->getActionPaths()); + return redirect()->to($redirect_url)->with('success', "{$entity->getTitle()} 계정 수정이 완료되었습니다."); } } diff --git a/app/Controllers/Auth/AuthController.php b/app/Controllers/Auth/AuthController.php index 4c9334a..6a85c91 100644 --- a/app/Controllers/Auth/AuthController.php +++ b/app/Controllers/Auth/AuthController.php @@ -76,13 +76,14 @@ abstract class AuthController extends CommonController return redirect()->back()->withInput()->with('error', "로그아웃 중 오류가 발생했습니다."); } } - protected function create_form_process(): void {} - protected function create_process(): RedirectResponse + final protected function create_form_process(): void {} + final protected function create_process(): RedirectResponse { return redirect()->to('/'); } - protected function modify_form_process($uid): mixed + final protected function modify_form_process($uid): void {} + final protected function modify_process($uid): RedirectResponse { - return new UserEntity(); + return redirect()->to('/'); } } diff --git a/app/Controllers/CommonController.php b/app/Controllers/CommonController.php index e04a820..7d682a5 100644 --- a/app/Controllers/CommonController.php +++ b/app/Controllers/CommonController.php @@ -86,10 +86,10 @@ abstract class CommonController extends BaseController $this->addViewDatas('myCertification', $this->myCertification); $this->addViewDatas('layout', $this->getLayout()); $this->addViewDatas('serviceForm', $this->service->getFormService()); - $this->addViewDatas('formFields', $this->service->getFormFields()); - $this->addViewDatas('formFilters', $this->service->getFormFilters()); - $this->addViewDatas('formRules', $this->service->getFormRules($action)); - $this->addViewDatas('formOptions', $this->service->getFormOptions($action)); + $this->addViewDatas('formFields', $this->service->getFormService()->getFormFields($action)); + $this->addViewDatas('formFilters', $this->service->getFormService()->getFormFilters($action)); + $this->addViewDatas('formRules', $this->service->getFormService()->getFormRules($action)); + $this->addViewDatas('formOptions', $this->service->getFormService()->getFormOptions($action)); } protected function action_render_process(string $action): void { @@ -155,15 +155,14 @@ abstract class CommonController extends BaseController return redirect()->back()->withInput()->with('error', $e->getMessage()); } } - abstract protected function modify_form_process($uid): mixed; + abstract protected function modify_form_process($uid): void; final public function modify_form($uid): string { $action = __FUNCTION__; try { //초기화 $this->action_init_process($action); - $entity = $this->modify_form_process($uid); - $this->addViewDatas('entity', $entity); + $this->modify_form_process($uid); $this->action_render_process($action); // dd($this->getViewDatas()); } catch (\Exception $e) { @@ -178,6 +177,23 @@ abstract class CommonController extends BaseController $this->getViewDatas() ); } + abstract protected function modify_process($uid): RedirectResponse; + final public function modify($uid): RedirectResponse + { + $action = __FUNCTION__; + try { + //초기화 + $this->action_init_process($action); + return $this->modify_process($uid); + } catch (ValidationException $e) { + // 검증 실패 시 폼으로 돌아가서 오류 메시지 표시 + log_message('error', $e->getMessage()); + return redirect()->back()->withInput()->with('errors', $e->getMessage()); + } catch (\Exception $e) { + log_message('error', $e->getMessage()); + return redirect()->back()->withInput()->with('error', $e->getMessage()); + } + } //리스트관련 //조건절 처리 // protected function index_condition_process(): void diff --git a/app/Forms/Auth/GoogleForm.php b/app/Forms/Auth/GoogleForm.php index 660996a..f584679 100644 --- a/app/Forms/Auth/GoogleForm.php +++ b/app/Forms/Auth/GoogleForm.php @@ -11,17 +11,17 @@ class GoogleForm extends CommonForm parent::__construct(); } - public function getFormFields(): array + public function getFormFields(string $action, ?array $fields = null): array { - return ["access_code"]; + return $fields ?? ["access_code"]; } - public function getFormFilters(): array + public function getFormFilters(string $action, ?array $fields = null): array { - return []; + return $fields ?? []; } - public function getBatchjobButtons(): array + public function getBatchjobButtons(string $action = 'index', ?array $buttions = null): array { - return []; + return $buttions ?? []; } public function getFormRule(string $action, string $field, array $rules = []): array { diff --git a/app/Forms/Auth/LocalForm.php b/app/Forms/Auth/LocalForm.php index c097d0f..338b545 100644 --- a/app/Forms/Auth/LocalForm.php +++ b/app/Forms/Auth/LocalForm.php @@ -11,17 +11,17 @@ class LocalForm extends CommonForm parent::__construct(); } - public function getFormFields(): array + public function getFormFields(string $action, ?array $fields = null): array { - return ["id", "passwd"]; + return $fields ?? ["id", "passwd"]; } - public function getFormFilters(): array + public function getFormFilters(string $action, ?array $fields = null): array { - return []; + return $fields ?? []; } - public function getBatchjobButtons(): array + public function getBatchjobButtons(string $action = 'index', ?array $buttions = null): array { - return []; + return $buttions ?? []; } public function getFormRule(string $action, string $field, array $rules = []): array { diff --git a/app/Forms/CommonForm.php b/app/Forms/CommonForm.php index 920cf7f..818c808 100644 --- a/app/Forms/CommonForm.php +++ b/app/Forms/CommonForm.php @@ -9,7 +9,7 @@ abstract class CommonForm { private array $_attributes = []; protected function __construct() {} - abstract public function getFormFields(): array; + abstract public function getFormFields(string $action, ?array $fields = null): array; final public function setAttributes(array $attributes): void { $this->_attributes = $attributes; @@ -21,48 +21,48 @@ abstract class CommonForm } return $this->_attributes[$key]; } - final public function getFormRules(string $action, array $fields, array $rules = []): array + final public function getFormRules(string $action, array $rules = [], ?array $fields = null): array { - foreach ($fields as $field) { + foreach ($fields ?? $this->getFormFields($action) as $field) { $rules = $this->getFormRule($action, $field, $rules); } return $rules; } - final public function getFormOptions(string $action, array $fields, array $options = []): array + final public function getFormOptions(string $action, array $options = [], ?array $fields = null): array { - foreach ($fields as $field) { + foreach ($fields ?? $this->getFormFilters($action) as $field) { $options = $this->getFormOption($action, $field, $options); } return $options; } //필수함수 - public function getFormFilters(): array + public function getFormFilters(string $action, ?array $fields = null): array { - return []; + return $fields ?? []; } - public function getViewFields(): array + public function getViewFields(string $action = 'view', ?array $fields = null): array { - return $this->getFormFields(); + return $fields ?? $this->getFormFields($action); } - public function getViewFilters(): array + public function getViewFilters(string $action = 'view', ?array $fields = null): array { - return $this->getFormFilters(); + return $fields ?? $this->getFormFilters($action); } - public function getIndexFields(): array + public function getIndexFields(string $action = 'index', ?array $fields = null): array { - return $this->getFormFields(); + return $fields ?? $this->getFormFields($action); } - public function getIndexFilters(): array + public function getIndexFilters(string $action = 'index', ?array $fields = null): array { - return $this->getFormFilters(); + return $fields ?? $this->getFormFilters($action); } - public function getBatchjobFields(): array + public function getBatchjobFields(string $action = 'index', ?array $fields = null): array { - return $this->getFormFields(); + return $fields ?? $this->getIndexFilters($action); } - public function getBatchjobButtons(): array + public function getBatchjobButtons(string $action = 'index', ?array $buttions = null): array { - return [ + return $buttions ?? [ 'batchjob' => '일괄 처리', 'batchjob_delete' => '일괄 삭제', ]; diff --git a/app/Forms/TrafficForm.php b/app/Forms/TrafficForm.php index e595fd4..55eddae 100644 --- a/app/Forms/TrafficForm.php +++ b/app/Forms/TrafficForm.php @@ -11,98 +11,21 @@ class TrafficForm extends CommonForm parent::__construct(); } - public function getFormFields(): array + public function getFormFields(string $action, ?array $fields = null): array { - return [ - "site", - "location", - "clientinfo_uid", - 'serverinfo_uid', - "rack", - "line", - "title", - "start_at", - "billing_at", - "status", - 'sale', - 'amount', - "history", + $fields = $fields ?? [ + 'client', + 'switch', + 'index', ]; - } - public function getFormFilters(): array - { - return [ - 'site', - 'location', - 'clientinfo_uid', - 'serverinfo_uid', - 'rack', - 'line', - 'status', - ]; - } - public function getIndexFields(): array - { - return [ - 'site', - 'location', - 'clientinfo_uid', - 'serverinfo_uid', - 'sale', - 'amount', - 'billing_at', - 'status', - 'start_at', - 'updated_at', - ]; - } - public function getIndexFilters(): array - { - return [ - 'site', - 'location', - 'clientinfo_uid', - 'serverinfo_uid', - 'user_uid', //home::dashboard의 최신신규서버현황에서 사용 - 'status', - ]; - } - public function getBatchjobFields(): array - { - return ['site', 'location', 'clientinfo_uid', 'status']; - } - final public function getBatchjobButtons(): array - { - return [ - 'batchjob' => '일괄 처리 ', - ]; - } - public function getFormRule(string $action, string $field, array $rules = []): array - { - - switch ($field) { - case "serverinfo_uid": - case "clientinfo_uid": - $rules[$field] = "required|numeric"; - break; - default: - $rules = parent::getFormRule($action, $field, $rules); - break; + if (in_array($action, ['modify_form', 'modify'])) { + $fields = $fields ?? [ + 'client', + 'switch', + 'index', + 'status', + ]; } - return $rules; - } - public function getFormOption(string $class_path, string $action, string $field, array $options = []): array - { - switch ($field) { - case 'serverinfo_uid': - $serverService = service('ServerService'); - $option = $serverService->getEntities(); - break; - default: - return parent::getFormOption($class_path, $action, $field, $options); - // break; - } - $options[$field] = $option; - return $options; + return $fields; } } diff --git a/app/Forms/UserForm.php b/app/Forms/UserForm.php index c146a89..3e8df0d 100644 --- a/app/Forms/UserForm.php +++ b/app/Forms/UserForm.php @@ -11,9 +11,9 @@ class UserForm extends CommonForm parent::__construct(); } - public function getFormFields(): array + public function getFormFields(string $action, ?array $fields = null): array { - return [ + $fields = $fields ?? [ 'id', 'passwd', 'confirmpassword', @@ -21,19 +21,30 @@ class UserForm extends CommonForm 'email', 'mobile', 'role', - 'status', ]; + if (in_array($action, ['modify_form', 'modify'])) { + $fields = $fields ?? [ + 'passwd', + 'confirmpassword', + 'name', + 'email', + 'mobile', + 'role', + 'status', + ]; + } + return $fields; } - public function getFormFilters(): array + public function getFormFilters(string $action, ?array $fields = null): array { - return [ + return $fields ?? [ 'role', 'status', ]; } - public function getIndexFields(): array + public function getIndexFields(string $action = 'index', ?array $fields = null): array { - return [ + return $fields ?? [ 'id', 'name', 'email', @@ -42,9 +53,9 @@ class UserForm extends CommonForm 'status', ]; } - public function getBatchjobFields(): array + public function getBatchjobFields(string $action = 'index', ?array $fields = null): array { - return ['status']; + return $fields ?? ['status']; } public function getFormRule(string $action, string $field, array $rules = []): array diff --git a/app/Services/CommonService.php b/app/Services/CommonService.php index e3c7c6a..9f3c28f 100644 --- a/app/Services/CommonService.php +++ b/app/Services/CommonService.php @@ -60,44 +60,6 @@ abstract class CommonService $row = $this->model->selectMax($this->model->getPKField())->get()->getRow(); return isset($row->uid) ? ((int)$row->uid + 1) : 1; } - //필수함수 - //Form관련 - public function getFormFields(): array - { - return $this->getFormService()->getFormFields(); - } - final public function getFormRules(string $action): array - { - return $this->getFormService()->getFormRules($action, $this->getFormFields()); - } - public function getFormFilters(): array - { - return $this->getFormService()->getFormFilters(); - } - public function getFormOptions(string $action): array - { - return $this->getFormService()->getFormOptions($action, $this->getFormFilters()); - } - public function getViewFields(): array - { - return $this->getFormService()->getViewFields(); - } - public function getViewFilters(): array - { - return $this->getFormService()->getViewFields(); - } - public function getIndexFields(): array - { - return $this->getFormService()->getIndexFields(); - } - public function getIndexFilters(): array - { - return $this->getFormService()->getIndexFilters(); - } - public function getBatchjobFields(): array - { - return $this->getFormService()->getBatchjobFields(); - } //Entity관련 protected function getEntity_process(mixed $entity): mixed { @@ -117,7 +79,23 @@ abstract class CommonService } return $entities; } - //Action관련 + //Validation용 + final protected function getValidationRules(string $action, array $allRules = []): array + { + foreach ($this->getFormService()->getFormFields($action) as $field) { + $allRules = array_merge($allRules, $this->getValidationRule($action, $field, $allRules)); + } + return $allRules; + } + protected function getValidationRule(string $action, string $field, array $rules = []): array + { + switch ($field) { + default: + $rules[$field] = $this->getFormService()->getValidationRule($action, $field); + break; + } + return $rules; + } //Index용 final public function getTotalCount(): int { diff --git a/app/Services/UserService.php b/app/Services/UserService.php index 414f9b6..6aed284 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -51,7 +51,7 @@ class UserService extends CommonService // DTO 객체를 배열로 변환하여 검증기에 전달 $formDatas = (array) $dto; // dd($formDatas); - if (!service('validation')->setRules($this->getFormRules(__FUNCTION__))->run($formDatas)) { + if (!service('validation')->setRules($this->getValidationRules(__FUNCTION__))->run($formDatas)) { throw new ValidationException(implode("\n", service('validation')->getErrors())); } $entity = new UserEntity($formDatas); @@ -60,18 +60,38 @@ class UserService extends CommonService throw new \Exception("{$entity->getTitle()} 등록 중 DB 오류가 발생하였습니다."); } // 💡 PK 타입에 따른 최종 Entity 반환 로직 분기 - if ($this->model->useAutoIncrement) { - // 1. 정수 PK인 경우: $result는 ID 번호이므로, 저장된 레코드를 DB에서 다시 조회합니다. - // (자동 설정된 필드(created_at 등)를 포함하기 위해 재조회) + if ($this->model->useAutoIncrement) { //정수 PK인 경우 $savedEntity = $this->model->find($result); - } else { - // 2. 문자열 PK인 경우: $result는 true/1 이므로, - // 이미 ID가 설정된 $entity 객체를 반환하거나, ID로 재조회합니다. - // Entity에 모든 필드(created_at 등)가 설정되지 않았다면 재조회가 안전합니다. + } else { //문자열 PK인 경우 $pkValue = $entity->{$this->model->primaryKey}; $savedEntity = $this->model->find($pkValue); } - if (!$savedEntity) { + if (!$savedEntity instanceof UserEntity) { + throw new \Exception("등록된 데이터를 찾을 수 없습니다."); + } + return $savedEntity; + } + public function modify(UserDTO $dto): UserEntity + { + // DTO 객체를 배열로 변환하여 검증기에 전달 + $formDatas = (array) $dto; + // dd($formDatas); + if (!service('validation')->setRules($this->getValidationRules(__FUNCTION__))->run($formDatas)) { + throw new ValidationException(implode("\n", service('validation')->getErrors())); + } + $entity = new UserEntity($formDatas); + $result = $this->model->update($entity->getPK(), $formDatas); + if (!$result) { + throw new \Exception("{$entity->getTitle()} 등록 중 DB 오류가 발생하였습니다."); + } + // 💡 PK 타입에 따른 최종 Entity 반환 로직 분기 + if ($this->model->useAutoIncrement) { //정수 PK인 경우 + $savedEntity = $this->model->find($result); + } else { //문자열 PK인 경우 + $pkValue = $entity->{$this->model->primaryKey}; + $savedEntity = $this->model->find($pkValue); + } + if (!$savedEntity instanceof UserEntity) { throw new \Exception("등록된 데이터를 찾을 수 없습니다."); } return $savedEntity; diff --git a/app/Views/admin/user/modify_form.php b/app/Views/admin/user/modify_form.php index ef972fc..fe95971 100644 --- a/app/Views/admin/user/modify_form.php +++ b/app/Views/admin/user/modify_form.php @@ -16,7 +16,7 @@ -
"btn btn-outline btn-primary")); ?>
+
"btn btn-outline btn-primary")); ?>
include("templates/{$viewDatas['control']['layout']}/form_content_bottom"); ?>