1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys AirBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\AirBundle\Controller
;
14 use Symfony\Component\HttpFoundation\Request
;
15 use Symfony\Component\HttpFoundation\Response
;
17 use Rapsys\AirBundle\Entity\Dance
;
18 use Rapsys\AirBundle\Entity\Location
;
19 use Rapsys\AirBundle\Entity\Session
;
24 class LocationController
extends DefaultController
{
28 * @param Request $request The request instance
29 * @return Response The rendered view
31 public function cities(Request
$request): Response
{
33 $this->context
['cities'] = $this->doctrine
->getRepository(Location
::class)->findCitiesAsArray($this->period
, 0);
36 $this->context
['dances'] = $this->doctrine
->getRepository(Dance
::class)->findNamesAsArray();
39 $response = new Response();
42 $this->modified
= max(array_map(function ($v) { return $v
['modified']; }, array_merge($this->context
['cities'], $this->context
['dances'])));
44 //Iterate on each cities
45 foreach($this->context
['cities'] as $id => $city) {
49 //Iterate on each locations
50 foreach($city['locations'] as $location) {
52 $coordinates[] = [ $location['latitude'], $location['longitude'] ];
56 #$this->context['cities'][$id]['multimap'] = $this->map->getMultiMap($city['multimap'], $this->modified->getTimestamp(), $city['locations']);
57 $this->context
['cities'][$id]['multimap'] = $this->image
->getMulti($coordinates) +
['caption' => $city['multimap']];
61 if ($this->checker
->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
63 $response->setLastModified(new \
DateTime('-1 year'));
66 $response->setPrivate();
70 //XXX: only for public to force revalidation by last modified
71 $response->setEtag(md5(serialize(array_merge($this->context
['cities'], $this->context
['dances']))));
74 $response->setLastModified($this->modified
);
77 $response->setPublic();
79 //Without role and modification
80 if ($response->isNotModified($request)) {
87 $this->context
['title']['page'] = $this->translator
->trans('Libre Air cities');
90 $this->context
['description'] = $this->translator
->trans('Libre Air city list');
93 $cities = array_map(function ($v) { return $v
['in']; }, $this->context
['cities']);
96 $dances = array_map(function ($v) { return $v
['name']; }, $this->context
['dances']);
99 $indoors = array_reduce($this->context
['cities'], function ($c, $v) { return array_merge($c
, $v
['indoors']); }, []);
102 $this->context
['keywords'] = array_values(
105 $this->translator
->trans('Cities'),
106 $this->translator
->trans('City list'),
107 $this->translator
->trans('Listing'),
112 $this->translator
->trans('calendar'),
113 $this->translator
->trans('Libre Air')
119 return $this->render('@RapsysAir/location/cities.html.twig', $this->context
, $response);
125 * @todo XXX: TODO: add <link rel="prev|next" for sessions or classes ? />
126 * @todo XXX: TODO: like described in: https://www.alsacreations.com/article/lire/1400-attribut-rel-relations.html#xnf-rel-attribute
127 * @todo XXX: TODO: or here: http://microformats.org/wiki/existing-rel-values#HTML5_link_type_extensions
129 * @param Request $request The request instance
130 * @param float $latitude The city latitude
131 * @param float $longitude The city longitude
132 * @return Response The rendered view
134 public function city(Request
$request, float $latitude, float $longitude, string $city): Response
{
136 if (!($this->context
['city'] = $this->doctrine
->getRepository(Location
::class)->findCityByLatitudeLongitudeAsArray(floatval($latitude), floatval($longitude)))) {
137 throw $this->createNotFoundException($this->translator
->trans('Unable to find city: %latitude%,%longitude%', ['%latitude%' => $latitude, '%longitude%' => $longitude]));
141 $this->context
['calendar'] = $this->doctrine
->getRepository(Session
::class)->findAllByPeriodAsCalendarArray($this->period
, !$this->checker
->isGranted('IS_AUTHENTICATED_REMEMBERED'), floatval($latitude), floatval($longitude));
144 $this->context
['dances'] = [];
146 //Iterate on each calendar
147 foreach($this->context
['calendar'] as $date => $calendar) {
148 //Iterate on each session
149 foreach($calendar['sessions'] as $sessionId => $session) {
150 //Session with application dance
151 if (!empty($session['application']['dance'])) {
153 $this->context
['dances'][$session['application']['dance']['id']] = $session['application']['dance'];
159 $this->context
['locations'] = $this->doctrine
->getRepository(Location
::class)->findAllByLatitudeLongitudeAsArray(floatval($latitude), floatval($longitude), $this->period
, 0);
162 //XXX: dance modified is already computed inside calendar modified
163 $this->modified
= max(array_merge([$this->context
['city']['updated']], array_map(function ($v) { return $v
['modified']; }, array_merge($this->context
['calendar'], $this->context
['locations']))));
166 $response = new Response();
169 if ($this->checker
->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
171 $response->setLastModified(new \
DateTime('-1 year'));
174 $response->setPrivate();
175 //Without logged user
178 //XXX: only for public to force revalidation by last modified
179 $response->setEtag(md5(serialize(array_merge($this->context
['city'], $this->context
['dances'], $this->context
['calendar'], $this->context
['locations']))));
182 $response->setLastModified($this->modified
);
185 $response->setPublic();
187 //Without role and modification
188 if ($response->isNotModified($request)) {
189 //Return 304 response
197 //Iterate on each locations
198 foreach($this->context
['locations'] as $location) {
200 $coordinates[] = [ $location['latitude'], $location['longitude'] ];
204 #$this->context['multimap'] = $this->map->getMultiMap($this->context['city']['multimap'], $this->modified->getTimestamp(), $this->context['locations']);
205 $this->context
['multimap'] = $this->image
->getMulti($coordinates) +
['caption' => $this->context
['city']['multimap']];
208 $this->context
['keywords'] = [
209 $this->context
['city']['city'],
210 $this->translator
->trans('Indoor'),
211 $this->translator
->trans('Outdoor'),
212 $this->translator
->trans('Calendar'),
213 $this->translator
->trans('Libre Air')
216 //With context dances
217 if (!empty($this->context
['dances'])) {
219 $dances = array_map(function ($v) { return $v
['name']; }, $this->context
['dances']);
221 //Insert dances in keywords
222 array_splice($this->context
['keywords'], 1, 0, $dances);
225 $dances = implode($this->translator
->trans(' and '), array_filter(array_merge([implode(', ', array_slice($dances, 0, -1))], array_slice($dances, -1)), 'strlen'));
228 $this->context
['title']['page'] = $this->translator
->trans('%dances% %city%', ['%dances%' => $dances, '%city%' => $this->context
['city']['in']]);
231 $this->context
['description'] = $this->translator
->trans('%dances% indoor and outdoor calendar %city%', ['%dances%' => $dances, '%city%' => $this->context
['city']['in']]);
234 $this->context
['title']['page'] = $this->translator
->trans('Dance %city%', ['%city%' => $this->context
['city']['in']]);
237 $this->context
['description'] = $this->translator
->trans('Indoor and outdoor dance calendar %city%', ['%city%' => $this->context
['city']['in']]);
240 //Set locations description
241 $this->context
['locations_description'] = $this->translator
->trans('Libre Air location list %city%', ['%city%' => $this->context
['city']['in']]);
244 return $this->render('@RapsysAir/location/city.html.twig', $this->context
, $response);
250 * Display all locations
252 * @param Request $request The request instance
254 * @return Response The rendered view
256 public function index(Request
$request): Response
{
258 $this->context
['locations'] = $this->doctrine
->getRepository(Location
::class)->findAllAsArray($this->period
);
261 $this->modified
= max(array_map(function ($v) { return $v
['updated']; }, $this->context
['locations']));
264 $response = new Response();
267 if ($this->checker
->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
269 $response->setLastModified(new \
DateTime('-1 year'));
272 $response->setPrivate();
273 //Without logged user
276 //XXX: only for public to force revalidation by last modified
277 $response->setEtag(md5(serialize($this->context
['locations'])));
280 $response->setLastModified($this->modified
);
283 $response->setPublic();
285 //Without role and modification
286 if ($response->isNotModified($request)) {
287 //Return 304 response
295 //Iterate on each locations
296 foreach($this->context
['locations'] as $location) {
298 $coordinates[] = [ $location['latitude'], $location['longitude'] ];
302 #$this->context['multimap'] = $this->map->getMultiMap($this->translator->trans('Libre Air locations sector map'), $this->modified->getTimestamp(), $this->context['locations']);
303 $this->context
['multimap'] = $this->image
->getMulti($coordinates) +
['caption' => $this->translator
->trans('Libre Air locations sector map')];
306 $this->context
['title']['page'] = $this->translator
->trans('Libre Air locations');
309 $this->context
['description'] = $this->translator
->trans('Libre Air location list');
312 $this->context
['keywords'] = [
313 $this->translator
->trans('locations'),
314 $this->translator
->trans('location list'),
315 $this->translator
->trans('listing'),
316 $this->translator
->trans('Libre Air')
319 //Create location forms for role_admin
320 if ($this->checker
->isGranted('ROLE_ADMIN')) {
321 //Fetch all locations
322 $locations = $this->doctrine
->getRepository(Location
::class)->findAll();
324 //Init locations to context
325 $this->context
['forms']['locations'] = [];
327 //Iterate on locations
328 foreach($this->context
['locations'] as $id => $location) {
329 //Create LocationType form
330 $form = $this->factory
->createNamed(
334 'Rapsys\AirBundle\Form\LocationType',
336 $locations[$location['id']],
337 //Set the form attributes
341 //Refill the fields in case of invalid form
342 $form->handleRequest($request);
345 if ($form->isSubmitted() && $form->isValid()) {
347 $data = $form->getData();
350 $data->setUpdated(new \
DateTime('now'));
352 //Queue location save
353 $this->manager
->persist($data);
355 //Flush to get the ids
356 $this->manager
->flush();
359 $this->addFlash('notice', $this->translator
->trans('Location %id% updated', ['%id%' => $location['id']]));
361 //Redirect to cleanup the form
362 return $this->redirectToRoute('rapsysair_location', ['location' => $location['id']]);
365 //Add form to context
366 $this->context
['forms']['locations'][$id] = $form->createView();
369 //Create LocationType form
370 $form = $this->factory
->createNamed(
374 'Rapsys\AirBundle\Form\LocationType',
377 //Set the form attributes
378 ['attr' => ['class' => 'col']]
381 //Refill the fields in case of invalid form
382 $form->handleRequest($request);
385 if ($form->isSubmitted() && $form->isValid()) {
387 $data = $form->getData();
389 //Queue location save
390 $this->manager
->persist($data);
392 //Flush to get the ids
393 $this->manager
->flush();
396 $this->addFlash('notice', $this->translator
->trans('Location created'));
398 //Redirect to cleanup the form
399 return $this->redirectToRoute('rapsysair_location', ['location' => $data->getId()]);
402 //Add form to context
403 $this->context
['forms']['location'] = $form->createView();
407 return $this->render('@RapsysAir/location/index.html.twig', $this->context
);
411 * List all sessions for the location
413 * Display all sessions for the location with an application or login form
415 * @TODO: add location edit form ???
417 * @param Request $request The request instance
418 * @param int $id The location id
419 * @param ?string $location The location slug
421 * @return Response The rendered view
423 public function view(Request
$request, int $id, ?string $location): Response
{
425 if (empty($this->context
['location'] = $this->doctrine
->getRepository(Location
::class)->findOneByIdAsArray($id, $this->locale
))) {
427 throw $this->createNotFoundException($this->translator
->trans('Unable to find location: %id%', ['%id%' => $id]));
431 if ($location !== $this->context
['location']['slug']) {
432 //Redirect on correctly spelled location
433 return $this->redirectToRoute('rapsysair_location_view', ['id' => $this->context
['location']['id'], 'location' => $this->context
['location']['slug']], Response
::HTTP_MOVED_PERMANENTLY
);
437 $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']);
440 $this->context
['dances'] = [];
442 //Iterate on each calendar
443 foreach($this->context
['calendar'] as $date => $calendar) {
444 //Iterate on each session
445 foreach($calendar['sessions'] as $sessionId => $session) {
446 //Session with application dance
447 if (!empty($session['application']['dance'])) {
449 $this->context
['dances'][$session['application']['dance']['id']] = $session['application']['dance'];
454 //Get locations at less than 2 km
455 $this->context
['locations'] = $this->doctrine
->getRepository(Location
::class)->findAllByLatitudeLongitudeAsArray($this->context
['location']['latitude'], $this->context
['location']['longitude'], $this->period
, 2);
458 //XXX: dance modified is already computed inside calendar modified
459 $this->modified
= max(array_merge([$this->context
['location']['modified']], array_map(function ($v) { return $v
['modified']; }, array_merge($this->context
['calendar'], $this->context
['locations']))));
462 $response = new Response();
465 if ($this->checker
->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
467 $response->setLastModified(new \
DateTime('-1 year'));
470 $response->setPrivate();
471 //Without logged user
474 //XXX: only for public to force revalidation by last modified
475 $response->setEtag(md5(serialize(array_merge($this->context
['location'], $this->context
['calendar'], $this->context
['locations']))));
478 $response->setLastModified($this->modified
);
481 $response->setPublic();
483 //Without role and modification
484 if ($response->isNotModified($request)) {
485 //Return 304 response
493 //Iterate on each locations
494 foreach($this->context
['locations'] as $location) {
496 $coordinates[] = [ $location['latitude'], $location['longitude'] ];
500 #$this->context['multimap'] = $this->map->getMultiMap($this->context['location']['multimap'], $this->modified->getTimestamp(), $this->context['locations']);
501 $this->context
['multimap'] = $this->image
->getMulti($coordinates) +
['caption' => $this->context
['location']['multimap']];
504 $this->context
['keywords'] = [
505 $this->context
['location']['title'],
506 $this->context
['location']['city']['title'],
507 $this->translator
->trans($this->context
['location']['indoor']?'Indoor':'Outdoor'),
508 $this->translator
->trans('Calendar'),
509 $this->translator
->trans('Libre Air')
513 if (!empty($this->context
['dances'])) {
515 $dances = array_map(function ($v) { return $v
['name']; }, $this->context
['dances']);
517 //Insert dances in keywords
518 array_splice($this->context
['keywords'], 2, 0, $dances);
521 $dances = implode($this->translator
->trans(' and '), array_filter(array_merge([implode(', ', array_slice($dances, 0, -1))], array_slice($dances, -1)), 'strlen'));
524 $this->context
['title']['page'] = $this->translator
->trans('%dances% %location%', ['%dances%' => $dances, '%location%' => $this->context
['location']['atin']]);
527 $this->context
['description'] = $this->translator
->trans('%dances% indoor and outdoor calendar %location%', ['%dances%' => $dances, '%location%' => $this->context
['location']['at']]);
531 $this->context
['title']['page'] = $this->translator
->trans('Dance %location%', ['%location%' => $this->context
['location']['atin']]);
534 $this->context
['description'] = $this->translator
->trans('Indoor and outdoor dance calendar %location%', ['%location%' => $this->context
['location']['at']]);
537 //Set locations description
538 $this->context
['locations_description'] = $this->translator
->trans('Libre Air location list %location% %city%', ['%location%' => $this->context
['location']['around'], '%city%' => $this->context
['location']['city']['in']]);
541 $this->context
['locations_link'] = $this->context
['location']['city']['link'];
543 //Set locations title
544 $this->context
['locations_title'] = $this->context
['location']['city']['title'].' ('.$this->context
['location']['city']['id'].')';
547 $this->context
['alternates'] +
= $this->context
['location']['alternates'];
550 return $this->render('@RapsysAir/location/view.html.twig', $this->context
, $response);