process module toggle activation
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
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()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user