+ $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']) {
+ //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);
+
+ //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()]));
+
+ //Unattribute session
+ $session->setApplication($application);
+
+ //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 autoattribute
+ } elseif ($action['autoattribute']) {
+ //Get best application
+ //XXX: best application may not issue result while grace time or bad behaviour
+ if (!empty($application = $doctrine->getRepository(Session::class)->findBestApplicationById($id))) {
+ //Attribute session
+ $session->setApplication($application);
+
+ //Update time
+ $session->setUpdated($datetime);
+
+ //Queue session save
+ $manager->persist($session);
+
+ //Add notice in flash message
+ $this->addFlash('notice', $this->translator->trans('Session %id% auto attributed', ['%id%' => $id]));
+ //No application
+ } else {
+ //Add warning in flash message
+ $this->addFlash('warning', $this->translator->trans('Session %id% not auto attributed', ['%id%' => $id]));
+ }
+ //With lock
+ } elseif ($action['lock']) {
+ //Already locked
+ if ($session->getLocked() !== null) {
+ //Set uncanceled
+ $canceled = null;
+
+ //Unlock session
+ $session->setLocked(null);
+ //Not locked
+ } else {
+ //Get application
+ if ($application = $session->getApplication()) {
+ //Set score
+ //XXX: magic cheat score 42
+ $application->setScore(42);
+
+ //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()]));
+ }
+
+ //Unattribute session
+ $session->setApplication(null);
+
+ //Lock session
+ $session->setLocked($datetime);
+ }
+
+# //Get applications
+# $applications = $doctrine->getRepository(Application::class)->findBySession($session);
+#
+# //Not empty
+# if (!empty($applications)) {
+# //Iterate on each applications
+# foreach($applications as $application) {
+# //Cancel application
+# $application->setCanceled($canceled);
+#
+# //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()]));
+# }
+# }
+
+ //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]));
+ //Unknown action
+ } else {
+ //Add warning in flash message
+ $this->addFlash('warning', $this->translator->trans('Session %id% not updated', ['%id%' => $id]));
+ }
+
+ //Flush to get the ids
+ $manager->flush();
+
+ //Extract and process referer
+ if ($referer = $request->headers->get('referer')) {
+ //Create referer request instance
+ $req = Request::create($referer);
+
+ //Get referer path
+ $path = $req->getPathInfo();
+
+ //Get referer query string
+ $query = $req->getQueryString();
+
+ //Remove script name
+ $path = str_replace($request->getScriptName(), '', $path);
+
+ //Try with referer path
+ try {
+ //Save old context
+ $oldContext = $this->router->getContext();
+
+ //Force clean context
+ //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42
+ $this->router->setContext(new RequestContext());
+
+ //Retrieve route matching path
+ $route = $this->router->match($path);
+
+ //Reset context
+ $this->router->setContext($oldContext);
+
+ //Clear old context
+ unset($oldContext);
+
+ //Extract name
+ $name = $route['_route'];
+
+ //Remove route and controller from route defaults
+ unset($route['_route'], $route['_controller']);
+
+ //Generate url
+ return $this->redirectToRoute($name, $route);
+ //No route matched
+ } catch(MethodNotAllowedException|ResourceNotFoundException $e) {
+ //Unset referer to fallback to default route
+ unset($referer);
+ }
+ }
+
+ //Redirect to cleanup the form
+ return $this->redirectToRoute('rapsys_air_session_view', ['id' => $id]);
+ }
+
+ /**
+ * List all sessions
+ *
+ * @desc Display all sessions with an application or login form
+ *
+ * @param Request $request The request instance
+ *
+ * @return Response The rendered view
+ */
+ public function index(Request $request) {
+ //Fetch doctrine
+ $doctrine = $this->getDoctrine();