]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Controller/EntryController.php
Added random feature
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / EntryController.php
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.
      *