X-Git-Url: https://git.rapsys.eu/airbundle/blobdiff_plain/c8e44d4a29898c915b640e3c339df07a67ddf6af..1b24cfa05c059a98376c1be397fc3f6c0df47fd1:/Controller/SessionController.php diff --git a/Controller/SessionController.php b/Controller/SessionController.php index 7bdb117..c3104a3 100644 --- a/Controller/SessionController.php +++ b/Controller/SessionController.php @@ -3,11 +3,428 @@ namespace Rapsys\AirBundle\Controller; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Routing\RequestContext; +use Symfony\Component\Routing\Exception\MethodNotAllowedException; +use Symfony\Component\Routing\Exception\ResourceNotFoundException; +use Rapsys\AirBundle\Entity\Application; +use Rapsys\AirBundle\Entity\User; use Rapsys\AirBundle\Entity\Slot; use Rapsys\AirBundle\Entity\Session; use Rapsys\AirBundle\Entity\Location; class SessionController extends DefaultController { + /** + * Edit session + * + * @desc Persist session and all required dependencies in database + * + * @param Request $request The request instance + * + * @return Response The rendered view or redirection + * + * @throws \RuntimeException When user has not at least guest role + */ + public function edit(Request $request, $id) { + //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(); + + //Fetch session + $session = $doctrine->getRepository(Session::class)->fetchOneById($id, $request->getLocale()); + + //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 SessionEditType form + $form = $this->createForm('Rapsys\AirBundle\Form\SessionEditType', 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 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 (true) { $form->isValid(); + //TODO: mettre une contrainte sur un des boutons submit, je sais pas encore comment + if (!$form->isValid()) { + //Set section + $section = $this->translator->trans('Session %id%', ['%id%' => $id]); + + //Set title + $title = $section.' - '.$this->translator->trans($this->config['site']['title']); + + //Add session in context + $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', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+$context+$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']) { + //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 * @@ -17,7 +434,7 @@ class SessionController extends DefaultController { * * @return Response The rendered view */ - public function index(Request $request = null) { + public function index(Request $request) { //Fetch doctrine $doctrine = $this->getDoctrine(); @@ -25,7 +442,7 @@ class SessionController extends DefaultController { $section = $this->translator->trans('Sessions'); //Set title - $title = $section.' - '.$this->translator->trans($this->config['site']['title']); + $title = $this->translator->trans($this->config['site']['title']).' - '.$section; //Init context $context = []; @@ -100,13 +517,15 @@ class SessionController extends DefaultController { $doctrine = $this->getDoctrine(); //Fetch session - $session = $doctrine->getRepository(Session::class)->fetchOneById($id); + if (empty($session = $doctrine->getRepository(Session::class)->fetchOneById($id, $request->getLocale()))) { + throw $this->createNotFoundException($this->translator->trans('Unable to find session: %id%', ['%id%' => $id])); + } //Set section $section = $this->translator->trans($session['l_title']); //Set title - $title = $this->translator->trans('Session %id%', ['%id%' => $id]).' - '.$section.' - '.$this->translator->trans($this->config['site']['title']); + $title = $this->translator->trans($this->config['site']['title']).' - '.$section.' - '.$this->translator->trans('Session %id%', ['%id%' => $id]); //Init context $context = []; @@ -131,6 +550,40 @@ class SessionController extends DefaultController { //Add form to context $context['application'] = $application->createView(); + + //Set now + $now = new \DateTime('now'); + + //Create SessionEditType form + $session_edit = $this->createForm('Rapsys\AirBundle\Form\SessionEditType', null, [ + //Set the action + 'action' => $this->generateUrl('rapsys_air_session_edit', [ 'id' => $id ]), + //Set the form attribute + 'attr' => [ 'class' => 'col' ], + //Set admin + 'admin' => $this->isGranted('ROLE_ADMIN'), + //Set default user to current + 'user' => $this->getUser()->getId(), + //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'] + ]); + + //Add form to context + $context['session_edit'] = $session_edit->createView(); //Create login form for anonymous } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) { //Create ApplicationType form @@ -161,6 +614,7 @@ class SessionController extends DefaultController { 'temperature' => $session['temperature'] !== null ? $session['temperature'].' °C' : $session['temperature'], 'temperaturemin' => $session['temperaturemin'] !== null ? $session['temperaturemin'].' °C' : $session['temperaturemin'], 'temperaturemax' => $session['temperaturemax'] !== null ? $session['temperaturemax'].' °C' : $session['temperaturemax'], + 'locked' => $session['locked'], 'created' => $session['created'], 'updated' => $session['updated'], 'title' => $this->translator->trans('Session %id%', ['%id%' => $id]), @@ -180,6 +634,10 @@ class SessionController extends DefaultController { 'id' => $session['t_id'], 'title' => $this->translator->trans($session['t_title']) ], + 'snippet' => [ + 'id' => $session['p_id'], + 'description' => $session['p_description'] + ], 'applications' => null ]; @@ -188,7 +646,9 @@ class SessionController extends DefaultController { $context['session']['application'] = [ 'user' => [ 'id' => $session['au_id'], - 'title' => $session['au_pseudonym'] + 'title' => $session['au_pseudonym'], + 'donation' => $session['au_donation'], + 'site' => $session['au_site'] ], 'id' => $session['a_id'], 'title' => $this->translator->trans('Application %id%', [ '%id%' => $session['a_id'] ]), @@ -208,7 +668,7 @@ class SessionController extends DefaultController { $session['sa_updated'] = array_map(function($v){return new \DateTime($v);}, explode("\n", $session['sa_updated'])); //Extract applications canceled //XXX: canceled is null before cancelation, replace NULL with 'NULL' to avoid silent drop in mysql - $session['sa_canceled'] = array_map(function($v){return $v==='NULL'?null:$v;}, explode("\n", $session['sa_canceled'])); + $session['sa_canceled'] = array_map(function($v){return $v==='NULL'?null:new \DateTime($v);}, explode("\n", $session['sa_canceled'])); //Extract applications user id $session['sau_id'] = explode("\n", $session['sau_id']); @@ -234,7 +694,21 @@ class SessionController extends DefaultController { } } + //Compute period + $period = new \DatePeriod( + //Start from first monday of week + new \DateTime('Monday this week'), + //Iterate on each day + new \DateInterval('P1D'), + //End with next sunday and 4 weeks + new \DateTime('Monday this week + 5 week') + ); + + //Fetch locations + //XXX: we want to display all active locations anyway + $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period, $session['au_id']); + //Render the view - return $this->render('@RapsysAir/session/view.html.twig', ['title' => $title, 'section' => $section]+$context+$this->context); + return $this->render('@RapsysAir/session/view.html.twig', ['title' => $title, 'section' => $section, 'locations' => $locations]+$context+$this->context); } }