3 namespace Rapsys\AirBundle\Controller
; 
   5 use Symfony\Component\HttpFoundation\Request
; 
   7 class LocationController 
extends DefaultController 
{ 
   9         public function index(Request 
$request = null) { 
  13                 //Create the form according to the FormType created previously. 
  14                 //And give the proper parameters 
  15                 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ 
  16                         // To set the action use $this->generateUrl('route_identifier') 
  17                         'action' => $this->generateUrl('rapsys_air_location'), 
  19                         'attr' => [ 'class' => 'form_col' ] 
  21                 return $this->render('@RapsysAir/location/index.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'calendar' => $calendar]+
$this->context
); 
  24         public function show(Request 
$request, $id) { 
  27         public function toto(Request 
$request) { 
  28                 //Prevent non-admin to access here 
  29                 $this->denyAccessUnlessGranted('ROLE_GUEST', null, 'Unable to access this page without ROLE_GUEST!'); 
  32                 $section = $this->translator
->trans('Admin'); 
  35                 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']); 
  37                 header('Content-Type: text/plain'); 
  40                 //Create the form according to the FormType created previously. 
  41                 //And give the proper parameters 
  42                 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ 
  43                         // To set the action use $this->generateUrl('route_identifier') 
  44                         'action' => $this->generateUrl('rapsys_air_admin'), 
  46                         'attr' => [ 'class' => 'form_col' ] 
  50                 $doctrine = $this->getDoctrine(); 
  53                 if ($request->isMethod('POST')) { 
  54                         // Refill the fields in case the form is not valid. 
  55                         $form->handleRequest($request); 
  57                         if ($form->isValid()) { 
  59                                 $data = $form->getData(); 
  62                                 $manager = $doctrine->getManager(); 
  64                                 //Protect session fetching 
  66                                         $session = $doctrine->getRepository(Session
::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']); 
  67                                 //Catch no session case 
  68                                 } catch (\Doctrine\ORM\NoResultException 
$e) { 
  70                                         $session = new Session(); 
  71                                         $session->setLocation($data['location']); 
  72                                         $session->setSlot($data['slot']); 
  73                                         $session->setDate($data['date']); 
  74                                         $session->setCreated(new \
DateTime('now')); 
  75                                         $session->setUpdated(new \
DateTime('now')); 
  76                                         $manager->persist($session); 
  77                                         //Flush to get the ids 
  84                                 //Protect application fetching 
  86                                         //TODO: handle admin case where we provide a user in extra 
  87                                         $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $this->getUser()); 
  89                                         //Add error message to mail field 
  90                                         $form->get('slot')->addError(new FormError($this->translator
->trans('Application already exists'))); 
  91                                 //Catch no application cases 
  92                                 //XXX: combine these catch when php 7.1 is available 
  93                                 } catch (\Doctrine\ORM\NoResultException 
$e) { 
  94                                 //Catch invalid argument because session is not already persisted 
  95                                 } catch(\Doctrine\ORM\ORMInvalidArgumentException 
$e) { 
  98                                 //Create new application if none found 
 100                                         //Create the application 
 101                                         $application = new Application(); 
 102                                         $application->setSession($session); 
 103                                         //TODO: handle admin case where we provide a user in extra 
 104                                         $application->setUser($this->getUser()); 
 105                                         $application->setCreated(new \
DateTime('now')); 
 106                                         $application->setUpdated(new \
DateTime('now')); 
 107                                         $manager->persist($application); 
 109                                         //Flush to get the ids 
 112                                         //Add notice in flash message 
 113                                         $this->addFlash('notice', $this->translator
->trans('Application request the %date% for %location% on the slot %slot% saved', ['%location%' => $data['location']->getTitle(), '%slot%' => $data['slot']->getTitle(), '%date%' => $data['date']->format('Y-m-d')])); 
 115                                         //Redirect to cleanup the form 
 116                                         return $this->redirectToRoute('rapsys_air_admin'); 
 122                 $period = new \
DatePeriod( 
 123                         //Start from first monday of week 
 124                         new \
DateTime('Monday this week'), 
 125                         //Iterate on each day 
 126                         new \
DateInterval('P1D'), 
 127                         //End with next sunday and 4 weeks 
 128                         new \
DateTime('Monday this week + 5 week') 
 132                 $sessions = $doctrine->getRepository(Session
::class)->findByDatePeriod($period); 
 140                 //Iterate on each day 
 141                 foreach($period as $date) { 
 142                         //Init day in calendar 
 143                         $calendar[$Ymd = $date->format('Ymd')] = [ 
 144                                 'title' => $date->format('d'), 
 148                         //Append month for first day of month 
 149                         if ($month != $date->format('m')) { 
 150                                 $month = $date->format('m'); 
 151                                 $calendar[$Ymd]['title'] .= '/'.$month; 
 154                         if ($date->format('U') == ($today = strtotime('today'))) { 
 155                                 $calendar[$Ymd]['title'] .= '/'.$month; 
 156                                 $calendar[$Ymd]['current'] = true; 
 157                                 $calendar[$Ymd]['class'][] =  'current'; 
 159                         //Disable passed days 
 160                         if ($date->format('U') < $today) { 
 161                                 $calendar[$Ymd]['disabled'] = true; 
 162                                 $calendar[$Ymd]['class'][] =  'disabled'; 
 164                         //Set next month days 
 165                         if ($date->format('m') > date('m')) { 
 166                                 $calendar[$Ymd]['next'] = true; 
 167                                 $calendar[$Ymd]['class'][] =  'next'; 
 169                         //Iterate on each session to find the one of the day 
 170                         foreach($sessions as $session) { 
 171                                 if (($sessionYmd = $session->getDate()->format('Ymd')) == $Ymd) { 
 172                                         //Count number of application 
 173                                         $count = count($session->getApplications()); 
 177                                         if ($session->getApplication()) { 
 178                                                 $class[] = 'granted'; 
 179                                         } elseif ($count == 0) { 
 180                                                 $class[] = 'orphaned'; 
 181                                         } elseif ($count > 1) { 
 182                                                 $class[] = 'disputed'; 
 184                                                 $class[] = 'pending'; 
 188                                         $calendar[$Ymd]['sessions'][$session->getSlot()->getId().$session->getLocation()->getId()] = [ 
 189                                                 'id' => $session->getId(), 
 190                                                 'title' => ($count > 1?'['.$count.'] ':'').$session->getSlot()->getTitle().' '.$session->getLocation()->getTitle(), 
 197                         ksort($calendar[$Ymd]['sessions']); 
 200                 return $this->render('@RapsysAir/admin/index.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'calendar' => $calendar]); 
 203         public function tata(Request 
$request, $id) { 
 204                 /*header('Content-Type: text/plain'); 
 209                 $section = $this->translator
->trans('Session %id%', ['%id%' => $id]); 
 212                 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']); 
 214                 //Create the form according to the FormType created previously. 
 215                 //And give the proper parameters 
 216                 /*$form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ 
 217                         // To set the action use $this->generateUrl('route_identifier') 
 218                         'action' => $this->generateUrl('rapsys_air_admin'), 
 220                         'attr' => [ 'class' => 'form_col' ] 
 224                 $doctrine = $this->getDoctrine(); 
 227                 $session = $doctrine->getRepository(Session
::class)->findOneById($id); 
 229                 return $this->render('@RapsysAir/admin/session.html.twig', ['title' => $title, 'section' => $section, /*'form' => $form->createView(),*/ 'session' => $session]);