diff --git a/config.xml b/config.xml index ef11fe2ba..debb9868f 100644 --- a/config.xml +++ b/config.xml @@ -2,7 +2,7 @@ ps_mbo - + diff --git a/config/services/eventbus.yml b/config/services/eventbus.yml new file mode 100755 index 000000000..123354761 --- /dev/null +++ b/config/services/eventbus.yml @@ -0,0 +1,9 @@ +services: + _defaults: + public: true + + mbo.ps_eventbus.installer: + class: 'PrestaShop\Module\Mbo\Service\ModuleInstaller' + public: true + arguments: + - "ps_eventbus" diff --git a/ps_mbo.php b/ps_mbo.php index 257810042..ae50e9485 100755 --- a/ps_mbo.php +++ b/ps_mbo.php @@ -49,7 +49,7 @@ class ps_mbo extends Module /** * @var string */ - public const VERSION = '4.11.4'; + public const VERSION = '4.12.0'; public const CONTROLLERS_WITH_CONNECTION_TOOLBAR = [ 'AdminModulesManage', @@ -94,7 +94,7 @@ class ps_mbo extends Module public function __construct() { $this->name = 'ps_mbo'; - $this->version = '4.11.4'; + $this->version = '4.12.0'; $this->author = 'PrestaShop'; $this->tab = 'administration'; $this->module_key = '6cad5414354fbef755c7df4ef1ab74eb'; diff --git a/src/Controller/Admin/ModuleCatalogController.php b/src/Controller/Admin/ModuleCatalogController.php old mode 100644 new mode 100755 index 64194fa3f..8f0e23b58 --- a/src/Controller/Admin/ModuleCatalogController.php +++ b/src/Controller/Admin/ModuleCatalogController.php @@ -69,7 +69,7 @@ public function indexAction(): Response try { $accountsFacade = $this->get('mbo.ps_accounts.facade'); $accountsService = $accountsFacade->getPsAccountsService(); - $this->ensurePsAccountIsEnabled(); + if ($this->ensurePsAccountIsEnabled()) $this->ensurePsEventbusEnabled(); } catch (\PrestaShop\PsAccountsInstaller\Installer\Exception\InstallerException $e) { $accountsInstaller = $this->get('mbo.ps_accounts.installer'); // Seems the module is not here, try to install it @@ -132,13 +132,23 @@ public function cdcErrorAction(): Response ); } - private function ensurePsAccountIsEnabled(): void + private function ensurePsAccountIsEnabled(): bool { $accountsInstaller = $this->get('mbo.ps_accounts.installer'); + if (!$accountsInstaller) return false; - if (null !== $accountsInstaller && !$accountsInstaller->isModuleEnabled()) { - $moduleManager = $this->get('prestashop.module.manager'); - $moduleManager->enable($accountsInstaller->getModuleName()); + $accountsEnabled = $accountsInstaller->isModuleEnabled(); + if ($accountsEnabled) return true; + + $moduleManager = $this->get('prestashop.module.manager'); + return $moduleManager->enable($accountsInstaller->getModuleName()); + } + + private function ensurePsEventbusEnabled() + { + $installer = $this->get('mbo.ps_eventbus.installer'); + if ($installer->install()) { + $installer->enable(); } } } diff --git a/src/DependencyInjection/ContainerProvider.php b/src/DependencyInjection/ContainerProvider.php index cb6d74667..eb8b6f84d 100644 --- a/src/DependencyInjection/ContainerProvider.php +++ b/src/DependencyInjection/ContainerProvider.php @@ -91,6 +91,7 @@ public function get(string $containerName): ContainerInterface $loader->load('http_clients.yml'); $loader->load('distribution.yml'); $loader->load('accounts.yml'); + $loader->load('eventbus.yml'); $loader->load('handler.yml'); $loader->load('api/distribution.yml'); diff --git a/src/Module/ActionsManager.php b/src/Module/ActionsManager.php index 8a0a92de6..7f65647bf 100644 --- a/src/Module/ActionsManager.php +++ b/src/Module/ActionsManager.php @@ -35,18 +35,9 @@ class ActionsManager */ private $filesManager; - /** - * @var Repository - * @TODO : Not needed anymore - */ - private $moduleRepository; - public function __construct( - FilesManager $filesManager, - Repository $moduleRepository - ) { + public function __construct(FilesManager $filesManager) { $this->filesManager = $filesManager; - $this->moduleRepository = $moduleRepository; } /** @@ -57,7 +48,7 @@ public function __construct( */ public function install(int $moduleId): void { - $moduleZip = $this->filesManager->downloadModule($moduleId); + $moduleZip = $this->downloadModule($moduleId); $this->filesManager->installFromSource($moduleZip); } diff --git a/src/Service/ModuleInstaller.php b/src/Service/ModuleInstaller.php new file mode 100755 index 000000000..816d02437 --- /dev/null +++ b/src/Service/ModuleInstaller.php @@ -0,0 +1,106 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ +declare(strict_types=1); + +namespace PrestaShop\Module\Mbo\Service; + +use PrestaShop\PrestaShop\Core\Addon\Module\ModuleManagerBuilder; + +class ModuleInstaller { + + private $moduleName; + private $moduleVersion; + private $absoluteCompare; + private $moduleManager; + + /** + * @param string $moduleName + * @param string|null $moduleVersion + * @param bool $absoluteCompare + */ + public function __construct(string $moduleName, string $moduleVersion = null, bool $absoluteCompare = false) { + $this->moduleName = $moduleName; + $this->moduleVersion = $moduleVersion; + $this->absoluteCompare = $absoluteCompare; + $moduleManagerBuilder = ModuleManagerBuilder::getInstance(); + $this->moduleManager = $moduleManagerBuilder->build(); + } + + /** + * Install ps_accounts module if not installed + * Method to call in every psx modules during the installation process + * + * @return bool + * + * @throws \Exception + */ + public function install() { + if ($this->isModuleInstalled() && $this->isModuleVersionSatisfied()) { + return true; + } + + return $this->moduleManager->install($this->moduleName); + } + + /** + * @return bool + */ + public function enable() + { + if (!$this->moduleManager->isEnabled($this->moduleName)) { + return $this->moduleManager->enable($this->moduleName); + } + + return true; + } + + /** + * @return bool + */ + public function isShopVersion17() + { + return version_compare(_PS_VERSION_, '1.7.0.0', '>='); + } + + /** + * @return boolean + */ + public function isModuleInstalled() { + if (false === $this->isShopVersion17()) { + return \Module::isInstalled($this->moduleName); + } + + return $this->moduleManager->isInstalled($this->moduleName); + } + + /** + * @return boolean + */ + public function isModuleVersionSatisfied() { + if (!$this->moduleVersion) return true; + $module = \Module::getInstanceByName($this->moduleName); + return version_compare( + $module->version, + $this->moduleVersion, + $this->absoluteCompare ? '>' : '>=' + ); + } + +} \ No newline at end of file diff --git a/src/Traits/Hooks/UseDashboardZoneOne.php b/src/Traits/Hooks/UseDashboardZoneOne.php old mode 100644 new mode 100755 index 4c16e5290..3ea45d0c2 --- a/src/Traits/Hooks/UseDashboardZoneOne.php +++ b/src/Traits/Hooks/UseDashboardZoneOne.php @@ -93,8 +93,19 @@ protected function loadPsAccounts(): string try { $accountsFacade = $this->get('mbo.ps_accounts.facade'); $accountsService = $accountsFacade->getPsAccountsService(); + if ($this->ensurePsAccountIsEnabled()) $this->ensurePsEventbusEnabled(); } catch (\PrestaShop\PsAccountsInstaller\Installer\Exception\InstallerException $e) { - ErrorHelper::reportError($e); + $accountsInstaller = $this->get('mbo.ps_accounts.installer'); + // Seems the module is not here, try to install it + $accountsInstaller->install(); + $accountsFacade = $this->get('mbo.ps_accounts.facade'); + try { + $accountsService = $accountsFacade->getPsAccountsService(); + } catch (\Exception $e) { + // Installation seems to not work properly + $accountsService = $accountsFacade = null; + ErrorHelper::reportError($e); + } } if (null !== $accountsFacade && null !== $accountsService) { @@ -113,4 +124,28 @@ protected function loadPsAccounts(): string return $urlAccountsCdn; } + + /** + * Return true if ps_account is enabled + * + * @return bool + */ + private function ensurePsAccountIsEnabled(): bool { + $accountsInstaller = $this->get('mbo.ps_accounts.installer'); + if (!$accountsInstaller) return false; + + $accountsEnabled = $accountsInstaller->isModuleEnabled(); + if ($accountsEnabled) return true; + + $moduleManager = $this->get('prestashop.module.manager'); + return $moduleManager->enable($accountsInstaller->getModuleName()); + } + + private function ensurePsEventbusEnabled() + { + $installer = $this->get('mbo.ps_eventbus.installer'); + if ($installer->install()) { + $installer->enable(); + } + } } diff --git a/views/templates/admin/controllers/module_catalog/catalog.html.twig b/views/templates/admin/controllers/module_catalog/catalog.html.twig index 12b913df2..1d1528be7 100644 --- a/views/templates/admin/controllers/module_catalog/catalog.html.twig +++ b/views/templates/admin/controllers/module_catalog/catalog.html.twig @@ -28,11 +28,13 @@ {% endif %} + {% if urlAccountsCdn is defined and urlAccountsCdn is not empty %} {# PrestaShop Account #}