diff --git a/core/lib/Thelia/Action/ShippingZone.php b/core/lib/Thelia/Action/ShippingZone.php new file mode 100644 index 000000000..9dfb53123 --- /dev/null +++ b/core/lib/Thelia/Action/ShippingZone.php @@ -0,0 +1,75 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Action; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Thelia\Core\Event\ShippingZone\ShippingZoneAddAreaEvent; +use Thelia\Core\Event\TheliaEvents; +use Thelia\Model\AreaDeliveryModule; + + +/** + * Class ShippingZone + * @package Thelia\Action + * @author Manuel Raynaud + */ +class ShippingZone extends BaseAction implements EventSubscriberInterface +{ + + public function addArea(ShippingZoneAddAreaEvent $event) + { + $areaDelivery = new AreaDeliveryModule(); + + $areaDelivery + ->setAreaId($event->getAreaId()) + ->setDeliveryModuleId($event->getShoppingZoneId()) + ->save(); + } + + /** + * Returns an array of event names this subscriber wants to listen to. + * + * The array keys are event names and the value can be: + * + * * The method name to call (priority defaults to 0) + * * An array composed of the method name to call and the priority + * * An array of arrays composed of the method names to call and respective + * priorities, or 0 if unset + * + * For instance: + * + * * array('eventName' => 'methodName') + * * array('eventName' => array('methodName', $priority)) + * * array('eventName' => array(array('methodName1', $priority), array('methodName2')) + * + * @return array The event names to listen to + * + * @api + */ + public static function getSubscribedEvents() + { + return array( + TheliaEvents::SHIPPING_ZONE_ADD_AREA => array('addArea', 128) + ); + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Config/Resources/action.xml b/core/lib/Thelia/Config/Resources/action.xml index 4770ec377..b3bc085bf 100755 --- a/core/lib/Thelia/Config/Resources/action.xml +++ b/core/lib/Thelia/Config/Resources/action.xml @@ -131,6 +131,11 @@ + + + + + diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index 2cf8851e1..6daefead9 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -683,6 +683,10 @@ \d+ + + Thelia\Controller\Admin\ShippingZoneController::addArea + + diff --git a/core/lib/Thelia/Controller/Admin/ShippingZoneController.php b/core/lib/Thelia/Controller/Admin/ShippingZoneController.php index d66967d44..8f1117d70 100644 --- a/core/lib/Thelia/Controller/Admin/ShippingZoneController.php +++ b/core/lib/Thelia/Controller/Admin/ShippingZoneController.php @@ -22,6 +22,9 @@ /*************************************************************************************/ namespace Thelia\Controller\Admin; +use Thelia\Core\Event\ShippingZone\ShippingZoneAddAreaEvent; +use Thelia\Core\Event\TheliaEvents; +use Thelia\Form\Exception\FormValidationException; /** * Class ShippingZoneController @@ -30,6 +33,8 @@ namespace Thelia\Controller\Admin; */ class ShippingZoneController extends BaseAdminController { + public $objectName = 'areaDeliveryModule'; + public function indexAction() { if (null !== $response = $this->checkAuth("admin.shipping-zones.view")) return $response; @@ -38,10 +43,64 @@ class ShippingZoneController extends BaseAdminController public function updateAction($shipping_zones_id) { + if (null !== $response = $this->checkAuth("admin.shipping-zones.view")) return $response; return $this->render("shipping-zones-edit", array( "shipping_zones_id" => $shipping_zones_id )); } + /** + * @return mixed|\Symfony\Component\HttpFoundation\Response + */ + public function addArea() + { + if (null !== $response = $this->checkAuth("admin.shipping-zones.update")) return $response; + + $shippingAreaForm = new \Thelia\Form\ShippingZone\ShippingZoneAddArea($this->getRequest()); + $error_msg = null; + + try { + $form = $this->validateForm($shippingAreaForm); + + $event = new ShippingZoneAddAreaEvent( + $form->get('area_id')->getData(), + $form->get('shipping_zone_id')->getData() + ); + + $this->dispatch(TheliaEvents::SHIPPING_ZONE_ADD_AREA, $event); + + // Redirect to the success URL + $this->redirect($shippingAreaForm->getSuccessUrl()); + + } catch (FormValidationException $ex) { + // Form cannot be validated + $error_msg = $this->createStandardFormValidationErrorMessage($ex); + } catch (\Exception $ex) { + // Any other error + $error_msg = $ex->getMessage(); + } + + $this->setupFormErrorContext( + $this->getTranslator()->trans("%obj modification", array('%obj' => $this->objectName)), $error_msg, $shippingAreaForm); + + // At this point, the form has errors, and should be redisplayed. + return $this->renderEditionTemplate(); + } + + /** + * Render the edition template + */ + protected function renderEditionTemplate() + { + return $this->render('admin.configuration.shipping-zones.update.view',array( + 'shipping_zones_id' => $this->getShippingZoneId() + )); + } + + protected function getShippingZoneId() + { + return $this->getRequest()->get('shipping_zone_id', 0); + } + } diff --git a/core/lib/Thelia/Core/Event/ShippingZone/ShippingZoneAddAreaEvent.php b/core/lib/Thelia/Core/Event/ShippingZone/ShippingZoneAddAreaEvent.php new file mode 100644 index 000000000..d1e406646 --- /dev/null +++ b/core/lib/Thelia/Core/Event/ShippingZone/ShippingZoneAddAreaEvent.php @@ -0,0 +1,87 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event\ShippingZone; +use Thelia\Core\Event\ActionEvent; + + +/** + * Class ShippingZoneAddAreaEvent + * @package Thelia\Core\Event\ShippingZone + * @author Manuel Raynaud + */ +class ShippingZoneAddAreaEvent extends ActionEvent +{ + protected $area_id; + protected $shopping_zone_id; + + function __construct($area_id, $shopping_zone_id) + { + $this->area_id = $area_id; + $this->shopping_zone_id = $shopping_zone_id; + } + + /** + * @param mixed $area_id + * + * @return $this + */ + public function setAreaId($area_id) + { + $this->area_id = $area_id; + + return $this; + } + + /** + * @return mixed + */ + public function getAreaId() + { + return $this->area_id; + } + + /** + * @param mixed $shopping_zone_id + * + * @return $this + */ + public function setShoppingZoneId($shopping_zone_id) + { + $this->shopping_zone_id = $shopping_zone_id; + + return $this; + } + + /** + * @return mixed + */ + public function getShoppingZoneId() + { + return $this->shopping_zone_id; + } + + + + +} \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index a21431bbc..aeade5ea8 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -232,7 +232,7 @@ final class TheliaEvents const BEFORE_UPDATECOUNTRY = "action.before_updateCountry"; const AFTER_UPDATECOUNTRY = "action.after_updateCountry"; - // -- SHIPPING CONFIGURATION MANAGEMENT + // -- AREA CONFIGURATION MANAGEMENT const AREA_CREATE = 'action.createArea'; const AREA_UPDATE = 'action.updateArea'; @@ -251,6 +251,10 @@ final class TheliaEvents const BEFORE_DELETEAREA = 'action.before_deleteArea'; const AFTER_DELETEAREA = 'action.after_deleteArea'; + // -- SHIPPING ZONE MANAGEMENT + + const SHIPPING_ZONE_ADD_AREA = 'action.shippingZone.addArea'; + // -- Categories Associated Content ---------------------------------------- const BEFORE_CREATECATEGORY_ASSOCIATED_CONTENT = "action.before_createCategoryAssociatedContent"; diff --git a/templates/admin/default/shipping-zones-edit.html b/templates/admin/default/shipping-zones-edit.html index 524c14d12..257b3de9c 100644 --- a/templates/admin/default/shipping-zones-edit.html +++ b/templates/admin/default/shipping-zones-edit.html @@ -27,11 +27,11 @@
{form name="thelia.shopping_zone_area"} -
+ {form_hidden_fields form=$form} {form_field form=$form field='success_url'} - {* the url the user is redirected to on login success *} + {* the url the user is redirected to on login success *} {/form_field} {form_field form=$form field='shipping_zone_id'}