shoppingmallv2/app/Controllers/Admin/EcommerceController.php
2023-08-01 10:58:12 +09:00

66 lines
2.6 KiB
PHP

<?php
namespace App\Controllers\Admin;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
class EcommerceController extends AdminController
{
private $_service = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
$this->_service = service('ecommerce');
parent::initController($request, $response, $logger);
$this->_viewPath .= strtolower($this->_service->getClassName());
}
protected function addCart_process()
{
//fieldData Rule 검사
if (!$this->validate($this->_viewDatas['fieldRules'])) {
throw new \Exception("장비구니 검증 오류발생\n" . implode("\n", $this->validator->getErrors()));
}
//fieldData 적용
$this->_viewDatas['fieldDatas'] = array();
foreach ($this->_viewDatas['fields'] as $field) {
$this->_viewDatas['fieldDatas'] = $this->getFieldFormData($field);
}
}
//장바구니에 담기
public function addCart()
{
$msg = "";
try {
$this->_viewDatas['fields'] = $this->_service->getFields(__FUNCTION__);
$this->_viewDatas['fieldRules'] = $this->_service->getFieldRules($this->_viewDatas['fields'], __FUNCTION__);
//Transaction 시작
$this->_service->transStart();
$this->addCart_process();
$this->_service->addCart($this->_viewDatas['fieldDatas']);
//Transaction Commit
$this->_service->transCommit();
$msg = sprintf(
"%s에서 해당 상품 %s개를 장바구니에 담았습니다.",
$this->_viewDatas['title'],
$this->_viewDatas['fieldDatas']['quantity']
);
return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL']));
} catch (\Exception $e) {
//Transaction Rollback
$this->_service->transRollback();
log_message("error", $e->getMessage());
log_message("error", var_export($this->_viewDatas['fieldDatas'], true));
$msg = sprintf(
"%s에서 다음 오류로 인해 장바구니에 담기를 실패하였습니다.\n%s",
$this->_viewDatas['title'],
$e->getMessage()
);
return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL']));
} finally {
$this->_session->setFlashdata("return_message", $msg);
}
}
}