X-Git-Url: https://git.rapsys.eu/airbundle/blobdiff_plain/f3fa09a58edbd628557c7e362f2e1232275c7733..afe9a4a1e36cab2e5fd0b352131f20765dc48a66:/Controller/LocationController.php?ds=sidebyside diff --git a/Controller/LocationController.php b/Controller/LocationController.php index 33b5870..0cb09f3 100644 --- a/Controller/LocationController.php +++ b/Controller/LocationController.php @@ -3,66 +3,290 @@ namespace Rapsys\AirBundle\Controller; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\RequestContext; +use Symfony\Component\Routing\Exception\MethodNotAllowedException; +use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Rapsys\AirBundle\Entity\Slot; use Rapsys\AirBundle\Entity\Session; use Rapsys\AirBundle\Entity\Location; class LocationController extends DefaultController { /** - * List all locations + * Add location * - * @desc Display all sessions by location with an application or login form + * @desc Persist location in database * * @param Request $request The request instance * - * @return Response The rendered view + * @return Response The rendered view or redirection + * + * @throws \RuntimeException When user has not at least admin role */ - public function index(Request $request = null) { - //Set section - $section = $this->translator->trans('Locations'); + public function add(Request $request) { + //Prevent non-guest to access here + $this->denyAccessUnlessGranted('ROLE_ADMIN', null, $this->translator->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator->trans('Admin')])); + + //Create LocationType form + $form = $this->createForm('Rapsys\AirBundle\Form\LocationType', null, [ + //Set the action + 'action' => $this->generateUrl('rapsys_air_location_add'), + //Set the form attribute + 'attr' => [] + ]); + + //Refill the fields in case of invalid form + $form->handleRequest($request); + + //Handle invalid form + if (!$form->isSubmitted() || !$form->isValid()) { + //Set section + $section = $this->translator->trans('Location add'); + + //Set title + $title = $this->translator->trans($this->config['site']['title']).' - '.$section; + + //Render the view + return $this->render('@RapsysAir/location/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+$this->context); + } - //Set title - $title = $section.' - '.$this->translator->trans($this->config['site']['title']); + //Get doctrine + $doctrine = $this->getDoctrine(); - //Init context - $context = []; + //Get manager + $manager = $doctrine->getManager(); - //Create application form for role_guest - if ($this->isGranted('ROLE_GUEST')) { - //Create ApplicationType form - $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ - //Set the action - 'action' => $this->generateUrl('rapsys_air_application_add'), - //Set the form attribute - 'attr' => [ 'class' => 'col' ], - //Set admin - 'admin' => $this->isGranted('ROLE_ADMIN'), - //Set default user to current - 'user' => $this->getUser()->getId(), - //Set default slot to evening - //XXX: default to Evening (3) - 'slot' => $this->getDoctrine()->getRepository(Slot::class)->findOneById(3) - ]); + //Get location + $location = $form->getData(); - //Add form to context - $context['application'] = $application->createView(); - //Create login form for anonymous - } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) { - //Create ApplicationType form - $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [ - //Set the action - 'action' => $this->generateUrl('rapsys_user_login'), - //Set the form attribute - 'attr' => [ 'class' => 'col' ] - ]); + //Set created + $location->setCreated(new \DateTime('now')); - //Add form to context - $context['login'] = $login->createView(); + //Set updated + $location->setUpdated(new \DateTime('now')); + + //Queue location save + $manager->persist($location); + + //Flush to get the ids + $manager->flush(); + + //Add notice + $this->addFlash('notice', $this->translator->trans('Location %id% created', ['%id%' => $location->getId()])); + + //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']); + + //Check if location view route + if ($name == 'rapsys_air_location_view' && !empty($route['id'])) { + //Replace id + $route['id'] = $location->getId(); + //Other routes + } else { + //Set location + $route['location'] = $location->getId(); + } + + //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', ['location' => $location->getId()]); + } + + /** + * Edit location + * + * @desc Persist location 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-admin to access here + $this->denyAccessUnlessGranted('ROLE_ADMIN', null, $this->translator->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator->trans('Admin')])); + + //Get doctrine + $doctrine = $this->getDoctrine(); + + //Get location + if (empty($location = $doctrine->getRepository(Location::class)->findOneById($id))) { + throw $this->createNotFoundException($this->translator->trans('Unable to find location: %id%', ['%id%' => $id])); + } + + //Create LocationType form + $form = $this->createForm('Rapsys\AirBundle\Form\LocationType', $location, [ + //Set the action + 'action' => $this->generateUrl('rapsys_air_location_edit', ['id' => $id]), + //Set the form attribute + 'attr' => [] + ]); + + //Refill the fields in case of invalid form + $form->handleRequest($request); + + //Handle invalid form + if (!$form->isSubmitted() || !$form->isValid()) { + //Set section + $section = $this->translator->trans('Location %id%', ['%id%' => $id]); + + //Set title + $title = $this->translator->trans($this->config['site']['title']).' - '.$section; + + //Render the view + return $this->render('@RapsysAir/location/edit.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'form' => $form->createView()]+$this->context); + } + + //Get manager + $manager = $doctrine->getManager(); + + //Set updated + $location->setUpdated(new \DateTime('now')); + + //Queue location save + $manager->persist($location); + + //Flush to get the ids + $manager->flush(); + + //Add notice + $this->addFlash('notice', $this->translator->trans('Location %id% updated', ['%id%' => $id])); + + //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']); + + //Check if location view route + if ($name == 'rapsys_air_location_view' && !empty($route['id'])) { + //Replace id + $route['id'] = $location->getId(); + //Other routes + } else { + //Set location + $route['location'] = $location->getId(); + } + + //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', ['location' => $location->getId()]); + } + + /** + * List all locations + * + * @desc Display all locations + * + * @param Request $request The request instance + * + * @return Response The rendered view + */ + public function index(Request $request): Response { //Fetch doctrine $doctrine = $this->getDoctrine(); + //Set section + $section = $this->translator->trans('Libre Air locations'); + + //Set description + $this->context['description'] = $this->translator->trans('Libre Air location list'); + + //Set keywords + $this->context['keywords'] = [ + $this->translator->trans('locations'), + $this->translator->trans('location list'), + $this->translator->trans('listing'), + $this->translator->trans('Libre Air') + ]; + + //Set title + $title = $this->translator->trans($this->config['site']['title']).' - '.$section; + //Compute period $period = new \DatePeriod( //Start from first monday of week @@ -70,97 +294,95 @@ class LocationController extends DefaultController { //Iterate on each day new \DateInterval('P1D'), //End with next sunday and 4 weeks - new \DateTime('Monday this week + 5 week') + new \DateTime( + $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 3 week':'Monday this week + 2 week' + ) ); - //Fetch sessions - $sessions = $doctrine->getRepository(Session::class)->findAllByDatePeriod($period); - - //Init calendar - $calendar = []; - - //Init month - $month = null; - - //Iterate on each day - foreach($period as $date) { - //Init day in calendar - $calendar[$Ymd = $date->format('Ymd')] = [ - 'title' => $date->format('d'), - 'class' => [], - 'sessions' => [] - ]; - //Append month for first day of month - if ($month != $date->format('m')) { - $month = $date->format('m'); - $calendar[$Ymd]['title'] .= '/'.$month; - } - //Deal with today - if ($date->format('U') == ($today = strtotime('today'))) { - $calendar[$Ymd]['title'] .= '/'.$month; - $calendar[$Ymd]['current'] = true; - $calendar[$Ymd]['class'][] = 'current'; - } - //Disable passed days - if ($date->format('U') < $today) { - $calendar[$Ymd]['disabled'] = true; - $calendar[$Ymd]['class'][] = 'disabled'; - } - //Set next month days - if ($date->format('m') > date('m')) { - $calendar[$Ymd]['next'] = true; - $calendar[$Ymd]['class'][] = 'next'; - } - //Iterate on each session to find the one of the day - foreach($sessions as $session) { - if (($sessionYmd = $session->getDate()->format('Ymd')) == $Ymd) { - //Count number of application - $count = count($session->getApplications()); - - //Compute classes - $class = []; - if ($session->getApplication()) { - $class[] = 'granted'; - } elseif ($count == 0) { - $class[] = 'orphaned'; - } elseif ($count > 1) { - $class[] = 'disputed'; - } else { - $class[] = 'pending'; - } - - //Add the session - $calendar[$Ymd]['sessions'][$session->getSlot()->getId().$session->getLocation()->getId()] = [ - 'id' => $session->getId(), - 'title' => ($count > 1?'['.$count.'] ':'').$session->getSlot()->getTitle().' '.$session->getLocation()->getTitle(), - 'class' => $class - ]; - } + //Create location forms for role_admin + if ($this->isGranted('ROLE_ADMIN')) { + //Fetch all locations + $locations = $doctrine->getRepository(Location::class)->findAll(); + + //Rekey by id + $locations = array_reduce($locations, function($carry, $item){$carry[$item->getId()] = $item; return $carry;}, []); + + //Init locations to context + $this->context['forms']['locations'] = []; + + //Iterate on locations + foreach($locations as $locationId => $location) { + //Create LocationType form + $form = $this->createForm('Rapsys\AirBundle\Form\LocationType', $location, [ + //Set the action + 'action' => $this->generateUrl('rapsys_air_location_edit', ['id' => $location->getId()]), + //Set the form attribute + 'attr' => [], + //Set block prefix + //TODO: make this shit works to prevent label collision + //XXX: see https://stackoverflow.com/questions/8703016/adding-a-prefix-to-a-form-label-for-translation + 'label_prefix' => 'location_'.$locationId + ]); + + //Add form to context + $this->context['forms']['locations'][$locationId] = $form->createView(); } - //Sort sessions - ksort($calendar[$Ymd]['sessions']); + //Create LocationType form + $form = $this->createForm('Rapsys\AirBundle\Form\LocationType', null, [ + //Set the action + 'action' => $this->generateUrl('rapsys_air_location_add'), + //Set the form attribute + 'attr' => [ 'class' => 'col' ] + ]); + + //Add form to context + $this->context['forms']['location'] = $form->createView(); } + //Fetch locations + //XXX: we want to display all active locations anyway + $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period); + //Render the view - return $this->render('@RapsysAir/location/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar]+$context+$this->context); + return $this->render('@RapsysAir/location/index.html.twig', ['title' => $title, 'section' => $section, 'locations' => $locations]+$this->context); } - public function view(Request $request, $id) { + /** + * List all sessions for the location + * + * @desc Display all sessions for the location with an application or login form + * + * @param Request $request The request instance + * @param int $id The location id + * + * @return Response The rendered view + */ + public function view(Request $request, $id): Response { //Fetch doctrine $doctrine = $this->getDoctrine(); //Fetch location - $location = $doctrine->getRepository(Location::class)->findOneById($id); + if (empty($location = $doctrine->getRepository(Location::class)->findOneById($id))) { + throw $this->createNotFoundException($this->translator->trans('Unable to find location: %id%', ['%id%' => $id])); + } //Set section - $section = $this->translator->trans($location); + $section = $this->translator->trans('Argentine Tango at '.$location); - //Set title - $title = $section.' - '.$this->translator->trans($this->config['site']['title']); + //Set description + $this->context['description'] = $this->translator->trans('Outdoor Argentine Tango session calendar %location%', [ '%location%' => $this->translator->trans('at '.$location) ]); + + //Set keywords + $this->context['keywords'] = [ + $this->translator->trans($location), + $this->translator->trans('outdoor'), + $this->translator->trans('Argentine Tango'), + $this->translator->trans('calendar') + ]; - //Init context - $context = []; + //Set title + $title = $this->translator->trans($this->config['site']['title']).' - '.$section; //Create application form for role_guest if ($this->isGranted('ROLE_GUEST')) { @@ -176,23 +398,13 @@ class LocationController extends DefaultController { 'user' => $this->getUser()->getId(), //Set default slot to evening //XXX: default to Evening (3) - 'slot' => $this->getDoctrine()->getRepository(Slot::class)->findOneById(3) + 'slot' => $doctrine->getRepository(Slot::class)->findOneById(3), + //Set default location to current one + 'location' => $location ]); //Add form to context - $context['application'] = $application->createView(); - //Create login form for anonymous - } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) { - //Create ApplicationType form - $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [ - //Set the action - 'action' => $this->generateUrl('rapsys_user_login'), - //Set the form attribute - 'attr' => [ 'class' => 'col' ] - ]); - - //Add form to context - $context['login'] = $login->createView(); + $this->context['forms']['application'] = $application->createView(); } //Compute period @@ -202,79 +414,19 @@ class LocationController extends DefaultController { //Iterate on each day new \DateInterval('P1D'), //End with next sunday and 4 weeks - new \DateTime('Monday this week + 5 week') + new \DateTime( + $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 3 week':'Monday this week + 2 week' + ) ); - //Fetch sessions - $sessions = $doctrine->getRepository(Session::class)->findAllByLocationDatePeriod($location, $period); - - //Init calendar - $calendar = []; - - //Init month - $month = null; - - //Iterate on each day - foreach($period as $date) { - //Init day in calendar - $calendar[$Ymd = $date->format('Ymd')] = [ - 'title' => $date->format('d'), - 'class' => [], - 'sessions' => [] - ]; - //Append month for first day of month - if ($month != $date->format('m')) { - $month = $date->format('m'); - $calendar[$Ymd]['title'] .= '/'.$month; - } - //Deal with today - if ($date->format('U') == ($today = strtotime('today'))) { - $calendar[$Ymd]['title'] .= '/'.$month; - $calendar[$Ymd]['current'] = true; - $calendar[$Ymd]['class'][] = 'current'; - } - //Disable passed days - if ($date->format('U') < $today) { - $calendar[$Ymd]['disabled'] = true; - $calendar[$Ymd]['class'][] = 'disabled'; - } - //Set next month days - if ($date->format('m') > date('m')) { - $calendar[$Ymd]['next'] = true; - $calendar[$Ymd]['class'][] = 'next'; - } - //Iterate on each session to find the one of the day - foreach($sessions as $session) { - if (($sessionYmd = $session->getDate()->format('Ymd')) == $Ymd) { - //Count number of application - $count = count($session->getApplications()); - - //Compute classes - $class = []; - if ($session->getApplication()) { - $class[] = 'granted'; - } elseif ($count == 0) { - $class[] = 'orphaned'; - } elseif ($count > 1) { - $class[] = 'disputed'; - } else { - $class[] = 'pending'; - } - - //Add the session - $calendar[$Ymd]['sessions'][$session->getSlot()->getId().$session->getLocation()->getId()] = [ - 'id' => $session->getId(), - 'title' => ($count > 1?'['.$count.'] ':'').$session->getSlot()->getTitle().' '.$session->getLocation()->getTitle(), - 'class' => $class - ]; - } - } + //Fetch calendar + $calendar = $doctrine->getRepository(Session::class)->fetchCalendarByDatePeriod($this->translator, $period, $id, $request->get('session'), !$this->isGranted('IS_AUTHENTICATED_REMEMBERED')); - //Sort sessions - ksort($calendar[$Ymd]['sessions']); - } + //Fetch locations + //XXX: we want to display all active locations anyway + $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period); //Render the view - return $this->render('@RapsysAir/location/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar]+$context+$this->context); + return $this->render('@RapsysAir/location/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$this->context); } }