130 lines
4.7 KiB
PHP
130 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
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;
|
|
|
|
abstract class CommonController extends BaseController
|
|
{
|
|
use LogTrait;
|
|
private array $_action_paths = [];
|
|
private array $_viewDatas = [];
|
|
protected $service = 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 public function addActionPaths(string $path)
|
|
{
|
|
$this->_action_paths[] = $path;
|
|
}
|
|
final public function getActionPaths($isArray = true, $delimeter = DIRECTORY_SEPARATOR): array|string
|
|
{
|
|
return $isArray ? $this->_action_paths : implode($delimeter, $this->_action_paths);
|
|
}
|
|
final public function addViewDatas(string $key, mixed $value)
|
|
{
|
|
$this->_viewDatas[$key] = $value;
|
|
}
|
|
final public function getViewDatas(?string $key = null): mixed
|
|
{
|
|
if ($key === null) {
|
|
return $this->_viewDatas;
|
|
}
|
|
return $this->_viewDatas[$key] ?? null;
|
|
}
|
|
//공통 필수기능
|
|
protected function doValidation(string $action): array
|
|
{
|
|
$dynamicRules = [];
|
|
foreach ($this->service->getFormRules($action) as $field => $rule) {
|
|
list($field, $rule) = $this->getFormRule($action, $field, $rule);
|
|
$dynamicRules[$field] = $rule;
|
|
}
|
|
//변경할 값 확인 : Upload된 파일 검증시 $this->request->getPOST()보다 먼처 체크필요
|
|
if (!$this->validate($dynamicRules)) {
|
|
throw new \Exception(static::class . "에서 작업 데이터 검증 오류발생\n" . implode(
|
|
"\n",
|
|
$this->validator->getErrors()
|
|
));
|
|
}
|
|
return $this->validator->getValidated();
|
|
}
|
|
protected function getFormRule(string $action, string $field, string $rule): array
|
|
{
|
|
switch ($field) {
|
|
default:
|
|
break;
|
|
}
|
|
return array($field, $rule);
|
|
}
|
|
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>
|
|
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 $redirect_url, string $message): RedirectResponse
|
|
{
|
|
return redirect()->to($redirect_url)->with('message', $message);
|
|
}
|
|
protected function action_render_process(array $view_paths, string $view_file, array $viewDatas): string
|
|
{
|
|
$lastest_path = array_pop($view_paths); //paths는 마지막을 뺀 앞단까지만 남음
|
|
// ✅ 중간 안내 화면으로
|
|
// return view('posts/success_redirect', [
|
|
// 'message' => '게시글이 성공적으로 등록되었습니다.',
|
|
// 'redirectUrl' => route_to('posts_list')
|
|
// ]);
|
|
// 중요한 작업 (결제 완료, 오류 등) → “로딩 페이지(View)”로 안내 후 JS redirect
|
|
$full_path = implode(DIRECTORY_SEPARATOR, [
|
|
...$view_paths,
|
|
$this->request->getVar('ActionTemplate') ?? $lastest_path,
|
|
$view_file
|
|
]);
|
|
$view_datas = [
|
|
...$viewDatas,
|
|
'forms' => ['attributes' => ['method' => "post",], 'hiddens' => []],
|
|
];
|
|
helper(['form', __FUNCTION__]);
|
|
return view($full_path, ['viewDatas' => $view_datas]);
|
|
}
|
|
}
|