process module toggle activation

This commit is contained in:
Manuel Raynaud
2013-10-17 18:50:00 +02:00
parent d25ad53d5a
commit 6d791f5bf7
7 changed files with 99 additions and 63 deletions

View File

@@ -25,6 +25,8 @@ namespace Thelia\Action;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\Module\ModuleToggleActivationEvent; use Thelia\Core\Event\Module\ModuleToggleActivationEvent;
use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\ModuleQuery;
use Thelia\Module\BaseModule;
/** /**
@@ -37,7 +39,24 @@ class Module extends BaseAction implements EventSubscriberInterface
public function toggleActivation(ModuleToggleActivationEvent $event) public function toggleActivation(ModuleToggleActivationEvent $event)
{ {
if (null !== $module = ModuleQuery::create()->findPk($event->getModuleId())) {
$moduleClass = new \ReflectionClass($module->getFullNamespace());
$moduleInstance = $moduleClass->newInstance();
if( method_exists($moduleInstance, 'setContainer')) {
$moduleInstance->setContainer($this->container);
if($module->getActivate() == BaseModule::IS_ACTIVATED) {
$moduleInstance->deActivate($module);
} else {
$moduleInstance->activate($module);
}
}
if($module->isModified()) {
$event->setModule($module);
}
}
} }
/** /**

View File

@@ -58,6 +58,11 @@ class ModuleController extends BaseAdminController
try { try {
$event = new ModuleToggleActivationEvent($module_id); $event = new ModuleToggleActivationEvent($module_id);
$this->dispatch(TheliaEvents::MODULE_TOGGLE_ACTIVATION, $event); $this->dispatch(TheliaEvents::MODULE_TOGGLE_ACTIVATION, $event);
if(null === $event->getModule()) {
throw new \LogicException(
$this->getTranslator()->trans("No %obj was updated.", array('%obj' => 'Module')));
}
} catch (\Exception $e) { } catch (\Exception $e) {
$message = $e->getMessage(); $message = $e->getMessage();
} }
@@ -69,7 +74,7 @@ class ModuleController extends BaseAdminController
} }
$response = $this->nullResponse(); $response = $this->nullResponse();
} else { } else {
$response = $this->render("modules"); $this->redirectToRoute('admin.module');
} }
return $response; return $response;

View File

@@ -24,7 +24,10 @@
namespace Thelia\Module; namespace Thelia\Module;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Propel;
use Symfony\Component\DependencyInjection\ContainerAware; use Symfony\Component\DependencyInjection\ContainerAware;
use Thelia\Model\Map\ModuleTableMap;
use Thelia\Model\ModuleI18nQuery; use Thelia\Model\ModuleI18nQuery;
use Thelia\Model\Map\ModuleImageTableMap; use Thelia\Model\Map\ModuleImageTableMap;
use Thelia\Model\ModuleI18n; use Thelia\Model\ModuleI18n;
@@ -50,22 +53,54 @@ abstract class BaseModule extends ContainerAware
} }
public function activate()
public function activate($moduleModel = null)
{ {
$moduleModel = $this->getModuleModel(); if(null === $moduleModel) {
$moduleModel = $this->getModuleModel();
}
if ($moduleModel->getActivate() == self::IS_NOT_ACTIVATED) { if ($moduleModel->getActivate() == self::IS_NOT_ACTIVATED) {
$moduleModel->setActivate(self::IS_ACTIVATED); $con = Propel::getWriteConnection(ModuleTableMap::DATABASE_NAME);
$moduleModel->save(); $con->beginTransaction();
try { try {
$this->afterActivation(); if($this->preActivation($con)) {
$moduleModel->setActivate(self::IS_ACTIVATED);
$moduleModel->save($con);
$this->postActivation($con);
$con->commit();
}
} catch (\Exception $e) { } catch (\Exception $e) {
$moduleModel->setActivate(self::IS_NOT_ACTIVATED); $con->rollBack();
$moduleModel->save();
throw $e; throw $e;
} }
} }
} }
public function deActivate($moduleModel = null)
{
if(null === $moduleModel) {
$moduleModel = $this->getModuleModel();
}
if ($moduleModel->getActivate() == self::IS_ACTIVATED) {
$con = Propel::getWriteConnection(ModuleTableMap::DATABASE_NAME);
$con->beginTransaction();
try {
if($this->preDeactivation($con)) {
$moduleModel->setActivate(self::IS_NOT_ACTIVATED);
$moduleModel->save($con);
$this->postDeactivation($con);
$con->commit();
}
} catch (\Exception $e) {
$con->rollBack();
throw $e;
}
}
}
public function hasContainer() public function hasContainer()
{ {
return null === $this->container; return null === $this->container;
@@ -190,8 +225,33 @@ abstract class BaseModule extends ContainerAware
return basename(dirname($this->reflected->getFileName())); return basename(dirname($this->reflected->getFileName()));
} }
abstract public function install(); public function install(){
abstract public function afterActivation();
abstract public function destroy(); }
public function preActivation(ConnectionInterface $con = null)
{
return true;
}
public function postActivation(ConnectionInterface $con = null)
{
}
public function preDeactivation(ConnectionInterface $con = null)
{
return true;
}
public function postDeactivation(ConnectionInterface $con = null)
{
}
public function destroy()
{
}
} }

View File

@@ -23,6 +23,7 @@
namespace Cheque; namespace Cheque;
use Propel\Runtime\Connection\ConnectionInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Thelia\Model\ModuleImageQuery; use Thelia\Model\ModuleImageQuery;
@@ -59,12 +60,8 @@ class Cheque extends BaseModule implements PaymentModuleInterface
// no special process, waiting for the cheque. // no special process, waiting for the cheque.
} }
public function install()
{
} public function postActivation(ConnectionInterface $con = null)
public function afterActivation()
{ {
/* insert the images from image folder if first module activation */ /* insert the images from image folder if first module activation */
$module = $this->getModuleModel(); $module = $this->getModuleModel();
@@ -82,10 +79,6 @@ class Cheque extends BaseModule implements PaymentModuleInterface
); );
} }
public function destroy()
{
// TODO: Implement destroy() method.
}
public function getCode() public function getCode()
{ {

View File

@@ -67,25 +67,6 @@ class Colissimo extends BaseModule implements DeliveryModuleInterface
return 2; return 2;
} }
public function afterActivation()
{
}
/**
* YOU HAVE TO IMPLEMENT HERE ABSTRACT METHODD FROM BaseModule Class
* Like install and destroy
*/
public function install()
{
// TODO: Implement install() method.
}
public function destroy()
{
// TODO: Implement destroy() method.
}
public function getCode() public function getCode()
{ {
return 'Colissimo'; return 'Colissimo';

View File

@@ -23,6 +23,7 @@
namespace FakeCB; namespace FakeCB;
use Propel\Runtime\Connection\ConnectionInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Thelia\Model\Base\ModuleImageQuery; use Thelia\Model\Base\ModuleImageQuery;
@@ -59,12 +60,8 @@ class FakeCB extends BaseModule implements PaymentModuleInterface
// TODO: Implement pay() method. // TODO: Implement pay() method.
} }
public function install()
{
} public function postActivation(ConnectionInterface $con = null)
public function afterActivation()
{ {
/* insert the images from image folder if first module activation */ /* insert the images from image folder if first module activation */
$module = $this->getModuleModel(); $module = $this->getModuleModel();
@@ -82,10 +79,6 @@ class FakeCB extends BaseModule implements PaymentModuleInterface
); );
} }
public function destroy()
{
// TODO: Implement destroy() method.
}
public function getCode() public function getCode()
{ {

View File

@@ -32,21 +32,6 @@ class TheliaDebugBar extends BaseModule
* Like install and destroy * Like install and destroy
*/ */
public function afterActivation()
{
}
public function install()
{
// TODO: Implement install() method.
}
public function destroy()
{
// TODO: Implement destroy() method.
}
public function getCode() public function getCode()
{ {
return 'TheliaDebugBar'; return 'TheliaDebugBar';