From dfaf4af21f7567342fc8a18fea541eb5c6349d03 Mon Sep 17 00:00:00 2001 From: mamazu <14860264+mamazu@users.noreply.github.com> Date: Sun, 8 Oct 2023 19:13:00 +0200 Subject: [PATCH 1/7] Applying the shop grids Paritally copying over some of the grids from #14449 --- .../ShopBundle/Grid/Account/OrderGrid.php | 106 ++++++++++++++++++ .../Bundle/ShopBundle/Grid/ProductGrid.php | 103 +++++++++++++++++ .../Resources/config/app/config.yml | 2 - .../Resources/config/services/grid.xml | 33 ++++++ 4 files changed, 242 insertions(+), 2 deletions(-) create mode 100644 src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php create mode 100644 src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php create mode 100644 src/Sylius/Bundle/ShopBundle/Resources/config/services/grid.xml diff --git a/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php b/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php new file mode 100644 index 00000000000..5f7f8fd06fc --- /dev/null +++ b/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php @@ -0,0 +1,106 @@ +setRepositoryMethod('createByCustomerAndChannelIdQueryBuilder', [ + $this->customerContext->getCustomer()?->getId(), + $this->channelContext->getChannel()->getId(), + ]) + ->orderBy('checkoutCompletedAt', 'desc') + ->addField( + TwigField::create('number', '@SyliusShop/Account/Order/Grid/Field/number.html.twig') + ->setLabel('sylius.ui.number') + ->setSortable(true) + ) + ->addField( + DateTimeField::create('checkoutCompletedAt', 'm/d/Y') + ->setLabel('sylius.ui.date') + ->setSortable(true) + ) + ->addField( + TwigField::create('shippingAddress', '@SyliusShop/Account/Order/Grid/Field/address.html.twig') + ->setLabel('sylius.ui.ship_to') + ) + ->addField( + TwigField::create('total', '@SyliusShop/Account/Order/Grid/Field/total.html.twig') + ->setLabel('sylius.ui.total') + ->setPath('.') + ->setSortable(true, 'total') + ) + ->addField( + TwigField::create('state', '@SyliusUi/Grid/Field/label.html.twig') + ->setLabel('sylius.ui.state') + ->setSortable(true) + ->addOptions([ + 'vars' => [ + 'labels' => '@SyliusShop/Account/Order/Label/State', + ], + ]) + ) + ->addActionGroup( + ItemActionGroup::create( + ShowAction::create([ + 'link' => [ + 'route' => 'sylius_shop_account_order_show', + 'parameters' => [ + 'number' => 'resource.number', + ], + ], + ]), + Action::create('pay', 'pay') + ->setOptions([ + 'link' => [ + 'route' => 'sylius_shop_order_show', + 'parameters' => [ + 'tokenValue' => 'resource.tokenvalue', + ], + ], + ]) + ), + ); + } + + public function getResourceClass(): string + { + return $this->resourceClass; + } +} diff --git a/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php b/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php new file mode 100644 index 00000000000..b8da93983f0 --- /dev/null +++ b/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php @@ -0,0 +1,103 @@ + $taxonRepository + */ + public function __construct( + private string $resourceClass, + private ChannelContextInterface $channelContext, + private TaxonRepositoryInterface $taxonRepository, + private LocaleContextInterface $localeContext, + RequestStack $requestStatck, + private bool $includeAllDescendants, + ) { + $this->resquest = $requestStatck->getMainRequest(); + } + + public static function getName(): string + { + return 'sylius_shop_product'; + } + + public function buildGrid(GridBuilderInterface $gridBuilder): void + { + Assert::notNull($this->resquest); + $localeCode = $this->localeContext->getLocaleCode(); + + $gridBuilder + ->setRepositoryMethod('createShopListQueryBuilder', [ + $this->channelContext->getChannel(), + $this->taxonRepository->findOneBySlug( + $this->resquest->attributes->get('slug'), + $localeCode, + ), + $localeCode, + $this->resquest->query->get('sorting') ?? [], + $this->includeAllDescendants, + ]) + ->orderBy('position', 'asc') + ->setLimits([ + 9, + 18, + 27, + ]) + ->addField( + DateTimeField::create('createdAt') + ->setSortable(true), + ) + ->addField( + StringField::create('position') + ->setSortable(true, 'productTaxon.position'), + ) + ->addField( + StringField::create('name') + ->setSortable(true, 'translation.name'), + ) + ->addField( + Field::create('price', 'int') + ->setSortable(true, 'channelPricing.price'), + ) + ->addFilter( + StringFilter::create('search', ['translation.name']) + ->setLabel(false) + ->addFormOption('type', 'contains') + ); + } + + public function getResourceClass(): string + { + return $this->resourceClass; + } +} diff --git a/src/Sylius/Bundle/ShopBundle/Resources/config/app/config.yml b/src/Sylius/Bundle/ShopBundle/Resources/config/app/config.yml index 1e1c7aeb836..4f6e9891c53 100644 --- a/src/Sylius/Bundle/ShopBundle/Resources/config/app/config.yml +++ b/src/Sylius/Bundle/ShopBundle/Resources/config/app/config.yml @@ -4,8 +4,6 @@ imports: - { resource: 'twig_hooks/**/*.yaml' } - { resource: "@SyliusShopBundle/Resources/config/app/sylius/sylius_mailer.yml" } - - { resource: "@SyliusShopBundle/Resources/config/grids/account/order.yml" } - - { resource: "@SyliusShopBundle/Resources/config/grids/product.yml" } parameters: sylius.security.shop_regex: "^/(?!%sylius_admin.path_name%|api/.*|api$|media/.*)[^/]++" diff --git a/src/Sylius/Bundle/ShopBundle/Resources/config/services/grid.xml b/src/Sylius/Bundle/ShopBundle/Resources/config/services/grid.xml new file mode 100644 index 00000000000..6f9f2a5febf --- /dev/null +++ b/src/Sylius/Bundle/ShopBundle/Resources/config/services/grid.xml @@ -0,0 +1,33 @@ + + + + + + + + %sylius.model.order.class% + + + + + + + %sylius.model.product.class% + + + + + %sylius_shop.product_grid.include_all_descendants% + + + + From 97d8268818ab60cf94cca7280f47ec2e913e2c9e Mon Sep 17 00:00:00 2001 From: mamazu <14860264+mamazu@users.noreply.github.com> Date: Mon, 16 Oct 2023 10:52:04 +0200 Subject: [PATCH 2/7] Fixing the errors? --- .../ShopBundle/Grid/Account/OrderGrid.php | 2 +- .../Bundle/ShopBundle/Grid/ProductGrid.php | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php b/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php index 5f7f8fd06fc..d584711d5e9 100644 --- a/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php +++ b/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php @@ -86,7 +86,7 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void ], ], ]), - Action::create('pay', 'pay') + Action::create('pay', 'shop_pay') ->setOptions([ 'link' => [ 'route' => 'sylius_shop_order_show', diff --git a/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php b/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php index b8da93983f0..abca65b2377 100644 --- a/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php +++ b/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php @@ -26,6 +26,7 @@ use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Webmozart\Assert\Assert; final class ProductGrid extends AbstractGrid implements ResourceAwareGridInterface @@ -56,15 +57,21 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void Assert::notNull($this->resquest); $localeCode = $this->localeContext->getLocaleCode(); + // @see Sylius\Bundle\ResourceBundle\ExpressionLanguage\NotNullExpressionFunctionProvider + $taxon = $this->taxonRepository->findOneBySlug( + $this->resquest->attributes->get('slug'), + $localeCode, + ); + if ($taxon === null) { + throw new NotFoundHttpException('Requested page is invalid'); + } + $gridBuilder ->setRepositoryMethod('createShopListQueryBuilder', [ $this->channelContext->getChannel(), - $this->taxonRepository->findOneBySlug( - $this->resquest->attributes->get('slug'), - $localeCode, - ), + $taxon, $localeCode, - $this->resquest->query->get('sorting') ?? [], + $this->resquest->query->all('sorting'), $this->includeAllDescendants, ]) ->orderBy('position', 'asc') From 2d73ce121d17284330237ab3933adac5f8bacb80 Mon Sep 17 00:00:00 2001 From: mamazu <14860264+mamazu@users.noreply.github.com> Date: Tue, 17 Oct 2023 10:22:44 +0200 Subject: [PATCH 3/7] Adding dirty fix for test --- .../views/Account/Order/Grid/Action/pay.html.twig | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/Sylius/Bundle/ShopBundle/Resources/views/Account/Order/Grid/Action/pay.html.twig diff --git a/src/Sylius/Bundle/ShopBundle/Resources/views/Account/Order/Grid/Action/pay.html.twig b/src/Sylius/Bundle/ShopBundle/Resources/views/Account/Order/Grid/Action/pay.html.twig new file mode 100644 index 00000000000..7accb631d18 --- /dev/null +++ b/src/Sylius/Bundle/ShopBundle/Resources/views/Account/Order/Grid/Action/pay.html.twig @@ -0,0 +1,7 @@ +{% import '@SyliusUi/Macro/buttons.html.twig' as buttons %} + +{% set path = options.link.url|default(path( + options.link.route|default(grid.requestConfiguration.getRouteName('show')), + options.link.parameters|default({'id': data.id}) +)) %} +{{ buttons.default(path, action.label, null, action.options.icon|default('credit card alternative'), 'blue') }} From 5a5240b017e07dcb50e1f23f88a5f42f649968a3 Mon Sep 17 00:00:00 2001 From: mamazu <14860264+mamazu@users.noreply.github.com> Date: Tue, 17 Oct 2023 18:10:50 +0200 Subject: [PATCH 4/7] Fixing errors and adding label back --- .../Bundle/ShopBundle/Grid/Account/OrderGrid.php | 1 + .../Bundle/ShopBundle/Grid/ProductGrid.php | 16 ++++++---------- .../Account/Order/Grid/Action/pay.html.twig | 12 +++++++----- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php b/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php index d584711d5e9..98c46f10281 100644 --- a/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php +++ b/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php @@ -87,6 +87,7 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void ], ]), Action::create('pay', 'shop_pay') + ->setLabel('sylius.ui.pay') ->setOptions([ 'link' => [ 'route' => 'sylius_shop_order_show', diff --git a/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php b/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php index abca65b2377..4fbd540f6f3 100644 --- a/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php +++ b/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php @@ -31,8 +31,6 @@ final class ProductGrid extends AbstractGrid implements ResourceAwareGridInterface { - private ?Request $resquest; - /** * @param TaxonRepositoryInterface $taxonRepository */ @@ -41,10 +39,9 @@ public function __construct( private ChannelContextInterface $channelContext, private TaxonRepositoryInterface $taxonRepository, private LocaleContextInterface $localeContext, - RequestStack $requestStatck, + private RequestStack $requestStack, private bool $includeAllDescendants, ) { - $this->resquest = $requestStatck->getMainRequest(); } public static function getName(): string @@ -54,14 +51,13 @@ public static function getName(): string public function buildGrid(GridBuilderInterface $gridBuilder): void { - Assert::notNull($this->resquest); + $request = $this->requestStack->getMainRequest(); + Assert::notNull($request, 'The '.self::class. ' does not work in CLI mode'); + $localeCode = $this->localeContext->getLocaleCode(); // @see Sylius\Bundle\ResourceBundle\ExpressionLanguage\NotNullExpressionFunctionProvider - $taxon = $this->taxonRepository->findOneBySlug( - $this->resquest->attributes->get('slug'), - $localeCode, - ); + $taxon = $this->taxonRepository->findOneBySlug($request->attributes->get('slug'), $localeCode); if ($taxon === null) { throw new NotFoundHttpException('Requested page is invalid'); } @@ -71,7 +67,7 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void $this->channelContext->getChannel(), $taxon, $localeCode, - $this->resquest->query->all('sorting'), + $request->query->all('sorting'), $this->includeAllDescendants, ]) ->orderBy('position', 'asc') diff --git a/src/Sylius/Bundle/ShopBundle/Resources/views/Account/Order/Grid/Action/pay.html.twig b/src/Sylius/Bundle/ShopBundle/Resources/views/Account/Order/Grid/Action/pay.html.twig index 7accb631d18..c9770b3613d 100644 --- a/src/Sylius/Bundle/ShopBundle/Resources/views/Account/Order/Grid/Action/pay.html.twig +++ b/src/Sylius/Bundle/ShopBundle/Resources/views/Account/Order/Grid/Action/pay.html.twig @@ -1,7 +1,9 @@ {% import '@SyliusUi/Macro/buttons.html.twig' as buttons %} -{% set path = options.link.url|default(path( - options.link.route|default(grid.requestConfiguration.getRouteName('show')), - options.link.parameters|default({'id': data.id}) -)) %} -{{ buttons.default(path, action.label, null, action.options.icon|default('credit card alternative'), 'blue') }} +{% if data.paymentState in ['awaiting_payment'] %} + {% set path = options.link.url|default(path( + options.link.route|default(grid.requestConfiguration.getRouteName('show')), + options.link.parameters|default({'id': data.id}) + )) %} + {{ buttons.default(path, action.label, null, action.options.icon|default('credit card alternative'), 'blue') }} +{% endif %} From e1258a69a68790fa12b89d4c12aa6c652ba54365 Mon Sep 17 00:00:00 2001 From: mamazu <14860264+mamazu@users.noreply.github.com> Date: Wed, 18 Oct 2023 10:38:36 +0200 Subject: [PATCH 5/7] Apply suggestions from code review Co-authored-by: Dmitri Perunov --- src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php | 4 ++-- src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php | 6 +++--- .../Bundle/ShopBundle/Resources/config/services/grid.xml | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php b/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php index 98c46f10281..50dc20b4365 100644 --- a/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php +++ b/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php @@ -3,7 +3,7 @@ /* * This file is part of the Sylius package. * - * (c) Paweł Jędrzejewski + * (c) Sylius Sp. z o.o. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -95,7 +95,7 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void 'tokenValue' => 'resource.tokenvalue', ], ], - ]) + ]), ), ); } diff --git a/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php b/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php index 4fbd540f6f3..5540e3630b7 100644 --- a/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php +++ b/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php @@ -3,7 +3,7 @@ /* * This file is part of the Sylius package. * - * (c) Paweł Jędrzejewski + * (c) Sylius Sp. z o.o. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -40,7 +40,7 @@ public function __construct( private TaxonRepositoryInterface $taxonRepository, private LocaleContextInterface $localeContext, private RequestStack $requestStack, - private bool $includeAllDescendants, + private bool $includeAllDescendants, ) { } @@ -52,7 +52,7 @@ public static function getName(): string public function buildGrid(GridBuilderInterface $gridBuilder): void { $request = $this->requestStack->getMainRequest(); - Assert::notNull($request, 'The '.self::class. ' does not work in CLI mode'); + Assert::notNull($request, 'No main request available.'); $localeCode = $this->localeContext->getLocaleCode(); diff --git a/src/Sylius/Bundle/ShopBundle/Resources/config/services/grid.xml b/src/Sylius/Bundle/ShopBundle/Resources/config/services/grid.xml index 6f9f2a5febf..f7ea7acf88e 100644 --- a/src/Sylius/Bundle/ShopBundle/Resources/config/services/grid.xml +++ b/src/Sylius/Bundle/ShopBundle/Resources/config/services/grid.xml @@ -4,7 +4,7 @@ This file is part of the Sylius package. - (c) Paweł Jędrzejewski + (c) Sylius Sp. z o.o. For the full copyright and license information, please view the LICENSE file that was distributed with this source code. From cb9a90cb87bc60924df284e05247ea5fd5093d29 Mon Sep 17 00:00:00 2001 From: mamazu <14860264+mamazu@users.noreply.github.com> Date: Wed, 18 Oct 2023 11:38:35 +0200 Subject: [PATCH 6/7] Using the right actions --- src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php | 4 +++- src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php | 8 ++++---- .../Bundle/ShopBundle/Resources/config/services/grid.xml | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php b/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php index 50dc20b4365..5b59e6d0e90 100644 --- a/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php +++ b/src/Sylius/Bundle/ShopBundle/Grid/Account/OrderGrid.php @@ -78,7 +78,9 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void ) ->addActionGroup( ItemActionGroup::create( - ShowAction::create([ + Action::create('show', 'shop_show') + ->setLabel('sylius.ui.show') + ->setOptions([ 'link' => [ 'route' => 'sylius_shop_account_order_show', 'parameters' => [ diff --git a/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php b/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php index 5540e3630b7..fceb943481e 100644 --- a/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php +++ b/src/Sylius/Bundle/ShopBundle/Grid/ProductGrid.php @@ -16,7 +16,7 @@ use Sylius\Bundle\GridBundle\Builder\Field\DateTimeField; use Sylius\Bundle\GridBundle\Builder\Field\Field; use Sylius\Bundle\GridBundle\Builder\Field\StringField; -use Sylius\Bundle\GridBundle\Builder\Filter\StringFilter; +use Sylius\Bundle\GridBundle\Builder\Filter\Filter; use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface; use Sylius\Bundle\GridBundle\Grid\AbstractGrid; use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface; @@ -24,7 +24,6 @@ use Sylius\Component\Core\Model\TaxonInterface; use Sylius\Component\Locale\Context\LocaleContextInterface; use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Webmozart\Assert\Assert; @@ -37,8 +36,8 @@ final class ProductGrid extends AbstractGrid implements ResourceAwareGridInterfa public function __construct( private string $resourceClass, private ChannelContextInterface $channelContext, - private TaxonRepositoryInterface $taxonRepository, private LocaleContextInterface $localeContext, + private TaxonRepositoryInterface $taxonRepository, private RequestStack $requestStack, private bool $includeAllDescendants, ) { @@ -93,8 +92,9 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void ->setSortable(true, 'channelPricing.price'), ) ->addFilter( - StringFilter::create('search', ['translation.name']) + Filter::create('search', 'shop_string') ->setLabel(false) + ->addOption('fields', ['translation.name']) ->addFormOption('type', 'contains') ); } diff --git a/src/Sylius/Bundle/ShopBundle/Resources/config/services/grid.xml b/src/Sylius/Bundle/ShopBundle/Resources/config/services/grid.xml index f7ea7acf88e..38cac1d9124 100644 --- a/src/Sylius/Bundle/ShopBundle/Resources/config/services/grid.xml +++ b/src/Sylius/Bundle/ShopBundle/Resources/config/services/grid.xml @@ -23,8 +23,8 @@ %sylius.model.product.class% - + %sylius_shop.product_grid.include_all_descendants% From c433a7d1da0947e78116800a0521dbe63305c68e Mon Sep 17 00:00:00 2001 From: mamazu <14860264+mamazu@users.noreply.github.com> Date: Fri, 27 Sep 2024 14:25:20 +0200 Subject: [PATCH 7/7] Removing custom template --- .../views/Account/Order/Grid/Action/pay.html.twig | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 src/Sylius/Bundle/ShopBundle/Resources/views/Account/Order/Grid/Action/pay.html.twig diff --git a/src/Sylius/Bundle/ShopBundle/Resources/views/Account/Order/Grid/Action/pay.html.twig b/src/Sylius/Bundle/ShopBundle/Resources/views/Account/Order/Grid/Action/pay.html.twig deleted file mode 100644 index c9770b3613d..00000000000 --- a/src/Sylius/Bundle/ShopBundle/Resources/views/Account/Order/Grid/Action/pay.html.twig +++ /dev/null @@ -1,9 +0,0 @@ -{% import '@SyliusUi/Macro/buttons.html.twig' as buttons %} - -{% if data.paymentState in ['awaiting_payment'] %} - {% set path = options.link.url|default(path( - options.link.route|default(grid.requestConfiguration.getRouteName('show')), - options.link.parameters|default({'id': data.id}) - )) %} - {{ buttons.default(path, action.label, null, action.options.icon|default('credit card alternative'), 'blue') }} -{% endif %}