wallabag/src/Wallabag/CoreBundle/Repository/EntryRepository.php

138 lines
3.6 KiB
PHP
Raw Normal View History

2015-01-22 16:18:56 +00:00
<?php
namespace Wallabag\CoreBundle\Repository;
2015-01-22 16:18:56 +00:00
use Doctrine\ORM\EntityRepository;
2015-01-23 11:45:24 +00:00
use Doctrine\ORM\Tools\Pagination\Paginator;
2015-01-22 16:18:56 +00:00
2015-02-06 06:45:32 +00:00
class EntryRepository extends EntityRepository
2015-01-22 16:18:56 +00:00
{
2015-01-23 13:58:17 +00:00
/**
* Retrieves unread entries for a user
*
* @param int $userId
* @param int $firstResult
* @param int $maxResults
*
2015-01-23 13:58:17 +00:00
* @return Paginator
*/
2015-01-23 11:45:24 +00:00
public function findUnreadByUser($userId, $firstResult, $maxResults = 12)
2015-01-22 16:18:56 +00:00
{
$qb = $this->createQueryBuilder('e')
2015-01-23 11:45:24 +00:00
->setFirstResult($firstResult)
->setMaxResults($maxResults)
->leftJoin('e.user', 'u')
2015-02-05 21:33:36 +00:00
->where('e.isArchived = false')
->andWhere('u.id =:userId')->setParameter('userId', $userId)
2015-02-05 06:54:04 +00:00
->orderBy('e.createdAt', 'desc')
2015-01-23 11:45:24 +00:00
->getQuery();
2015-01-22 16:18:56 +00:00
2015-01-23 13:58:17 +00:00
$paginator = new Paginator($qb);
2015-01-23 11:45:24 +00:00
2015-01-23 13:58:17 +00:00
return $paginator;
2015-01-22 16:18:56 +00:00
}
2015-01-22 20:11:22 +00:00
2015-01-23 13:58:17 +00:00
/**
* Retrieves read entries for a user
*
* @param int $userId
* @param int $firstResult
* @param int $maxResults
*
2015-01-23 13:58:17 +00:00
* @return Paginator
*/
2015-01-23 11:45:24 +00:00
public function findArchiveByUser($userId, $firstResult, $maxResults = 12)
2015-01-22 20:11:22 +00:00
{
$qb = $this->createQueryBuilder('e')
->select('e')
2015-01-23 11:45:24 +00:00
->setFirstResult($firstResult)
->setMaxResults($maxResults)
->leftJoin('e.user', 'u')
2015-02-05 21:33:36 +00:00
->where('e.isArchived = true')
->andWhere('u.id =:userId')->setParameter('userId', $userId)
2015-02-05 06:54:04 +00:00
->orderBy('e.createdAt', 'desc')
2015-01-23 13:58:17 +00:00
->getQuery();
$paginator = new Paginator($qb);
2015-01-22 20:11:22 +00:00
2015-01-23 13:58:17 +00:00
return $paginator;
2015-01-22 20:11:22 +00:00
}
2015-01-23 13:58:17 +00:00
/**
* Retrieves starred entries for a user
*
* @param int $userId
* @param int $firstResult
* @param int $maxResults
*
2015-01-23 13:58:17 +00:00
* @return Paginator
*/
2015-01-23 11:45:24 +00:00
public function findStarredByUser($userId, $firstResult, $maxResults = 12)
2015-01-22 20:11:22 +00:00
{
$qb = $this->createQueryBuilder('e')
->select('e')
2015-01-23 11:45:24 +00:00
->setFirstResult($firstResult)
->setMaxResults($maxResults)
->leftJoin('e.user', 'u')
2015-02-05 21:33:36 +00:00
->where('e.isStarred = true')
->andWhere('u.id =:userId')->setParameter('userId', $userId)
2015-02-05 06:54:04 +00:00
->orderBy('e.createdAt', 'desc')
2015-01-23 13:58:17 +00:00
->getQuery();
$paginator = new Paginator($qb);
2015-01-22 20:11:22 +00:00
2015-01-23 13:58:17 +00:00
return $paginator;
2015-01-22 20:11:22 +00:00
}
2015-01-30 06:50:52 +00:00
/**
* Find Entries
*
2015-02-09 21:07:39 +00:00
* @param int $userId
* @param bool $isArchived
* @param bool $isStarred
* @param string $sort
* @param string $order
*
2015-02-10 12:53:00 +00:00
* @return array
*/
2015-02-20 14:36:25 +00:00
public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC')
2015-01-30 06:50:52 +00:00
{
$qb = $this->createQueryBuilder('e')
->where('e.user =:userId')->setParameter('userId', $userId);
if (null !== $isArchived) {
$qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
}
if (null !== $isStarred) {
$qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
}
2015-02-05 06:54:04 +00:00
if ('created' === $sort) {
$qb->orderBy('e.createdAt', $order);
} elseif ('updated' === $sort) {
$qb->orderBy('e.updatedAt', $order);
}
return $qb
2015-01-30 06:50:52 +00:00
->getQuery()
->getResult();
2015-01-30 06:50:52 +00:00
}
2015-02-24 06:42:09 +00:00
2015-02-24 21:00:24 +00:00
/**
* Fetch an entry with a tag. Only used for tests.
*
* @return Entry
*/
2015-02-24 06:42:09 +00:00
public function findOneWithTags()
{
$qb = $this->createQueryBuilder('e')
->innerJoin('e.tags', 't')
->addSelect('t');
return $qb
->getQuery()
->getOneOrNullResult();
}
2015-01-22 16:18:56 +00:00
}