myauth = service('myauth'); } 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 public function getViewDatas(): array { return $this->_viewDatas; } protected function setValidation(string $action, string $field, Validation $validation): Validation { switch ($field) { default: $validation->setRule($field, $field, $this->getService()->getModel()->getFieldRule($action, $field)); break; } return $validation; } final protected function getValidation(string $action, array $fields): Validation { $validation = service('validation'); foreach ($fields as $field) { $validation = $this->setValidation($action, $field, $validation); } return $validation; } //Field별 Form Option용 protected function getFormFieldOption(string $field, array $options): array { switch ($field) { default: $options[$field] = lang($this->getService()->getClassPath() . '.' . strtoupper($field)); // dd($options); break; } // dd($options); return $options; } final protected function getFormFieldOptions(array $fields, array $options = []): array { foreach ($fields as $field) { if (is_array($field)) { throw new \Exception(__FUNCTION__ . "에서 field array 입니다.\n" . var_export($field, true)); } $options = $this->getFormFieldOption($field, $options); } // dd($options); return $options; } //Field관련 protected function init(string $action, array $fields = []): void { $this->action = $action; $this->fields = array_key_exists('fields', $fields) && is_array($fields['fields']) && count($fields['fields']) ? $fields['fields'] : $this->getService()->getModel()->getFields(); $this->field_rules = array_key_exists('field_rules', $fields) && is_array($fields['field_rules']) && count($fields['field_rules']) ? $fields['field_rules'] : $this->getService()->getModel()->getFieldRules($this->action, $this->fields); $this->filter_fields = array_key_exists('filter_fields', $fields) && is_array($fields['filter_fields']) && count($fields['filter_fields']) ? $fields['filter_fields'] : $this->getService()->getModel()->getFilterFields(); $this->field_options = array_key_exists('field_options', $fields) && is_array($fields['field_optionss']) && count($fields['field_options']) ? $fields['field_options'] : $this->getFormFieldOptions($this->filter_fields); $this->batchjob_fields = array_key_exists('batchjob_fields', $fields) && is_array($fields['batchjob_fields']) && count($fields['batchjob_fields']) ? $fields['filter_fields'] : $this->getService()->getModel()->getBatchJobFields(); } // 생성 protected function create_init(string $action, $fields = []): void { $this->init($action, $fields); } protected function create_form_process(): void { } final public function create_form(): RedirectResponse|string { $this->create_init('create'); return $this->create_form_procedure(); } protected function create_form_process_result(): string { return view( $this->view_path . "create", ['viewDatas' => $this->getViewDatas()] ); } final protected function create_form_procedure(): RedirectResponse|string { try { helper(['form']); $this->create_form_process(); $this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []]; return $this->create_form_process_result(); } catch (\Exception $e) { return redirect()->back()->with('error', $e->getMessage()); } } protected function create_validate(string $action, array $fields): array { //변경할 값 확인 : Upload된 파일 검증시 $this->request->getPOST()보다 먼처 체크필요 $validation = $this->getValidation($action, $fields); if (!$validation->withRequest($this->request)->run()) { throw new \Exception("{$this->getService()->getClassName()} 작업 데이터 검증 오류발생\n" . implode( "\n", $validation->getErrors() )); } return $validation->getValidated(); } protected function create_process(): void { $this->formDatas = $this->create_validate($this->action, $this->fields); $this->entity = $this->getService()->create($this->formDatas); } protected function create_process_result($message): RedirectResponse|string { $url = strtolower(base_url() . $this->uri_path . $this->getService()->getClassName()) . "/view/" . $this->entity->getPK(); return redirect()->to($url)->with('error', $message); } protected function create_process_failed($message): RedirectResponse|string { return redirect()->back()->withInput()->with('error', $message); } final protected function create_procedure(): RedirectResponse|string { //Transaction Start $this->getService()->getModel()->transStart(); try { helper(['form']); $this->create_process(); $this->getService()->getModel()->transCommit(); return $this->create_process_result(MESSAGES["SUCCESS"]); } catch (\Exception $e) { //Transaction Rollback $this->getService()->getModel()->transRollback(); return $this->create_process_result($e->getMessage()); } } final public function create(): RedirectResponse|string { $this->create_init(__FUNCTION__); return $this->create_procedure(); } //수정관련 protected function modify_init(string $action, $fields = []): void { $this->init($action, $fields); } final public function modify_form(mixed $uid): RedirectResponse|string { $this->modify_init('modify'); return $this->modify_form_procedure($uid); } protected function modify_form_process(mixed $uid): void { $this->entity = $this->getService()->getModel()->getEntityByPK($uid); if ($this->entity === null) { throw new \Exception("해당 정보를 찾을수 없습니다."); } } protected function modify_form_process_result(): string { return view( $this->view_path . "modify", ['viewDatas' => $this->getViewDatas()] ); } final protected function modify_form_procedure(mixed $uid): RedirectResponse|string { try { helper(['form']); $this->modify_form_process($uid); $this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []]; return $this->modify_form_process_result(); } catch (\Exception $e) { return redirect()->back()->with('error', $e->getMessage()); } } final protected function modify_validate(string $action, array $fields): array { //변경할 값 확인 : Upload된 파일 검증시 $this->request->getVar()보다 먼처 체크필요 $validation = $this->getValidation($action, $fields); if (!$validation->withRequest($this->request)->run()) { throw new \Exception("{$this->getService()->getClassName()} 작업 데이터 검증 오류발생\n" . implode( "\n", $validation->getErrors() )); } return $validation->getValidated(); } //modify,toggle,batchjob 공통사용 protected function modify_process(mixed $uid): void { $this->formDatas = $this->modify_validate($this->action, $this->fields); //자신정보정의 $this->entity = $this->getService()->getModel()->getEntityByPK($uid); if ($this->entity === null) { throw new \Exception(__FUNCTION__ . " => {$uid} 정보를 찾을수 없습니다."); } $this->entity = $this->getService()->modify($this->entity, $this->formDatas); } protected function modify_process_result($message): RedirectResponse|string { $url = strtolower(base_url() . $this->uri_path . $this->getService()->getClassName()) . "/view/" . $this->entity->getPK(); return redirect()->to($url)->with('error', $message); } protected function modify_process_failed($message): RedirectResponse|string { return redirect()->back()->withInput()->with('error', $message); } final protected function modify_procedure(mixed $uid): RedirectResponse|string { //Transaction Start $this->getService()->getModel()->transStart(); try { helper(['form']); $this->modify_process($uid); $this->getService()->getModel()->transCommit(); return $this->modify_process_result(MESSAGES["SUCCESS"]); } catch (\Exception $e) { //Transaction Rollback $this->getService()->getModel()->transRollback(); return $this->modify_process_result($e->getMessage()); } } final public function modify(int $uid): RedirectResponse|string { $this->modify_init(__FUNCTION__); return $this->modify_procedure($uid); } //단일필드작업 protected function toggle_process_result($message): RedirectResponse|string { return redirect()->to($this->myauth->popPreviousUrl())->with('error', $message); } protected function toggle_process_failed($message): RedirectResponse|string { return redirect()->back()->with('error', $message); } final protected function toggle_procedure(mixed $uid, string $field): RedirectResponse { //Transaction Start $this->getService()->getModel()->transStart(); try { $this->action = __FUNCTION__; $this->fields = [$field]; $this->modify_process($uid); $this->getService()->getModel()->transCommit(); return $this->toggle_process_result(MESSAGES["SUCCESS"]); } catch (\Exception $e) { //Transaction Rollback $this->getService()->getModel()->transRollback(); return $this->toggle_process_failed($e->getMessage()); } } final public function toggle(mixed $uid, string $field): RedirectResponse|string { return $this->toggle_procedure($uid, $field); } //일괄처리작업 protected function batchjob_process_result($message): RedirectResponse|string { return redirect()->to($this->myauth->popPreviousUrl())->with('error', $message); } protected function batchjob_process_failed($message): RedirectResponse|string { return redirect()->back()->with('error', $message); } final protected function batchjob_procedure(): RedirectResponse { //Transaction Start $this->getService()->getModel()->transStart(); try { //데이터가 있는경우 Field만 처리하기위해 $fields = []; foreach ($this->batchjob_fields as $field) { if ($this->request->getVar($field)) { $fields[$field] = $field; } } $this->fields = $fields; //변경할 UIDS $uids = $this->request->getVar('batchjob_uids'); if (!$uids) { throw new \Exception("적용할 리스트를 선택하셔야합니다."); } foreach (explode(",", $uids) as $uid) { $this->modify_process($uid); } $this->getService()->getModel()->transCommit(); return $this->batchjob_process_result(MESSAGES["SUCCESS"]); } catch (\Exception $e) { //Transaction Rollback $this->getService()->getModel()->transRollback(); return $this->batchjob_process_failed($e->getMessage()); } } //일괄처리작업 final public function batchjob(): RedirectResponse { $this->init(__FUNCTION__); return $this->batchjob_procedure(); } //삭제 delete,batchjob_delete 공통사용 protected function delete_process_result($message): RedirectResponse|string { return redirect()->to($this->myauth->popPreviousUrl())->with('error', $message); } protected function delete_process_failed($message): RedirectResponse|string { return redirect()->back()->with('error', $message); } protected function delete_process(mixed $uid): void { //자신정보정의 $this->entity = $this->getService()->getModel()->getEntityByPK($uid); if ($this->entity === null) { throw new \Exception("{$uid} 정보를 찾을수 없습니다."); } $this->entity = $this->getService()->delete($this->entity); } final protected function delete_procedure(mixed $uid): RedirectResponse|string { //Transaction Start $this->getService()->getModel()->transStart(); try { $this->delete_process($uid); $this->getService()->getModel()->transCommit(); return $this->delete_process_result(MESSAGES["SUCCESS"]); } catch (\Exception $e) { //Transaction Rollback $this->getService()->getModel()->transRollback(); return $this->delete_process_failed($e->getMessage()); } } final public function delete(mixed $uid): RedirectResponse|string { return $this->delete_procedure($uid); } //일괄삭제 protected function batchjob_delete_process_result($message): RedirectResponse|string { return redirect()->to($this->myauth->popPreviousUrl())->with('error', $message); } protected function batchjob_delete_process_failed($message): RedirectResponse|string { return redirect()->back()->with('error', $message); } final protected function batchjob_delete_procedure(): RedirectResponse|string { //Transaction Start $this->getService()->getModel()->transStart(); try { //변경할 UIDS $uids = $this->request->getVar('batchjob_uids'); if (!$uids) { throw new \Exception("적용할 리스트를 선택하셔야합니다."); } foreach (explode(",", $uids) as $uid) { $this->delete_process($uid); } $this->getService()->getModel()->transCommit(); return $this->batchjob_delete_process_result(MESSAGES["SUCCESS"]); } catch (\Exception $e) { //Transaction Rollback $this->getService()->getModel()->transRollback(); return $this->batchjob_delete_process_failed($e->getMessage()); } } final public function batchjob_delete(): RedirectResponse|string { return $this->batchjob_delete_procedure(); } //View protected function view_init(string $action, $fields = []): void { $this->init($action, $fields); } protected function view_process(mixed $uid): void { //자신정보정의 $this->entity = $this->getService()->getModel()->getEntityByPK($uid); if ($this->entity === null) { throw new \Exception("해당 정보를 찾을수 없습니다."); } } protected function view_process_result(): string { helper(['form']); $this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []]; return view( $this->view_path . "view", ['viewDatas' => $this->getViewDatas()] ); } protected function view_process_failed($message): RedirectResponse|string { return redirect()->back()->with('error', $message); } final protected function view_procedure(mixed $uid): RedirectResponse|string { try { $this->view_process($uid); return $this->view_process_result(); } catch (\Exception $e) { return $this->view_process_failed($e->getMessage()); } } final public function view(string $uid): RedirectResponse|string { $this->view_init(__FUNCTION__); return $this->view_procedure($uid); } // 리스트 protected function index_init(string $action, $fields = []): void { $this->init($action, $fields); } protected function index_condition_process(): void { //조건절 처리 foreach ($this->filter_fields as $field) { $this->$field = $this->request->getVar($field) ?: DEFAULTS['EMPTY']; if ($this->$field !== DEFAULTS['EMPTY']) { $this->getService()->getModel()->setList_FieldFilter($field, $this->$field); } } //검색어 처리 $this->word = $this->request->getVar('word') ?: DEFAULTS['EMPTY']; if ($this->word !== DEFAULTS['EMPTY']) { $this->getService()->getModel()->setList_WordFilter($this->word); } //검색일 처리 $this->start = $this->request->getVar('start') ?: DEFAULTS['EMPTY']; $this->end = $this->request->getVar('end') ?: DEFAULTS['EMPTY']; $this->getService()->getModel()->setList_DateFilter($this->start, $this->end); } //Totalcount 처리 private function index_total_process(): int { $this->index_condition_process(); $total_count = $this->getService()->getModel()->countAllResults(); // log_message("debug", $this->getService()->getModel()->getLastQuery()); return $total_count; } //PageNation 처리 private function index_pagination_process($pager_group = 'default', int $segment = 0, $template = 'bootstrap_full'): string { //Page, Per_page필요부분 $this->page = (int) $this->request->getVar('page') ?: 1; $this->per_page = (int) $this->request->getVar('per_page') ?: intval(env("mvc.default.list.per_page") ?? 10); //줄수 처리용 $page_options = array("" => "줄수선택"); 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; $this->page_options = $page_options; // 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"); // $this->getService()->getModel()->paginate($this->per_page, $pager_group, $this->page, $segment); $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); } protected function index_entitys_process_orderBy(): void { $this->order_field = $this->request->getVar('order_field') ?: DEFAULTS['EMPTY']; $this->order_value = $this->request->getVar('order_value') ?: DEFAULTS['EMPTY']; if ($this->order_field !== DEFAULTS['EMPTY'] && $this->order_value !== DEFAULTS['EMPTY']) { $this->getService()->getModel()->orderBy(sprintf( "%s.%s %s", $this->getService()->getModel()::TABLE, $this->order_field, $this->order_value )); } else { $this->getService()->getModel()->orderBy(sprintf( "%s.%s %s", $this->getService()->getModel()::TABLE, $this->getService()->getModel()::PK, "DESC" )); } } protected function index_entitys_process_limit(): void { $this->getService()->getModel()->limit($this->per_page, $this->page * $this->per_page - $this->per_page); } protected function index_entitys_process(): array { $this->index_condition_process(); //Sorting 처리 $this->index_entitys_process_orderBy(); //Limit관련 $this->index_entitys_process_limit(); $entitys = $this->getService()->getModel()->select($this->getService()->getModel()::TABLE . '.*')->findAll(); // log_message("debug", $this->getService()->getModel()->getLastQuery()); return $entitys; } protected function index_process_result(): string { return view( $this->view_path . "index", ['viewDatas' => $this->getViewDatas()] ); } final protected function index_procedure(): string { try { helper(['form']); //URL처리 $this->uri = $this->request->getUri(); //total 처리 $this->total_count = $this->index_total_process(); //pagenation 처리 $this->pagination = $this->index_pagination_process(); //모델 처리 $this->entitys = $this->index_entitys_process(); // 현재 URL을 스택에 저장 $this->myauth->pushCurrentUrl($this->request->getUri()->getPath() . ($this->request->getUri()->getQuery() ? "?" . $this->request->getUri()->getQuery() : "")); return $this->index_process_result(); } catch (\Exception $e) { return $this->helper->alert($e->getMessage()); } } public function index(): string { $this->index_init(__FUNCTION__); return $this->index_procedure(); } //OUPUT Document 관련 private function output_save_process(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; default: if (!is_file($full_path)) { throw new \Exception("첨부파일이 확인되지 않습니다.\n"); } // //oupuput directly // header("Content-Type: application/vnd.ms-excel"); // header(sprintf("Content-Disposition: attachment; filename=%s", urlencode($output_name))); // header("Expires: 0"); // header("Cache-Control: must-revalidate"); // header("Pragma: public"); // header("Content-Length:" . filesize($full_path)); // return $writer->save('php://output'); break; } return array($full_path, $file_name); } //File Download관련 final protected function download_procedure(string $output_type, mixed $uid = false): DownloadResponse|RedirectResponse { try { helper(['form']); //URL처리 $this->uri = $this->request->getUri(); switch ($output_type) { case 'excel': case 'pdf': // string buffer에서 읽어오는 경우 $this->entitys = $this->index_entitys_process(); $html = view( 'templates' . DIRECTORY_SEPARATOR . $this->action, ['viewDatas' => $this->getViewDatas()] ); //data loading $reader = new Html(); $loaded_data = $reader->loadFromString($html); list($full_path, $file_name) = $this->output_save_process($output_type, $loaded_data); $full_path .= DIRECTORY_SEPARATOR . $file_name; break; default: if (!$uid) { throw new \Exception("{$output_type}은 반드시 uid의 값이 필요합니다."); } $this->entity = $this->getService()->getModel()->getEntityByPK($uid); if ($this->entity === null) { throw new \Exception("{$uid} 정보를 찾을수 없습니다."); } 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->helper->alert($e->getMessage()); } } // Download final public function download(string $output_type, mixed $uid = false): DownloadResponse|string { $this->init(__FUNCTION__); return $this->download_procedure($output_type, $uid); } }