2015-01-22 16:18:56 +00:00
|
|
|
<?php
|
|
|
|
|
2015-01-23 15:28:37 +00:00
|
|
|
namespace Wallabag\CoreBundle\Repository;
|
2015-01-22 16:18:56 +00:00
|
|
|
|
|
|
|
use Doctrine\ORM\EntityRepository;
|
2015-03-06 10:07:22 +00:00
|
|
|
use Pagerfanta\Adapter\DoctrineORMAdapter;
|
|
|
|
use Pagerfanta\Pagerfanta;
|
2015-12-29 13:50:52 +00:00
|
|
|
use Wallabag\CoreBundle\Entity\Tag;
|
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
|
|
|
/**
|
2015-08-20 18:10:06 +00:00
|
|
|
* Return a query builder to used by other getBuilderFor* method.
|
2015-01-23 13:58:17 +00:00
|
|
|
*
|
2015-02-08 22:05:51 +00:00
|
|
|
* @param int $userId
|
|
|
|
*
|
2015-08-07 20:20:30 +00:00
|
|
|
* @return QueryBuilder
|
2015-01-23 13:58:17 +00:00
|
|
|
*/
|
2015-08-20 18:10:06 +00:00
|
|
|
private function getBuilderByUser($userId)
|
2015-01-22 16:18:56 +00:00
|
|
|
{
|
2015-08-07 20:20:30 +00:00
|
|
|
return $this->createQueryBuilder('e')
|
2015-02-08 22:05:51 +00:00
|
|
|
->leftJoin('e.user', 'u')
|
2015-08-20 18:10:06 +00:00
|
|
|
->andWhere('u.id = :userId')->setParameter('userId', $userId)
|
|
|
|
->orderBy('e.id', 'desc')
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2015-08-21 05:38:18 +00:00
|
|
|
/**
|
|
|
|
* Retrieves all entries for a user.
|
|
|
|
*
|
|
|
|
* @param int $userId
|
|
|
|
*
|
|
|
|
* @return QueryBuilder
|
|
|
|
*/
|
|
|
|
public function getBuilderForAllByUser($userId)
|
|
|
|
{
|
|
|
|
return $this
|
|
|
|
->getBuilderByUser($userId)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2015-08-20 18:10:06 +00:00
|
|
|
/**
|
|
|
|
* Retrieves unread entries for a user.
|
|
|
|
*
|
|
|
|
* @param int $userId
|
|
|
|
*
|
|
|
|
* @return QueryBuilder
|
|
|
|
*/
|
|
|
|
public function getBuilderForUnreadByUser($userId)
|
|
|
|
{
|
|
|
|
return $this
|
|
|
|
->getBuilderByUser($userId)
|
|
|
|
->andWhere('e.isArchived = false')
|
|
|
|
;
|
2015-01-22 16:18:56 +00:00
|
|
|
}
|
2015-01-22 20:11:22 +00:00
|
|
|
|
2015-01-23 13:58:17 +00:00
|
|
|
/**
|
2015-05-30 11:52:26 +00:00
|
|
|
* Retrieves read entries for a user.
|
2015-01-23 13:58:17 +00:00
|
|
|
*
|
2015-02-08 22:05:51 +00:00
|
|
|
* @param int $userId
|
|
|
|
*
|
2015-08-07 20:20:30 +00:00
|
|
|
* @return QueryBuilder
|
2015-01-23 13:58:17 +00:00
|
|
|
*/
|
2015-08-20 18:10:06 +00:00
|
|
|
public function getBuilderForArchiveByUser($userId)
|
2015-01-22 20:11:22 +00:00
|
|
|
{
|
2015-08-20 18:10:06 +00:00
|
|
|
return $this
|
|
|
|
->getBuilderByUser($userId)
|
|
|
|
->andWhere('e.isArchived = true')
|
|
|
|
;
|
2015-01-22 20:11:22 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 13:58:17 +00:00
|
|
|
/**
|
2015-05-30 11:52:26 +00:00
|
|
|
* Retrieves starred entries for a user.
|
2015-01-23 13:58:17 +00:00
|
|
|
*
|
2015-02-08 22:05:51 +00:00
|
|
|
* @param int $userId
|
|
|
|
*
|
2015-08-07 20:20:30 +00:00
|
|
|
* @return QueryBuilder
|
2015-01-23 13:58:17 +00:00
|
|
|
*/
|
2015-08-20 18:10:06 +00:00
|
|
|
public function getBuilderForStarredByUser($userId)
|
2015-01-22 20:11:22 +00:00
|
|
|
{
|
2015-08-20 18:10:06 +00:00
|
|
|
return $this
|
|
|
|
->getBuilderByUser($userId)
|
|
|
|
->andWhere('e.isStarred = true')
|
|
|
|
;
|
2015-01-22 20:11:22 +00:00
|
|
|
}
|
2015-01-30 06:50:52 +00:00
|
|
|
|
2015-02-08 22:05:51 +00:00
|
|
|
/**
|
2015-05-30 11:52:26 +00:00
|
|
|
* Find Entries.
|
2015-02-08 22:05:51 +00:00
|
|
|
*
|
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-08 22:05:51 +00:00
|
|
|
*
|
2015-02-10 12:53:00 +00:00
|
|
|
* @return array
|
2015-02-08 22:05:51 +00:00
|
|
|
*/
|
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')
|
2015-02-20 10:45:38 +00:00
|
|
|
->where('e.user =:userId')->setParameter('userId', $userId);
|
2015-02-04 17:06:42 +00:00
|
|
|
|
2015-02-08 22:05:51 +00:00
|
|
|
if (null !== $isArchived) {
|
|
|
|
$qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
|
2015-02-04 17:06:42 +00:00
|
|
|
}
|
|
|
|
|
2015-02-08 22:05:51 +00:00
|
|
|
if (null !== $isStarred) {
|
|
|
|
$qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
|
2015-02-04 17:06:42 +00:00
|
|
|
}
|
|
|
|
|
2015-02-05 06:54:04 +00:00
|
|
|
if ('created' === $sort) {
|
2015-03-28 13:28:24 +00:00
|
|
|
$qb->orderBy('e.id', $order);
|
2015-02-05 06:54:04 +00:00
|
|
|
} elseif ('updated' === $sort) {
|
|
|
|
$qb->orderBy('e.updatedAt', $order);
|
|
|
|
}
|
|
|
|
|
2015-03-06 10:07:22 +00:00
|
|
|
$pagerAdapter = new DoctrineORMAdapter($qb);
|
|
|
|
|
|
|
|
return new Pagerfanta($pagerAdapter);
|
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-26 08:41:42 +00:00
|
|
|
public function findOneWithTags($userId)
|
2015-02-24 06:42:09 +00:00
|
|
|
{
|
|
|
|
$qb = $this->createQueryBuilder('e')
|
|
|
|
->innerJoin('e.tags', 't')
|
2015-02-26 13:25:40 +00:00
|
|
|
->innerJoin('e.user', 'u')
|
|
|
|
->addSelect('t', 'u')
|
|
|
|
->where('e.user=:userId')->setParameter('userId', $userId)
|
|
|
|
;
|
2015-02-26 08:41:42 +00:00
|
|
|
|
2015-02-26 13:25:40 +00:00
|
|
|
return $qb->getQuery()->getResult();
|
2015-02-24 06:42:09 +00:00
|
|
|
}
|
2015-09-23 05:55:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find distinct language for a given user.
|
|
|
|
* Used to build the filter language list.
|
|
|
|
*
|
|
|
|
* @param int $userId User id
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function findDistinctLanguageByUser($userId)
|
|
|
|
{
|
|
|
|
$results = $this->createQueryBuilder('e')
|
|
|
|
->select('e.language')
|
|
|
|
->where('e.user = :userId')->setParameter('userId', $userId)
|
|
|
|
->andWhere('e.language IS NOT NULL')
|
|
|
|
->groupBy('e.language')
|
|
|
|
->orderBy('e.language', ' ASC')
|
|
|
|
->getQuery()
|
|
|
|
->getResult();
|
|
|
|
|
|
|
|
$languages = array();
|
|
|
|
foreach ($results as $result) {
|
|
|
|
$languages[$result['language']] = $result['language'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $languages;
|
|
|
|
}
|
2015-09-28 17:35:55 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-01 07:26:52 +00:00
|
|
|
* Used only in test case to get the right entry associated to the right user.
|
2015-09-28 17:35:55 +00:00
|
|
|
*
|
2015-10-01 07:26:52 +00:00
|
|
|
* @param string $username
|
2015-09-28 17:35:55 +00:00
|
|
|
*
|
|
|
|
* @return Entry
|
|
|
|
*/
|
|
|
|
public function findOneByUsernameAndNotArchived($username)
|
|
|
|
{
|
|
|
|
return $this->createQueryBuilder('e')
|
|
|
|
->leftJoin('e.user', 'u')
|
|
|
|
->where('u.username = :username')->setParameter('username', $username)
|
|
|
|
->andWhere('e.isArchived = false')
|
|
|
|
->setMaxResults(1)
|
|
|
|
->getQuery()
|
|
|
|
->getSingleResult();
|
|
|
|
}
|
2015-12-29 13:50:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a tag from all user entries.
|
2015-12-29 14:08:33 +00:00
|
|
|
*
|
|
|
|
* We need to loop on each entry attached to the given tag to remove it, since Doctrine doesn't know EntryTag entity because it's a ManyToMany relation.
|
|
|
|
* It could be faster with one query but I don't know how to retrieve the table name `entry_tag` which can have a prefix.
|
2015-12-29 13:50:52 +00:00
|
|
|
*
|
|
|
|
* @param int $userId
|
|
|
|
* @param Tag $tag
|
|
|
|
*/
|
|
|
|
public function removeTag($userId, Tag $tag)
|
|
|
|
{
|
2015-12-29 14:08:33 +00:00
|
|
|
$entries = $this->getBuilderByUser($userId)
|
|
|
|
->innerJoin('e.tags', 't')
|
|
|
|
->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
|
|
|
|
->getQuery()
|
|
|
|
->getResult();
|
|
|
|
|
|
|
|
foreach ($entries as $entry) {
|
|
|
|
$entry->removeTag($tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->getEntityManager()->flush();
|
|
|
|
|
|
|
|
// An other solution can be to use raw query but I can't find a way to retrieve the `entry_tag` table name since it can be prefixed....
|
|
|
|
// $sql = 'DELETE et FROM entry_tag et WHERE et.entry_id IN ( SELECT e.id FROM entry e WHERE e.user_id = :userId ) AND et.tag_id = :tagId';
|
|
|
|
// $stmt = $this->getEntityManager()->getConnection()->prepare($sql);
|
|
|
|
// $stmt->execute([
|
|
|
|
// 'userId' => $userId,
|
|
|
|
// 'tagId' => $tag->getId(),
|
|
|
|
// ]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all entries that are attached to a give tag id.
|
|
|
|
*
|
|
|
|
* @param int $userId
|
|
|
|
* @param int $tagId
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function findAllByTagId($userId, $tagId)
|
|
|
|
{
|
|
|
|
return $this->getBuilderByUser($userId)
|
|
|
|
->innerJoin('e.tags', 't')
|
|
|
|
->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
|
|
|
|
->getQuery()
|
|
|
|
->getResult();
|
2015-12-29 13:50:52 +00:00
|
|
|
}
|
2015-01-22 16:18:56 +00:00
|
|
|
}
|