diff --git a/src/Controller/EntryController.php b/src/Controller/EntryController.php index ca34e0dd1..bee6dad15 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', 'url', 'readingTime'], 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..a3c65e20b 100644 --- a/src/Form/Type/EntryFilterType.php +++ b/src/Form/Type/EntryFilterType.php @@ -205,6 +205,24 @@ 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', + 'entry.sort.by.url' => 'url', + 'entry.sort.by.reading_time' => 'readingTime', + ], + '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..b43d38b15 100644 --- a/translations/messages.en.yml +++ b/translations/messages.en.yml @@ -237,6 +237,16 @@ 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 + url: URL + reading_time: Reading time + 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