Restored previous version

This commit is contained in:
Franck Allimant
2014-04-18 00:00:25 +02:00
parent 52ecfe7dad
commit 917b05b65e

View File

@@ -1,185 +1,173 @@
<?php <?php
/*************************************************************************************/ /*************************************************************************************/
/* */ /* */
/* Thelia */ /* Thelia */
/* */ /* */
/* Copyright (c) OpenStudio */ /* Copyright (c) OpenStudio */
/* email : info@thelia.net */ /* email : info@thelia.net */
/* web : http://www.thelia.net */ /* web : http://www.thelia.net */
/* */ /* */
/* This program is free software; you can redistribute it and/or modify */ /* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */ /* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */ /* the Free Software Foundation; either version 3 of the License */
/* */ /* */
/* This program is distributed in the hope that it will be useful, */ /* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */ /* GNU General Public License for more details. */
/* */ /* */
/* You should have received a copy of the GNU General Public License */ /* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */ /* */
/*************************************************************************************/ /*************************************************************************************/
namespace Front\Controller; namespace Front\Controller;
use Propel\Runtime\Exception\PropelException; use Propel\Runtime\Exception\PropelException;
use Thelia\Controller\Front\BaseFrontController; use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\Event\Order\OrderEvent; use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Form\Exception\FormValidationException; use Thelia\Form\Exception\FormValidationException;
use Thelia\Core\Event\Cart\CartEvent; use Thelia\Core\Event\Cart\CartEvent;
use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\TheliaEvents;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Thelia\Form\CartAdd; use Thelia\Form\CartAdd;
use Thelia\Model\AddressQuery; use Thelia\Model\AddressQuery;
use Thelia\Module\Exception\DeliveryException;
class CartController extends BaseFrontController
class CartController extends BaseFrontController {
{ use \Thelia\Cart\CartTrait;
use \Thelia\Cart\CartTrait;
public function addItem()
public function addItem() {
{ $request = $this->getRequest();
$request = $this->getRequest();
$cartAdd = $this->getAddCartForm($request);
$cartAdd = $this->getAddCartForm($request); $message = null;
$message = null;
try {
try { $form = $this->validateForm($cartAdd);
$form = $this->validateForm($cartAdd);
$cartEvent = $this->getCartEvent();
$cartEvent = $this->getCartEvent(); $cartEvent->setNewness($form->get("newness")->getData());
$cartEvent->setNewness($form->get("newness")->getData()); $cartEvent->setAppend($form->get("append")->getData());
$cartEvent->setAppend($form->get("append")->getData()); $cartEvent->setQuantity($form->get("quantity")->getData());
$cartEvent->setQuantity($form->get("quantity")->getData()); $cartEvent->setProductSaleElementsId($form->get("product_sale_elements_id")->getData());
$cartEvent->setProductSaleElementsId($form->get("product_sale_elements_id")->getData()); $cartEvent->setProduct($form->get("product")->getData());
$cartEvent->setProduct($form->get("product")->getData());
$this->getDispatcher()->dispatch(TheliaEvents::CART_ADDITEM, $cartEvent);
$this->getDispatcher()->dispatch(TheliaEvents::CART_ADDITEM, $cartEvent);
$this->afterModifyCart();
$this->afterModifyCart();
$this->redirectSuccess();
$this->redirectSuccess();
} catch (PropelException $e) {
} catch (PropelException $e) { \Thelia\Log\Tlog::getInstance()->error(sprintf("Failed to add item to cart with message : %s", $e->getMessage()));
\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";
$message = "Failed to add this article to your cart, please try again"; } catch (FormValidationException $e) {
} catch (FormValidationException $e) { $message = $e->getMessage();
$message = $e->getMessage(); }
}
// If Ajax Request
// If Ajax Request if ($this->getRequest()->isXmlHttpRequest()) {
if ($this->getRequest()->isXmlHttpRequest()) { $request = $this->getRequest();
$request = $this->getRequest(); $request->attributes->set('_view', "includes/mini-cart");
$request->attributes->set('_view', "includes/mini-cart"); }
}
if ($message) {
if ($message) { $cartAdd->setErrorMessage($message);
$cartAdd->setErrorMessage($message); $this->getParserContext()->addForm($cartAdd);
$this->getParserContext()->addForm($cartAdd); }
} }
}
public function changeItem()
public function changeItem() {
{ $cartEvent = $this->getCartEvent();
$cartEvent = $this->getCartEvent(); $cartEvent->setCartItem($this->getRequest()->get("cart_item"));
$cartEvent->setCartItem($this->getRequest()->get("cart_item")); $cartEvent->setQuantity($this->getRequest()->get("quantity"));
$cartEvent->setQuantity($this->getRequest()->get("quantity"));
try {
try { $this->dispatch(TheliaEvents::CART_UPDATEITEM, $cartEvent);
$this->dispatch(TheliaEvents::CART_UPDATEITEM, $cartEvent);
$this->afterModifyCart();
$this->afterModifyCart();
$this->redirectSuccess();
$this->redirectSuccess(); } catch (PropelException $e) {
} catch (PropelException $e) { $this->getParserContext()->setGeneralError($e->getMessage());
$this->getParserContext()->setGeneralError($e->getMessage()); }
}
}
}
public function deleteItem()
public function deleteItem() {
{ $cartEvent = $this->getCartEvent();
$cartEvent = $this->getCartEvent(); $cartEvent->setCartItem($this->getRequest()->get("cart_item"));
$cartEvent->setCartItem($this->getRequest()->get("cart_item"));
try {
try { $this->getDispatcher()->dispatch(TheliaEvents::CART_DELETEITEM, $cartEvent);
$this->getDispatcher()->dispatch(TheliaEvents::CART_DELETEITEM, $cartEvent);
$this->afterModifyCart();
$this->afterModifyCart();
$this->redirectSuccess();
$this->redirectSuccess(); } catch (PropelException $e) {
} catch (PropelException $e) { \Thelia\Log\Tlog::getInstance()->error(sprintf("error during deleting cartItem with message : %s", $e->getMessage()));
\Thelia\Log\Tlog::getInstance()->error(sprintf("error during deleting cartItem with message : %s", $e->getMessage())); $this->getParserContext()->setGeneralError($e->getMessage());
$this->getParserContext()->setGeneralError($e->getMessage()); }
}
}
}
/**
/** * use Thelia\Cart\CartTrait for searching current cart or create a new one
* use Thelia\Cart\CartTrait for searching current cart or create a new one *
* * @return \Thelia\Core\Event\Cart\CartEvent
* @return \Thelia\Core\Event\Cart\CartEvent */
*/ protected function getCartEvent()
protected function getCartEvent() {
{ $cart = $this->getCart($this->getDispatcher(), $this->getRequest());
$cart = $this->getCart($this->getDispatcher(), $this->getRequest());
return new CartEvent($cart);
return new CartEvent($cart); }
}
/**
/** * Find the good way to construct the cart form
* Find the good way to construct the cart form *
* * @param Request $request
* @param Request $request * @return CartAdd
* @return CartAdd */
*/ private function getAddCartForm(Request $request)
private function getAddCartForm(Request $request) {
{ if ($request->isMethod("post")) {
if ($request->isMethod("post")) { $cartAdd = new CartAdd($request);
$cartAdd = new CartAdd($request); } else {
} else { $cartAdd = new CartAdd(
$cartAdd = new CartAdd( $request,
$request, "form",
"form", array(),
array(), array(
array( 'csrf_protection' => false,
'csrf_protection' => false, )
) );
); }
}
return $cartAdd;
return $cartAdd; }
}
protected function afterModifyCart()
protected function afterModifyCart() {
{ /* recalculate postage amount */
/* recalculate postage amount */ $order = $this->getSession()->getOrder();
$order = $this->getSession()->getOrder(); if (null !== $order) {
if (null !== $order) { $deliveryModule = $order->getModuleRelatedByDeliveryModuleId();
$deliveryModule = $order->getModuleRelatedByDeliveryModuleId(); $deliveryAddress = AddressQuery::create()->findPk($order->chosenDeliveryAddress);
$deliveryAddress = AddressQuery::create()->findPk($order->chosenDeliveryAddress);
if (null !== $deliveryModule && null !== $deliveryAddress) {
if (null !== $deliveryModule && null !== $deliveryAddress) { $moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode()));
$moduleInstance = $this->container->get(sprintf('module.%s', $deliveryModule->getCode())); $postage = $moduleInstance->getPostage($deliveryAddress->getCountry());
$orderEvent = new OrderEvent($order); $orderEvent = new OrderEvent($order);
$orderEvent->setPostage($postage);
try {
$postage = $moduleInstance->getPostage($deliveryAddress->getCountry()); $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
}
$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);
}
}
}
}
}