From acaa4a969e329eea4bb0a1847a43b57a15a7a0eb Mon Sep 17 00:00:00 2001 From: Etienne Roudeix Date: Tue, 17 Sep 2013 16:38:16 +0200 Subject: [PATCH 1/8] empty cart or delivery exception --- .../lib/Thelia/Config/Resources/routing/front.xml | 4 ++-- .../Thelia/Controller/Front/OrderController.php | 4 ++-- .../Thelia/Core/EventListener/ViewListener.php | 15 +++++++++++++++ .../Core/HttpFoundation/Session/Session.php | 3 +++ core/lib/Thelia/Core/Routing/RewritingRouter.php | 2 +- .../Core/Template/Smarty/Plugins/Security.php | 13 ++++++++++++- core/lib/Thelia/Exception/OrderException.php | 15 ++++++++++++++- templates/default/order_invoice.html | 14 ++++++++++---- 8 files changed, 59 insertions(+), 11 deletions(-) diff --git a/core/lib/Thelia/Config/Resources/routing/front.xml b/core/lib/Thelia/Config/Resources/routing/front.xml index 7705c81cc..e702a1938 100755 --- a/core/lib/Thelia/Config/Resources/routing/front.xml +++ b/core/lib/Thelia/Config/Resources/routing/front.xml @@ -112,12 +112,12 @@ cart - + Thelia\Controller\Front\OrderController::deliver order_delivery - + Thelia\Controller\Front\DefaultController::noAction order_delivery diff --git a/core/lib/Thelia/Controller/Front/OrderController.php b/core/lib/Thelia/Controller/Front/OrderController.php index f34715894..393f71ebd 100755 --- a/core/lib/Thelia/Controller/Front/OrderController.php +++ b/core/lib/Thelia/Controller/Front/OrderController.php @@ -69,9 +69,9 @@ class OrderController extends BaseFrontController /* check that the delivery module fetch the delivery address area */ if(AreaDeliveryModuleQuery::create() ->filterByAreaId($deliveryAddress->getCountry()->getAreaId()) - ->filterByDeliveryModuleId() + ->filterByDeliveryModuleId($deliveryModuleId) ->count() == 0) { - throw new \Exception("PUKE"); + throw new \Exception("Delivery module cannot be use with selected delivery address"); } diff --git a/core/lib/Thelia/Core/EventListener/ViewListener.php b/core/lib/Thelia/Core/EventListener/ViewListener.php index c723c2112..9de281dbe 100755 --- a/core/lib/Thelia/Core/EventListener/ViewListener.php +++ b/core/lib/Thelia/Core/EventListener/ViewListener.php @@ -28,8 +28,10 @@ use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Router; use Thelia\Core\Template\Exception\ResourceNotFoundException; use Thelia\Core\Template\ParserInterface; +use Thelia\Exception\OrderException; use Thelia\Tools\Redirect; use Thelia\Tools\URL; use Thelia\Core\Security\Exception\AuthenticationException; @@ -87,6 +89,19 @@ class ViewListener implements EventSubscriberInterface // Redirect to the login template Redirect::exec($this->container->get('thelia.url.manager')->viewUrl($ex->getLoginTemplate())); + } catch (OrderException $e) { + switch($e->getCode()) { + case OrderException::CART_EMPTY: + // Redirect to the cart template + Redirect::exec($this->container->get('router.chainRequest')->generate($e->cartRoute, $e->arguments, Router::ABSOLUTE_URL)); + break; + case OrderException::UNDEFINED_DELIVERY: + // Redirect to the delivery choice template + Redirect::exec($this->container->get('router.chainRequest')->generate($e->orderDeliveryRoute, $e->arguments, Router::ABSOLUTE_URL)); + break; + } + + throw $e; } } diff --git a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php index 5edd007b3..8a0952ff4 100755 --- a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php +++ b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php @@ -218,6 +218,9 @@ class Session extends BaseSession return $this; } + /** + * @return Order + */ public function getOrder() { return $this->get("thelia.order"); diff --git a/core/lib/Thelia/Core/Routing/RewritingRouter.php b/core/lib/Thelia/Core/Routing/RewritingRouter.php index 2b663a979..9b736a614 100644 --- a/core/lib/Thelia/Core/Routing/RewritingRouter.php +++ b/core/lib/Thelia/Core/Routing/RewritingRouter.php @@ -128,7 +128,7 @@ class RewritingRouter implements RouterInterface, RequestMatcherInterface */ public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH) { - // TODO: Implement generate() method. + throw new RouteNotFoundException(); } /** diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/Security.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/Security.php index 71b9c3f81..abe84a292 100755 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/Security.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/Security.php @@ -78,7 +78,17 @@ class Security extends AbstractSmartyPlugin { $cart = $this->request->getSession()->getCart(); if($cart===null || $cart->countCartItems() == 0) { - throw new OrderException('Cart must not be empty', OrderException::CART_EMPTY); + throw new OrderException('Cart must not be empty', OrderException::CART_EMPTY, array('empty' => 1)); + } + + return ""; + } + + public function checkValidDeliveryFunction($params, &$smarty) + { + $order = $this->request->getSession()->getOrder(); + if(null === $order || null === $order->chosenDeliveryAddress || null === $order->getDeliveryModuleId()) { + throw new OrderException('Delivery must be defined', OrderException::UNDEFINED_DELIVERY, array('missing' => 1)); } return ""; @@ -94,6 +104,7 @@ class Security extends AbstractSmartyPlugin return array( new SmartyPluginDescriptor('function', 'check_auth', $this, 'checkAuthFunction'), new SmartyPluginDescriptor('function', 'check_cart_not_empty', $this, 'checkCartNotEmptyFunction'), + new SmartyPluginDescriptor('function', 'check_valid_delivery', $this, 'checkValidDeliveryFunction'), ); } } diff --git a/core/lib/Thelia/Exception/OrderException.php b/core/lib/Thelia/Exception/OrderException.php index d276f8b59..70fd21c01 100755 --- a/core/lib/Thelia/Exception/OrderException.php +++ b/core/lib/Thelia/Exception/OrderException.php @@ -25,12 +25,25 @@ namespace Thelia\Exception; class OrderException extends \RuntimeException { + /** + * @var string The cart template name + */ + public $cartRoute = "cart.view"; + public $orderDeliveryRoute = "order.delivery"; + + public $arguments = array(); + const UNKNOWN_EXCEPTION = 0; const CART_EMPTY = 100; - public function __construct($message, $code = null, $previous = null) + const UNDEFINED_DELIVERY = 200; + + public function __construct($message, $code = null, $arguments = array(), $previous = null) { + if(is_array($arguments)) { + $this->arguments = $arguments; + } if ($code === null) { $code = self::UNKNOWN_EXCEPTION; } diff --git a/templates/default/order_invoice.html b/templates/default/order_invoice.html index f1a6e4205..ba9b7b677 100644 --- a/templates/default/order_invoice.html +++ b/templates/default/order_invoice.html @@ -1,5 +1,11 @@ {extends file="layout.tpl"} +{block name="no-return-functions"} + {check_auth context="front" roles="CUSTOMER" login_tpl="login"} + {check_cart_not_empty} + {check_valid_delivery} +{/block} + {block name="breadcrumb"}