]> Raphaƫl G. Git Repositories - airbundle/blob - Controller/ApplicationController.php
Add 75004 accuweather uris
[airbundle] / Controller / ApplicationController.php
1 <?php
2
3 namespace Rapsys\AirBundle\Controller;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\Routing\RequestContext;
7 use Symfony\Component\Form\FormError;
8 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
9 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
10 use Rapsys\AirBundle\Entity\Slot;
11 use Rapsys\AirBundle\Entity\User;
12 use Rapsys\AirBundle\Entity\Session;
13 use Rapsys\AirBundle\Entity\Application;
14
15 class ApplicationController extends DefaultController {
16 /**
17 * Add application
18 *
19 * @desc Persist application and all required dependencies in database
20 *
21 * @param Request $request The request instance
22 *
23 * @return Response The rendered view or redirection
24 *
25 * @throws \RuntimeException When user has not at least guest role
26 */
27 public function add(Request $request) {
28 //Prevent non-guest to access here
29 $this->denyAccessUnlessGranted('ROLE_GUEST', null, $this->translator->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator->trans('Guest')]));
30
31 //Reject non post requests
32 if (!$request->isMethod('POST')) {
33 throw new \RuntimeException('Request method MUST be POST');
34 }
35
36 //Create ApplicationType form
37 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
38 //Set the action
39 'action' => $this->generateUrl('rapsys_air_application_add'),
40 //Set the form attribute
41 #'attr' => [ 'class' => 'col' ],
42 //Set admin
43 'admin' => $this->isGranted('ROLE_ADMIN'),
44 //Set default user to current
45 'user' => $this->getUser()->getId(),
46 //Set default slot to evening
47 //XXX: default to Evening (3)
48 'slot' => $this->getDoctrine()->getRepository(Slot::class)->findOneById(3)
49 ]);
50
51 //Refill the fields in case of invalid form
52 $form->handleRequest($request);
53
54 //Handle invalid form
55 if (!$form->isValid()) {
56 //Set section
57 $section = $this->translator->trans('Application add');
58
59 //Set title
60 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
61
62 //Render the view
63 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+$this->context);
64 }
65
66 //Get doctrine
67 $doctrine = $this->getDoctrine();
68
69 //Get manager
70 $manager = $doctrine->getManager();
71
72 //Get data
73 $data = $form->getData();
74
75 //Count session at location in last month for guest
76 if (!$this->isGranted('ROLE_REGULAR') && !empty($session = $doctrine->getRepository(Session::class)->findOneWithinLastMonthByLocationUser($data['location']->getId(), $this->getUser()->getId()))) {
77 //Add warning in flash message
78 $this->addFlash('warning', $this->translator->trans('Monthly application %location% already exists', ['%location%' => $this->translator->trans('at '.$data['location'])]));
79
80 //Redirect to cleanup the form
81 return $this->redirectToRoute('rapsys_air_session_view', ['id' => $session['id']]);
82 }
83
84 //Protect session fetching
85 try {
86 //Fetch session
87 $session = $doctrine->getRepository(Session::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']);
88 //Catch no session case
89 } catch (\Doctrine\ORM\NoResultException $e) {
90 //Create the session
91 $session = new Session();
92 $session->setLocation($data['location']);
93 $session->setDate($data['date']);
94 $session->setSlot($data['slot']);
95 $session->setCreated(new \DateTime('now'));
96 $session->setUpdated(new \DateTime('now'));
97
98 //Get short location
99 $short = $data['location']->getShort();
100
101 //Get slot
102 $slot = $data['slot']->getTitle();
103
104 //Set premium
105 $session->setPremium($premium = false);
106
107 //Check if slot is afternoon
108 //XXX: premium is stored only for Afternoon and Evening
109 if ($slot == 'Afternoon') {
110 //Compute premium
111 //XXX: a session is considered premium a day off
112 $session->setPremium($premium = $this->isPremium($data['date']));
113 //Check if slot is evening
114 //XXX: premium is stored only for Afternoon and Evening
115 } elseif ($slot == 'Evening') {
116 //Compute premium
117 //XXX: a session is considered premium the eve of a day off
118 $session->setPremium($premium = $this->isPremium((clone $data['date'])->add(new \DateInterval('P1D'))));
119 //Check if slot is after
120 } elseif ($slot == 'After') {
121 //Compute premium
122 //XXX: a session is considered premium the eve of a day off
123 $premium = $this->isPremium((clone $data['date'])->add(new \DateInterval('P1D')));
124 }
125
126 //Set default length at 6h
127 //XXX: date part will be truncated on save
128 $session->setLength(new \DateTime('06:00:00'));
129
130 //Check if admin
131 if ($this->isGranted('ROLE_ADMIN')) {
132 //Check if morning
133 if ($slot == 'Morning') {
134 //Set begin at 9h
135 $session->setBegin(new \DateTime('09:00:00'));
136
137 //Set length at 5h
138 $session->setLength(new \DateTime('05:00:00'));
139 //Check if afternoon
140 } elseif ($slot == 'Afternoon') {
141 //Set begin at 14h
142 $session->setBegin(new \DateTime('14:00:00'));
143
144 //Set length at 5h
145 $session->setLength(new \DateTime('05:00:00'));
146 //Check if evening
147 } elseif ($slot == 'Evening') {
148 //Set begin at 19h
149 $session->setBegin(new \DateTime('19:00:00'));
150
151 //Check if next day is premium
152 if ($premium) {
153 //Set length at 7h
154 $session->setLength(new \DateTime('07:00:00'));
155 }
156 //Check if after
157 } else {
158 //Set begin at 1h
159 $session->setBegin(new \DateTime('01:00:00'));
160
161 //Set length at 4h
162 $session->setLength(new \DateTime('04:00:00'));
163
164 //Check if next day is premium
165 if ($premium) {
166 //Set begin at 2h
167 $session->setBegin(new \DateTime('02:00:00'));
168
169 //Set length at 3h
170 $session->setLength(new \DateTime('03:00:00'));
171 }
172 }
173 //Docks => 14h -> 19h | 19h -> 01/02h
174 //XXX: remove Garnier from here to switch back to 21h
175 } elseif (in_array($short, ['Docks', 'Garnier']) && in_array($slot, ['Afternoon', 'Evening', 'After'])) {
176 //Check if afternoon
177 if ($slot == 'Afternoon') {
178 //Set begin at 14h
179 $session->setBegin(new \DateTime('14:00:00'));
180
181 //Set length at 5h
182 $session->setLength(new \DateTime('05:00:00'));
183 //Check if evening
184 } elseif ($slot == 'Evening') {
185 //Set begin at 19h
186 $session->setBegin(new \DateTime('19:00:00'));
187
188 //Check if next day is premium
189 if ($premium) {
190 //Set length at 7h
191 $session->setLength(new \DateTime('07:00:00'));
192 }
193 //Check if after
194 } else {
195 //Set begin at 1h
196 $session->setBegin(new \DateTime('01:00:00'));
197
198 //Set length at 4h
199 $session->setLength(new \DateTime('04:00:00'));
200
201 //Check if next day is premium
202 if ($premium) {
203 //Set begin at 2h
204 $session->setBegin(new \DateTime('02:00:00'));
205
206 //Set length at 3h
207 $session->setLength(new \DateTime('03:00:00'));
208 }
209 }
210 //Garnier => 21h -> 01/02h
211 } elseif ($short == 'Garnier' && in_array($slot, ['Evening', 'After'])) {
212 //Check if evening
213 if ($slot == 'Evening') {
214 //Set begin at 21h
215 $session->setBegin(new \DateTime('21:00:00'));
216
217 //Set length at 5h
218 $session->setLength(new \DateTime('05:00:00'));
219
220 //Check if next day is premium
221 if ($premium) {
222 //Set length at 6h
223 $session->setLength(new \DateTime('06:00:00'));
224 }
225 //Check if after
226 } else {
227 //Set begin at 1h
228 $session->setBegin(new \DateTime('01:00:00'));
229
230 //Set length at 4h
231 $session->setLength(new \DateTime('04:00:00'));
232
233 //Check if next day is premium
234 if ($premium) {
235 //Set begin at 2h
236 $session->setBegin(new \DateTime('02:00:00'));
237
238 //Set length at 3h
239 $session->setLength(new \DateTime('03:00:00'));
240 }
241 }
242 //Trocadero|Tokyo|Swan|Honore|Orsay => 19h -> 01/02h
243 } elseif (in_array($short, ['Trocadero', 'Tokyo', 'Swan', 'Honore', 'Orsay']) && in_array($slot, ['Evening', 'After'])) {
244 //Check if evening
245 if ($slot == 'Evening') {
246 //Set begin at 19h
247 $session->setBegin(new \DateTime('19:00:00'));
248
249 //Check if next day is premium
250 if ($premium) {
251 //Set length at 7h
252 $session->setLength(new \DateTime('07:00:00'));
253 }
254 //Check if after
255 } else {
256 //Set begin at 1h
257 $session->setBegin(new \DateTime('01:00:00'));
258
259 //Set length at 4h
260 $session->setLength(new \DateTime('04:00:00'));
261
262 //Check if next day is premium
263 if ($premium) {
264 //Set begin at 2h
265 $session->setBegin(new \DateTime('02:00:00'));
266
267 //Set length at 3h
268 $session->setLength(new \DateTime('03:00:00'));
269 }
270 }
271 //La Villette => 14h -> 19h
272 } elseif ($short == 'Villette' && $slot == 'Afternoon') {
273 //Set begin at 14h
274 $session->setBegin(new \DateTime('14:00:00'));
275
276 //Set length at 5h
277 $session->setLength(new \DateTime('05:00:00'));
278 //Place Colette => 14h -> 21h
279 //TODO: add check here that it's a millegaux account ?
280 } elseif ($short == 'Colette' && $slot == 'Afternoon') {
281 //Set begin at 14h
282 $session->setBegin(new \DateTime('14:00:00'));
283
284 //Set length at 7h
285 $session->setLength(new \DateTime('07:00:00'));
286 //Galerie d'OrlƩans => 14h -> 18h
287 } elseif ($short == 'Orleans' && $slot == 'Afternoon') {
288 //Set begin at 14h
289 $session->setBegin(new \DateTime('14:00:00'));
290
291 //Set length at 4h
292 $session->setLength(new \DateTime('04:00:00'));
293 //Combination not supported
294 } else {
295 //Add error in flash message
296 $this->addFlash('error', $this->translator->trans('Session on %date% %location% %slot% not yet supported', ['%location%' => $this->translator->trans('at '.$data['location']), '%slot%' => $this->translator->trans('the '.strtolower($data['slot'])), '%date%' => $data['date']->format('Y-m-d')]));
297
298 //Set section
299 $section = $this->translator->trans('Application add');
300
301 //Set title
302 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
303
304 //Render the view
305 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+$this->context);
306 }
307
308 //Check if admin
309 if (!$this->isGranted('ROLE_ADMIN') && $session->getStart() < new \DateTime('00:00:00')) {
310 //Add error in flash message
311 $this->addFlash('error', $this->translator->trans('Session in the past on %date% %location% %slot% not yet supported', ['%location%' => $this->translator->trans('at '.$data['location']), '%slot%' => $this->translator->trans('the '.strtolower($data['slot'])), '%date%' => $data['date']->format('Y-m-d')]));
312
313 //Set section
314 $section = $this->translator->trans('Application add');
315
316 //Set title
317 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
318
319 //Render the view
320 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+$this->context);
321 }
322
323 //Queue session save
324 $manager->persist($session);
325
326 //Flush to get the ids
327 #$manager->flush();
328
329 $this->addFlash('notice', $this->translator->trans('Session on %date% %location% %slot% created', ['%location%' => $this->translator->trans('at '.$data['location']), '%slot%' => $this->translator->trans('the '.strtolower($data['slot'])), '%date%' => $data['date']->format('Y-m-d')]));
330 }
331
332 //Set user
333 $user = $this->getUser();
334
335 //Replace with requested user for admin
336 if ($this->isGranted('ROLE_ADMIN') && !empty($data['user'])) {
337 $user = $this->getDoctrine()->getRepository(User::class)->findOneById($data['user']);
338 }
339
340 //Protect application fetching
341 try {
342 //Retrieve application
343 $application = $doctrine->getRepository(Application::class)->findOneBySessionUser($session, $user);
344
345 //Add warning in flash message
346 $this->addFlash('warning', $this->translator->trans('Application on %date% %location% %slot% already exists', ['%location%' => $this->translator->trans('at '.$data['location']), '%slot%' => $this->translator->trans('the '.strtolower($data['slot'])), '%date%' => $data['date']->format('Y-m-d')]));
347 //Catch no application and session without identifier (not persisted&flushed) cases
348 } catch (\Doctrine\ORM\NoResultException|\Doctrine\ORM\ORMInvalidArgumentException $e) {
349 //Create the application
350 $application = new Application();
351 $application->setSession($session);
352 $application->setUser($user);
353 $application->setCreated(new \DateTime('now'));
354 $application->setUpdated(new \DateTime('now'));
355
356 //Refresh session updated field
357 $session->setUpdated(new \DateTime('now'));
358
359 //Queue session save
360 $manager->persist($session);
361
362 //Queue application save
363 $manager->persist($application);
364
365 //Flush to get the ids
366 $manager->flush();
367
368 //Add notice in flash message
369 $this->addFlash('notice', $this->translator->trans('Application on %date% %location% %slot% created', ['%location%' => $this->translator->trans('at '.$data['location']), '%slot%' => $this->translator->trans('the '.strtolower($data['slot'])), '%date%' => $data['date']->format('Y-m-d')]));
370 }
371
372 //Extract and process referer
373 if ($referer = $request->headers->get('referer')) {
374 //Create referer request instance
375 $req = Request::create($referer);
376
377 //Get referer path
378 $path = $req->getPathInfo();
379
380 //Get referer query string
381 $query = $req->getQueryString();
382
383 //Remove script name
384 $path = str_replace($request->getScriptName(), '', $path);
385
386 //Try with referer path
387 try {
388 //Save old context
389 $oldContext = $this->router->getContext();
390
391 //Force clean context
392 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42
393 $this->router->setContext(new RequestContext());
394
395 //Retrieve route matching path
396 $route = $this->router->match($path);
397
398 //Reset context
399 $this->router->setContext($oldContext);
400
401 //Clear old context
402 unset($oldContext);
403
404 //Extract name
405 $name = $route['_route'];
406
407 //Remove route and controller from route defaults
408 unset($route['_route'], $route['_controller']);
409
410 //Generate url
411 return $this->redirectToRoute($name, ['session' => $session->getId()]+$route);
412 //No route matched
413 } catch(MethodNotAllowedException|ResourceNotFoundException $e) {
414 //Unset referer to fallback to default route
415 unset($referer);
416 }
417 }
418
419 //Redirect to cleanup the form
420 return $this->redirectToRoute('rapsys_air', ['session' => $session->getId()]);
421 }
422
423 /**
424 * Compute eastern for selected year
425 *
426 * @param int $year The eastern year
427 *
428 * @return DateTime The eastern date
429 */
430 function getEastern($year) {
431 //Set static
432 static $data = null;
433 //Check if already computed
434 if (isset($data[$year])) {
435 //Return computed eastern
436 return $data[$year];
437 //Check if data is null
438 } elseif (is_null($data)) {
439 //Init data array
440 $data = [];
441 }
442 $d = (19 * ($year % 19) + 24) % 30;
443 $e = (2 * ($year % 4) + 4 * ($year % 7) + 6 * $d + 5) % 7;
444
445 $day = 22 + $d + $e;
446 $month = 3;
447
448 if ($day > 31) {
449 $day = $d + $e - 9;
450 $month = 4;
451 } elseif ($d == 29 && $e == 6) {
452 $day = 10;
453 $month = 4;
454 } elseif ($d == 28 && $e == 6) {
455 $day = 18;
456 $month = 4;
457 }
458
459 //Store eastern in data
460 return ($data[$year] = new \DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day)));
461 }
462
463 /**
464 * Check if date is a premium day
465 *
466 * @desc Consider as premium a day off
467 *
468 * @param DateTime $date The date to check
469 * @return bool Whether the date is off or not
470 */
471 function isPremium($date) {
472 //Get day number
473 $w = $date->format('w');
474
475 //Check if weekend day
476 if ($w == 0 || $w == 6) {
477 //Date is weekend day
478 return true;
479 }
480
481 //Get date day
482 $d = $date->format('d');
483
484 //Get date month
485 $m = $date->format('m');
486
487 //Check if fixed holiday
488 if (
489 //Check if 1st january
490 ($d == 1 && $m == 1) ||
491 //Check if 1st may
492 ($d == 1 && $m == 5) ||
493 //Check if 8st may
494 ($d == 8 && $m == 5) ||
495 //Check if 14st july
496 ($d == 14 && $m == 7) ||
497 //Check if 15st august
498 ($d == 15 && $m == 8) ||
499 //Check if 1st november
500 ($d == 1 && $m == 11) ||
501 //Check if 11st november
502 ($d == 11 && $m == 11) ||
503 //Check if 25st december
504 ($d == 25 && $m == 12)
505 ) {
506 //Date is a fixed holiday
507 return true;
508 }
509
510 //Get eastern
511 $eastern = $this->getEastern($date->format('Y'));
512
513 //Check dynamic holidays
514 if (
515 (clone $eastern)->add(new \DateInterval('P1D')) == $date ||
516 (clone $eastern)->add(new \DateInterval('P39D')) == $date ||
517 (clone $eastern)->add(new \DateInterval('P50D')) == $date
518 ) {
519 //Date is a dynamic holiday
520 return true;
521 }
522
523 //Date is not a holiday and week day
524 return false;
525 }
526 }