add pre/post methods in Area model

This commit is contained in:
Manuel Raynaud
2013-10-14 11:26:13 +02:00
parent 998e1369a5
commit 820a92c9c1
4 changed files with 70 additions and 1 deletions

View File

@@ -24,6 +24,7 @@
namespace Thelia\Action;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\Area\AreaAddCountryEvent;
use Thelia\Core\Event\Area\AreaDeleteEvent;
use Thelia\Core\Event\Area\AreaRemoveCountryEvent;
use Thelia\Core\Event\Area\AreaUpdatePostageEvent;
use Thelia\Core\Event\TheliaEvents;
@@ -43,6 +44,7 @@ class Area extends BaseAction implements EventSubscriberInterface
public function addCountry(AreaAddCountryEvent $event)
{
if (null !== $country = CountryQuery::create()->findPk($event->getCountryId())) {
$country->setDispatcher($this->getDispatcher());
$country->setAreaId($event->getAreaId())
->save();
@@ -53,6 +55,7 @@ class Area extends BaseAction implements EventSubscriberInterface
public function removeCountry(AreaRemoveCountryEvent $event)
{
if (null !== $country = CountryQuery::create()->findPk($event->getCountryId())) {
$country->setDispatcher($this->getDispatcher());
$country->setAreaId(null)
->save();
}
@@ -61,6 +64,7 @@ class Area extends BaseAction implements EventSubscriberInterface
public function updatePostage(AreaUpdatePostageEvent $event)
{
if (null !== $area = AreaQuery::create()->findPk($event->getAreaId())) {
$area->setDispatcher($this->getDispatcher());
$area
->setPostage($event->getPostage())
->save();
@@ -69,6 +73,16 @@ class Area extends BaseAction implements EventSubscriberInterface
}
}
public function delete(AreaDeleteEvent $event)
{
if (null !== $area = AreaQuery::create()->findPk($event->getAreaId())) {
$area->setDispatcher($this->getDispatcher());
$area->delete();
$event->setArea($area);
}
}
/**
* Returns an array of event names this subscriber wants to listen to.
@@ -95,7 +109,8 @@ class Area extends BaseAction implements EventSubscriberInterface
return array(
TheliaEvents::AREA_ADD_COUNTRY => array('addCountry', 128),
TheliaEvents::AREA_REMOVE_COUNTRY => array('removeCountry', 128),
TheliaEvents::AREA_POSTAGE_UPDATE => array('updatePostage', 128)
TheliaEvents::AREA_POSTAGE_UPDATE => array('updatePostage', 128),
TheliaEvents::AREA_DELETE => array('delete', 128)
);
}
}

View File

@@ -696,6 +696,10 @@
<requirement key="area_id">\d+</requirement>
</route>
<route id="admin.configuration.shipping-configuration..delete" path="/admin/configuration/shipping_configuration/delete">
<default key="_controller">Thelia\Controller\Admin\AreaController::deleteAction</default>
</route>
<route id="admin.configuration.shipping-configuration.update.postage" path="/admin/configuration/shipping_configuration/update_postage/{area_id}">
<default key="_controller">Thelia\Controller\Admin\AreaController::updatePostageAction</default>
<requirement key="area_id">\d+</requirement>

View File

@@ -242,6 +242,15 @@ final class TheliaEvents
const AREA_REMOVE_COUNTRY = 'action.area.removeCountry';
const AREA_POSTAGE_UPDATE = 'action.area.postageUpdate';
const BEFORE_CREATEAREA = 'action.before_createArea';
const AFTER_CREATEAREA = 'action.after_createArea';
const BEFORE_UPDATEAREA = 'action.before_updateArea';
const AFTER_UPDATEAREA = 'action.after_updateArea';
const BEFORE_DELETEAREA = 'action.before_deleteArea';
const AFTER_DELETEAREA = 'action.after_deleteArea';
// -- Categories Associated Content ----------------------------------------
const BEFORE_CREATECATEGORY_ASSOCIATED_CONTENT = "action.before_createCategoryAssociatedContent";

View File

@@ -2,8 +2,49 @@
namespace Thelia\Model;
use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Core\Event\Area\AreaEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\Base\Area as BaseArea;
class Area extends BaseArea {
use \Thelia\Model\Tools\ModelEventDispatcherTrait;
public function preInsert(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_CREATEAREA, new AreaEvent($this));
return true;
}
public function postInsert(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_CREATEAREA, new AreaEvent($this));
}
public function preUpdate(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_UPDATEAREA, new AreaEvent($this));
return true;
}
public function postUpdate(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_UPDATEAREA, new AreaEvent($this));
}
public function preDelete(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_DELETEAREA, new AreaEvent($this));
return true;
}
public function postDelete(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_DELETEAREA, new AreaEvent($this));
}
}