diff --git a/core/lib/Thelia/Action/Module.php b/core/lib/Thelia/Action/Module.php index 4d286d789..5eaa227ad 100644 --- a/core/lib/Thelia/Action/Module.php +++ b/core/lib/Thelia/Action/Module.php @@ -24,6 +24,7 @@ namespace Thelia\Action; use Propel\Runtime\Propel; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\Filesystem\Filesystem; use Thelia\Core\Event\Cache\CacheEvent; use Thelia\Core\Event\Module\ModuleDeleteEvent; use Thelia\Core\Event\Module\ModuleToggleActivationEvent; @@ -64,7 +65,37 @@ class Module extends BaseAction implements EventSubscriberInterface public function delete(ModuleDeleteEvent $event) { - + if (null !== $module = ModuleQuery::create()->findPk($event->getModuleId())) { + $con = Propel::getWriteConnection(ModuleTableMap::DATABASE_NAME); + $con->beginTransaction(); + + try { + if(null === $module->getFullNamespace()) { + throw new \LogicException('can not instanciante this module if namespace is null. Maybe the model is not loaded ?'); + } + + $reflected = new \ReflectionClass($module->getFullNamespace()); + $instance = $reflected->newInstance(); + $instance->setContainer($this->container); + + $path = dirname($reflected->getFileName()); + + $instance->destroy($con); + + $fs = new Filesystem(); + $fs->remove($path); + + $module->delete($con); + + $con->commit(); + + $event->setModule($module); + + } catch (\Exception $e) { + $con->rollBack(); + throw $e; + } + } } protected function cacheClear() diff --git a/core/lib/Thelia/Controller/Admin/ModuleController.php b/core/lib/Thelia/Controller/Admin/ModuleController.php index 2be309321..a10e9aa8a 100644 --- a/core/lib/Thelia/Controller/Admin/ModuleController.php +++ b/core/lib/Thelia/Controller/Admin/ModuleController.php @@ -88,6 +88,7 @@ class ModuleController extends BaseAdminController { if (null !== $response = $this->checkAuth("admin.module.delete")) return $response; + $message = null; try { $module_id = $this->getRequest()->get('module_id'); @@ -96,8 +97,22 @@ class ModuleController extends BaseAdminController $this->dispatch(TheliaEvents::MODULE_DELETE, $deleteEvent); - } catch (\Exception $e) { + if($deleteEvent->hasModule() === false) { + throw new \LogicException( + $this->getTranslator()->trans("No %obj was updated.", array('%obj' => 'Module'))); + } + } catch (\Exception $e) { + \Thelia\Log\Tlog::getInstance()->error(sprintf("error during module removal : %s", $message)); + $message = $e->getMessage(); + } + + if($message) { + return $this->render("modules", array( + "error_message" => $message + )); + } else { + $this->redirectToRoute('admin.module'); } } } diff --git a/core/lib/Thelia/Core/Event/Module/ModuleEvent.php b/core/lib/Thelia/Core/Event/Module/ModuleEvent.php index c37b2b995..583a3afc9 100644 --- a/core/lib/Thelia/Core/Event/Module/ModuleEvent.php +++ b/core/lib/Thelia/Core/Event/Module/ModuleEvent.php @@ -62,4 +62,9 @@ class ModuleEvent extends ActionEvent return $this->module; } + public function hasModule() + { + return null !== $this->module; + } + } diff --git a/core/lib/Thelia/Module/BaseModule.php b/core/lib/Thelia/Module/BaseModule.php index 598e25baa..79acdd4b3 100755 --- a/core/lib/Thelia/Module/BaseModule.php +++ b/core/lib/Thelia/Module/BaseModule.php @@ -225,7 +225,7 @@ abstract class BaseModule extends ContainerAware return basename(dirname($this->reflected->getFileName())); } - public function install() + public function install(ConnectionInterface $con = null) { } @@ -249,7 +249,7 @@ abstract class BaseModule extends ContainerAware } - public function destroy() + public function destroy(ConnectionInterface $con = null) { } diff --git a/templates/admin/default/modules.html b/templates/admin/default/modules.html index 847a5ea4a..86c336a04 100644 --- a/templates/admin/default/modules.html +++ b/templates/admin/default/modules.html @@ -25,6 +25,7 @@
+ {if $error_message}
{$error_message}
{/if} {include file="includes/module-block.html" module_type="1" caption_title={intl l='Classic modules'}} {include file="includes/module-block.html" module_type="2" caption_title={intl l='Delivery modules'}} {include file="includes/module-block.html" module_type="3" caption_title={intl l='Payment modules'}}