From 56826f42dd7b31615f1d6a3600784b36663d1b40 Mon Sep 17 00:00:00 2001 From: Martin Grossmann Date: Mon, 18 Sep 2023 08:06:28 +0200 Subject: [PATCH] [shopsys] added quick search in promo codes (#2786) --- .../Controller/Admin/PromoCodeController.php | 14 +++++++++--- .../PromoCode/Grid/PromoCodeGridFactory.php | 15 ++++++++++--- .../Admin/Content/PromoCode/list.html.twig | 22 +++++++++++++++++++ app/translations/messages.cs.po | 3 +++ app/translations/messages.en.po | 3 +++ 5 files changed, 51 insertions(+), 6 deletions(-) diff --git a/app/src/Controller/Admin/PromoCodeController.php b/app/src/Controller/Admin/PromoCodeController.php index 6f07d8f85c..2ae0fabe24 100644 --- a/app/src/Controller/Admin/PromoCodeController.php +++ b/app/src/Controller/Admin/PromoCodeController.php @@ -8,6 +8,8 @@ use League\Csv\Writer; use Shopsys\FrameworkBundle\Controller\Admin\PromoCodeController as BasePromoCodeController; use Shopsys\FrameworkBundle\Form\Admin\PromoCode\PromoCodeFormType; +use Shopsys\FrameworkBundle\Form\Admin\QuickSearch\QuickSearchFormData; +use Shopsys\FrameworkBundle\Form\Admin\QuickSearch\QuickSearchFormType; use Shopsys\FrameworkBundle\Model\Administrator\AdministratorGridFacade; use Shopsys\FrameworkBundle\Model\AdminNavigation\BreadcrumbOverrider; use Shopsys\FrameworkBundle\Model\Order\PromoCode\Grid\PromoCodeGridFactory; @@ -38,26 +40,32 @@ public function __construct( PromoCodeDataFactoryInterface $promoCodeDataFactory, PromoCodeGridFactory $promoCodeGridFactory, BreadcrumbOverrider $breadcrumbOverrider, - private PromoCodeMassGeneratedBatchGridFactory $promoCodeMassGeneratedBatchGridFactory, + private readonly PromoCodeMassGeneratedBatchGridFactory $promoCodeMassGeneratedBatchGridFactory, ) { parent::__construct($promoCodeFacade, $administratorGridFacade, $promoCodeDataFactory, $promoCodeGridFactory, $breadcrumbOverrider); } /** + * @Route("/promo-code/list") + * @param \Symfony\Component\HttpFoundation\Request $request * @return \Symfony\Component\HttpFoundation\Response */ - public function listAction() + public function listAction(Request $request): Response { /** @var \App\Model\Administrator\Administrator $administrator */ $administrator = $this->getUser(); - $grid = $this->promoCodeGridFactory->create(); + $quickSearchForm = $this->createForm(QuickSearchFormType::class, new QuickSearchFormData()); + $quickSearchForm->handleRequest($request); + + $grid = $this->promoCodeGridFactory->create(search: $quickSearchForm->getData()->text); $grid->enablePaging(); $this->administratorGridFacade->restoreAndRememberGridLimit($administrator, $grid); return $this->render('Admin/Content/PromoCode/list.html.twig', [ 'gridView' => $grid->createView(), + 'quickSearchForm' => $quickSearchForm->createView(), ]); } diff --git a/app/src/Model/Order/PromoCode/Grid/PromoCodeGridFactory.php b/app/src/Model/Order/PromoCode/Grid/PromoCodeGridFactory.php index 314f93f573..545e08b6e4 100644 --- a/app/src/Model/Order/PromoCode/Grid/PromoCodeGridFactory.php +++ b/app/src/Model/Order/PromoCode/Grid/PromoCodeGridFactory.php @@ -24,17 +24,18 @@ class PromoCodeGridFactory extends BasePromoCodeGridFactory public function __construct( EntityManagerInterface $em, GridFactory $gridFactory, - private AdminDomainTabsFacade $adminDomainTabsFacade, - private PromoCodeLimitRepository $promoCodeLimitRepository, + private readonly AdminDomainTabsFacade $adminDomainTabsFacade, + private readonly PromoCodeLimitRepository $promoCodeLimitRepository, ) { parent::__construct($em, $gridFactory); } /** * @param bool $withEditButton + * @param string|null $search * @return \Shopsys\FrameworkBundle\Component\Grid\Grid */ - public function create($withEditButton = true) + public function create($withEditButton = true, ?string $search = null) { $queryBuilder = $this->em->createQueryBuilder(); $queryBuilder @@ -42,11 +43,19 @@ public function create($withEditButton = true) ->from(PromoCode::class, 'pc') ->where('pc.domainId = :domainId') ->setParameter('domainId', $this->adminDomainTabsFacade->getSelectedDomainId()); + $manipulator = function ($row) { $row['pc']['percent'] = $this->getLimitsByPromoCodeId($row['pc']['id']); return $row; }; + + if ($search !== null) { + $queryBuilder + ->andWhere('LOWER(pc.code) LIKE LOWER(:search)') + ->setParameter('search', '%' . $search . '%'); + } + $dataSource = new QueryBuilderWithRowManipulatorDataSource($queryBuilder, 'pc.id', $manipulator); $grid = $this->gridFactory->create('promoCodeList', $dataSource); diff --git a/app/templates/Admin/Content/PromoCode/list.html.twig b/app/templates/Admin/Content/PromoCode/list.html.twig index 252d87afac..c7bb063268 100644 --- a/app/templates/Admin/Content/PromoCode/list.html.twig +++ b/app/templates/Admin/Content/PromoCode/list.html.twig @@ -2,6 +2,28 @@ {% block main_content %} {{ render(controller('Shopsys\\FrameworkBundle\\Controller\\Admin\\DomainController::domainTabsAction')) }} + +
+ +
+
+ + {{ form_start(quickSearchForm) }} + + {{ form_end(quickSearchForm) }} +
+
+
+ {{ parent() }} {% endblock %} diff --git a/app/translations/messages.cs.po b/app/translations/messages.cs.po index 25cb5439c9..6a5470da65 100644 --- a/app/translations/messages.cs.po +++ b/app/translations/messages.cs.po @@ -1762,6 +1762,9 @@ msgstr "Hledání" msgid "Search [verb]" msgstr "Vyhledat" +msgid "Search by code..." +msgstr "Vyhledat podle kódu..." + msgid "Search by slug..." msgstr "Vyhledat podle slugu..." diff --git a/app/translations/messages.en.po b/app/translations/messages.en.po index 6b116529ac..8fb5cf595d 100644 --- a/app/translations/messages.en.po +++ b/app/translations/messages.en.po @@ -1762,6 +1762,9 @@ msgstr "" msgid "Search [verb]" msgstr "Search" +msgid "Search by code..." +msgstr "" + msgid "Search by slug..." msgstr ""