From 0c2e944977bca365dd76ecda10b574698f2cd456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Adamczyk?= Date: Thu, 21 Dec 2017 14:27:20 +0100 Subject: [PATCH] EZP-28619: Add pagination to Role Assignments and Policies --- src/bundle/Controller/PolicyController.php | 24 ++++++++++++-- .../Controller/RoleAssignmentController.php | 32 ++++++++++++++++--- src/bundle/Controller/RoleController.php | 16 +++++++++- .../Configuration/Parser/Pagination.php | 4 +++ .../config/ezplatform_default_settings.yml | 2 ++ src/bundle/Resources/config/routing.yml | 6 +++- .../Resources/config/services/controllers.yml | 10 ++++++ .../views/admin/policy/list.html.twig | 19 ++++++++++- .../Resources/views/admin/role/view.html.twig | 15 ++++++--- .../admin/role_assignment/list.html.twig | 19 ++++++++++- 10 files changed, 131 insertions(+), 16 deletions(-) diff --git a/src/bundle/Controller/PolicyController.php b/src/bundle/Controller/PolicyController.php index bbad372544..7b276d0b72 100644 --- a/src/bundle/Controller/PolicyController.php +++ b/src/bundle/Controller/PolicyController.php @@ -23,6 +23,8 @@ use EzSystems\EzPlatformAdminUi\Form\Factory\FormFactory; use EzSystems\EzPlatformAdminUi\Form\SubmitHandler; use EzSystems\EzPlatformAdminUi\Notification\NotificationHandlerInterface; +use Pagerfanta\Adapter\ArrayAdapter; +use Pagerfanta\Pagerfanta; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -53,6 +55,9 @@ class PolicyController extends Controller /** @var SubmitHandler */ private $submitHandler; + /** @var int */ + private $defaultPaginationLimit; + /** * PolicyController constructor. * @@ -63,6 +68,7 @@ class PolicyController extends Controller * @param PolicyUpdateMapper $policyUpdateMapper * @param FormFactory $formFactory * @param SubmitHandler $submitHandler + * @param int $defaultPaginationLimit */ public function __construct( NotificationHandlerInterface $notificationHandler, @@ -71,7 +77,8 @@ public function __construct( PolicyCreateMapper $policyCreateMapper, PolicyUpdateMapper $policyUpdateMapper, FormFactory $formFactory, - SubmitHandler $submitHandler + SubmitHandler $submitHandler, + int $defaultPaginationLimit ) { $this->notificationHandler = $notificationHandler; $this->translator = $translator; @@ -80,11 +87,20 @@ public function __construct( $this->policyUpdateMapper = $policyUpdateMapper; $this->formFactory = $formFactory; $this->submitHandler = $submitHandler; + $this->defaultPaginationLimit = $defaultPaginationLimit; } - public function listAction(Role $role): Response + public function listAction(Role $role, string $routeName, int $policyPage = 1): Response { - $policies = $role->getPolicies(); + $pagerfanta = new Pagerfanta( + new ArrayAdapter($role->getPolicies()) + ); + + $pagerfanta->setMaxPerPage($this->defaultPaginationLimit); + $pagerfanta->setCurrentPage(min($policyPage, $pagerfanta->getNbPages())); + + /** @var Policy[] $policies */ + $policies = $pagerfanta->getCurrentPageResults(); $deletePoliciesForm = $this->formFactory->deletePolicies( new PoliciesDeleteData($role, $this->getPoliciesNumbers($policies)) @@ -93,6 +109,8 @@ public function listAction(Role $role): Response return $this->render('@EzPlatformAdminUi/admin/policy/list.html.twig', [ 'form_policies_delete' => $deletePoliciesForm->createView(), 'role' => $role, + 'pager' => $pagerfanta, + 'route_name' => $routeName, ]); } diff --git a/src/bundle/Controller/RoleAssignmentController.php b/src/bundle/Controller/RoleAssignmentController.php index 6ce8a8f43b..6a55cd7e5b 100644 --- a/src/bundle/Controller/RoleAssignmentController.php +++ b/src/bundle/Controller/RoleAssignmentController.php @@ -22,6 +22,8 @@ use EzSystems\EzPlatformAdminUi\Form\Factory\FormFactory; use EzSystems\EzPlatformAdminUi\Form\SubmitHandler; use EzSystems\EzPlatformAdminUi\Notification\NotificationHandlerInterface; +use Pagerfanta\Adapter\ArrayAdapter; +use Pagerfanta\Pagerfanta; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -46,6 +48,9 @@ class RoleAssignmentController extends Controller /** @var SubmitHandler */ private $submitHandler; + /** @var int */ + private $defaultPaginationLimit; + /** * PolicyController constructor. * @@ -54,24 +59,42 @@ class RoleAssignmentController extends Controller * @param RoleService $roleService * @param FormFactory $formFactory * @param SubmitHandler $submitHandler + * @param int $defaultPaginationLimit */ public function __construct( NotificationHandlerInterface $notificationHandler, TranslatorInterface $translator, RoleService $roleService, FormFactory $formFactory, - SubmitHandler $submitHandler + SubmitHandler $submitHandler, + int $defaultPaginationLimit ) { $this->notificationHandler = $notificationHandler; $this->translator = $translator; $this->roleService = $roleService; $this->formFactory = $formFactory; $this->submitHandler = $submitHandler; + $this->defaultPaginationLimit = $defaultPaginationLimit; } - public function listAction(Role $role): Response + /** + * @param Role $role + * @param string $routeName + * @param int $assignmentPage + * + * @return Response + */ + public function listAction(Role $role, string $routeName, int $assignmentPage = 1): Response { - $assignments = $this->roleService->getRoleAssignments($role); + $pagerfanta = new Pagerfanta( + new ArrayAdapter($this->roleService->getRoleAssignments($role)) + ); + + $pagerfanta->setMaxPerPage($this->defaultPaginationLimit); + $pagerfanta->setCurrentPage(min($assignmentPage, $pagerfanta->getNbPages())); + + /** @var RoleAssignment[] $assignments */ + $assignments = $pagerfanta->getCurrentPageResults(); $deleteRoleAssignmentsForm = $this->formFactory->deleteRoleAssignments( new RoleAssignmentsDeleteData($role, $this->getRoleAssignmentsNumbers($assignments)) @@ -80,7 +103,8 @@ public function listAction(Role $role): Response return $this->render('@EzPlatformAdminUi/admin/role_assignment/list.html.twig', [ 'role' => $role, 'form_role_assignments_delete' => $deleteRoleAssignmentsForm->createView(), - 'assignments' => $assignments, + 'pager' => $pagerfanta, + 'route_name' => $routeName, ]); } diff --git a/src/bundle/Controller/RoleController.php b/src/bundle/Controller/RoleController.php index 62b79b95b4..1cb4696092 100644 --- a/src/bundle/Controller/RoleController.php +++ b/src/bundle/Controller/RoleController.php @@ -8,6 +8,7 @@ namespace EzSystems\EzPlatformAdminUiBundle\Controller; +use eZ\Publish\API\Repository\Exceptions\UnauthorizedException as APIUnauthorizedException; use eZ\Publish\API\Repository\RoleService; use eZ\Publish\API\Repository\Values\User\Role; use EzSystems\EzPlatformAdminUi\Form\Data\Role\RoleCreateData; @@ -120,7 +121,17 @@ public function listAction(Request $request): Response ]); } - public function viewAction(Role $role): Response + /** + * @param Request $request + * @param Role $role + * @param int $policyPage + * @param int $assignmentPage + * + * @return Response + * + * @throws APIUnauthorizedException + */ + public function viewAction(Request $request, Role $role, int $policyPage = 1, int $assignmentPage = 1): Response { $deleteForm = $this->formFactory->deleteRole( new RoleDeleteData($role) @@ -132,6 +143,9 @@ public function viewAction(Role $role): Response 'role' => $role, 'assignments' => $assignments, 'delete_form' => $deleteForm->createView(), + 'route_name' => $request->get('_route'), + 'policyPage' => $policyPage, + 'assignmentPage' => $assignmentPage, ]); } diff --git a/src/bundle/DependencyInjection/Configuration/Parser/Pagination.php b/src/bundle/DependencyInjection/Configuration/Parser/Pagination.php index 9c77e8d275..652273e244 100644 --- a/src/bundle/DependencyInjection/Configuration/Parser/Pagination.php +++ b/src/bundle/DependencyInjection/Configuration/Parser/Pagination.php @@ -45,6 +45,8 @@ public function addSemanticConfig(NodeBuilder $nodeBuilder) ->scalarNode('section_limit')->isRequired()->end() ->scalarNode('language_limit')->isRequired()->end() ->scalarNode('role_limit')->isRequired()->end() + ->scalarNode('role_assignment_limit')->isRequired()->end() + ->scalarNode('policy_limit')->isRequired()->end() ->scalarNode('content_type_group_limit')->isRequired()->end() ->scalarNode('content_type_limit')->isRequired()->end() ->end() @@ -67,6 +69,8 @@ public function mapConfig(array &$scopeSettings, $currentScope, ContextualizerIn 'section_limit', 'language_limit', 'role_limit', + 'role_assignment_limit', + 'policy_limit', 'content_type_group_limit', 'content_type_limit', ]; diff --git a/src/bundle/Resources/config/ezplatform_default_settings.yml b/src/bundle/Resources/config/ezplatform_default_settings.yml index a742605927..31b0c854a3 100644 --- a/src/bundle/Resources/config/ezplatform_default_settings.yml +++ b/src/bundle/Resources/config/ezplatform_default_settings.yml @@ -12,6 +12,8 @@ parameters: ezsettings.default.pagination.role_limit: 10 ezsettings.default.pagination.content_type_group_limit: 10 ezsettings.default.pagination.content_type_limit: 10 + ezsettings.default.pagination.role_assignment_limit: 10 + ezsettings.default.pagination.policy_limit: 10 # Subitems Module ezsettings.default.subitems_module.limit: 10 diff --git a/src/bundle/Resources/config/routing.yml b/src/bundle/Resources/config/routing.yml index 5f22ad7f6e..932e06fe97 100644 --- a/src/bundle/Resources/config/routing.yml +++ b/src/bundle/Resources/config/routing.yml @@ -123,12 +123,16 @@ ezplatform.role.list: _controller: 'EzPlatformAdminUiBundle:Role:list' ezplatform.role.view: - path: /role/{roleId} + path: /role/{roleId}/{policyPage}/{assignmentPage} methods: ['GET'] defaults: _controller: 'EzPlatformAdminUiBundle:Role:view' + policyPage: 1 + assignmentPage: 1 requirements: roleId: \d+ + policyPage: \d+ + assignmentPage: \d+ ezplatform.role.create: path: /role/create diff --git a/src/bundle/Resources/config/services/controllers.yml b/src/bundle/Resources/config/services/controllers.yml index 8c5c8bb05e..54e9dfe6f6 100644 --- a/src/bundle/Resources/config/services/controllers.yml +++ b/src/bundle/Resources/config/services/controllers.yml @@ -56,3 +56,13 @@ services: public: true arguments: $defaultPaginationLimit: '$pagination.role_limit$' + + EzSystems\EzPlatformAdminUiBundle\Controller\RoleAssignmentController: + public: true + arguments: + $defaultPaginationLimit: '$pagination.role_assignment_limit$' + + EzSystems\EzPlatformAdminUiBundle\Controller\PolicyController: + public: true + arguments: + $defaultPaginationLimit: '$pagination.policy_limit$' diff --git a/src/bundle/Resources/views/admin/policy/list.html.twig b/src/bundle/Resources/views/admin/policy/list.html.twig index 247fa132ce..88f670c7d6 100644 --- a/src/bundle/Resources/views/admin/policy/list.html.twig +++ b/src/bundle/Resources/views/admin/policy/list.html.twig @@ -44,7 +44,7 @@ - {% for policy in role.policies %} + {% for policy in pager.currentPageResults %} {{ form_widget(form_policies_delete.policies[policy.id]) }} {{ policy.module|capitalize }} @@ -79,4 +79,21 @@ {{ form_end(form_policies_delete) }} + + {% if pager.haveToPaginate %} +
+ + {{ 'pagination.viewing'|trans({ + '%viewing%': pager.currentPageResults|length, + '%total%': pager.nbResults}, 'pagination')|desc('Viewing %viewing% out of %total% items')|raw }} + +
+
+ {{ pagerfanta(pager, 'ez',{ + 'routeName': route_name, + 'routeParams': {'_fragment': 'policies', 'roleId': role.id}, + 'pageParameter': '[policyPage]', + }) }} +
+ {% endif %} diff --git a/src/bundle/Resources/views/admin/role/view.html.twig b/src/bundle/Resources/views/admin/role/view.html.twig index 2d26a82b46..e7222a6f44 100644 --- a/src/bundle/Resources/views/admin/role/view.html.twig +++ b/src/bundle/Resources/views/admin/role/view.html.twig @@ -18,7 +18,7 @@ iconName: 'roles' } %} -