From f881edbaa6cc5015029fd79c38e0be3da9d1d64a Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 23 Oct 2013 09:19:53 +0200 Subject: [PATCH] create toggleDefault method in lang method and modify in Country model --- core/lib/Thelia/Action/Lang.php | 12 ++++++++++++ core/lib/Thelia/Model/Country.php | 27 +++++++++++++++++++++------ core/lib/Thelia/Model/Lang.php | 28 ++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/core/lib/Thelia/Action/Lang.php b/core/lib/Thelia/Action/Lang.php index cbd4d074b..bf3df3f62 100644 --- a/core/lib/Thelia/Action/Lang.php +++ b/core/lib/Thelia/Action/Lang.php @@ -23,6 +23,7 @@ namespace Thelia\Action; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Thelia\Core\Event\Lang\LangToggleDefaultEvent; use Thelia\Core\Event\Lang\LangUpdateEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Model\LangQuery; @@ -52,6 +53,17 @@ class Lang extends BaseAction implements EventSubscriberInterface } } + public function toggleDefault(LangToggleDefaultEvent $event) + { + if (null !== $lang = LangQuery::create()->findPk($event->getLangId())) { + $lang->setDispatcher($this->getDispatcher()); + + $lang->toggleDefault(); + + $event->setLang($lang); + } + } + /** * Returns an array of event names this subscriber wants to listen to. * diff --git a/core/lib/Thelia/Model/Country.php b/core/lib/Thelia/Model/Country.php index e64bd4609..d30f2d281 100755 --- a/core/lib/Thelia/Model/Country.php +++ b/core/lib/Thelia/Model/Country.php @@ -3,9 +3,12 @@ namespace Thelia\Model; use Propel\Runtime\Connection\ConnectionInterface; +use Propel\Runtime\Exception\PropelException; +use Propel\Runtime\Propel; use Thelia\Core\Event\Country\CountryEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Model\Base\Country as BaseCountry; +use Thelia\Model\Map\CountryTableMap; class Country extends BaseCountry { @@ -16,13 +19,25 @@ class Country extends BaseCountry if($this->getId() === null) { throw new \RuntimeException("impossible to just uncheck default country, choose a new one"); } - CountryQuery::create() - ->filterByByDefault(1) - ->update(array('ByDefault' => 0)); - $this - ->setByDefault(1) - ->save(); + $con = Propel::getWriteConnection(CountryTableMap::DATABASE_NAME); + $con->beginTransaction(); + + try { + CountryQuery::create() + ->filterByByDefault(1) + ->update(array('ByDefault' => 0), $con); + + $this + ->setByDefault(1) + ->save($con); + + $con->commit(); + } catch(PropelException $e) { + $con->rollBack(); + throw $e; + } + } public function preInsert(ConnectionInterface $con = null) diff --git a/core/lib/Thelia/Model/Lang.php b/core/lib/Thelia/Model/Lang.php index 054a4e807..189b640ab 100755 --- a/core/lib/Thelia/Model/Lang.php +++ b/core/lib/Thelia/Model/Lang.php @@ -3,9 +3,13 @@ namespace Thelia\Model; use Propel\Runtime\Connection\ConnectionInterface; +use Propel\Runtime\Exception\PropelException; +use Propel\Runtime\Propel; use Thelia\Core\Event\Lang\LangEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Model\Base\Lang as BaseLang; +use Thelia\Model\Base\LangQuery; +use Thelia\Model\Map\LangTableMap; class Lang extends BaseLang { @@ -25,6 +29,30 @@ class Lang extends BaseLang { return $default_lang; } + public function toggleDefault() + { + if($this->getId() === null) { + throw new \RuntimeException("impossible to just uncheck default language, choose a new one"); + } + $con = Propel::getWriteConnection(LangTableMap::DATABASE_NAME); + $con->beginTransaction(); + try { + LangQuery::create() + ->filterByByDefault(1) + ->update(array('ByDefault' => 0), $con); + + $this + ->setByDefault(1) + ->save($con); + + $con->commit(); + } catch(PropelException $e) { + $con->rollBack(); + throw $e; + } + + } + public function preInsert(ConnectionInterface $con = null) { $this->dispatchEvent(TheliaEvents::BEFORE_CREATELANG, new LangEvent($this));