]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Added random feature
authorNicolas Lœuillet <nicolas@loeuillet.org>
Fri, 22 Dec 2017 14:44:00 +0000 (15:44 +0100)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Thu, 11 Oct 2018 09:50:36 +0000 (11:50 +0200)
18 files changed:
src/Wallabag/CoreBundle/Controller/EntryController.php
src/Wallabag/CoreBundle/Repository/EntryRepository.php
src/Wallabag/CoreBundle/Resources/translations/messages.da.yml
src/Wallabag/CoreBundle/Resources/translations/messages.de.yml
src/Wallabag/CoreBundle/Resources/translations/messages.en.yml
src/Wallabag/CoreBundle/Resources/translations/messages.es.yml
src/Wallabag/CoreBundle/Resources/translations/messages.fa.yml
src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml
src/Wallabag/CoreBundle/Resources/translations/messages.it.yml
src/Wallabag/CoreBundle/Resources/translations/messages.oc.yml
src/Wallabag/CoreBundle/Resources/translations/messages.pl.yml
src/Wallabag/CoreBundle/Resources/translations/messages.pt.yml
src/Wallabag/CoreBundle/Resources/translations/messages.ro.yml
src/Wallabag/CoreBundle/Resources/translations/messages.ru.yml
src/Wallabag/CoreBundle/Resources/translations/messages.th.yml
src/Wallabag/CoreBundle/Resources/translations/messages.tr.yml
src/Wallabag/CoreBundle/Resources/views/themes/baggy/Entry/entries.html.twig
src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig

index ac372a333594f20a48c925d95272130ee96b7d5c..6c843ba7a8991039e67b1a2b7159dcbfe64cbcf1 100644 (file)
@@ -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.
      *
index 93c630c00162412229f1c1705ea1c4d133895445..7fe090be14c8d76dec53d79dc7f6e1178f3076ef 100644 (file)
@@ -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);
     }
 }
index e1384675459ebce56c82bc066a633d1c382aa028..8049d6252f973804129af5cf669666cd32f36e3d 100644 (file)
@@ -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'
index c297ffb5dedf9ff3d219e796fd71bf2d0bb4112f..fcc0a415bb15f1215d97f9013ae573523dd3f52a 100644 (file)
@@ -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'
index bd81c72f2eb02f56e593411352d0ef65c72e6e6a..e7418ed28329af4e90da56770633c547990124a3 100644 (file)
@@ -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'
index 700190a67adb7023a9a3fe03708f6cfc5d8f2498..7c97e2d804ad15f8e371664403e87be74486cb28 100644 (file)
@@ -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'
index 8364593365ebf6c6a7e47afed654d2de5d05f5e1..fc23f9279c4af2e52a84399a2503e358839f28ce 100644 (file)
@@ -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: 'برچسب افزوده شد'
index edf2965431a605b6a38831ab8932f25ac7523f33..1c5fb5c8d4ef1334d81230e9696671393da4164e 100644 (file)
@@ -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é"
index 47292116f63c4c5223ed403339889553c35888e6..7ea0b7eae9c3628ca77f3a2af8cc0f4b0cdc8d89 100644 (file)
@@ -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'
index 95bc9560f896a1de726600b17c79777749936e7d..4fd74fd879c273c6abf34e46d2b27ade8fc1d80f 100644 (file)
@@ -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'
index a64e60b015646dcd4236c63b0b8345aadec89f5f..37b31aab6c59d890ed39bc28d5becd5897684b35 100644 (file)
@@ -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'
index 7aef9694ef4c7dc9579722b98dd9aa3edbe178ca..ff7f2b49998b45c64c451dbadc483d50d0cc2299 100644 (file)
@@ -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'
index 9b7068c663c03917b5ef62628eaa1f005a10e2ee..51fed69c9577ec0fadef6612a489c657ec9fced3 100644 (file)
@@ -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'
index 5f210c934dc4e05695bb1eacc83e8bf95db9c3d1..f3506d43572a034d53e6120458f098bfbb633b23 100644 (file)
@@ -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%" удален'
index 9d22f90d3096062370a5c465afdb2aae82a21d7e..f231e79a03f0560e466f6b5ce0eeb12b5afe2628 100644 (file)
@@ -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: 'แท็กที่เพิ่ม'
index 5c95fe63022a455bd1ebce6daedecfa5ec07f30c..148047a094341aed1a70b0ccfc5a3dcbff81116c 100644 (file)
@@ -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'
index adc704bb1eeee0dac9d34ff4fd0aa5ac61a12a6e..ac2e6688d477c778cbf717637256c65db78ea511 100644 (file)
 
 {% block content %}
     {% set currentRoute = app.request.attributes.get('_route') %}
+    {% if currentRoute == 'homepage' %}
+        {% set currentRoute = 'unread' %}
+    {% endif %}
     {% set listMode = app.user.config.listMode %}
     <div class="results">
         <div class="nb-results">{{ 'entry.list.number_on_the_page'|transchoice(entries.count) }}</div>
         <div class="pagination">
             <a href="{{ path('switch_view_mode') }}"><i class="listMode-btn material-icons md-24">{% if listMode == 0 %}list{% else %}view_module{% endif %}</i></a>
+            <a href="{{ path(currentRoute ~ '_random') }}">random</a>
             {% if app.user.config.rssToken %}
                 {% include "@WallabagCore/themes/common/Entry/_rss_link.html.twig" %}
             {% endif %}
@@ -89,9 +93,6 @@
     {% set currentTag = null %}
     {% if tag is defined %}
         {% set currentTag = tag %}
-    {% endif %}
-    {% if currentRoute == 'homepage' %}
-        {% set currentRoute = 'unread' %}
     {% endif %}
         <h2>{{ 'entry.list.export_title'|trans }}</h2>
         <a href="javascript: void(null);" id="download-form-close" class="close-button--popup close-button">&times;</a>
index e883503e67e7fa90e6a69a93e14e4600d19c42e8..586cd7b1924b27c4de53b54ca79ce32bd9061709 100644 (file)
 {% block content %}
     {% set listMode = app.user.config.listMode %}
     {% set currentRoute = app.request.attributes.get('_route') %}
+    {% if currentRoute == 'homepage' %}
+        {% set currentRoute = 'unread' %}
+    {% endif %}
     <div class="results">
         <div class="nb-results">
             {{ 'entry.list.number_on_the_page'|transchoice(entries.count) }}
             <a href="{{ path('switch_view_mode') }}"><i class="material-icons">{% if listMode == 0 %}view_list{% else %}view_module{% endif %}</i></a>
+            <a href="{{ path(currentRoute ~ '_random') }}">random</a>
             {% if app.user.config.rssToken %}
                 {% include "@WallabagCore/themes/common/Entry/_rss_link.html.twig" %}
             {% endif %}
@@ -61,9 +65,6 @@
     {% set currentTag = null %}
     {% if tag is defined %}
         {% set currentTag = tag.slug %}
-    {% endif %}
-    {% if currentRoute == 'homepage' %}
-        {% set currentRoute = 'unread' %}
     {% endif %}
         <h4 class="center">{{ 'entry.list.export_title'|trans }}</h4>
         <ul>