From 19c407f2967786bb106ebae636055b30af900e7b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nicolas=20L=C5=93uillet?= Date: Sun, 12 Apr 2020 18:42:31 +0200 Subject: [PATCH] First draft for EntrySortType --- .../CoreBundle/Controller/EntryController.php | 14 +++-- .../CoreBundle/Form/Type/EntrySortType.php | 53 +++++++++++++++++++ .../CoreBundle/Resources/config/services.yml | 7 +++ .../themes/material/Entry/entries.html.twig | 42 +++++++-------- 4 files changed, 90 insertions(+), 26 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Form/Type/EntrySortType.php diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index 5157653c..85e71116 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php @@ -15,6 +15,7 @@ use Wallabag\CoreBundle\Event\EntryDeletedEvent; use Wallabag\CoreBundle\Event\EntrySavedEvent; use Wallabag\CoreBundle\Form\Type\EditEntryType; use Wallabag\CoreBundle\Form\Type\EntryFilterType; +use Wallabag\CoreBundle\Form\Type\EntrySortType; use Wallabag\CoreBundle\Form\Type\NewEntryType; use Wallabag\CoreBundle\Form\Type\SearchEntryType; @@ -535,11 +536,11 @@ class EntryController extends Controller // defined as null by default because each repository method have the right field as default value too // like `getBuilderForStarredByUser` will have `starredAt` sort by default $sortBy = null; - if (\in_array($request->get('sort', 'createdAt'), ['id', 'title', 'createdAt', 'updatedAt', 'starredAt', 'archivedAt'], true)) { - $sortBy = $request->get('sort', null); + if (\in_array($request->get('entry_sort')['sortType'], ['id', 'title', 'createdAt', 'updatedAt', 'starredAt', 'archivedAt'], true)) { + $sortBy = $request->get('entry_sort')['sortType']; } - $direction = 'DESC' === $request->get('direction') ? 'DESC' : 'ASC'; + $direction = isset($request->get('entry_sort')['sortOrder']) ? 'DESC' : 'ASC'; switch ($type) { case 'search': @@ -565,6 +566,7 @@ class EntryController extends Controller } $form = $this->createForm(EntryFilterType::class); + $sortForm = $this->createForm(EntrySortType::class); if ($request->query->has($form->getName())) { // manually bind values from the request @@ -574,6 +576,11 @@ class EntryController extends Controller $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); } + if ($request->query->has($sortForm->getName())) { + // manually bind values from the request + $sortForm->submit($request->query->get($sortForm->getName())); + } + $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false); $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare($pagerAdapter); @@ -592,6 +599,7 @@ class EntryController extends Controller return $this->render( 'WallabagCoreBundle:Entry:entries.html.twig', [ 'form' => $form->createView(), + 'sortForm' => $sortForm->createView(), 'entries' => $entries, 'currentPage' => $page, 'searchTerm' => $searchTerm, diff --git a/src/Wallabag/CoreBundle/Form/Type/EntrySortType.php b/src/Wallabag/CoreBundle/Form/Type/EntrySortType.php new file mode 100644 index 00000000..2555f68d --- /dev/null +++ b/src/Wallabag/CoreBundle/Form/Type/EntrySortType.php @@ -0,0 +1,53 @@ +user = $tokenStorage->getToken() ? $tokenStorage->getToken()->getUser() : null; + + if (null === $this->user || !\is_object($this->user)) { + return; + } + } + + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder + ->add('sortOrder', CheckboxFilterType::class) + ->add('sortType', ChoiceFilterType::class, [ + 'choices' => [ + 'createdAt' => 'createdAt', + 'title' => 'title', + 'updatedAt' => 'updatedAt', + ], + 'label' => 'entry.sort.status_label', + ]) + ; + } + + public function getBlockPrefix() + { + return 'entry_sort'; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'csrf_protection' => false, + 'validation_groups' => ['sortering'], + ]); + } +} diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index 8417ac35..8867ec0c 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml @@ -22,6 +22,13 @@ services: tags: - { name: form.type } + wallabag_core.sort.type.entry: + class: Wallabag\CoreBundle\Form\Type\EntrySortType + arguments: + - "@security.token_storage" + tags: + - { name: form.type } + wallabag_core.param_converter.username_feed_token_converter: class: Wallabag\CoreBundle\ParamConverter\UsernameFeedTokenConverter tags: diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig index e6497893..7706896f 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig @@ -209,36 +209,32 @@ {% endif %} + {% if sortForm is not null %}
+

{{ 'entry.sort.title'|trans }}

-
-
- -
+
+
+ {{ form_widget(sortForm.sortType) }} + {{ form_label(sortForm.sortType) }} +
-
- -
+
+
+ +
+
-
-
- +
+
-
+
- - {# {% include "WallabagCoreBundle:Entry:pager.html.twig" with {'entries': entries} %} #} + {% endif %} {% endblock %} -- 2.41.0