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-159: Added pagination in locations tab #1735

Merged
merged 8 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function addSemanticConfig(NodeBuilder $nodeBuilder)
->scalarNode('content_policy_limit')->isRequired()->end()
->scalarNode('notification_limit')->isRequired()->end()
->scalarNode('content_draft_limit')->isRequired()->end()
->scalarNode('location_limit')->isRequired()->end()
->end()
->end();
}
Expand Down Expand Up @@ -89,6 +90,7 @@ public function mapConfig(array &$scopeSettings, $currentScope, ContextualizerIn
'content_policy_limit',
'notification_limit',
'content_draft_limit',
'location_limit',
];

foreach ($keys as $key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ parameters:
ezsettings.default.pagination.notification_limit: 5
ezsettings.admin_group.pagination.user_settings_limit: 10
ezsettings.default.pagination.content_draft_limit: 10
ezsettings.default.pagination.location_limit: 10

# Security
ezsettings.default.security.token_interval_spec: PT1H
Expand Down
17 changes: 17 additions & 0 deletions src/bundle/Resources/views/content/tab/locations/tab.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@
</div>
{{ form_end(form_content_location_remove) }}

{% if pager is defined and pager.haveToPaginate %}
<div class="row justify-content-center align-items-center mb-2 ez-pagination__spacing">
<span class="ez-pagination__text">
{{ 'pagination.viewing'|trans({
'%viewing%': pager.currentPageResults|length,
'%total%': pager.nbResults}, 'pagination')|desc('Viewing <strong>%viewing%</strong> out of <strong>%total%</strong> items')|raw }}
</span>
</div>
<div class="row justify-content-center align-items-center ez-pagination__btn mb-5">
{{ pagerfanta(pager, 'ez', pager_options|merge({
'routeParams': {
'_fragment': 'ez-tab-location-view-locations'
}
})) }}
</div>
{% endif %}

{{ form(form_content_location_update_visibility, {'action': path('ezplatform.location.update_visibility')}) }}
{{ form(form_content_location_main_update, {'action': path('ezplatform.content.update_main_location')}) }}

Expand Down
69 changes: 58 additions & 11 deletions src/lib/Tab/LocationView/LocationsTab.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@

namespace EzSystems\EzPlatformAdminUi\Tab\LocationView;

use eZ\Publish\API\Repository\SearchService;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
use eZ\Publish\Core\Pagination\Pagerfanta\LocationSearchAdapter;
use EzSystems\EzPlatformAdminUi\Form\Data\Content\Location\ContentMainLocationUpdateData;
use EzSystems\EzPlatformAdminUi\Form\Data\Content\Location\ContentLocationAddData;
use EzSystems\EzPlatformAdminUi\Form\Data\Content\Location\ContentLocationRemoveData;
use EzSystems\EzPlatformAdminUi\Form\Data\Location\LocationSwapData;
use EzSystems\EzPlatformAdminUi\Form\Data\Location\LocationUpdateVisibilityData;
use EzSystems\EzPlatformAdminUi\Form\Factory\FormFactory;
use eZ\Publish\API\Repository\Values\Content\Query;
use EzSystems\EzPlatformAdminUi\Tab\AbstractEventDispatchingTab;
use EzSystems\EzPlatformAdminUi\Tab\OrderedTabInterface;
use EzSystems\EzPlatformAdminUi\UI\Dataset\DatasetFactory;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use EzSystems\EzPlatformAdminUi\UI\Value\Content\Location\Mapper;
use Pagerfanta\Pagerfanta;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Twig\Environment;
Expand All @@ -28,9 +35,7 @@
class LocationsTab extends AbstractEventDispatchingTab implements OrderedTabInterface
{
const URI_FRAGMENT = 'ez-tab-location-view-locations';

/** @var \EzSystems\EzPlatformAdminUi\UI\Dataset\DatasetFactory */
protected $datasetFactory;
private const PAGINATION_PARAM_NAME = 'locations-tab-page';

/** @var \EzSystems\EzPlatformAdminUi\Form\Factory\FormFactory */
protected $formFactory;
Expand All @@ -41,30 +46,51 @@ class LocationsTab extends AbstractEventDispatchingTab implements OrderedTabInte
/** @var \eZ\Publish\API\Repository\PermissionResolver */
protected $permissionResolver;

/** @var \Symfony\Component\HttpFoundation\RequestStack */
private $requestStack;

/** @var \eZ\Publish\API\Repository\SearchService */
private $searchService;

/** @var \EzSystems\EzPlatformAdminUi\UI\Value\Content\Location\Mapper */
private $locationToUILocationMapper;

/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
private $configResolver;

/**
* @param \Twig\Environment $twig
* @param \Symfony\Component\Translation\TranslatorInterface $translator
* @param \EzSystems\EzPlatformAdminUi\UI\Dataset\DatasetFactory $datasetFactory
* @param \EzSystems\EzPlatformAdminUi\Form\Factory\FormFactory $formFactory
* @param \Symfony\Component\Routing\Generator\UrlGeneratorInterface $urlGenerator
* @param \eZ\Publish\API\Repository\PermissionResolver $permissionResolver
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* @param \eZ\Publish\API\Repository\SearchService $searchService
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* @param \EzSystems\EzPlatformAdminUi\UI\Value\Content\Location\Mapper$locationToUILocationMapper
* @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver
*/
public function __construct(
Environment $twig,
TranslatorInterface $translator,
DatasetFactory $datasetFactory,
FormFactory $formFactory,
UrlGeneratorInterface $urlGenerator,
PermissionResolver $permissionResolver,
EventDispatcherInterface $eventDispatcher
EventDispatcherInterface $eventDispatcher,
SearchService $searchService,
RequestStack $requestStack,
Mapper $locationToUILocationMapper,
ConfigResolverInterface $configResolver
) {
parent::__construct($twig, $translator, $eventDispatcher);

$this->datasetFactory = $datasetFactory;
$this->formFactory = $formFactory;
$this->urlGenerator = $urlGenerator;
$this->permissionResolver = $permissionResolver;
$this->requestStack = $requestStack;
$this->configResolver = $configResolver;
$this->searchService = $searchService;
$this->locationToUILocationMapper = $locationToUILocationMapper;
}

/**
Expand Down Expand Up @@ -112,12 +138,29 @@ public function getTemplateParameters(array $contextParameters = []): array
$versionInfo = $content->getVersionInfo();
$contentInfo = $versionInfo->getContentInfo();
$locations = [];
$pagination = null;
$defaultPaginationLimit = $this->configResolver->getParameter('pagination.location_limit');

if ($contentInfo->published) {
$locationsDataset = $this->datasetFactory->locations();
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
$locationsDataset->load($contentInfo);
$currentPage = $this->requestStack->getCurrentRequest()->query->getInt(
self::PAGINATION_PARAM_NAME, 1
);

$locationQuery = new LocationQuery([
'filter' => new Query\Criterion\ContentId($contentInfo->id),
]);

$pagination = new Pagerfanta(
new LocationSearchAdapter(
$locationQuery,
$this->searchService
)
);

$locations = $locationsDataset->getLocations();
$pagination->setMaxPerPage($defaultPaginationLimit);
$pagination->setCurrentPage(max($currentPage, 1));
$locationsArray = iterator_to_array($pagination);
$locations = $this->locationToUILocationMapper->map($locationsArray);
}

$formLocationAdd = $this->createLocationAddForm($location);
Expand All @@ -142,6 +185,10 @@ public function getTemplateParameters(array $contextParameters = []): array
}

$viewParameters = [
'pager' => $pagination,
'pager_options' => [
'pageParameter' => sprintf('[%s]', self::PAGINATION_PARAM_NAME),
],
'locations' => $locations,
'form_content_location_add' => $formLocationAdd->createView(),
'form_content_location_remove' => $formLocationRemove->createView(),
Expand Down
56 changes: 56 additions & 0 deletions src/lib/UI/Value/Content/Location/Mapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

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

namespace EzSystems\EzPlatformAdminUi\UI\Value\Content\Location;

use EzSystems\EzPlatformAdminUi\UI\Value\ValueFactory;
use EzSystems\EzPlatformAdminUi\UI\Value;

final class Mapper
{
/** @var \EzSystems\EzPlatformAdminUi\UI\Value\ValueFactory */
private $valueFactory;

public function __construct(
ValueFactory $valueFactory
) {
$this->valueFactory = $valueFactory;
}

/**
* @param \eZ\Publish\API\Repository\Values\Content\Location[] $locations
*
* @return Value\Content\Location[]
*/
public function map(array $locations): array
{
$data = array_map(
[$this->valueFactory, 'createLocation'],
$locations
);

return $this->prioritizeMainLocation($data);
}

/**
* @param \eZ\Publish\API\Repository\Values\Content\Location[] $locations
*
* @return Value\Content\Location[]
*/
private function prioritizeMainLocation(array $locations): array
{
$key = array_search(true, array_column($locations, 'main'));
if ($key !== false) {
$location = $locations[$key];
unset($locations[$key]);
array_unshift($locations, $location);
}

return $locations;
}
}