diff --git a/core/lib/Thelia/Action/Area.php b/core/lib/Thelia/Action/Area.php index d3aa5bbc5..ddb1fcdf5 100644 --- a/core/lib/Thelia/Action/Area.php +++ b/core/lib/Thelia/Action/Area.php @@ -25,6 +25,7 @@ namespace Thelia\Action; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Thelia\Core\Event\Area\AreaAddCountryEvent; use Thelia\Core\Event\Area\AreaRemoveCountryEvent; +use Thelia\Core\Event\Area\AreaUpdatePostageEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Model\AreaQuery; use Thelia\Model\CountryQuery; @@ -57,6 +58,17 @@ class Area extends BaseAction implements EventSubscriberInterface } } + public function updatePostage(AreaUpdatePostageEvent $event) + { + if (null !== $area = AreaQuery::create()->findPk($event->getAreaId())) { + $area + ->setPostage($event->getPostage()) + ->save(); + + $event->setArea($area); + } + } + /** * Returns an array of event names this subscriber wants to listen to. @@ -82,7 +94,8 @@ class Area extends BaseAction implements EventSubscriberInterface { return array( TheliaEvents::AREA_ADD_COUNTRY => array('addCountry', 128), - TheliaEvents::AREA_REMOVE_COUNTRY => array('removeCountry', 128) + TheliaEvents::AREA_REMOVE_COUNTRY => array('removeCountry', 128), + TheliaEvents::AREA_POSTAGE_UPDATE => array('updatePostage', 128) ); } } \ No newline at end of file diff --git a/core/lib/Thelia/Controller/Admin/AreaController.php b/core/lib/Thelia/Controller/Admin/AreaController.php index 62333fb7b..824678ae3 100644 --- a/core/lib/Thelia/Controller/Admin/AreaController.php +++ b/core/lib/Thelia/Controller/Admin/AreaController.php @@ -28,10 +28,12 @@ use Thelia\Core\Event\Area\AreaCreateEvent; use Thelia\Core\Event\Area\AreaDeleteEvent; use Thelia\Core\Event\Area\AreaRemoveCountryEvent; use Thelia\Core\Event\Area\AreaUpdateEvent; +use Thelia\Core\Event\Area\AreaUpdatePostageEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Form\Area\AreaCountryForm; use Thelia\Form\Area\AreaCreateForm; use Thelia\Form\Area\AreaModificationForm; +use Thelia\Form\Area\AreaPostageForm; use Thelia\Form\Exception\FormValidationException; use Thelia\Model\AreaQuery; @@ -251,10 +253,6 @@ class AreaController extends AbstractCrudController $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()); @@ -284,4 +282,45 @@ class AreaController extends AbstractCrudController $this->redirectToEditionTemplate(); } + + public function updatePostageAction() + { + if (null !== $response = $this->checkAuth($this->updatePermissionIdentifier)) return $response; + + $areaUpdateForm = new AreaPostageForm($this->getRequest()); + $error_msg = null; + + try { + $form = $this->validateForm($areaUpdateForm); + + $event = new AreaUpdatePostageEvent($form->get('area_id')->getData()); + $event->setPostage($form->get('postage')->getData()); + + $this->dispatch(TheliaEvents::AREA_POSTAGE_UPDATE, $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))); + } + + // Redirect to the success URL + $this->redirect($areaUpdateForm->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, $areaUpdateForm); + + // At this point, the form has errors, and should be redisplayed. + return $this->renderEditionTemplate(); + } } diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 6b8cfddc0..9d9e484ef 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -240,6 +240,7 @@ final class TheliaEvents const AREA_ADD_COUNTRY = 'action.area.addCountry'; const AREA_REMOVE_COUNTRY = 'action.area.removeCountry'; + const AREA_POSTAGE_UPDATE = 'action.area.postageUpdate'; // -- Categories Associated Content ---------------------------------------- diff --git a/templates/admin/default/shipping-configuration-edit.html b/templates/admin/default/shipping-configuration-edit.html index e8275dc15..768561313 100644 --- a/templates/admin/default/shipping-configuration-edit.html +++ b/templates/admin/default/shipping-configuration-edit.html @@ -95,7 +95,7 @@ {form_field form=$form field='postage'}