aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Repository/EntryRepository.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Repository/EntryRepository.php')
-rw-r--r--src/Wallabag/CoreBundle/Repository/EntryRepository.php62
1 files changed, 53 insertions, 9 deletions
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
index cebce714..45366623 100644
--- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php
+++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
@@ -3,6 +3,7 @@
3namespace Wallabag\CoreBundle\Repository; 3namespace Wallabag\CoreBundle\Repository;
4 4
5use Doctrine\ORM\EntityRepository; 5use Doctrine\ORM\EntityRepository;
6use Doctrine\ORM\NoResultException;
6use Doctrine\ORM\QueryBuilder; 7use Doctrine\ORM\QueryBuilder;
7use Pagerfanta\Adapter\DoctrineORMAdapter; 8use Pagerfanta\Adapter\DoctrineORMAdapter;
8use Pagerfanta\Pagerfanta; 9use Pagerfanta\Pagerfanta;
@@ -50,7 +51,7 @@ class EntryRepository extends EntityRepository
50 public function getBuilderForArchiveByUser($userId) 51 public function getBuilderForArchiveByUser($userId)
51 { 52 {
52 return $this 53 return $this
53 ->getSortedQueryBuilderByUser($userId) 54 ->getSortedQueryBuilderByUser($userId, 'archivedAt', 'desc')
54 ->andWhere('e.isArchived = true') 55 ->andWhere('e.isArchived = true')
55 ; 56 ;
56 } 57 }
@@ -110,8 +111,7 @@ class EntryRepository extends EntityRepository
110 */ 111 */
111 public function getBuilderForUntaggedByUser($userId) 112 public function getBuilderForUntaggedByUser($userId)
112 { 113 {
113 return $this 114 return $this->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId));
114 ->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId));
115 } 115 }
116 116
117 /** 117 /**
@@ -193,6 +193,8 @@ class EntryRepository extends EntityRepository
193 $qb->orderBy('e.id', $order); 193 $qb->orderBy('e.id', $order);
194 } elseif ('updated' === $sort) { 194 } elseif ('updated' === $sort) {
195 $qb->orderBy('e.updatedAt', $order); 195 $qb->orderBy('e.updatedAt', $order);
196 } elseif ('archived' === $sort) {
197 $qb->orderBy('e.archivedAt', $order);
196 } 198 }
197 199
198 $pagerAdapter = new DoctrineORMAdapter($qb, true, false); 200 $pagerAdapter = new DoctrineORMAdapter($qb, true, false);
@@ -324,8 +326,8 @@ class EntryRepository extends EntityRepository
324 * Find an entry by its url and its owner. 326 * Find an entry by its url and its owner.
325 * If it exists, return the entry otherwise return false. 327 * If it exists, return the entry otherwise return false.
326 * 328 *
327 * @param $url 329 * @param string $url
328 * @param $userId 330 * @param int $userId
329 * 331 *
330 * @return Entry|bool 332 * @return Entry|bool
331 */ 333 */
@@ -416,8 +418,8 @@ class EntryRepository extends EntityRepository
416 /** 418 /**
417 * Find all entries by url and owner. 419 * Find all entries by url and owner.
418 * 420 *
419 * @param $url 421 * @param string $url
420 * @param $userId 422 * @param int $userId
421 * 423 *
422 * @return array 424 * @return array
423 */ 425 */
@@ -431,6 +433,49 @@ class EntryRepository extends EntityRepository
431 } 433 }
432 434
433 /** 435 /**
436 * Returns a random entry, filtering by status.
437 *
438 * @param int $userId
439 * @param string $type Can be unread, archive, starred, etc
440 *
441 * @throws NoResultException
442 *
443 * @return Entry
444 */
445 public function getRandomEntry($userId, $type = '')
446 {
447 $qb = $this->getQueryBuilderByUser($userId)
448 ->select('e.id');
449
450 switch ($type) {
451 case 'unread':
452 $qb->andWhere('e.isArchived = false');
453 break;
454 case 'archive':
455 $qb->andWhere('e.isArchived = true');
456 break;
457 case 'starred':
458 $qb->andWhere('e.isStarred = true');
459 break;
460 case 'untagged':
461 $qb->leftJoin('e.tags', 't');
462 $qb->andWhere('t.id is null');
463 break;
464 }
465
466 $ids = $qb->getQuery()->getArrayResult();
467
468 if (empty($ids)) {
469 throw new NoResultException();
470 }
471
472 // random select one in the list
473 $randomId = $ids[mt_rand(0, \count($ids) - 1)]['id'];
474
475 return $this->find($randomId);
476 }
477
478 /**
434 * Return a query builder to be used by other getBuilderFor* method. 479 * Return a query builder to be used by other getBuilderFor* method.
435 * 480 *
436 * @param int $userId 481 * @param int $userId
@@ -468,7 +513,6 @@ class EntryRepository extends EntityRepository
468 */ 513 */
469 private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') 514 private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc')
470 { 515 {
471 return $qb 516 return $qb->orderBy(sprintf('e.%s', $sortBy), $direction);
472 ->orderBy(sprintf('e.%s', $sortBy), $direction);
473 } 517 }
474} 518}