Skip to content
Snippets Groups Projects
Commit 5745e62e authored by David Beniamine's avatar David Beniamine
Browse files

Merge branch '5-filter-by-group-sur-toutes-les-pages' into 'RC-Rekall-v1.2'

Resolve "Filter by group n’apparait que sur la première page de la liste des capsules"

See merge request !100
parents d2d7b2fa 7e6a8bc5
No related branches found
No related tags found
1 merge request!100Resolve "Filter by group n’apparait que sur la première page de la liste des capsules"
...@@ -13,6 +13,7 @@ use App\Repository\CapsuleRepository; ...@@ -13,6 +13,7 @@ use App\Repository\CapsuleRepository;
use App\Builder\CapsuleBuilder; use App\Builder\CapsuleBuilder;
use App\Form\CreateCapsuleFormType; use App\Form\CreateCapsuleFormType;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
use Knp\Component\Pager\PaginatorInterface; use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
...@@ -75,29 +76,53 @@ class CapsuleController extends AbstractController ...@@ -75,29 +76,53 @@ class CapsuleController extends AbstractController
$all_capsules = $current_user->getCapsules()->toArray(); $all_capsules = $current_user->getCapsules()->toArray();
$groups = $this->prependGroupAllToUserGroups($current_user); $groups = $this->prependGroupAllToUserGroups($current_user);
$form = $this->createForm(FilterByGroupFormType::class, ['groups' => $groups]); $group_id = $request->query->getInt('group', Group::$GROUP_ALL_ID);
$form->handleRequest($request); $group = $this->getGroupByIdOrNull($groups, $group_id, $groups[0]);
if ($form->isSubmitted() && $form->isValid()) { $filter_by_group_form = $this->createForm(
$group = $form->getData()['name']; FilterByGroupFormType::class,
['groups' => $groups, 'method' => 'GET', 'selected_group' => $group]
);
$filter_by_group_form->handleRequest($request);
if ($filter_by_group_form->isSubmitted() && $filter_by_group_form->isValid()) {
$group = $filter_by_group_form->getData()['name'];
if (! $group instanceof Group) { if (! $group instanceof Group) {
throw new \Exception('Group not found'); throw new \Exception('Group not found');
} }
}
if ($group->getId() !== Group::$GROUP_ALL_ID) { if ($group instanceof Group && $group->getId() !== Group::$GROUP_ALL_ID) {
$all_capsules = $group->getCapsules()->toArray(); $all_capsules = $group->getCapsules()->toArray();
} }
}
$nb_elements_per_page = 5;
// Fix the current page.
// For example, the user is on the page 5 without filter and apply a filter that reduce the number of pages.
// We display the last page.
$current_page = min(
[
intval((count($all_capsules) / $nb_elements_per_page) + 1),
$request->query->getInt('page', 1)
]
);
$request->query->set('page', $current_page);
// Pass group param to the paginator (it will be sent as GET data)
$capsules = $paginator->paginate( $capsules = $paginator->paginate(
$all_capsules, $all_capsules,
$request->query->getInt('page', 1), $current_page,
5 $nb_elements_per_page
); );
if ($capsules instanceof SlidingPagination) {
$capsules->setParam('group', $group->getId());
}
return $this->renderForm('capsule/index.html.twig', [ return $this->renderForm('capsule/index.html.twig', [
'filterByGroupForm' => $form, 'filterByGroupForm' => $filter_by_group_form,
'capsules' => $capsules, 'capsules' => $capsules,
'current_user' => $current_user, 'current_user' => $current_user,
'legacy_url' => $this->getParameter('app.legacy_external_prefix'), 'legacy_url' => $this->getParameter('app.legacy_external_prefix'),
...@@ -371,4 +396,25 @@ class CapsuleController extends AbstractController ...@@ -371,4 +396,25 @@ class CapsuleController extends AbstractController
return $groups; return $groups;
} }
/**
* @param array<Group> $groups
* @param int|null $id
* @param Group $defaultGroup The returned group if current groups isn't found
* @return Group
*/
private function getGroupByIdOrNull(array $groups, ?int $id, Group $defaultGroup): Group
{
if (null === $id) {
return $defaultGroup;
}
foreach ($groups as $g) {
if ($g->getId() === $id) {
return $g;
}
}
return $defaultGroup;
}
} }
...@@ -15,7 +15,7 @@ class FilterByGroupFormType extends AbstractType ...@@ -15,7 +15,7 @@ class FilterByGroupFormType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options): void public function buildForm(FormBuilderInterface $builder, array $options): void
{ {
$groups = $options['data']['groups']; $groups = $options['data']['groups'];
$selected_group = $options['data']['selected_group'];
$builder $builder
->add('name', ChoiceType::class, [ ->add('name', ChoiceType::class, [
'choices' => $groups, 'choices' => $groups,
...@@ -24,9 +24,10 @@ class FilterByGroupFormType extends AbstractType ...@@ -24,9 +24,10 @@ class FilterByGroupFormType extends AbstractType
'choice_label' => function (?Group $entity) { 'choice_label' => function (?Group $entity) {
return $entity ? $entity->getName() : ''; return $entity ? $entity->getName() : '';
}, },
'choice_value' => 'name', 'choice_value' => 'id',
'choice_name' => 'name', 'choice_name' => 'id',
'expanded' => false 'expanded' => false,
'data' => $selected_group
]); ]);
} }
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
{{ 'capsule.title'|trans }} {{ 'capsule.title'|trans }}
</h2> </h2>
{% if app.request.query.get('page')|default(1) == 1 %}
{% if current_user.getGroups()|length > 1 %} {% if current_user.getGroups()|length > 1 %}
{{ form_start(filterByGroupForm, {'attr': {novalidate: 'novalidate', 'class': 'd-flex flex-column flex-nowrap'}}) }} {{ form_start(filterByGroupForm, {'attr': {novalidate: 'novalidate', 'class': 'd-flex flex-column flex-nowrap'}}) }}
{{ form_widget(filterByGroupForm._token) }} {{ form_widget(filterByGroupForm._token) }}
...@@ -23,7 +22,6 @@ ...@@ -23,7 +22,6 @@
</div> </div>
{{ form_end(filterByGroupForm) }} {{ form_end(filterByGroupForm) }}
{% endif %} {% endif %}
{% endif %}
<form class="d-flex mb-4 mb-lg-0"> <form class="d-flex mb-4 mb-lg-0">
<button class="btn btn-orange text-uppercase" formaction="{{ path('create_capsule') }}"> <button class="btn btn-orange text-uppercase" formaction="{{ path('create_capsule') }}">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment