Skip to content

Commit

Permalink
PIPRES-348: WIP move subscription options to subscription tab
Browse files Browse the repository at this point in the history
  • Loading branch information
mandan2 committed Oct 17, 2023
1 parent 3fa88fd commit 330642b
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 0 deletions.
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
46 changes: 46 additions & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,49 @@ services:

Mollie\Subscription\Grid\Accessibility\SubscriptionCancelAccessibility:
class: Mollie\Subscription\Grid\Accessibility\SubscriptionCancelAccessibility

carrier_options_provider:
class: Mollie\Subscription\Form\ChoiceProvider\CarrierOptionsProvider

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_carrier_option_choice_provider:
class: 'Mollie\Subscription\Form\ChoiceProvider\CarrierOptionProvider'
# TODO arguments for repositories

subscriptions_options_type:
class: 'Mollie\Subscription\Form\Options\SubscriptionOptionsType'
parent: 'form.type.translatable.aware'
public: true
arguments:
- '@carrier_options_provider'
- '@Mollie'
tags:
- { name: form.type }

prestashop.admin.order.invoices.options.form_handler:
class: 'PrestaShop\PrestaShop\Core\Form\Handler'
arguments:
- '@form.factory'
- '@prestashop.core.hook.dispatcher'
- '@prestashop.admin.order.invoices.options.data_provider'
- 'PrestaShopBundle\Form\Admin\Sell\Order\Invoices\InvoiceOptionsType'
- 'OrdersInvoicesOptions'

# TODO form_handler in <1.7.8
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'
- 'SubscriptionsOptions'
21 changes: 21 additions & 0 deletions subscription/Controller/Symfony/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,35 @@ public function indexAction(SubscriptionFilters $filters, Request $request)
/** @var GridFactoryInterface $currencyGridFactory */
$currencyGridFactory = $this->leagueContainer->getService('subscription_grid_factory');
$currencyGrid = $currencyGridFactory->getGrid($filters);
$optionsForm = $this->get('subscription_options_form_handler')->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' => $optionsForm->createView(),
]);
}

/**
* @AdminSecurity("is_granted('create', request.get('_legacy_controller'))")
*
* @param Request $request
*
* @return RedirectResponse
*/
public function submitOptionsAction(Request $request): RedirectResponse
{
// TODO implement data submit request

$this->addFlash(
'success',
$this->module->l('Options saved successfully', self::FILE_NAME)
);

return $this->redirectToRoute('admin_subscription_index');
}

/**
* Provides filters functionality.
*
Expand Down
17 changes: 17 additions & 0 deletions subscription/Form/ChoiceProvider/CarrierOptionsProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Mollie\Subscription\Form\ChoiceProvider;

use PrestaShop\PrestaShop\Core\Form\FormChoiceProviderInterface;

class CarrierOptionsProvider implements FormChoiceProviderInterface
{
// TODO implement

public function getChoices(): array
{
$choices[1] = 'test-carrier';

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(
$configuration['carrier']
);
}
}
34 changes: 34 additions & 0 deletions subscription/Form/Options/SubscriptionOptionsDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Mollie\Subscription\Form\Options;

use PrestaShop\PrestaShop\Core\Configuration\DataConfigurationInterface;
use PrestaShop\PrestaShop\Core\Form\FormDataProviderInterface;

final class SubscriptionOptionsDataProvider implements FormDataProviderInterface
{
/** @var DataConfigurationInterface */
private $subscriptionOptionsConfiguration;

public function __construct(
DataConfigurationInterface $subscriptionOptionsConfiguration
) {
$this->subscriptionOptionsConfiguration = $subscriptionOptionsConfiguration;
}

/**
* {@inheritdoc}
*/
public function getData(): array
{
return $this->subscriptionOptionsConfiguration->getConfiguration();
}

/**
* {@inheritdoc}
*/
public function setData(array $data): array
{
return $this->subscriptionOptionsConfiguration->updateConfiguration($data);
}
}
46 changes: 46 additions & 0 deletions subscription/Form/Options/SubscriptionOptionsType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Mollie\Subscription\Form\Options;

use Module;
use PrestaShop\PrestaShop\Core\Form\FormChoiceProviderInterface;
use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Translation\TranslatorInterface;

class SubscriptionOptionsType extends TranslatorAwareType
{
/** @var FormChoiceProviderInterface */
private $carrierOptionProvider;
/** @var Module */
private $module;

public function __construct(
TranslatorInterface $translator,
array $locales,
FormChoiceProviderInterface $carrierOptionProvider,
Module $module
) {
parent::__construct($translator, $locales);

$this->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 structure should look like below visible => writable to configuration value
// [
// 'Yes' => 'stock_yes',
// 'No' => 'stock_no',
// ],
// TODO migrate to modern translation system
'placeholder' => $this->module->l('Choose your carrier'),
]);
}
}
32 changes: 32 additions & 0 deletions views/templates/admin/Subscription/subscriptions-grid.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,38 @@

{% block content %}

<div class="row justify-content-center">
{# TODO migrate to another template#}
{% block subscriptions_options %}
<div class="col">
{{ form_start(subscriptionOptionsForm, {method: 'POST', action: path('admin_subscription_options_submit'), attr: {id: 'subscription-options'}}) }}
<div class="card">
<h3 class="card-header">
<i class="material-icons">settings</i> {{ 'Subscriptions options'|trans }}
</h3>
<div class="card-block row">
<div class="card-text">
<div class="form-group row">
{# TODO translations will be enabled only after we will migrate to moden translation system #}
{{ ps.label_with_help('Carrier to use in subscription orders'|trans, 'WARNING: do not change selection after getting first subscription order.'|trans) }}
<div class="col-sm">
{{ form_errors(subscriptionOptionsForm.carrier) }}
{{ form_widget(subscriptionOptionsForm.carrier) }}
</div>
</div>
{{ form_rest(subscriptionOptionsForm) }}
</div>
</div>
<div class="card-footer">
<div class="d-flex justify-content-end">
<button class="btn btn-primary" id="save-invoices-options-button">{{ 'Save'|trans({}, 'Admin.Actions') }}</button>
</div>
</div>
</div>
{{ form_end(subscriptionOptionsForm) }}
</div>
{% endblock %}

<div class="row">
<div class="col">
{{ include('@PrestaShop/Admin/Common/Grid/grid_panel.html.twig', {'grid': currencyGrid }) }}
Expand Down

0 comments on commit 330642b

Please sign in to comment.