isLoggedIn = false; $this->uri = $request->getUri(); if ($this->getMyAuth()->isLoggedIn()) { $this->isLoggedIn = true; $this->myAuthName = $this->getMyAuth()->getNameByAuthInfo(); $this->myAuthUID = $this->getMyAuth()->getUIDByAuthInfo(); } $this->_db = \Config\Database::connect(); } final public function __get($name) { if (!array_key_exists($name, $this->_viewDatas)) { return null; } return $this->_viewDatas[$name]; } final public function __set($name, $value): void { $this->_viewDatas[$name] = $value; } final protected function getMyAuth(): mixed { if (!$this->_myAuth) { $this->_myAuth = service('myauth'); } return $this->_myAuth; } final protected function getControlDatas(?string $key = null): mixed { if (!$key) { return $this->_control; } return array_key_exists($key, $this->_control) ? $this->_control[$key] : null; } private function setControlDatas(string $key, mixed $values): void { if (!array_key_exists($key, $this->_control)) { $this->_control[$key] = []; } $this->_control[$key] = $values; } final protected function getViewDatas(): array { return $this->_viewDatas; } final protected function getMyLogService(): mixed { if (!$this->_myLogService) { $this->_myLogService = new MyLogService(); } return $this->_myLogService; } //Index,FieldForm관련 final protected function setAction(string $action): void { $this->_control['action'] = $action; } final protected function getAction(): string { if (!array_key_exists('action', $this->_control)) { throw new \Exception("action이 정의되지 않았습니다."); } return $this->_control['action']; } final protected function initAction(array $actionFields = []): void { //action Fields정의용 $this->initAction_process(actionFields: $actionFields); $temps = []; foreach ($this->getControlDatas('actionFields') as $field) { $temps[$field] = $this->getFormFieldRule_process($this->getAction(), $field); } // echo var_dump($temps); // echo "TEST3"; // exit; $this->setControlDatas('field_rules', $temps); //Form용 Options정의 $temps = []; foreach ($this->getControlDatas('actionFilters') as $field) { $temps[$field] = []; foreach ($this->getFormFieldOption_process($field) as $option) { $temps[$field][$option->getPK()] = $option; } } $this->setControlDatas('filter_optons', $temps); } //입력 데이터 검증 private function doValidate(array $rules, array $formDatas, ?Validation $validation = null): array { //변경할 값 확인 : Upload된 파일 검증시 $this->request->getPOST()보다 먼처 체크필요 if (!$validation) { $validation = service('validation'); } // dd($rules); foreach ($rules as $field => $rule) { $validation = $this->doValidation_process($validation, $field, $rule); } // dd($formDatas); if (!$validation->run($formDatas)) { throw new \Exception("{$this->getService()->getClassName()} 작업 데이터 검증 오류발생\n" . implode( "\n", $validation->getErrors() )); } return $formDatas; // return $validation->getValidated(); } //전체 FormDatas 전달값받기 final protected function getFormDatas(array $fields, array $formDatas = []): array { foreach ($fields as $field) { //Post값이 null이면 formDatas에서 해당하는 default값이 있는지 확인후 넣고,없으면 최종적으로 null $datas = $this->request->getPost($field); if ($datas !== null) { $formDatas[$field] = $datas; } else { $formDatas = $this->getFormData_process($field, $formDatas); } } return $formDatas; } final protected function setFieldDefaultValue(string $field, mixed $default): void { if (array_key_exists('field_default_values', $this->_control)) { $this->_control['field_default_values'] = []; } $this->_control['field_default_values'][$field] = $default; } //FormDatas 검증 //생성 기본기능 final public function create_form(): RedirectResponse|string { try { //각 Field 초기화 $this->setAction(__FUNCTION__); $this->initAction(); $this->create_form_process(); //actionFilters에 해당하는 값이 있을 경우 정의 helper(['form']); $this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []]; return $this->getResultSuccess(); } catch (\Exception $e) { return $this->getResultFail($e->getMessage()); } } final public function create(): RedirectResponse|string { $this->_db->transStart(); try { //각 Field 초기화 $this->setAction(__FUNCTION__); $this->initAction(); //전달값받기 $formDatas = $this->getFormDatas($this->getControlDatas('actionFields')); $this->create_process($formDatas); $this->_db->transCommit(); return $this->getResultSuccess(); } catch (\Exception $e) { $this->_db->transRollback(); return $this->getResultFail($e->getMessage()); } } //수정 기본기능 final public function modify_form(mixed $uid): RedirectResponse|string { try { //각 Field 초기화 $this->setAction(__FUNCTION__); $this->initAction(); //기존 Entity 가져오기 $entity = $this->getService()->getEntity($uid); if (!$entity) { throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다."); } $this->modify_form_process($entity); helper(['form']); $this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []]; return $this->getResultSuccess(); } catch (\Exception $e) { return $this->getResultFail($e->getMessage()); } } final public function modify(int $uid): RedirectResponse|string { //Transaction Start $this->_db->transStart(); try { //각 Field 초기화 $this->setAction(__FUNCTION__); $this->initAction(); //전달값받기 $formDatas = $this->getFormDatas($this->getControlDatas('actionFields')); //기존 Entity 가져오기 $entity = $this->getService()->getEntity($uid); if (!$entity) { throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다."); } $this->modify_process($entity, $formDatas); $this->_db->transCommit(); return $this->getResultSuccess(); } catch (\Exception $e) { $this->_db->transRollback(); return $this->getResultFail($e->getMessage()); } } //단일필드작업기능 final public function toggle(mixed $uid, string $field): RedirectResponse|string { //Transaction Start $this->_db->transStart(); try { //각 Field 초기화:조건항목 Field는 한개만 존재하므로 Field와 Rule을 정의 $this->setAction(__FUNCTION__); $this->initAction(['fields' => [$field], 'filters' => [$field]]); //입력값정의 $formDatas = [$field => $this->request->getVar($field)]; //기존 Entity 가져오기 $entity = $this->getService()->getEntity($uid); if (!$entity) { throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다."); } $this->toggle_process($entity, $formDatas); $this->_db->transCommit(); return $this->getResultSuccess(); } catch (\Exception $e) { $this->_db->transRollback(); return $this->getResultFail($e->getMessage()); } } //일괄처리작업기능 final public function batchjob(): RedirectResponse|string { //Transaction Start $this->_db->transStart(); try { $selectedFields = []; //getBatchJobFields를 이용해서 선택된 Field 와 값정의 $formDatas = []; foreach ($this->getService()->getBatchJobFields() as $field) { $value = $this->request->getPost($field); if ($value) { $selectedFields[] = $field; $formDatas[$field] = $value; } } if (!count($selectedFields)) { throw new \Exception("변경할 조건항목을 선택하셔야합니다."); } //변경할 UIDS 정의 $uids = $this->request->getPost('batchjob_uids[]'); if (!is_array($uids) || !count($uids)) { throw new \Exception("적용할 리스트을 선택하셔야합니다."); } //각 Field 초기화: 일괄작업은 선택된 조건항목 Field만 존재하므로 Field와 Rule을 정의 $this->setAction(__FUNCTION__); $this->initAction(['fields' => [$selectedFields], 'filters' => [$selectedFields]]); $entities = []; foreach ($uids as $uid) { $entities = $this->batchjob_process($uid, $formDatas, $entities); } $this->entities = $entities; $this->_db->transCommit(); LogCollector::debug(sprintf("%s에서 총 %s개중 %s개 일괄작업을 완료하였습니다.", __METHOD__, count($uids), count($this->entities))); return $this->getResultSuccess(); } catch (\Exception $e) { $this->_db->transRollback(); return $this->getResultFail($e->getMessage()); } } final public function delete(mixed $uid): RedirectResponse|string { //Transaction Start $this->_db->transStart(); try { //각 Field 초기화:삭제는 다른 초기화 필요없음 $this->setAction(__FUNCTION__); //기존 Entity 가져오기 $entity = $this->getService()->getEntity($uid); if (!$entity) { throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다."); } $this->delete_process($entity); $this->_db->transCommit(); return $this->getResultSuccess(); } catch (\Exception $e) { $this->_db->transRollback(); return $this->getResultFail($e->getMessage()); } } //일괄삭제 final public function batchjob_delete(): RedirectResponse|string { //Transaction Start $this->_db->transStart(); try { //변경할 UIDS $uids = $this->request->getPost('batchjob_uids[]'); if (!is_array($uids) || !count($uids)) { throw new \Exception("적용할 리스트를 선택하셔야합니다."); } //각 Field 초기화:삭제는 다른 초기화 필요없음 $this->setAction(__FUNCTION__); $entities = []; foreach ($uids as $uid) { $entities = $this->batchjob_delete_process($uid, $entities); } $this->entities = $entities; $this->_db->transCommit(); LogCollector::debug(sprintf("%s에서 총 %s개중 %s개 일괄삭제를 완료하였습니다.", __METHOD__, count($uids), count($this->entities))); return $this->getResultSuccess(); } catch (\Exception $e) { $this->_db->transRollback(); return $this->getResultFail($e->getMessage()); } } final public function view(string $uid): RedirectResponse|string { try { //각 Field 초기화 $this->setAction(__FUNCTION__); $this->initAction(); //기존 Entity 가져오기 $entity = $this->getService()->getEntity($uid); if (!$entity) { throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다."); } $this->view_process($entity); helper(['form']); $this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []]; return $this->getResultSuccess(); } catch (\Exception $e) { return $this->getResultFail($e->getMessage()); } } //OUPUT Document 관련 private function download_document(string $document_type, mixed $loaded_data): array { $full_path = WRITEPATH . DIRECTORY_SEPARATOR . "excel"; switch ($document_type) { case 'excel': $file_name = sprintf("%s_%s.xlsx", $this->getService()->getClassName(), date('Y-m-d_Hm')); $writer = IOFactory::createWriter($loaded_data, 'Xlsx'); $writer->save($full_path . DIRECTORY_SEPARATOR . $file_name); break; case 'pdf': $file_name = sprintf("%s_%s.pdf", $this->getService()->getClassName(), date('Y-m-d_Hm')); $writer = new Mpdf($loaded_data); $writer->save($full_path . DIRECTORY_SEPARATOR . $file_name); break; } return array($full_path, $file_name); } // Download final public function download(string $output_type, mixed $uid = false): DownloadResponse|RedirectResponse|string { try { //각 Field 초기화 $this->setAction(__FUNCTION__); $this->initAction(); //URL처리 // $this->uri = $this->request->getUri(); switch ($output_type) { case 'excel': case 'pdf': helper(['form']); $this->index_process(); $html = $this->getResultSuccess(); //data loading $reader = new Html(); $loaded_data = $reader->loadFromString($html); list($full_path, $file_name) = $this->download_document($output_type, $loaded_data); $full_path .= DIRECTORY_SEPARATOR . $file_name; break; default: if (!$uid) { throw new \Exception("{$output_type}은 반드시 uid의 값이 필요합니다."); } $entity = $this->getService()->getEntity($uid); if (!$entity) { throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다."); } $this->entity = $entity; list($file_name, $uploaded_filename) = $this->entity->getDownlaodFile(); $full_path = WRITEPATH . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR . $uploaded_filename; break; } return $this->response->download($full_path, null)->setFileName($file_name); } catch (\Exception $e) { return $this->getResultFail($e->getMessage()); } } //공통 기본 기능 //추가 개별 처리 기능 //action Fields정의용 protected function initAction_process(array $actionFields = []): void { switch ($this->getAction()) { case 'view': $temps = is_array($actionFields) && array_key_exists('fields', $actionFields) ? $actionFields : $this->getService()->getViewFields(); $this->setControlDatas('actionFields', is_array($temps) && array_key_exists('fields', $temps) ? $temps['fields'] : []); $this->setControlDatas('actionFilters', is_array($temps) && array_key_exists('filters', $temps) ? $temps['filters'] : []); break; case 'index': $temps = is_array($actionFields) && array_key_exists('fields', $actionFields) ? $actionFields : $this->getService()->getIndexFields(); $this->setControlDatas('actionFields', is_array($temps) && array_key_exists('fields', $temps) ? $temps['fields'] : []); $this->setControlDatas('actionFilters', is_array($temps) && array_key_exists('filters', $temps) ? $temps['filters'] : []); $this->setControlDatas('batchjob_fields', array_key_exists('batchjob_fields', $temps) ? $temps['batchjob_fields'] : []); $this->setControlDatas('batchjob_buttions', array_key_exists('batchjob_buttions', $temps) ? $temps['batchjob_buttions'] : []); break; default: $temps = is_array($actionFields) && array_key_exists('fields', $actionFields) ? $actionFields : $this->getService()->getFormFields(); $this->setControlDatas('actionFields', is_array($temps) && array_key_exists('fields', $temps) ? $temps['fields'] : []); $this->setControlDatas('actionFilters', is_array($temps) && array_key_exists('filters', $temps) ? $temps['filters'] : []); break; } } //FieldForm관련용 protected function getFormFieldOption_process(string $field, array $options = []): array { switch ($field) { default: $options = $this->getService()->getFormFieldOption($field, $options); break; } if (!is_array($options)) { throw new \Exception(__FUNCTION__ . "에서 {$field}의 options 값이 array가 아닙니다.\n" . var_export($options, true)); } return $options; } protected function getFormFieldRule_process(string $action, string $field): string { if (is_array($field)) { throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true)); } switch ($field) { default: $rule = $this->getService()->getFormFieldRule($action, $field); break; } return $rule; } //FormData Field별 전달값 처리 protected function getFormData_process(string $field, array $formDatas): array { return $formDatas; } protected function doValidation_process(Validation $validation, string $field, string $rule): Validation { switch ($field) { default: $validation->setRule($field, $field, $rule); break; } return $validation; } //Process Result처리 protected function getResultFail(string $message = MESSAGES["FAILED"]): RedirectResponse { LogCollector::debug($message); $this->getMyLogService()->save($this->getService()->getClassName(), $this->getAction(), $message, $this->getMyAuth()->getUIDByAuthInfo()); if ($this->request->getMethod() === 'POST') { return redirect()->back()->withInput()->with('error', $message); } return redirect()->to($this->getMyAuth()->popPreviousUrl())->with('error', $message); } protected function getResultSuccess(string $message = MESSAGES["SUCCESS"], ?string $actionTemplate = null): RedirectResponse|string { helper(['form']); switch ($this->getControlDatas('action')) { case 'create': case 'modify': $this->getMyLogService()->save($this->getService()->getClassName(), $this->getAction(), $message, $this->getMyAuth()->getUIDByAuthInfo()); $result = $this->view($this->entity->getPK()); break; case 'create_form': case 'modify_form': case 'login_form': case 'view': case 'index': case 'download': $this->control = $this->getControlDatas(); $this->getHelper()->setViewDatas($this->getViewDatas()); $actionTemplate = $this->request->getVar('ActionTemplate') ?? $actionTemplate; if ($actionTemplate) { $view_file = $this->view_path . $actionTemplate . DIRECTORY_SEPARATOR . $this->getAction(); } else { $view_file = $this->view_path . $this->getAction(); } $result = view($view_file, ['viewDatas' => $this->getViewDatas()]); break; default: $result = redirect()->to($this->getMyAuth()->popPreviousUrl())->with('error', $message); break; } return $result; } //Index,FieldForm관련 //생성관련 protected function create_form_process(): void {} protected function create_process(array $formDatas): void { //데이터 검증 $validDatas = $this->doValidate($this->getControlDatas('field_rules'), $formDatas); $this->entity = $this->getService()->create($validDatas); } //수정관련 protected function modify_form_process(mixed $entity): void { $this->entity = $entity; } protected function modify_process(mixed $entity, array $formDatas): void { //데이터 검증 $validDatas = $this->doValidate($this->getControlDatas('field_rules'), $formDatas); $this->entity = $this->getService()->modify($entity, $validDatas); } //단일필드작업기능 protected function toggle_process(mixed $entity, array $formDatas): void { //데이터 검증 $validDatas = $this->doValidate($this->getControlDatas('field_rules'), $formDatas); $this->entity = $this->getService()->modify($entity, $validDatas); } //일괄처리작업기능 protected function batchjob_process(string|int $uid, array $formDatas, array $entities = []): array { //기존 Entity 가져오기 $entity = $this->getService()->getEntity($uid); if (!$entity) { LogCollector::debug(__METHOD__ . "에서 {$uid}에 대한 정보를 찾을수 없습니다."); } else { //데이터 검증 $validDatas = $this->doValidate($this->getControlDatas('field_rules'), $formDatas); $entities[] = $this->getService()->modify($entity, $validDatas); } return $entities; } //삭제관련 protected function delete_process(mixed $entity): void { $this->entity = $this->getService()->delete($entity); } //일괄삭제관련 protected function batchjob_delete_process(string|int $uid, array $entities = []): array { //기존 Entity 가져오기 $entity = $this->getService()->getEntity($uid); if (!$entity) { LogCollector::debug(__METHOD__ . "에서 {$uid}에 대한 정보를 찾을수 없습니다."); } else { $entities[] = $this->getService()->delete($entity); } return $entities; } //View 관련 protected function view_process(mixed $entity): void { $this->entity = $entity; } //리스트관련 //조건절 처리 //Filter Field별 전달값 처리 protected function index_condition_filterfield_process(string $field, $filter_values = []): array { switch ($field) { default: $filter_value = $this->request->getVar($field); if ($filter_value !== null && $filter_value !== '') { $this->getService()->index_condition_filterField($field, $filter_value); $filter_values[$field] = $filter_value; } break; } return $filter_values; } protected function index_condition_process(): void { //Paramter로 전달된 Filter값을 정의용 $filter_values = []; foreach ($this->_control['actionFilters'] as $field) { $filter_values = $this->index_condition_filterfield_process($field, $filter_values); } $this->_control['filter_values'] = $filter_values; //검색어조건절 처리 $this->word = $this->request->getVar('word'); if ($this->word !== null && $this->word !== '') { $this->getService()->index_condition_filterWord($this->word); } //날자검색 $this->start = $this->request->getVar('start'); $this->end = $this->request->getVar('end'); if ($this->start !== null && $this->start !== '' && $this->end !== null && $this->end !== '') { $this->getService()->index_condition_filterDate($this->start, $this->end); } } //PageNation 처리 protected function index_pagenation_process($pager_group = 'default', int $segment = 0, $template = 'bootstrap_full') { //Page, Per_page필요부분 $this->page = (int) $this->request->getVar('page') ?: 1; $this->per_page = (int) $this->request->getVar('per_page') ?: intval(DEFAULT_LIST_PERPAGE ?? 20); // 1.Views/Pagers/에 bootstrap_full.php,bootstrap_simple.php 생성 // 2.app/Config/Pager.php/$templates에 'bootstrap_full => 'Pagers\bootstrap_full', // 'bootstrap_simple' => 'Pagers\bootstrap_simple', 추가 $pager = service("pager"); $pager->makeLinks($this->page, $this->per_page, $this->total_count, $template, $segment, $pager_group); $this->page = $pager->getCurrentPage($pager_group); $this->total_page = $pager->getPageCount($pager_group); return $pager->links($pager_group, $template); } //Page출력 처리 protected function index_pageOptions_process(): array { $page_options = ["" => "줄수선택"]; for ($i = $this->per_page; $i <= $this->total_count; $i += $this->per_page) { $page_options[$i] = $i; } $page_options[$this->total_count] = $this->total_count; return $page_options; } //Entities처리 protected function index_process(array $entities = []): array { foreach ($this->getService()->getEntities() as $entity) { $entities[] = $entity; } return $entities; } public function index(): RedirectResponse|string { try { //각 Field 초기화 $this->setAction(__FUNCTION__); $this->initAction(); helper(['form']); //Return Url정의 $this->getMyAuth()->pushCurrentUrl($this->request->getUri()->getPath() . ($this->request->getUri()->getQuery() ? "?" . $this->request->getUri()->getQuery() : "")); //조건절 처리 $this->index_condition_process(); //TotalCount (SoftDelete적용이 되려면 countAllResults를 사용해야함) $this->total_count = $this->getService()->getTotalCount(); //Pagination 처리 $this->pagination = $this->index_pagenation_process(); //줄수 처리용 $this->page_options = $this->index_pageOptions_process(); //조건절 처리 //OrcerBy , Limit 처리 $this->order_field = $this->request->getVar('order_field'); $this->order_value = $this->request->getVar('order_value'); $this->getService()->setOrderBy($this->order_field, $this->order_value); $this->getService()->setLimit($this->per_page); $this->getService()->setOffset(($this->page - 1) * $this->per_page); $this->index_condition_process(); $this->entities = $this->index_process(); return $this->getResultSuccess(); } catch (\Exception $e) { return $e->getMessage(); // return $this->getResultFail($e->getMessage()); } } }