diff --git a/core/lib/Thelia/Action/Cart.php b/core/lib/Thelia/Action/Cart.php index 5090f0c3d..5856e22b9 100755 --- a/core/lib/Thelia/Action/Cart.php +++ b/core/lib/Thelia/Action/Cart.php @@ -52,49 +52,29 @@ class Cart extends BaseAction implements EventSubscriberInterface */ public function addArticle(CartEvent $event) { - $request = $event->getRequest(); - $message = null; - try { - $cartAdd = $this->getAddCartForm($request); - $form = $this->validateForm($cartAdd); + $cart = $event->cart; + $newness = $event->newness; + $append = $event->append; + $quantity = $event->quantity; - $cart = $event->getCart(); - $newness = $form->get("newness")->getData(); - $append = $form->get("append")->getData(); - $quantity = $form->get("quantity")->getData(); + $productSaleElementsId = $event->productSaleElementsId; + $productId = $event->product; - $productSaleElementsId = $form->get("product_sale_elements_id")->getData(); - $productId = $form->get("product")->getData(); + $cartItem = $this->findItem($cart->getId(), $productId, $productSaleElementsId); - $cartItem = $this->findItem($cart->getId(), $productId, $productSaleElementsId); + if ($cartItem === null || $newness) { + $productPrice = ProductPriceQuery::create() + ->filterByProductSaleElementsId($productSaleElementsId) + ->findOne(); - if ($cartItem === null || $newness) { - $productPrice = ProductPriceQuery::create() - ->filterByProductSaleElementsId($productSaleElementsId) - ->findOne() - ; - - $this->addItem($cart, $productId, $productSaleElementsId, $quantity, $productPrice); - } - - if ($append && $cartItem !== null) { - $this->updateQuantity($cartItem, $quantity); - } - - } 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 ($message) { - // The form has errors, propagate it. - $this->propagateFormError($cartAdd, $message, $event); + $this->addItem($cart, $productId, $productSaleElementsId, $quantity, $productPrice); } + if ($append && $cartItem !== null) { + $this->updateQuantity($cartItem, $quantity); + } } /** diff --git a/core/lib/Thelia/Config/Resources/routing/front.xml b/core/lib/Thelia/Config/Resources/routing/front.xml index 1d7840dbd..87adbe32f 100644 --- a/core/lib/Thelia/Config/Resources/routing/front.xml +++ b/core/lib/Thelia/Config/Resources/routing/front.xml @@ -19,11 +19,11 @@ connexion - - Thelia\Controller\Front\CartController::changeArticle + + Thelia\Controller\Front\CartController::addArticle - - Thelia\Controller\Front\CartController::deleteArticle + + Thelia\Controller\Front\CartController::changeArticle diff --git a/core/lib/Thelia/Controller/BaseController.php b/core/lib/Thelia/Controller/BaseController.php index abf7930cc..9d8daac36 100755 --- a/core/lib/Thelia/Controller/BaseController.php +++ b/core/lib/Thelia/Controller/BaseController.php @@ -32,6 +32,8 @@ use Thelia\Core\Template\ParserContext; use Thelia\Core\Event\ActionEvent; use Symfony\Component\EventDispatcher\EventDispatcher; use Thelia\Core\Factory\ActionEventFactory; +use Thelia\Form\BaseForm; +use Thelia\Action\Exception\FormValidationException; /** * @@ -89,7 +91,7 @@ class BaseController extends ContainerAware /** * Return the event dispatcher, * - * @return EventDispatcherInterface + * @return \Symfony\Component\EventDispatcher\EventDispatcher */ public function getDispatcher() { @@ -140,6 +142,32 @@ class BaseController extends ContainerAware return $request->getSession(); } + /** + * Validate a BaseForm + * + * @param BaseForm $aBaseForm the form + * @param string $expectedMethod the expected method, POST or GET, or null for any of them + * @throws FormValidationException is the form contains error, or the method is not the right one + * @return \Symfony\Component\Form\Form Form the symfony form object + */ + protected function validateForm(BaseForm $aBaseForm, $expectedMethod = null) + { + $form = $aBaseForm->getForm(); + + if ($expectedMethod == null || $aBaseForm->getRequest()->isMethod($expectedMethod)) { + + $form->bind($aBaseForm->getRequest()); + + if ($form->isValid()) { + return $form; + } else { + throw new FormValidationException("Missing or invalid data"); + } + } else { + throw new FormValidationException(sprintf("Wrong form method, %s expected.", $expectedMethod)); + } + } + /** * * redirect request to specify url diff --git a/core/lib/Thelia/Controller/Front/CartController.php b/core/lib/Thelia/Controller/Front/CartController.php index 760dade39..9b988ab15 100644 --- a/core/lib/Thelia/Controller/Front/CartController.php +++ b/core/lib/Thelia/Controller/Front/CartController.php @@ -22,8 +22,12 @@ /*************************************************************************************/ namespace Thelia\Controller\Front; +use Propel\Runtime\Exception\PropelException; +use Thelia\Action\Exception\FormValidationException; use Thelia\Core\Event\CartEvent; use Thelia\Core\Event\TheliaEvents; +use Symfony\Component\HttpFoundation\Request; +use Thelia\Form\CartAdd; class CartController extends BaseFrontController { @@ -31,14 +35,39 @@ class CartController extends BaseFrontController public function addArticle() { - $cartEvent = $this->getCartEvent(); + $request = $this->getRequest(); - $this->dispatch(TheliaEvents::CART_ADDITEM, $cartEvent); + $cartAdd = $this->getAddCartForm($request); + $message = null; - $this->redirectSuccess(); + try { + $form = $this->validateForm($cartAdd); + + $cartEvent = $this->getCartEvent(); + $cartEvent->newness = $form->get("newness")->getData(); + $cartEvent->append = $form->get("append")->getData(); + $cartEvent->quantity = $form->get("quantity")->getData(); + $cartEvent->productSaleElementsId = $form->get("product_sale_elements_id")->getData(); + $cartEvent->product = $form->get("product")->getData(); + + $this->getDispatcher()->dispatch(TheliaEvents::CART_ADDITEM, $cartEvent); + + $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 ($message) { + $cartAdd->setErrorMessage($e->getMessage()); + $this->getParserContext()->setErrorForm($cartAdd); + } } - public function modifyArticle() + public function changeArticle() { $cartEvent = $this->getCartEvent(); @@ -56,12 +85,40 @@ class CartController extends BaseFrontController $this->redirectSuccess(); } + /** + * use Thelia\Cart\CartTrait for searching current cart or create a new one + * + * @return CartEvent + */ protected function getCartEvent() { - $request = $this->getRequest(); - $cart = $this->getCart($request); + $cart = $this->getCart($this->getRequest()); - return new CartEvent($request, $cart); + 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; } } diff --git a/core/lib/Thelia/Core/Event/CartEvent.php b/core/lib/Thelia/Core/Event/CartEvent.php index a03a5679e..8cb072f12 100644 --- a/core/lib/Thelia/Core/Event/CartEvent.php +++ b/core/lib/Thelia/Core/Event/CartEvent.php @@ -23,21 +23,22 @@ namespace Thelia\Core\Event; +use Symfony\Component\EventDispatcher\Event; use Thelia\Core\HttpFoundation\Request; use Thelia\Model\Cart; -class CartEvent extends ActionEvent +class CartEvent extends Event { - protected $cart; + public $cart; + public $quantity; + public $append; + public $newness; + public $productSaleElementsId; + public $product; + public $cartItem; - public function __construct(Request $request, Cart $cart) + public function __construct(Cart $cart) { - parent::__construct($request); $this->cart = $cart; } - - public function getCart() - { - return $this->cart; - } } diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 1c6362ab3..ac3306c6a 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -116,7 +116,7 @@ final class TheliaEvents /** * sent when a new item is added to current cart */ - const AFTER_CARTADDITEM = "cart.addItem"; + const AFTER_CARTADDITEM = "cart.after.addItem"; /** * sent when a cart item is modify diff --git a/core/lib/Thelia/Form/BaseForm.php b/core/lib/Thelia/Form/BaseForm.php index e6a62defa..ac336f994 100755 --- a/core/lib/Thelia/Form/BaseForm.php +++ b/core/lib/Thelia/Form/BaseForm.php @@ -171,6 +171,7 @@ abstract class BaseForm */ public function setErrorMessage($message) { + $this->setError(true); $this->error_message = $message; } diff --git a/core/lib/Thelia/Model/CartItem.php b/core/lib/Thelia/Model/CartItem.php index ed40622cf..e82d266ea 100644 --- a/core/lib/Thelia/Model/CartItem.php +++ b/core/lib/Thelia/Model/CartItem.php @@ -23,7 +23,7 @@ class CartItem extends BaseCartItem if ($this->dispatcher) { $cartEvent = new CartEvent($this->getCart()); - $this->dispatcher->dispatch(TheliaEvents::CART_ADDITEM, $cartEvent); + $this->dispatcher->dispatch(TheliaEvents::AFTER_CARTADDITEM, $cartEvent); } } @@ -32,7 +32,7 @@ class CartItem extends BaseCartItem if ($this->dispatcher) { $cartEvent = new CartEvent($this->getCart()); - $this->dispatcher->dispatch(TheliaEvents::CART_MODIFYITEM, $cartEvent); + $this->dispatcher->dispatch(TheliaEvents::AFTER_CARTCHANGEITEM, $cartEvent); } }