shoppingmallv2/app/Controllers/Front/BoardController.php
최준흠git config git config --helpgit config --global user.name 최준흠 a0c66ee3ec shoppingmallv2 init...
2023-08-04 23:19:19 +09:00

203 lines
8.3 KiB
PHP

<?php
namespace App\Controllers\Front;
use App\Models\BoardModel;
use App\Models\CategoryModel;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
class BoardController extends FrontController
{
private $_category_uid = null;
private $_categoryModel = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
$this->_model = new BoardModel($this->getFields());
parent::initController($request, $response, $logger);
$this->_viewPath .= strtolower($this->_model->getClassName());
}
private function getCategoryModel(): CategoryModel
{
return $this->_categoryModel = $this->_categoryModel ?: new CategoryModel();
}
public function getFields(string $action = ""): array
{
$fields = ['title', "board_file", "passwd", "content"];
switch ($action) {
case "index":
case "excel":
return ['title', "board_file", "view_cnt", "created_at"];
break;
case "view":
return ['title', "board_file", "view_cnt", "created_at", "content"];
break;
default:
return $fields;
break;
}
}
public function getFieldFilters(): array
{
return [];
}
public function getFieldBatchFilters(): array
{
return parent::getFieldBatchFilters();
}
//Field별 Form Datas 처리용
protected function getFieldFormData(string $field, $entity = null): array
{
switch ($field) {
case 'passwd':
$this->_viewDatas['fieldDatas'][$field] = $this->request->getVar($field);
$this->_viewDatas['fieldDatas']['confirmpassword'] = $this->request->getVar('confirmpassword');
break;
case 'board_file':
$this->_viewDatas['fieldDatas'][$field] = $this->single_upload_procedure($field, $entity);
break;
default:
return parent::getFieldFormData($field, $entity);
break;
}
return $this->_viewDatas['fieldDatas'];
}
//Insert관련
protected function insert_form_process()
{
$this->_category_uid = $this->request->getVar('category_uid') ?: throw new \Exception("범주를 지정하지 않으셨습니다.");
$this->_viewDatas['category'] = $this->getCategoryModel()->getEntity([$this->getCategoryModel()->getPrimaryKey() => $this->_category_uid]);
//사용자가 Category에서 해당 게시판의 WRITE권한이 있는지 확인
if (!isRole_CommonHelper(
$this->_viewDatas['currentRoles'],
$this->_viewDatas['category'],
CATEGORY_ROLE_FIELDS['WRITE']
)) {
throw new \Exception("고객님은 쓰기권한이 없습니다.");
}
return parent::insert_form();
}
protected function insert_process()
{
$this->_category_uid = $this->request->getVar('category_uid') ?: throw new \Exception("범주를 지정하지 않으셨습니다.");
$this->_viewDatas['category'] = $this->getCategoryModel()->getEntity([$this->getCategoryModel()->getPrimaryKey() => $this->_category_uid]);
//사용자가 Category에서 해당 게시판의 WRITE권한이 있는지 확인
if (!isRole_CommonHelper(
$this->_viewDatas['currentRoles'],
$this->_viewDatas['category'],
CATEGORY_ROLE_FIELDS['WRITE']
)) {
throw new \Exception("고객님은 쓰기권한이 없습니다.");
}
return parent::insert();
}
//Update관련
protected function update_form_process($entity)
{
//본인이 작성한글인지 최종확인용 정상접속이 아닌 위회해서 수정을 시도방지용
if (!$this->_viewDatas[SESSION_NAMES['ISLOGIN']] || $entity->getUser_Uid() == $this->_viewDatas['auth'][AUTH_FIELDS['ID']]) {
throw new \Exception("작성자 본인글인지 여부가 확인되지 않습니다.");
}
return parent::update_form_process($entity);
}
protected function update_process($entity)
{
//본인이 작성한글인지 최종확인용 정상접속이 아닌 위회해서 수정을 시도방지용
if (!$this->_viewDatas[SESSION_NAMES['ISLOGIN']] || $entity->getUser_Uid() == $this->_viewDatas['auth'][AUTH_FIELDS['ID']]) {
throw new \Exception("작성자 본인글인지 여부가 확인되지 않습니다.");
}
return parent::update_process($entity);
}
//Reply관련
protected function reply_form_process($entity)
{
$this->_viewDatas['category'] = $this->getCategoryModel()->getEntity(
[$this->getCategoryModel()->getPrimaryKey() => $entity->getCategory_Uid()]
);
// echo var_export(isRole_CommonHelper(
// $this->_viewDatas['currentRoles'],
// $this->_viewDatas['category'],
// CATEGORY_ROLE_FIELDS['REPLY']
// ), true);
// exit;
//사용자가 Category에서 해당 게시판의 REPLY권한이 있는지 확인
if (!isRole_CommonHelper(
$this->_viewDatas['currentRoles'],
$this->_viewDatas['category'],
CATEGORY_ROLE_FIELDS['REPLY']
)) {
throw new \Exception("고객님은 답변권한이 없습니다.");
}
return parent::reply_form_process($entity);
}
protected function reply_process($entity)
{
$this->_viewDatas['category'] = $this->getCategoryModel()->getEntity(
[$this->getCategoryModel()->getPrimaryKey() => $entity->getCategory_Uid()]
);
//사용자가 Category에서 해당 게시판의 REPLY권한이 있는지 확인
if (!isRole_CommonHelper(
$this->_viewDatas['currentRoles'],
$this->_viewDatas['category'],
CATEGORY_ROLE_FIELDS['REPLY']
)) {
throw new \Exception("고객님은 답변권한이 없습니다.");
}
return parent::reply_process($entity);
}
//Delete 관련
protected function delete_process($entity)
{
//본인이 작성한글인지 최종확인용 정상접속이 아닌 위회해서 삭제 시도 방지용
if (!$this->_viewDatas[SESSION_NAMES['ISLOGIN']] || $entity->getUser_Uid() == $this->_viewDatas['auth'][AUTH_FIELDS['ID']]) {
throw new \Exception("작성자 본인글인지 여부가 확인되지 않습니다.");
}
return parent::delete_process($entity);
}
//View관련
protected function view_process($entity)
{
$this->_viewDatas['category'] = $this->getCategoryModel()->getEntity(
[$this->getCategoryModel()->getPrimaryKey() => $entity->getCategory_Uid()]
);
//사용자가 Category에서 해당 게시판의 READ권한이 있는지 확인
if (!isRole_CommonHelper(
$this->_viewDatas['currentRoles'],
$this->_viewDatas['category'],
CATEGORY_ROLE_FIELDS['READ']
)) {
throw new \Exception("고객님은 읽기권한이 없습니다.");
}
//조회수 올리기
$this->_model->addViewCount($entity);
return parent::view_process($entity);
}
//Index관련
protected function index_process()
{
$this->_category_uid = $this->request->getVar('category_uid') ?: throw new \Exception("범주를 지정하지 않으셨습니다.");
$this->_viewDatas['category'] = $this->getCategoryModel()->getEntity([$this->getCategoryModel()->getPrimaryKey() => $this->_category_uid]);
//사용자가 Category에서 해당 게시판의 ACCESS권한이 있는지 확인
if (!isRole_CommonHelper(
$this->_viewDatas['currentRoles'],
$this->_viewDatas['category'],
CATEGORY_ROLE_FIELDS['ACCESS']
)) {
throw new \Exception("고객님은 접속권한이 없습니다.");
}
return parent::index_process();
}
//Category 및 Status 조건추가
protected function index_setCondition()
{
$this->_model->where("category_uid", $this->_viewDatas['category']->getPrimaryKey());
$this->_model->where("status", DEFAULTS['STATUS']);
parent::index_setCondition();
}
}