]> Raphaƫl G. Git Repositories - airbundle/blobdiff - Repository/LocationRepository.php
Shorten air bundle route aliases
[airbundle] / Repository / LocationRepository.php
index d8acc143009e4d10c43299bd6ea073d644ba039d..4c479b41ff2c4e54e4e76f20dbdccbb9efe46d4b 100644 (file)
 namespace Rapsys\AirBundle\Repository;
 
 use Doctrine\ORM\Query\ResultSetMapping;
+
 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
 use Symfony\Component\Routing\RouterInterface;
 
+use Rapsys\AirBundle\Repository;
+
 /**
  * LocationRepository
  *
  * @TODO: use new window function syntax https://mariadb.com/kb/en/window-functions-overview/ MAX(updated) OVER (PARTITION updated) AS modified ???
  */
-class LocationRepository extends EntityRepository {
+class LocationRepository extends Repository {
        /**
         * Find locations
         *
@@ -48,8 +51,8 @@ SELECT
        l.longitude,
        l.indoor,
        l.updated
-FROM RapsysAirBundle:Location AS l
-LEFT JOIN RapsysAirBundle:Session AS s ON (l.id = s.location_id)
+FROM Rapsys\AirBundle\Entity\Location AS l
+LEFT JOIN Rapsys\AirBundle\Entity\Session AS s ON (l.id = s.location_id)
 GROUP BY l.id
 ORDER BY COUNT(IF(s.date BETWEEN :begin AND :end, s.id, NULL)) DESC, COUNT(s.id) DESC, l.id
 SQL;
@@ -93,7 +96,7 @@ SQL;
                                'updated' => $data['updated'],
                                //XXX: Useless ???
                                'slug' => $location = $this->slugger->slug($title),
-                               'link' => $this->router->generate('rapsys_air_location_view', ['id' => $data['id'], 'location' => $this->slugger->slug($location)])
+                               'link' => $this->router->generate('rapsysair_location_view', ['id' => $data['id'], 'location' => $this->slugger->slug($location)])
                        ];
                }
 
@@ -122,7 +125,7 @@ SELECT
        GROUP_CONCAT(a.longitude ORDER BY a.pcount DESC, a.count DESC, a.id SEPARATOR "\\n") AS longitudes,
        GROUP_CONCAT(a.indoor ORDER BY a.pcount DESC, a.count DESC, a.id SEPARATOR "\\n") AS indoors,
        GROUP_CONCAT(a.count ORDER BY a.pcount DESC, a.count DESC, a.id SEPARATOR "\\n") AS counts,
-       MAX(a.updated) AS modified
+       MAX(a.modified) AS modified
 FROM (
        SELECT
                l.id,
@@ -131,18 +134,18 @@ FROM (
                l.latitude,
                l.longitude,
                l.indoor,
-               l.updated,
+               GREATEST(l.created, l.updated, COALESCE(s.created, 0), COALESCE(s.updated, 0)) AS modified,
                l.zipcode,
                COUNT(s.id) AS count,
                COUNT(IF(s.date BETWEEN :begin AND :end, s.id, NULL)) AS pcount
-       FROM RapsysAirBundle:Location AS l
-       LEFT JOIN RapsysAirBundle:Session AS s ON (l.id = s.location_id)
+       FROM Rapsys\AirBundle\Entity\Location AS l
+       LEFT JOIN Rapsys\AirBundle\Entity\Session AS s ON (l.id = s.location_id)
        GROUP BY l.id
        ORDER BY NULL
        LIMIT 0, :limit
 ) AS a
-GROUP BY a.city, SUBSTRING(a.zipcode, 1, 3)
-ORDER BY a.city, SUBSTRING(a.zipcode, 1, 3)
+GROUP BY a.city, SUBSTRING(a.zipcode, 1, 2)
+ORDER BY a.city, a.zipcode
 SQL;
 
                //Replace bundle entity name by table name
@@ -227,7 +230,7 @@ SQL;
                                        'latitude' => floatval($latitudes[$k]),
                                        'longitude' => floatval($longitudes[$k]),
                                        'indoor' => $indoors[$k] == 0 ? $this->translator->trans('outdoor') : $this->translator->trans('indoor'),
-                                       'link' => $this->router->generate('rapsys_air_location_view', ['id' => $id, 'location' => $this->slugger->slug($location)])
+                                       'link' => $this->router->generate('rapsysair_location_view', ['id' => $id, 'location' => $this->slugger->slug($location)])
                                ];
                        }
 
@@ -243,7 +246,7 @@ SQL;
                                'modified' => $data['modified'],
                                //XXX: Useless ???
                                'slug' => $city = $this->slugger->slug($data['city']),
-                               'link' => $this->router->generate('rapsys_air_city_view', ['city' => $city, 'latitude' => $data['latitude'], 'longitude' => $data['longitude']]),
+                               'link' => $this->router->generate('rapsysair_city_view', ['city' => $city, 'latitude' => $data['latitude'], 'longitude' => $data['longitude']]),
                                'locations' => $data['locations']
                        ];
                }
@@ -268,8 +271,8 @@ SELECT
        ROUND(AVG(l.latitude), 6) AS latitude,
        ROUND(AVG(l.longitude), 6) AS longitude,
        MAX(l.updated) AS updated
-FROM RapsysAirBundle:Location AS l
-GROUP BY city, SUBSTRING(l.zipcode, 1, 3)
+FROM Rapsys\AirBundle\Entity\Location AS l
+GROUP BY city, SUBSTRING(l.zipcode, 1, 2)
 ORDER BY ACOS(SIN(RADIANS(:latitude))*SIN(RADIANS(l.latitude))+COS(RADIANS(:latitude))*COS(RADIANS(l.latitude))*COS(RADIANS(:longitude - l.longitude)))*40030.17/2/PI()
 LIMIT 0, 1
 SQL;
@@ -315,7 +318,7 @@ SQL;
                        'multimap' => $this->translator->trans($result['city'].' sector map'),
                        //XXX: Useless ???
                        'slug' => $slug = $this->slugger->slug($result['city']),
-                       'link' => $this->router->generate('rapsys_air_city_view', ['city' => $slug, 'latitude' => $result['latitude'], 'longitude' => $result['longitude']])
+                       'link' => $this->router->generate('rapsysair_city_view', ['city' => $slug, 'latitude' => $result['latitude'], 'longitude' => $result['longitude']])
                ];
        }
 
@@ -357,8 +360,9 @@ SELECT
        a.title,
        a.latitude,
        a.longitude,
+       a.created,
        a.updated,
-       MAX(a.updated) AS modified,
+       MAX(GREATEST(a.modified, COALESCE(s.created, 0), COALESCE(s.updated, 0))) AS modified,
        COUNT(s.id) AS count
 FROM (
        SELECT
@@ -366,12 +370,14 @@ FROM (
                l.title,
                l.latitude,
                l.longitude,
-               l.updated
-       FROM RapsysAirBundle:Location AS l
+               l.created,
+               l.updated,
+               GREATEST(l.created, l.updated) AS modified
+       FROM Rapsys\AirBundle\Entity\Location AS l
        WHERE l.latitude BETWEEN :minlat AND :maxlat AND l.longitude BETWEEN :minlong AND :maxlong
        LIMIT 0, :limit
 ) AS a
-LEFT JOIN RapsysAirBundle:Session s ON (s.location_id = a.id)
+LEFT JOIN Rapsys\AirBundle\Entity\Session s ON (s.location_id = a.id)
 GROUP BY a.id
 ORDER BY COUNT(IF(s.date BETWEEN :begin AND :end, s.id, NULL)) DESC, count DESC, a.id
 SQL;
@@ -390,6 +396,7 @@ SQL;
                        ->addScalarResult('title', 'title', 'string')
                        ->addScalarResult('latitude', 'latitude', 'float')
                        ->addScalarResult('longitude', 'longitude', 'float')
+                       ->addScalarResult('created', 'created', 'datetime')
                        ->addScalarResult('updated', 'updated', 'datetime')
                        ->addScalarResult('modified', 'modified', 'datetime')
                        ->addScalarResult('count', 'count', 'integer');
@@ -422,11 +429,12 @@ SQL;
                                'title' => $title = $this->translator->trans($data['title']),
                                'latitude' => $data['latitude'],
                                'longitude' => $data['longitude'],
+                               'created' => $data['created'],
                                'updated' => $data['updated'],
                                'modified' => $data['modified'],
                                'count' => $data['count'],
                                'slug' => $slug = $this->slugger->slug($title),
-                               'link' => $this->router->generate('rapsys_air_location_view', ['id' => $data['id'], 'location' => $slug])
+                               'link' => $this->router->generate('rapsysair_location_view', ['id' => $data['id'], 'location' => $slug])
                        ];
                }
 
@@ -556,7 +564,7 @@ SQL;
                                'tcount' => $data['tcount'],
                                'modified' => $data['modified'],
                                'slug' => $slug = $this->slugger->slug($title),
-                               'link' => $this->router->generate('rapsys_air_location_view', ['id' => $data['id'], 'location' => $slug])
+                               'link' => $this->router->generate('rapsysair_location_view', ['id' => $data['id'], 'location' => $slug])
                        ];
                }
 
@@ -586,8 +594,8 @@ SELECT
        SUBSTRING(l.zipcode, 1, 2) AS city_id,
        ROUND(AVG(l2.latitude), 6) AS city_latitude,
        ROUND(AVG(l2.longitude), 6) AS city_longitude
-FROM RapsysAirBundle:Location AS l
-JOIN RapsysAirBundle:Location AS l2 ON (l2.city = l.city AND SUBSTRING(l.zipcode, 1, 3) = SUBSTRING(l.zipcode, 1, 3))
+FROM Rapsys\AirBundle\Entity\Location AS l
+JOIN Rapsys\AirBundle\Entity\Location AS l2 ON (l2.city = l.city AND SUBSTRING(l2.zipcode, 1, 3) = SUBSTRING(l.zipcode, 1, 3))
 WHERE l.id = :id
 GROUP BY l.id
 LIMIT 0, 1
@@ -632,7 +640,7 @@ SQL;
                $result['alternates'] = [];
 
                //Set route
-               $route = 'rapsys_air_location_view';
+               $route = 'rapsysair_location_view';
 
                //Set route params
                $routeParams = ['id' => $id];
@@ -676,7 +684,7 @@ SQL;
                                'id' => $result['city_id'],
                                'title' => $result['city'],
                                'in' => $this->translator->trans('in '.$result['city']),
-                               'link' => $this->router->generate('rapsys_air_city_view', ['city' => $result['city'], 'latitude' => $result['city_latitude'], 'longitude' => $result['city_longitude']])
+                               'link' => $this->router->generate('rapsysair_city_view', ['city' => $result['city'], 'latitude' => $result['city_latitude'], 'longitude' => $result['city_longitude']])
                        ],
                        'title' => $title = $this->translator->trans($result['title']),
                        'latitude' => $result['latitude'],
@@ -703,7 +711,7 @@ SQL;
        public function findComplementBySessionId(int $id): array {
                //Fetch complement locations
                $ret = $this->getEntityManager()
-                         ->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')
+                         ->createQuery('SELECT l.id, l.title FROM Rapsys\AirBundle\Entity\Session s LEFT JOIN Rapsys\AirBundle\Entity\Session s2 WITH s2.id != s.id AND s2.slot = s.slot AND s2.date = s.date LEFT JOIN Rapsys\AirBundle\Entity\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')
                        ->setParameter('sid', $id)
                        ->getArrayResult();
 
@@ -725,8 +733,8 @@ SQL;
        public function findByUserId(int $userId): array {
                //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)
+FROM Rapsys\AirBundle\Entity\UserLocation AS ul
+JOIN Rapsys\AirBundle\Entity\Location AS l ON (l.id = ul.location_id)
 WHERE ul.user_id = :id';
 
                //Replace bundle entity name by table name
@@ -737,7 +745,7 @@ WHERE ul.user_id = :id';
                $rsm = new ResultSetMapping();
 
                //Declare result set for our request
-               $rsm->addEntityResult('RapsysAirBundle:Location', 'l')
+               $rsm->addEntityResult('Rapsys\AirBundle\Entity\Location', 'l')
                        ->addFieldResult('l', 'id', 'id')
                        ->addFieldResult('l', 'title', 'title');