3 namespace Rapsys\AirBundle\Repository
;
5 use Symfony\Component\Translation\TranslatorInterface
;
6 use Doctrine\ORM\Query\ResultSetMapping
;
11 class LocationRepository
extends \Doctrine\ORM\EntityRepository
{
13 * Find complementary locations by session id
15 * @param $id The session id
16 * @return array The other locations
18 public function findComplementBySessionId($id) {
19 //Fetch complement locations
20 $ret = $this->getEntityManager()
21 ->createQuery('SELECT l.id, l.title FROM RapsysAirBundle:Session s LEFT JOIN RapsysAirBundle:Session s2 WITH s2.id != s.id AND s2.slot = s.slot AND s2.date = s.date LEFT JOIN RapsysAirBundle:Location l WITH l.id != s.location AND (l.id != s2.location OR s2.location IS NULL) WHERE s.id = :sid GROUP BY l.id ORDER BY l.id')
22 ->setParameter('sid', $id)
26 $ret = array_column($ret, 'id', 'title');
32 * Find translated location title sorted by date period
34 * @param $translator The TranslatorInterface instance
35 * @param $period The date period
36 * @param $granted The session is granted
38 public function findTranslatedSortedByPeriod(TranslatorInterface
$translator, $period, $userId = null) {
40 $ret = $this->getEntityManager()
43 FROM RapsysAirBundle:Location l
44 LEFT JOIN RapsysAirBundle:Session s WITH s.location = l.id AND s.date BETWEEN :begin AND :end
45 LEFT JOIN RapsysAirBundle:Application a WITH a.id = s.application'.(!empty($userId)?' AND a.user = :uid':'').'
47 ORDER BY '.(!empty($userId)?'COUNT(a.id) DESC, ':'').'COUNT(s.id) DESC, l.id'
49 ->setParameter('begin', $period->getStartDate())
50 ->setParameter('end', $period->getEndDate());
52 //Set optional user id
53 if (!empty($userId)) {
54 $ret->setParameter('uid', $userId);
58 $ret = $ret->getResult();
61 $ret = array_column($ret, 'title', 'id');
64 foreach($ret as $k => $v) {
65 $ret[$k] = $translator->trans($v);
73 * Fetch translated location title with session by date period
75 * @param $translator The TranslatorInterface instance
76 * @param $period The date period
77 * @param $granted The session is granted
80 public function fetchTranslatedLocationByDatePeriod(TranslatorInterface
$translator, $period, $granted = false) {
82 $ret = $this->getEntityManager()
83 ->createQuery('SELECT l.id, l.title FROM RapsysAirBundle:Session s JOIN RapsysAirBundle:Location l WHERE '.($granted?'s.application IS NOT NULL AND ':'').'l.id = s.location AND s.date BETWEEN :begin AND :end GROUP BY l.id ORDER BY l.id')
84 ->setParameter('begin', $period->getStartDate())
85 ->setParameter('end', $period->getEndDate())
89 $ret = array_column($ret, 'title', 'id');
92 foreach($ret as $k => $v) {
93 $ret[$k] = $translator->trans($v);
101 * Fetch translated location title with user session by date period
103 * @param $translator The TranslatorInterface instance
104 * @param $period The date period
105 * @param $userId The user uid
108 public function fetchTranslatedUserLocationByDatePeriod(TranslatorInterface
$translator, $period, $userId) {
110 $ret = $this->getEntityManager()
111 ->createQuery('SELECT l.id, l.title FROM RapsysAirBundle:Application a JOIN RapsysAirBundle:Session s JOIN RapsysAirBundle:Location l WHERE a.user = :uid AND a.session = s.id AND s.date BETWEEN :begin AND :end AND s.location = l.id GROUP BY l.id ORDER BY l.id')
112 ->setParameter('begin', $period->getStartDate())
113 ->setParameter('end', $period->getEndDate())
114 ->setParameter('uid', $userId)
118 $ret = array_column($ret, 'title', 'id');
121 foreach($ret as $k => $v) {
122 $ret[$k] = $translator->trans($v);
130 * Find locations by user id
132 * @param $id The user id
133 * @return array The user locations
135 public function findByUserId($userId) {
137 $em = $this->getEntityManager();
140 $qs = $em->getConfiguration()->getQuoteStrategy();
141 $dp = $em->getConnection()->getDatabasePlatform();
143 //Get quoted table names
144 //XXX: this allow to make this code table name independent
146 'RapsysAirBundle:UserLocation' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('locations'), $em->getClassMetadata('RapsysAirBundle:User'), $dp),
147 'RapsysAirBundle:Location' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Location'), $dp)
151 $req = 'SELECT l.id, l.title
152 FROM RapsysAirBundle:UserLocation AS ul
153 JOIN RapsysAirBundle:Location AS l ON (l.id = ul.location_id)
154 WHERE ul.user_id = :uid';
156 //Replace bundle entity name by table name
157 $req = str_replace(array_keys($tables), array_values($tables), $req);
159 //Get result set mapping instance
160 //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
161 $rsm = new ResultSetMapping();
163 //Declare result set for our request
164 $rsm->addEntityResult('RapsysAirBundle:Location', 'l');
165 $rsm->addFieldResult('l', 'id', 'id');
166 $rsm->addFieldResult('l', 'title', 'title');
170 ->createNativeQuery($req, $rsm)
171 ->setParameter('uid', $userId)