Initial commit

This commit is contained in:
2020-10-07 10:37:15 +02:00
commit ce5f440392
28157 changed files with 4429172 additions and 0 deletions

View File

@@ -0,0 +1,93 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo;
use PrestaShop\PrestaShop\Adapter\Configuration;
use PrestaShop\PrestaShop\Adapter\LegacyContext;
use PrestaShop\PrestaShop\Core\Foundation\Version;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Responsible of provide Addons url with Context params for Module Selection and Theme Catalog pages
*/
class AddonsSelectionLinkProvider
{
/**
* @var Version
*/
private $version;
/**
* @var LegacyContext
*/
private $context;
/**
* @var Configuration
*/
private $configuration;
/**
* @var RequestStack
*/
private $requestStack;
/**
* @param Version $version
* @param LegacyContext $context
* @param Configuration $configuration
* @param RequestStack $requestStack
*/
public function __construct(
Version $version,
LegacyContext $context,
Configuration $configuration,
RequestStack $requestStack
) {
$this->version = $version;
$this->context = $context;
$this->configuration = $configuration;
$this->requestStack = $requestStack;
}
/**
* We cannot use http_build_query() here due to a bug on Addons
*
* @see https://github.com/PrestaShop/PrestaShop/pull/9255/files#r200498010
*
* @return string
*/
public function getLinkUrl()
{
$link = 'https://addons.prestashop.com/iframe/search-1.7.php?psVersion=' . $this->version->getVersion()
. '&isoLang=' . $this->context->getContext()->language->iso_code
. '&isoCurrency=' . $this->context->getContext()->currency->iso_code
. '&isoCountry=' . $this->context->getContext()->country->iso_code
. '&activity=' . $this->configuration->getInt('PS_SHOP_ACTIVITY')
. '&parentUrl=' . $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost();
if ('AdminPsMboTheme' === $this->requestStack->getCurrentRequest()->attributes->get('_legacy_controller')) {
$link .= '&onlyThemes=1';
}
return $link;
}
}

View File

@@ -0,0 +1,60 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\Controller\Admin;
use PrestaShopBundle\Controller\Admin\Improve\Modules\ModuleAbstractController;
use PrestaShopBundle\Security\Annotation\AdminSecurity;
use Symfony\Component\HttpFoundation\Response;
/**
* Responsible of "Improve > Modules > Modules Catalog" page display.
*/
class ModuleCatalogController extends ModuleAbstractController
{
/**
* Module Catalog page
*
* @AdminSecurity("is_granted(['read', 'create', 'update', 'delete'], 'ADMINMODULESSF_')")
*
* @return Response
*/
public function indexAction()
{
return $this->render(
'@PrestaShop/Admin/Module/catalog.html.twig',
[
'layoutHeaderToolbarBtn' => $this->getToolbarButtons(),
'layoutTitle' => $this->trans('Modules catalog', 'Admin.Navigation.Menu'),
'requireAddonsSearch' => true,
'requireBulkActions' => false,
'showContentHeader' => true,
'enableSidebar' => true,
'help_link' => $this->generateSidebarLink('AdminModules'),
'requireFilterStatus' => false,
'level' => $this->authorizationLevel(self::CONTROLLER_NAME),
'errorMessage' => $this->trans(
'You do not have permission to add this.',
'Admin.Notifications.Error'
),
]
);
}
}

View File

@@ -0,0 +1,95 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\Controller\Admin;
use PrestaShop\Module\Mbo\RecommendedModule\RecommendedModulePresenterInterface;
use PrestaShop\Module\Mbo\Tab\TabCollectionProviderInterface;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
/**
* Responsible of render json data for ajax display of Recommended Modules.
*/
class ModuleRecommendedController extends FrameworkBundleAdminController
{
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var TabCollectionProviderInterface
*/
private $tabCollectionProvider;
/**
* @var RecommendedModulePresenterInterface
*/
private $recommendedModulePresenter;
/**
* @param RequestStack $requestStack
* @param TabCollectionProviderInterface $tabCollectionProvider
* @param RecommendedModulePresenterInterface $recommendedModulePresenter
*/
public function __construct(
RequestStack $requestStack,
TabCollectionProviderInterface $tabCollectionProvider,
RecommendedModulePresenterInterface $recommendedModulePresenter
) {
parent::__construct();
$this->requestStack = $requestStack;
$this->tabCollectionProvider = $tabCollectionProvider;
$this->recommendedModulePresenter = $recommendedModulePresenter;
}
/**
* @return JsonResponse
*/
public function indexAction()
{
$response = new JsonResponse();
try {
$tabCollection = $this->tabCollectionProvider->getTabCollection();
$tabClassName = $this->requestStack->getCurrentRequest()->get('tabClassName');
$tab = $tabCollection->getTab($tabClassName);
$response->setData([
'content' => $this->renderView(
'@Modules/ps_mbo/views/templates/admin/controllers/module_catalog/recommended-modules.html.twig',
[
'recommendedModulesInstalled' => $this->recommendedModulePresenter->presentCollection($tab->getRecommendedModulesInstalled()),
'recommendedModulesNotInstalled' => $this->recommendedModulePresenter->presentCollection($tab->getRecommendedModulesNotInstalled()),
]
),
]);
} catch (ServiceUnavailableHttpException $exception) {
$response->setData([
'content' => $this->renderView('@Modules/ps_mbo/views/templates/admin/error.html.twig'),
]);
$response->setStatusCode($exception->getStatusCode());
$response->headers->add($exception->getHeaders());
}
return $response;
}
}

View File

@@ -0,0 +1,100 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\Controller\Admin;
use PrestaShop\Module\Mbo\AddonsSelectionLinkProvider;
use PrestaShop\Module\Mbo\ExternalContentProvider\ExternalContentProviderInterface;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Security\Annotation\AdminSecurity;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
/**
* Responsible of "Improve > Modules > Modules Catalog > Modules Selection" page display.
*/
class ModuleSelectionController extends FrameworkBundleAdminController
{
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var ExternalContentProviderInterface
*/
private $externalContentProvider;
/**
* @var AddonsSelectionLinkProvider
*/
private $addonsSelectionLinkProvider;
/**
* @param RequestStack $requestStack
* @param ExternalContentProviderInterface $externalContentCollectionProvider
* @param AddonsSelectionLinkProvider $addonsSelectionLinkProvider
*/
public function __construct(
RequestStack $requestStack,
ExternalContentProviderInterface $externalContentCollectionProvider,
AddonsSelectionLinkProvider $addonsSelectionLinkProvider
) {
parent::__construct();
$this->requestStack = $requestStack;
$this->externalContentProvider = $externalContentCollectionProvider;
$this->addonsSelectionLinkProvider = $addonsSelectionLinkProvider;
}
/**
* @AdminSecurity("is_granted('read', request.get('_legacy_controller'))")
*
* @return Response
*/
public function indexAction()
{
$response = new Response();
try {
$response->setContent($this->renderView(
'@Modules/ps_mbo/views/templates/admin/controllers/module_catalog/addons_store.html.twig',
[
'pageContent' => $this->externalContentProvider->getContent($this->addonsSelectionLinkProvider->getLinkUrl()),
'layoutHeaderToolbarBtn' => [],
'layoutTitle' => $this->trans('Module selection', 'Admin.Navigation.Menu'),
'requireAddonsSearch' => true,
'requireBulkActions' => false,
'showContentHeader' => true,
'enableSidebar' => true,
'help_link' => $this->generateSidebarLink($this->requestStack->getCurrentRequest()->get('_legacy_controller')),
'requireFilterStatus' => false,
'level' => $this->authorizationLevel($this->requestStack->getCurrentRequest()->get('_legacy_controller')),
]
));
} catch (ServiceUnavailableHttpException $exception) {
$response->setContent($this->renderView('@Modules/ps_mbo/views/templates/admin/error.html.twig'));
$response->setStatusCode($exception->getStatusCode());
$response->headers->add($exception->getHeaders());
}
return $response;
}
}

View File

@@ -0,0 +1,100 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\Controller\Admin;
use PrestaShop\Module\Mbo\AddonsSelectionLinkProvider;
use PrestaShop\Module\Mbo\ExternalContentProvider\ExternalContentProviderInterface;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Security\Annotation\AdminSecurity;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
/**
* Responsible of "Improve > Design > Themes Catalog" page display.
*/
class ThemeCatalogController extends FrameworkBundleAdminController
{
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var ExternalContentProviderInterface
*/
private $externalContentProvider;
/**
* @var AddonsSelectionLinkProvider
*/
private $addonsSelectionLinkProvider;
/**
* @param RequestStack $requestStack
* @param ExternalContentProviderInterface $externalContentCollectionProvider
* @param AddonsSelectionLinkProvider $addonsSelectionLinkProvider
*/
public function __construct(
RequestStack $requestStack,
ExternalContentProviderInterface $externalContentCollectionProvider,
AddonsSelectionLinkProvider $addonsSelectionLinkProvider
) {
parent::__construct();
$this->requestStack = $requestStack;
$this->externalContentProvider = $externalContentCollectionProvider;
$this->addonsSelectionLinkProvider = $addonsSelectionLinkProvider;
}
/**
* @AdminSecurity("is_granted('read', request.get('_legacy_controller'))")
*
* @return Response
*/
public function indexAction()
{
$response = new Response();
try {
$response->setContent($this->renderView(
'@Modules/ps_mbo/views/templates/admin/controllers/theme_catalog/addons_store.html.twig',
[
'pageContent' => $this->externalContentProvider->getContent($this->addonsSelectionLinkProvider->getLinkUrl()),
'layoutHeaderToolbarBtn' => [],
'layoutTitle' => $this->trans('Themes Catalog', 'Admin.Navigation.Menu'),
'requireAddonsSearch' => true,
'requireBulkActions' => false,
'showContentHeader' => true,
'enableSidebar' => true,
'help_link' => $this->generateSidebarLink($this->requestStack->getCurrentRequest()->get('_legacy_controller')),
'requireFilterStatus' => false,
'level' => $this->authorizationLevel($this->requestStack->getCurrentRequest()->get('_legacy_controller')),
]
));
} catch (ServiceUnavailableHttpException $exception) {
$response->setContent($this->renderView('@Modules/ps_mbo/views/templates/admin/error.html.twig'));
$response->setStatusCode($exception->getStatusCode());
$response->headers->add($exception->getHeaders());
}
return $response;
}
}

View File

@@ -0,0 +1,28 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@@ -0,0 +1,28 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@@ -0,0 +1,106 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\ExternalContentProvider;
use Closure;
use PrestaShop\CircuitBreaker\FactorySettings;
use PrestaShop\CircuitBreaker\SimpleCircuitBreakerFactory;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ExternalContentProvider implements ExternalContentProviderInterface
{
const ALLOWED_FAILURES = 2;
const TIMEOUT_SECONDS = 0.6;
const THRESHOLD_SECONDS = 3600; // Retry in 1 hour
/**
* @var SimpleCircuitBreakerFactory
*/
private $circuitBreakerFactory;
/**
* @var OptionsResolver
*/
private $optionsResolver;
/**
* Constructor.
*/
public function __construct()
{
$this->circuitBreakerFactory = new SimpleCircuitBreakerFactory();
$this->optionsResolver = new OptionsResolver();
$this->configureOptions();
}
/**
* {@inheritdoc}
*
* @throws ServiceUnavailableHttpException
*/
public function getContent($url, array $options = [])
{
$settings = $this->optionsResolver->resolve($options);
$apiSettings = new FactorySettings(
$settings['failures'],
$settings['timeout'],
$settings['threshold']
);
$circuitBreaker = $this->circuitBreakerFactory->create($apiSettings);
return $circuitBreaker->call(
$url,
$settings['client_options'],
$this->circuitBreakerFallback()
);
}
private function configureOptions()
{
$this->optionsResolver->setDefaults([
'failures' => self::ALLOWED_FAILURES,
'timeout' => self::TIMEOUT_SECONDS,
'threshold' => self::THRESHOLD_SECONDS,
'client_options' => [],
]);
$this->optionsResolver->setAllowedTypes('failures', 'numeric');
$this->optionsResolver->setAllowedTypes('timeout', 'numeric');
$this->optionsResolver->setAllowedTypes('threshold', 'numeric');
$this->optionsResolver->setAllowedTypes('client_options', 'array');
}
/**
* Called by CircuitBreaker if the service is unavailable
*
* @return Closure
*/
private function circuitBreakerFallback()
{
return function () {
throw new ServiceUnavailableHttpException(self::THRESHOLD_SECONDS);
};
}
}

View File

@@ -0,0 +1,32 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\ExternalContentProvider;
interface ExternalContentProviderInterface
{
/**
* @param string $url
* @param array $options
*
* @return string
*/
public function getContent($url, array $options = []);
}

View File

@@ -0,0 +1,28 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@@ -0,0 +1,132 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo;
use Module;
use PrestaShop\PrestaShop\Adapter\LegacyContext;
use PrestaShop\PrestaShop\Adapter\Module\AdminModuleDataProvider;
use PrestaShop\PrestaShop\Adapter\Presenter\PresenterInterface;
use PrestaShop\PrestaShop\Core\Addon\AddonsCollection;
use PrestaShop\PrestaShop\Core\Addon\Module\ModuleRepository;
use PrestaShop\PrestaShop\Core\Addon\Module\ModuleRepositoryInterface;
use PrestaShopBundle\Entity\Repository\TabRepository;
use Profile;
class ModuleCollectionDataProvider
{
/**
* @var AdminModuleDataProvider
*/
private $addonsProvider;
/**
* @var ModuleRepositoryInterface
*/
private $moduleRepository;
/**
* @var PresenterInterface
*/
private $modulePresenter;
/**
* @var TabRepository
*/
private $tabRepository;
/**
* @var LegacyContext
*/
private $context;
/**
* Constructor.
*
* @param AdminModuleDataProvider $addonsProvider
* @param ModuleRepositoryInterface $moduleRepository
* @param PresenterInterface $modulePresenter
* @param TabRepository $tabRepository
* @param LegacyContext $context
*/
public function __construct(
AdminModuleDataProvider $addonsProvider,
ModuleRepositoryInterface $moduleRepository,
PresenterInterface $modulePresenter,
TabRepository $tabRepository,
LegacyContext $context
) {
$this->addonsProvider = $addonsProvider;
$this->moduleRepository = $moduleRepository;
$this->modulePresenter = $modulePresenter;
$this->tabRepository = $tabRepository;
$this->context = $context;
}
/**
* @param array $moduleNames
*
* @return array
*/
public function getData(array $moduleNames)
{
$data = [];
$modulesOnDisk = AddonsCollection::createFrom($this->moduleRepository->getList());
$modulesOnDisk = $this->addonsProvider->generateAddonsUrls($modulesOnDisk);
foreach ($modulesOnDisk as $module) {
/** @var \PrestaShop\PrestaShop\Adapter\Module\Module $module */
if (!in_array($module->get('name'), $moduleNames)) {
continue;
}
if ($module->get('id')) {
$isEmployeeAllowed = (bool) Module::getPermissionStatic(
$module->get('id'),
'configure',
$this->context->getContext()->employee
);
} else {
$ModuleTabId = $this->tabRepository->findOneIdByClassName('AdminModules');
/** @var array $access */
$access = Profile::getProfileAccess(
$this->context->getContext()->employee->id_profile,
$ModuleTabId
);
$isEmployeeAllowed = !$access['edit'];
}
if (false === $isEmployeeAllowed) {
continue;
}
if ($module->get('author') === ModuleRepository::PARTNER_AUTHOR) {
$module->set('type', 'addonsPartner');
}
$module->fillLogo();
$data[$module->get('name')] = $this->modulePresenter->present($module);
}
return $data;
}
}

View File

@@ -0,0 +1,77 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\RecommendedLink;
class RecommendedLink implements RecommendedLinkInterface
{
/**
* @var string
*/
private $id;
/**
* @var string
*/
private $url;
/**
* @var string
*/
private $name;
/**
* RecommendedLink constructor.
*
* @param string $id
* @param string $url
* @param string $name
*/
public function __construct($id, $url, $name)
{
$this->id = $id;
$this->url = $url;
$this->name = $name;
}
/**
* {@inheritdoc}
*/
public function getId()
{
return $this->id;
}
/**
* {@inheritdoc}
*/
public function getUrl()
{
return $this->url;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}
}

View File

@@ -0,0 +1,45 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\RecommendedLink;
interface RecommendedLinkInterface
{
/**
* Get the identifier of the recommended link.
*
* @return string
*/
public function getId();
/**
* Get the url of the recommended module.
*
* @return string
*/
public function getUrl();
/**
* Get the display name of the recommended module.
*
* @return string
*/
public function getName();
}

View File

@@ -0,0 +1,98 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\RecommendedLink;
use PrestaShop\PrestaShop\Adapter\LegacyContext;
use Symfony\Component\Serializer\SerializerInterface;
class RecommendedLinkProvider
{
/**
* @var LegacyContext
*/
private $context;
/**
* @var SerializerInterface
*/
private $serializer;
/**
* Constructor.
*
* @param SerializerInterface $serializer
* @param LegacyContext $context
*/
public function __construct(
LegacyContext $context,
SerializerInterface $serializer
) {
$this->context = $context;
$this->serializer = $serializer;
}
/**
* @return RecommendedLink[]
*/
public function getRecommendedLinks()
{
$recommendedLinks = [];
$cacheFile = $this->getCacheFile();
if ($cacheFile) {
$recommendedLinks = $this->serializer->deserialize(
file_get_contents($cacheFile),
sprintf('%s[]', RecommendedLink::class),
'json'
);
}
return $recommendedLinks;
}
/**
* Search a cache associated to context language or try to fallback to default locale
*
* @return string
*/
private function getCacheFile()
{
$cacheFile = _PS_MODULE_DIR_
. 'ps_mbo/cache/recommended-links-'
. strtolower($this->context->getContext()->language->iso_code)
. '.json'
;
if (file_exists($cacheFile)) {
return $cacheFile;
}
$cacheFile = _PS_MODULE_DIR_
. 'ps_mbo/cache/recommended-links-en.json'
;
if (file_exists($cacheFile)) {
return $cacheFile;
}
return '';
}
}

View File

@@ -0,0 +1,116 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\RecommendedModule;
class RecommendedModule implements RecommendedModuleInterface
{
/**
* @var string technical name of the recommended module
*/
private $moduleName;
/**
* @var int position of the recommended module
*/
private $position;
/**
* @var bool
*/
private $isInstalled;
/**
* @var array
*/
private $moduleData;
/**
* {@inheritdoc}
*/
public function getModuleName()
{
return $this->moduleName;
}
/**
* {@inheritdoc}
*/
public function setModuleName($moduleName)
{
$this->moduleName = $moduleName;
return $this;
}
/**
* {@inheritdoc}
*/
public function getPosition()
{
return $this->position;
}
/**
* {@inheritdoc}
*/
public function setPosition($position)
{
$this->position = $position;
return $this;
}
/**
* {@inheritdoc}
*/
public function isInstalled()
{
return $this->isInstalled;
}
/**
* {@inheritdoc}
*/
public function setInstalled($isInstalled)
{
$this->isInstalled = $isInstalled;
return $this;
}
/**
* {@inheritdoc}
*/
public function getModuleData()
{
return $this->moduleData;
}
/**
* {@inheritdoc}
*/
public function setModuleData($moduleData)
{
$this->moduleData = $moduleData;
return $this;
}
}

View File

@@ -0,0 +1,192 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\RecommendedModule;
use ArrayIterator;
use Closure;
class RecommendedModuleCollection implements RecommendedModuleCollectionInterface
{
/**
* @var RecommendedModuleInterface[]
*/
private $recommendedModules = [];
/**
* {@inheritdoc}
*/
public function addRecommendedModule(RecommendedModuleInterface $recommendedModule)
{
$this->recommendedModules[] = $recommendedModule;
return $this;
}
/**
* {@inheritdoc}
*/
public function getRecommendedModule($moduleName)
{
foreach ($this->recommendedModules as $recommendedModule) {
if ($moduleName === $recommendedModule->getModuleName()) {
return $recommendedModule;
}
}
return false;
}
/**
* {@inheritdoc}
*/
public function getRecommendedModuleNames()
{
$recommendedModuleNames = [];
foreach ($this->recommendedModules as $recommendedModule) {
$recommendedModuleNames[] = $recommendedModule->getModuleName();
}
return $recommendedModuleNames;
}
/**
* {@inheritdoc}
*/
public function offsetExists($offset)
{
return array_key_exists($offset, $this->recommendedModules);
}
/**
* {@inheritdoc}
*/
public function offsetGet($offset)
{
return $this->recommendedModules[$offset];
}
/**
* {@inheritdoc}
*/
public function offsetSet($offset, $value)
{
$this->recommendedModules[$offset] = $value;
}
/**
* {@inheritdoc}
*/
public function offsetUnset($offset)
{
unset($this->recommendedModules[$offset]);
}
/**
* {@inheritdoc}
*/
public function getIterator()
{
return new ArrayIterator($this->recommendedModules);
}
/**
* {@inheritdoc}
*/
public function count()
{
return count($this->recommendedModules);
}
/**
* {@inheritdoc}
*/
public function isEmpty()
{
return empty($this->recommendedModules);
}
/**
* {@inheritdoc}
*/
public function sortByPosition()
{
$this->sort(function (
RecommendedModuleInterface $recommendedModuleA,
RecommendedModuleInterface $recommendedModuleB
) {
if ($recommendedModuleA->getPosition() === $recommendedModuleB->getPosition()) {
return 0;
}
return ($recommendedModuleA->getPosition() < $recommendedModuleB->getPosition()) ? -1 : 1;
});
}
/**
* {@inheritdoc}
*/
public function getInstalled()
{
return $this->filter(function (RecommendedModuleInterface $recommendedModule) {
return $recommendedModule->isInstalled();
});
}
/**
* {@inheritdoc}
*/
public function getNotInstalled()
{
return $this->filter(function (RecommendedModuleInterface $recommendedModule) {
return !$recommendedModule->isInstalled();
});
}
/**
* @param Closure $closure
*
* @return RecommendedModuleCollection
*/
private function filter(Closure $closure)
{
$recommendedModules = new static();
$recommendedModules->recommendedModules = array_filter(
$this->recommendedModules,
$closure,
ARRAY_FILTER_USE_BOTH
);
$recommendedModules->sortByPosition();
return $recommendedModules;
}
/**
* @param Closure $closure
*/
private function sort(Closure $closure)
{
uasort(
$this->recommendedModules,
$closure
);
}
}

View File

@@ -0,0 +1,84 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\RecommendedModule;
use ArrayAccess;
use Countable;
use IteratorAggregate;
interface RecommendedModuleCollectionInterface extends ArrayAccess, IteratorAggregate, Countable
{
/**
* Add a recommended module to this collection.
*
* @param RecommendedModuleInterface $recommendedModule
*
* @return self
*/
public function addRecommendedModule(RecommendedModuleInterface $recommendedModule);
/**
* Get a recommended module by name
*
* @param string $moduleName
*
* @return RecommendedModuleInterface|false
*/
public function getRecommendedModule($moduleName);
/**
* Get names of recommended modules
*
* @return string[]
*/
public function getRecommendedModuleNames();
/**
* @param mixed $offset
*
* @return RecommendedModuleInterface
*/
public function offsetGet($offset);
/**
* @return bool
*/
public function isEmpty();
/**
* Sort recommended modules by position
*/
public function sortByPosition();
/**
* Get recommended modules installed.
*
* @return RecommendedModuleCollectionInterface
*/
public function getInstalled();
/**
* Get recommended modules not installed.
*
* @return RecommendedModuleCollectionInterface
*/
public function getNotInstalled();
}

View File

@@ -0,0 +1,80 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\RecommendedModule;
interface RecommendedModuleInterface
{
/**
* Get the technical name of the recommended module.
*
* @return string
*/
public function getModuleName();
/**
* @param string $moduleName
*
* @return RecommendedModuleInterface
*/
public function setModuleName($moduleName);
/**
* Get the position of the recommended module.
*
* @return int
*/
public function getPosition();
/**
* @param int $position
*
* @return RecommendedModuleInterface
*/
public function setPosition($position);
/**
* Check if the recommended modules is installed.
*
* @return bool
*/
public function isInstalled();
/**
* @param bool $isInstalled
*
* @return RecommendedModuleInterface
*/
public function setInstalled($isInstalled);
/**
* Get the recommended module data.
*
* @return array
*/
public function getModuleData();
/**
* @param array $moduleData
*
* @return RecommendedModuleInterface
*/
public function setModuleData($moduleData);
}

View File

@@ -0,0 +1,46 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\RecommendedModule;
class RecommendedModulePresenter implements RecommendedModulePresenterInterface
{
/**
* {@inheritdoc}
*/
public function present(RecommendedModuleInterface $recommendedModule)
{
return $recommendedModule->getModuleData();
}
/**
* {@inheritdoc}
*/
public function presentCollection(RecommendedModuleCollectionInterface $recommendedModules)
{
$recommendedModulesPresented = [];
foreach ($recommendedModules as $recommendedModule) {
$recommendedModulesPresented[] = $this->present($recommendedModule);
}
return $recommendedModulesPresented;
}
}

View File

@@ -0,0 +1,42 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\RecommendedModule;
interface RecommendedModulePresenterInterface
{
/**
* Transform a RecommendedModuleInterface as a simple array of data.
*
* @param RecommendedModuleInterface $recommendedModule
*
* @return array
*/
public function present(RecommendedModuleInterface $recommendedModule);
/**
* Transform a collection of RecommendedModulesInterface as a simple array of data.
*
* @param RecommendedModuleCollectionInterface $recommendedModules
*
* @return array
*/
public function presentCollection(RecommendedModuleCollectionInterface $recommendedModules);
}

View File

@@ -0,0 +1,28 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@@ -0,0 +1,158 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\Tab;
use PrestaShop\Module\Mbo\RecommendedModule\RecommendedModuleCollection;
use PrestaShop\Module\Mbo\RecommendedModule\RecommendedModuleCollectionInterface;
class Tab implements TabInterface
{
/**
* @var string class name of the tab
*/
private $legacyClassName;
/**
* @var string class name of the tab
*/
private $displayMode;
/**
* @var RecommendedModuleCollectionInterface recommended modules of the tab
*/
private $recommendedModules;
/**
* Tab constructor.
*/
public function __construct()
{
$this->recommendedModules = new RecommendedModuleCollection();
}
/**
* {@inheritdoc}
*/
public function getLegacyClassName()
{
return $this->legacyClassName;
}
/**
* {@inheritdoc}
*/
public function setLegacyClassName($legacyClassName)
{
$this->legacyClassName = $legacyClassName;
return $this;
}
/**
* {@inheritdoc}
*/
public function getDisplayMode()
{
return $this->displayMode;
}
/**
* {@inheritdoc}
*/
public function setDisplayMode($displayMode)
{
$this->displayMode = $displayMode;
return $this;
}
/**
* {@inheritdoc}
*/
public function getRecommendedModules()
{
return $this->recommendedModules;
}
/**
* {@inheritdoc}
*/
public function setRecommendedModules(RecommendedModuleCollectionInterface $recommendedModules)
{
$this->recommendedModules = $recommendedModules;
return $this;
}
/**
* {@inheritdoc}
*/
public function hasRecommendedModules()
{
return !$this->recommendedModules->isEmpty();
}
/**
* {@inheritdoc}
*/
public function getRecommendedModulesInstalled()
{
$recommendedModulesInstalled = $this->getRecommendedModules();
if ($this->hasRecommendedModules()) {
return $recommendedModulesInstalled->getInstalled();
}
return $recommendedModulesInstalled;
}
/**
* {@inheritdoc}
*/
public function getRecommendedModulesNotInstalled()
{
$recommendedModulesNotInstalled = $this->getRecommendedModules();
if ($this->hasRecommendedModules()) {
return $recommendedModulesNotInstalled->getNotInstalled();
}
return $recommendedModulesNotInstalled;
}
/**
* {@inheritdoc}
*/
public function shouldDisplayButton()
{
return $this->hasRecommendedModules()
&& TabInterface::DISPLAY_MODE_MODAL === $this->getDisplayMode();
}
/**
* {@inheritdoc}
*/
public function shouldDisplayAfterContent()
{
return $this->hasRecommendedModules()
&& TabInterface::DISPLAY_MODE_AFTER_CONTENT === $this->getDisplayMode();
}
}

View File

@@ -0,0 +1,111 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\Tab;
use ArrayIterator;
class TabCollection implements TabCollectionInterface
{
/**
* @var TabInterface[]
*/
private $tabs = [];
/**
* {@inheritdoc}
*/
public function addTab(TabInterface $tab)
{
$this->tabs[] = $tab;
return $this;
}
/**
* {@inheritdoc}
*/
public function getTab($tabClassName)
{
foreach ($this->tabs as $tab) {
if ($tabClassName === $tab->getLegacyClassName()) {
return $tab;
}
}
return new Tab();
}
/**
* {@inheritdoc}
*/
public function offsetExists($offset)
{
return array_key_exists($offset, $this->tabs);
}
/**
* {@inheritdoc}
*/
public function offsetSet($offset, $value)
{
$this->tabs[$offset] = $value;
}
/**
* {@inheritdoc}
*/
public function offsetGet($offset)
{
return $this->tabs[$offset];
}
/**
* {@inheritdoc}
*/
public function offsetUnset($offset)
{
unset($this->tabs[$offset]);
}
/**
* {@inheritdoc}
*/
public function getIterator()
{
return new ArrayIterator($this->tabs);
}
/**
* {@inheritdoc}
*/
public function count()
{
return count($this->tabs);
}
/**
* {@inheritdoc}
*/
public function isEmpty()
{
return empty($this->tabs);
}
}

View File

@@ -0,0 +1,83 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\Tab;
class TabCollectionDecoderXml
{
private $content;
/**
* Constructor.
*
* @param string $content
*/
public function __construct($content)
{
$this->content = $content;
}
/**
* @return array
*/
public function toArray()
{
$data = [];
if (empty($this->content)) {
return $data;
}
$simpleXMLElement = @simplexml_load_string($this->content);
if (false === $simpleXMLElement
|| !isset($simpleXMLElement->tab)
) {
return $data;
}
foreach ($simpleXMLElement->tab as $tab) {
$tabClassName = null;
$tabDisplayMode = 'slider_list';
$tabRecommendedModules = [];
foreach ($tab->attributes() as $key => $value) {
if ('class_name' === $key) {
$tabClassName = (string) $value;
}
if ('display_type' === $key) {
$tabDisplayMode = (string) $value;
}
}
foreach ($tab->children() as $module) {
if (isset($module['position'], $module['name'])) {
$tabRecommendedModules[(int) $module['position']] = (string) $module['name'];
}
}
if (!empty($tabClassName)) {
$data[$tabClassName] = [
'displayMode' => $tabDisplayMode,
'recommendedModules' => $tabRecommendedModules,
];
}
}
return $data;
}
}

View File

@@ -0,0 +1,104 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\Tab;
use PrestaShop\Module\Mbo\ModuleCollectionDataProvider;
use PrestaShop\Module\Mbo\RecommendedModule\RecommendedModule;
use PrestaShop\Module\Mbo\RecommendedModule\RecommendedModuleCollection;
class TabCollectionFactory implements TabCollectionFactoryInterface
{
private $moduleCollectionDataProvider;
/**
* Constructor.
*
* @param ModuleCollectionDataProvider $moduleCollectionDataProvider
*/
public function __construct(ModuleCollectionDataProvider $moduleCollectionDataProvider)
{
$this->moduleCollectionDataProvider = $moduleCollectionDataProvider;
}
/**
* {@inheritdoc}
*/
public function buildFromArray(array $data)
{
$tabCollection = new TabCollection();
if (empty($data)) {
return $tabCollection;
}
$modulesData = $this->moduleCollectionDataProvider->getData($this->getModuleNames($data));
if (empty($modulesData)) {
return $tabCollection;
}
foreach ($data as $tabClassName => $tabData) {
$recommendedModuleCollection = new RecommendedModuleCollection();
foreach ($tabData['recommendedModules'] as $position => $moduleName) {
if (isset($modulesData[$moduleName])) {
$recommendedModule = new RecommendedModule();
$recommendedModule->setModuleName($moduleName);
$recommendedModule->setPosition((int) $position);
$recommendedModule->setInstalled((bool) $modulesData[$moduleName]['database']['installed']);
$recommendedModule->setModuleData($modulesData[$moduleName]);
$recommendedModuleCollection->addRecommendedModule($recommendedModule);
}
}
if (!$recommendedModuleCollection->isEmpty()) {
$recommendedModuleCollection->sortByPosition();
$tab = new Tab();
$tab->setLegacyClassName($tabClassName);
$tab->setDisplayMode($tabData['displayMode']);
$tab->setRecommendedModules($recommendedModuleCollection);
$tabCollection->addTab($tab);
}
}
return $tabCollection;
}
/**
* @param array $data
*
* @return string[]
*/
private function getModuleNames(array $data)
{
$moduleNames = [];
foreach ($data as $tabData) {
foreach ($tabData['recommendedModules'] as $moduleName) {
$moduleNames[] = $moduleName;
}
}
return array_unique($moduleNames);
}
}

View File

@@ -0,0 +1,33 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\Tab;
interface TabCollectionFactoryInterface
{
/**
* Builds a tabs recommended modules collection from an array.
*
* @param array $data
*
* @return TabCollectionInterface
*/
public function buildFromArray(array $data);
}

View File

@@ -0,0 +1,56 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\Tab;
use ArrayAccess;
use Countable;
use IteratorAggregate;
interface TabCollectionInterface extends ArrayAccess, IteratorAggregate, Countable
{
/**
* Add a tab to this collection.
*
* @param TabInterface $tab
*
* @return self
*/
public function addTab(TabInterface $tab);
/**
* @param string $tabClassName
*
* @return TabInterface
*/
public function getTab($tabClassName);
/**
* @param mixed $offset
*
* @return TabInterface
*/
public function offsetGet($offset);
/**
* @return bool
*/
public function isEmpty();
}

View File

@@ -0,0 +1,128 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\Tab;
use Doctrine\Common\Cache\CacheProvider;
use PrestaShop\Module\Mbo\ExternalContentProvider\ExternalContentProviderInterface;
use PrestaShop\PrestaShop\Adapter\LegacyContext;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
class TabCollectionProvider implements TabCollectionProviderInterface
{
const CACHE_KEY = 'recommendedModules';
const CACHE_LIFETIME_SECONDS = 604800;
const API_URL = 'https://api.prestashop.com/xml/tab_modules_list_17.xml';
/**
* @var LegacyContext
*/
private $context;
/**
* @var ExternalContentProviderInterface
*/
private $externalContentProvider;
/**
* @var TabCollectionFactoryInterface
*/
private $tabCollectionFactory;
/**
* @var CacheProvider|null
*/
private $cacheProvider;
/**
* @param LegacyContext $context
* @param ExternalContentProviderInterface $externalContentProvider
* @param TabCollectionFactoryInterface $tabCollectionFactory
* @param CacheProvider|null $cacheProvider
*/
public function __construct(
LegacyContext $context,
ExternalContentProviderInterface $externalContentProvider,
TabCollectionFactoryInterface $tabCollectionFactory,
CacheProvider $cacheProvider = null
) {
$this->context = $context;
$this->externalContentProvider = $externalContentProvider;
$this->tabCollectionFactory = $tabCollectionFactory;
$this->cacheProvider = $cacheProvider;
}
/**
* {@inheritdoc}
*/
public function getTabCollection()
{
if ($this->isTabCollectionCached()) {
return $this->cacheProvider->fetch($this->getCacheKey());
}
$tabCollection = $this->getTabCollectionFromApi();
if ($this->cacheProvider
&& false === $tabCollection->isEmpty()
) {
$this->cacheProvider->save(
$this->getCacheKey(),
$tabCollection,
static::CACHE_LIFETIME_SECONDS
);
}
return $tabCollection;
}
private function getCacheKey()
{
return static::CACHE_KEY . '-' . $this->context->getEmployeeLanguageIso();
}
/**
* Check if recommended modules cache is set
*
* @return bool
*/
public function isTabCollectionCached()
{
return $this->cacheProvider
&& $this->cacheProvider->contains($this->getCacheKey());
}
/**
* Retrieve tabs with recommended modules from PrestaShop
*
* @return TabCollectionInterface
*
* @throws ServiceUnavailableHttpException
*/
private function getTabCollectionFromApi()
{
$apiResponse = $this->externalContentProvider->getContent(self::API_URL);
$tabCollectionDecoderXml = new TabCollectionDecoderXml($apiResponse);
return $this->tabCollectionFactory->buildFromArray($tabCollectionDecoderXml->toArray());
}
}

View File

@@ -0,0 +1,29 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\Tab;
interface TabCollectionProviderInterface
{
/**
* @return TabCollectionInterface
*/
public function getTabCollection();
}

View File

@@ -0,0 +1,103 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\Module\Mbo\Tab;
use PrestaShop\Module\Mbo\RecommendedModule\RecommendedModuleCollectionInterface;
interface TabInterface
{
const DISPLAY_MODE_MODAL = 'slider_list';
const DISPLAY_MODE_AFTER_CONTENT = 'default_list';
/**
* Get the class name of the tab.
*
* @return string
*/
public function getLegacyClassName();
/**
* @param string $legacyClassName
*
* @return TabInterface
*/
public function setLegacyClassName($legacyClassName);
/**
* Get the display mode of the tab.
*
* @return string
*/
public function getDisplayMode();
/**
* @param string $displayMode
*
* @return TabInterface
*/
public function setDisplayMode($displayMode);
/**
* Get the recommended modules of the tab.
*
* @return RecommendedModuleCollectionInterface
*/
public function getRecommendedModules();
/**
* @param RecommendedModuleCollectionInterface $recommendedModules
*
* @return TabInterface
*/
public function setRecommendedModules(RecommendedModuleCollectionInterface $recommendedModules);
/**
* Check if the tab has recommended modules.
*
* @return bool
*/
public function hasRecommendedModules();
/**
* Get the installed recommended modules of the tab.
*
* @return RecommendedModuleCollectionInterface
*/
public function getRecommendedModulesInstalled();
/**
* Get the not installed recommended modules of the tab.
*
* @return RecommendedModuleCollectionInterface
*/
public function getRecommendedModulesNotInstalled();
/**
* @return bool
*/
public function shouldDisplayButton();
/**
* @return bool
*/
public function shouldDisplayAfterContent();
}

View File

@@ -0,0 +1,28 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@@ -0,0 +1,28 @@
<?php
/**
* 2007-2020 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;