Skip to content

Commit

Permalink
[shopsys] added order filter by domain in admin (#2796)
Browse files Browse the repository at this point in the history
  • Loading branch information
grossmannmartin authored Sep 21, 2023
2 parents ea18738 + 824e3e4 commit a34e572
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 162 deletions.
73 changes: 73 additions & 0 deletions src/Component/Domain/AdminDomainFilterTabsFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Component\Domain;

use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Component\Domain\Exception\InvalidDomainIdException;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
use Symfony\Component\HttpFoundation\RequestStack;

class AdminDomainFilterTabsFacade
{
protected const SESSION_PREFIX = 'admin_domain_filter_tabs_';

/**
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
*/
public function __construct(
protected readonly RequestStack $requestStack,
protected readonly Domain $domain,
) {
}

/**
* @param string $namespace
* @return int|null
*/
public function getSelectedDomainId(string $namespace): ?int
{
return $this->getSelectedDomainConfig($namespace)?->getId();
}

/**
* @param string $namespace
* @param int|null $domainId
*/
public function setSelectedDomainId(string $namespace, ?int $domainId): void
{
$this->requestStack->getSession()->set($this->getSessionKey($namespace), $domainId);
}

/**
* @param string $namespace
* @return \Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig|null
*/
public function getSelectedDomainConfig(string $namespace): ?DomainConfig
{
try {
$domainId = $this->requestStack->getSession()->get($this->getSessionKey($namespace));

if ($domainId === null) {
return null;
}

return $this->domain->getDomainConfigById($domainId);
} catch (InvalidDomainIdException|SessionNotFoundException) {
$this->requestStack->getSession()->set($this->getSessionKey($namespace), null);

return null;
}
}

/**
* @param string $namespace
* @return string
*/
protected function getSessionKey(string $namespace): string
{
return static::SESSION_PREFIX . $namespace;
}
}
50 changes: 9 additions & 41 deletions src/Controller/Admin/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,33 @@
namespace Shopsys\FrameworkBundle\Controller\Admin;

use Nette\Utils\Json;
use Shopsys\FrameworkBundle\Component\Domain\AdminDomainFilterTabsFacade;
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Component\Domain\Exception\InvalidDomainIdException;
use Shopsys\FrameworkBundle\Component\Router\Security\Annotation\CsrfProtection;
use Shopsys\FrameworkBundle\Form\Admin\Category\CategoryFormType;
use Shopsys\FrameworkBundle\Model\AdminNavigation\BreadcrumbOverrider;
use Shopsys\FrameworkBundle\Model\Category\CategoryDataFactoryInterface;
use Shopsys\FrameworkBundle\Model\Category\CategoryFacade;
use Shopsys\FrameworkBundle\Model\Category\Exception\CategoryNotFoundException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class CategoryController extends AdminBaseController
{
protected const ALL_DOMAINS = 0;

/**
* @param \Shopsys\FrameworkBundle\Model\Category\CategoryFacade $categoryFacade
* @param \Shopsys\FrameworkBundle\Model\Category\CategoryDataFactoryInterface $categoryDataFactory
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
* @param \Shopsys\FrameworkBundle\Model\AdminNavigation\BreadcrumbOverrider $breadcrumbOverrider
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* @param \Shopsys\FrameworkBundle\Component\Domain\AdminDomainFilterTabsFacade $adminDomainFilterTabsFacade
*/
public function __construct(
protected readonly CategoryFacade $categoryFacade,
protected readonly CategoryDataFactoryInterface $categoryDataFactory,
protected readonly Domain $domain,
protected readonly BreadcrumbOverrider $breadcrumbOverrider,
protected readonly RequestStack $requestStack,
protected readonly AdminDomainFilterTabsFacade $adminDomainFilterTabsFacade,
) {
}

Expand Down Expand Up @@ -128,40 +125,24 @@ public function newAction(Request $request): Response
*/
public function listAction(Request $request): Response
{
if (count($this->domain->getAll()) > 1) {
if ($request->query->has('domain')) {
$domainId = (int)$request->query->get('domain');
} else {
$domainId = (int)$this->requestStack->getSession()->get('categories_selected_domain_id', static::ALL_DOMAINS);
}
} else {
$domainId = static::ALL_DOMAINS;
}
$domainFilterNamespace = 'category';
$selectedDomainId = $this->adminDomainFilterTabsFacade->getSelectedDomainId($domainFilterNamespace);

if ($domainId !== static::ALL_DOMAINS) {
try {
$this->domain->getDomainConfigById($domainId);
} catch (InvalidDomainIdException $ex) {
$domainId = static::ALL_DOMAINS;
}
}

$this->requestStack->getSession()->set('categories_selected_domain_id', $domainId);

if ($domainId === static::ALL_DOMAINS) {
if ($selectedDomainId === null) {
$categoriesWithPreloadedChildren = $this->categoryFacade->getAllCategoriesWithPreloadedChildren(
$request->getLocale(),
);
} else {
$categoriesWithPreloadedChildren = $this->categoryFacade->getVisibleCategoriesWithPreloadedChildrenForDomain(
$domainId,
$selectedDomainId,
$request->getLocale(),
);
}

return $this->render('@ShopsysFramework/Admin/Content/Category/list.html.twig', [
'categoriesWithPreloadedChildren' => $categoriesWithPreloadedChildren,
'isForAllDomains' => ($domainId === static::ALL_DOMAINS),
'domainFilterNamespace' => $domainFilterNamespace,
'isForAllDomains' => ($selectedDomainId === null),
]);
}

Expand Down Expand Up @@ -207,19 +188,6 @@ public function deleteAction(int $id): Response
return $this->redirectToRoute('admin_category_list');
}

/**
* @return \Symfony\Component\HttpFoundation\Response
*/
public function listDomainTabsAction(): Response
{
$domainId = $this->requestStack->getSession()->get('categories_selected_domain_id', static::ALL_DOMAINS);

return $this->render('@ShopsysFramework/Admin/Content/Category/domainTabs.html.twig', [
'domainConfigs' => $this->domain->getAll(),
'selectedDomainId' => $domainId,
]);
}

/**
* @Route("/category/branch/{domainId}/{id}", requirements={"domainId" = "\d+", "id" = "\d+"}, condition="request.isXmlHttpRequest()")
* @param int $domainId
Expand Down
58 changes: 58 additions & 0 deletions src/Controller/Admin/DomainFilterController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Controller\Admin;

use Shopsys\FrameworkBundle\Component\Domain\AdminDomainFilterTabsFacade;
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class DomainFilterController extends AdminBaseController
{
/**
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
* @param \Shopsys\FrameworkBundle\Component\Domain\AdminDomainFilterTabsFacade $adminDomainFilterTabsFacade
*/
public function __construct(
protected readonly Domain $domain,
protected readonly AdminDomainFilterTabsFacade $adminDomainFilterTabsFacade,
) {
}

/**
* @param string $namespace
* @return \Symfony\Component\HttpFoundation\Response
*/
public function domainFilterTabsAction(string $namespace): Response
{
return $this->render('@ShopsysFramework/Admin/Inline/Domain/filter.html.twig', [
'domainConfigs' => $this->domain->getAll(),
'namespace' => $namespace,
'selectedDomainId' => $this->adminDomainFilterTabsFacade->getSelectedDomainId($namespace),
]);
}

/**
* @Route("/multidomain/filter-domain/{namespace}/{domainId}", requirements={"domainId" = "\d+"})
* @param \Symfony\Component\HttpFoundation\Request $request
* @param string $namespace
* @param int|null $domainId
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function selectDomainAction(Request $request, string $namespace, ?int $domainId = null): RedirectResponse
{
$this->adminDomainFilterTabsFacade->setSelectedDomainId($namespace, $domainId);

$referer = $request->server->get('HTTP_REFERER');

if ($referer === null) {
return $this->redirectToRoute('admin_default_dashboard');
}

return $this->redirect($referer);
}
}
14 changes: 14 additions & 0 deletions src/Controller/Admin/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Shopsys\FrameworkBundle\Controller\Admin;

use Shopsys\FrameworkBundle\Component\Domain\AdminDomainFilterTabsFacade;
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Component\Grid\DataSourceInterface;
use Shopsys\FrameworkBundle\Component\Grid\GridFactory;
Expand Down Expand Up @@ -36,6 +37,7 @@ class OrderController extends AdminBaseController
* @param \Shopsys\FrameworkBundle\Model\Order\Item\OrderItemFacade $orderItemFacade
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
* @param \Shopsys\FrameworkBundle\Model\Order\OrderDataFactoryInterface $orderDataFactory
* @param \Shopsys\FrameworkBundle\Component\Domain\AdminDomainFilterTabsFacade $adminDomainFilterTabsFacade
*/
public function __construct(
protected readonly OrderFacade $orderFacade,
Expand All @@ -47,6 +49,7 @@ public function __construct(
protected readonly OrderItemFacade $orderItemFacade,
protected readonly Domain $domain,
protected readonly OrderDataFactoryInterface $orderDataFactory,
protected readonly AdminDomainFilterTabsFacade $adminDomainFilterTabsFacade,
) {
}

Expand Down Expand Up @@ -132,6 +135,8 @@ public function addProductAction(Request $request, $orderId)
*/
public function listAction(Request $request)
{
$domainFilterNamespace = 'orders';

/** @var \Shopsys\FrameworkBundle\Model\Administrator\Administrator $administrator */
$administrator = $this->getUser();
$advancedSearchForm = $this->advancedSearchOrderFacade->createAdvancedSearchOrderForm($request);
Expand All @@ -152,6 +157,14 @@ public function listAction(Request $request)
$queryBuilder = $this->orderFacade->getOrderListQueryBuilderByQuickSearchData($quickSearchForm->getData());
}

$selectedDomainId = $this->adminDomainFilterTabsFacade->getSelectedDomainId($domainFilterNamespace);

if ($selectedDomainId !== null) {
$queryBuilder
->andWhere('o.domainId = :selectedDomainId')
->setParameter('selectedDomainId', $selectedDomainId);
}

$dataSource = new QueryBuilderWithRowManipulatorDataSource(
$queryBuilder,
'o.id',
Expand Down Expand Up @@ -187,6 +200,7 @@ function ($row) {

return $this->render('@ShopsysFramework/Admin/Content/Order/list.html.twig', [
'gridView' => $grid->createView(),
'domainFilterNamespace' => $domainFilterNamespace,
'quickSearchForm' => $quickSearchForm->createView(),
'advancedSearchForm' => $advancedSearchForm->createView(),
'isAdvancedSearchFormSubmitted' => $this->advancedSearchOrderFacade->isAdvancedSearchOrderFormSubmitted(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderCityFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderCreateDateFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderDomainFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderEmailFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderLastNameFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderNameFilter;
Expand All @@ -26,7 +25,6 @@ public function __construct()
$this->addFilterTranslation(OrderNumberFilter::NAME, t('Order number'));
$this->addFilterTranslation(OrderCreateDateFilter::NAME, t('Created on'));
$this->addFilterTranslation(OrderPriceFilterWithVatFilter::NAME, t('Price including VAT'));
$this->addFilterTranslation(OrderDomainFilter::NAME, t('Domain'));
$this->addFilterTranslation(OrderStatusFilter::NAME, t('Status of order'));
$this->addFilterTranslation(OrderProductFilter::NAME, t('Product in order'));
$this->addFilterTranslation(OrderPhoneNumberFilter::NAME, t('Customer phone number'));
Expand Down
11 changes: 1 addition & 10 deletions src/Model/AdvancedSearch/OrderAdvancedSearchConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

namespace Shopsys\FrameworkBundle\Model\AdvancedSearch;

use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderCityFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderCreateDateFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderDomainFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderEmailFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderLastNameFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderNameFilter;
Expand All @@ -24,7 +22,6 @@ class OrderAdvancedSearchConfig extends AdvancedSearchConfig
* @param \Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderNumberFilter $orderNumberFilter
* @param \Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderCreateDateFilter $orderCreateDateFilter
* @param \Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderPriceFilterWithVatFilter $orderPriceFilterWithVatFilter
* @param \Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderDomainFilter $orderDomainFilter
* @param \Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderStatusFilter $orderStatusFilter
* @param \Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderProductFilter $orderProductFilter
* @param \Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderPhoneNumberFilter $orderPhoneNumberFilter
Expand All @@ -33,13 +30,12 @@ class OrderAdvancedSearchConfig extends AdvancedSearchConfig
* @param \Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderLastNameFilter $orderLastNameFilter
* @param \Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderEmailFilter $orderEmailFilter
* @param \Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderCityFilter $orderCityFilter
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
* @throws \Shopsys\FrameworkBundle\Model\AdvancedSearch\Exception\AdvancedSearchFilterAlreadyExistsException
*/
public function __construct(
OrderNumberFilter $orderNumberFilter,
OrderCreateDateFilter $orderCreateDateFilter,
OrderPriceFilterWithVatFilter $orderPriceFilterWithVatFilter,
OrderDomainFilter $orderDomainFilter,
OrderStatusFilter $orderStatusFilter,
OrderProductFilter $orderProductFilter,
OrderPhoneNumberFilter $orderPhoneNumberFilter,
Expand All @@ -48,18 +44,13 @@ public function __construct(
OrderLastNameFilter $orderLastNameFilter,
OrderEmailFilter $orderEmailFilter,
OrderCityFilter $orderCityFilter,
Domain $domain,
) {
parent::__construct();

$this->registerFilter($orderPriceFilterWithVatFilter);
$this->registerFilter($orderNumberFilter);
$this->registerFilter($orderCreateDateFilter);
$this->registerFilter($orderStatusFilter);

if ($domain->isMultidomain()) {
$this->registerFilter($orderDomainFilter);
}
$this->registerFilter($orderProductFilter);
$this->registerFilter($orderNameFilter);
$this->registerFilter($orderLastNameFilter);
Expand Down
Loading

0 comments on commit a34e572

Please sign in to comment.