shoppingmallv2 init...

This commit is contained in:
최준흠git config git config --helpgit config --global user.name 최준흠 2023-08-01 06:16:35 +09:00
parent 27d55ec628
commit 8260c76739
7 changed files with 50 additions and 162 deletions

View File

@ -36,22 +36,22 @@ abstract class BaseBackend
return $this->_model->getTitleField();
}
//Form Fields
public function getFields(string $action)
public function getFields(string $action): array
{
return $this->_model->getFields($action);
}
//Field별 Form Rule용
public function getFieldRules(array $fields, string $action)
public function getFieldRules(array $fields, string $action): array
{
return $this->_model->getFieldRules($fields, $action);
}
//Field별 Form Filter용
public function getFieldFilters()
public function getFieldFilters(): array
{
return $this->_model->getFieldFilters();
}
//Field별 Form BatchFilter용
public function getFieldBatchFilters()
public function getFieldBatchFilters(): array
{
return $this->_model->getFieldBatchFilters();
}

View File

@ -2,13 +2,16 @@
namespace App\Backend;
use App\Models\OrderModel;
use App\Models\ProductModel;
class EcommerceBackend extends BaseBackend
{
private $_product = null;
private $_order = null;
public function __construct()
{
parent::__construct('Order');
parent::__construct('Ecommerce');
$this->_product = new ProductBackend();
$this->_order = new OrderBackend();
}
@ -22,71 +25,39 @@ class EcommerceBackend extends BaseBackend
$this->_product;
}
//초기선언부--------
//Title Field
public function getTitleField()
{
return "product_uid";
}
//Form Fields
public function getFields(string $action = ""): array
public function getFields(string $action): array
{
$fields = ['product_uid', "quantity", "price"];
switch ($action) {
default:
return $fields;
break;
}
return ["product_uid", "quantity", "price", "status"];
}
public function getFieldFilters(): array
{
return ['product_uid', "user_uid"];
}
public function getFieldBatchFilters(): array
{
return ["status"];
}
public function getFieldRule(string $field, array $rules, string $action = ""): array
{
switch ($field) {
case $this->getTitleField():
case "product_uid":
$rules[$field] = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";
break;
case 'quantity':
case 'price':
$rules[$field] = "required|numeric";
break;
default:
throw new \Exception(__FUNCTION__ . "에서 오류발생: Field명:{$field}는 사용할수없습니다.");
break;
}
return $rules;
}
public function getFieldRules(array $fields, string $action)
//Field별 Form Rule용
public function getFieldRules(array $fields, string $action): array
{
$rules = array();
foreach ($fields as $field) {
$rules = $this->getFieldRule($field, $rules, $action);
switch ($field) {
case "product_uid":
$rules[$field] = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";
break;
case "sess_id":
$rules[$field] = "required|string";
break;
case 'quantity':
case 'price':
$rules[$field] = "required|numeric";
break;
default:
break;
}
}
return $rules;
}
//Entity값 가져오기
public function getEntity($uid)
{
throw new \Exception(__FUNCTION__ . "에서 오류발생: 사용할수없습니다.");
}
public function getEntitys($condition)
{
throw new \Exception(__FUNCTION__ . "에서 오류발생: 사용할수없습니다.");
}
//-------초기선언부
//장바구니에 담기
public function addCart(string $product_uid, int $quantity, int $price)
public function addCart(array $fieldDatas)
{
$product = $this->_product->getEntity($product_uid);
$this->_product->addCart($product, $quantity, $price);
$this->_order->addCart($product, $quantity, $price);
$product = $this->_product->getEntity($fieldDatas['product_uid']);
$this->_product->addCart($product, $fieldDatas['quantity'], $fieldDatas['price']);
$this->_order->addCart($product, $fieldDatas['quantity'], $fieldDatas['price']);
}
}

View File

@ -129,7 +129,6 @@ $routes->group('admin', ['namespace' => 'App\Controllers\Admin', 'filter' => 'au
$routes->group('ecommerce', static function ($routes) {
$routes->get('', 'EcommerceController::index');
$routes->post('addcart', 'EcommerceController::addCart');
$routes->get('view/(:uuid)', 'EcommerceController::viewCart/$1');
});
});
$routes->group('front', ['namespace' => 'App\Controllers\Front'], function ($routes) {

View File

@ -16,18 +16,31 @@ class EcommerceController extends AdminController
$this->_viewPath .= strtolower($this->_backend->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 = $this->init(__FUNCTION__);
$this->_viewDatas['fields'] = $this->_backend->getFields(__FUNCTION__);
$this->_viewDatas['fieldRules'] = $this->_backend->getFieldRules($this->_viewDatas['fields'], __FUNCTION__);
//Transaction 시작
$this->_backend->transStart();
$this->insert_process();
// $this->_backend->transStart();
$this->addCart_process();
$this->_backend->addCart($this->_viewDatas['fieldDatas']);
//Transaction Commit
$this->_backend->transCommit();
// $this->_backend->transCommit();
$msg = sprintf(
"%s에서 해당 상품 %s개를 장바구니에 담았습니다.",
$this->_viewDatas['title'],
@ -35,7 +48,7 @@ class EcommerceController extends AdminController
);
} catch (\Exception $e) {
//Transaction Rollback
$this->_backend->transRollback();
// $this->_backend->transRollback();
$msg = sprintf(
"%s에서 다음 오류로 인해 장바구니에 담기를 실패하였습니다.\n%s",
$this->_viewDatas['title'],
@ -48,40 +61,4 @@ class EcommerceController extends AdminController
return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL']));
}
}
//View관련
public function viewCart($order_uid)
{
try {
$this->_viewDatas = $this->init(__FUNCTION__);
helper(['form']);
$this->_viewDatas['forms'] = ['attributes' => ['method' => "post",], 'hiddens' => []];
$this->_viewDatas['order'] = $this->_backend->getOrderBackend()->getEntity($order_uid);
$this->_viewDatas['product'] = $this->_backend->getProductBackend()->getEntity($this->_viewDatas['order']->getProduct_uid());
$this->_session->keepFlashdata(SESSION_NAMES['RETURN_URL']);
return view($this->_viewPath . '/view', $this->_viewDatas);
} catch (\Exception $e) {
return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL']))->with('return_message', $e->getMessage());
}
}
public function index()
{
try {
$this->_viewDatas = $this->init(__FUNCTION__);
helper(['form']);
$this->_viewDatas['forms'] = ['attributes' => ['method' => "post",], 'hiddens' => []];
foreach ($this->_viewDatas['fieldFilters'] as $field) {
$this->_viewDatas[$field] = $this->request->getVar($field) ?: DEFAULTS['EMPTY'];
}
//모델 처리
$this->_viewDatas['entitys'] = $this->_backend->getOrderBackend()->getEntitys(['status' => DEFAULTS['STATUS']]);
// log_message("debug", __METHOD__ . "에서 호출:" . $this->_backend->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);
} catch (\Exception $e) {
return redirect()->back()->with('return_message', $e->getMessage());
}
}
}

View File

@ -1,59 +0,0 @@
<?php
namespace App\Controllers\Front;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
class EcommerceController extends FrontController
{
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
$this->_backend = service('ecommerce');
parent::initController($request, $response, $logger);
$this->_viewPath .= strtolower($this->_backend->getClassName());
}
//장바구니에 담기
public function addCart()
{
$msg = "";
try {
$this->_viewDatas = $this->init(__FUNCTION__);
//Transaction 시작
$this->_backend->transStart();
$this->insert_process();
$this->_backend->addCart($this->_viewDatas['fieldDatas']);
//Transaction Commit
$this->_backend->transCommit();
$msg = sprintf(
"%s에서 해당 상품 %s개를 장바구니에 담았습니다.",
$this->_viewDatas['title'],
$this->_viewDatas['fieldDatas']['quantity']
);
} catch (\Exception $e) {
//Transaction Rollback
$this->_backend->transRollback();
$msg = sprintf(
"%s에서 다음 오류로 인해 장바구니에 담기를 실패하였습니다.\n%s",
$this->_viewDatas['title'],
$e->getMessage()
);
log_message("error", $e->getMessage());
log_message("error", var_export($this->_viewDatas['fieldDatas'], true));
} finally {
$this->_session->setFlashdata("return_message", $msg);
return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL']));
}
}
//View관련
protected function view_process($entity)
{
$this->_viewDatas['user'] = $this->_backend->getUserModel()->getEntity([$this->_backend->getUserModel()->getPrimaryKey() => $entity->getUser_uid()]);
$this->_viewDatas['product'] = $this->_backend->getProductModel()->getEntity([$this->_backend->getProductModel()->getPrimaryKey() => $entity->getProduct_uid()]);
return parent::view_process($entity);
}
}

View File

@ -1,6 +1,6 @@
<?php
return [
'title' => "장바구니 정보",
'title' => "주문 정보",
'label' => [
'uid' => "번호",
'product_uid' => "상품정보",

View File

@ -1,4 +1,4 @@
<?= form_open("admin/ecommerce/addcart/" . $entity->getPrimaryKey(), ['method' => 'post']) ?>
<?= form_open("admin/ecommerce/addcart", ['method' => 'post']) ?>
<?= form_hidden("product_uid", $entity->getPrimaryKey()) ?>
<?= form_hidden("price", $entity->getPrice() - $entity->getSale()) ?>
구매 수량 : <?= getFieldForm_ProductHelper('quantity', 1, $fieldFormOptions) ?>