diff --git a/app/Controllers/BaseController.php b/app/Controllers/BaseController.php index 3530708..82cd4d2 100644 --- a/app/Controllers/BaseController.php +++ b/app/Controllers/BaseController.php @@ -148,7 +148,7 @@ abstract class BaseController extends Controller return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL']))->with('return_message', $e->getMessage()); } } - protected function insert_process() + protected function insert_validate() { //fieldData Rule 검사 if (!$this->validate($this->_viewDatas['fieldRules'])) { @@ -160,13 +160,17 @@ abstract class BaseController extends Controller $this->_viewDatas['fieldDatas'] = $this->getFieldFormData($field); } } + protected function insert_process() + { + return $this->_model->create($this->_viewDatas['fieldDatas']); + } public function insert() { $msg = ""; try { $this->_viewDatas = $this->init(__FUNCTION__); - $this->insert_process(); - $entity = $this->_model->create($this->_viewDatas['fieldDatas']); + $this->insert_validate(); + $entity = $this->insert_process(); $msg = "{$this->_viewDatas['title']}에서 {$entity->getTitle()}의 " . __FUNCTION__ . " 완료하였습니다."; return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL'])); } catch (\Exception $e) { @@ -185,7 +189,7 @@ abstract class BaseController extends Controller $this->_viewDatas = $this->init(__FUNCTION__); $this->_viewDatas['forms'] = ['attributes' => ['method' => "post",], 'hiddens' => []]; helper(['form']); - $this->_session->keepFlashdata(SESSION_NAMES['RETURN_URL']); + $$this->_session->keepFlashdata(SESSION_NAMES['RETURN_URL']); $this->_viewDatas['entity'] = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]); return view($this->_viewPath . '/update', $this->_viewDatas); } catch (\Exception $e) { @@ -193,7 +197,7 @@ abstract class BaseController extends Controller return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL']))->with('return_message', $e->getMessage()); } } - protected function update_process() + protected function update_validate($entity) { //fieldData Rule 검사 if (!$this->validate($this->_viewDatas['fieldRules'])) { @@ -202,21 +206,25 @@ abstract class BaseController extends Controller //fieldData 적용 $this->_viewDatas['fieldDatas'] = array(); foreach ($this->_viewDatas['fields'] as $field) { - $this->_viewDatas['fieldDatas'] = $this->getFieldFormData($field, $this->_viewDatas['entity']); + $this->_viewDatas['fieldDatas'] = $this->getFieldFormData($field, $entity); log_message( "info", - "{$field} : {$this->_viewDatas['entity']->$field} => " . var_export($this->_viewDatas['fieldDatas'][$field]) + "{$field} : {$entity->$field} => " . var_export($this->_viewDatas['fieldDatas'][$field]) ); } } + protected function update_process($entity) + { + return $this->_model->modify($entity, $this->_viewDatas['fieldDatas']); + } public function update($uid) { $msg = ""; try { $this->_viewDatas = $this->init(__FUNCTION__); - $entity = $this->_viewDatas['entity'] = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]); - $this->update_process(); - $entity = $this->_model->modify($entity, $this->_viewDatas['fieldDatas']); + $entity = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]); + $this->update_validate($entity); + $entity = $this->update_process($entity); $msg = "{$this->_viewDatas['title']}에서 {$entity->getTitle()}의 " . __FUNCTION__ . " 완료하였습니다."; return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL'])); } catch (\Exception $e) { @@ -248,18 +256,22 @@ abstract class BaseController extends Controller return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL']))->with('return_message', $e->getMessage()); } } - protected function reply_process() + protected function reply_validate($entity) { - $this->update_process(); + $this->update_validate($entity); + } + protected function reply_process($entity) + { + return $this->_model->reply($entity, $this->_viewDatas['fieldDatas']); } public function reply($uid) { $msg = ""; try { $this->_viewDatas = $this->init(__FUNCTION__); - $entity = $this->_viewDatas['entity'] = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]); - $this->reply_process(); - $entity = $this->_model->reply($entity, $this->_viewDatas['fieldDatas']); + $entity = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]); + $this->reply_validate($entity); + $entity = $this->reply_process($entity); $msg = "{$this->_viewDatas['title']}에서 {$entity->getTitle()}의 " . __FUNCTION__ . " 완료하였습니다."; return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL'])); } catch (\Exception $e) { @@ -272,18 +284,22 @@ abstract class BaseController extends Controller } //Toggle 관련 - protected function toggle_process() + protected function toggle_validate($entity) { - $this->update_process(); + $this->update_validate($entity); + } + protected function toggle_process($entity) + { + return $this->update_process($entity); } public function toggle($uid, string $field) { $msg = ""; try { $this->_viewDatas = $this->init(__FUNCTION__, [$field]); - $entity = $this->_viewDatas['entity'] = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]); - $this->toggle_process(); - $entity = $this->_model->modify($entity, $this->_viewDatas['fieldDatas']); + $entity = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]); + $this->toggle_validate($entity); + $entity = $this->toggle_process($entity); $msg = "{$this->_viewDatas['title']}에서 {$entity->getTitle()}의 " . __FUNCTION__ . " 완료하였습니다."; return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL'])); } catch (\Exception $e) { @@ -295,9 +311,13 @@ abstract class BaseController extends Controller } } //Batchjob 관련 - protected function batchjob_process() + protected function batchjob_validate($entity) { - $this->update_process(); + $this->update_validate($entity); + } + protected function batchjob_process($entity) + { + return $this->update_process($entity); } public function batchjob() { @@ -317,14 +337,14 @@ abstract class BaseController extends Controller } $this->_viewDatas = $this->init(__FUNCTION__, $fields); $uids = $this->request->getVar('batchjob_uids') ?: throw new \Exception($this->_viewDatas['title'] . '에서 변경할 항목(uid)이 선택되지 않았습니다.'); - $cnt = 1; //Transaction 시작 $this->_model->transStart(); foreach ($uids as $uid) { try { - $entity = $this->_viewDatas['entity'] = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]); - $this->batchjob_process(); + $entity = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]); + $this->batchjob_validate($entity); + $entity = $this->batchjob_process($entity); array_push($entitys, $this->_model->modify($entity, $this->_viewDatas['fieldDatas'])); array_push($batchjobs, "{$cnt}. {$uid}->{$entity->getTitle()}는 완료."); } catch (\Exception $e) { @@ -360,16 +380,21 @@ abstract class BaseController extends Controller } //Delete 관련 + protected function delete_process($entity) + { + if (!$this->_model->delete($entity->getPrimaryKey())) { + log_message("error", __FUNCTION__ . "에서 호출:" . $this->_model->getLastQuery()); + log_message("error", implode("\n", $this->_model->errors())); + throw new \Exception(__FUNCTION__ . " 오류 발생.\n" . var_export($this->_model->errors(), true)); + } + return $entity; + } public function delete($uid) { $msg = ""; try { $entity = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]); - if (!$this->_model->delete($entity->getPrimaryKey())) { - log_message("error", __FUNCTION__ . "에서 호출:" . $this->_model->getLastQuery()); - log_message("error", implode("\n", $this->_model->errors())); - throw new \Exception(__FUNCTION__ . " 오류 발생.\n" . var_export($this->_model->errors(), true)); - } + $this->delete_process($entity); $msg = "{$this->_viewDatas['title']}에서 {$entity->getTitle()}의 " . __FUNCTION__ . " 완료하였습니다."; return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL'])); } catch (\Exception $e) { @@ -392,7 +417,7 @@ abstract class BaseController extends Controller $this->_viewDatas = $this->init(__FUNCTION__); helper(['form']); $this->_viewDatas['forms'] = ['attributes' => ['method' => "post",], 'hiddens' => []]; - $entity = $this->_viewDatas['entity'] = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]); + $this->_viewDatas['entity'] = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]); $this->_session->keepFlashdata(SESSION_NAMES['RETURN_URL']); return view($this->_viewPath . '/view', $this->_viewDatas); } catch (\Exception $e) { diff --git a/app/Controllers/Front/OrderController.php b/app/Controllers/Front/OrderController.php index 5cafeff..c107b7e 100644 --- a/app/Controllers/Front/OrderController.php +++ b/app/Controllers/Front/OrderController.php @@ -50,28 +50,31 @@ class OrderController extends FrontController $order_uids = $this->_session->get(SESSION_NAMES['CART']) ?: array(); $this->_session->set(SESSION_NAMES['CART'], [...$order_uids, $entity->getPrimaryKey()]); } - + protected function insert_process() + { + $this->_productModel ?: new ProductModel(); + //상품정보 가져오기 + $product = $this->_productModel->getEntity([$this->_model->getPrimaryKey() => $this->_viewDatas['fieldDatas']['product_uid']]); + //구매 금액 비교 + $price = ($product->getPrice() - $product->getSale()) * $this->_viewDatas['fieldDatas']['quantity']; + if ($price != $this->_viewDatas['fieldDatas']['price']) { + throw new \Exception("실 상품금액{$price} 와 구매금액{$this->_viewDatas['fieldDatas']['price']}이 서로 다릅니다."); + } + //상품재고 줄이기,Order(장바구니)에넣기,Order(장바구니)번호 세션넣기 + $this->_productModel->decreaseStock($product, $this->_viewDatas['fieldDatas']['quantity']); + //장바구니에 넣기 + $entity = parent::insert_process(); + //Cart세션정의 + $this->setCart($entity); + return $entity; + } public function insert() { $msg = ""; try { - $this->_viewDatas['fields'] = $this->getFields(__FUNCTION__); - $this->_viewDatas['fieldRules'] = $this->_model->getFieldRules($this->_viewDatas['fields'], __FUNCTION__); - $this->insert_process(); - $this->_productModel ?: new ProductModel(); - //상품정보 가져오기 - $product = $this->_productModel->getEntity([$this->_model->getPrimaryKey() => $this->_viewDatas['fieldDatas']['product_uid']]); - //구매 금액 비교 - $price = ($product->getPrice() - $product->getSale()) * $this->_viewDatas['fieldDatas']['quantity']; - if ($price != $this->_viewDatas['fieldDatas']['price']) { - throw new \Exception("실 상품금액{$price} 와 구매금액{$this->_viewDatas['fieldDatas']['price']}이 서로 다릅니다."); - } //Transaction 시작 $this->_model->transStart(); - //Order(장바구니)에 넣기,상품재고 줄이기,Order(장바구니)번호 세션넣기 - $entity = $this->_model->create($this->_viewDatas['fieldDatas']); - $this->_productModel->decreaseStock($product, $this->_viewDatas['fieldDatas']['quantity']); - $this->setCart($entity); + $this->insert_process(); //Transaction Commit $this->_model->transComplete(); $msg = sprintf( @@ -89,7 +92,7 @@ class OrderController extends FrontController $this->_viewDatas['title'], $e->getMessage() ); - return redirect()->to($this->_session->getFlashdata(SESSION_NAMES['RETURN_URL'])); + return redirect()->back()->withInput(); } finally { $this->_session->setFlashdata("return_message", $msg); }