]> Raphaƫl G. Git Repositories - airbundle/blobdiff - Controller/LocationController.php
Add created field to findAllIndexed results
[airbundle] / Controller / LocationController.php
index 3631826232dbab13c5eae1407a0d10d01bbf00a5..9ee9471af6ceda4badb0f3086cdfd35a3d025727 100644 (file)
@@ -13,15 +13,10 @@ namespace Rapsys\AirBundle\Controller;
 
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
-use Symfony\Component\Routing\Exception\MethodNotAllowedException;
-use Symfony\Component\Routing\Exception\ResourceNotFoundException;
-use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
-use Symfony\Component\Routing\RequestContext;
 
 use Rapsys\AirBundle\Entity\Dance;
 use Rapsys\AirBundle\Entity\Location;
 use Rapsys\AirBundle\Entity\Session;
-use Rapsys\AirBundle\Entity\Slot;
 
 /**
  * {@inheritdoc}
@@ -35,7 +30,7 @@ class LocationController extends DefaultController {
         */
        public function cities(Request $request): Response {
                //Add cities
-               $this->context['cities'] = $this->doctrine->getRepository(Location::class)->findCitiesAsArray($this->period);
+               $this->context['cities'] = $this->doctrine->getRepository(Location::class)->findCitiesAsArray($this->period, 0);
 
                //Add dances
                $this->context['dances'] = $this->doctrine->getRepository(Dance::class)->findNamesAsArray();
@@ -49,12 +44,11 @@ class LocationController extends DefaultController {
                //Add city multi
                foreach($this->context['cities'] as $id => $city) {
                        //Add city multi
-                       #$this->osm->getMultiImage($city['link'], $city['osm'], $this->modified->getTimestamp(), $city['latitude'], $city['longitude'], $city['locations'], $this->osm->getMultiZoom($city['latitude'], $city['longitude'], $city['locations'], 16));
-                       $this->context['cities'][$id]['multimap'] = $this->map->getMultiMap($city['multimap'], $this->modified->getTimestamp(), $city['latitude'], $city['longitude'], $city['locations'], $this->map->getMultiZoom($city['latitude'], $city['longitude'], $city['locations']));
+                       $this->context['cities'][$id]['multimap'] = $this->map->getMultiMap($city['multimap'], $this->modified->getTimestamp(), $city['locations']);
                }
 
                //With logged user
-               if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
+               if ($this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
                        //Set last modified
                        $response->setLastModified(new \DateTime('-1 year'));
 
@@ -80,7 +74,7 @@ class LocationController extends DefaultController {
                }
 
                //Set section
-               $this->context['title'] = $this->translator->trans('Libre Air cities');
+               $this->context['title']['page'] = $this->translator->trans('Libre Air cities');
 
                //Set description
                $this->context['description'] = $this->translator->trans('Libre Air city list');
@@ -134,7 +128,7 @@ class LocationController extends DefaultController {
                }
 
                //Add calendar
-               $this->context['calendar'] = $this->doctrine->getRepository(Session::class)->findAllByPeriodAsArray($this->period, $request->getLocale(), !$this->isGranted('IS_AUTHENTICATED_REMEMBERED'), floatval($latitude), floatval($longitude));
+               $this->context['calendar'] = $this->doctrine->getRepository(Session::class)->findAllByPeriodAsCalendarArray($this->period, !$this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED'), floatval($latitude), floatval($longitude));
 
                //Set dances
                $this->context['dances'] = [];
@@ -152,7 +146,7 @@ class LocationController extends DefaultController {
                }
 
                //Add locations
-               $this->context['locations'] = $this->doctrine->getRepository(Location::class)->findAllByLatitudeLongitudeAsArray(floatval($latitude), floatval($longitude), $this->period);
+               $this->context['locations'] = $this->doctrine->getRepository(Location::class)->findAllByLatitudeLongitudeAsArray(floatval($latitude), floatval($longitude), $this->period, 0);
 
                //Set modified
                //XXX: dance modified is already computed inside calendar modified
@@ -162,7 +156,7 @@ class LocationController extends DefaultController {
                $response = new Response();
 
                //With logged user
-               if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
+               if ($this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
                        //Set last modified
                        $response->setLastModified(new \DateTime('-1 year'));
 
@@ -188,7 +182,7 @@ class LocationController extends DefaultController {
                }
 
                //Add multi
-               $this->context['multimap'] = $this->map->getMultiMap($this->context['city']['multimap'], $this->modified->getTimestamp(), $latitude, $longitude, $this->context['locations'], $this->map->getMultiZoom($latitude, $longitude, $this->context['locations']));
+               $this->context['multimap'] = $this->map->getMultiMap($this->context['city']['multimap'], $this->modified->getTimestamp(), $this->context['locations']);
 
                //Set keywords
                $this->context['keywords'] = [
@@ -211,13 +205,13 @@ class LocationController extends DefaultController {
                        $dances = implode($this->translator->trans(' and '), array_filter(array_merge([implode(', ', array_slice($dances, 0, -1))], array_slice($dances, -1)), 'strlen'));
 
                        //Set title
-                       $this->context['title'] = $this->translator->trans('%dances% %city%', ['%dances%' => $dances, '%city%' => $this->context['city']['in']]);
+                       $this->context['title']['page'] = $this->translator->trans('%dances% %city%', ['%dances%' => $dances, '%city%' => $this->context['city']['in']]);
 
                        //Set description
                        $this->context['description'] = $this->translator->trans('%dances% indoor and outdoor calendar %city%', ['%dances%' => $dances, '%city%' => $this->context['city']['in']]);
                } else {
                        //Set title
-                       $this->context['title'] = $this->translator->trans('Dance %city%', ['%city%' => $this->context['city']['in']]);
+                       $this->context['title']['page'] = $this->translator->trans('Dance %city%', ['%city%' => $this->context['city']['in']]);
 
                        //Set description
                        $this->context['description'] = $this->translator->trans('Indoor and outdoor dance calendar %city%', ['%city%' => $this->context['city']['in']]);
@@ -233,7 +227,7 @@ class LocationController extends DefaultController {
        /**
         * List all locations
         *
-        * @desc Display all locations
+        * Display all locations
         *
         * @param Request $request The request instance
         *
@@ -250,7 +244,7 @@ class LocationController extends DefaultController {
                $response = new Response();
 
                //With logged user
-               if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
+               if ($this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
                        //Set last modified
                        $response->setLastModified(new \DateTime('-1 year'));
 
@@ -275,23 +269,11 @@ class LocationController extends DefaultController {
                        }
                }
 
-               //Set latitudes
-               $latitudes = array_map(function ($v) { return $v['latitude']; }, $this->context['locations']);
-
-               //Set latitude
-               $latitude = round(array_sum($latitudes)/count($latitudes), 6);
-
-               //Set longitudes
-               $longitudes = array_map(function ($v) { return $v['longitude']; }, $this->context['locations']);
-
-               //Set longitude
-               $longitude = round(array_sum($longitudes)/count($longitudes), 6);
-
                //Add multi map
-               $this->context['multimap'] = $this->map->getMultiMap($this->translator->trans('Libre Air locations sector map'), $this->modified->getTimestamp(), $latitude, $longitude, $this->context['locations'], $this->map->getMultiZoom($latitude, $longitude, $this->context['locations']));
+               $this->context['multimap'] = $this->map->getMultiMap($this->translator->trans('Libre Air locations sector map'), $this->modified->getTimestamp(), $this->context['locations']);
 
                //Set title
-               $this->context['title'] = $this->translator->trans('Libre Air locations');
+               $this->context['title']['page'] = $this->translator->trans('Libre Air locations');
 
                //Set description
                $this->context['description'] = $this->translator->trans('Libre Air location list');
@@ -305,7 +287,7 @@ class LocationController extends DefaultController {
                ];
 
                //Create location forms for role_admin
-               if ($this->isGranted('ROLE_ADMIN')) {
+               if ($this->checker->isGranted('ROLE_ADMIN')) {
                        //Fetch all locations
                        $locations = $this->doctrine->getRepository(Location::class)->findAll();
 
@@ -347,7 +329,7 @@ class LocationController extends DefaultController {
                                        $this->addFlash('notice', $this->translator->trans('Location %id% updated', ['%id%' => $location['id']]));
 
                                        //Redirect to cleanup the form
-                                       return $this->redirectToRoute('rapsys_air_location', ['location' => $location['id']]);
+                                       return $this->redirectToRoute('rapsysair_location', ['location' => $location['id']]);
                                }
 
                                //Add form to context
@@ -384,7 +366,7 @@ class LocationController extends DefaultController {
                                $this->addFlash('notice', $this->translator->trans('Location created'));
 
                                //Redirect to cleanup the form
-                               return $this->redirectToRoute('rapsys_air_location', ['location' => $data->getId()]);
+                               return $this->redirectToRoute('rapsysair_location', ['location' => $data->getId()]);
                        }
 
                        //Add form to context
@@ -404,18 +386,25 @@ class LocationController extends DefaultController {
         *
         * @param Request $request The request instance
         * @param int $id The location id
+        * @param ?string $location The location slug
         *
         * @return Response The rendered view
         */
-       public function view(Request $request, int $id): Response {
+       public function view(Request $request, int $id, ?string $location): Response {
                //Without location
                if (empty($this->context['location'] = $this->doctrine->getRepository(Location::class)->findOneByIdAsArray($id, $this->locale))) {
                        //Throw 404
                        throw $this->createNotFoundException($this->translator->trans('Unable to find location: %id%', ['%id%' => $id]));
                }
 
+               //With invalid slug
+               if ($location !== $this->context['location']['slug']) {
+                       //Redirect on correctly spelled location
+                       return $this->redirectToRoute('rapsysair_location_view', ['id' => $this->context['location']['id'], 'location' => $this->context['location']['slug']], Response::HTTP_MOVED_PERMANENTLY);
+               }
+
                //Fetch calendar
-               $this->context['calendar'] = $this->doctrine->getRepository(Session::class)->findAllByPeriodAsArray($this->period, $this->locale, !$this->isGranted('IS_AUTHENTICATED_REMEMBERED'), $this->context['location']['latitude'], $this->context['location']['longitude']);
+               $this->context['calendar'] = $this->doctrine->getRepository(Session::class)->findAllByPeriodAsCalendarArray($this->period, !$this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED'), $this->context['location']['latitude'], $this->context['location']['longitude']);
 
                //Set dances
                $this->context['dances'] = [];
@@ -437,13 +426,13 @@ class LocationController extends DefaultController {
 
                //Set modified
                //XXX: dance modified is already computed inside calendar modified
-               $this->modified = max(array_merge([$this->context['location']['updated']], array_map(function ($v) { return $v['modified']; }, array_merge($this->context['calendar'], $this->context['locations']))));
+               $this->modified = max(array_merge([$this->context['location']['modified']], array_map(function ($v) { return $v['modified']; }, array_merge($this->context['calendar'], $this->context['locations']))));
 
                //Create response
                $response = new Response();
 
                //With logged user
-               if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
+               if ($this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
                        //Set last modified
                        $response->setLastModified(new \DateTime('-1 year'));
 
@@ -469,12 +458,12 @@ class LocationController extends DefaultController {
                }
 
                //Add multi map
-               $this->context['multimap'] = $this->map->getMultiMap($this->context['location']['multimap'], $this->modified->getTimestamp(), $this->context['location']['latitude'], $this->context['location']['longitude'], $this->context['locations'], $this->map->getMultiZoom($this->context['location']['latitude'], $this->context['location']['longitude'], $this->context['locations']));
+               $this->context['multimap'] = $this->map->getMultiMap($this->context['location']['multimap'], $this->modified->getTimestamp(), $this->context['locations']);
 
                //Set keywords
                $this->context['keywords'] = [
                        $this->context['location']['title'],
-                       $this->context['location']['city'],
+                       $this->context['location']['city']['title'],
                        $this->translator->trans($this->context['location']['indoor']?'Indoor':'Outdoor'),
                        $this->translator->trans('Calendar'),
                        $this->translator->trans('Libre Air')
@@ -492,21 +481,27 @@ class LocationController extends DefaultController {
                        $dances = implode($this->translator->trans(' and '), array_filter(array_merge([implode(', ', array_slice($dances, 0, -1))], array_slice($dances, -1)), 'strlen'));
 
                        //Set title
-                       $this->context['title'] = $this->translator->trans('%dances% %location%', ['%dances%' => $dances, '%location%' => $this->context['location']['atin']]);
+                       $this->context['title']['page'] = $this->translator->trans('%dances% %location%', ['%dances%' => $dances, '%location%' => $this->context['location']['atin']]);
 
                        //Set description
                        $this->context['description'] = $this->translator->trans('%dances% indoor and outdoor calendar %location%', ['%dances%' => $dances, '%location%' => $this->context['location']['at']]);
                //Without dances
                } else {
                        //Set title
-                       $this->context['title'] = $this->translator->trans('Dance %location%', ['%location%' => $this->context['location']['atin']]);
+                       $this->context['title']['page'] = $this->translator->trans('Dance %location%', ['%location%' => $this->context['location']['atin']]);
 
                        //Set description
-                       $this->context['description'] = $this->translator->trans('Indoor and outdoor dance calendar %location%', [ '%location%' => $this->context['location']['at'] ]);
+                       $this->context['description'] = $this->translator->trans('Indoor and outdoor dance calendar %location%', ['%location%' => $this->context['location']['at']]);
                }
 
                //Set locations description
-               $this->context['locations_description'] = $this->translator->trans('Libre Air location list %location%', ['%location%' => $this->context['location']['atin']]);
+               $this->context['locations_description'] = $this->translator->trans('Libre Air location list %location% %city%', ['%location%' => $this->context['location']['around'], '%city%' => $this->context['location']['city']['in']]);
+
+               //Set locations link
+               $this->context['locations_link'] = $this->context['location']['city']['link'];
+
+               //Set locations title
+               $this->context['locations_title'] = $this->context['location']['city']['title'].' ('.$this->context['location']['city']['id'].')';
 
                //Set alternates
                $this->context['alternates'] += $this->context['location']['alternates'];