From afa9a46abf332ee2850996d2c078b9be8e253172 Mon Sep 17 00:00:00 2001 From: Etienne Roudeix Date: Mon, 16 Sep 2013 18:55:29 +0200 Subject: [PATCH] order delivery form --- core/lib/Thelia/Config/Resources/config.xml | 2 + .../Thelia/Config/Resources/routing/front.xml | 15 ++- .../Controller/Front/OrderController.php | 79 ++++++++++++++ core/lib/Thelia/Core/Event/OrderEvent.php | 101 ++++++++++++++++++ core/lib/Thelia/Core/Event/TheliaEvents.php | 9 +- .../Core/HttpFoundation/Session/Session.php | 20 ++-- core/lib/Thelia/Form/OrderDelivery.php | 94 ++++++++++++++++ templates/default/cart.html | 2 +- templates/default/cart_billing.html | 4 +- 9 files changed, 307 insertions(+), 19 deletions(-) create mode 100755 core/lib/Thelia/Controller/Front/OrderController.php create mode 100755 core/lib/Thelia/Core/Event/OrderEvent.php create mode 100755 core/lib/Thelia/Form/OrderDelivery.php diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index 691cf2198..0eda0c37b 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -58,6 +58,8 @@
+ + diff --git a/core/lib/Thelia/Config/Resources/routing/front.xml b/core/lib/Thelia/Config/Resources/routing/front.xml index cea964153..a8b91a05d 100755 --- a/core/lib/Thelia/Config/Resources/routing/front.xml +++ b/core/lib/Thelia/Config/Resources/routing/front.xml @@ -98,11 +98,6 @@ cart - - Thelia\Controller\Front\DefaultController::noAction - cart_billing - - Thelia\Controller\Front\CartController::addItem @@ -117,6 +112,16 @@ cart + + Thelia\Controller\Front\DefaultController::noAction + cart_billing + + + + Thelia\Controller\Front\OrderController::deliver + cart_billing + + diff --git a/core/lib/Thelia/Controller/Front/OrderController.php b/core/lib/Thelia/Controller/Front/OrderController.php new file mode 100755 index 000000000..0508b4dc9 --- /dev/null +++ b/core/lib/Thelia/Controller/Front/OrderController.php @@ -0,0 +1,79 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Controller\Front; + +use Propel\Runtime\Exception\PropelException; +use Thelia\Form\Exception\FormValidationException; +use Thelia\Core\Event\OrderEvent; +use Thelia\Core\Event\TheliaEvents; +use Symfony\Component\HttpFoundation\Request; +use Thelia\Model\Order; + +class OrderController extends BaseFrontController +{ + /** + * set billing address + * set delivery address + * set delivery module + */ + public function deliver() + { + $orderEvent = $this->getOrderEvent(); + //$orderEvent->setBillingAddress($this->getRequest()->get("billing-address")); + $orderEvent->setDeliveryAddress($this->getRequest()->get("delivery-address")); + $orderEvent->setDeliveryModule($this->getRequest()->get("delivery-module")); + + try { + //$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_BILLING_ADDRESS, $orderEvent); + $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_ADDRESS, $orderEvent); + $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent); + + $this->redirectSuccess(); + } catch (PropelException $e) { + $this->getParserContext()->setGeneralError($e->getMessage()); + } + + } + + protected function getOrderEvent() + { + $order = $this->getOrder($this->getRequest()); + + return new OrderEvent($order); + } + + public function getOrder(Request $request) + { + $session = $request->getSession(); + + if (null !== $order = $session->getOrder()) { + return $order; + } + + $order = new Order(); + + $session->setOrder($order); + + return $order; + } +} diff --git a/core/lib/Thelia/Core/Event/OrderEvent.php b/core/lib/Thelia/Core/Event/OrderEvent.php new file mode 100755 index 000000000..8b6762e99 --- /dev/null +++ b/core/lib/Thelia/Core/Event/OrderEvent.php @@ -0,0 +1,101 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event; + +use Thelia\Model\Address; +use Thelia\Model\AddressQuery; +use Thelia\Model\Module; +use Thelia\Model\Order; + +class OrderEvent extends ActionEvent +{ + protected $order = null; + protected $billingAddress = null; + protected $deliveryAddress = null; + protected $deliveryModule = null; + + /** + * @param Order $order + */ + public function __construct(Order $order) + { + $this->order = $order; + } + + /** + * @param Address $address + */ + public function setBillingAddress(Address $address) + { + $this->deliveryAddress = $address->getId(); + } + + /** + * @param Address $address + */ + public function setDeliveryAddress(Address $address) + { + $this->deliveryAddress = $address->getId(); + } + + /** + * @param Module $module + */ + public function setDeliveryModule(Module $module) + { + $this->deliveryModule = $module->getId(); + } + + /** + * @return null|Order + */ + public function getOrder() + { + return $this->order; + } + + /** + * @return array|mixed|Address + */ + public function getBillingAddress() + { + return AddressQuery::create()->findPk($this->billingAddress); + } + + /** + * @return array|mixed|Address + */ + public function getDeliveryAddress() + { + return AddressQuery::create()->findPk($this->deliveryAddress); + } + + /** + * @return array|mixed|Address + */ + public function getDeliveryModule() + { + return AddressQuery::create()->findPk($this->deliveryModule); + } +} diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 2867762aa..02256a360 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -218,13 +218,20 @@ final class TheliaEvents const CART_DELETEITEM = "action.deleteArticle"; + /** + * Order linked event + */ + const ORDER_SET_BILLING_ADDRESS = "action.order.setBillingAddress"; + const ORDER_SET_DELIVERY_ADDRESS = "action.order.setDeliveryAddress"; + const ORDER_SET_DELIVERY_MODULE = "action.order.setDeliveryModule"; + /** * Sent on image processing */ const IMAGE_PROCESS = "action.processImage"; /** - * Sent on cimage cache clear request + * Sent on image cache clear request */ const IMAGE_CLEAR_CACHE = "action.clearImageCache"; diff --git a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php index e9e3d50bf..5edd007b3 100755 --- a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php +++ b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php @@ -29,6 +29,7 @@ use Thelia\Exception\InvalidCartException; use Thelia\Model\CartQuery; use Thelia\Model\Cart; use Thelia\Model\Currency; +use Thelia\Model\Order; use Thelia\Tools\URL; use Thelia\Model\Lang; @@ -43,6 +44,8 @@ use Thelia\Model\Lang; class Session extends BaseSession { /** + * @param bool $forceDefault + * * @return \Thelia\Model\Lang|null */ public function getLang($forceDefault = true) @@ -205,22 +208,19 @@ class Session extends BaseSession return $this; } - /** - * assign delivery id in session - * - * @param $delivery_id - * @return $this - */ - public function setDelivery($delivery_id) + // -- Order ------------------------------------------------------------------ + + + public function setOrder(Order $order) { - $this->set("thelia.delivery_id", $delivery_id); + $this->set("thelia.order", $order); return $this; } - public function getDelivery() + public function getOrder() { - return $this->get("thelia.delivery_id"); + return $this->get("thelia.order"); } diff --git a/core/lib/Thelia/Form/OrderDelivery.php b/core/lib/Thelia/Form/OrderDelivery.php new file mode 100755 index 000000000..d0ee0efb3 --- /dev/null +++ b/core/lib/Thelia/Form/OrderDelivery.php @@ -0,0 +1,94 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Form; + +use Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\ExecutionContextInterface; +use Thelia\Model\AddressQuery; +use Thelia\Model\ConfigQuery; +use Thelia\Core\Translation\Translator; +use Thelia\Model\ModuleQuery; +use Thelia\Module\BaseModule; + +/** + * Class OrderDelivery + * @package Thelia\Form + * @author Etienne Roudeix + */ +class OrderDelivery extends BaseForm +{ + protected function buildForm() + { + $this->formBuilder + ->add("delivery-address", "integer", array( + "constraints" => array( + new Constraints\NotBlank(), + new Constraints\Callback(array( + "methods" => array( + array($this, "verifyDeliveryAddress") + ) + )) + ) + )) + ->add("delivery-module", "integer", array( + "constraints" => array( + new Constraints\NotBlank(), + new Constraints\Callback(array( + "methods" => array( + array($this, "verifyDeliveryModule") + ) + )) + ) + )); + } + + public function verifyDeliveryAddress($value, ExecutionContextInterface $context) + { + $address = AddressQuery::create() + ->findPk($value); + + if(null === $address) { + $context->addViolation("Address ID not found"); + } elseif($address->getCustomerId() !== $this->request->getSession()->getCustomerUser()->getId()) { + $context->addViolation("Address does not belong to you"); + } + } + + public function verifyDeliveryModule($value, ExecutionContextInterface $context) + { + $module = ModuleQuery::create() + ->filterByType(BaseModule::DELIVERY_MODULE_TYPE) + ->filterByActivate(1) + ->filterById($value) + ->find(); + + if(null === $module) { + $context->addViolation("Delivery module ID not found"); + } + } + + public function getName() + { + return "thelia_order_delivery"; + } +} \ No newline at end of file diff --git a/templates/default/cart.html b/templates/default/cart.html index 6f91d8581..483e34436 100644 --- a/templates/default/cart.html +++ b/templates/default/cart.html @@ -133,7 +133,7 @@ Continue Shopping - Proceed checkout + Proceed checkout diff --git a/templates/default/cart_billing.html b/templates/default/cart_billing.html index 123596609..45c6eadf1 100644 --- a/templates/default/cart_billing.html +++ b/templates/default/cart_billing.html @@ -24,7 +24,7 @@ 4 Secure payment - +
@@ -164,7 +164,7 @@ {loop type="delivery" name="deliveries" force_return="true"}