]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Move icon into the top menu bar
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / EntryRepository.php
index a26de0a83a5d7e8ea3282d55f4cb999242f4e025..fabb19638b7f1b9da2968929614d0658df807d47 100644 (file)
@@ -325,8 +325,8 @@ class EntryRepository extends EntityRepository
      * Find an entry by its url and its owner.
      * If it exists, return the entry otherwise return false.
      *
-     * @param $url
-     * @param $userId
+     * @param string $url
+     * @param int    $userId
      *
      * @return Entry|bool
      */
@@ -417,8 +417,8 @@ class EntryRepository extends EntityRepository
     /**
      * Find all entries by url and owner.
      *
-     * @param $url
-     * @param $userId
+     * @param string $url
+     * @param int    $userId
      *
      * @return array
      */
@@ -434,41 +434,40 @@ class EntryRepository extends EntityRepository
     /**
      * Returns a random entry, filtering by status.
      *
-     * @param $userId
-     * @param string $status can be unread, archive or starred
+     * @param int    $userId
+     * @param string $type   Can be unread, archive, starred, etc
      *
      * @throws \Doctrine\ORM\NoResultException
-     * @throws \Doctrine\ORM\NonUniqueResultException
      *
      * @return Entry
      */
-    public function getRandomEntry($userId, $status = '')
+    public function getRandomEntry($userId, $type = '')
     {
-        $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);
+        $qb = $this->getQueryBuilderByUser($userId)
+            ->select('e.id');
 
-        if ('unread' === $status) {
-            $qb->andWhere('e.isArchived = false');
+        switch ($type) {
+            case 'unread':
+                $qb->andWhere('e.isArchived = false');
+                break;
+            case 'archive':
+                $qb->andWhere('e.isArchived = true');
+                break;
+            case 'starred':
+                $qb->andWhere('e.isStarred = true');
+                break;
+            case 'untagged':
+                $qb->leftJoin('e.tags', 't');
+                $qb->andWhere('t.id is null');
+                break;
         }
 
-        if ('archive' === $status) {
-            $qb->andWhere('e.isArchived = true');
-        }
+        $ids = $qb->getQuery()->getArrayResult();
 
-        if ('starred' === $status) {
-            $qb->andWhere('e.isStarred = true');
-        }
+        // random select one in the list
+        $randomId = $ids[mt_rand(0, \count($ids) - 1)]['id'];
 
-        return $qb->andWhere('e.id >= :rand')
-            ->setParameter('rand', rand(0, $max))
-            ->setMaxResults(1)
-            ->getQuery()
-            ->getSingleResult();
+        return $this->find($randomId);
     }
 
     /**