From d445415be7aa7e5bc2c06e674be58f6e57273e72 Mon Sep 17 00:00:00 2001 From: mandan2 <61560082+mandan2@users.noreply.github.com> Date: Tue, 24 Oct 2023 09:39:02 +0300 Subject: [PATCH] PIPRES-348: Move subscription options to subscription tab (#832) * PIPRES-348: WIP move subscription options to subscription tab * added carrier repository to provide with carrier choices, moved template to another file * phpstan * adjusted PS 1.7.6 support * removed validations from formProcess * simplified formProcess --- config/routes.yml | 8 +++ config/services.yml | 45 ++++++++++++++ src/Builder/FormBuilder.php | 57 +---------------- .../Symfony/AbstractSymfonyController.php | 6 +- .../Symfony/SubscriptionController.php | 55 +++++++++++++++-- .../ChoiceProvider/CarrierOptionsProvider.php | 35 +++++++++++ .../SubscriptionOptionsConfiguration.php | 60 ++++++++++++++++++ .../SubscriptionOptionsDataProvider.php | 43 +++++++++++++ .../Form/Options/SubscriptionOptionsType.php | 41 +++++++++++++ .../Handler/RecurringOrderHandler.php | 1 + subscription/Logger/Logger.php | 2 + .../Subscription/subscriptions-grid.html.twig | 4 +- .../subscriptions-settings.html.twig | 61 +++++++++++++++++++ 13 files changed, 352 insertions(+), 66 deletions(-) create mode 100644 subscription/Form/ChoiceProvider/CarrierOptionsProvider.php create mode 100644 subscription/Form/Options/SubscriptionOptionsConfiguration.php create mode 100644 subscription/Form/Options/SubscriptionOptionsDataProvider.php create mode 100644 subscription/Form/Options/SubscriptionOptionsType.php create mode 100644 views/templates/admin/Subscription/subscriptions-settings.html.twig diff --git a/config/routes.yml b/config/routes.yml index b2814487e..204756e25 100644 --- a/config/routes.yml +++ b/config/routes.yml @@ -14,6 +14,14 @@ admin_subscription_index: _legacy_controller: AdminMollieSubscriptionOrders _legacy_link: AdminMollieSubscriptionOrders +admin_subscription_options_submit: + path: admin-subscription-options-submit + methods: POST + defaults: + _controller: Mollie\Subscription\Controller\Symfony\SubscriptionController::submitOptionsAction + _legacy_controller: AdminMollieSubscriptionOrders + _legacy_link: AdminMollieSubscriptionOrders + admin_subscription_search: path: admin-subscription methods: POST diff --git a/config/services.yml b/config/services.yml index c0da042b5..76634b961 100644 --- a/config/services.yml +++ b/config/services.yml @@ -41,3 +41,48 @@ services: Mollie\Subscription\Grid\Accessibility\SubscriptionCancelAccessibility: class: Mollie\Subscription\Grid\Accessibility\SubscriptionCancelAccessibility + + carrier_options_provider: + class: Mollie\Subscription\Form\ChoiceProvider\CarrierOptionsProvider + public: true + arguments: + - '@Mollie' + + subscription_options_configuration: + class: Mollie\Subscription\Form\Options\SubscriptionOptionsConfiguration + arguments: + - '@prestashop.adapter.legacy.configuration' + + subscription_options_data_provider: + class: Mollie\Subscription\Form\Options\SubscriptionOptionsDataProvider + arguments: + - '@subscription_options_configuration' + + subscription_options_type: + class: Mollie\Subscription\Form\Options\SubscriptionOptionsType + parent: 'form.type.translatable.aware' + public: true + arguments: + - '@carrier_options_provider' + - '@Mollie' + tags: + - { name: form.type } + + subscription_options_form_handler: + class: 'PrestaShop\PrestaShop\Core\Form\Handler' + arguments: + - '@form.factory' + - '@prestashop.core.hook.dispatcher' + - '@subscription_options_data_provider' + - 'Mollie\Subscription\Form\Options\SubscriptionOptionsType' + - 'SubscriptionOptions' + + # NOTE: works for PS < 1.7.8 + subscription_options_form_handler_deprecated: + class: 'PrestaShop\PrestaShop\Core\Form\FormHandler' + arguments: + - '@=service("form.factory").createBuilder()' + - '@prestashop.core.hook.dispatcher' + - '@subscription_options_data_provider' + - 'subscription_options': 'Mollie\Subscription\Form\Options\SubscriptionOptionsType' + - 'SubscriptionOptions' diff --git a/src/Builder/FormBuilder.php b/src/Builder/FormBuilder.php index 111c9f83f..eacef51b6 100644 --- a/src/Builder/FormBuilder.php +++ b/src/Builder/FormBuilder.php @@ -24,7 +24,6 @@ use Mollie\Api\Types\RefundStatus; use Mollie\Config\Config; use Mollie\Provider\CustomLogoProviderInterface; -use Mollie\Repository\CarrierRepositoryInterface; use Mollie\Repository\TaxRulesGroupRepositoryInterface; use Mollie\Service\ApiService; use Mollie\Service\ConfigFieldService; @@ -91,8 +90,6 @@ class FormBuilder /** @var Context */ private $context; - /** @var CarrierRepositoryInterface */ - private $carrierRepository; public function __construct( Mollie $module, @@ -106,8 +103,7 @@ public function __construct( CustomLogoProviderInterface $creditCardLogoProvider, ConfigurationAdapter $configuration, TaxRulesGroupRepositoryInterface $taxRulesGroupRepository, - Context $context, - CarrierRepositoryInterface $carrierRepository + Context $context ) { $this->module = $module; $this->apiService = $apiService; @@ -121,7 +117,6 @@ public function __construct( $this->configuration = $configuration; $this->taxRulesGroupRepository = $taxRulesGroupRepository; $this->context = $context; - $this->carrierRepository = $carrierRepository; } public function buildSettingsForm() @@ -532,8 +527,6 @@ protected function getAdvancedSettingsSection() ], ]; - $input = array_merge($input, $this->getShippingOptions($advancedSettings)); - $messageStatus = $this->module->l('Status for %s payments', self::FILE_NAME); $descriptionStatus = $this->module->l('`%s` payments get `%s` status', self::FILE_NAME); $messageMail = $this->module->l('Send email when %s', self::FILE_NAME); @@ -828,52 +821,4 @@ private function getSettingTabs($isApiKeyProvided) return $tabs; } - - private function getShippingOptions(string $tab): array - { - /** @var \Carrier[] $carriers */ - $carriers = $this->carrierRepository->findAllBy([ - 'active' => 1, - 'deleted' => 0, - ]); - - $mappedCarriers = []; - - $mappedCarriers[] = [ - 'id' => 0, - 'name' => $this->module->l('Not selected', self::FILE_NAME), - ]; - - foreach ($carriers as $carrier) { - $mappedCarriers[] = [ - 'id' => $carrier->id, - 'name' => $carrier->name, - ]; - } - - $header = [ - 'type' => 'mollie-h2', - 'name' => '', - 'tab' => $tab, - 'title' => $this->module->l('Subscriptions', self::FILE_NAME), - ]; - - $options = [ - 'type' => 'select', - 'label' => $this->module->l('Select shipping option to use in subscription orders', self::FILE_NAME), - 'desc' => $this->module->l('WARNING: do not change selection after getting first subscription order.', self::FILE_NAME), - 'tab' => $tab, - 'name' => Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID, - 'options' => [ - 'query' => $mappedCarriers, - 'id' => 'id', - 'name' => 'name', - ], - ]; - - return [ - $header, - $options, - ]; - } } diff --git a/subscription/Controller/Symfony/AbstractSymfonyController.php b/subscription/Controller/Symfony/AbstractSymfonyController.php index f7b35a56c..2ea50f1d3 100644 --- a/subscription/Controller/Symfony/AbstractSymfonyController.php +++ b/subscription/Controller/Symfony/AbstractSymfonyController.php @@ -4,7 +4,6 @@ use Module; use Mollie; -use Mollie\ServiceProvider\LeagueServiceContainerProvider; use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController; /** @@ -12,16 +11,13 @@ */ abstract class AbstractSymfonyController extends FrameworkBundleAdminController { - /** @var LeagueServiceContainerProvider */ - protected $leagueContainer; - /** @var Mollie */ protected $module; public function __construct() { parent::__construct(); - $this->leagueContainer = new LeagueServiceContainerProvider(); + /* @phpstan-ignore-next-line */ $this->module = Module::getInstanceByName('mollie'); } diff --git a/subscription/Controller/Symfony/SubscriptionController.php b/subscription/Controller/Symfony/SubscriptionController.php index ba13cca0d..29dea1fcb 100644 --- a/subscription/Controller/Symfony/SubscriptionController.php +++ b/subscription/Controller/Symfony/SubscriptionController.php @@ -10,6 +10,8 @@ use Mollie\Subscription\Filters\SubscriptionFilters; use Mollie\Subscription\Grid\SubscriptionGridDefinitionFactory; use Mollie\Subscription\Handler\SubscriptionCancellationHandler; +use Mollie\Utility\PsVersionUtility; +use PrestaShop\PrestaShop\Core\Form\FormHandlerInterface; use PrestaShop\PrestaShop\Core\Grid\GridFactoryInterface; use PrestaShopBundle\Security\Annotation\AdminSecurity; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -30,7 +32,7 @@ class SubscriptionController extends AbstractSymfonyController public function indexAction(SubscriptionFilters $filters, Request $request) { /** @var Shop $shop */ - $shop = $this->leagueContainer->getService(Shop::class); + $shop = $this->module->getService(Shop::class); if ($shop->getContext() !== \Shop::CONTEXT_SHOP) { if (!$this->get('session')->getFlashBag()->has('error')) { @@ -41,16 +43,61 @@ public function indexAction(SubscriptionFilters $filters, Request $request) } /** @var GridFactoryInterface $currencyGridFactory */ - $currencyGridFactory = $this->leagueContainer->getService('subscription_grid_factory'); + $currencyGridFactory = $this->module->getService('subscription_grid_factory'); $currencyGrid = $currencyGridFactory->getGrid($filters); + if (PsVersionUtility::isPsVersionGreaterOrEqualTo(_PS_VERSION_, '1.7.8.0')) { + $formHandler = $this->get('subscription_options_form_handler')->getForm(); + } else { + $formHandler = $this->get('subscription_options_form_handler_deprecated')->getForm(); + } + return $this->render('@Modules/mollie/views/templates/admin/Subscription/subscriptions-grid.html.twig', [ 'currencyGrid' => $this->presentGrid($currencyGrid), 'enableSidebar' => true, - 'help_link' => $this->generateSidebarLink($request->attributes->get('_legacy_controller')), + 'subscriptionOptionsForm' => $formHandler->createView(), ]); } + /** + * @AdminSecurity("is_granted('create', request.get('_legacy_controller'))") + * + * @param Request $request + * + * @return RedirectResponse + */ + public function submitOptionsAction(Request $request): RedirectResponse + { + if (PsVersionUtility::isPsVersionGreaterOrEqualTo(_PS_VERSION_, '1.7.8.0')) { + /** @var FormHandlerInterface $formHandler */ + $formHandler = $this->get('subscription_options_form_handler'); + } else { + /** @var FormHandlerInterface $formHandler */ + $formHandler = $this->get('subscription_options_form_handler_deprecated'); + } + + $form = $formHandler->getForm(); + $form->handleRequest($request); + + if (!$form->isSubmitted() || !$form->isValid()) { + $this->addFlash( + 'error', + $this->module->l('Failed to save options. Try again or contact support.', self::FILE_NAME) + ); + + return $this->redirectToRoute('admin_subscription_index'); + } + + $formHandler->save($form->getData()); + + $this->addFlash( + 'success', + $this->module->l('Options saved successfully.', self::FILE_NAME) + ); + + return $this->redirectToRoute('admin_subscription_index'); + } + /** * Provides filters functionality. * @@ -82,7 +129,7 @@ public function searchAction(Request $request): RedirectResponse public function cancelAction(int $subscriptionId): RedirectResponse { /** @var SubscriptionCancellationHandler $subscriptionCancellationHandler */ - $subscriptionCancellationHandler = $this->leagueContainer->getService(SubscriptionCancellationHandler::class); + $subscriptionCancellationHandler = $this->module->getService(SubscriptionCancellationHandler::class); try { $subscriptionCancellationHandler->handle($subscriptionId); diff --git a/subscription/Form/ChoiceProvider/CarrierOptionsProvider.php b/subscription/Form/ChoiceProvider/CarrierOptionsProvider.php new file mode 100644 index 000000000..17c45a00a --- /dev/null +++ b/subscription/Form/ChoiceProvider/CarrierOptionsProvider.php @@ -0,0 +1,35 @@ +carrierRepository = $module->getService(CarrierRepositoryInterface::class); + } + + public function getChoices(): array + { + /** @var \Carrier[] $carriers */ + $carriers = $this->carrierRepository->findAllBy([ + 'active' => 1, + 'deleted' => 0, + ]); + + $choices = []; + + foreach ($carriers as $carrier) { + $choices[$carrier->name] = (int) $carrier->id; + } + + return $choices; + } +} diff --git a/subscription/Form/Options/SubscriptionOptionsConfiguration.php b/subscription/Form/Options/SubscriptionOptionsConfiguration.php new file mode 100644 index 000000000..e3cb1ee5a --- /dev/null +++ b/subscription/Form/Options/SubscriptionOptionsConfiguration.php @@ -0,0 +1,60 @@ +configuration = $configuration; + } + + /** + * {@inheritdoc} + */ + public function getConfiguration(): array + { + return [ + 'carrier' => $this->configuration->getInt(Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID), + ]; + } + + /** + * {@inheritdoc} + */ + public function updateConfiguration(array $configuration): array + { + if (!$this->validateConfiguration($configuration)) { + return []; + } + + $this->configuration->set( + Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID, + $configuration['carrier'] + ); + + return []; + } + + /** + * {@inheritdoc} + */ + public function validateConfiguration(array $configuration): bool + { + return isset( + $configuration['carrier'] + ); + } +} diff --git a/subscription/Form/Options/SubscriptionOptionsDataProvider.php b/subscription/Form/Options/SubscriptionOptionsDataProvider.php new file mode 100644 index 000000000..2d534613c --- /dev/null +++ b/subscription/Form/Options/SubscriptionOptionsDataProvider.php @@ -0,0 +1,43 @@ +subscriptionOptionsConfiguration = $subscriptionOptionsConfiguration; + } + + /** + * {@inheritdoc} + */ + public function getData(): array + { + if (PsVersionUtility::isPsVersionGreaterOrEqualTo(_PS_VERSION_, '1.7.8.0')) { + return $this->subscriptionOptionsConfiguration->getConfiguration(); + } + + return ['subscription_options' => $this->subscriptionOptionsConfiguration->getConfiguration()]; + } + + /** + * {@inheritdoc} + */ + public function setData(array $data): array + { + if (PsVersionUtility::isPsVersionGreaterOrEqualTo(_PS_VERSION_, '1.7.8.0')) { + return $this->subscriptionOptionsConfiguration->updateConfiguration($data); + } + + return $this->subscriptionOptionsConfiguration->updateConfiguration($data['subscription_options']); + } +} diff --git a/subscription/Form/Options/SubscriptionOptionsType.php b/subscription/Form/Options/SubscriptionOptionsType.php new file mode 100644 index 000000000..22de3db2e --- /dev/null +++ b/subscription/Form/Options/SubscriptionOptionsType.php @@ -0,0 +1,41 @@ +carrierOptionProvider = $carrierOptionProvider; + $this->module = $module; + } + + public function buildForm(FormBuilderInterface $builder, array $options): void + { + $builder + ->add('carrier', ChoiceType::class, [ + 'required' => true, + 'choices' => $this->carrierOptionProvider->getChoices(), + // TODO migrate to modern translation system + 'placeholder' => $this->module->l('Choose your carrier'), + ]); + } +} diff --git a/subscription/Handler/RecurringOrderHandler.php b/subscription/Handler/RecurringOrderHandler.php index 96cea54d3..a35e2a265 100644 --- a/subscription/Handler/RecurringOrderHandler.php +++ b/subscription/Handler/RecurringOrderHandler.php @@ -85,6 +85,7 @@ public function __construct( ConfigurationAdapter $configuration, RecurringOrdersProductRepositoryInterface $recurringOrdersProductRepository, CarrierRepositoryInterface $carrierRepository, + // TODO use subscription logger after it's fixed PrestaLoggerInterface $logger, CreateSpecificPriceAction $createSpecificPriceAction, OrderRepositoryInterface $orderRepository diff --git a/subscription/Logger/Logger.php b/subscription/Logger/Logger.php index 11b6cc7fc..86f9a3df1 100644 --- a/subscription/Logger/Logger.php +++ b/subscription/Logger/Logger.php @@ -10,6 +10,8 @@ class Logger implements LoggerInterface const LOG_OBJECT_TYPE = 'mollie_sub_log'; + // TODO fix this logger + /** * @return null */ diff --git a/views/templates/admin/Subscription/subscriptions-grid.html.twig b/views/templates/admin/Subscription/subscriptions-grid.html.twig index 735467ba9..ee791a18e 100644 --- a/views/templates/admin/Subscription/subscriptions-grid.html.twig +++ b/views/templates/admin/Subscription/subscriptions-grid.html.twig @@ -1,4 +1,4 @@ -{#** +{# ** * 2007-2019 PrestaShop and Contributors * * NOTICE OF LICENSE @@ -29,6 +29,8 @@ {% block content %} + {{ include('@Modules/mollie/views/templates/admin/Subscription/subscriptions-settings.html.twig') }} +