]> Raphaël G. Git Repositories - airbundle/blob - Controller/LocationController.php
Replace map util by image to generate multimap
[airbundle] / Controller / LocationController.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys AirBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\AirBundle\Controller;
13
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\Response;
16
17 use Rapsys\AirBundle\Entity\Dance;
18 use Rapsys\AirBundle\Entity\Location;
19 use Rapsys\AirBundle\Entity\Session;
20
21 /**
22 * {@inheritdoc}
23 */
24 class LocationController extends DefaultController {
25 /**
26 * List all cities
27 *
28 * @param Request $request The request instance
29 * @return Response The rendered view
30 */
31 public function cities(Request $request): Response {
32 //Add cities
33 $this->context['cities'] = $this->doctrine->getRepository(Location::class)->findCitiesAsArray($this->period, 0);
34
35 //Add dances
36 $this->context['dances'] = $this->doctrine->getRepository(Dance::class)->findNamesAsArray();
37
38 //Create response
39 $response = new Response();
40
41 //Set modified
42 $this->modified = max(array_map(function ($v) { return $v['modified']; }, array_merge($this->context['cities'], $this->context['dances'])));
43
44 //Iterate on each cities
45 foreach($this->context['cities'] as $id => $city) {
46 //Set coordinates
47 $coordinates = [];
48
49 //Iterate on each locations
50 foreach($city['locations'] as $location) {
51 //Add coordinates
52 $coordinates[] = [ $location['latitude'], $location['longitude'] ];
53 }
54
55 //Add city multi
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']];
58 }
59
60 //With logged user
61 if ($this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
62 //Set last modified
63 $response->setLastModified(new \DateTime('-1 year'));
64
65 //Set as private
66 $response->setPrivate();
67 //Without logged user
68 } else {
69 //Set etag
70 //XXX: only for public to force revalidation by last modified
71 $response->setEtag(md5(serialize(array_merge($this->context['cities'], $this->context['dances']))));
72
73 //Set last modified
74 $response->setLastModified($this->modified);
75
76 //Set as public
77 $response->setPublic();
78
79 //Without role and modification
80 if ($response->isNotModified($request)) {
81 //Return 304 response
82 return $response;
83 }
84 }
85
86 //Set section
87 $this->context['title']['page'] = $this->translator->trans('Libre Air cities');
88
89 //Set description
90 $this->context['description'] = $this->translator->trans('Libre Air city list');
91
92 //Set cities
93 $cities = array_map(function ($v) { return $v['in']; }, $this->context['cities']);
94
95 //Set dances
96 $dances = array_map(function ($v) { return $v['name']; }, $this->context['dances']);
97
98 //Set indoors
99 $indoors = array_reduce($this->context['cities'], function ($c, $v) { return array_merge($c, $v['indoors']); }, []);
100
101 //Set keywords
102 $this->context['keywords'] = array_values(
103 array_merge(
104 [
105 $this->translator->trans('Cities'),
106 $this->translator->trans('City list'),
107 $this->translator->trans('Listing'),
108 ],
109 $cities,
110 $indoors,
111 [
112 $this->translator->trans('calendar'),
113 $this->translator->trans('Libre Air')
114 ]
115 )
116 );
117
118 //Render the view
119 return $this->render('@RapsysAir/location/cities.html.twig', $this->context, $response);
120 }
121
122 /**
123 * Display city
124 *
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
128 *
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
133 */
134 public function city(Request $request, float $latitude, float $longitude, string $city): Response {
135 //Get city
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]));
138 }
139
140 //Add calendar
141 $this->context['calendar'] = $this->doctrine->getRepository(Session::class)->findAllByPeriodAsCalendarArray($this->period, !$this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED'), floatval($latitude), floatval($longitude));
142
143 //Set dances
144 $this->context['dances'] = [];
145
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'])) {
152 //Add dance
153 $this->context['dances'][$session['application']['dance']['id']] = $session['application']['dance'];
154 }
155 }
156 }
157
158 //Add locations
159 $this->context['locations'] = $this->doctrine->getRepository(Location::class)->findAllByLatitudeLongitudeAsArray(floatval($latitude), floatval($longitude), $this->period, 0);
160
161 //Set modified
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']))));
164
165 //Create response
166 $response = new Response();
167
168 //With logged user
169 if ($this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
170 //Set last modified
171 $response->setLastModified(new \DateTime('-1 year'));
172
173 //Set as private
174 $response->setPrivate();
175 //Without logged user
176 } else {
177 //Set etag
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']))));
180
181 //Set last modified
182 $response->setLastModified($this->modified);
183
184 //Set as public
185 $response->setPublic();
186
187 //Without role and modification
188 if ($response->isNotModified($request)) {
189 //Return 304 response
190 return $response;
191 }
192 }
193
194 //Set coordinates
195 $coordinates = [];
196
197 //Iterate on each locations
198 foreach($this->context['locations'] as $location) {
199 //Add coordinates
200 $coordinates[] = [ $location['latitude'], $location['longitude'] ];
201 }
202
203 //Add multi
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']];
206
207 //Set keywords
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')
214 ];
215
216 //With context dances
217 if (!empty($this->context['dances'])) {
218 //Set dances
219 $dances = array_map(function ($v) { return $v['name']; }, $this->context['dances']);
220
221 //Insert dances in keywords
222 array_splice($this->context['keywords'], 1, 0, $dances);
223
224 //Get textual dances
225 $dances = implode($this->translator->trans(' and '), array_filter(array_merge([implode(', ', array_slice($dances, 0, -1))], array_slice($dances, -1)), 'strlen'));
226
227 //Set title
228 $this->context['title']['page'] = $this->translator->trans('%dances% %city%', ['%dances%' => $dances, '%city%' => $this->context['city']['in']]);
229
230 //Set description
231 $this->context['description'] = $this->translator->trans('%dances% indoor and outdoor calendar %city%', ['%dances%' => $dances, '%city%' => $this->context['city']['in']]);
232 } else {
233 //Set title
234 $this->context['title']['page'] = $this->translator->trans('Dance %city%', ['%city%' => $this->context['city']['in']]);
235
236 //Set description
237 $this->context['description'] = $this->translator->trans('Indoor and outdoor dance calendar %city%', ['%city%' => $this->context['city']['in']]);
238 }
239
240 //Set locations description
241 $this->context['locations_description'] = $this->translator->trans('Libre Air location list %city%', ['%city%' => $this->context['city']['in']]);
242
243 //Render the view
244 return $this->render('@RapsysAir/location/city.html.twig', $this->context, $response);
245 }
246
247 /**
248 * List all locations
249 *
250 * Display all locations
251 *
252 * @param Request $request The request instance
253 *
254 * @return Response The rendered view
255 */
256 public function index(Request $request): Response {
257 //Get locations
258 $this->context['locations'] = $this->doctrine->getRepository(Location::class)->findAllAsArray($this->period);
259
260 //Set modified
261 $this->modified = max(array_map(function ($v) { return $v['updated']; }, $this->context['locations']));
262
263 //Create response
264 $response = new Response();
265
266 //With logged user
267 if ($this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
268 //Set last modified
269 $response->setLastModified(new \DateTime('-1 year'));
270
271 //Set as private
272 $response->setPrivate();
273 //Without logged user
274 } else {
275 //Set etag
276 //XXX: only for public to force revalidation by last modified
277 $response->setEtag(md5(serialize($this->context['locations'])));
278
279 //Set last modified
280 $response->setLastModified($this->modified);
281
282 //Set as public
283 $response->setPublic();
284
285 //Without role and modification
286 if ($response->isNotModified($request)) {
287 //Return 304 response
288 return $response;
289 }
290 }
291
292 //Set coordinates
293 $coordinates = [];
294
295 //Iterate on each locations
296 foreach($this->context['locations'] as $location) {
297 //Add coordinates
298 $coordinates[] = [ $location['latitude'], $location['longitude'] ];
299 }
300
301 //Add multi map
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')];
304
305 //Set title
306 $this->context['title']['page'] = $this->translator->trans('Libre Air locations');
307
308 //Set description
309 $this->context['description'] = $this->translator->trans('Libre Air location list');
310
311 //Set keywords
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')
317 ];
318
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();
323
324 //Init locations to context
325 $this->context['forms']['locations'] = [];
326
327 //Iterate on locations
328 foreach($this->context['locations'] as $id => $location) {
329 //Create LocationType form
330 $form = $this->factory->createNamed(
331 //Set form id
332 'locations_'.$id,
333 //Set form type
334 'Rapsys\AirBundle\Form\LocationType',
335 //Set form data
336 $locations[$location['id']],
337 //Set the form attributes
338 ['attr' => []]
339 );
340
341 //Refill the fields in case of invalid form
342 $form->handleRequest($request);
343
344 //Handle valid form
345 if ($form->isSubmitted() && $form->isValid()) {
346 //Get data
347 $data = $form->getData();
348
349 //Set updated
350 $data->setUpdated(new \DateTime('now'));
351
352 //Queue location save
353 $this->manager->persist($data);
354
355 //Flush to get the ids
356 $this->manager->flush();
357
358 //Add notice
359 $this->addFlash('notice', $this->translator->trans('Location %id% updated', ['%id%' => $location['id']]));
360
361 //Redirect to cleanup the form
362 return $this->redirectToRoute('rapsysair_location', ['location' => $location['id']]);
363 }
364
365 //Add form to context
366 $this->context['forms']['locations'][$id] = $form->createView();
367 }
368
369 //Create LocationType form
370 $form = $this->factory->createNamed(
371 //Set form id
372 'locations',
373 //Set form type
374 'Rapsys\AirBundle\Form\LocationType',
375 //Set form data
376 new Location(),
377 //Set the form attributes
378 ['attr' => ['class' => 'col']]
379 );
380
381 //Refill the fields in case of invalid form
382 $form->handleRequest($request);
383
384 //Handle valid form
385 if ($form->isSubmitted() && $form->isValid()) {
386 //Get data
387 $data = $form->getData();
388
389 //Queue location save
390 $this->manager->persist($data);
391
392 //Flush to get the ids
393 $this->manager->flush();
394
395 //Add notice
396 $this->addFlash('notice', $this->translator->trans('Location created'));
397
398 //Redirect to cleanup the form
399 return $this->redirectToRoute('rapsysair_location', ['location' => $data->getId()]);
400 }
401
402 //Add form to context
403 $this->context['forms']['location'] = $form->createView();
404 }
405
406 //Render the view
407 return $this->render('@RapsysAir/location/index.html.twig', $this->context);
408 }
409
410 /**
411 * List all sessions for the location
412 *
413 * Display all sessions for the location with an application or login form
414 *
415 * @TODO: add location edit form ???
416 *
417 * @param Request $request The request instance
418 * @param int $id The location id
419 * @param ?string $location The location slug
420 *
421 * @return Response The rendered view
422 */
423 public function view(Request $request, int $id, ?string $location): Response {
424 //Without location
425 if (empty($this->context['location'] = $this->doctrine->getRepository(Location::class)->findOneByIdAsArray($id, $this->locale))) {
426 //Throw 404
427 throw $this->createNotFoundException($this->translator->trans('Unable to find location: %id%', ['%id%' => $id]));
428 }
429
430 //With invalid slug
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);
434 }
435
436 //Fetch calendar
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']);
438
439 //Set dances
440 $this->context['dances'] = [];
441
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'])) {
448 //Add dance
449 $this->context['dances'][$session['application']['dance']['id']] = $session['application']['dance'];
450 }
451 }
452 }
453
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);
456
457 //Set modified
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']))));
460
461 //Create response
462 $response = new Response();
463
464 //With logged user
465 if ($this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
466 //Set last modified
467 $response->setLastModified(new \DateTime('-1 year'));
468
469 //Set as private
470 $response->setPrivate();
471 //Without logged user
472 } else {
473 //Set etag
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']))));
476
477 //Set last modified
478 $response->setLastModified($this->modified);
479
480 //Set as public
481 $response->setPublic();
482
483 //Without role and modification
484 if ($response->isNotModified($request)) {
485 //Return 304 response
486 return $response;
487 }
488 }
489
490 //Set coordinates
491 $coordinates = [];
492
493 //Iterate on each locations
494 foreach($this->context['locations'] as $location) {
495 //Add coordinates
496 $coordinates[] = [ $location['latitude'], $location['longitude'] ];
497 }
498
499 //Add multi map
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']];
502
503 //Set keywords
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')
510 ];
511
512 //With dances
513 if (!empty($this->context['dances'])) {
514 //Set dances
515 $dances = array_map(function ($v) { return $v['name']; }, $this->context['dances']);
516
517 //Insert dances in keywords
518 array_splice($this->context['keywords'], 2, 0, $dances);
519
520 //Get textual dances
521 $dances = implode($this->translator->trans(' and '), array_filter(array_merge([implode(', ', array_slice($dances, 0, -1))], array_slice($dances, -1)), 'strlen'));
522
523 //Set title
524 $this->context['title']['page'] = $this->translator->trans('%dances% %location%', ['%dances%' => $dances, '%location%' => $this->context['location']['atin']]);
525
526 //Set description
527 $this->context['description'] = $this->translator->trans('%dances% indoor and outdoor calendar %location%', ['%dances%' => $dances, '%location%' => $this->context['location']['at']]);
528 //Without dances
529 } else {
530 //Set title
531 $this->context['title']['page'] = $this->translator->trans('Dance %location%', ['%location%' => $this->context['location']['atin']]);
532
533 //Set description
534 $this->context['description'] = $this->translator->trans('Indoor and outdoor dance calendar %location%', ['%location%' => $this->context['location']['at']]);
535 }
536
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']]);
539
540 //Set locations link
541 $this->context['locations_link'] = $this->context['location']['city']['link'];
542
543 //Set locations title
544 $this->context['locations_title'] = $this->context['location']['city']['title'].' ('.$this->context['location']['city']['id'].')';
545
546 //Set alternates
547 $this->context['alternates'] += $this->context['location']['alternates'];
548
549 //Render the view
550 return $this->render('@RapsysAir/location/view.html.twig', $this->context, $response);
551 }
552 }