From 43467681fd5dfcb5a39cc86521070d256ca74c23 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 8 Oct 2013 10:16:18 +0200 Subject: [PATCH] enabled change default country --- core/lib/Thelia/Action/Country.php | 18 +++++-- .../Thelia/Config/Resources/routing/admin.xml | 4 ++ .../Controller/Admin/CountryController.php | 22 +++++++++ core/lib/Thelia/Controller/BaseController.php | 4 +- .../Country/CountryToggleDefaultEvent.php | 48 +++++++++++++++++++ core/lib/Thelia/Core/Event/TheliaEvents.php | 2 +- .../lib/Thelia/Core/Template/Loop/Country.php | 4 +- core/lib/Thelia/Model/Country.php | 11 +++++ templates/admin/default/countries.html | 2 +- 9 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 core/lib/Thelia/Core/Event/Country/CountryToggleDefaultEvent.php diff --git a/core/lib/Thelia/Action/Country.php b/core/lib/Thelia/Action/Country.php index f4f59b2bb..9bccfcea5 100644 --- a/core/lib/Thelia/Action/Country.php +++ b/core/lib/Thelia/Action/Country.php @@ -26,6 +26,7 @@ namespace Thelia\Action; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Thelia\Core\Event\Country\CountryCreateEvent; use Thelia\Core\Event\Country\CountryDeleteEvent; +use Thelia\Core\Event\Country\CountryToggleDefaultEvent; use Thelia\Core\Event\Country\CountryUpdateEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Model\Country as CountryModel; @@ -70,6 +71,16 @@ class Country extends BaseAction implements EventSubscriberInterface } } + public function toggleDefault(CountryToggleDefaultEvent $event) + { + if( null !== $country = CountryQuery::create()->findPk($event->getCountryId())) + { + $country->toggleDefault(); + + $event->setCountry($country); + } + } + /** * Returns an array of event names this subscriber wants to listen to. @@ -94,9 +105,10 @@ class Country extends BaseAction implements EventSubscriberInterface public static function getSubscribedEvents() { return array( - TheliaEvents::COUNTRY_CREATE => array('create', 128), - TheliaEvents::COUNTRY_UPDATE => array('update', 128), - TheliaEvents::COUNTRY_DELETE => array('delete', 128), + TheliaEvents::COUNTRY_CREATE => array('create', 128), + TheliaEvents::COUNTRY_UPDATE => array('update', 128), + TheliaEvents::COUNTRY_DELETE => array('delete', 128), + TheliaEvents::COUNTRY_TOGGLE_DEFAULT => array('toggleDefault', 128) ); } } \ No newline at end of file diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index c69c0659d..5df02e5bd 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -424,6 +424,10 @@ Thelia\Controller\Admin\CountryController::deleteAction + + Thelia\Controller\Admin\CountryController::toggleDefaultAction + + Thelia\Controller\Admin\ContentController::createAction diff --git a/core/lib/Thelia/Controller/Admin/CountryController.php b/core/lib/Thelia/Controller/Admin/CountryController.php index c63be90b4..a272f7364 100644 --- a/core/lib/Thelia/Controller/Admin/CountryController.php +++ b/core/lib/Thelia/Controller/Admin/CountryController.php @@ -24,6 +24,7 @@ namespace Thelia\Controller\Admin; use Thelia\Core\Event\Country\CountryCreateEvent; use Thelia\Core\Event\Country\CountryDeleteEvent; +use Thelia\Core\Event\Country\CountryToggleDefaultEvent; use Thelia\Core\Event\Country\CountryUpdateEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Form\CountryCreationForm; @@ -245,4 +246,25 @@ class CountryController extends AbstractCrudController { $this->redirectToRoute('admin.configuration.countries.default'); } + + public function toggleDefaultAction() + { + if (null !== $response = $this->checkAuth($this->updatePermissionIdentifier)) return $response; + $content = null; + if (null !== $country_id = $this->getRequest()->get('country_id')) { + $toogleDefaultEvent = new CountryToggleDefaultEvent($country_id); + try { + $this->dispatch(TheliaEvents::COUNTRY_TOGGLE_DEFAULT, $toogleDefaultEvent); + + if($toogleDefaultEvent->hasCountry()) { + return $this->nullResponse(); + } + } catch (\Exception $ex) { + $content = $ex->getMessage(); + } + + } + + return $this->nullResponse($content, 500); + } } diff --git a/core/lib/Thelia/Controller/BaseController.php b/core/lib/Thelia/Controller/BaseController.php index 51d2015ea..8c819159b 100755 --- a/core/lib/Thelia/Controller/BaseController.php +++ b/core/lib/Thelia/Controller/BaseController.php @@ -58,9 +58,9 @@ class BaseController extends ContainerAware /** * Return an empty response (after an ajax request, for example) */ - protected function nullResponse() + protected function nullResponse($status = 200) { - return new Response(); + return new Response(null, $status); } /** diff --git a/core/lib/Thelia/Core/Event/Country/CountryToggleDefaultEvent.php b/core/lib/Thelia/Core/Event/Country/CountryToggleDefaultEvent.php new file mode 100644 index 000000000..8f8142d0d --- /dev/null +++ b/core/lib/Thelia/Core/Event/Country/CountryToggleDefaultEvent.php @@ -0,0 +1,48 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event\Country; + + +/** + * Class CountryToggleDefaultEvent + * @package Thelia\Core\Event\Country + * @author manuel raynaud + */ +class CountryToggleDefaultEvent extends CountryEvent +{ + protected $country_id; + + function __construct($country_id) + { + $this->country_id = $country_id; + } + + /** + * @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 daf647085..4e4f55ba8 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -219,7 +219,7 @@ final class TheliaEvents const COUNTRY_CREATE = "action.createCountry"; const COUNTRY_UPDATE = "action.updateCountry"; const COUNTRY_DELETE = "action.deleteCountry"; - const COUNTRY_TOGGLE_VISIBILITY = "action.toggleCountryVisibility"; + const COUNTRY_TOGGLE_DEFAULT = "action.toggleCountryDefault"; //const COUNTRY_UPDATE_POSITION = "action.updateFolderPosition"; diff --git a/core/lib/Thelia/Core/Template/Loop/Country.php b/core/lib/Thelia/Core/Template/Loop/Country.php index 3a05b684a..c3271a6ac 100755 --- a/core/lib/Thelia/Core/Template/Loop/Country.php +++ b/core/lib/Thelia/Core/Template/Loop/Country.php @@ -115,8 +115,10 @@ class Country extends BaseI18nLoop ->set("POSTSCRIPTUM", $country->getVirtualColumn('i18n_POSTSCRIPTUM')) ->set("ISOCODE", $country->getIsocode()) ->set("ISOALPHA2", $country->getIsoalpha2()) - ->set("ISOALPHA3", $country->getIsoalpha3()); + ->set("ISOALPHA3", $country->getIsoalpha3()) + ->set('IS_DEFAULT', $country->getByDefault()) + ; $loopResult->addRow($loopResultRow); } diff --git a/core/lib/Thelia/Model/Country.php b/core/lib/Thelia/Model/Country.php index 9dc409910..e254d4a5e 100755 --- a/core/lib/Thelia/Model/Country.php +++ b/core/lib/Thelia/Model/Country.php @@ -11,6 +11,17 @@ class Country extends BaseCountry { use \Thelia\Model\Tools\ModelEventDispatcherTrait; + public function toggleDefault() + { + CountryQuery::create() + ->filterByByDefault(1) + ->update(array('ByDefault' => 0)); + + $this + ->setByDefault(1) + ->save(); + } + public function preInsert(ConnectionInterface $con = null) { $this->dispatchEvent(TheliaEvents::BEFORE_CREATECOUNTRY, new CountryEvent($this)); diff --git a/templates/admin/default/countries.html b/templates/admin/default/countries.html index ad03857a2..99d46ed17 100644 --- a/templates/admin/default/countries.html +++ b/templates/admin/default/countries.html @@ -55,7 +55,7 @@ {$TITLE}
- +