Skip to content

Commit

Permalink
Merge pull request PrestaShop#37083 from boherm/PrestaShop#37082-refa…
Browse files Browse the repository at this point in the history
…cto-improve-design-payment-shipping-prestashopadmincontroller
  • Loading branch information
boherm authored Oct 16, 2024
2 parents ff2d056 + 3fb75bd commit da18a50
Show file tree
Hide file tree
Showing 12 changed files with 483 additions and 412 deletions.
8 changes: 8 additions & 0 deletions phpstan-disallowed-calls.neon
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ parameters:
allowIn:
- src/Adapter/*
- src/PrestaShopBundle/Install/*
- src/PrestaShopBundle/Controller/Admin/Improve/Design/PositionsController.php
-
class: 'Symfony\Component\Translation\TranslatorInterface'
message: 'use Symfony\Contracts\Translation\TranslatorInterface instead'
Expand All @@ -293,6 +294,8 @@ parameters:
- src/PrestaShopBundle/Controller/Admin/Sell/Catalog/Product/ProductController.php
- src/PrestaShopBundle/Controller/Admin/Configure/ShopParameters/MetaController.php
- src/PrestaShopBundle/Controller/Admin/Configure/ShopParameters/OrderStateController.php
- src/PrestaShopBundle/Controller/Admin/Improve/Design/CmsPageController.php
- src/PrestaShopBundle/Controller/Admin/Improve/Design/MailThemeController.php
disallowedConstants:
-
constant:
Expand Down Expand Up @@ -423,3 +426,8 @@ parameters:
- src/*
- classes/*
- controllers/*
ignoreErrors:
-
message: "#^Namespace Module is forbidden, No legacy calls inside the prestashop bundle\\. Please create an interface and an adapter if you need to\\.$#"
count: 1
path: src/PrestaShopBundle/Controller/Admin/Improve/Design/PositionsController.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,24 @@
use PrestaShop\PrestaShop\Core\Domain\ImageSettings\Exception\ImageTypeNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\ImageSettings\Query\GetImageTypeForEditing;
use PrestaShop\PrestaShop\Core\Domain\ImageSettings\QueryResult\EditableImageType;
use PrestaShop\PrestaShop\Core\Form\FormHandlerInterface as ConfigurationFormHandlerInterface;
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\Builder\FormBuilderInterface;
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\Handler\FormHandlerInterface;
use PrestaShop\PrestaShop\Core\Grid\GridFactoryInterface;
use PrestaShop\PrestaShop\Core\Search\Filters\ImageTypeFilters;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Controller\Admin\PrestaShopAdminController;
use PrestaShopBundle\Form\Admin\Improve\Design\ImageSettings\DeleteImageTypeType;
use PrestaShopBundle\Form\Admin\Improve\Design\ImageSettings\RegenerateThumbnailsType;
use PrestaShopBundle\Security\Attribute\AdminSecurity;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Responsible for Image Settings actions in Back Office
*/
class ImageSettingsController extends FrameworkBundleAdminController
class ImageSettingsController extends PrestaShopAdminController
{
/**
* Displays image settings listing page.
Expand All @@ -59,18 +64,22 @@ class ImageSettingsController extends FrameworkBundleAdminController
* @return Response
*/
#[AdminSecurity("is_granted('read', request.get('_legacy_controller'))")]
public function indexAction(Request $request, ImageTypeFilters $filters): Response
{
public function indexAction(
Request $request,
ImageTypeFilters $filters,
#[Autowire(service: 'prestashop.core.grid.factory.image_type')]
GridFactoryInterface $imageTypeGridFactory,
#[Autowire(service: 'prestashop.admin.image_settings.form_handler')]
ConfigurationFormHandlerInterface $configFormHandler
): Response {
// Get image type grid
$imageTypeGridFactory = $this->get('prestashop.core.grid.factory.image_type');
$imageTypeGrid = $imageTypeGridFactory->getGrid($filters);

// Create form for deleting image type if needed (in modal)
$deleteForm = $this->createForm(DeleteImageTypeType::class);

// Create form to set some image settings
$configFormBuilder = $this->get('prestashop.admin.image_settings.form_handler');
$configForm = $configFormBuilder->getForm();
$configForm = $configFormHandler->getForm();

// Create form to regenerate thumbnails
$regenThumbnailsForm = $this->createForm(RegenerateThumbnailsType::class);
Expand All @@ -83,7 +92,7 @@ public function indexAction(Request $request, ImageTypeFilters $filters): Respon
'layoutHeaderToolbarBtn' => [
'add' => [
'href' => $this->generateUrl('admin_image_settings_create'),
'desc' => $this->trans('Add new image type', 'Admin.Design.Feature'),
'desc' => $this->trans('Add new image type', [], 'Admin.Design.Feature'),
'icon' => 'add_circle_outline',
],
],
Expand All @@ -93,18 +102,20 @@ public function indexAction(Request $request, ImageTypeFilters $filters): Respon
}

#[AdminSecurity("is_granted('update', request.get('_legacy_controller'))")]
public function saveSettingsAction(Request $request): Response
{
public function saveSettingsAction(
Request $request,
#[Autowire(service: 'prestashop.admin.image_settings.form_handler')]
ConfigurationFormHandlerInterface $configFormHandler
): Response {
try {
// Create form to set some image settings
$configFormHandler = $this->get('prestashop.admin.image_settings.form_handler');
$configForm = $configFormHandler->getForm();
$configForm->handleRequest($request);

if ($configForm->isSubmitted()) {
if ($configForm->isValid()) {
$configFormHandler->save($configForm->getData());
$this->addFlash('success', $this->trans('The settings have been successfully updated.', 'Admin.Notifications.Success'));
$this->addFlash('success', $this->trans('The settings have been successfully updated.', [], 'Admin.Notifications.Success'));
} else {
$this->addFlashFormErrors($configForm);
}
Expand All @@ -124,19 +135,21 @@ public function saveSettingsAction(Request $request): Response
* @return Response
*/
#[AdminSecurity("is_granted('create', request.get('_legacy_controller'))", redirectRoute: 'admin_image_settings_index')]
public function createAction(Request $request): Response
{
$formBuilder = $this->get('prestashop.core.form.identifiable_object.builder.image_type_form_builder');
$formHandler = $this->get('prestashop.core.form.identifiable_object.handler.image_type_form_handler');

public function createAction(
Request $request,
#[Autowire(service: 'prestashop.core.form.identifiable_object.builder.image_type_form_builder')]
FormBuilderInterface $formBuilder,
#[Autowire(service: 'prestashop.core.form.identifiable_object.handler.image_type_form_handler')]
FormHandlerInterface $formHandler,
): Response {
$form = $formBuilder->getForm();
$form->handleRequest($request);

try {
$handleResult = $formHandler->handle($form);

if ($handleResult->isSubmitted() && $handleResult->isValid()) {
$this->addFlash('success', $this->trans('Successful creation', 'Admin.Notifications.Success'));
$this->addFlash('success', $this->trans('Successful creation', [], 'Admin.Notifications.Success'));

return $this->redirectToRoute('admin_image_settings_index');
}
Expand All @@ -148,7 +161,7 @@ public function createAction(Request $request): Response
'form' => $form->createView(),
'help_link' => $this->generateSidebarLink($request->attributes->get('_legacy_controller')),
'enableSidebar' => true,
'layoutTitle' => $this->trans('Add new', 'Admin.Actions'),
'layoutTitle' => $this->trans('Add new', [], 'Admin.Actions'),
]);
}

Expand All @@ -161,22 +174,25 @@ public function createAction(Request $request): Response
* @return Response
*/
#[AdminSecurity("is_granted('update', request.get('_legacy_controller'))", redirectRoute: 'admin_image_settings_index')]
public function editAction(int $imageTypeId, Request $request): Response
{
public function editAction(
int $imageTypeId,
Request $request,
#[Autowire(service: 'prestashop.core.form.identifiable_object.builder.image_type_form_builder')]
FormBuilderInterface $formBuilder,
#[Autowire(service: 'prestashop.core.form.identifiable_object.handler.image_type_form_handler')]
FormHandlerInterface $formHandler,
): Response {
try {
/** @var EditableImageType $editableImageType */
$editableImageType = $this->getQueryBus()->handle(new GetImageTypeForEditing($imageTypeId));

$formBuilder = $this->get('prestashop.core.form.identifiable_object.builder.image_type_form_builder');
$formHandler = $this->get('prestashop.core.form.identifiable_object.handler.image_type_form_handler');
$editableImageType = $this->dispatchQuery(new GetImageTypeForEditing($imageTypeId));

$form = $formBuilder->getFormFor($imageTypeId);
$form->handleRequest($request);

$result = $formHandler->handleFor($imageTypeId, $form);

if ($result->isSubmitted() && $result->isValid()) {
$this->addFlash('success', $this->trans('Update successful', 'Admin.Notifications.Success'));
$this->addFlash('success', $this->trans('Update successful', [], 'Admin.Notifications.Success'));

return $this->redirectToRoute('admin_image_settings_index');
}
Expand All @@ -192,7 +208,7 @@ public function editAction(int $imageTypeId, Request $request): Response
'form' => $form->createView(),
'help_link' => $this->generateSidebarLink($request->attributes->get('_legacy_controller')),
'enableSidebar' => true,
'layoutTitle' => $this->trans('Edit: %value%', 'Admin.Actions', ['%value%' => $editableImageType->getName()]),
'layoutTitle' => $this->trans('Edit: %value%', ['%value%' => $editableImageType->getName()], 'Admin.Actions'),
]);
}

Expand All @@ -212,12 +228,12 @@ public function deleteAction(Request $request, int $imageTypeId): RedirectRespon

// If we need to delete images files too
if ($deleteForm->get('delete_images_files_too')->getNormData()) {
$this->getCommandBus()->handle(new DeleteImagesFromTypeCommand($imageTypeId));
$this->dispatchCommand(new DeleteImagesFromTypeCommand($imageTypeId));
}

// Delete image type
$this->getCommandBus()->handle(new DeleteImageTypeCommand($imageTypeId));
$this->addFlash('success', $this->trans('Successful deletion', 'Admin.Notifications.Success'));
$this->dispatchCommand(new DeleteImageTypeCommand($imageTypeId));
$this->addFlash('success', $this->trans('Successful deletion', [], 'Admin.Notifications.Success'));
} catch (Exception $e) {
$this->addFlash('error', $e->getMessage());
}
Expand All @@ -238,11 +254,11 @@ public function bulkDeleteAction(Request $request): RedirectResponse
$ids = $this->getBulkIdsFromRequest($request);

try {
$this->getCommandBus()->handle(new BulkDeleteImageTypeCommand($ids));
$this->dispatchCommand(new BulkDeleteImageTypeCommand($ids));

$this->addFlash(
'success',
$this->trans('The selection has been successfully deleted.', 'Admin.Notifications.Success')
$this->trans('The selection has been successfully deleted.', [], 'Admin.Notifications.Success')
);
} catch (Exception $e) {
$this->addFlash('error', $e->getMessage());
Expand Down Expand Up @@ -279,12 +295,12 @@ public function regenerateThumbnailsAction(Request $request): RedirectResponse
$regenThumbnailsForm = $this->createForm(RegenerateThumbnailsType::class);
$regenThumbnailsForm->handleRequest($request);

$this->getCommandBus()->handle(new RegenerateThumbnailsCommand(
$this->dispatchCommand(new RegenerateThumbnailsCommand(
$regenThumbnailsForm->get('image')->getData(),
$regenThumbnailsForm->get('image-type')->getData(),
$regenThumbnailsForm->get('erase-previous-images')->getData()
));
$this->addFlash('success', $this->trans('The thumbnails were successfully regenerated.', 'Admin.Notifications.Success'));
$this->addFlash('success', $this->trans('The thumbnails were successfully regenerated.', [], 'Admin.Notifications.Success'));
} catch (Exception $e) {
$this->addFlash('error', $e->getMessage());
}
Expand Down
Loading

0 comments on commit da18a50

Please sign in to comment.