750 lines
31 KiB
PHP
750 lines
31 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Controllers\BaseController;
|
|
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use CodeIgniter\Validation\Validation;
|
|
|
|
use CodeIgniter\HTTP\DownloadResponse;
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
use PhpOffice\PhpSpreadsheet\Reader\Html;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use App\Libraries\LogCollector;
|
|
use App\Services\MyLogService;
|
|
|
|
abstract class CommonController extends BaseController
|
|
{
|
|
private $_myAuth = null;
|
|
private ?MyLogService $_myLogService = null;
|
|
private $_viewDatas = [];
|
|
private $_control = [];
|
|
abstract public function getService(): mixed;
|
|
abstract function getHelper(): mixed;
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
parent::initController($request, $response, $logger);
|
|
$this->isLoggedIn = false;
|
|
$this->uri = $request->getUri();
|
|
if ($this->getMyAuth()->isLoggedIn()) {
|
|
$this->isLoggedIn = true;
|
|
$this->myAuthName = $this->getMyAuth()->getNameByAuthInfo();
|
|
$this->myAuthUID = $this->getMyAuth()->getUIDByAuthInfo();
|
|
}
|
|
}
|
|
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 public function getViewDatas(): array
|
|
{
|
|
return $this->_viewDatas;
|
|
}
|
|
final public function getMyLogService(): mixed
|
|
{
|
|
if (!$this->_myLogService) {
|
|
$this->_myLogService = new MyLogService();
|
|
}
|
|
return $this->_myLogService;
|
|
}
|
|
//Index,FieldForm관련
|
|
final protected function getControlDatas(): array
|
|
{
|
|
return $this->_control;
|
|
}
|
|
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 setFormFields(array $fields): void
|
|
{
|
|
$this->_control['form_fields'] = $fields;
|
|
}
|
|
final protected function getFormFields(): array
|
|
{
|
|
return $this->_control['form_fields'] ?? [];
|
|
}
|
|
final protected function setIndexFields(array $fields): void
|
|
{
|
|
$this->_control['index_fields'] = $fields;
|
|
}
|
|
final protected function getIndexFields(): array
|
|
{
|
|
return $this->_control['index_fields'] ?? [];
|
|
}
|
|
final protected function setViewFields(array $fields): void
|
|
{
|
|
$this->_control['view_fields'] = $fields;
|
|
}
|
|
final protected function getViewFields(): array
|
|
{
|
|
return $this->_control['view_fields'] ?? [];
|
|
}
|
|
final protected function setFilterFields(array $fields): void
|
|
{
|
|
$this->_control['filter_fields'] = $fields;
|
|
}
|
|
final protected function getFilterFields(): array
|
|
{
|
|
return $this->_control['filter_fields'] ?? [];
|
|
}
|
|
final protected function setBatchjobFields(array $fields): void
|
|
{
|
|
$this->_control['batchjob_fields'] = $fields;
|
|
}
|
|
final protected function getBatchjobFields(): array
|
|
{
|
|
return $this->_control['batchjob_fields'] ?? [];
|
|
}
|
|
final protected function setFieldRule(string $field, string $rule): void
|
|
{
|
|
if (!array_key_exists('field_rules', $this->_control)) {
|
|
$this->_control['field_rules'] = [];
|
|
}
|
|
$this->_control['field_rules'][$field] = $rule;
|
|
}
|
|
final protected function getFieldRule(string $field): string
|
|
{
|
|
return $this->_control['field_rules'][$field] ?? [];
|
|
}
|
|
final protected function getFieldRules(): array
|
|
{
|
|
return $this->_control['field_rules'] ?? [];
|
|
}
|
|
final protected function setFilterFieldOption(string $field, array $options): void
|
|
{
|
|
if (!array_key_exists('filter_optons', $this->_control)) {
|
|
$this->_control['filter_optons'] = [];
|
|
}
|
|
$this->_control['filter_optons'][$field] = $options;
|
|
}
|
|
final protected function getFilterFieldOption(string $field): array
|
|
{
|
|
return $this->_control['filter_optons'][$field] ?? [];
|
|
}
|
|
final protected function getFilterFieldOptions(): array
|
|
{
|
|
return $this->_control['filter_optons'] ?? [];
|
|
}
|
|
|
|
protected function initAction(string $action): void
|
|
{ //각 Field 초기화
|
|
$this->setAction($action);
|
|
$this->setFormFields($this->getService()->getFormFields());
|
|
$this->setIndexFields($this->getService()->getIndexFields());
|
|
$this->setViewFields($this->getService()->getViewFields());
|
|
$this->setFilterFields($this->getService()->getFilterFields());
|
|
foreach ($this->getViewFields() as $field) {
|
|
$this->setFieldRule($field, $this->getFormFieldRule($this->getAction(), $field));
|
|
}
|
|
foreach ($this->getFilterFields() as $field) {
|
|
$this->setFilterFieldOption($field, $this->getFormFieldOption($field));
|
|
}
|
|
$this->setBatchJobFields($this->getService()->getBatchJobFields());
|
|
}
|
|
protected function getFormFieldRule(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;
|
|
}
|
|
protected function getFormFieldOption(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 setValidation(Validation $validation, string $field, string $rule): Validation
|
|
{
|
|
switch ($field) {
|
|
default:
|
|
$validation->setRule($field, $field, $rule);
|
|
break;
|
|
}
|
|
return $validation;
|
|
}
|
|
//Field관련
|
|
//데이터 검증
|
|
final protected function doValidate(array $rules, array $formDatas, ?Validation $validation = null): array
|
|
{
|
|
//변경할 값 확인 : Upload된 파일 검증시 $this->request->getPOST()보다 먼처 체크필요
|
|
if (!$validation) {
|
|
$validation = service('validation');
|
|
}
|
|
foreach ($rules as $field => $rule) {
|
|
$validation = $this->setValidation($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();
|
|
}
|
|
final protected function getResultFail(string $message = MESSAGES["FAILED"]): RedirectResponse
|
|
{
|
|
LogCollector::debug($message);
|
|
$this->getMyLogService()->save($this->getService()->getClassName(), __FUNCTION__, $message);
|
|
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
|
|
{
|
|
switch ($this->getAction()) {
|
|
case 'create':
|
|
case 'modify':
|
|
$this->getMyLogService()->save($this->getService()->getClassName(), __FUNCTION__, $message);
|
|
$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 {}
|
|
final public function create_form(): RedirectResponse|string
|
|
{
|
|
try {
|
|
//각 Field 초기화
|
|
// $this->getMyAuth()->pushCurrentUrl($this->request->getUri()->getPath() . ($this->request->getUri()->getQuery() ? "?" . $this->request->getUri()->getQuery() : ""));
|
|
$this->initAction(__FUNCTION__);
|
|
//FieldRule정의
|
|
foreach ($this->getFormFields() as $field) {
|
|
$this->setFieldRule($field, $this->getFormFieldRule($this->getAction(), $field));
|
|
}
|
|
helper(['form']);
|
|
//filter_fields에 해당하는 값이 있을 경우 정의
|
|
foreach ($this->getFilterFields() as $field) {
|
|
$value = $this->request->getVar($field);
|
|
if ($value) {
|
|
$this->$field = $value;
|
|
}
|
|
}
|
|
$this->create_form_process();
|
|
$this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []];
|
|
return $this->getResultSuccess();
|
|
} catch (\Exception $e) {
|
|
return $this->getResultFail($e->getMessage());
|
|
}
|
|
}
|
|
protected function create_process(array $formDatas): mixed
|
|
{
|
|
//데이터 검증
|
|
$formDatas = $this->doValidate($this->getFieldRules(), $formDatas);
|
|
return $this->getService()->create($formDatas);
|
|
}
|
|
final public function create(): RedirectResponse|string
|
|
{
|
|
$this->getService()->getModel()->transStart();
|
|
try {
|
|
//각 Field 초기화
|
|
$this->initAction(__FUNCTION__);
|
|
//FieldRule정의
|
|
foreach ($this->getFormFields() as $field) {
|
|
$this->setFieldRule($field, $this->getFormFieldRule($this->getAction(), $field));
|
|
}
|
|
//입력값정의
|
|
$formDatas = [];
|
|
foreach ($this->getFormFields() as $field) {
|
|
$formDatas[$field] = $this->request->getPost($field);
|
|
}
|
|
// dd($formDatas);
|
|
$this->entity = $this->create_process($formDatas);
|
|
$this->getService()->getModel()->transCommit();
|
|
return $this->getResultSuccess();
|
|
} catch (\Exception $e) {
|
|
$this->getService()->getModel()->transRollback();
|
|
return $this->getResultFail($e->getMessage());
|
|
}
|
|
}
|
|
//수정관련
|
|
protected function modify_form_process(mixed $entity): mixed
|
|
{
|
|
return $entity;
|
|
}
|
|
final public function modify_form(mixed $uid): RedirectResponse|string
|
|
{
|
|
try {
|
|
//각 Field 초기화
|
|
// $this->getMyAuth()->pushCurrentUrl($this->request->getUri()->getPath() . ($this->request->getUri()->getQuery() ? "?" . $this->request->getUri()->getQuery() : ""));
|
|
$this->initAction(__FUNCTION__);
|
|
//FieldRule정의
|
|
foreach ($this->getFormFields() as $field) {
|
|
$this->setFieldRule($field, $this->getFormFieldRule($this->getAction(), $field));
|
|
}
|
|
helper(['form']);
|
|
//filter_fields에 해당하는 값이 있을 경우 정의
|
|
foreach ($this->getFilterFields() as $field) {
|
|
$value = $this->request->getVar($field);
|
|
if ($value) {
|
|
$this->$field = $value;
|
|
}
|
|
}
|
|
//기존 Entity 가져오기
|
|
$entity = $this->getService()->getEntity($uid);
|
|
if (!$entity) {
|
|
throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다.");
|
|
}
|
|
$this->entity = $this->modify_form_process($entity);
|
|
$this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []];
|
|
$this->getService()->getModel()->transCommit();
|
|
return $this->getResultSuccess();
|
|
} catch (\Exception $e) {
|
|
return $this->getResultFail($e->getMessage());
|
|
}
|
|
}
|
|
protected function modify_process(mixed $entity, array $formDatas): mixed
|
|
{
|
|
//데이터 검증
|
|
$formDatas = $this->doValidate($this->getFieldRules(), $formDatas);
|
|
return $this->getService()->modify($entity, $formDatas);
|
|
}
|
|
final public function modify(int $uid): RedirectResponse|string
|
|
{
|
|
//Transaction Start
|
|
$this->getService()->getModel()->transStart();
|
|
try {
|
|
//각 Field 초기화
|
|
$this->initAction(__FUNCTION__);
|
|
//FieldRule정의
|
|
foreach ($this->getFormFields() as $field) {
|
|
$this->setFieldRule($field, $this->getFormFieldRule($this->getAction(), $field));
|
|
}
|
|
//입력값정의
|
|
$formDatas = [];
|
|
foreach ($this->getFormFields() as $field) {
|
|
$formDatas[$field] = $this->request->getPost($field);
|
|
}
|
|
//기존 Entity 가져오기
|
|
$entity = $this->getService()->getEntity($uid);
|
|
if (!$entity) {
|
|
throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다.");
|
|
}
|
|
$this->entity = $this->modify_process($entity, $formDatas);
|
|
$this->getService()->getModel()->transCommit();
|
|
return $this->getResultSuccess();
|
|
} catch (\Exception $e) {
|
|
$this->getService()->getModel()->transRollback();
|
|
return $this->getResultFail($e->getMessage());
|
|
}
|
|
}
|
|
//단일필드작업
|
|
final protected function toggle_process(mixed $entity, array $formDatas): mixed
|
|
{
|
|
//modify_process를 호출하여 수정처리
|
|
return $this->modify_process($entity, $formDatas);
|
|
}
|
|
final public function toggle(mixed $uid, string $field): RedirectResponse|string
|
|
{
|
|
//Transaction Start
|
|
$this->getService()->getModel()->transStart();
|
|
try {
|
|
//각 Field 초기화:조건항목 Field는 한개만 존재하므로 Field와 Rule을 재정의 필요
|
|
$this->setAction(__FUNCTION__);
|
|
//Field와 Rule을 재정의
|
|
$this->setFormFields([$field]);
|
|
$this->setFieldRule($field, $this->getFormFieldRule($this->getAction(), $field));
|
|
//입력값정의
|
|
$formDatas = [$field => $this->request->getVar($field)];
|
|
//기존 Entity 가져오기
|
|
$entity = $this->getService()->getEntity($uid);
|
|
if (!$entity) {
|
|
throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다.");
|
|
}
|
|
$this->entity = $this->toggle_process($entity, $formDatas);
|
|
$this->getService()->getModel()->transCommit();
|
|
return $this->getResultSuccess();
|
|
} catch (\Exception $e) {
|
|
$this->getService()->getModel()->transRollback();
|
|
return $this->getResultFail($e->getMessage());
|
|
}
|
|
}
|
|
//일괄처리작업
|
|
final protected function batchjob_process(mixed $entity, array $formDatas): mixed
|
|
{
|
|
//modify_process를 호출하여 수정처리
|
|
return $this->modify_process($entity, $formDatas);
|
|
}
|
|
final public function batchjob(): RedirectResponse|string
|
|
{
|
|
//Transaction Start
|
|
$this->getService()->getModel()->transStart();
|
|
try {
|
|
//각 Field 초기화: 일괄작업은 선택된 조건항목 Field만 존재하므로 Field와 Rule을 재정의 필요
|
|
$this->setAction(__FUNCTION__);
|
|
$selectedFields = [];
|
|
$formDatas = [];
|
|
foreach ($this->getService()->getBatchJobFields() as $field) {
|
|
//입력한 Field,값정의
|
|
$value = $this->request->getPost($field);
|
|
if ($value) {
|
|
$selectedFields[] = $field;
|
|
$formDatas[$field] = $value;
|
|
}
|
|
}
|
|
if (!count($selectedFields)) {
|
|
throw new \Exception("변경할 조건항목을 선택하셔야합니다.");
|
|
}
|
|
//선택된 Fields와 Rules을 재정의
|
|
$this->setFormFields([$field]);
|
|
foreach ($this->getFormFields() as $field) {
|
|
$this->setFieldRule($field, $this->getFormFieldRule($this->getAction(), $field));
|
|
}
|
|
//변경할 UIDS 정의
|
|
$uids = $this->request->getPost('batchjob_uids[]');
|
|
if (!is_array($uids) || !count($uids)) {
|
|
throw new \Exception("적용할 리스트을 선택하셔야합니다.");
|
|
}
|
|
$entities = [];
|
|
foreach ($uids as $uid) {
|
|
//기존 Entity 가져오기
|
|
$entity = $this->getService()->getEntity($uid);
|
|
if (!$entity) {
|
|
LogCollector::debug(__METHOD__ . "에서 {$uid}에 대한 정보를 찾을수 없습니다.");
|
|
} else {
|
|
$entities[] = $this->batchjob_process($entity, $formDatas);
|
|
}
|
|
}
|
|
|
|
$this->entities = $entities;
|
|
$this->getService()->getModel()->transCommit();
|
|
LogCollector::debug(sprintf("%s에서 총 %s개중 %s개 일괄작업을 완료하였습니다.", __METHOD__, count($uids), count($entities)));
|
|
return $this->getResultSuccess();
|
|
} catch (\Exception $e) {
|
|
$this->getService()->getModel()->transRollback();
|
|
return $this->getResultFail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
//삭제,일괄삭제 공통사용
|
|
protected function delete_process(mixed $entity): mixed
|
|
{
|
|
return $this->getService()->delete($entity);
|
|
}
|
|
final public function delete(mixed $uid): RedirectResponse|string
|
|
{
|
|
//Transaction Start
|
|
$this->getService()->getModel()->transStart();
|
|
try {
|
|
//각 Field 초기화:삭제는 다른 초기화 필요없음
|
|
$this->setAction(__FUNCTION__);
|
|
//기존 Entity 가져오기
|
|
$entity = $this->getService()->getEntity($uid);
|
|
if (!$entity) {
|
|
throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다.");
|
|
}
|
|
$this->entity = $this->delete_process($entity);
|
|
$this->getService()->getModel()->transCommit();
|
|
return $this->getResultSuccess();
|
|
} catch (\Exception $e) {
|
|
$this->getService()->getModel()->transRollback();
|
|
return $this->getResultFail($e->getMessage());
|
|
}
|
|
}
|
|
//일괄삭제
|
|
final protected function batchjob_delete_process(mixed $entity): mixed
|
|
{
|
|
//delete_process를 호출하여 삭제처리
|
|
return $this->delete_process($entity);
|
|
}
|
|
final public function batchjob_delete(): RedirectResponse|string
|
|
{
|
|
//Transaction Start
|
|
$this->getService()->getModel()->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) {
|
|
//기존 Entity 가져오기
|
|
$entity = $this->getService()->getEntity($uid);
|
|
if (!$entity) {
|
|
LogCollector::debug(__METHOD__ . "에서 {$uid}에 대한 정보를 찾을수 없습니다.");
|
|
} else {
|
|
$entities[] = $this->batchjob_delete_process($entity);
|
|
}
|
|
}
|
|
$this->entities = $entities;
|
|
$this->getService()->getModel()->transCommit();
|
|
LogCollector::debug(sprintf("%s에서 총 %s개중 %s개 일괄삭제를 완료하였습니다.", __METHOD__, count($uids), count($entities)));
|
|
return $this->getResultSuccess();
|
|
} catch (\Exception $e) {
|
|
$this->getService()->getModel()->transRollback();
|
|
return $this->getResultFail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
//View
|
|
protected function view_process(mixed $entity): mixed
|
|
{
|
|
return $entity;
|
|
}
|
|
final public function view(string $uid): RedirectResponse|string
|
|
{
|
|
try {
|
|
//각 Field 초기화
|
|
$this->initAction(__FUNCTION__);
|
|
//FieldRule정의
|
|
foreach ($this->getViewFields() as $field) {
|
|
$this->setFieldRule($field, $this->getFormFieldRule($this->getAction(), $field));
|
|
}
|
|
helper(['form']);
|
|
//기존 Entity 가져오기
|
|
$entity = $this->getService()->getEntity($uid);
|
|
if (!$entity) {
|
|
throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다.");
|
|
}
|
|
//filter_fields에 해당하는 값이 있을 경우 정의
|
|
foreach ($this->getFilterFields() as $field) {
|
|
$value = $this->request->getVar($field);
|
|
if ($value) {
|
|
$this->$field = $value;
|
|
}
|
|
}
|
|
$this->entity = $this->view_process($entity);
|
|
$this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []];
|
|
return $this->getResultSuccess();
|
|
} catch (\Exception $e) {
|
|
return $this->getResultFail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
//리스트
|
|
//Filter 조건절 처리
|
|
protected function setFilterConditionForList(): void
|
|
{
|
|
foreach ($this->getFilterFields() as $field) {
|
|
$this->$field = $this->request->getVar($field);
|
|
if ($this->$field !== null && $this->$field !== '') {
|
|
$this->getService()->getModel()->where("{$this->getService()->getModel()->getTable()}.{$field}", $this->$field);
|
|
}
|
|
}
|
|
}
|
|
//검색어 조건절 처리
|
|
protected function setWordConditionForList(): void
|
|
{
|
|
$this->word = $this->request->getVar('word');
|
|
if ($this->word !== null && $this->word !== '') {
|
|
$this->getService()->getModel()->setList_WordFilter($this->word);
|
|
}
|
|
}
|
|
//검색일 조건절 처리
|
|
protected function setDateConditionForList(): void
|
|
{
|
|
$this->start = $this->request->getVar('start');
|
|
if ($this->start !== null && $this->start !== '') {
|
|
$this->getService()->getModel()->where(sprintf("%s.created_at >= '%s 00:00:00'", $this->getService()->getModel()->getTable(), $this->start));
|
|
}
|
|
$this->end = $this->request->getVar('end');
|
|
if ($this->end !== null && $this->end !== '') {
|
|
$this->getService()->getModel()->where(sprintf("%s.created_at <= '%s 23:59:59'", $this->getService()->getModel()->getTable(), $this->end));
|
|
}
|
|
}
|
|
//OrderBy 처리
|
|
protected function setOrderByForList(): void
|
|
{
|
|
$this->order_field = $this->request->getVar('order_field');
|
|
$this->order_value = $this->request->getVar('order_value');
|
|
if ($this->order_field !== null && $this->order_field !== '') {
|
|
$this->getService()->getModel()->orderBy(sprintf("%s.%s %s", $this->getService()->getModel()->getTable(), $this->order_field, $this->order_value ?: "DESC"));
|
|
} else {
|
|
$this->getService()->getModel()->orderBy(sprintf("%s.%s %s", $this->getService()->getModel()->getTable(), $this->getService()->getModel()->getPKField(), "DESC"));
|
|
}
|
|
}
|
|
//조건절 처리
|
|
protected function setConditionForList(): void
|
|
{
|
|
$this->setFilterConditionForList();
|
|
$this->setWordConditionForList();
|
|
$this->setDateConditionForList();
|
|
}
|
|
//PageNation 처리
|
|
protected function getPageOptiosForList(): 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;
|
|
}
|
|
protected function getPaginationForList($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");
|
|
// $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_process(): array
|
|
{
|
|
//조건절 처리
|
|
$this->setConditionForList();
|
|
//TotalCount (SoftDelete적용이 되려면 countAllResults를 사용해야함)
|
|
$this->total_count = $this->getService()->getModel()->selectCount('*', 'cnt')->countAllResults();
|
|
//Pagination 처리
|
|
$this->pagination = $this->getPaginationForList();
|
|
//줄수 처리용
|
|
$this->page_options = $this->getPageOptiosForList();
|
|
//조건절 , OrcerBy , Limit 처리
|
|
$this->setConditionForList();
|
|
$this->setOrderByForList();
|
|
$this->getService()->getModel()->limit($this->per_page);
|
|
$this->getService()->getModel()->offset(($this->page - 1) * $this->per_page);
|
|
return $this->getService()->getEntities();
|
|
}
|
|
public function index(): RedirectResponse|string
|
|
{
|
|
try {
|
|
//각 Field 초기화
|
|
$this->initAction(__FUNCTION__);
|
|
//Return Url정의
|
|
$this->getMyAuth()->pushCurrentUrl($this->request->getUri()->getPath() . ($this->request->getUri()->getQuery() ? "?" . $this->request->getUri()->getQuery() : ""));
|
|
//FieldRule정의
|
|
foreach ($this->getIndexFields() as $field) {
|
|
$this->setFieldRule($field, $this->getFormFieldRule($this->getAction(), $field));
|
|
}
|
|
// 현재 URL을 스택에 저장
|
|
helper(['form']);
|
|
$this->entities = $this->index_process();
|
|
return $this->getResultSuccess();
|
|
} catch (\Exception $e) {
|
|
return $e->getMessage();
|
|
// 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->initAction(__FUNCTION__);
|
|
//URL처리
|
|
// $this->uri = $this->request->getUri();
|
|
switch ($output_type) {
|
|
case 'excel':
|
|
case 'pdf':
|
|
helper(['form']);
|
|
// string buffer에서 읽어오는 경우
|
|
$this->entities = $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());
|
|
}
|
|
}
|
|
}
|