shoppingmallv2 init...

This commit is contained in:
최준흠 2023-08-02 11:46:50 +09:00
parent 9746aa3a96
commit 991d507027
4 changed files with 51 additions and 65 deletions

View File

@ -149,6 +149,7 @@ define('SESSION_NAMES', [
'RETURN_URL' => "return_url", 'RETURN_URL' => "return_url",
'ISLOGIN' => "islogined", 'ISLOGIN' => "islogined",
'AUTH' => 'auth', 'AUTH' => 'auth',
'CART' => 'cart'
]); ]);
define('AUTH_FIELDS', ['ID' => 'id', 'TITLE' => 'title', 'ROLE' => 'role']); define('AUTH_FIELDS', ['ID' => 'id', 'TITLE' => 'title', 'ROLE' => 'role']);

View File

@ -3,6 +3,7 @@
namespace App\Controllers; namespace App\Controllers;
use App\Models\UserModel;
use CodeIgniter\Controller; use CodeIgniter\Controller;
use CodeIgniter\HTTP\CLIRequest; use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\Files\UploadedFile; use CodeIgniter\HTTP\Files\UploadedFile;
@ -10,7 +11,6 @@ use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use App\Models\UserModel;
/** /**
* Class BaseController * Class BaseController
@ -401,6 +401,22 @@ abstract class BaseController extends Controller
} }
//Index 관련 //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 private function index_getPagination($pager_group = 'default', int $segment = 0, $template = 'bootstrap_full'): string
{ {
// 1.Views/Pagers/에 bootstrap_full.php,bootstrap_simple.php 생성 // 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); $this->_viewDatas['total_page'] = $pager->getPageCount($pager_group);
return $pager->links($pager_group, $template); return $pager->links($pager_group, $template);
} }
private function index_setCondition() private function index_getEntitys(): array
{
//조건절 처리
$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)
{ {
$this->index_setCondition(); $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() public function index()
{ {
@ -455,12 +458,13 @@ abstract class BaseController extends Controller
$this->_viewDatas['end'] = $this->request->getVar('end') ?: ''; $this->_viewDatas['end'] = $this->request->getVar('end') ?: '';
$this->_viewDatas['order_field'] = $this->request->getVar('order_field') ?: 'uid'; $this->_viewDatas['order_field'] = $this->request->getVar('order_field') ?: 'uid';
$this->_viewDatas['order_value'] = $this->request->getVar('order_value') ?: 'DESC'; $this->_viewDatas['order_value'] = $this->request->getVar('order_value') ?: 'DESC';
$this->_viewDatas['page'] = $this->request->getVar('page') ?: 1; $this->_viewDatas['page'] = (int)$this->request->getVar('page') ?: 1;
$this->_viewDatas['per_page'] = $this->request->getVar('per_page') ?: DEFAULTS['PERPAGE']; $this->_viewDatas['per_page'] = (int)$this->request->getVar('per_page') ?: DEFAULTS['PERPAGE'];
$this->_viewDatas['uri'] = $this->request->getUri(); $this->_viewDatas['uri'] = $this->request->getUri();
//Totalcount 처리 //Totalcount 처리
$this->index_setCondition(); $this->index_setCondition();
$this->_viewDatas['total_count'] = $this->_model->countAllResults(); $this->_viewDatas['total_count'] = $this->_model->countAllResults();
// log_message("debug", __METHOD__ . "에서 TotalCount 호출:" . $this->_model->getLastQuery());
//줄수 처리용 //줄수 처리용
$this->_viewDatas['pageOptions'] = array("" => "줄수선택"); $this->_viewDatas['pageOptions'] = array("" => "줄수선택");
for ($i = 10; $i <= $this->_viewDatas['total_count'] + $this->_viewDatas['per_page']; $i += 10) { for ($i = 10; $i <= $this->_viewDatas['total_count'] + $this->_viewDatas['per_page']; $i += 10) {
@ -469,8 +473,8 @@ abstract class BaseController extends Controller
//pagenation 처리 //pagenation 처리
$this->_viewDatas['pagination'] = $this->index_getPagination(); $this->_viewDatas['pagination'] = $this->index_getPagination();
//모델 처리 //모델 처리
$this->_viewDatas['entitys'] = $this->index_getEntitys((int)$this->_viewDatas['page'], (int)$this->_viewDatas['per_page']); $this->_viewDatas['entitys'] = $this->index_getEntitys();
// log_message("debug", __METHOD__ . "에서 호출:" . $this->_model->getLastQuery()); // log_message("debug", __METHOD__ . "에서 findAll 호출:" . $this->_model->getLastQuery());
//setting return_url to session flashdata //setting return_url to session flashdata
$this->_session->setFlashdata(SESSION_NAMES['RETURN_URL'], current_url() . '?' . $this->request->getUri()->getQuery() ?: ""); $this->_session->setFlashdata(SESSION_NAMES['RETURN_URL'], current_url() . '?' . $this->request->getUri()->getQuery() ?: "");
return view($this->_viewPath . '/index', $this->_viewDatas); return view($this->_viewPath . '/index', $this->_viewDatas);

View File

@ -5,16 +5,16 @@ namespace App\Controllers\Front;
use App\Entities\OrderEntity; use App\Entities\OrderEntity;
use App\Models\OrderModel; use App\Models\OrderModel;
use App\Models\ProductModel; use App\Models\ProductModel;
use CodeIgniter\Cookie\Cookie;
use CodeIgniter\Cookie\CookieStore;
use CodeIgniter\Cookie\Exceptions\CookieException; use CodeIgniter\Cookie\Exceptions\CookieException;
use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\HTTP\ResponseInterface;
use DateTime;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
class OrderController extends FrontController 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) public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{ {
$this->_model = new OrderModel($this->getFields()); $this->_model = new OrderModel($this->getFields());
@ -22,7 +22,6 @@ class OrderController extends FrontController
$this->_viewDatas['title'] = lang($this->_model->getClassName() . '.title'); $this->_viewDatas['title'] = lang($this->_model->getClassName() . '.title');
$this->_viewPath .= strtolower($this->_model->getClassName()); $this->_viewPath .= strtolower($this->_model->getClassName());
helper($this->_model->getClassName()); helper($this->_model->getClassName());
helper('cookie');
} }
final public function getFields(string $action = ""): array final public function getFields(string $action = ""): array
@ -48,43 +47,12 @@ class OrderController extends FrontController
return ["status"]; return ["status"];
} }
//쿠키에 장바구니에 담긴 Order UID를 추가해준다. //세션에 장바구니에 담긴 Order UID를 추가해준다.
private function setOrderCookie(OrderEntity $entity) private function setCart(OrderEntity $entity)
{ {
try { $order_uids = $this->_session->get(SESSION_NAMES['CART']) ?: array();
$order_uids = array(); $this->_session->set(SESSION_NAMES['CART'], [...$order_uids, $entity->getPrimaryKey()]);
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());
}
} }
// 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() protected function insert_process()
@ -111,7 +79,7 @@ class OrderController extends FrontController
//Transaction 시작 //Transaction 시작
$this->_model->transStart(); $this->_model->transStart();
$entity = $this->_model->create($this->_viewDatas['fieldDatas']); $entity = $this->_model->create($this->_viewDatas['fieldDatas']);
$this->setOrderCookie($entity); $this->setCart($entity);
//Transaction Commit //Transaction Commit
$this->_model->transComplete(); $this->_model->transComplete();
$msg = sprintf( $msg = sprintf(
@ -134,4 +102,17 @@ class OrderController extends FrontController
$this->_session->setFlashdata("return_message", $msg); $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);
}
}
} }

View File

@ -2,9 +2,9 @@
namespace App\Libraries\Adapter\Auth; namespace App\Libraries\Adapter\Auth;
use App\Entities\UserEntity;
use App\Models\UserModel; use App\Models\UserModel;
use App\Models\UserSNSModel; use App\Models\UserSNSModel;
use App\Entities\UserEntity;
// 참고:https://github.com/SyntaxPhoenix/iloclient // 참고:https://github.com/SyntaxPhoenix/iloclient
abstract class Adapter abstract class Adapter
@ -50,7 +50,7 @@ abstract class Adapter
{ {
$this->_session->set(SESSION_NAMES['ISLOGIN'], true); $this->_session->set(SESSION_NAMES['ISLOGIN'], true);
$auths = []; $auths = [];
foreach (AUTH_FIELDS as $key => $field) { foreach (array_values(AUTH_FIELDS) as $field) {
switch ($field) { switch ($field) {
case 'id': case 'id':
$auths[$field] = $entity->getPrimaryKey(); $auths[$field] = $entity->getPrimaryKey();