Skip to content

Commit

Permalink
added domain filter to order list in admin
Browse files Browse the repository at this point in the history
  • Loading branch information
grossmannmartin committed Sep 21, 2023
1 parent ea18738 commit 64114d8
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 0 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;
}
}
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
3 changes: 3 additions & 0 deletions src/Resources/views/Admin/Content/Order/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@
</div>
</div>
</div>

{{ render(controller('Shopsys\\FrameworkBundle\\Controller\\Admin\\DomainFilterController::domainFilterTabsAction', { namespace: domainFilterNamespace })) }}

{{ gridView.render() }}
{% endblock %}
32 changes: 32 additions & 0 deletions src/Resources/views/Admin/Inline/Domain/filter.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% if domainConfigs|length > 1 %}
<div class="in-domain" role="tablist">
{% if selectedDomainId is null %}
<span class="in-domain__item in-domain__item--active">
{{ 'All domains'|trans }}
</span>
{% else %}
<a class="in-domain__item"
href="{{ url('admin_domainfilter_selectdomain', {
domainId: null,
namespace: namespace
}) }}">
{{ 'All domains'|trans }}
</a>
{% endif %}

{% for domainConfig in domainConfigs %}
{% if domainConfig.id == selectedDomainId %}
<span class="in-domain__item in-domain__item--active">
{{ domainIcon(domainConfig.id) }}
{{ domainConfig.name }}
</span>
{% else %}
<a class="in-domain__item"
href="{{ url('admin_domainfilter_selectdomain', { domainId: domainConfig.id, namespace: namespace }) }}">
{{ domainIcon(domainConfig.id) }}
{{ domainConfig.name }}
</a>
{% endif %}
{% endfor %}
</div>
{% endif %}

0 comments on commit 64114d8

Please sign in to comment.