- public function edit(Request $request, $id): Response {
- //Prevent non-guest to access here
- $this->denyAccessUnlessGranted('ROLE_GUEST', null, $this->translator->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator->trans('Guest')]));
-
- //Reject non post requests
- if (!$request->isMethod('POST')) {
- throw new \RuntimeException('Request method MUST be POST');
- }
-
- //Get doctrine
- $doctrine = $this->getDoctrine();
-
- //Set locale
- $locale = $request->getLocale();
-
- //Fetch session
- $session = $doctrine->getRepository(Session::class)->fetchOneById($id, $locale);
-
- //Check if
- if (
- //we are admin
- !$this->isGranted('ROLE_ADMIN') &&
- //or attributed user
- $this->getUser()->getId() != $session['au_id'] &&
- //or application without attributed user
- $session['au_id'] !== null && !in_array($this->getUser()->getId(), explode("\n", $session['sau_id']))
- ) {
- //Prevent non admin and non attributed user access
- throw $this->createAccessDeniedException();
- }
-
- //Set now
- $now = new \DateTime('now');
-
- //Create SessionType form
- $form = $this->createForm('Rapsys\AirBundle\Form\SessionType', null, [
- //Set the action
- 'action' => $this->generateUrl('rapsys_air_session_edit', [ 'id' => $id ]),
- //Set the form attribute
- 'attr' => [],
- //Set admin
- 'admin' => $this->isGranted('ROLE_ADMIN'),
- //Set default user to current
- 'user' => $this->getUser()->getId(),
- //Set date
- 'date' => $session['date'],
- //Set begin
- 'begin' => $session['begin'],
- //Set length
- 'length' => $session['length'],
- //Set raincancel
- 'raincancel' => ($this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() == $session['au_id']) && $session['rainfall'] >= 2,
- //Set cancel
- 'cancel' => $this->isGranted('ROLE_ADMIN') || in_array($this->getUser()->getId(), explode("\n", $session['sau_id'])),
- //Set modify
- 'modify' => $this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() == $session['au_id'] && $session['stop'] >= $now && $this->isGranted('ROLE_REGULAR'),
- //Set move
- 'move' => $this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() == $session['au_id'] && $session['stop'] >= $now && $this->isGranted('ROLE_SENIOR'),
- //Set attribute
- 'attribute' => $this->isGranted('ROLE_ADMIN') && $session['locked'] === null,
- //Set session
- 'session' => $session['id']
- ]);
-
- //Refill the fields in case of invalid form
- $form->handleRequest($request);
-
- //Handle invalid data
- if (!$form->isSubmitted() || !$form->isValid()) {
- //Set page
- $this->context['title'] = $this->translator->trans(!empty($session['au_id'])?'Session %id% by %pseudonym%':'Session %id%', ['%id%' => $id, '%pseudonym%' => $session['au_pseudonym']]);
-
- //Set facebook title
- $this->context['facebook']['metas']['og:title'] = $this->context['title'].' '.$this->translator->trans('at '.$session['l_title']);
-
- //Set section
- $this->context['section'] = $this->translator->trans($session['l_title']);
-
- //Set localization date formater
- $intl = new \IntlDateFormatter($locale, \IntlDateFormatter::GREGORIAN, \IntlDateFormatter::SHORT);
-
- //Set description
- $this->context['description'] = $this->translator->trans('Outdoor Argentine Tango session the %date%', [ '%date%' => $intl->format($session['start']) ]);
-
- //Set localization date formater
- $intlDate = new \IntlDateFormatter($locale, \IntlDateFormatter::TRADITIONAL, \IntlDateFormatter::NONE);
-
- //Set localization time formater
- $intlTime = new \IntlDateFormatter($locale, \IntlDateFormatter::NONE, \IntlDateFormatter::SHORT);
-
- //Set facebook image
- $this->context['facebook'] += [
- 'texts' => [
- $session['au_pseudonym'] => [
- 'font' => 'irishgrover',
- 'size' => 110
- ],
- ucfirst($intlDate->format($session['start']))."\n".$this->translator->trans('From %start% to %stop%', ['%start%' => $intlTime->format($session['start']), '%stop%' => $intlTime->format($session['stop'])]) => [
- 'align' => 'left'
- ],
- $this->translator->trans('at '.$session['l_title']) => [
- 'align' => 'right',
- 'font' => 'labelleaurore',
- 'size' => 75
- ]
- ],
- 'updated' => $session['updated']->format('U')
- ];
-
- //Add session in context
- $this->context['session'] = [
- 'id' => $id,
- 'title' => $this->translator->trans('Session %id%', ['%id%' => $id]),
- 'location' => [
- 'id' => $session['l_id'],
- 'at' => $this->translator->trans('at '.$session['l_title'])
- ]
- ];
-
- //Render the view
- return $this->render('@RapsysAir/session/edit.html.twig', ['form' => $form->createView()]+$this->context);
- }
-
- //Get manager
- $manager = $doctrine->getManager();
-
- //Get data
- $data = $form->getData();
-
- //Fetch session
- $session = $doctrine->getRepository(Session::class)->findOneById($id);
-
- //Set user
- $user = $this->getUser();
-
- //Replace with requested user for admin
- if ($this->isGranted('ROLE_ADMIN') && !empty($data['user'])) {
- $user = $doctrine->getRepository(User::class)->findOneById($data['user']);
- }
-
- //Set datetime
- $datetime = new \DateTime('now');
-
- //Set canceled time at start minus one day
- $canceled = (clone $session->getStart())->sub(new \DateInterval('P1D'));
-
- //Set action
- $action = [
- 'raincancel' => $form->has('raincancel') && $form->get('raincancel')->isClicked(),
- 'modify' => $form->has('modify') && $form->get('modify')->isClicked(),
- 'move' => $form->has('move') && $form->get('move')->isClicked(),
- 'cancel' => $form->has('cancel') && $form->get('cancel')->isClicked(),
- 'forcecancel' => $form->has('forcecancel') && $form->get('forcecancel')->isClicked(),
- 'attribute' => $form->has('attribute') && $form->get('attribute')->isClicked(),
- 'autoattribute' => $form->has('autoattribute') && $form->get('autoattribute')->isClicked(),
- 'lock' => $form->has('lock') && $form->get('lock')->isClicked(),
- ];
-
- //With raincancel and application and (rainfall or admin)
- if ($action['raincancel'] && ($application = $session->getApplication()) && ($session->getRainfall() >= 2 || $this->isGranted('ROLE_ADMIN'))) {
- //Cancel application at start minus one day
- $application->setCanceled($canceled);
-
- //Update time
- $application->setUpdated($datetime);
-
- //Insufficient rainfall
- //XXX: is admin
- if ($session->getRainfall() < 2) {
- //Set score
- //XXX: magic cheat score 42
- $application->setScore(42);
- }
-
- //Queue application save
- $manager->persist($application);
-
- //Add notice in flash message
- $this->addFlash('notice', $this->translator->trans('Application %id% updated', ['%id%' => $application->getId()]));
-
- //Update time
- $session->setUpdated($datetime);
-
- //Queue session save
- $manager->persist($session);
-
- //Add notice in flash message
- $this->addFlash('notice', $this->translator->trans('Session %id% updated', ['%id%' => $id]));
- //With modify
- } elseif ($action['modify']) {
- //With admin
- if ($this->isGranted('ROLE_ADMIN')) {
- //Set date
- $session->setDate($data['date']);
- }
-
- //Set begin
- $session->setBegin($data['begin']);
-
- //Set length
- $session->setLength($data['length']);
-
- //Update time
- $session->setUpdated($datetime);
-
- //Queue session save
- $manager->persist($session);
-
- //Add notice in flash message
- $this->addFlash('notice', $this->translator->trans('Session %id% updated', ['%id%' => $id]));
- //With move
- } elseif ($action['move']) {
- //Set location
- $session->setLocation($doctrine->getRepository(Location::class)->findOneById($data['location']));
-
- //Update time
- $session->setUpdated($datetime);
-
- //Queue session save
- $manager->persist($session);
-
- //Add notice in flash message
- $this->addFlash('notice', $this->translator->trans('Session %id% updated', ['%id%' => $id]));
- //With cancel or forcecancel
- } elseif ($action['cancel'] || $action['forcecancel']) {
- //Get application
- $application = $doctrine->getRepository(Application::class)->findOneBySessionUser($session, $user);
-
- //Not already canceled
- if ($application->getCanceled() === null) {
- //Cancel application
- $application->setCanceled($datetime);
-
- //Check if application is session application and (canceled 24h before start or forcecancel (as admin))
- #if ($session->getApplication() == $application && ($datetime < $canceled || $action['forcecancel'])) {
- if ($session->getApplication() == $application && $action['forcecancel']) {
- //Set score
- //XXX: magic cheat score 42
- $application->setScore(42);
-
- //Unattribute session
- $session->setApplication(null);
-
- //Update time
- $session->setUpdated($datetime);
-
- //Queue session save
- $manager->persist($session);
-
- //Add notice in flash message
- $this->addFlash('notice', $this->translator->trans('Session %id% updated', ['%id%' => $id]));
- }
- //Already canceled
- } else {
- //Uncancel application
- $application->setCanceled(null);
- }
-
- //Update time
- $application->setUpdated($datetime);
-
- //Queue application save
- $manager->persist($application);
-
- //Add notice in flash message
- $this->addFlash('notice', $this->translator->trans('Application %id% updated', ['%id%' => $application->getId()]));
- //With attribute
- } elseif ($action['attribute']) {
- //Get application
- $application = $doctrine->getRepository(Application::class)->findOneBySessionUser($session, $user);
-
- //Already canceled
- if ($application->getCanceled() !== null) {
- //Uncancel application
- $application->setCanceled(null);
- }
-
- //Set score
- //XXX: magic cheat score 42
- $application->setScore(42);