From ee360905ce41c6f28c34419f18e50cd87ae387e5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 22 Dec 2017 15:44:00 +0100 Subject: [PATCH] Added random feature --- .../CoreBundle/Controller/EntryController.php | 105 ++++++++++++++++++ .../CoreBundle/Repository/EntryRepository.php | 46 +++++++- .../Resources/translations/messages.da.yml | 1 + .../Resources/translations/messages.de.yml | 1 + .../Resources/translations/messages.en.yml | 1 + .../Resources/translations/messages.es.yml | 1 + .../Resources/translations/messages.fa.yml | 1 + .../Resources/translations/messages.fr.yml | 1 + .../Resources/translations/messages.it.yml | 1 + .../Resources/translations/messages.oc.yml | 1 + .../Resources/translations/messages.pl.yml | 1 + .../Resources/translations/messages.pt.yml | 1 + .../Resources/translations/messages.ro.yml | 1 + .../Resources/translations/messages.ru.yml | 3 +- .../Resources/translations/messages.th.yml | 1 + .../Resources/translations/messages.tr.yml | 1 + .../themes/baggy/Entry/entries.html.twig | 7 +- .../themes/material/Entry/entries.html.twig | 7 +- 18 files changed, 170 insertions(+), 11 deletions(-) diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index ac372a33..6c843ba7 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php @@ -2,6 +2,7 @@ namespace Wallabag\CoreBundle\Controller; +use Doctrine\ORM\NoResultException; use Pagerfanta\Adapter\DoctrineORMAdapter; use Pagerfanta\Exception\OutOfRangeCurrentPageException; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; @@ -232,6 +233,110 @@ class EntryController extends Controller return $this->showEntries('starred', $request, $page); } + /** + * Shows random unread entry. + * + * @param Entry $entry + * + * @Route("/unread/random", name="unread_random") + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function showRandomUnreadEntryAction() + { + $repository = $this->get('wallabag_core.entry_repository'); + + try { + $entry = $repository->getRandomEntry($this->getUser()->getId(), 'unread'); + } catch (NoResultException $e) { + $bag = $this->get('session')->getFlashBag(); + $bag->clear(); + $bag->add('notice', 'flashes.entry.notice.no_random_entry'); + + return $this->redirect($this->generateUrl('homepage')); + } + + return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); + } + + /** + * Shows random favorite entry. + * + * @param Entry $entry + * + * @Route("/starred/random", name="starred_random") + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function showRandomStarredEntryAction() + { + $repository = $this->get('wallabag_core.entry_repository'); + + try { + $entry = $repository->getRandomEntry($this->getUser()->getId(), 'starred'); + } catch (NoResultException $e) { + $bag = $this->get('session')->getFlashBag(); + $bag->clear(); + $bag->add('notice', 'flashes.entry.notice.no_random_entry'); + + return $this->redirect($this->generateUrl('homepage')); + } + + return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); + } + + /** + * Shows random archived entry. + * + * @param Entry $entry + * + * @Route("/archive/random", name="archive_random") + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function showRandomArchiveEntryAction() + { + $repository = $this->get('wallabag_core.entry_repository'); + + try { + $entry = $repository->getRandomEntry($this->getUser()->getId(), 'starred'); + } catch (NoResultException $e) { + $bag = $this->get('session')->getFlashBag(); + $bag->clear(); + $bag->add('notice', 'flashes.entry.notice.no_random_entry'); + + return $this->redirect($this->generateUrl('homepage')); + } + + return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); + } + + /** + * Shows random all entry. + * + * @param Entry $entry + * + * @Route("/all/random", name="all_random") + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function showRandomAllEntryAction() + { + $repository = $this->get('wallabag_core.entry_repository'); + + try { + $entry = $repository->getRandomEntry($this->getUser()->getId()); + } catch (NoResultException $e) { + $bag = $this->get('session')->getFlashBag(); + $bag->clear(); + $bag->add('notice', 'flashes.entry.notice.no_random_entry'); + + return $this->redirect($this->generateUrl('homepage')); + } + + return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); + } + /** * Shows entry content. * diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 93c630c0..7fe090be 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -110,8 +110,7 @@ class EntryRepository extends EntityRepository */ public function getBuilderForUntaggedByUser($userId) { - return $this - ->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId)); + return $this->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId)); } /** @@ -428,6 +427,46 @@ class EntryRepository extends EntityRepository ->getResult(); } + /** + * Returns a random entry, filtering by status. + * + * @param $userId + * @param string $status can be unread, archive or starred + * + * @throws \Doctrine\ORM\NoResultException + * @throws \Doctrine\ORM\NonUniqueResultException + * + * @return Entry + */ + public function getRandomEntry($userId, $status = '') + { + $max = $this->getEntityManager() + ->createQuery('SELECT MAX(e.id) FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId') + ->setParameter('userId', $userId) + ->getSingleScalarResult(); + + $qb = $this->createQueryBuilder('e') + ->where('e.user = :user_id')->setParameter('user_id', $userId); + + if ('unread' === $status) { + $qb->andWhere('e.isArchived = false'); + } + + if ('archive' === $status) { + $qb->andWhere('e.isArchived = true'); + } + + if ('starred' === $status) { + $qb->andWhere('e.isStarred = true'); + } + + return $qb->andWhere('e.id >= :rand') + ->setParameter('rand', rand(0, $max)) + ->setMaxResults(1) + ->getQuery() + ->getSingleResult(); + } + /** * Return a query builder to be used by other getBuilderFor* method. * @@ -466,7 +505,6 @@ class EntryRepository extends EntityRepository */ private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') { - return $qb - ->orderBy(sprintf('e.%s', $sortBy), $direction); + return $qb->orderBy(sprintf('e.%s', $sortBy), $direction); } } diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.da.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.da.yml index e1384675..8049d625 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.da.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.da.yml @@ -582,6 +582,7 @@ flashes: entry_starred: 'Artikel markeret som favorit' entry_unstarred: 'Artikel ikke længere markeret som favorit' entry_deleted: 'Artikel slettet' + # no_random_entry: 'No article with these criterias was found' tag: notice: # tag_added: 'Tag added' diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.de.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.de.yml index c297ffb5..fcc0a415 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.de.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.de.yml @@ -582,6 +582,7 @@ flashes: entry_starred: 'Eintrag favorisiert' entry_unstarred: 'Eintrag defavorisiert' entry_deleted: 'Eintrag gelöscht' + # no_random_entry: 'No article with these criterias was found' tag: notice: tag_added: 'Tag hinzugefügt' diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml index bd81c72f..e7418ed2 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml @@ -582,6 +582,7 @@ flashes: entry_starred: 'Entry starred' entry_unstarred: 'Entry unstarred' entry_deleted: 'Entry deleted' + no_random_entry: 'No article with these criterias was found' tag: notice: tag_added: 'Tag added' diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.es.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.es.yml index 700190a6..7c97e2d8 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.es.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.es.yml @@ -582,6 +582,7 @@ flashes: entry_starred: 'Artículo marcado como favorito' entry_unstarred: 'Artículo desmarcado como favorito' entry_deleted: 'Artículo eliminado' + # no_random_entry: 'No article with these criterias was found' tag: notice: tag_added: 'Etiqueta añadida' diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.fa.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.fa.yml index 83645933..fc23f927 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.fa.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.fa.yml @@ -582,6 +582,7 @@ flashes: entry_starred: 'مقاله برگزیده شد' entry_unstarred: 'مقاله نابرگزیده شد' entry_deleted: 'مقاله پاک شد' + # no_random_entry: 'No article with these criterias was found' tag: notice: tag_added: 'برچسب افزوده شد' diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml index edf29654..1c5fb5c8 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml @@ -582,6 +582,7 @@ flashes: entry_starred: "Article ajouté dans les favoris" entry_unstarred: "Article retiré des favoris" entry_deleted: "Article supprimé" + no_random_entry: "Aucun article correspond aux critères n'a été trouvé" tag: notice: tag_added: "Tag ajouté" diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.it.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.it.yml index 47292116..7ea0b7ea 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.it.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.it.yml @@ -582,6 +582,7 @@ flashes: entry_starred: 'Contenuto segnato come preferito' entry_unstarred: 'Contenuto rimosso dai preferiti' entry_deleted: 'Contenuto eliminato' + # no_random_entry: 'No article with these criterias was found' tag: notice: tag_added: 'Etichetta aggiunta' diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.oc.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.oc.yml index 95bc9560..4fd74fd8 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.oc.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.oc.yml @@ -582,6 +582,7 @@ flashes: entry_starred: 'Article ajustat dins los favorits' entry_unstarred: 'Article quitat dels favorits' entry_deleted: 'Article suprimit' + # no_random_entry: 'No article with these criterias was found' tag: notice: tag_added: 'Etiqueta ajustada' diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.pl.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.pl.yml index a64e60b0..37b31aab 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.pl.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.pl.yml @@ -582,6 +582,7 @@ flashes: entry_starred: 'Wpis oznaczony gwiazdką' entry_unstarred: 'Wpis odznaczony gwiazdką' entry_deleted: 'Wpis usunięty' + # no_random_entry: 'No article with these criterias was found' tag: notice: tag_added: 'Tag dodany' diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.pt.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.pt.yml index 7aef9694..ff7f2b49 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.pt.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.pt.yml @@ -582,6 +582,7 @@ flashes: entry_starred: 'Entrada destacada' entry_unstarred: 'Entrada não destacada' entry_deleted: 'Entrada apagada' + # no_random_entry: 'No article with these criterias was found' tag: notice: tag_added: 'Tag adicionada' diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.ro.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.ro.yml index 9b7068c6..51fed69c 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.ro.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.ro.yml @@ -582,6 +582,7 @@ flashes: entry_starred: 'Articol adăugat la favorite' entry_unstarred: 'Articol șters de la favorite' entry_deleted: 'Articol șters' + # no_random_entry: 'No article with these criterias was found' tag: notice: # tag_added: 'Tag added' diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.ru.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.ru.yml index 5f210c93..f3506d43 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.ru.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.ru.yml @@ -544,6 +544,7 @@ flashes: entry_starred: 'Запись помечена звездочкой' entry_unstarred: 'Пометка звездочкой у записи убрана' entry_deleted: 'Запись удалена' + # no_random_entry: 'No article with these criterias was found' tag: notice: tag_added: 'Тег добавлен' @@ -564,4 +565,4 @@ flashes: notice: added: 'Пользователь "%username%" добавлен' updated: 'Пользователь "%username%" обновлен' - deleted: 'Пользователь "%username%" удален' \ No newline at end of file + deleted: 'Пользователь "%username%" удален' diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.th.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.th.yml index 9d22f90d..f231e79a 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.th.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.th.yml @@ -580,6 +580,7 @@ flashes: entry_starred: 'รายการที่แสดง' entry_unstarred: 'รายการที่ไม่ได้แสดง' entry_deleted: 'รายการที่ถูกลบ' + # no_random_entry: 'No article with these criterias was found' tag: notice: tag_added: 'แท็กที่เพิ่ม' diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.tr.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.tr.yml index 5c95fe63..148047a0 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.tr.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.tr.yml @@ -560,6 +560,7 @@ flashes: entry_starred: 'Makale favorilere eklendi' entry_unstarred: 'Makale favorilerden çıkartıldı' entry_deleted: 'Makale silindi' + # no_random_entry: 'No article with these criterias was found' tag: notice: tag_added: 'Etiket eklendi' diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Entry/entries.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Entry/entries.html.twig index adc704bb..ac2e6688 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Entry/entries.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Entry/entries.html.twig @@ -20,11 +20,15 @@ {% block content %} {% set currentRoute = app.request.attributes.get('_route') %} + {% if currentRoute == 'homepage' %} + {% set currentRoute = 'unread' %} + {% endif %} {% set listMode = app.user.config.listMode %}
{{ 'entry.list.number_on_the_page'|transchoice(entries.count) }}