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(Translator::getInstance()->trans('Cannot instanciante module "%name%": the namespace is null. Maybe the model is not loaded ?', array('%name%' => $module->getCode()))); } try { $instance = $module->createInstance(); $instance->setContainer($this->container); $path = $module->getAbsoluteBaseDir(); $instance->destroy($con, $event->getDeleteData()); $fs = new Filesystem(); $fs->remove($path); } catch (\ReflectionException $ex) { // Happens probably because the module directory has been deleted. // Log a warning, and delete the database entry. Tlog::getInstance()->addWarning(Translator::getInstance()->trans('Failed to create instance of module "%name%" when trying to delete module. Module directory has probably been deleted', array('%name%' => $module->getCode()))); } $module->delete($con); $con->commit(); $event->setModule($module); $this->cacheClear($event->getDispatcher()); } catch (\Exception $e) { $con->rollBack(); throw $e; } } }
public function deleteModule(ModuleDeleteEvent $event) { if ($event->getModuleId()) { ModuleHookQuery::create()->filterByModuleId($event->getModuleId())->delete(); } return $event; }
/** * @param \Thelia\Core\Event\Module\ModuleInstallEvent $event * * @throws \Exception * @throws \Symfony\Component\Filesystem\Exception\IOException * @throws \Exception */ public function install(ModuleInstallEvent $event) { $moduleDefinition = $event->getModuleDefinition(); $oldModule = ModuleQuery::create()->findOneByFullNamespace($moduleDefinition->getNamespace()); $dispatcher = $event->getDispatcher(); $fs = new Filesystem(); $activated = false; // check existing module if (null !== $oldModule) { $activated = $oldModule->getActivate(); if ($activated) { // deactivate $toggleEvent = new ModuleToggleActivationEvent($oldModule); // disable the check of the module because it's already done $toggleEvent->setNoCheck(true); $toggleEvent->setDispatcher($dispatcher); $dispatcher->dispatch(TheliaEvents::MODULE_TOGGLE_ACTIVATION, $toggleEvent); } // delete $modulePath = $oldModule->getAbsoluteBaseDir(); $deleteEvent = new ModuleDeleteEvent($oldModule); $deleteEvent->setDispatcher($dispatcher); try { $dispatcher->dispatch(TheliaEvents::MODULE_DELETE, $deleteEvent); } catch (Exception $ex) { // if module has not been deleted if ($fs->exists($modulePath)) { throw $ex; } } } // move new module $modulePath = sprintf('%s%s', THELIA_MODULE_DIR, $event->getModuleDefinition()->getCode()); try { $fs->mirror($event->getModulePath(), $modulePath); } catch (IOException $ex) { if (!$fs->exists($modulePath)) { throw $ex; } } // Update the module $moduleDescriptorFile = sprintf('%s%s%s%s%s', $modulePath, DS, 'Config', DS, 'module.xml'); $moduleManagement = new ModuleManagement(); $file = new SplFileInfo($moduleDescriptorFile); $module = $moduleManagement->updateModule($file, $this->container); // activate if old was activated if ($activated) { $toggleEvent = new ModuleToggleActivationEvent($module->getId()); $toggleEvent->setNoCheck(true); $toggleEvent->setDispatcher($dispatcher); $dispatcher->dispatch(TheliaEvents::MODULE_TOGGLE_ACTIVATION, $toggleEvent); } $event->setModule($module); }
public function deleteAction() { if (null !== ($response = $this->checkAuth(AdminResources::MODULE, array(), AccessManager::DELETE))) { return $response; } $message = false; try { $this->getTokenProvider()->checkToken($this->getRequest()->query->get("_token")); $module_id = $this->getRequest()->get('module_id'); $deleteEvent = new ModuleDeleteEvent($module_id); $deleteEvent->setDeleteData('1' == $this->getRequest()->get('delete-module-data', '0')); $this->dispatch(TheliaEvents::MODULE_DELETE, $deleteEvent); if ($deleteEvent->hasModule() === false) { throw new \LogicException($this->getTranslator()->trans("No %obj was updated.", array('%obj' => 'Module'))); } } catch (\Exception $e) { $message = $e->getMessage(); Tlog::getInstance()->addError("Error during module removal", $e); } if (false !== $message) { $response = $this->render("modules", array("error_message" => $message)); } else { $response = $this->generateRedirectFromRoute('admin.module'); } return $response; }
public function delete(ModuleDeleteEvent $event, $eventName, EventDispatcherInterface $dispatcher) { $con = Propel::getWriteConnection(ModuleTableMap::DATABASE_NAME); $con->beginTransaction(); if (null !== ($module = ModuleQuery::create()->findPk($event->getModuleId(), $con))) { try { if (null === $module->getFullNamespace()) { throw new \LogicException(Translator::getInstance()->trans('Cannot instantiate module "%name%": the namespace is null. Maybe the model is not loaded ?', ['%name%' => $module->getCode()])); } // If the module is referenced by an order, display a meaningful error // instead of 'delete cannot delete' caused by a constraint violation. // FIXME: we hav to find a way to delete modules used by order. if (OrderQuery::create()->filterByDeliveryModuleId($module->getId())->count() > 0 || OrderQuery::create()->filterByPaymentModuleId($module->getId())->count() > 0) { throw new \LogicException(Translator::getInstance()->trans('The module "%name%" is currently in use by at least one order, and can\'t be deleted.', ['%name%' => $module->getCode()])); } try { if ($module->getMandatory() == BaseModule::IS_MANDATORY && $event->getAssumeDelete() === false) { throw new \Exception(Translator::getInstance()->trans('Can\'t remove a core module')); } // First, try to create an instance $instance = $module->createInstance(); // Then, if module is activated, check if we can deactivate it if ($module->getActivate()) { // check for modules that depend of this one $this->checkDeactivation($module); } $instance->setContainer($this->container); $path = $module->getAbsoluteBaseDir(); $instance->destroy($con, $event->getDeleteData()); $fs = new Filesystem(); $fs->remove($path); } catch (\ReflectionException $ex) { // Happens probably because the module directory has been deleted. // Log a warning, and delete the database entry. Tlog::getInstance()->addWarning(Translator::getInstance()->trans('Failed to create instance of module "%name%" when trying to delete module. Module directory has probably been deleted', ['%name%' => $module->getCode()])); } catch (FileNotFoundException $fnfe) { // The module directory has been deleted. // Log a warning, and delete the database entry. Tlog::getInstance()->addWarning(Translator::getInstance()->trans('Module "%name%" directory was not found', ['%name%' => $module->getCode()])); } $module->delete($con); $con->commit(); $event->setModule($module); $this->cacheClear($dispatcher); } catch (\Exception $e) { $con->rollBack(); throw $e; } } }