Skip to content

Commit

Permalink
feat(pagination): add pagination to list controllers
Browse files Browse the repository at this point in the history
fix merge conflicts

composer update to fix composer synchro between lock and json
  • Loading branch information
olivmai committed Jun 9, 2020
1 parent 1a23e70 commit 551bcc2
Show file tree
Hide file tree
Showing 26 changed files with 1,076 additions and 1,616 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"beberlei/assert": "^3.2",
"doctrine/annotations": "^1.8",
"doctrine/doctrine-fixtures-bundle": "^3.3",
"knplabs/knp-paginator-bundle": "^5.2",
"friendsofsymfony/jsrouting-bundle": "^2.5",
"martin-georgiev/postgresql-for-doctrine": "^1.3",
"odolbeau/phone-number-bundle": "^3.0",
Expand Down
2,472 changes: 889 additions & 1,583 deletions composer.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle::class => ['dev' => true, 'test' => true, 'panther' => true],
Hautelook\AliceBundle\HautelookAliceBundle::class => ['dev' => true, 'test' => true, 'panther' => true],
Misd\PhoneNumberBundle\MisdPhoneNumberBundle::class => ['all' => true],
Knp\Bundle\PaginatorBundle\KnpPaginatorBundle::class => ['all' => true],
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true],
FOS\JsRoutingBundle\FOSJsRoutingBundle::class => ['all' => true],
];
3 changes: 3 additions & 0 deletions config/packages/knp_paginator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
knp_paginator:
template:
pagination: '@KnpPaginator/Pagination/twitter_bootstrap_v4_pagination.html.twig'
1 change: 1 addition & 0 deletions config/parameters.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
app.pagination_default_limit: 30
app.most_important_skills_limit: 6 # used for separing data in the planning
app.most_important_skills_to_display: [ch_vpsp, ch_vl] # used for displaying a badge near the user name
app.slot_interval: '%env(APP_SLOT_INTERVAL)%'
Expand Down
59 changes: 59 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
app.pagination_default_limit: 30
app.most_important_skills_limit: 6 # used for separing data in the planning
app.most_important_skills_to_display: [ch_vpsp, ch_vl] # used for displaying a badge near the user name
app.slot_interval: '%env(APP_SLOT_INTERVAL)%'
app.available_skills:
# Caution: the order matters for the planning display
ci_bspp:
label: CI Réseau BSPP
includes: [ci_samu]
ci_samu:
label: CI Réseau Samu
includes: [ci]
ch_vpsp:
label: Chauffeur VPSP
includes: [pse2, ch_vl]
ci:
label: CI
includes: [pse2]
pse2:
label: PSE2
includes: [pse1]
pse1:
label: PSE1
includes: [psc1]
ch_vl:
label: Chauffeur VL
includes: [benevole]
infirmier:
label: Infirmier.e local.e
includes: [benevole]
ce_maraude:
label: Chef.fe d'équipe maraudes
includes: [solidar]
solidar:
label: Solidar
includes: [maraudeur]
maraudeur:
label: Maraudeur.se
includes: [benevole]
tsa_coreg:
label: TSA / Coreg
includes: [pse2]
logisticien:
label: Logisticien.ne Croix-Rouge
includes: [benevole]
resp_perm_soc:
label: Responsable Permanence Sociale
includes: [benevole]
anim_fle:
label: Animateur.rice de cours de FLE
includes: [benevole]
psc1:
label: PSC1
includes: [benevole]
benevole:
label: Bénévole

services:
# default configuration for services in *this* file
Expand Down
15 changes: 12 additions & 3 deletions src/Controller/Organization/AssetType/AssetTypeListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use App\Entity\Organization;
use App\Repository\AssetTypeRepository;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

Expand All @@ -18,19 +20,26 @@
class AssetTypeListController extends AbstractController
{
private AssetTypeRepository $assetTypeRepository;
private PaginatorInterface $paginator;

public function __construct(AssetTypeRepository $assetTypeRepository)
public function __construct(AssetTypeRepository $assetTypeRepository, PaginatorInterface $paginator)
{
$this->assetTypeRepository = $assetTypeRepository;
$this->paginator = $paginator;
}

public function __invoke(): Response
public function __invoke(Request $request): Response
{
/** @var Organization $organization */
$organization = $this->getUser();
$assetTypes = $this->paginator->paginate(
$this->assetTypeRepository->findByOrganization($organization),
$request->query->getInt('page', 1),
$this->getParameter('app.pagination_default_limit')
);

return $this->render('organization/assetType/list.html.twig', [
'assetTypes' => $this->assetTypeRepository->findByOrganization($organization),
'assetTypes' => $assetTypes,
]);
}
}
14 changes: 11 additions & 3 deletions src/Controller/Organization/Children/ListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use App\Entity\Organization;
use App\Repository\OrganizationRepository;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
Expand All @@ -17,20 +19,26 @@
class ListController extends AbstractController
{
protected OrganizationRepository $organizationRepository;
private PaginatorInterface $paginator;

public function __construct(OrganizationRepository $organizationRepository)
public function __construct(OrganizationRepository $organizationRepository, PaginatorInterface $paginator)
{
$this->organizationRepository = $organizationRepository;
$this->paginator = $paginator;
}

public function __invoke(): Response
public function __invoke(Request $request): Response
{
$organization = $this->getUser();
if (!$organization instanceof Organization || !$organization->isParent()) {
throw new AccessDeniedException();
}

$organizations = $this->organizationRepository->findBy(['parent' => $organization], ['name' => 'ASC']);
$organizations = $this->paginator->paginate(
$this->organizationRepository->findChildrenQueryBuilder($organization),
$request->query->getInt('page', 1),
$this->getParameter('app.pagination_default_limit')
);

return $this->render('organization/list.html.twig', [
'organizations' => $organizations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Form\Factory\OrganizationSelectorFormFactory;
use App\Repository\CommissionableAssetRepository;
use App\Security\Voter\OrganizationVoter;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -22,23 +23,30 @@ class AssetsListController extends AbstractController
{
private CommissionableAssetRepository $assetRepository;
private OrganizationSelectorFormFactory $organizationSelectorFormFactory;
private PaginatorInterface $paginator;

public function __construct(CommissionableAssetRepository $assetRepository, OrganizationSelectorFormFactory $organizationSelectorFormFactory)
public function __construct(CommissionableAssetRepository $assetRepository, OrganizationSelectorFormFactory $organizationSelectorFormFactory, PaginatorInterface $paginator)
{
$this->assetRepository = $assetRepository;
$this->organizationSelectorFormFactory = $organizationSelectorFormFactory;
$this->paginator = $paginator;
}

public function __invoke(Request $request, Organization $organization): Response
{
/** @var Organization $currentOrganization */
$currentOrganization = $this->getUser();
$assets = $this->paginator->paginate(
$this->assetRepository->findByOrganizationAndChildrenQb($organization),
$request->query->getInt('page', 1),
$this->getParameter('app.pagination_default_limit')
);

return $this->render(
'organization/commissionable_asset/list.html.twig',
[
'organization' => $organization,
'assets' => $this->assetRepository->findByOrganization($organization),
'assets' => $assets,
'organization_selector_form' => $this->organizationSelectorFormFactory->createForm(
$organization,
$currentOrganization
Expand Down
23 changes: 18 additions & 5 deletions src/Controller/Organization/Mission/MissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Form\Type\MissionsSearchType;
use App\Form\Type\MissionType;
use App\Repository\MissionRepository;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -27,40 +28,52 @@ class MissionController extends AbstractOrganizationController
{
private MissionRepository $missionRepository;
private PlanningDomain $planningDomain;
private PaginatorInterface $paginator;

public function __construct(MissionRepository $missionRepository, PlanningDomain $planningDomain)
public function __construct(MissionRepository $missionRepository, PlanningDomain $planningDomain, PaginatorInterface $paginator)
{
$this->missionRepository = $missionRepository;
$this->planningDomain = $planningDomain;
$this->paginator = $paginator;
}

/**
* @Route(name="app_organization_mission_index", methods={"GET"})
*/
public function index(): Response
public function index(Request $request): Response
{
$form = $this->planningDomain->generateForm(MissionsSearchType::class);
$filters = $form->getData();
$missions = $this->paginator->paginate(
$this->missionRepository->findByFiltersQb($filters),
$request->query->getInt('page', 1),
$this->getParameter('app.pagination_default_limit')
);

return $this->render('organization/mission/index.html.twig', [
'filters' => $filters,
'form' => $form->createView(),
'missions' => $this->missionRepository->findByFilters($filters),
'missions' => $missions,
]);
}

/**
* @Route("/full", name="app_organization_mission_full_list", methods={"GET"})
*/
public function fullList(): Response
public function fullList(Request $request): Response
{
$form = $this->planningDomain->generateForm(MissionsSearchType::class);
$filters = $form->getData();
$missions = $this->paginator->paginate(
$this->missionRepository->findByFiltersQb($filters),
$request->query->getInt('page', 1),
$this->getParameter('app.pagination_default_limit')
);

return $this->render('organization/mission/list_full.html.twig', [
'filters' => $filters,
'form' => $form->createView(),
'missions' => $this->missionRepository->findByFilters($filters),
'missions' => $missions,
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Entity\Organization;
use App\Form\Type\MissionTypeType;
use App\Repository\MissionTypeRepository;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -30,11 +31,15 @@ public function __construct(MissionTypeRepository $missionTypeRepository)
/**
* @Route(name="app_organization_mission_type_index", methods={"GET"})
*/
public function index(): Response
public function index(Request $request, PaginatorInterface $paginator): Response
{
/** @var Organization $organization */
$organization = $this->getUser();
$missionTypes = $this->missionTypeRepository->findByOrganization($organization);
$missionTypes = $paginator->paginate(
$this->missionTypeRepository->findByOrganizationQb($organization),
$request->query->getInt('page', 1),
$this->getParameter('app.pagination_default_limit')
);

return $this->render('organization/mission_type/index.html.twig', [
'mission_types' => $missionTypes,
Expand Down
12 changes: 10 additions & 2 deletions src/Controller/Organization/User/UserListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Repository\OrganizationRepository;
use App\Repository\UserRepository;
use App\Security\Voter\OrganizationVoter;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -24,24 +25,31 @@ class UserListController extends AbstractController
protected UserRepository $userRepository;
protected OrganizationRepository $organizationRepository;
private OrganizationSelectorFormFactory $organizationSelectorFormFactory;
private PaginatorInterface $paginator;

public function __construct(OrganizationRepository $organizationRepository, UserRepository $userRepository, OrganizationSelectorFormFactory $organizationSelectorFormFactory)
public function __construct(OrganizationRepository $organizationRepository, UserRepository $userRepository, OrganizationSelectorFormFactory $organizationSelectorFormFactory, PaginatorInterface $paginator)
{
$this->userRepository = $userRepository;
$this->organizationRepository = $organizationRepository;
$this->organizationSelectorFormFactory = $organizationSelectorFormFactory;
$this->paginator = $paginator;
}

public function __invoke(Request $request, Organization $organization): Response
{
/** @var Organization $currentOrganization */
$currentOrganization = $this->getUser();
$users = $this->paginator->paginate(
$this->userRepository->findByOrganizationAndChildrenQb($organization),
$request->query->getInt('page', 1),
$this->getParameter('app.pagination_default_limit')
);

return $this->render(
'organization/user/list.html.twig',
[
'organization' => $organization,
'users' => $this->userRepository->findByOrganization($organization),
'users' => $users,
'organization_selector_form' => $this->organizationSelectorFormFactory->createForm(
$organization,
$currentOrganization,
Expand Down
8 changes: 5 additions & 3 deletions src/Repository/MissionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ public function findByOrganizationQb(Organization $organization): QueryBuilder

public function findByFilters(array $filters): array
{
return $this->findByFiltersQb($filters)->getQuery()->getResult();
$qb = $this->findByFiltersQb($filters);
$qb->setMaxResults(100);

return $qb->getQuery()->getResult();
}

public function findByFiltersQb(array $filters): QueryBuilder
Expand All @@ -56,8 +59,7 @@ public function findByFiltersQb(array $filters): QueryBuilder
$qb->setParameter('types', $filters['missionTypes']);
}

$qb->orderBy('m.startTime', 'ASC');
$qb->setMaxResults(100); // TODO Paginate
$qb->orderBy('m.startTime', 'DESC');

return $qb;
}
Expand Down
12 changes: 12 additions & 0 deletions src/Repository/OrganizationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ public function findByParentQueryBuilder(Organization $organization): QueryBuild
return $qb;
}

public function findChildrenQueryBuilder(Organization $organization): QueryBuilder
{
$qb = $this->createQueryBuilder('o');

$qb
->where('o.parent = :orga')
->setParameter('orga', $organization)
->addOrderBy('o.name', 'ASC');

return $qb;
}

public function findByIdOrParentIdQueryBuilder(int $organizationId, QueryBuilder $qb = null): QueryBuilder
{
$alias = 'o';
Expand Down
Loading

0 comments on commit 551bcc2

Please sign in to comment.