process lang udate action

This commit is contained in:
Manuel Raynaud
2013-10-22 19:56:18 +02:00
parent a2d292f6a3
commit 38761a7275
7 changed files with 198 additions and 2 deletions

View File

@@ -0,0 +1,81 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
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 <mraynaud@openstudio.fr>
*/
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)
);
}
}

View File

@@ -160,6 +160,11 @@
<argument type="service" id="service_container"/>
<tag name="kernel.event_subscriber"/>
</service>
<service id="thelia.action.lang" class="Thelia\Action\Lang">
<argument type="service" id="service_container"/>
<tag name="kernel.event_subscriber"/>
</service>
</services>
</config>

View File

@@ -931,7 +931,7 @@
<requirement key="lang_id">\d+</requirement>
</route>
<route id="admin.configuration.languages.update" path="/admin/configuration/languages/save/{lang_id}">
<route id="admin.configuration.languages.update.process" path="/admin/configuration/languages/save/{lang_id}">
<default key="_controller">Thelia\Controller\Admin\LangController::processUpdateAction</default>
<requirement key="lang_id">\d+</requirement>
</route>

View File

@@ -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');
}
}

View File

@@ -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;
}
}

View File

@@ -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';
}

View File

@@ -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));
}
}