671 lines
26 KiB
PHP
671 lines
26 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Entities\CommonEntity;
|
|
use App\Controllers\BaseController;
|
|
use App\Libraries\AuthContext;
|
|
use App\Traits\LogTrait;
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use CodeIgniter\Validation\Exceptions\ValidationException;
|
|
use CodeIgniter\HTTP\DownloadResponse;
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
use PhpOffice\PhpSpreadsheet\Reader\Html;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
abstract class CommonController extends BaseController
|
|
{
|
|
use LogTrait;
|
|
use ResponseTrait;
|
|
protected $request;
|
|
|
|
private array $_action_paths = [];
|
|
private array $_viewDatas = [];
|
|
protected $service = null;
|
|
private ?string $_title = null;
|
|
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
parent::initController($request, $response, $logger);
|
|
}
|
|
|
|
final protected function getAuthContext(): AuthContext
|
|
{
|
|
return service('myauth')->getAuthContext();
|
|
}
|
|
|
|
final protected function addActionPaths(string $path)
|
|
{
|
|
$this->_action_paths[] = $path;
|
|
}
|
|
|
|
final protected function getActionPaths($isArray = true, $delimeter = DIRECTORY_SEPARATOR): array|string
|
|
{
|
|
return $isArray ? $this->_action_paths : implode($delimeter, $this->_action_paths);
|
|
}
|
|
|
|
final protected function addViewDatas(string $key, mixed $value)
|
|
{
|
|
$this->_viewDatas[$key] = $value;
|
|
}
|
|
|
|
final protected function getViewDatas(?string $key = null): mixed
|
|
{
|
|
if ($key === null) {
|
|
return $this->_viewDatas;
|
|
}
|
|
return $this->_viewDatas[$key] ?? null;
|
|
}
|
|
|
|
protected function getTitle(): string
|
|
{
|
|
if ($this->_title === null) {
|
|
$this->_title = lang("{$this->service->getClassPaths(false)}.title");
|
|
}
|
|
return $this->_title;
|
|
}
|
|
|
|
//공통 필수기능
|
|
//필수함수
|
|
//사용자정의 함수
|
|
protected function action_init_process(string $action): void
|
|
{
|
|
$this->addViewDatas('action', $action);
|
|
$this->addViewDatas('authContext', $this->getAuthContext());
|
|
$this->addViewDatas('classPath', $this->service->getClassPaths(false));
|
|
}
|
|
|
|
protected function action_modal_process(string $message): string
|
|
{
|
|
return "
|
|
<script>
|
|
// IMPORTANT: The original alert() should be replaced with a custom modal/toast in production environment.
|
|
// alert('" . addslashes($message) . "');
|
|
|
|
// Bootstrap 5 모달 닫기
|
|
var modalEl = document.getElementById('modal_action_form');
|
|
if (modalEl) {
|
|
var modal = bootstrap.Modal.getInstance(modalEl);
|
|
if (!modal) {
|
|
modal = new bootstrap.Modal(modalEl);
|
|
}
|
|
modal.hide();
|
|
}
|
|
|
|
// 모달 닫힌 후 부모 페이지 새로고침 (애니메이션 고려)
|
|
setTimeout(function() {
|
|
if (window.parent) {
|
|
window.parent.location.reload();
|
|
} else if (window.opener && !window.opener.closed) {
|
|
window.opener.location.reload();
|
|
}
|
|
}, 500);
|
|
</script>
|
|
";
|
|
}
|
|
|
|
protected function action_redirect_process(string $type, string $message, ?string $redirect_url = null): RedirectResponse
|
|
{
|
|
switch ($type) {
|
|
case 'warning':
|
|
case 'error':
|
|
case 'critical':
|
|
case 'alert':
|
|
case 'emergency':
|
|
log_message($type, $message);
|
|
$result = $redirect_url ? $result = redirect()->to($redirect_url)->with('message', $message) : redirect()->back()->withInput()->with('message', $message);
|
|
break;
|
|
case 'debug':
|
|
case 'info':
|
|
case 'notice':
|
|
default:
|
|
log_message($type, $message);
|
|
$redirect_url = $redirect_url ?? $this->getAuthContext()->popPreviousUrl() ?? implode(DIRECTORY_SEPARATOR, $this->getActionPaths());
|
|
$result = redirect()->to($redirect_url)->with('message', $message);
|
|
break;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
protected function action_render_process(array $view_paths, string $view_file, array $viewDatas): string
|
|
{
|
|
$lastest_path = array_pop($view_paths); //paths는 마지막을 뺀 앞단까지만 남음
|
|
|
|
$full_path = implode(DIRECTORY_SEPARATOR, $view_paths);
|
|
|
|
// GET/POST/JSON 데이터에서 'ActionTemplate' 파라미터를 가져옵니다.
|
|
$final_path = $this->request->getVar('ActionTemplate');
|
|
|
|
if ($final_path) {
|
|
$full_path .= DIRECTORY_SEPARATOR . $final_path;
|
|
}
|
|
|
|
$view_datas = [
|
|
...$viewDatas,
|
|
'forms' => ['attributes' => ['method' => "post",], 'hiddens' => []],
|
|
];
|
|
|
|
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);
|
|
$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());
|
|
}
|
|
}
|
|
|
|
protected function create_process(): CommonEntity
|
|
{
|
|
// POST 데이터를 DTO 객체로 변환 (getPost()는 POST 요청 본문만 가져옵니다.)
|
|
$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__);
|
|
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) {
|
|
throw new \Exception("{$this->getTitle()}에 번호가 정의 되지 않았습니다.");
|
|
}
|
|
$entity = $this->service->getEntity($uid);
|
|
if (!$entity instanceof CommonEntity) {
|
|
throw new \Exception("{$uid}에 해당하는 {$this->getTitle()}을 찾을수 없습니다.");
|
|
}
|
|
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);
|
|
$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());
|
|
}
|
|
}
|
|
|
|
protected function modify_process($uid): CommonEntity
|
|
{
|
|
// POST 데이터를 DTO 객체로 변환
|
|
$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__);
|
|
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
|
|
{
|
|
// POST 데이터 전체를 가져옵니다.
|
|
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 {
|
|
//사전작업
|
|
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 = [];
|
|
$errors = [];
|
|
foreach ($uids as $uid) {
|
|
try {
|
|
$entities[] = $this->batchjob_process($uid, $formDatas);
|
|
} catch (ValidationException $e) {
|
|
log_message('error', "{$this->getTitle()}에서 {$uid} 수정 검증오류:" . $e->getMessage());
|
|
$errors[] = $e->getMessage();
|
|
} catch (\Exception $e) {
|
|
log_message('error', "{$this->getTitle()}에서 {$uid} 수정 오류:" . $e->getMessage());
|
|
$errors[] = $e->getMessage();
|
|
}
|
|
}
|
|
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 {
|
|
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
|
|
{
|
|
// POST 데이터 전체를 가져옵니다.
|
|
$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->delete($uid);
|
|
}
|
|
|
|
final public function batchjob_delete(): string|RedirectResponse
|
|
{
|
|
try {
|
|
$uids = $this->batchjob_delete_pre_process();
|
|
$entities = [];
|
|
$errors = [];
|
|
foreach ($uids as $uid) {
|
|
try {
|
|
$entities[] = $this->batchjob_delete_process($uid);
|
|
} catch (\Exception $e) {
|
|
log_message('error', "{$this->getTitle()}에서 {$uid} 삭제 오류:" . $e->getMessage());
|
|
$errors[] = $e->getMessage();
|
|
}
|
|
}
|
|
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) {
|
|
throw new \Exception("{$this->getTitle()}에 번호가 정의 되지 않았습니다.");
|
|
}
|
|
$entity = $this->service->getEntity($uid);
|
|
if (!$entity instanceof CommonEntity) {
|
|
throw new \Exception("{$uid}에 해당하는 {$this->getTitle()}을 찾을수 없습니다.");
|
|
}
|
|
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);
|
|
$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());
|
|
}
|
|
}
|
|
|
|
//리스트관련
|
|
//조건절 처리
|
|
protected function index_condition_process(string $action): void
|
|
{
|
|
//Filter조건절 처리
|
|
$index_filters = [];
|
|
foreach ($this->service->getFormService()->getFormFilters($action) as $field) {
|
|
// getVar()를 사용하여 GET, POST, JSON 입력을 통합하여 가져옵니다.
|
|
$value = $this->request->getVar($field) ?? null;
|
|
if ($value) {
|
|
$this->service->setFilter($field, $value);
|
|
$index_filters[$field] = $value;
|
|
}
|
|
}
|
|
$this->addViewDatas('index_filters', $index_filters);
|
|
|
|
//검색어조건절 처리
|
|
$index_word = $this->request->getVar('index_word');
|
|
if ($index_word !== null && $index_word !== '') {
|
|
$this->service->setSearchWord($index_word);
|
|
}
|
|
$this->addViewDatas('index_word', $index_word);
|
|
|
|
//날자검색
|
|
$index_start = $this->request->getVar('index_start');
|
|
$index_end = $this->request->getVar('index_end');
|
|
if ($index_start !== null && $index_start !== '' && $index_end !== null && $index_end !== '') {
|
|
$this->service->setDateFilter($index_start, $index_end);
|
|
}
|
|
$this->addViewDatas('index_start', $index_start);
|
|
$this->addViewDatas('index_end', $index_end);
|
|
|
|
//OrcerBy처리
|
|
$order_field = $this->request->getVar('order_field');
|
|
$order_value = $this->request->getVar('order_value');
|
|
$this->service->setOrderBy($order_field, $order_value);
|
|
$this->addViewDatas('order_field', $order_field);
|
|
$this->addViewDatas('order_value', $order_value);
|
|
}
|
|
|
|
//Index Option출력용
|
|
protected function pagenation_options_process(int $index_totalcount, int $perpage): array
|
|
{
|
|
$page_options = ["" => "줄수선택"];
|
|
for ($i = $perpage; $i <= $index_totalcount; $i += $perpage) {
|
|
$page_options[$i] = $i;
|
|
}
|
|
$page_options[$index_totalcount] = $index_totalcount;
|
|
return $page_options;
|
|
}
|
|
|
|
//PageNation 처리
|
|
protected function pagenation_process(int $index_totalcount, int $page, int $perpage, $pager_group = 'default', int $segment = 0, $template = 'bootstrap_full'): mixed
|
|
{
|
|
// 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($page, $perpage, $index_totalcount, $template, $segment, $pager_group);
|
|
// $page = $pager->getCurrentPage($pager_group);
|
|
$this->addViewDatas('index_totalpage', $pager->getPageCount($pager_group));
|
|
return $pager->links($pager_group, $template);
|
|
}
|
|
|
|
//Entities처리 (하위 클래스에서 오버라이드 가능)
|
|
protected function index_process(array $entities = []): array
|
|
{
|
|
foreach ($this->service->getEntities() as $entity) {
|
|
$entities[] = $entity;
|
|
}
|
|
return $entities;
|
|
}
|
|
|
|
// HTML View 출력 처리 (기존 로직)
|
|
protected function index_result_process(string $action): string
|
|
{
|
|
return $this->action_render_process($this->getActionPaths(), $action, $this->getViewDatas());
|
|
}
|
|
|
|
protected function index_api_result_process(string $action): ResponseInterface
|
|
{
|
|
$viewDatas = $this->getViewDatas();
|
|
|
|
// 필요한 핵심 데이터만 추출
|
|
$data = [
|
|
'entities' => $viewDatas['entities'] ?? [],
|
|
'total_count' => $viewDatas['index_totalcount'] ?? 0,
|
|
'page' => $viewDatas['page'] ?? 1,
|
|
'perpage' => $viewDatas['perpage'] ?? (DEFAULTS['INDEX_PERPAGE'] ?? 10), // DEFAULTS 확인 필요
|
|
'filters' => $viewDatas['index_filters'] ?? [],
|
|
'index_word' => $viewDatas['index_word'] ?? null,
|
|
'index_start' => $viewDatas['index_start'] ?? null,
|
|
'index_end' => $viewDatas['index_end'] ?? null,
|
|
];
|
|
|
|
// CodeIgniter의 ResponseTrait의 respond() 메서드를 사용하여 JSON으로 응답
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => "{$this->getTitle()} 목록 API 조회 성공",
|
|
'data' => $data,
|
|
]);
|
|
}
|
|
|
|
public function index(): string|ResponseInterface
|
|
{
|
|
$action = __FUNCTION__;
|
|
|
|
// API 요청 여부를 확인합니다.
|
|
// is_api 또는 RequestTemplate 파라미터를 getVar()로 통합하여 확인합니다.
|
|
$isApiRequest = $this->request->getVar('is_api') === '1' ||
|
|
$this->request->getVar('RequestTemplate') === 'api';
|
|
|
|
try {
|
|
//초기화
|
|
$this->action_init_process($action);
|
|
$this->addViewDatas('uri', $this->request->getUri());
|
|
|
|
// Page, Per_page 설정
|
|
$page = (int) $this->request->getVar('page') ?: 1;
|
|
$perpage = (int) $this->request->getVar('perpage') ?: intval(DEFAULTS['INDEX_PERPAGE'] ?? 10);
|
|
$this->addViewDatas('page', $page);
|
|
$this->addViewDatas('perpage', $perpage);
|
|
|
|
// index_totalcount를 위한 조건절 처리 (오버라이드 가능)
|
|
$this->index_condition_process($action);
|
|
$index_totalcount = $this->service->getTotalCount();
|
|
$this->addViewDatas('index_totalcount', $index_totalcount);
|
|
|
|
// API 요청이 아닌 경우에만 HTML 뷰를 위한 페이지네이션 링크와 옵션을 준비
|
|
if (!$isApiRequest) {
|
|
$this->addViewDatas('index_pagination', $this->pagenation_process($index_totalcount, $page, $perpage));
|
|
$this->addViewDatas('index_pagination_options', $this->pagenation_options_process($index_totalcount, $perpage));
|
|
}
|
|
|
|
// 조건절 LIMIT , OFFSET 처리 List용 (기존 로직 유지)
|
|
$this->index_condition_process($action);
|
|
$this->service->setLimit($perpage);
|
|
$this->service->setOffset(($page - 1) * $perpage);
|
|
|
|
// Entities 처리 (API든 View든 공통적으로 필요하며, 하위 클래스에서 오버라이드 가능)
|
|
$this->addViewDatas('entities', $this->index_process());
|
|
|
|
helper(['form']);
|
|
|
|
// 인자 없이 getVar() 호출 시, GET, POST, JSON 등 모든 데이터를 가져오며, 없을 경우 빈 배열을 사용합니다.
|
|
$this->addViewDatas('formDatas', $this->request->getVar() ?? []);
|
|
} catch (\Exception $e) {
|
|
// API 요청인 경우 JSON 오류 메시지를 반환
|
|
if ($isApiRequest) {
|
|
return $this->failServerError($e->getMessage());
|
|
}
|
|
// 일반 요청인 경우 세션 오류 메시지를 설정
|
|
session()->setFlashdata('message', $e->getMessage());
|
|
}
|
|
|
|
// --- 결과 분기 처리 ---
|
|
|
|
if ($isApiRequest) {
|
|
// API 요청에 대한 JSON 렌더링
|
|
return $this->index_api_result_process($action);
|
|
}
|
|
|
|
// 현재 URL을 세션에 저장 (일반 View 요청 시에만)
|
|
$this->getAuthContext()->pushCurrentUrl($this->request->getUri()->getPath() . ($this->request->getUri()->getQuery() ? "?" . $this->request->getUri()->getQuery() : ""));
|
|
|
|
// HTML 뷰 렌더링
|
|
return $this->index_result_process($action);
|
|
}
|
|
|
|
//OUPUT Document 관련
|
|
protected function downloadByDocumentType(string $document_type, mixed $loaded_data): array
|
|
{
|
|
$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'));
|
|
$writer = IOFactory::createWriter($loaded_data, 'Xlsx');
|
|
$writer->save($full_path . DIRECTORY_SEPARATOR . $file_name);
|
|
break;
|
|
case 'pdf':
|
|
$file_name = sprintf("%s_%s.pdf", $this->service->getClassPaths(false, "_"), 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);
|
|
}
|
|
|
|
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
|
|
final public function download(string $output_type, mixed $uid = false): DownloadResponse|RedirectResponse|string
|
|
{
|
|
$action = __FUNCTION__;
|
|
try {
|
|
//초기화
|
|
$this->action_init_process($action);
|
|
return $this->download_process($action, $output_type, $uid);
|
|
} catch (\Exception $e) {
|
|
return $this->action_redirect_process('error', "{$this->getTitle()}에서 오류:" . $e->getMessage());
|
|
}
|
|
}
|
|
}
|