Skip to content

Commit

Permalink
position update refactorization
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksydan committed Jan 5, 2024
1 parent 02870a9 commit f8251b8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 21 deletions.
7 changes: 0 additions & 7 deletions config/admin/grid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,3 @@ services:
arguments:
- '@oksydan.is_imageslider.grid.data.factory.image_slider_data_factory'
- '@oksydan.is_imageslider.provider.image_provider'

oksydan.is_imageslider.grid.position_definition:
class: PrestaShop\PrestaShop\Core\Grid\Position\PositionDefinition
arguments:
- 'image_slider'
- 'id_slide'
- 'position'
21 changes: 7 additions & 14 deletions src/Controller/ImagesliderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Oksydan\IsImageslider\Filter\ImageSliderFileters;
use Oksydan\IsImageslider\Handler\Slide\DeleteSlideHandler;
use Oksydan\IsImageslider\Handler\Slide\ToggleSlideActivityHandler;
use Oksydan\IsImageslider\Handler\Slide\UpdateSliderPositionHandler;
use Oksydan\IsImageslider\Repository\ImageSliderRepository;
use Oksydan\IsImageslider\Translations\TranslationDomains;
use PrestaShop\PrestaShop\Core\Form\FormHandlerInterface;
Expand Down Expand Up @@ -46,6 +47,8 @@ class ImagesliderController extends FrameworkBundleAdminController

private ToggleSlideActivityHandler $toggleSlideActivityHandler;

private UpdateSliderPositionHandler $updateSliderPositionHandler;

public function __construct(
TemplateCache $templateCache,
DeleteSlideHandler $deleteSlideHandler,
Expand All @@ -56,7 +59,8 @@ public function __construct(
IdentifiableObjectFormHandlerInterface $imagesliderFormHandler,
TranslatorInterface $translator,
ImageSliderRepository $imageSliderRepository,
ToggleSlideActivityHandler $toggleSlideActivityHandler
ToggleSlideActivityHandler $toggleSlideActivityHandler,
UpdateSliderPositionHandler $updateSliderPositionHandler
) {
parent::__construct();
$this->templateCache = $templateCache;
Expand All @@ -69,6 +73,7 @@ public function __construct(
$this->translator = $translator;
$this->imageSliderRepository = $imageSliderRepository;
$this->toggleSlideActivityHandler = $toggleSlideActivityHandler;
$this->updateSliderPositionHandler = $updateSliderPositionHandler;
}

public function index(ImageSliderFileters $filters): Response
Expand Down Expand Up @@ -246,19 +251,7 @@ public function toggleStatus(Request $request, int $slideId): Response
public function updatePositionAction(Request $request): Response
{
try {
$positionsData = [
'positions' => $request->request->get('positions'),
];

$positionDefinition = $this->get('oksydan.is_imageslider.grid.position_definition');

$positionUpdateFactory = $this->get('prestashop.core.grid.position.position_update_factory');
$positionUpdate = $positionUpdateFactory->buildPositionUpdate($positionsData, $positionDefinition);

$updater = $this->get('prestashop.core.grid.position.doctrine_grid_position_updater');

$updater->update($positionUpdate);
$this->clearTemplateCache();
$this->updateSliderPositionHandler->handle($request->request->get('positions'));

$this->addFlash('success', $this->trans('Successful update.', 'Admin.Notifications.Success'));
} catch (PositionDataException|PositionUpdateException $e) {
Expand Down
58 changes: 58 additions & 0 deletions src/Handler/Slide/UpdateSliderPositionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Oksydan\IsImageslider\Handler\Slide;

use Oksydan\IsImageslider\Cache\TemplateCache;
use PrestaShop\PrestaShop\Core\Grid\Position\Exception\PositionDataException;
use PrestaShop\PrestaShop\Core\Grid\Position\Exception\PositionUpdateException;
use PrestaShop\PrestaShop\Core\Grid\Position\GridPositionUpdater;
use PrestaShop\PrestaShop\Core\Grid\Position\PositionDefinition;
use PrestaShop\PrestaShop\Core\Grid\Position\PositionUpdateFactory;

class UpdateSliderPositionHandler
{
protected TemplateCache $templateCache;

protected GridPositionUpdater $gridPositionUpdater;

public function __construct(
TemplateCache $templateCache,
GridPositionUpdater $gridPositionUpdater
) {
$this->templateCache = $templateCache;
$this->gridPositionUpdater = $gridPositionUpdater;
}

/**
* @throws PositionUpdateException
* @throws PositionDataException
*/
public function handle(array $positions): void
{
$positionDefinition = new PositionDefinition(
'image_slider',
'id_slide',
'position'
);

$positionUpdateFactory = new PositionUpdateFactory(
'positions',
'rowId',
'oldPosition',
'newPosition',
'idParent'
);

$positionsData = [
'positions' => $positions,
];

$positionUpdate = $positionUpdateFactory->buildPositionUpdate($positionsData, $positionDefinition);

$this->gridPositionUpdater->update($positionUpdate);

$this->templateCache->clearTemplateCache();
}
}

0 comments on commit f8251b8

Please sign in to comment.