Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-2000: Added search into URL Wildcard tab #2042

Merged
merged 10 commits into from
May 25, 2022
2 changes: 2 additions & 0 deletions src/bundle/Resources/config/services/forms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ services:
tags:
- { name: form.type, alias: ezplatform_content_forms_url_list }

Ibexa\AdminUi\Form\Type\URLWildcard\URLWildcardListType: ~

EzSystems\EzPlatformAdminUi\Form\Type\URLWildcard\URLWildcardDeleteType: ~

EzSystems\EzPlatformAdminUi\Form\Type\URLWildcard\URLWildcardType: ~
Expand Down
15 changes: 15 additions & 0 deletions src/bundle/Resources/translations/ezplatform_url_wildcard.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@
<target state="new">Forward</target>
<note>key: url_wildcard.type.forward</note>
</trans-unit>
<trans-unit id="5646653e7d129c0a5c96b6b87e99eeb9d92a6fee" resname="url_wildcard.type.all">
<source>All</source>
<target state="new">All</target>
<note>key: url_wildcard.type.all</note>
</trans-unit>
<trans-unit id="d5f8b02988ca5a63f52253b3eec84f9cc8f799cd" resname="url_wildcard.search.placeholder">
<source>Search for URL wildcards</source>
<target state="new">Search for URL wildcards</target>
<note>key: url_wildcard.search.placeholder</note>
</trans-unit>
<trans-unit id="fc64faec5bb26550f5008c70a89e7ba76f0d5dd1" resname="url_wildcard.search">
<source>Search</source>
<target state="new">Search</target>
<note>key: url_wildcard.search</note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@
}
%}

<section class="container">
{{ form_start(form_list, {'attr': {'class': 'form-inline justify-content-between'}}) }}
<div class="input-group">
{{ form_widget(form_list.searchQuery, { attr: {
'placeholder': 'url_wildcard.search.placeholder'|trans|desc('Search for URL wildcards'),
}}) }}

<button class="btn btn-primary ml-2">
{{ 'url_wildcard.search'|trans|desc("Search") }}
</button>
</div>
<div class="d-inline-flex">
{{ form_label(form_list.type) }}
{{ form_widget(form_list.type) }}
</div>

{{ form_end(form_list) }}
</section>

<section class="container mt-4">
{% if not url_wildcards_enabled %}
<div class="ez-alert ez-alert--info mb-4">
Expand Down
51 changes: 51 additions & 0 deletions src/lib/Form/Data/URLWildcard/URLWildcardListData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\AdminUi\Form\Data\URLWildcard;

final class URLWildcardListData
{
/** @var string|null */
public $searchQuery;

/** @var bool|null */
public $type;

/** @var int */
public $limit;

public function getSearchQuery(): ?string
{
return $this->searchQuery;
}

public function setSearchQuery(?string $searchQuery): void
{
$this->searchQuery = $searchQuery;
}

public function getType(): ?bool
{
return $this->type;
}

public function setType(?bool $type): void
{
$this->type = $type;
}

public function getLimit(): int
{
return $this->limit;
}

public function setLimit(int $limit): void
{
$this->limit = $limit;
}
}
64 changes: 64 additions & 0 deletions src/lib/Form/Type/URLWildcard/URLWildcardListType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\AdminUi\Form\Type\URLWildcard;

use Ibexa\AdminUi\Form\Data\URLWildcard\URLWildcardListData;
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Translation\TranslationContainerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SearchType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class URLWildcardListType extends AbstractType implements TranslationContainerInterface
{
private const TYPE_DIRECT = 'url_wildcard.type.direct';
private const TYPE_FORWARD = 'url_wildcard.type.forward';
private const PLACEHOLDER = 'url_wildcard.type.all';

public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('type', ChoiceType::class, [
'choices' => [
self::TYPE_DIRECT => true,
self::TYPE_FORWARD => false,
],
'placeholder' => self::PLACEHOLDER,
'required' => false,
]);

$builder->add('searchQuery', SearchType::class, [
'required' => false,
]);

$builder->add('limit', HiddenType::class);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => URLWildcardListData::class,
'translation_domain' => 'ezplatform_url_wildcard',
]);
}

/**
* @return array<\JMS\TranslationBundle\Model\Message>
*/
public static function getTranslationMessages(): array
{
return [
(new Message(self::TYPE_DIRECT, 'ezplatform_url_wildcard'))->setDesc('Direct'),
(new Message(self::TYPE_FORWARD, 'ezplatform_url_wildcard'))->setDesc('Forward'),
(new Message(self::PLACEHOLDER, 'ezplatform_url_wildcard'))->setDesc('All'),
];
}
}
33 changes: 21 additions & 12 deletions src/lib/Pagination/Pagerfanta/URLWildcardAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Ibexa\AdminUi\Pagination\Pagerfanta;

use eZ\Publish\API\Repository\URLWildcardService;
use Ibexa\Contracts\Core\Repository\Values\Content\URLWildcard\URLWildcardQuery;
use Pagerfanta\Adapter\AdapterInterface;

final class URLWildcardAdapter implements AdapterInterface
Expand All @@ -17,35 +18,43 @@ final class URLWildcardAdapter implements AdapterInterface
/** @var int */
private $nbResults;

public function __construct(URLWildcardService $urlWildcardService)
/** @var \Ibexa\Contracts\Core\Repository\Values\Content\URLWildcard\URLWildcardQuery */
private $query;

public function __construct(URLWildcardQuery $query, URLWildcardService $urlWildcardService)
{
$this->query = $query;
$this->urlWildcardService = $urlWildcardService;
}

/**
* Returns the number of results.
* @inheritdoc
*
* @return int the number of results
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function getNbResults(): int
{
if ($this->nbResults !== null) {
return $this->nbResults;
}
$query = clone $this->query;
$query->offset = 0;
$query->limit = 0;

return $this->nbResults = $this->urlWildcardService->countAll();
return $this->urlWildcardService->findUrlWildcards($query)->totalCount;
}

/**
* Returns a slice of the results.
*
* @param int $offset the offset
* @param int $length the length
* @inheritdoc
*
* @return \eZ\Publish\API\Repository\Values\Content\URLWildcard[]
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function getSlice($offset, $length): array
{
return $this->urlWildcardService->loadAll($offset, $length);
$query = clone $this->query;
$query->offset = $offset;
$query->limit = $length;
$query->performCount = false;

return $this->urlWildcardService->findUrlWildcards($query)->items;
}
}
87 changes: 75 additions & 12 deletions src/lib/Tab/URLManagement/URLWildcardsTab.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@
use eZ\Publish\API\Repository\URLWildcardService;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use EzSystems\EzPlatformAdminUi\Form\Data\URLWildcard\URLWildcardDeleteData;
use EzSystems\EzPlatformAdminUi\Form\Factory\FormFactory;
use EzSystems\EzPlatformAdminUi\Form\Type\URLWildcard\URLWildcardDeleteType;
use EzSystems\EzPlatformAdminUi\Form\Type\URLWildcard\URLWildcardType;
use EzSystems\EzPlatformAdminUi\Tab\AbstractTab;
use EzSystems\EzPlatformAdminUi\Tab\OrderedTabInterface;
use Ibexa\AdminUi\Form\Data\URLWildcard\URLWildcardListData;
use Ibexa\AdminUi\Form\Type\URLWildcard\URLWildcardListType;
use Ibexa\AdminUi\Pagination\Pagerfanta\URLWildcardAdapter;
use Ibexa\Contracts\Core\Repository\Values\Content\URLWildcard\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\URLWildcard\Query\SortClause;
use Ibexa\Contracts\Core\Repository\Values\Content\URLWildcard\URLWildcardQuery;
use Pagerfanta\Pagerfanta;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;

Expand All @@ -39,7 +48,7 @@ class URLWildcardsTab extends AbstractTab implements OrderedTabInterface
/** @var \Symfony\Component\HttpFoundation\RequestStack */
private $requestStack;

/** @var \EzSystems\EzPlatformAdminUi\Form\Factory\FormFactory */
/** @var \Symfony\Component\Form\FormFactoryInterface */
private $formFactory;

public function __construct(
Expand All @@ -49,7 +58,7 @@ public function __construct(
ConfigResolverInterface $configResolver,
RequestStack $requestStack,
URLWildcardService $urlWildcardService,
FormFactory $formFactory
FormFactoryInterface $formFactory
) {
parent::__construct($twig, $translator);

Expand Down Expand Up @@ -97,42 +106,96 @@ public function getOrder(): int
*/
public function renderView(array $parameters): string
{
$currentPage = $this->requestStack->getCurrentRequest()->query->getInt(
$limit = $this->configResolver->getParameter('pagination.url_wildcards');
$currentRequest = $this->requestStack->getCurrentRequest();
$page = $this->requestStack->getCurrentRequest()->query->getInt(
self::PAGINATION_PARAM_NAME, 1
);
$limit = $this->configResolver->getParameter('pagination.url_wildcards');

$pagerfanta = new Pagerfanta(
$data = new URLWildcardListData();
$data->setLimit($limit);

$searchUrlWildcardForm = $this->formFactory->create(
URLWildcardListType::class,
$data,
[
'method' => Request::METHOD_GET,
'csrf_protection' => false,
]
);

$searchUrlWildcardForm->handleRequest($currentRequest);

if ($searchUrlWildcardForm->isSubmitted() && !$searchUrlWildcardForm->isValid()) {
throw new BadRequestHttpException('The search form is not valid');
}

$urlWildcardLists = new Pagerfanta(
new URLWildcardAdapter(
$this->buildListQuery($data),
$this->urlWildcardService
)
);
$pagerfanta->setMaxPerPage($limit);
$pagerfanta->setCurrentPage(min(max($currentPage, 1), $pagerfanta->getNbPages()));

$urlWildcards = $pagerfanta->getCurrentPageResults();
$urlWildcardLists->setMaxPerPage($data->getLimit());
$urlWildcardLists->setCurrentPage(min($page, $urlWildcardLists->getNbPages()));

$urlWildcards = $urlWildcardLists->getCurrentPageResults();
$urlWildcardsChoices = [];
foreach ($urlWildcards as $urlWildcardItem) {
$urlWildcardsChoices[$urlWildcardItem->id] = false;
}

$deleteUrlWildcardDeleteForm = $this->formFactory->deleteURLWildcard(
$deleteUrlWildcardDeleteForm = $this->formFactory->create(
URLWildcardDeleteType::class,
new URLWildcardDeleteData($urlWildcardsChoices)
);

$addUrlWildcardForm = $this->formFactory->createURLWildcard();
$addUrlWildcardForm = $this->formFactory->create(URLWildcardType::class);
$urlWildcardsEnabled = $this->configResolver->getParameter('url_wildcards.enabled');
$canManageWildcards = $this->permissionResolver->hasAccess('content', 'urltranslator');

return $this->twig->render('@ezdesign/url_wildcard/list.html.twig', [
'url_wildcards' => $pagerfanta,
'url_wildcards' => $urlWildcardLists,
'pager_options' => [
'pageParameter' => '[' . self::PAGINATION_PARAM_NAME . ']',
],
'form' => $deleteUrlWildcardDeleteForm->createView(),
'form_list' => $searchUrlWildcardForm->createView(),
'form_add' => $addUrlWildcardForm->createView(),
'url_wildcards_enabled' => $urlWildcardsEnabled,
'can_manage' => $canManageWildcards,
]);
}

private function buildListQuery(URLWildcardListData $data): URLWildcardQuery
{
$query = new URLWildcardQuery();
$query->sortClauses = [
new SortClause\DestinationUrl(),
];

$criteria = [];

if ($data->searchQuery !== null) {
$urlCriterion = [
new Criterion\DestinationUrl($data->searchQuery),
new Criterion\SourceUrl($data->searchQuery),
];

$criteria[] = new Criterion\LogicalOr($urlCriterion);
}

if ($data->type !== null) {
$criteria[] = new Criterion\Type($data->type);
}

if (empty($criteria)) {
$criteria[] = new Criterion\MatchAll();
}

$query->filter = new Criterion\LogicalAnd($criteria);

return $query;
}
}
Loading