]> Raphaƫl G. Git Repositories - airbundle/blob - Repository/LocationRepository.php
New layout
[airbundle] / Repository / LocationRepository.php
1 <?php
2
3 namespace Rapsys\AirBundle\Repository;
4
5 use Symfony\Component\Translation\TranslatorInterface;
6
7 /**
8 * LocationRepository
9 */
10 class LocationRepository extends \Doctrine\ORM\EntityRepository {
11 /**
12 * Fetch translated location with session by date period
13 *
14 * @param $translator The TranslatorInterface instance
15 * @param $period The date period
16 * @param $granted The session is granted
17 */
18 public function fetchTranslatedLocationByDatePeriod(TranslatorInterface $translator, $period, $granted = false) {
19 //Fetch sessions
20 $ret = $this->getEntityManager()
21 ->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')
22 ->setParameter('begin', $period->getStartDate())
23 ->setParameter('end', $period->getEndDate())
24 ->getResult();
25
26 //Rekey array
27 $ret = array_column($ret, 'title', 'id');
28
29 //Filter array
30 foreach($ret as $k => $v) {
31 $ret[$k] = $translator->trans($v);
32 }
33
34 //Send result
35 return $ret;
36 }
37
38 /**
39 * Fetch translated user location with session by date period
40 *
41 * @param $translator The TranslatorInterface instance
42 * @param $period The date period
43 * @param $userId The user uid
44 */
45 public function fetchTranslatedUserLocationByDatePeriod(TranslatorInterface $translator, $period, $userId) {
46 //Fetch sessions
47 $ret = $this->getEntityManager()
48 ->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')
49 #SELECT l.id, l.title FROM RapsysAirBundle:Session s JOIN RapsysAirBundle:Application a JOIN RapsysAirBundle:Location l WHERE s.date BETWEEN :begin AND :end AND s.id = a.session AND l.id = s.location GROUP BY l.id ORDER BY l.id
50 ->setParameter('begin', $period->getStartDate())
51 ->setParameter('end', $period->getEndDate())
52 ->setParameter('uid', $userId)
53 ->getResult();
54
55 //Rekey array
56 $ret = array_column($ret, 'title', 'id');
57
58 //Filter array
59 foreach($ret as $k => $v) {
60 $ret[$k] = $translator->trans($v);
61 }
62
63 //Send result
64 return $ret;
65 }
66 }