From 991d50702765f45c5bd72ea14e43dd7cd92a0c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Wed, 2 Aug 2023 11:46:50 +0900 Subject: [PATCH] shoppingmallv2 init... --- app/Config/Constants.php | 1 + app/Controllers/BaseController.php | 50 ++++++++++--------- app/Controllers/Front/OrderController.php | 61 ++++++++--------------- app/Libraries/Adapter/Auth/Adapter.php | 4 +- 4 files changed, 51 insertions(+), 65 deletions(-) diff --git a/app/Config/Constants.php b/app/Config/Constants.php index 2da7cf5..2fc5314 100644 --- a/app/Config/Constants.php +++ b/app/Config/Constants.php @@ -149,6 +149,7 @@ define('SESSION_NAMES', [ 'RETURN_URL' => "return_url", 'ISLOGIN' => "islogined", 'AUTH' => 'auth', + 'CART' => 'cart' ]); define('AUTH_FIELDS', ['ID' => 'id', 'TITLE' => 'title', 'ROLE' => 'role']); diff --git a/app/Controllers/BaseController.php b/app/Controllers/BaseController.php index f99cc01..3530708 100644 --- a/app/Controllers/BaseController.php +++ b/app/Controllers/BaseController.php @@ -3,6 +3,7 @@ namespace App\Controllers; +use App\Models\UserModel; use CodeIgniter\Controller; use CodeIgniter\HTTP\CLIRequest; use CodeIgniter\HTTP\Files\UploadedFile; @@ -10,7 +11,6 @@ use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; use Psr\Log\LoggerInterface; -use App\Models\UserModel; /** * Class BaseController @@ -401,6 +401,22 @@ abstract class BaseController extends Controller } //Index 관련 + protected function index_setCondition() + { + //조건절 처리 + $filterFields = array(); + foreach ($this->_viewDatas['fieldFilters'] as $field) { + if (!is_null($this->request->getVar($field))) { + $filterFields[$field] = $this->request->getVar($field); + } + } + $word = $this->request->getVar('word'); + $start = $this->request->getVar('start'); + $end = $this->request->getVar('end'); + $order_field = $this->request->getVar('order_field'); + $order_value = $this->request->getVar('order_value'); + $this->_model->setCondition($filterFields, $word, $start, $end, $order_field, $order_value); + } private function index_getPagination($pager_group = 'default', int $segment = 0, $template = 'bootstrap_full'): string { // 1.Views/Pagers/에 bootstrap_full.php,bootstrap_simple.php 생성 @@ -420,26 +436,13 @@ abstract class BaseController extends Controller $this->_viewDatas['total_page'] = $pager->getPageCount($pager_group); return $pager->links($pager_group, $template); } - private function index_setCondition() - { - //조건절 처리 - $filterFields = array(); - foreach ($this->_viewDatas['fieldFilters'] as $field) { - if (!is_null($this->request->getVar($field))) { - $filterFields[$field] = $this->request->getVar($field); - } - } - $word = $this->request->getVar('word'); - $start = $this->request->getVar('start'); - $end = $this->request->getVar('end'); - $order_field = $this->request->getVar('order_field'); - $order_value = $this->request->getVar('order_value'); - $this->_model->setCondition($filterFields, $word, $start, $end, $order_field, $order_value); - } - private function index_getEntitys(int $page = 0, int $per_page = 0) + private function index_getEntitys(): array { $this->index_setCondition(); - return $per_page ? $this->_model->findAll($per_page, $page * $per_page - $per_page) : $this->_model->findAll(); + return $this->_viewDatas['page'] ? $this->_model->findAll( + $this->_viewDatas['per_page'], + $this->_viewDatas['page'] * $this->_viewDatas['per_page'] - $this->_viewDatas['per_page'] + ) : $this->_model->findAll(); } public function index() { @@ -455,12 +458,13 @@ abstract class BaseController extends Controller $this->_viewDatas['end'] = $this->request->getVar('end') ?: ''; $this->_viewDatas['order_field'] = $this->request->getVar('order_field') ?: 'uid'; $this->_viewDatas['order_value'] = $this->request->getVar('order_value') ?: 'DESC'; - $this->_viewDatas['page'] = $this->request->getVar('page') ?: 1; - $this->_viewDatas['per_page'] = $this->request->getVar('per_page') ?: DEFAULTS['PERPAGE']; + $this->_viewDatas['page'] = (int)$this->request->getVar('page') ?: 1; + $this->_viewDatas['per_page'] = (int)$this->request->getVar('per_page') ?: DEFAULTS['PERPAGE']; $this->_viewDatas['uri'] = $this->request->getUri(); //Totalcount 처리 $this->index_setCondition(); $this->_viewDatas['total_count'] = $this->_model->countAllResults(); + // log_message("debug", __METHOD__ . "에서 TotalCount 호출:" . $this->_model->getLastQuery()); //줄수 처리용 $this->_viewDatas['pageOptions'] = array("" => "줄수선택"); for ($i = 10; $i <= $this->_viewDatas['total_count'] + $this->_viewDatas['per_page']; $i += 10) { @@ -469,8 +473,8 @@ abstract class BaseController extends Controller //pagenation 처리 $this->_viewDatas['pagination'] = $this->index_getPagination(); //모델 처리 - $this->_viewDatas['entitys'] = $this->index_getEntitys((int)$this->_viewDatas['page'], (int)$this->_viewDatas['per_page']); - // log_message("debug", __METHOD__ . "에서 호출:" . $this->_model->getLastQuery()); + $this->_viewDatas['entitys'] = $this->index_getEntitys(); + // log_message("debug", __METHOD__ . "에서 findAll 호출:" . $this->_model->getLastQuery()); //setting return_url to session flashdata $this->_session->setFlashdata(SESSION_NAMES['RETURN_URL'], current_url() . '?' . $this->request->getUri()->getQuery() ?: ""); return view($this->_viewPath . '/index', $this->_viewDatas); diff --git a/app/Controllers/Front/OrderController.php b/app/Controllers/Front/OrderController.php index 35c965f..2aca89f 100644 --- a/app/Controllers/Front/OrderController.php +++ b/app/Controllers/Front/OrderController.php @@ -5,16 +5,16 @@ namespace App\Controllers\Front; use App\Entities\OrderEntity; use App\Models\OrderModel; use App\Models\ProductModel; +use CodeIgniter\Cookie\Cookie; +use CodeIgniter\Cookie\CookieStore; use CodeIgniter\Cookie\Exceptions\CookieException; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; +use DateTime; use Psr\Log\LoggerInterface; class OrderController extends FrontController { - private $_cart_cookie_name = "order_uids"; - private $_cart_cookie_delimeter = "|"; - private $_cart_cookie_expire = 3600; //1Hour public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { $this->_model = new OrderModel($this->getFields()); @@ -22,7 +22,6 @@ class OrderController extends FrontController $this->_viewDatas['title'] = lang($this->_model->getClassName() . '.title'); $this->_viewPath .= strtolower($this->_model->getClassName()); helper($this->_model->getClassName()); - helper('cookie'); } final public function getFields(string $action = ""): array @@ -48,43 +47,12 @@ class OrderController extends FrontController return ["status"]; } - //쿠키에 장바구니에 담긴 Order UID를 추가해준다. - private function setOrderCookie(OrderEntity $entity) + //세션에 장바구니에 담긴 Order UID를 추가해준다. + private function setCart(OrderEntity $entity) { - try { - $order_uids = array(); - if (has_cookie($this->_cart_cookie_name)) { - $order_uids = explode($this->_cart_cookie_delimeter, get_cookie($this->_cart_cookie_name)); - } - // delete_cookie($this->_cart_cookie_name); - set_cookie([ - 'name' => $this->_cart_cookie_name, - 'value' => implode($this->_cart_cookie_delimeter, [...$order_uids, $entity->getPrimaryKey()]), - 'expire' => time() + $this->_cart_cookie_expire, - 'domain' => ".localhost", - 'path' => '/', - ]); - } catch (CookieException $e) { - throw new \Exception(__FUNCTION__ . "에서 오류발생:\n" . $e->getMessage()); - } + $order_uids = $this->_session->get(SESSION_NAMES['CART']) ?: array(); + $this->_session->set(SESSION_NAMES['CART'], [...$order_uids, $entity->getPrimaryKey()]); } - // private function setOrderCookie_ORG(OrderEntity $entity) - // { - // $order_uids = array(); - // if (isset($_COOKIE[$this->_cart_cookie_name])) { - // $order_uids = explode($this->_cart_cookie_delimeter, $_COOKIE[$this->_cart_cookie_name]); - // } - // // setcookie($this->_cart_cookie_name, time() - $this->_cart_cookie_expire); - // if (setcookie( - // $this->_cart_cookie_name, - // implode($this->_cart_cookie_delimeter, [...$order_uids, $entity->getPrimaryKey()]), - // time() + $this->_cart_cookie_expire, - // '/', - // '.localhost' - // )) { - // throw new \Exception(__FUNCTION__ . "에서 SetCookie 오류발생"); - // } - // } //장바구니에 담기 protected function insert_process() @@ -111,7 +79,7 @@ class OrderController extends FrontController //Transaction 시작 $this->_model->transStart(); $entity = $this->_model->create($this->_viewDatas['fieldDatas']); - $this->setOrderCookie($entity); + $this->setCart($entity); //Transaction Commit $this->_model->transComplete(); $msg = sprintf( @@ -134,4 +102,17 @@ class OrderController extends FrontController $this->_session->setFlashdata("return_message", $msg); } } + + protected function index_setCondition() + { + parent::index_setCondition(); + //세션에 Cart정보(order_uids)가 있으면 + $uids = $this->_session->get(SESSION_NAMES['CART']) ?: array('NONE'); + //또는 Login했으면 사용자정보(user_uid)에 맞는 Order정보 가져오기 + if ($this->_session->get(SESSION_NAMES['ISLOGIN'])) { + $this->_model->where('user_uid', $this->_session->get(SESSION_NAMES['AUTH'])[AUTH_FIELDS['ID']]); + } elseif (count($uids)) { + $this->_model->whereIn('uid', $uids); + } + } } diff --git a/app/Libraries/Adapter/Auth/Adapter.php b/app/Libraries/Adapter/Auth/Adapter.php index 83ed02a..1e18cd4 100644 --- a/app/Libraries/Adapter/Auth/Adapter.php +++ b/app/Libraries/Adapter/Auth/Adapter.php @@ -2,9 +2,9 @@ namespace App\Libraries\Adapter\Auth; +use App\Entities\UserEntity; use App\Models\UserModel; use App\Models\UserSNSModel; -use App\Entities\UserEntity; // 참고:https://github.com/SyntaxPhoenix/iloclient abstract class Adapter @@ -50,7 +50,7 @@ abstract class Adapter { $this->_session->set(SESSION_NAMES['ISLOGIN'], true); $auths = []; - foreach (AUTH_FIELDS as $key => $field) { + foreach (array_values(AUTH_FIELDS) as $field) { switch ($field) { case 'id': $auths[$field] = $entity->getPrimaryKey();