diff --git a/core/lib/Thelia/Action/Module.php b/core/lib/Thelia/Action/Module.php index 91a51c3af..18fdb85cb 100644 --- a/core/lib/Thelia/Action/Module.php +++ b/core/lib/Thelia/Action/Module.php @@ -25,6 +25,8 @@ namespace Thelia\Action; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Thelia\Core\Event\Module\ModuleToggleActivationEvent; 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) { + 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); + } + } } /** diff --git a/core/lib/Thelia/Controller/Admin/ModuleController.php b/core/lib/Thelia/Controller/Admin/ModuleController.php index 66bd11e49..ec7ff472f 100644 --- a/core/lib/Thelia/Controller/Admin/ModuleController.php +++ b/core/lib/Thelia/Controller/Admin/ModuleController.php @@ -58,6 +58,11 @@ class ModuleController extends BaseAdminController try { $event = new ModuleToggleActivationEvent($module_id); $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) { $message = $e->getMessage(); } @@ -69,7 +74,7 @@ class ModuleController extends BaseAdminController } $response = $this->nullResponse(); } else { - $response = $this->render("modules"); + $this->redirectToRoute('admin.module'); } return $response; diff --git a/core/lib/Thelia/Module/BaseModule.php b/core/lib/Thelia/Module/BaseModule.php index c7979be6e..6cfc79d28 100755 --- a/core/lib/Thelia/Module/BaseModule.php +++ b/core/lib/Thelia/Module/BaseModule.php @@ -24,7 +24,10 @@ namespace Thelia\Module; +use Propel\Runtime\Connection\ConnectionInterface; +use Propel\Runtime\Propel; use Symfony\Component\DependencyInjection\ContainerAware; +use Thelia\Model\Map\ModuleTableMap; use Thelia\Model\ModuleI18nQuery; use Thelia\Model\Map\ModuleImageTableMap; 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) { - $moduleModel->setActivate(self::IS_ACTIVATED); - $moduleModel->save(); + $con = Propel::getWriteConnection(ModuleTableMap::DATABASE_NAME); + $con->beginTransaction(); try { - $this->afterActivation(); + if($this->preActivation($con)) { + $moduleModel->setActivate(self::IS_ACTIVATED); + $moduleModel->save($con); + $this->postActivation($con); + $con->commit(); + } } catch (\Exception $e) { - $moduleModel->setActivate(self::IS_NOT_ACTIVATED); - $moduleModel->save(); + $con->rollBack(); 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() { return null === $this->container; @@ -190,8 +225,33 @@ abstract class BaseModule extends ContainerAware return basename(dirname($this->reflected->getFileName())); } - abstract public function install(); - abstract public function afterActivation(); - abstract public function destroy(); + public function install(){ + + } + + 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() + { + + } } diff --git a/local/modules/Cheque/Cheque.php b/local/modules/Cheque/Cheque.php index 4516c84f3..324728990 100755 --- a/local/modules/Cheque/Cheque.php +++ b/local/modules/Cheque/Cheque.php @@ -23,6 +23,7 @@ namespace Cheque; +use Propel\Runtime\Connection\ConnectionInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; use Thelia\Model\ModuleImageQuery; @@ -59,12 +60,8 @@ class Cheque extends BaseModule implements PaymentModuleInterface // no special process, waiting for the cheque. } - public function install() - { - } - - public function afterActivation() + public function postActivation(ConnectionInterface $con = null) { /* insert the images from image folder if first module activation */ $module = $this->getModuleModel(); @@ -82,10 +79,6 @@ class Cheque extends BaseModule implements PaymentModuleInterface ); } - public function destroy() - { - // TODO: Implement destroy() method. - } public function getCode() { diff --git a/local/modules/Colissimo/Colissimo.php b/local/modules/Colissimo/Colissimo.php index 7ff972feb..adbbb32c3 100755 --- a/local/modules/Colissimo/Colissimo.php +++ b/local/modules/Colissimo/Colissimo.php @@ -67,25 +67,6 @@ class Colissimo extends BaseModule implements DeliveryModuleInterface 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() { return 'Colissimo'; diff --git a/local/modules/FakeCB/FakeCB.php b/local/modules/FakeCB/FakeCB.php index ca682fab3..58c8318da 100755 --- a/local/modules/FakeCB/FakeCB.php +++ b/local/modules/FakeCB/FakeCB.php @@ -23,6 +23,7 @@ namespace FakeCB; +use Propel\Runtime\Connection\ConnectionInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; use Thelia\Model\Base\ModuleImageQuery; @@ -59,12 +60,8 @@ class FakeCB extends BaseModule implements PaymentModuleInterface // TODO: Implement pay() method. } - public function install() - { - } - - public function afterActivation() + public function postActivation(ConnectionInterface $con = null) { /* insert the images from image folder if first module activation */ $module = $this->getModuleModel(); @@ -82,10 +79,6 @@ class FakeCB extends BaseModule implements PaymentModuleInterface ); } - public function destroy() - { - // TODO: Implement destroy() method. - } public function getCode() { diff --git a/local/modules/TheliaDebugBar/TheliaDebugBar.php b/local/modules/TheliaDebugBar/TheliaDebugBar.php index da9fddf12..466f50d5a 100755 --- a/local/modules/TheliaDebugBar/TheliaDebugBar.php +++ b/local/modules/TheliaDebugBar/TheliaDebugBar.php @@ -32,21 +32,6 @@ class TheliaDebugBar extends BaseModule * Like install and destroy */ - public function afterActivation() - { - - } - - public function install() - { - // TODO: Implement install() method. - } - - public function destroy() - { - // TODO: Implement destroy() method. - } - public function getCode() { return 'TheliaDebugBar';