diff --git a/local/modules/Front/Controller/CartController.php b/local/modules/Front/Controller/CartController.php index ed3875756..59110d62a 100644 --- a/local/modules/Front/Controller/CartController.php +++ b/local/modules/Front/Controller/CartController.php @@ -1,185 +1,173 @@ -. */ -/* */ -/*************************************************************************************/ -namespace Front\Controller; - -use Propel\Runtime\Exception\PropelException; -use Thelia\Controller\Front\BaseFrontController; -use Thelia\Core\Event\Order\OrderEvent; -use Thelia\Form\Exception\FormValidationException; -use Thelia\Core\Event\Cart\CartEvent; -use Thelia\Core\Event\TheliaEvents; -use Symfony\Component\HttpFoundation\Request; -use Thelia\Form\CartAdd; -use Thelia\Model\AddressQuery; -use Thelia\Module\Exception\DeliveryException; - -class CartController extends BaseFrontController -{ - use \Thelia\Cart\CartTrait; - - public function addItem() - { - $request = $this->getRequest(); - - $cartAdd = $this->getAddCartForm($request); - $message = null; - - try { - $form = $this->validateForm($cartAdd); - - $cartEvent = $this->getCartEvent(); - $cartEvent->setNewness($form->get("newness")->getData()); - $cartEvent->setAppend($form->get("append")->getData()); - $cartEvent->setQuantity($form->get("quantity")->getData()); - $cartEvent->setProductSaleElementsId($form->get("product_sale_elements_id")->getData()); - $cartEvent->setProduct($form->get("product")->getData()); - - $this->getDispatcher()->dispatch(TheliaEvents::CART_ADDITEM, $cartEvent); - - $this->afterModifyCart(); - - $this->redirectSuccess(); - - } catch (PropelException $e) { - \Thelia\Log\Tlog::getInstance()->error(sprintf("Failed to add item to cart with message : %s", $e->getMessage())); - $message = "Failed to add this article to your cart, please try again"; - } catch (FormValidationException $e) { - $message = $e->getMessage(); - } - - // If Ajax Request - if ($this->getRequest()->isXmlHttpRequest()) { - $request = $this->getRequest(); - $request->attributes->set('_view', "includes/mini-cart"); - } - - if ($message) { - $cartAdd->setErrorMessage($message); - $this->getParserContext()->addForm($cartAdd); - } - } - - public function changeItem() - { - $cartEvent = $this->getCartEvent(); - $cartEvent->setCartItem($this->getRequest()->get("cart_item")); - $cartEvent->setQuantity($this->getRequest()->get("quantity")); - - try { - $this->dispatch(TheliaEvents::CART_UPDATEITEM, $cartEvent); - - $this->afterModifyCart(); - - $this->redirectSuccess(); - } catch (PropelException $e) { - $this->getParserContext()->setGeneralError($e->getMessage()); - } - - } - - public function deleteItem() - { - $cartEvent = $this->getCartEvent(); - $cartEvent->setCartItem($this->getRequest()->get("cart_item")); - - try { - $this->getDispatcher()->dispatch(TheliaEvents::CART_DELETEITEM, $cartEvent); - - $this->afterModifyCart(); - - $this->redirectSuccess(); - } catch (PropelException $e) { - \Thelia\Log\Tlog::getInstance()->error(sprintf("error during deleting cartItem with message : %s", $e->getMessage())); - $this->getParserContext()->setGeneralError($e->getMessage()); - } - - } - - /** - * use Thelia\Cart\CartTrait for searching current cart or create a new one - * - * @return \Thelia\Core\Event\Cart\CartEvent - */ - protected function getCartEvent() - { - $cart = $this->getCart($this->getDispatcher(), $this->getRequest()); - - return new CartEvent($cart); - } - - /** - * Find the good way to construct the cart form - * - * @param Request $request - * @return CartAdd - */ - private function getAddCartForm(Request $request) - { - if ($request->isMethod("post")) { - $cartAdd = new CartAdd($request); - } else { - $cartAdd = new CartAdd( - $request, - "form", - array(), - array( - 'csrf_protection' => false, - ) - ); - } - - return $cartAdd; - } - - protected function afterModifyCart() - { - /* recalculate postage amount */ - $order = $this->getSession()->getOrder(); - if (null !== $order) { - $deliveryModule = $order->getModuleRelatedByDeliveryModuleId(); - $deliveryAddress = AddressQuery::create()->findPk($order->chosenDeliveryAddress); - - if (null !== $deliveryModule && null !== $deliveryAddress) { - $moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode())); - - $orderEvent = new OrderEvent($order); - - try { - $postage = $moduleInstance->getPostage($deliveryAddress->getCountry()); - - $orderEvent->setPostage($postage); - - $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent); - } - catch (DeliveryException $ex) { - // The postage has been chosen, but changes in the cart causes an exception. - // Reset the postage data in the order - $orderEvent->setDeliveryModule(0); - - $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent); - } - } - } - } - -} +. */ +/* */ +/*************************************************************************************/ +namespace Front\Controller; + +use Propel\Runtime\Exception\PropelException; +use Thelia\Controller\Front\BaseFrontController; +use Thelia\Core\Event\Order\OrderEvent; +use Thelia\Form\Exception\FormValidationException; +use Thelia\Core\Event\Cart\CartEvent; +use Thelia\Core\Event\TheliaEvents; +use Symfony\Component\HttpFoundation\Request; +use Thelia\Form\CartAdd; +use Thelia\Model\AddressQuery; + +class CartController extends BaseFrontController +{ + use \Thelia\Cart\CartTrait; + + public function addItem() + { + $request = $this->getRequest(); + + $cartAdd = $this->getAddCartForm($request); + $message = null; + + try { + $form = $this->validateForm($cartAdd); + + $cartEvent = $this->getCartEvent(); + $cartEvent->setNewness($form->get("newness")->getData()); + $cartEvent->setAppend($form->get("append")->getData()); + $cartEvent->setQuantity($form->get("quantity")->getData()); + $cartEvent->setProductSaleElementsId($form->get("product_sale_elements_id")->getData()); + $cartEvent->setProduct($form->get("product")->getData()); + + $this->getDispatcher()->dispatch(TheliaEvents::CART_ADDITEM, $cartEvent); + + $this->afterModifyCart(); + + $this->redirectSuccess(); + + } catch (PropelException $e) { + \Thelia\Log\Tlog::getInstance()->error(sprintf("Failed to add item to cart with message : %s", $e->getMessage())); + $message = "Failed to add this article to your cart, please try again"; + } catch (FormValidationException $e) { + $message = $e->getMessage(); + } + + // If Ajax Request + if ($this->getRequest()->isXmlHttpRequest()) { + $request = $this->getRequest(); + $request->attributes->set('_view', "includes/mini-cart"); + } + + if ($message) { + $cartAdd->setErrorMessage($message); + $this->getParserContext()->addForm($cartAdd); + } + } + + public function changeItem() + { + $cartEvent = $this->getCartEvent(); + $cartEvent->setCartItem($this->getRequest()->get("cart_item")); + $cartEvent->setQuantity($this->getRequest()->get("quantity")); + + try { + $this->dispatch(TheliaEvents::CART_UPDATEITEM, $cartEvent); + + $this->afterModifyCart(); + + $this->redirectSuccess(); + } catch (PropelException $e) { + $this->getParserContext()->setGeneralError($e->getMessage()); + } + + } + + public function deleteItem() + { + $cartEvent = $this->getCartEvent(); + $cartEvent->setCartItem($this->getRequest()->get("cart_item")); + + try { + $this->getDispatcher()->dispatch(TheliaEvents::CART_DELETEITEM, $cartEvent); + + $this->afterModifyCart(); + + $this->redirectSuccess(); + } catch (PropelException $e) { + \Thelia\Log\Tlog::getInstance()->error(sprintf("error during deleting cartItem with message : %s", $e->getMessage())); + $this->getParserContext()->setGeneralError($e->getMessage()); + } + + } + + /** + * use Thelia\Cart\CartTrait for searching current cart or create a new one + * + * @return \Thelia\Core\Event\Cart\CartEvent + */ + protected function getCartEvent() + { + $cart = $this->getCart($this->getDispatcher(), $this->getRequest()); + + return new CartEvent($cart); + } + + /** + * Find the good way to construct the cart form + * + * @param Request $request + * @return CartAdd + */ + private function getAddCartForm(Request $request) + { + if ($request->isMethod("post")) { + $cartAdd = new CartAdd($request); + } else { + $cartAdd = new CartAdd( + $request, + "form", + array(), + array( + 'csrf_protection' => false, + ) + ); + } + + return $cartAdd; + } + + protected function afterModifyCart() + { + /* recalculate postage amount */ + $order = $this->getSession()->getOrder(); + if (null !== $order) { + $deliveryModule = $order->getModuleRelatedByDeliveryModuleId(); + $deliveryAddress = AddressQuery::create()->findPk($order->chosenDeliveryAddress); + + if (null !== $deliveryModule && null !== $deliveryAddress) { + $moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode())); + $postage = $moduleInstance->getPostage($deliveryAddress->getCountry()); + + $orderEvent = new OrderEvent($order); + $orderEvent->setPostage($postage); + + $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent); + } + } + } + +}