From fc9379ca6309033bcc09107c2d3e77093cde604c Mon Sep 17 00:00:00 2001 From: Andrey Date: Thu, 30 May 2013 18:58:05 +0200 Subject: [PATCH] Add more search methods to repositories --- .../Repositories/OrderElementRepository.php | 19 ++++++-- .../Repositories/SessionRepository.php | 48 +++++++++++++++---- .../Repositories/UserSettingRepository.php | 40 ++++++++++++++++ 3 files changed, 93 insertions(+), 14 deletions(-) diff --git a/lib/Doctrine/Repositories/OrderElementRepository.php b/lib/Doctrine/Repositories/OrderElementRepository.php index 88c305fbea..916c536108 100644 --- a/lib/Doctrine/Repositories/OrderElementRepository.php +++ b/lib/Doctrine/Repositories/OrderElementRepository.php @@ -16,11 +16,22 @@ class OrderElementRepository extends EntityRepository { $dql = 'SELECT f FROM Entities\FeedPublisher f WHERE f.id = :id '; - + $query = $this->_em->createQuery($dql); $query->setParameter('id', $id); - $feed = $query->getResult(); - - return $feed[0]; + + return $query->getOneOrNullResult(); + } + + public function findByUser(\Entities\Feed $feed, \User_Adapter $user) + { + $dql = 'SELECT f FROM Entities\FeedPublisher f + WHERE f.usr_id = :usrId AND f.feed = :feed'; + + $query = $this->_em->createQuery($dql); + $query->setParameter('usrId', $user->get_id()); + $query->setParameter('feed', $feed); + + return $query->getOneOrNullResult(); } } diff --git a/lib/Doctrine/Repositories/SessionRepository.php b/lib/Doctrine/Repositories/SessionRepository.php index 3a927bb8e9..a3ca1b71e0 100644 --- a/lib/Doctrine/Repositories/SessionRepository.php +++ b/lib/Doctrine/Repositories/SessionRepository.php @@ -2,6 +2,7 @@ namespace Repositories; +use Alchemy\Phrasea\Application; use Doctrine\ORM\EntityRepository; /** @@ -12,7 +13,7 @@ use Doctrine\ORM\EntityRepository; */ class SessionRepository extends EntityRepository { - + /** * * @param User_Adapter $user @@ -35,19 +36,46 @@ class SessionRepository extends EntityRepository $query = $this->_em->createQuery($dql); $feeds = $query->getResult(); - + return $feeds; } - - public function find($id) + + public function findAllPublic() { $dql = 'SELECT f FROM Entities\Feed f - WHERE f.id = :id '; - + WHERE f.public = true + ORDER BY f.created_on DESC'; + $query = $this->_em->createQuery($dql); - $query->setParameter('id', $id); - $feed = $query->getResult(); - - return $feed[0]; + $feeds = $query->getResult(); + + return $feeds; + } + + public function loadWithUser(Application $app, \User_Adapter $user, $id) + { + $feed = $this->find($id); + if ($feed) { + $coll = $feed->getCollection($app); + if ($feed->getPublic() + || $coll === null + || in_array($coll->get_base_id(), array_keys($user->ACL()->get_granted_base()))) { + return $feed; + } + } + + return null; + } + + public function findByIdArray(array $feed_id) + { + $dql = 'SELECT f FROM Entities\Feed f + ORDER BY f.created_on DESC + WHERE f.id IN (' . implode(",", $feed_id) . ')'; + + $query = $this->_em->createQuery($dql); + $feeds = $query->getResult(); + + return $feeds; } } diff --git a/lib/Doctrine/Repositories/UserSettingRepository.php b/lib/Doctrine/Repositories/UserSettingRepository.php index 872f603c7f..3537ecd8b5 100644 --- a/lib/Doctrine/Repositories/UserSettingRepository.php +++ b/lib/Doctrine/Repositories/UserSettingRepository.php @@ -12,4 +12,44 @@ use Doctrine\ORM\EntityRepository; */ class UserSettingRepository extends EntityRepository { + public function find($id) + { + $dql = 'SELECT f FROM Entities\FeedEntry f + WHERE f.id = :id '; + + $query = $this->_em->createQuery($dql); + $query->setParameter('id', $id); + + return $query->getOneOrNullResult(); + } + + public function findByFeed($feed, $id) + { + $dql = 'SELECT f FROM Entities\FeedEntry f + WHERE f.id = :id AND f.feed = :feed'; + + $query = $this->_em->createQuery($dql); + $query->setParameter('id', $id); + $query->setParameter('feed', $feed); + + return $query->getOneOrNullResult(); + } + + public function findByFeeds($feeds, $offset_start = null, $how_many = null) + { + $dql = 'SELECT f FROM Entities\FeedEntry f + WHERE f.feed IN (:feeds)'; + + $query = $this->_em->createQuery($dql); + $query->setParameter('feeds', $feeds); + + if (null !== $offset_start) { + $query->setFirstResult($offset_start); + } + if (null !== $how_many) { + $query->setMaxResults($how_many); + } + + return $query->getResult(); + } }