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
4 changes: 4 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,10 @@ services:
tags:
- { name: form.type, alias: ezplatform_content_forms_url_list }

Ibexa\AdminUi\Form\Type\URLWildcard\URLWildcardListType:
arguments:
- '@translator'

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>All</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>All</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,26 @@
}
%}

<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,
}}) }}

<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) }} &nbsp;
{{ form_widget(form_list.type) }}
</div>

{{ form_widget(form_list.page, { attr: { value: '1' }}) }}
{{ 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
26 changes: 26 additions & 0 deletions src/lib/Form/Data/URLWildcard/URLWildcardListData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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;

use eZ\Publish\API\Repository\Values\ValueObject;

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

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

/** @var int */
public $page = 1;

/** @var int */
public $limit = 10;
}
18 changes: 18 additions & 0 deletions src/lib/Form/Factory/FormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@
use EzSystems\EzPlatformAdminUi\Form\Type\User\UserPasswordResetType;
use EzSystems\EzPlatformAdminUi\Form\Type\Version\VersionRemoveType;
use EzSystems\EzPlatformUser\Form\Type\UserSettingUpdateType;
use Ibexa\AdminUi\Form\Data\URLWildcard\URLWildcardListData;
use Ibexa\AdminUi\Form\Type\URLWildcard\URLWildcardListType;
use Ibexa\Platform\Bundle\Search\Form\Data\SearchData as IbexaSearchData;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Util\StringUtil;
Expand Down Expand Up @@ -1172,6 +1175,21 @@ public function removeContentDraft(
return $this->formFactory->createNamed($name, ContentRemoveType::class, $data);
}

public function createURLWildcardList(
?URLWildcardListData $data = null,
?string $name = null,
array $options = []
): FormInterface {
$name = $name ?: StringUtil::fqcnToBlockPrefix(IbexaSearchData::class);

return $this->formFactory->createNamed(
$name,
URLWildcardListType::class,
$data ?? new URLWildcardListData(),
$options
);
}

/**
* @param \EzSystems\EzPlatformAdminUi\Form\Data\URLWildcard\URLWildcardData|null $data
* @param string|null $name
Expand Down
70 changes: 70 additions & 0 deletions src/lib/Form/Type/URLWildcard/URLWildcardListType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace Ibexa\AdminUi\Form\Type\URLWildcard;

use Ibexa\AdminUi\Form\Data\URLWildcard\URLWildcardListData;
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;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* URLWildcard list form.
*/
class URLWildcardListType extends AbstractType
{
/**
* @var \Symfony\Contracts\Translation\TranslatorInterface
*/
private $translator;

/**
* URLListType constructor.
*
* @param \Symfony\Contracts\Translation\TranslatorInterface $translator
*/
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}

/**
* @inheritdoc
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('type', ChoiceType::class, [
'choices' => [
$this->translator->trans(/** @Desc("Direct") */ 'url_wildcard.type.direct', [], 'ezplatform_url_wildcard') => true,
$this->translator->trans(/** @Desc("Forward") */ 'url_wildcard.type.forward', [], 'ezplatform_url_wildcard') => false,
],
'placeholder' => $this->translator->trans(/** @Desc("All") */ 'url_wildcard.type.all', [], 'ezplatform_url_wildcard'),
'required' => false,
]);

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

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

/**
* @inheritdoc
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => URLWildcardListData::class,
'translation_domain' => 'ezplatform_url_wildcard',
]);
}
}
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;
}
}
65 changes: 57 additions & 8 deletions src/lib/Tab/URLManagement/URLWildcardsTab.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
use EzSystems\EzPlatformAdminUi\Form\Factory\FormFactory;
use EzSystems\EzPlatformAdminUi\Tab\AbstractTab;
use EzSystems\EzPlatformAdminUi\Tab\OrderedTabInterface;
use Ibexa\AdminUi\Form\Data\URLWildcard\URLWildcardListData;
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\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;

Expand Down Expand Up @@ -97,20 +103,31 @@ public function getOrder(): int
*/
public function renderView(array $parameters): string
{
$currentPage = $this->requestStack->getCurrentRequest()->query->getInt(
self::PAGINATION_PARAM_NAME, 1
);
$limit = $this->configResolver->getParameter('pagination.url_wildcards');
$data = new URLWildcardListData(['limit' => $limit]);

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

$searchUrlWildcardForm->handleRequest($this->requestStack->getCurrentRequest());

if ($searchUrlWildcardForm->isSubmitted() && !$searchUrlWildcardForm->isValid()) {
throw new BadRequestHttpException();
}

$pagerfanta = new Pagerfanta(
$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->setCurrentPage($data->page);
$urlWildcardLists->setMaxPerPage($data->limit);

$urlWildcards = $urlWildcardLists->getCurrentPageResults();
$urlWildcardsChoices = [];
foreach ($urlWildcards as $urlWildcardItem) {
$urlWildcardsChoices[$urlWildcardItem->id] = false;
Expand All @@ -125,14 +142,46 @@ public function renderView(array $parameters): string
$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 = [];

$UrlCriterion[] = new Criterion\DestinationUrl($data->searchQuery);
$UrlCriterion[] = 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