diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Repository/EntryRepository.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Repository/EntryRepository.php | 86 |
1 files changed, 77 insertions, 9 deletions
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index cebce714..f5089729 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -3,6 +3,7 @@ | |||
3 | namespace Wallabag\CoreBundle\Repository; | 3 | namespace Wallabag\CoreBundle\Repository; |
4 | 4 | ||
5 | use Doctrine\ORM\EntityRepository; | 5 | use Doctrine\ORM\EntityRepository; |
6 | use Doctrine\ORM\NoResultException; | ||
6 | use Doctrine\ORM\QueryBuilder; | 7 | use Doctrine\ORM\QueryBuilder; |
7 | use Pagerfanta\Adapter\DoctrineORMAdapter; | 8 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
8 | use Pagerfanta\Pagerfanta; | 9 | use 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 | */ |
@@ -345,6 +347,30 @@ class EntryRepository extends EntityRepository | |||
345 | } | 347 | } |
346 | 348 | ||
347 | /** | 349 | /** |
350 | * Find an entry by its hashed url and its owner. | ||
351 | * If it exists, return the entry otherwise return false. | ||
352 | * | ||
353 | * @param string $hashedUrl Url hashed using sha1 | ||
354 | * @param int $userId | ||
355 | * | ||
356 | * @return Entry|bool | ||
357 | */ | ||
358 | public function findByHashedUrlAndUserId($hashedUrl, $userId) | ||
359 | { | ||
360 | $res = $this->createQueryBuilder('e') | ||
361 | ->where('e.hashedUrl = :hashed_url')->setParameter('hashed_url', $hashedUrl) | ||
362 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) | ||
363 | ->getQuery() | ||
364 | ->getResult(); | ||
365 | |||
366 | if (\count($res)) { | ||
367 | return current($res); | ||
368 | } | ||
369 | |||
370 | return false; | ||
371 | } | ||
372 | |||
373 | /** | ||
348 | * Count all entries for a user. | 374 | * Count all entries for a user. |
349 | * | 375 | * |
350 | * @param int $userId | 376 | * @param int $userId |
@@ -416,8 +442,8 @@ class EntryRepository extends EntityRepository | |||
416 | /** | 442 | /** |
417 | * Find all entries by url and owner. | 443 | * Find all entries by url and owner. |
418 | * | 444 | * |
419 | * @param $url | 445 | * @param string $url |
420 | * @param $userId | 446 | * @param int $userId |
421 | * | 447 | * |
422 | * @return array | 448 | * @return array |
423 | */ | 449 | */ |
@@ -431,6 +457,49 @@ class EntryRepository extends EntityRepository | |||
431 | } | 457 | } |
432 | 458 | ||
433 | /** | 459 | /** |
460 | * Returns a random entry, filtering by status. | ||
461 | * | ||
462 | * @param int $userId | ||
463 | * @param string $type Can be unread, archive, starred, etc | ||
464 | * | ||
465 | * @throws NoResultException | ||
466 | * | ||
467 | * @return Entry | ||
468 | */ | ||
469 | public function getRandomEntry($userId, $type = '') | ||
470 | { | ||
471 | $qb = $this->getQueryBuilderByUser($userId) | ||
472 | ->select('e.id'); | ||
473 | |||
474 | switch ($type) { | ||
475 | case 'unread': | ||
476 | $qb->andWhere('e.isArchived = false'); | ||
477 | break; | ||
478 | case 'archive': | ||
479 | $qb->andWhere('e.isArchived = true'); | ||
480 | break; | ||
481 | case 'starred': | ||
482 | $qb->andWhere('e.isStarred = true'); | ||
483 | break; | ||
484 | case 'untagged': | ||
485 | $qb->leftJoin('e.tags', 't'); | ||
486 | $qb->andWhere('t.id is null'); | ||
487 | break; | ||
488 | } | ||
489 | |||
490 | $ids = $qb->getQuery()->getArrayResult(); | ||
491 | |||
492 | if (empty($ids)) { | ||
493 | throw new NoResultException(); | ||
494 | } | ||
495 | |||
496 | // random select one in the list | ||
497 | $randomId = $ids[mt_rand(0, \count($ids) - 1)]['id']; | ||
498 | |||
499 | return $this->find($randomId); | ||
500 | } | ||
501 | |||
502 | /** | ||
434 | * Return a query builder to be used by other getBuilderFor* method. | 503 | * Return a query builder to be used by other getBuilderFor* method. |
435 | * | 504 | * |
436 | * @param int $userId | 505 | * @param int $userId |
@@ -468,7 +537,6 @@ class EntryRepository extends EntityRepository | |||
468 | */ | 537 | */ |
469 | private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') | 538 | private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') |
470 | { | 539 | { |
471 | return $qb | 540 | return $qb->orderBy(sprintf('e.%s', $sortBy), $direction); |
472 | ->orderBy(sprintf('e.%s', $sortBy), $direction); | ||
473 | } | 541 | } |
474 | } | 542 | } |