diff --git a/app/Controllers/CommonController.php b/app/Controllers/CommonController.php index b7c204f..6efd1db 100644 --- a/app/Controllers/CommonController.php +++ b/app/Controllers/CommonController.php @@ -139,20 +139,23 @@ abstract class CommonController extends BaseController helper(['form', __FUNCTION__]); return view($full_path . DIRECTORY_SEPARATOR . $view_file, ['viewDatas' => $view_datas]); } - + //생성 protected function create_form_process(array $formDatas = []): array { //Form Default값 설정 return $formDatas; } + protected function create_form_result_process(string $action): string|RedirectResponse + { + return $this->action_render_process($this->getActionPaths(), $action, $this->getViewDatas()); + } final public function create_form(): string|RedirectResponse { try { $action = __FUNCTION__; $this->action_init_process($action); - $formDatas = $this->create_form_process(); - $this->addViewDatas('formDatas', $formDatas); - return $this->action_render_process($this->getActionPaths(), $action, $this->getViewDatas()); + $this->addViewDatas('formDatas', $this->create_form_process()); + return $this->create_form_result_process($action); } catch (\Exception $e) { return $this->action_redirect_process('error', "{$this->getTitle()}에서 생성폼 오류:" . $e->getMessage()); } @@ -163,18 +166,22 @@ abstract class CommonController extends BaseController $dto = $this->service->createDTO($this->request->getPost()); return $this->service->create($dto); } + protected function create_result_process(CommonEntity $entity): string|RedirectResponse + { + return $this->action_modal_process("{$this->getTitle()}에서 {$entity->getTitle()} 생성이 완료되었습니다."); + } final public function create(): string|RedirectResponse { try { $this->action_init_process(__FUNCTION__); - $entity = $this->create_process(); - return $this->action_modal_process("{$this->getTitle()}에서 생성이 완료되었습니다."); + return $this->create_result_process($this->create_process()); } catch (ValidationException $e) { return $this->action_redirect_process('error', "{$this->getTitle()}에서 생성 검증오류:" . $e->getMessage()); } catch (\Exception $e) { return $this->action_redirect_process('error', "{$this->getTitle()}에서 생성 오류:" . $e->getMessage()); } } + //수정 protected function modify_form_process($uid): CommonEntity { if (!$uid) { @@ -186,14 +193,17 @@ abstract class CommonController extends BaseController } return $entity; } + protected function modify_form_result_process(string $action): string|RedirectResponse + { + return $this->action_render_process($this->getActionPaths(), $action, $this->getViewDatas()); + } final public function modify_form($uid): string|RedirectResponse { try { $action = __FUNCTION__; $this->action_init_process($action); - $entity = $this->modify_form_process($uid); - $this->addViewDatas('entity', $entity); - return $this->action_render_process($this->getActionPaths(), $action, $this->getViewDatas()); + $this->addViewDatas('entity', $this->modify_form_process($uid)); + return $this->modify_form_result_process($action); } catch (\Exception $e) { return $this->action_redirect_process('error', "{$this->getTitle()}에서 {$uid} 수정폼 오류:" . $e->getMessage()); } @@ -204,129 +214,160 @@ abstract class CommonController extends BaseController $dto = $this->service->createDTO($this->request->getPost()); return $this->service->modify($uid, $dto); } + protected function modify_result_process(CommonEntity $entity): string|RedirectResponse + { + return $this->action_modal_process("{$this->getTitle()}에서 {$entity->getTitle()} 수정이 완료되었습니다."); + } final public function modify($uid): string|RedirectResponse { try { $this->action_init_process(__FUNCTION__); - $entity = $this->modify_process($uid); - return $this->action_modal_process("{$this->getTitle()}에서 {$uid} 수정이 완료되었습니다."); + return $this->modify_result_process($this->modify_process($uid)); } catch (ValidationException $e) { return $this->action_redirect_process('error', "{$this->getTitle()}에서 {$uid} 수정 검증오류:" . $e->getMessage()); } catch (\Exception $e) { return $this->action_redirect_process('error', "{$this->getTitle()}에서 {$uid} 수정 오류:" . $e->getMessage()); } } + //일괄처리 + protected function batchjob_pre_process(array $postDatas = []): array + { + foreach ($this->request->getPost() as $field => $value) { + $postDatas[$field] = $value; + } + //1. postDatas에서 선택된 uids 정보 추출 + $uids = []; + if (isset($postDatas['batchjob_uids'])) { + $uids = $postDatas['batchjob_uids']; + unset($postDatas['batchjob_uids']); + } + if (empty($uids)) { + throw new \Exception("적용할 리스트을 선택하셔야합니다."); + } + //2. postDatas에서 필요없는 정보 버림 + unset($postDatas['batchjob_submit']); + //3. 나머지는 $formDatas로 사용할 데이터 추출 + $formDatas = []; + foreach ($postDatas as $field => $value) { + if ($value !== "") { + $formDatas[$field] = $value; + } + } + if (empty($formDatas)) { + throw new \Exception("변경할 조건항목을 선택하셔야합니다."); + } + //4. 데이터가 있는 필드 추출 + $selectedFields = array_keys($formDatas); + return array($uids, $selectedFields, $formDatas); + } protected function batchjob_process($uid, array $formDatas): CommonEntity { return $this->service->batchjob($uid, $formDatas); } + protected function batchjob_result_process(array $uids, array $entities, array $errors): string|RedirectResponse + { + return $this->action_redirect_process('info', sprintf( + "%s에서 %s개 처리완료, %s개 오류, 총:%s개 수정이 완료되었습니다.", + $this->getTitle(), + count($entities), + count($errors), + count($uids) + )); + } final public function batchjob(): string|RedirectResponse { try { - $postDatas = $this->request->getPost(); - //1. postDatas에서 선택된 uids 정보 추출 - $uids = []; - if (isset($postDatas['batchjob_uids'])) { - $uids = $postDatas['batchjob_uids']; - unset($postDatas['batchjob_uids']); - } - if (empty($uids)) { - throw new \Exception("적용할 리스트을 선택하셔야합니다."); - } - //2. postDatas에서 필요없는 정보 버림 - unset($postDatas['batchjob_submit']); - //3. 나머지는 $formDatas로 사용할 데이터 추출 - $formDatas = []; - foreach ($postDatas as $field => $value) { - if ($value !== "") { - $formDatas[$field] = $value; - } - } - if (empty($formDatas)) { - throw new \Exception("변경할 조건항목을 선택하셔야합니다."); - } - //4. 데이터가 있는 필드 추출 - $selectedFields = array_keys($formDatas); + //사전작업 + list($uids, $selectedFields, $formDatas) = $this->batchjob_pre_process(); //초기화 $this->service->getFormService()->setFormFields($selectedFields); $this->service->getFormService()->setFormRules(__FUNCTION__, $selectedFields); $this->service->getFormService()->setFormFilters($selectedFields); $this->service->getFormService()->setFormOptions($selectedFields); $entities = []; - $error = 0; + $errors = []; foreach ($uids as $uid) { try { $entities[] = $this->batchjob_process($uid, $formDatas); } catch (ValidationException $e) { log_message('error', "{$this->getTitle()}에서 {$uid} 수정 검증오류:" . $e->getMessage()); - $error++; + $errors[] = $e->getMessage(); } catch (\Exception $e) { log_message('error', "{$this->getTitle()}에서 {$uid} 수정 오류:" . $e->getMessage()); - $error++; + $errors[] = $e->getMessage(); } } - return $this->action_redirect_process('info', sprintf( - "%s에서 %s개 처리완료, %s개 오류, 총:%s개 수정이 완료되었습니다.", - $this->getTitle(), - count($entities), - $error, - count($uids) - )); + return $this->batchjob_result_process($uids, $entities, $errors); } catch (\Exception $e) { return $this->action_redirect_process('error', "{$this->getTitle()}에서 일괄작업처리 오류:" . $e->getMessage()); } } + //삭제 protected function delete_process($uid): CommonEntity { return $this->service->delete($uid); } + protected function delete_result_process(CommonEntity $entity): string|RedirectResponse + { + return $this->action_redirect_process('info', "{$this->getTitle()}에서 {$entity->getTitle()} 삭제가 완료되었습니다."); + } final public function delete($uid): RedirectResponse { try { - $entity = $this->delete_process($uid); - return $this->action_redirect_process('info', "{$this->getTitle()}에서 {$entity->getTitle()} 삭제가 완료되었습니다."); + return $this->delete_result_process($this->delete_process($uid)); } catch (\Exception $e) { return $this->action_redirect_process('error', "{$this->getTitle()}에서 {$uid} 삭제 오류:" . $e->getMessage()); } } + //일괄삭제 + protected function batchjob_delete_pre_process(): array + { + $postDatas = $this->request->getPost(); + //1. postDatas에서 선택된 uids 정보 추출 + $uids = []; + if (isset($postDatas['batchjob_uids'])) { + $uids = $postDatas['batchjob_uids']; + unset($postDatas['batchjob_uids']); + } + if (empty($uids)) { + throw new \Exception("삭제할 리스트을 선택하셔야합니다."); + } + return array($uids); + } + protected function batchjob_delete_result_process(array $uids, array $entities, array $errors): string|RedirectResponse + { + return $this->action_redirect_process('info', sprintf( + "%s에서 %s개 처리완료, %s개 오류, 총:%s개 일괄삭제가 완료되었습니다.", + $this->getTitle(), + count($entities), + count($errors), + count($uids) + )); + } protected function batchjob_delete_process($uid): CommonEntity { - return $this->service->batchjob_delete($uid); + return $this->service->delete($uid); } final public function batchjob_delete(): string|RedirectResponse { try { - $postDatas = $this->request->getPost(); - //1. postDatas에서 선택된 uids 정보 추출 - $uids = []; - if (isset($postDatas['batchjob_uids'])) { - $uids = $postDatas['batchjob_uids']; - unset($postDatas['batchjob_uids']); - } - if (empty($uids)) { - throw new \Exception("삭제할 리스트을 선택하셔야합니다."); - } + $uids = $this->batchjob_delete_pre_process(); $entities = []; - $error = 0; + $errors = []; foreach ($uids as $uid) { try { $entities[] = $this->batchjob_delete_process($uid); } catch (\Exception $e) { log_message('error', "{$this->getTitle()}에서 {$uid} 삭제 오류:" . $e->getMessage()); - $error++; + $errors[] = $e->getMessage(); } } - return $this->action_redirect_process('info', sprintf( - "%s에서 %s개 처리완료, %s개 오류, 총:%s개 일괄삭제가 완료되었습니다.", - $this->getTitle(), - count($entities), - $error, - count($uids) - )); + return $this->batchjob_delete_result_process($uids, $entities, $errors); } catch (\Exception $e) { return $this->action_redirect_process('error', "{$this->getTitle()}에서 일괄삭제 오류:" . $e->getMessage()); } } + //상세보기 protected function view_process($uid): CommonEntity { if (!$uid) { @@ -338,14 +379,17 @@ abstract class CommonController extends BaseController } return $entity; } + protected function view_result_process(string $action): string + { + return $this->action_render_process($this->getActionPaths(), $action, $this->getViewDatas()); + } final public function view($uid): string|RedirectResponse { try { $action = __FUNCTION__; $this->action_init_process($action); - $entity = $this->view_process($uid); - $this->addViewDatas('entity', $entity); - return $this->action_render_process($this->getActionPaths(), $action, $this->getViewDatas()); + $this->addViewDatas('entity', $this->view_process($uid)); + return $this->view_result_process($action); } catch (\Exception $e) { return $this->action_redirect_process('error', "{$this->getTitle()}에서 {$uid} 상세보기 오류:" . $e->getMessage()); } @@ -417,6 +461,10 @@ abstract class CommonController extends BaseController } return $entities; } + protected function index_result_process(string $action): string + { + return $this->action_render_process($this->getActionPaths(), $action, $this->getViewDatas()); + } public function index(): string { $action = __FUNCTION__; @@ -448,13 +496,13 @@ abstract class CommonController extends BaseController } //현재 URL을 세션에 저장 $this->getAuthContext()->pushCurrentUrl($this->request->getUri()->getPath() . ($this->request->getUri()->getQuery() ? "?" . $this->request->getUri()->getQuery() : "")); - return $this->action_render_process($this->getActionPaths(), $action, $this->getViewDatas()); + return $this->index_result_process($action); } //OUPUT Document 관련 - protected function download_process(string $document_type, mixed $loaded_data): array + protected function downloadByDocumentType(string $document_type, mixed $loaded_data): array { - $full_path = WRITEPATH . DIRECTORY_SEPARATOR . "excel"; + $full_path = WRITEPATH . DIRECTORY_SEPARATOR . "download"; switch ($document_type) { case 'excel': $file_name = sprintf("%s_%s.xlsx", $this->service->getClassPaths(false, "_"), date('Y-m-d_Hm')); @@ -469,40 +517,44 @@ abstract class CommonController extends BaseController } return array($full_path, $file_name); } + protected function download_process(string $action, string $output_type, mixed $uid = null): DownloadResponse|RedirectResponse|string + { + switch ($output_type) { + case 'excel': + case 'pdf': + helper(['form']); + $this->index_condition_process($action); + $this->addViewDatas('entities', $this->index_process()); + $html = $this->action_render_process($this->getActionPaths(), $action, $this->getViewDatas()); + //data loading + $reader = new Html(); + $loaded_data = $reader->loadFromString($html); + list($full_path, $file_name) = $this->downloadByDocumentType($output_type, $loaded_data); + $full_path .= DIRECTORY_SEPARATOR . $file_name; + break; + default: + if (!$uid) { + throw new \Exception("{$output_type}은 반드시 uid의 값이 필요합니다."); + } + $entity = $this->service->getEntity($uid); + if (!$entity) { + throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다."); + } + $this->addViewDatas('entity', $entity); + list($file_name, $uploaded_filename) = $entity->getDownlaodFile(); + $full_path = WRITEPATH . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR . $uploaded_filename; + break; + } + return $this->response->download($full_path, null)->setFileName($file_name); + } // Download - public function download(string $output_type, mixed $uid = false): DownloadResponse|RedirectResponse|string + final public function download(string $output_type, mixed $uid = false): DownloadResponse|RedirectResponse|string { $action = __FUNCTION__; try { //초기화 $this->action_init_process($action); - switch ($output_type) { - case 'excel': - case 'pdf': - helper(['form']); - $this->index_condition_process($action); - $this->addViewDatas('entities', $this->index_process()); - $html = $this->action_render_process($this->getActionPaths(), $action, $this->getViewDatas()); - //data loading - $reader = new Html(); - $loaded_data = $reader->loadFromString($html); - list($full_path, $file_name) = $this->download_process($output_type, $loaded_data); - $full_path .= DIRECTORY_SEPARATOR . $file_name; - break; - default: - if (!$uid) { - throw new \Exception("{$output_type}은 반드시 uid의 값이 필요합니다."); - } - $entity = $this->service->getEntity($uid); - if (!$entity) { - throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다."); - } - $this->addViewDatas('entity', $entity); - list($file_name, $uploaded_filename) = $entity->getDownlaodFile(); - $full_path = WRITEPATH . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR . $uploaded_filename; - break; - } - return $this->response->download($full_path, null)->setFileName($file_name); + return $this->download_process($action, $output_type, $uid); } catch (\Exception $e) { return $this->action_redirect_process('error', "{$this->getTitle()}에서 오류:" . $e->getMessage()); } diff --git a/writable/excel/index.html b/writable/download/index.html similarity index 100% rename from writable/excel/index.html rename to writable/download/index.html