From 38761a727532de8640c8992088e6bc732bf8c076 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 22 Oct 2013 19:56:18 +0200 Subject: [PATCH] process lang udate action --- core/lib/Thelia/Action/Lang.php | 81 +++++++++++++++++++ core/lib/Thelia/Config/Resources/action.xml | 5 ++ .../Thelia/Config/Resources/routing/admin.xml | 2 +- .../Controller/Admin/LangController.php | 26 +++++- .../Core/Event/Lang/LangUpdateEvent.php | 34 ++++++++ core/lib/Thelia/Core/Event/TheliaEvents.php | 11 +++ core/lib/Thelia/Model/Lang.php | 41 ++++++++++ 7 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 core/lib/Thelia/Action/Lang.php diff --git a/core/lib/Thelia/Action/Lang.php b/core/lib/Thelia/Action/Lang.php new file mode 100644 index 000000000..88a544b5f --- /dev/null +++ b/core/lib/Thelia/Action/Lang.php @@ -0,0 +1,81 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Action; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Thelia\Core\Event\Lang\LangUpdateEvent; +use Thelia\Core\Event\TheliaEvents; +use Thelia\Model\LangQuery; + + +/** + * Class Lang + * @package Thelia\Action + * @author Manuel Raynaud + */ +class Lang extends BaseAction implements EventSubscriberInterface +{ + + public function update(LangUpdateEvent $event) + { + if (null !== $lang = LangQuery::create()->findPk($event->getId())) { + $lang->setDispatcher($this->getDispatcher()); + + $lang->setTitle($event->getTitle()) + ->setLocale($event->getLocale()) + ->setCode($event->getCode()) + ->setDateFormat($event->getDateFormat()) + ->setTimeFormat($event->getTimeFormat()) + ->save(); + + $event->setLang($lang); + } + } + + /** + * 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::LANG_UPDATE => array('update', 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 e5d6c9492..ab8be7d00 100755 --- a/core/lib/Thelia/Config/Resources/action.xml +++ b/core/lib/Thelia/Config/Resources/action.xml @@ -160,6 +160,11 @@ + + + + + diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index aec9e1588..607724d5d 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -931,7 +931,7 @@ \d+ - + Thelia\Controller\Admin\LangController::processUpdateAction \d+ diff --git a/core/lib/Thelia/Controller/Admin/LangController.php b/core/lib/Thelia/Controller/Admin/LangController.php index dd6deeff9..908bdeadf 100644 --- a/core/lib/Thelia/Controller/Admin/LangController.php +++ b/core/lib/Thelia/Controller/Admin/LangController.php @@ -23,6 +23,8 @@ namespace Thelia\Controller\Admin; +use Thelia\Core\Event\Lang\LangUpdateEvent; +use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Security\AccessManager; use Thelia\Core\Security\Resource\AdminResources; use Thelia\Form\Lang\LangUpdateForm; @@ -78,8 +80,30 @@ class LangController extends BaseAdminController try { $form = $this->validateForm($langForm); - } catch(\Exception $e) { + $event = new LangUpdateEvent($form->get('id')->getData()); + $event + ->setTitle($form->get('title')->getData()) + ->setCode($form->get('code')->getData()) + ->setLocale($form->get('locale')->getData()) + ->setDateFormat($form->get('date_format')->getData()) + ->setTimeFormat($form->get('time_format')->getData()) + ; + + $this->dispatch(TheliaEvents::LANG_UPDATE, $event); + + if (false === $event->hasLang()) { + throw new \LogicException( + $this->getTranslator()->trans("No %obj was updated.", array('%obj', 'Lang'))); + } + + $changedObject = $event->getLang(); + $this->adminLogAppend(sprintf("%s %s (ID %s) modified", 'Lang', $changedObject->getTitle(), $changedObject->getId())); + $this->redirectToRoute('/admin/configuration/languages'); + } catch(\Exception $e) { + $error_msg = $e->getMessage(); } + + return $this->render('languages'); } } \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/Lang/LangUpdateEvent.php b/core/lib/Thelia/Core/Event/Lang/LangUpdateEvent.php index a520dd4c5..f0d4b928d 100644 --- a/core/lib/Thelia/Core/Event/Lang/LangUpdateEvent.php +++ b/core/lib/Thelia/Core/Event/Lang/LangUpdateEvent.php @@ -31,5 +31,39 @@ namespace Thelia\Core\Event\Lang; */ class LangUpdateEvent extends LangCreateEvent { + /** + * @var int lang id + */ + protected $id; + + /** + * @param int $id + */ + function __construct($id) + { + $this->id = $id; + } + + + /** + * @param int $id + * + * @return $this + */ + public function setId($id) + { + $this->id = $id; + + return $this; + } + + /** + * @return int + */ + public function getId() + { + return $this->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 bc16115f9..7e02adaff 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -688,4 +688,15 @@ final class TheliaEvents * sent for subscribing to the newsletter */ const NEWSLETTER_SUBSCRIBE = 'thelia.newsletter.subscribe'; + + const LANG_UPDATE = 'action.lang.update'; + + const BEFORE_UPDATELANG = 'action.lang.beforeUpdate'; + const AFTER_UPDATELANG = 'action.lang.afterUpdate'; + + const BEFORE_CREATELANG = 'action.lang.beforeCreate'; + const AFTER_CREATELANG = 'action.lang.afterCreate'; + + const BEFORE_DELETELANG = 'action.lang.beforeDelete'; + const AFTER_DELETELANG = 'action.lang.afterDelete'; } diff --git a/core/lib/Thelia/Model/Lang.php b/core/lib/Thelia/Model/Lang.php index 3c45ade33..054a4e807 100755 --- a/core/lib/Thelia/Model/Lang.php +++ b/core/lib/Thelia/Model/Lang.php @@ -2,10 +2,14 @@ namespace Thelia\Model; +use Propel\Runtime\Connection\ConnectionInterface; +use Thelia\Core\Event\Lang\LangEvent; +use Thelia\Core\Event\TheliaEvents; use Thelia\Model\Base\Lang as BaseLang; class Lang extends BaseLang { + use \Thelia\Model\Tools\ModelEventDispatcherTrait; /** * Return the default language object, using a local variable to cache it. * @@ -20,4 +24,41 @@ class Lang extends BaseLang { return $default_lang; } + + public function preInsert(ConnectionInterface $con = null) + { + $this->dispatchEvent(TheliaEvents::BEFORE_CREATELANG, new LangEvent($this)); + + return true; + } + + public function postInsert(ConnectionInterface $con = null) + { + $this->dispatchEvent(TheliaEvents::AFTER_CREATELANG, new LangEvent($this)); + } + + public function preUpdate(ConnectionInterface $con = null) + { + $this->dispatchEvent(TheliaEvents::BEFORE_UPDATELANG, new LangEvent($this)); + + return true; + } + + public function postUpdate(ConnectionInterface $con = null) + { + $this->dispatchEvent(TheliaEvents::AFTER_UPDATELANG, new LangEvent($this)); + } + + public function preDelete(ConnectionInterface $con = null) + { + $this->dispatchEvent(TheliaEvents::BEFORE_DELETELANG, new LangEvent($this)); + + return true; + } + + public function postDelete(ConnectionInterface $con = null) + { + $this->dispatchEvent(TheliaEvents::AFTER_DELETELANG, new LangEvent($this)); + } + }