X-Git-Url: https://git.rapsys.eu/airbundle/blobdiff_plain/b27f01bf7d1af9cd1bdd4b39ed1718b434b136db..3ae948f1e2bcd932dee8b7ade9c1d151a617d776:/Repository/LocationRepository.php diff --git a/Repository/LocationRepository.php b/Repository/LocationRepository.php index 234d0e2..129a176 100644 --- a/Repository/LocationRepository.php +++ b/Repository/LocationRepository.php @@ -3,6 +3,7 @@ namespace Rapsys\AirBundle\Repository; use Symfony\Component\Translation\TranslatorInterface; +use Doctrine\ORM\Query\ResultSetMapping; /** * LocationRepository @@ -28,12 +29,54 @@ class LocationRepository extends \Doctrine\ORM\EntityRepository { } /** - * Fetch translated location with session by date period + * Find translated location title sorted by date period * * @param $translator The TranslatorInterface instance * @param $period The date period * @param $granted The session is granted */ + public function findTranslatedSortedByPeriod(TranslatorInterface $translator, $period, $userId = null) { + //Fetch sessions + $ret = $this->getEntityManager() + ->createQuery( +'SELECT l.id, l.title +FROM RapsysAirBundle:Location l +LEFT JOIN RapsysAirBundle:Session s WITH s.location = l.id AND s.date BETWEEN :begin AND :end +LEFT JOIN RapsysAirBundle:Application a WITH a.id = s.application'.(!empty($userId)?' AND a.user = :uid':'').' +GROUP BY l.id +ORDER BY '.(!empty($userId)?'COUNT(a.id) DESC, ':'').'COUNT(s.id) DESC, l.id' + ) + ->setParameter('begin', $period->getStartDate()) + ->setParameter('end', $period->getEndDate()); + + //Set optional user id + if (!empty($userId)) { + $ret->setParameter('uid', $userId); + } + + //Get Result + $ret = $ret->getResult(); + + //Rekey array + $ret = array_column($ret, 'title', 'id'); + + //Filter array + foreach($ret as $k => $v) { + $ret[$k] = $translator->trans($v); + } + + //Send result + return $ret; + } + + /** + * Fetch translated location title with session by date period + * + * @param $translator The TranslatorInterface instance + * @param $period The date period + * @param $granted The session is granted + * TODO: a dropper + */ public function fetchTranslatedLocationByDatePeriod(TranslatorInterface $translator, $period, $granted = false) { //Fetch sessions $ret = $this->getEntityManager() @@ -55,17 +98,17 @@ class LocationRepository extends \Doctrine\ORM\EntityRepository { } /** - * Fetch translated user location with session by date period + * Fetch translated location title with user session by date period * * @param $translator The TranslatorInterface instance * @param $period The date period * @param $userId The user uid + * TODO: a dropper */ public function fetchTranslatedUserLocationByDatePeriod(TranslatorInterface $translator, $period, $userId) { //Fetch sessions $ret = $this->getEntityManager() - ->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') -#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 + ->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') ->setParameter('begin', $period->getStartDate()) ->setParameter('end', $period->getEndDate()) ->setParameter('uid', $userId) @@ -82,4 +125,50 @@ class LocationRepository extends \Doctrine\ORM\EntityRepository { //Send result return $ret; } + + /** + * Find locations by user id + * + * @param $id The user id + * @return array The user locations + */ + public function findByUserId($userId) { + //Get entity manager + $em = $this->getEntityManager(); + + //Get quote strategy + $qs = $em->getConfiguration()->getQuoteStrategy(); + $dp = $em->getConnection()->getDatabasePlatform(); + + //Get quoted table names + //XXX: this allow to make this code table name independent + $tables = [ + 'RapsysAirBundle:UserLocation' => $qs->getJoinTableName($em->getClassMetadata('RapsysAirBundle:User')->getAssociationMapping('locations'), $em->getClassMetadata('RapsysAirBundle:User'), $dp), + 'RapsysAirBundle:Location' => $qs->getTableName($em->getClassMetadata('RapsysAirBundle:Location'), $dp) + ]; + + //Set the request + $req = 'SELECT l.id, l.title +FROM RapsysAirBundle:UserLocation AS ul +JOIN RapsysAirBundle:Location AS l ON (l.id = ul.location_id) +WHERE ul.user_id = :uid'; + + //Replace bundle entity name by table name + $req = str_replace(array_keys($tables), array_values($tables), $req); + + //Get result set mapping instance + //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php + $rsm = new ResultSetMapping(); + + //Declare result set for our request + $rsm->addEntityResult('RapsysAirBundle:Location', 'l'); + $rsm->addFieldResult('l', 'id', 'id'); + $rsm->addFieldResult('l', 'title', 'title'); + + //Send result + return $em + ->createNativeQuery($req, $rsm) + ->setParameter('uid', $userId) + ->getResult(); + } }