From 7e8a3c432fdc7e54a8d6bebcde557ac515141317 Mon Sep 17 00:00:00 2001 From: Kevin Decherf Date: Sat, 20 Feb 2021 23:15:46 +0100 Subject: [PATCH 1/2] Add sorting function to filter view Fixes #2178 Signed-off-by: Kevin Decherf --- src/Controller/EntryController.php | 12 +++++++++++- src/Form/Type/EntryFilterType.php | 16 ++++++++++++++++ src/Repository/EntryRepository.php | 5 +++-- templates/Entry/entries.html.twig | 16 ++++++++++++++++ translations/messages.en.yml | 8 ++++++++ 5 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/Controller/EntryController.php b/src/Controller/EntryController.php index ca34e0dd1..19186e51a 100644 --- a/src/Controller/EntryController.php +++ b/src/Controller/EntryController.php @@ -627,6 +627,16 @@ class EntryController extends AbstractController $currentEntryId = $request->attributes->getInt('id'); $formOptions = []; + $direction = 'desc'; + $sortBy = null; + + if (null !== $request->get('entry_filter') && null !== $request->get('entry_filter')['sortType'] && '' !== $request->get('entry_filter')['sortType']) { + $direction = (null !== $request->get('entry_filter')['sortOrder'] && \in_array($request->get('entry_filter')['sortOrder'], ['asc', 'desc'], true)) ? $request->get('entry_filter')['sortOrder'] : 'desc'; + + if (\in_array($request->get('entry_filter')['sortType'], ['id','title','createdAt'], true)) { + $sortBy = $request->get('entry_filter')['sortType']; + } + } switch ($type) { case 'search': @@ -654,7 +664,7 @@ class EntryController extends AbstractController $qb = $this->entryRepository->getBuilderForSameDomainByUser($this->getUser()->getId(), $currentEntryId); break; case 'all': - $qb = $this->entryRepository->getBuilderForAllByUser($this->getUser()->getId()); + $qb = $this->entryRepository->getBuilderForAllByUser($this->getUser()->getId(), $sortBy, $direction); break; default: throw new \InvalidArgumentException(\sprintf('Type "%s" is not implemented.', $type)); diff --git a/src/Form/Type/EntryFilterType.php b/src/Form/Type/EntryFilterType.php index f4e9b76f0..299668547 100644 --- a/src/Form/Type/EntryFilterType.php +++ b/src/Form/Type/EntryFilterType.php @@ -205,6 +205,22 @@ class EntryFilterType extends AbstractType 'choices' => array_flip($this->repository->findDistinctLanguageByUser($user->getId())), 'label' => 'entry.filters.language_label', ]) + ->add('sortOrder', ChoiceFilterType::class, [ + 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) { }, + 'choices' => [ + 'entry.sort.ascending' => 'asc', + 'entry.sort.descending' => 'desc', + ], + 'label' => 'entry.sort.order_label', + ]) + ->add('sortType', ChoiceFilterType::class, [ + 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) { }, + 'choices' => [ + 'entry.sort.by.creation_date' => 'createdAt', + 'entry.sort.by.title' => 'title', + ], + 'label' => 'entry.sort.status_label', + ]) ; } diff --git a/src/Repository/EntryRepository.php b/src/Repository/EntryRepository.php index b60bafe84..d74b8694b 100644 --- a/src/Repository/EntryRepository.php +++ b/src/Repository/EntryRepository.php @@ -30,10 +30,11 @@ class EntryRepository extends ServiceEntityRepository * * @return QueryBuilder */ - public function getBuilderForAllByUser($userId) + public function getBuilderForAllByUser($userId, $sortBy = 'createdAt', $direction = 'desc') { + $sortBy = $sortBy ?: 'createdAt'; return $this - ->getSortedQueryBuilderByUser($userId) + ->getSortedQueryBuilderByUser($userId, $sortBy, $direction) ; } diff --git a/templates/Entry/entries.html.twig b/templates/Entry/entries.html.twig index 0b7daefaa..2955faae5 100644 --- a/templates/Entry/entries.html.twig +++ b/templates/Entry/entries.html.twig @@ -276,6 +276,22 @@ +
+ {{ form_label(form.sortType) }} +
+ +
+ {{ form_label(form.sortOrder) }} +
+ +
+ {{ form_widget(form.sortType) }} +
+ +
+ {{ form_widget(form.sortOrder) }} +
+
diff --git a/translations/messages.en.yml b/translations/messages.en.yml index fcf566acf..dcea7893f 100644 --- a/translations/messages.en.yml +++ b/translations/messages.en.yml @@ -237,6 +237,14 @@ entry: untagged: Untagged entries all: All entries same_domain: Same domain + sort: + status_label: Sort by + order_label: Order + by: + creation_date: Creation date + title: Title + ascending: Ascending + descending: Descending list: number_on_the_page: '{0} There are no entries.|{1} There is one entry.|]1,Inf[ There are %count% entries.' reading_time: estimated reading time From 5981cfb8b89829e6d235df4e8a2acd0de8d73e18 Mon Sep 17 00:00:00 2001 From: Kevin Decherf Date: Sat, 28 Jan 2023 22:42:43 +0100 Subject: [PATCH 2/2] Support sorting by reading time and URL in filter view Signed-off-by: Kevin Decherf --- src/Controller/EntryController.php | 2 +- src/Form/Type/EntryFilterType.php | 2 ++ translations/messages.en.yml | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Controller/EntryController.php b/src/Controller/EntryController.php index 19186e51a..bee6dad15 100644 --- a/src/Controller/EntryController.php +++ b/src/Controller/EntryController.php @@ -633,7 +633,7 @@ class EntryController extends AbstractController if (null !== $request->get('entry_filter') && null !== $request->get('entry_filter')['sortType'] && '' !== $request->get('entry_filter')['sortType']) { $direction = (null !== $request->get('entry_filter')['sortOrder'] && \in_array($request->get('entry_filter')['sortOrder'], ['asc', 'desc'], true)) ? $request->get('entry_filter')['sortOrder'] : 'desc'; - if (\in_array($request->get('entry_filter')['sortType'], ['id','title','createdAt'], true)) { + if (\in_array($request->get('entry_filter')['sortType'], ['id','title','createdAt', 'url', 'readingTime'], true)) { $sortBy = $request->get('entry_filter')['sortType']; } } diff --git a/src/Form/Type/EntryFilterType.php b/src/Form/Type/EntryFilterType.php index 299668547..a3c65e20b 100644 --- a/src/Form/Type/EntryFilterType.php +++ b/src/Form/Type/EntryFilterType.php @@ -218,6 +218,8 @@ class EntryFilterType extends AbstractType 'choices' => [ 'entry.sort.by.creation_date' => 'createdAt', 'entry.sort.by.title' => 'title', + 'entry.sort.by.url' => 'url', + 'entry.sort.by.reading_time' => 'readingTime', ], 'label' => 'entry.sort.status_label', ]) diff --git a/translations/messages.en.yml b/translations/messages.en.yml index dcea7893f..b43d38b15 100644 --- a/translations/messages.en.yml +++ b/translations/messages.en.yml @@ -243,6 +243,8 @@ entry: by: creation_date: Creation date title: Title + url: URL + reading_time: Reading time ascending: Ascending descending: Descending list: