]> Raphaƫl G. Git Repositories - airbundle/blobdiff - Repository/LocationRepository.php
Rename rapsysair:calendar2 command to rapsysair:calendar
[airbundle] / Repository / LocationRepository.php
index 1ac821c7553124825c253631bbf7b32219d83eba..9b621ef3cafe2b073c511ee42867c2bd665bf8e4 100644 (file)
@@ -96,7 +96,7 @@ SQL;
                                'updated' => $data['updated'],
                                //XXX: Useless ???
                                'slug' => $location = $this->slugger->slug($title),
                                '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)])
                        ];
                }
 
                        ];
                }
 
@@ -125,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,
        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,
 FROM (
        SELECT
                l.id,
@@ -134,7 +134,7 @@ FROM (
                l.latitude,
                l.longitude,
                l.indoor,
                l.latitude,
                l.longitude,
                l.indoor,
-               l.updated,
+               GREATEST(l.created, l.updated, COALESCE(s.created, '1970-01-01'), COALESCE(s.updated, '1970-01-01')) AS modified,
                l.zipcode,
                COUNT(s.id) AS count,
                COUNT(IF(s.date BETWEEN :begin AND :end, s.id, NULL)) AS pcount
                l.zipcode,
                COUNT(s.id) AS count,
                COUNT(IF(s.date BETWEEN :begin AND :end, s.id, NULL)) AS pcount
@@ -144,8 +144,8 @@ FROM (
        ORDER BY NULL
        LIMIT 0, :limit
 ) AS a
        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
 SQL;
 
                //Replace bundle entity name by table name
@@ -230,7 +230,7 @@ SQL;
                                        'latitude' => floatval($latitudes[$k]),
                                        'longitude' => floatval($longitudes[$k]),
                                        'indoor' => $indoors[$k] == 0 ? $this->translator->trans('outdoor') : $this->translator->trans('indoor'),
                                        '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)])
                                ];
                        }
 
                                ];
                        }
 
@@ -246,7 +246,7 @@ SQL;
                                'modified' => $data['modified'],
                                //XXX: Useless ???
                                'slug' => $city = $this->slugger->slug($data['city']),
                                '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']
                        ];
                }
                                'locations' => $data['locations']
                        ];
                }
@@ -272,7 +272,7 @@ SELECT
        ROUND(AVG(l.longitude), 6) AS longitude,
        MAX(l.updated) AS updated
 FROM Rapsys\AirBundle\Entity\Location AS l
        ROUND(AVG(l.longitude), 6) AS longitude,
        MAX(l.updated) AS updated
 FROM Rapsys\AirBundle\Entity\Location AS l
-GROUP BY city, SUBSTRING(l.zipcode, 1, 3)
+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;
 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;
@@ -318,7 +318,7 @@ SQL;
                        'multimap' => $this->translator->trans($result['city'].' sector map'),
                        //XXX: Useless ???
                        'slug' => $slug = $this->slugger->slug($result['city']),
                        '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']])
                ];
        }
 
                ];
        }
 
@@ -360,8 +360,9 @@ SELECT
        a.title,
        a.latitude,
        a.longitude,
        a.title,
        a.latitude,
        a.longitude,
+       a.created,
        a.updated,
        a.updated,
-       MAX(a.updated) AS modified,
+       MAX(GREATEST(a.modified, COALESCE(s.created, '1970-01-01'), COALESCE(s.updated, '1970-01-01'))) AS modified,
        COUNT(s.id) AS count
 FROM (
        SELECT
        COUNT(s.id) AS count
 FROM (
        SELECT
@@ -369,7 +370,9 @@ FROM (
                l.title,
                l.latitude,
                l.longitude,
                l.title,
                l.latitude,
                l.longitude,
-               l.updated
+               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
        FROM Rapsys\AirBundle\Entity\Location AS l
        WHERE l.latitude BETWEEN :minlat AND :maxlat AND l.longitude BETWEEN :minlong AND :maxlong
        LIMIT 0, :limit
@@ -393,6 +396,7 @@ SQL;
                        ->addScalarResult('title', 'title', 'string')
                        ->addScalarResult('latitude', 'latitude', 'float')
                        ->addScalarResult('longitude', 'longitude', 'float')
                        ->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');
                        ->addScalarResult('updated', 'updated', 'datetime')
                        ->addScalarResult('modified', 'modified', 'datetime')
                        ->addScalarResult('count', 'count', 'integer');
@@ -425,11 +429,12 @@ SQL;
                                'title' => $title = $this->translator->trans($data['title']),
                                'latitude' => $data['latitude'],
                                'longitude' => $data['longitude'],
                                '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),
                                '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])
                        ];
                }
 
                        ];
                }
 
@@ -454,8 +459,9 @@ SELECT
        a.city,
        a.latitude,
        a.longitude,
        a.city,
        a.latitude,
        a.longitude,
+       a.created,
        a.updated,
        a.updated,
-       a.modified,
+       MAX(GREATEST(a.modified, COALESCE(s3.created, '1970-01-01'), COALESCE(s3.updated, '1970-01-01'))) AS modified,
        a.pcount,
        COUNT(s3.id) AS tcount
 FROM (
        a.pcount,
        COUNT(s3.id) AS tcount
 FROM (
@@ -465,9 +471,10 @@ FROM (
                b.city,
                b.latitude,
                b.longitude,
                b.city,
                b.latitude,
                b.longitude,
+               b.created,
                b.updated,
                b.updated,
-               COUNT(s2.id) AS pcount,
-               MAX(b.updated) AS modified
+               MAX(GREATEST(b.modified, COALESCE(s2.created, '1970-01-01'), COALESCE(s2.updated, '1970-01-01'))) AS modified,
+               COUNT(s2.id) AS pcount
        FROM (
                SELECT
                        l2.id,
        FROM (
                SELECT
                        l2.id,
@@ -475,7 +482,9 @@ FROM (
                        l2.title,
                        l2.latitude,
                        l2.longitude,
                        l2.title,
                        l2.latitude,
                        l2.longitude,
-                       l2.updated
+                       l2.created,
+                       l2.updated,
+                       GREATEST(l2.created, l2.updated) AS modified
                FROM (
                        SELECT
                                l.id,
                FROM (
                        SELECT
                                l.id,
@@ -520,10 +529,11 @@ SQL;
                        ->addScalarResult('city', 'city', 'string')
                        ->addScalarResult('latitude', 'latitude', 'float')
                        ->addScalarResult('longitude', 'longitude', 'float')
                        ->addScalarResult('city', 'city', 'string')
                        ->addScalarResult('latitude', 'latitude', 'float')
                        ->addScalarResult('longitude', 'longitude', 'float')
+                       ->addScalarResult('created', 'created', 'datetime')
                        ->addScalarResult('updated', 'updated', 'datetime')
                        ->addScalarResult('updated', 'updated', 'datetime')
+                       ->addScalarResult('modified', 'modified', 'datetime')
                        ->addScalarResult('pcount', 'pcount', 'integer')
                        ->addScalarResult('pcount', 'pcount', 'integer')
-                       ->addScalarResult('tcount', 'tcount', 'integer')
-                       ->addScalarResult('modified', 'modified', 'datetime');
+                       ->addScalarResult('tcount', 'tcount', 'integer');
 
                //Get result
                $result = $this->_em
 
                //Get result
                $result = $this->_em
@@ -554,12 +564,13 @@ SQL;
                                'miniature' => $this->translator->trans($data['title'].' miniature'),
                                'latitude' => $data['latitude'],
                                'longitude' => $data['longitude'],
                                'miniature' => $this->translator->trans($data['title'].' miniature'),
                                'latitude' => $data['latitude'],
                                'longitude' => $data['longitude'],
+                               'created' => $data['created'],
                                'updated' => $data['updated'],
                                'updated' => $data['updated'],
+                               'modified' => $data['modified'],
                                'pcount' => $data['pcount'],
                                'tcount' => $data['tcount'],
                                'pcount' => $data['pcount'],
                                'tcount' => $data['tcount'],
-                               'modified' => $data['modified'],
                                'slug' => $slug = $this->slugger->slug($title),
                                '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])
                        ];
                }
 
                        ];
                }
 
@@ -585,12 +596,12 @@ SELECT
        l.longitude,
        l.indoor,
        l.zipcode,
        l.longitude,
        l.indoor,
        l.zipcode,
-       MAX(l2.updated) AS updated,
+       MAX(GREATEST(l.created, l.updated, l2.created, l2.updated)) AS modified,
        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 Rapsys\AirBundle\Entity\Location AS l
        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 Rapsys\AirBundle\Entity\Location AS l
-JOIN Rapsys\AirBundle\Entity\Location AS l2 ON (l2.city = l.city AND SUBSTRING(l.zipcode, 1, 3) = SUBSTRING(l.zipcode, 1, 3))
+JOIN Rapsys\AirBundle\Entity\Location AS l2 ON (l2.city = l.city AND SUBSTRING(l2.zipcode, 1, 2) = SUBSTRING(l.zipcode, 1, 2))
 WHERE l.id = :id
 GROUP BY l.id
 LIMIT 0, 1
 WHERE l.id = :id
 GROUP BY l.id
 LIMIT 0, 1
@@ -613,7 +624,7 @@ SQL;
                        ->addScalarResult('longitude', 'longitude', 'float')
                        ->addScalarResult('indoor', 'indoor', 'boolean')
                        ->addScalarResult('zipcode', 'zipcode', 'string')
                        ->addScalarResult('longitude', 'longitude', 'float')
                        ->addScalarResult('indoor', 'indoor', 'boolean')
                        ->addScalarResult('zipcode', 'zipcode', 'string')
-                       ->addScalarResult('updated', 'updated', 'datetime')
+                       ->addScalarResult('modified', 'modified', 'datetime')
                        ->addScalarResult('city_id', 'city_id', 'integer')
                        ->addScalarResult('city_latitude', 'city_latitude', 'float')
                        ->addScalarResult('city_longitude', 'city_longitude', 'float')
                        ->addScalarResult('city_id', 'city_id', 'integer')
                        ->addScalarResult('city_latitude', 'city_latitude', 'float')
                        ->addScalarResult('city_longitude', 'city_longitude', 'float')
@@ -635,7 +646,7 @@ SQL;
                $result['alternates'] = [];
 
                //Set route
                $result['alternates'] = [];
 
                //Set route
-               $route = 'rapsys_air_location_view';
+               $route = 'rapsysair_location_view';
 
                //Set route params
                $routeParams = ['id' => $id];
 
                //Set route params
                $routeParams = ['id' => $id];
@@ -679,13 +690,13 @@ SQL;
                                'id' => $result['city_id'],
                                'title' => $result['city'],
                                'in' => $this->translator->trans('in '.$result['city']),
                                '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'],
                        'longitude' => $result['longitude'],
                        'indoor' => $result['indoor'],
                        ],
                        'title' => $title = $this->translator->trans($result['title']),
                        'latitude' => $result['latitude'],
                        'longitude' => $result['longitude'],
                        'indoor' => $result['indoor'],
-                       'updated' => $result['updated'],
+                       'modified' => $result['modified'],
                        'around' => $this->translator->trans('around '.$result['title']),
                        'at' => $this->translator->trans('at '.$result['title']),
                        'atin' => $this->translator->trans('at '.$result['title']).' '.$this->translator->trans('in '.$result['city']),
                        'around' => $this->translator->trans('around '.$result['title']),
                        'at' => $this->translator->trans('at '.$result['title']),
                        'atin' => $this->translator->trans('at '.$result['title']).' '.$this->translator->trans('in '.$result['city']),
@@ -705,18 +716,15 @@ SQL;
         */
        public function findComplementBySessionId(int $id): array {
                //Fetch complement locations
         */
        public function findComplementBySessionId(int $id): array {
                //Fetch complement locations
-               $ret = $this->getEntityManager()
-                         ->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();
-
-               //TODO: try to improve with:
-               #->addIndexByScalar('city');
-
-               //Rekey array
-               $ret = array_column($ret, 'id', 'title');
-
-               return $ret;
+               return array_column(
+                       $this->getEntityManager()
+                               #->createQuery('SELECT l.id, l.title FROM Rapsys\AirBundle\Entity\Location l JOIN Rapsys\AirBundle\Entity\Session s WITH s.id = :sid LEFT JOIN Rapsys\AirBundle\Entity\Session s2 WITH s2.id != s.id AND s2.slot = s.slot AND s2.date = s.date WHERE l.id != s.location AND s2.location IS NULL 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(),
+                       'id',
+                       'title'
+               );
        }
 
        /**
        }
 
        /**