]> 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>
Sat, 19 Jan 2019 20:09:32 +0000 (21:09 +0100)
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 8b6cf443efdb2441fa602950739b959aa482fa83..a26de0a83a5d7e8ea3282d55f4cb999242f4e025 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));
     }
 
     /**
@@ -432,6 +431,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.
      *
@@ -470,7 +509,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 4cf6991606f77bc1e53c5fef862c4647a89098c8..0099148afa47644f72de2a67b28911b0f2d5977d 100644 (file)
@@ -590,6 +590,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 10981788081ef000f94ec0ac2ec1658e891d7f24..5e531b781eeb4ee2aa11a3a5a5d519755c6d4b43 100644 (file)
@@ -590,6 +590,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 95e10faf43349c3de8b2caaec97baee084da120d..2b952bd504c0087cb616935791f7fda3180371f3 100644 (file)
@@ -590,6 +590,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 c95bee5b881d960dc9f44436e36c34809811f42d..2e2fbb936a984a6a785dbcf3b24d600f9f737ee9 100644 (file)
@@ -590,6 +590,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 4fde53ddce74a06d14c2f31a15bbb276ff35ed2c..c58bbccb47813c3a1770d23036b03ad72ec5f23f 100644 (file)
@@ -590,6 +590,7 @@ flashes:
             entry_starred: 'مقاله برگزیده شد'
             entry_unstarred: 'مقاله نابرگزیده شد'
             entry_deleted: 'مقاله پاک شد'
+            # no_random_entry: 'No article with these criterias was found'
     tag:
         notice:
             tag_added: 'برچسب افزوده شد'
index edf3ac358f4a4a9280bc384439fedde0ec254168..c3887f2413d3d414add1ee2e36b7736ca4b79665 100644 (file)
@@ -590,6 +590,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 f178ddbf5ee17bc10b5f4785c716dca5c61c7543..c2135ac01167be4f0866d9cbfe0bbfb687a25f99 100644 (file)
@@ -590,6 +590,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 a1220f527593d190b72f0f0a6a9f918e33257bcf..6e2c1cf93b14b1268828aa51eddbdd9cff6b3297 100644 (file)
@@ -590,6 +590,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 b6f7faf7e37bc86602bb759078b7853cd755d830..18156cbea4787f07faba0783366fc968c739e463 100644 (file)
@@ -590,6 +590,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 78df254fa015bd3b91f64e37cccd0a8bea256eeb..8493af4762e8b2940d0a5b845c1e8ac4f0d74727 100644 (file)
@@ -590,6 +590,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 8312ca15fb6e59138f79f5739037d47d3b54964a..8fba13dce6494f136e73e84ed4ce0f6e746a32f9 100644 (file)
@@ -590,6 +590,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 f14aad12141dba0b6a92bbe0033561f9c1d02956..56a63b142bc929a95be673c22db85bcb527d8096 100644 (file)
@@ -555,6 +555,7 @@ flashes:
             entry_starred: 'Запись помечена звездочкой'
             entry_unstarred: 'Пометка звездочкой у записи убрана'
             entry_deleted: 'Запись удалена'
+            # no_random_entry: 'No article with these criterias was found'
     tag:
         notice:
             tag_added: 'Тег добавлен'
index 7dbb1399b294c9d187c01bf3ffbbc253d756b909..9f0a65325953dcea991fbe3b6689db2bcde7498e 100644 (file)
@@ -588,6 +588,7 @@ flashes:
             entry_starred: 'รายการที่แสดง'
             entry_unstarred: 'รายการที่ไม่ได้แสดง'
             entry_deleted: 'รายการที่ถูกลบ'
+            # no_random_entry: 'No article with these criterias was found'
     tag:
         notice:
             tag_added: 'แท็กที่เพิ่ม'
index b4bc04d07bd7c8c7bc36d7592fad97e2a0577a52..a2093223c415eb876665c57b40285d9ba950b5ef 100644 (file)
@@ -568,6 +568,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 832112be73a87fbf1859862739f7de4932f2f6ba..7108efbd430bd3c929f55499e86796ef5dee8ddd 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 742dd330b4591e50b7da4c54858171de285f22d7..5deda0fc8a3849f823a36d7f7a479847b76fe6d1 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 %}
@@ -59,9 +63,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>