Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIPRES-348: Move subscription options to subscription tab #832

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config/routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
57 changes: 1 addition & 56 deletions src/Builder/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -91,8 +90,6 @@ class FormBuilder

/** @var Context */
private $context;
/** @var CarrierRepositoryInterface */
private $carrierRepository;

public function __construct(
Mollie $module,
Expand All @@ -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;
Expand All @@ -121,7 +117,6 @@ public function __construct(
$this->configuration = $configuration;
$this->taxRulesGroupRepository = $taxRulesGroupRepository;
$this->context = $context;
$this->carrierRepository = $carrierRepository;
}

public function buildSettingsForm()
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@

use Module;
use Mollie;
use Mollie\ServiceProvider\LeagueServiceContainerProvider;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;

/**
* Class AbstractAdminController - an abstraction for all admin module controllers
*/
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');
}
Expand Down
55 changes: 51 additions & 4 deletions subscription/Controller/Symfony/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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')) {
Expand All @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down
35 changes: 35 additions & 0 deletions subscription/Form/ChoiceProvider/CarrierOptionsProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Mollie\Subscription\Form\ChoiceProvider;

use Mollie\Repository\CarrierRepositoryInterface;
use PrestaShop\PrestaShop\Core\Form\FormChoiceProviderInterface;

class CarrierOptionsProvider implements FormChoiceProviderInterface
{
/** @var CarrierRepositoryInterface */
private $carrierRepository;

public function __construct(
\Mollie $module
) {
$this->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;
}
}
60 changes: 60 additions & 0 deletions subscription/Form/Options/SubscriptionOptionsConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Mollie\Subscription\Form\Options;

use Mollie\Config\Config;
use PrestaShop\PrestaShop\Adapter\Configuration;
use PrestaShop\PrestaShop\Core\Configuration\DataConfigurationInterface;

final class SubscriptionOptionsConfiguration implements DataConfigurationInterface
{
/**
* @var Configuration
*/
private $configuration;

/**
* @param Configuration $configuration
*/
public function __construct(Configuration $configuration)
{
$this->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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless function tbh.

$configuration['carrier']
);
}
}
Loading
Loading