dbmsv4/app/Controllers/Admin/Part/CPUController.php
2025-11-19 11:39:59 +09:00

225 lines
8.3 KiB
PHP

<?php
namespace App\Controllers\Admin\Part;
use App\Entities\Part\CPUEntity;
use CodeIgniter\HTTP\DownloadResponse;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Validation\Exceptions\ValidationException;
use Psr\Log\LoggerInterface;
use RuntimeException;
class CPUController extends PartController
{
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
if ($this->service === null) {
$this->service = service('part_cpuservice');
}
}
protected function action_init_process(string $action): void
{
$fields = [
"title",
"price",
"stock",
];
$filters = [
"status",
];
$batchjobFilters = ['status'];
// $actionButtons = ['view' => ICONS['SEARCH']];
// $batchjobButtons = [];
parent::action_init_process($action);
switch ($action) {
case 'create':
case 'create_form':
case 'modify':
case 'modify_form':
$fields = [...$fields, 'status'];
break;
case 'view':
$fields = [...$fields, 'status', 'created_at'];
break;
case 'index':
case 'download':
$fields = [...$fields, 'status', 'created_at'];
break;
default:
throw new \Exception("[{$action}] 지원하지 않는 action입니다.");
// break;
}
$this->service->getFormService()->setFormFields($fields);
$this->service->getFormService()->setFormRules($action, $fields);
$this->service->getFormService()->setFormFilters($filters);
$this->service->getFormService()->setFormOptions($filters);
$this->service->getFormService()->setBatchjobFilters($batchjobFilters);
// $this->service->getFormService()->setActionButtons($actionButtons);
// $this->service->getFormService()->setBatchjobButtons($batchjobButtons);
parent::action_init_process($action);
}
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());
}
}
public function create(): string|RedirectResponse
{
try {
$action = __FUNCTION__;
$this->action_init_process($action);
$entity = $this->create_process();
if (!$entity instanceof CPUEntity) {
throw new RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 CPUEntity만 가능");
}
return $this->create_result_process($entity);
} 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());
}
}
public function modify_form($uid): string|RedirectResponse
{
try {
$action = __FUNCTION__;
$this->action_init_process($action);
$entity = $this->modify_form_process($uid);
if (!$entity instanceof CPUEntity) {
throw new RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 CPUEntity만 가능");
}
$this->addViewDatas('entity', $entity);
return $this->modify_form_result_process($action);
} catch (\Exception $e) {
return $this->action_redirect_process('error', "{$this->getTitle()}에서 {$uid} 수정폼 오류:" . $e->getMessage());
}
}
public function modify($uid): string|RedirectResponse
{
try {
$action = __FUNCTION__;
$this->action_init_process($action);
$entity = $this->modify_process($uid);
if (!$entity instanceof CPUEntity) {
throw new RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 CPUEntity만 가능");
}
$this->addViewDatas('entity', $entity);
return $this->modify_result_process($entity);
} 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());
}
}
public function delete($uid): RedirectResponse
{
try {
$entity = $this->delete_process($uid);
if (!$entity instanceof CPUEntity) {
throw new RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 CPUEntity만 가능");
}
return $this->delete_result_process($entity);
} catch (\Exception $e) {
return $this->action_redirect_process('error', "{$this->getTitle()}에서 {$uid} 삭제 오류:" . $e->getMessage());
}
}
public function view($uid): string|RedirectResponse
{
try {
$action = __FUNCTION__;
$this->action_init_process($action);
$entity = $this->view_process($uid);
if (!$entity instanceof CPUEntity) {
throw new RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 CPUEntity만 가능");
}
$this->addViewDatas('entity', $entity);
return $this->view_result_process($action);
} catch (\Exception $e) {
return $this->action_redirect_process('error', "{$this->getTitle()}에서 {$uid} 상세보기 오류:" . $e->getMessage());
}
}
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());
}
}
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());
}
}
public function index(): string|ResponseInterface
{
try {
$action = __FUNCTION__;
$this->action_init_process($action);
$this->index_process($action);
$this->index_result_process($action);
} catch (\Exception $e) {
session()->setFlashdata('message', $e->getMessage());
}
return $this->index_result_process($action);
}
public function download(string $output_type, mixed $uid = false): DownloadResponse|RedirectResponse|string
{
try {
$action = __FUNCTION__;
$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());
}
}
//기본 함수 작업
//Custom 추가 함수
}