create toggleDefault method in lang method and modify in Country model

This commit is contained in:
Manuel Raynaud
2013-10-23 09:19:53 +02:00
parent eac2c274e7
commit f881edbaa6
3 changed files with 61 additions and 6 deletions

View File

@@ -23,6 +23,7 @@
namespace Thelia\Action; namespace Thelia\Action;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\Lang\LangToggleDefaultEvent;
use Thelia\Core\Event\Lang\LangUpdateEvent; use Thelia\Core\Event\Lang\LangUpdateEvent;
use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\LangQuery; 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. * Returns an array of event names this subscriber wants to listen to.
* *

View File

@@ -3,9 +3,12 @@
namespace Thelia\Model; namespace Thelia\Model;
use Propel\Runtime\Connection\ConnectionInterface; use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Exception\PropelException;
use Propel\Runtime\Propel;
use Thelia\Core\Event\Country\CountryEvent; use Thelia\Core\Event\Country\CountryEvent;
use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\Base\Country as BaseCountry; use Thelia\Model\Base\Country as BaseCountry;
use Thelia\Model\Map\CountryTableMap;
class Country extends BaseCountry class Country extends BaseCountry
{ {
@@ -16,13 +19,25 @@ class Country extends BaseCountry
if($this->getId() === null) { if($this->getId() === null) {
throw new \RuntimeException("impossible to just uncheck default country, choose a new one"); throw new \RuntimeException("impossible to just uncheck default country, choose a new one");
} }
CountryQuery::create()
->filterByByDefault(1)
->update(array('ByDefault' => 0));
$this $con = Propel::getWriteConnection(CountryTableMap::DATABASE_NAME);
->setByDefault(1) $con->beginTransaction();
->save();
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) public function preInsert(ConnectionInterface $con = null)

View File

@@ -3,9 +3,13 @@
namespace Thelia\Model; namespace Thelia\Model;
use Propel\Runtime\Connection\ConnectionInterface; use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Exception\PropelException;
use Propel\Runtime\Propel;
use Thelia\Core\Event\Lang\LangEvent; use Thelia\Core\Event\Lang\LangEvent;
use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\Base\Lang as BaseLang; use Thelia\Model\Base\Lang as BaseLang;
use Thelia\Model\Base\LangQuery;
use Thelia\Model\Map\LangTableMap;
class Lang extends BaseLang { class Lang extends BaseLang {
@@ -25,6 +29,30 @@ class Lang extends BaseLang {
return $default_lang; 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) public function preInsert(ConnectionInterface $con = null)
{ {
$this->dispatchEvent(TheliaEvents::BEFORE_CREATELANG, new LangEvent($this)); $this->dispatchEvent(TheliaEvents::BEFORE_CREATELANG, new LangEvent($this));