trafficmonitor/app/Controllers/Admin/AdminController.php

209 lines
8.9 KiB
PHP

<?php
namespace App\Controllers\Admin;
use Psr\Log\LoggerInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\RedirectResponse;
use App\Entities\CommonEntity;
use App\Controllers\CommonController;
use CodeIgniter\Validation\Exceptions\ValidationException;
use CodeIgniter\HTTP\DownloadResponse;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Html;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
abstract class AdminController extends CommonController
{
public const PATH = 'admin';
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->addActionPaths(self::PATH);
}
final protected function getLayout(): string
{
return self::PATH;
}
protected function create_form_process(array $formDatas = []): array
{
return $formDatas;
}
final public function create_form(): string
{
$action = __FUNCTION__;
try {
//초기화
$this->action_init_process($action);
$this->addViewDatas('formDatas', $this->create_form_process());
} catch (\Exception $e) {
log_message('error', $e->getMessage());
session()->setFlashdata('message', $e->getMessage());
}
return $this->action_render_process($this->getActionPaths(), $action, $this->getViewDatas());
}
abstract protected function create_process(): RedirectResponse;
final public function create(): RedirectResponse
{
$action = __FUNCTION__;
try {
//초기화
$this->action_init_process($action);
return $this->create_process();
} catch (ValidationException $e) {
// 검증 실패 시 폼으로 돌아가서 오류 메시지 표시
log_message('error', $e->getMessage());
return redirect()->back()->withInput()->with('message', $e->getMessage());
} catch (\Exception $e) {
log_message('error', $e->getMessage());
return redirect()->back()->withInput()->with('message', $e->getMessage());
}
}
protected function modify_form_process($uid): CommonEntity
{
if (!$uid) {
throw new \Exception("계정 번호가 정의 되지 않았습니다.");
}
$entity = $this->service->getEntity($uid);
if (!$entity instanceof CommonEntity) {
throw new \Exception("{$uid}에 해당하는 계정을 찾을수 없습니다.");
}
return $entity;
}
final public function modify_form($uid): string
{
$action = __FUNCTION__;
try {
//초기화
$this->action_init_process($action);
$entity = $this->modify_form_process($uid);
$this->addViewDatas('entity', $entity);
} catch (\Exception $e) {
log_message('error', $e->getMessage());
session()->setFlashdata('message', $e->getMessage());
}
return $this->action_render_process($this->getActionPaths(), $action, $this->getViewDatas());
}
abstract protected function modify_process($uid): RedirectResponse;
final public function modify($uid): RedirectResponse
{
$action = __FUNCTION__;
try {
//초기화
$this->action_init_process($action);
return $this->modify_process($uid);
} catch (ValidationException $e) {
// 검증 실패 시 폼으로 돌아가서 오류 메시지 표시
log_message('error', $e->getMessage());
return redirect()->back()->withInput()->with('message', $e->getMessage());
} catch (\Exception $e) {
log_message('error', $e->getMessage());
return redirect()->back()->withInput()->with('message', $e->getMessage());
}
}
//리스트관련
//조건절 처리
protected function index_condition_process(): void
{
//Filter조건절 처리
$index_filters = [];
foreach ($this->service->getFormFilters() as $field) {
$value = $this->request->getVar($field) ?? null;
if ($value) {
$this->service->index_condition_filterField($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->index_condition_filterWord($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->index_condition_filterDate($index_start, $index_end);
}
$this->addViewDatas('index_start', $index_start);
$this->addViewDatas('index_end', $index_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->service->getEntities() as $entity) {
// $entities[] = $entity;
// }
// return $entities;
// }
// public function index(): RedirectResponse|string
// {
// try {
// $this->service->setAction(__FUNCTION__);
// $this->service->setFormFields();
// //전달값정의
// $this->service->setFormDatas($this->request->getGet());
// $this->service->setFormFilters();
// $this->service->setFormRules();
// $this->service->setFormOptions();
// //일괄작업용 Fields정의
// $this->service->setControlDatas('batchjob_fields', $this->service->getBatchjobFields());
// //일괄작업용 버튼정의
// $this->service->setControlDatas('batchjob_buttions', $this->service->getBatchjobButtons());
// 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->service->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->service->setOrderBy($this->order_field, $this->order_value);
// $this->service->setLimit($this->per_page);
// $this->service->setOffset(($this->page - 1) * $this->per_page);
// $this->index_condition_process();
// $this->entities = $this->index_process();
// return $this->getResultSuccess();
// } catch (\Exception $e) {
// return $this->getResultFail($e->getMessage());
// }
// }
}