diff --git a/core/lib/Thelia/Action/Area.php b/core/lib/Thelia/Action/Area.php new file mode 100644 index 000000000..14fcfa87b --- /dev/null +++ b/core/lib/Thelia/Action/Area.php @@ -0,0 +1,78 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Action; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Thelia\Core\Event\Area\AreaAddCountryEvent; +use Thelia\Core\Event\TheliaEvents; +use Thelia\Model\AreaQuery; +use Thelia\Model\CountryQuery; +use Thelia\Action\BaseAction; + + +/** + * Class Area + * @package Thelia\Action + * @author Manuel Raynaud + */ +class Area extends BaseAction implements EventSubscriberInterface +{ + + public function addCountry(AreaAddCountryEvent $event) + { + if (null !== $country = CountryQuery::create()->findPk($event->getCountryId())) { + $country->setAreaId($event->getAreaId()) + ->save(); + + $event->setArea($country->getArea()); + } + } + + + /** + * 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::AREA_ADD_COUNTRY => array('addCountry', 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 939cc9d88..4770ec377 100755 --- a/core/lib/Thelia/Config/Resources/action.xml +++ b/core/lib/Thelia/Config/Resources/action.xml @@ -126,6 +126,11 @@ + + + + + diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index 79165d2ac..1cb3486d8 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -696,6 +696,10 @@ \d+ + + Thelia\Controller\Admin\AreaController::addCountry + + diff --git a/core/lib/Thelia/Controller/Admin/AreaController.php b/core/lib/Thelia/Controller/Admin/AreaController.php index 9ab31ac2f..46f90d40b 100644 --- a/core/lib/Thelia/Controller/Admin/AreaController.php +++ b/core/lib/Thelia/Controller/Admin/AreaController.php @@ -23,12 +23,15 @@ namespace Thelia\Controller\Admin; +use Thelia\Core\Event\Area\AreaAddCountryEvent; use Thelia\Core\Event\Area\AreaCreateEvent; use Thelia\Core\Event\Area\AreaDeleteEvent; use Thelia\Core\Event\Area\AreaUpdateEvent; use Thelia\Core\Event\TheliaEvents; +use Thelia\Form\Area\AreaCountryForm; use Thelia\Form\Area\AreaCreateForm; use Thelia\Form\Area\AreaModificationForm; +use Thelia\Form\Exception\FormValidationException; use Thelia\Model\AreaQuery; /** @@ -219,4 +222,53 @@ class AreaController extends AbstractCrudController { $this->redirectToRoute('admin.configuration.shipping-configuration.default'); } + + /** + * add a country to a define area + */ + public function addCountry() + { + // Check current user authorization + if (null !== $response = $this->checkAuth($this->updatePermissionIdentifier)) return $response; + + $areaCountryForm = new AreaCountryForm($this->getRequest()); + $error_msg = null; + try { + + $form = $this->validateForm($areaCountryForm); + + $event = new AreaAddCountryEvent($form->get('area_id')->getData(), $form->get('country_id')->getData()); + + $this->dispatch(TheliaEvents::AREA_ADD_COUNTRY, $event); + + if (! $this->eventContainsObject($event)) + throw new \LogicException( + $this->getTranslator()->trans("No %obj was updated.", array('%obj', $this->objectName))); + + // Log object modification + if (null !== $changedObject = $this->getObjectFromEvent($event)) { + $this->adminLogAppend(sprintf("%s %s (ID %s) modified", ucfirst($this->objectName), $this->getObjectLabel($changedObject), $this->getObjectId($changedObject))); + } + + if ($this->getRequest()->get('save_mode') == 'stay') { + $this->redirectToEditionTemplate($this->getRequest()); + } + + // Redirect to the success URL + $this->redirect($areaCountryForm->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, $areaCountryForm); + + // At this point, the form has errors, and should be redisplayed. + return $this->renderEditionTemplate(); + } } diff --git a/core/lib/Thelia/Core/Event/Area/AreaAddCountryEvent.php b/core/lib/Thelia/Core/Event/Area/AreaAddCountryEvent.php new file mode 100644 index 000000000..b6af3c5b3 --- /dev/null +++ b/core/lib/Thelia/Core/Event/Area/AreaAddCountryEvent.php @@ -0,0 +1,86 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event\Area; + + +/** + * Class AreaAddCountryEvent + * @package Thelia\Core\Event\Area + * @author Manuel Raynaud + */ +class AreaAddCountryEvent extends AreaEvent +{ + protected $area_id; + protected $country_id; + + function __construct($area_id, $country_id) + { + $this->area_id = $area_id; + $this->country_id = $country_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 $country_id + * + * @return $this + */ + public function setCountryId($country_id) + { + $this->country_id = $country_id; + + return $this; + } + + /** + * @return mixed + */ + public function getCountryId() + { + return $this->country_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 36db1f699..8145a247f 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -237,6 +237,7 @@ final class TheliaEvents const AREA_CREATE = 'action.createArea'; const AREA_UPDATE = 'action.updateArea'; const AREA_DELETE = 'action.deleteArea'; + const AREA_ADD_COUNTRY = 'action.area.addCountry'; // -- Categories Associated Content ---------------------------------------- diff --git a/templates/admin/default/shipping-configuration-edit.html b/templates/admin/default/shipping-configuration-edit.html index 7bf8164f9..78dfe4e7c 100644 --- a/templates/admin/default/shipping-configuration-edit.html +++ b/templates/admin/default/shipping-configuration-edit.html @@ -8,12 +8,12 @@
- + {loop name="area-edit" type="area" id=$area_id}
@@ -21,7 +21,7 @@
- {intl l='Edit shipping configuration %title' title=$TITLE} + {intl l='Edit shipping configuration %title' title=$NAME}
@@ -29,7 +29,7 @@ {form name="thelia.admin.area.country"} -
+ {form_hidden_fields form=$form} {form_field form=$form field='success_url'} @@ -102,7 +102,15 @@
+ {/loop} + {elseloop rel="area-edit"} +
+
+ {intl l="No area defined with this id"} +
+
+ {/elseloop}