Skip to content

Commit

Permalink
[CatalogPromotions][UI] Removing catalog promotion
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafikooo committed Sep 15, 2022
1 parent 15f3696 commit c95d73a
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\AdminBundle\Controller;

use Sylius\Bundle\CoreBundle\CatalogPromotion\Processor\CatalogPromotionRemovalProcessorInterface;
use Sylius\Component\Promotion\Exception\CatalogPromotionNotFoundException;
use Sylius\Component\Promotion\Exception\InvalidCatalogPromotionStateException;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

final class RemoveCatalogPromotionAction
{
public function __construct(private CatalogPromotionRemovalProcessorInterface $catalogPromotionRemovalProcessor)
{
}

public function __invoke(Request $request): Response
{
try {
$this->catalogPromotionRemovalProcessor->removeCatalogPromotion($request->attributes->get('code'));

/** @var Session $session */
$session = $request->getSession();
$session->getFlashBag()->add('success', 'sylius.catalog_promotion.remove');

return new RedirectResponse($request->headers->get('referer'));
} catch (CatalogPromotionNotFoundException) {
throw new NotFoundHttpException('The catalog promotion has not been found');
} catch (InvalidCatalogPromotionStateException $exception) {
throw new BadRequestException($exception->getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
sylius_grid:
templates:
action:
delete_catalog_promotion: "@SyliusAdmin/Grid/deleteCatalogPromotion.html.twig"
grids:
sylius_admin_catalog_promotion:
driver:
Expand Down Expand Up @@ -90,3 +93,11 @@ sylius_grid:
type: show
update:
type: update
delete:
type: delete_catalog_promotion
options:
link:
route: sylius_admin_catalog_promotion_delete
parameters:
code: resource.code
state: resource.state
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,19 @@ sylius_admin_catalog_promotion:

sylius_admin_catalog_promotion_show:
path: /catalog-promotions/{id}
methods: [GET]
defaults:
_controller: sylius.controller.catalog_promotion:showAction
_sylius:
section: admin
template: "@SyliusAdmin/CatalogPromotion/show.html.twig"
permission: true

sylius_admin_catalog_promotion_delete:
path: /catalog-promotions/{code}
methods: [DELETE]
defaults:
_controller: Sylius\Bundle\AdminBundle\Controller\RemoveCatalogPromotionAction
_sylius:
section: admin
permission: true
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@
<argument type="string">%kernel.environment%</argument>
</service>

<service id="Sylius\Bundle\AdminBundle\Controller\RemoveCatalogPromotionAction" public="true">
<argument type="service" id="Sylius\Bundle\CoreBundle\CatalogPromotion\Processor\CatalogPromotionRemovalProcessorInterface" />
</service>

<service
id="Sylius\Bundle\AdminBundle\Controller\RedirectHandler"
decorates="sylius.resource_controller.redirect_handler"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% set path = options.link.url|default(path(options.link.route, options.link.parameters)) %}

<form action="{{ path }}" method="post">
<button
{% if options.state == 'processing' %} disabled {% endif %}
class="ui labeled {% if options.state != 'processing' %}red {% endif %}icon button"
type="submit"
data-requires-confirmation {{ sylius_test_html_attribute('delete-button') }}
>
<i class="icon {% if action.icon is not empty %} {{ action.icon }} {% else %}trash{% endif %}"></i>
{% if action.label is not empty %} {{ action.label|trans }} {% else %} {{ 'sylius.ui.delete'|trans }} {% endif %}
</button>
<input type="hidden" name="_csrf_token" value="{{ csrf_token(options.link.parameters.code) }}" />
<input type="hidden" name="_method" value="DELETE"/>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
sylius:
promotion_coupon:
generate: 'Promotion coupons have been successfully generated.'
catalog_promotion:
remove: 'Removing of catalog promotion has been requested. This process can take a while depending on the number of affected products.'

0 comments on commit c95d73a

Please sign in to comment.