3 namespace Rapsys\AirBundle\Controller
; 
   5 use Symfony\Component\HttpFoundation\Request
; 
   6 use Rapsys\AirBundle\Entity\Slot
; 
   7 use Rapsys\AirBundle\Entity\User
; 
   8 use Rapsys\AirBundle\Entity\Session
; 
   9 use Rapsys\AirBundle\Entity\Application
; 
  11 class ApplicationController 
extends DefaultController 
{ 
  15          * @desc Persist application and all required dependencies in database 
  17          * @param Request $request The request instance 
  19          * @return Response The rendered view or redirection 
  21          * @throws \RuntimeException When user has not at least guest role 
  23         public function add(Request 
$request) { 
  24                 //Prevent non-guest to access here 
  25                 $this->denyAccessUnlessGranted('ROLE_GUEST', null, $this->translator
->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator
->trans('Guest')])); 
  27                 //Create ApplicationType form 
  28                 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ 
  30                         'action' => $this->generateUrl('rapsys_air_application_add'), 
  31                         //Set the form attribute 
  32                         #'attr' => [ 'class' => 'col' ], 
  34                         'admin' => $this->isGranted('ROLE_ADMIN'), 
  35                         //Set default user to current 
  36                         'user' => $this->getUser()->getId(), 
  37                         //Set default slot to evening 
  38                         //XXX: default to Evening (3) 
  39                         'slot' => $this->getDoctrine()->getRepository(Slot
::class)->findOneById(3) 
  42                 //Reject non post requests 
  43                 if (!$request->isMethod('POST')) { 
  44                         throw new \
RuntimeException('Request method MUST be POST'); 
  47                 //Refill the fields in case of invalid form 
  48                 $form->handleRequest($request); 
  51                 if (!$form->isValid()) { 
  53                         $section = $this->translator
->trans('Application Add'); 
  56                         $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']); 
  59                         return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form]+
$this->context
); 
  63                 $doctrine = $this->getDoctrine(); 
  66                 $manager = $doctrine->getManager(); 
  69                 $data = $form->getData(); 
  71                 //Protect session fetching 
  74                         $session = $doctrine->getRepository(Session
::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']); 
  75                 //Catch no session case 
  76                 } catch (\Doctrine\ORM\NoResultException 
$e) { 
  78                         $session = new Session(); 
  79                         $session->setLocation($data['location']); 
  80                         $session->setDate($data['date']); 
  81                         $session->setSlot($data['slot']); 
  82                         $session->setCreated(new \
DateTime('now')); 
  83                         $session->setUpdated(new \
DateTime('now')); 
  86                         $manager->persist($session); 
  88                         //Flush to get the ids 
  91                         $this->addFlash('notice', $this->translator
->trans('Session on %date% %location% %slot% created', ['%location%' => $this->translator
->trans('at '.$data['location']), '%slot%' => $this->translator
->trans('the '.strtolower($data['slot'])), '%date%' => $data['date']->format('Y-m-d')])); 
  95                 $user = $this->getUser(); 
  97                 //Replace with requested user for admin 
  98                 if ($this->isGranted('ROLE_ADMIN') && !empty($data['user'])) { 
  99                         $user = $this->getDoctrine()->getRepository(User
::class)->findOneById($data['user']); 
 102                 //Protect application fetching 
 104                         //Retrieve application 
 105                         $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $user); 
 107                         //Add notice in flash message 
 108                         //TODO: set warning about application already exists bla bla bla... 
 109                         #$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')])); 
 111                         //Add error message to mail field 
 112                         #$form->get('slot')->addError(new FormError($this->translator->trans('Application already exists'))); 
 114                         //TODO: redirect anyway on uri with application highlighted 
 115                 //Catch no application and session without identifier (not persisted&flushed) cases 
 116                 } catch (\Doctrine\ORM\NoResultException
|\Doctrine\ORM\ORMInvalidArgumentException 
$e) { 
 117                         //Create the application 
 118                         $application = new Application(); 
 119                         $application->setSession($session); 
 120                         $application->setUser($user); 
 121                         $application->setCreated(new \
DateTime('now')); 
 122                         $application->setUpdated(new \
DateTime('now')); 
 124                         //Refresh session updated field 
 125                         $session->setUpdated(new \
DateTime('now')); 
 128                         $manager->persist($session); 
 130                         //Queue application save 
 131                         $manager->persist($application); 
 133                         //Flush to get the ids 
 136                         //Add notice in flash message 
 137                         $this->addFlash('notice', $this->translator
->trans('Application on %date% %location% %slot% created', ['%location%' => $this->translator
->trans('at '.$data['location']), '%slot%' => $this->translator
->trans('the '.strtolower($data['slot'])), '%date%' => $data['date']->format('Y-m-d')])); 
 140                 //Try unshort return field 
 142                         !empty($data['return']) && 
 143                         ($unshort = $this->slugger
->unshort($data['return'])) && 
 144                         ($route = json_decode($unshort, true)) !== null 
 146                         $return = $this->generateUrl($route['_route'], ['session' => $session->getId()?:1]+
$route['_route_params']); 
 150                 header('Content-Type: text/plain'); 
 152                 //Extract and process referer 
 153                 if ($referer = $request->headers
->get('referer')) { 
 154                         //Create referer request instance 
 155                         $req = Request
::create($referer); 
 158                         $path = $req->getPathInfo(); 
 160                         //Get referer query string 
 161                         $query = $req->getQueryString(); 
 164                         $path = str_replace($request->getScriptName(), '', $path); 
 166                         //Try with referer path 
 168                                 var_dump($this->router
); 
 170                                 var_dump($path = '/location'); 
 171                                 var_dump($this->router
->match($path)); 
 172                                 var_dump($path = '/fr/emplacement'); 
 174                                 var_dump($this->router
->match()); 
 179                                 //Retrieve route matching path 
 180                                 $route = $this->router
->match($path); 
 184                                 //Verify that it differ from current one 
 185                                 if (($name = $route['_route']) == $logout) { 
 186                                         throw new ResourceNotFoundException('Identical referer and logout route'); 
 189                                 //Remove route and controller from route defaults 
 190                                 unset($route['_route'], $route['_controller']); 
 193                                 $url = $this->router
->generate($name, $route); 
 195                         } catch(ResourceNotFoundException 
$e) { 
 196                                 //Unset referer to fallback to default route 
 200                 var_dump($request->headers
->get('referer')); 
 201                 #var_dump($request->get('_route')); 
 206                 //Fetch slugger helper 
 207                 $slugger = $this->get('rapsys.slugger'); 
 209                 var_dump($short = $slugger->short(json_encode(['_route' => $request->get('_route'), '_route_params' => $request->get('_route_params')]))); 
 211                 var_dump($ret = json_decode($slugger->unshort($short), true)); 
 213                 var_dump($this->generateUrl($ret['_route'], $ret['_route_params'])); 
 214                 #var_dump(json_decode($slugger->unshort($data['return']))); 
 215                 #var_dump($application->getId()); 
 219                 $application = false; 
 221                 //Protect application fetching 
 223                         //TODO: handle admin case where we provide a user in extra 
 224                         $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $this->getUser()); 
 226                         //Add error message to mail field 
 227                         $form->get('slot')->addError(new FormError($this->translator
->trans('Application already exists'))); 
 228                 //Catch no application cases 
 229                 //XXX: combine these catch when php 7.1 is available 
 230                 } catch (\Doctrine\ORM\NoResultException 
$e) { 
 231                 //Catch invalid argument because session is not already persisted 
 232                 } catch(\Doctrine\ORM\ORMInvalidArgumentException 
$e) { 
 235                 //Create new application if none found 
 237                         //Create the application 
 238                         $application = new Application(); 
 239                         $application->setSession($session); 
 240                         //TODO: handle admin case where we provide a user in extra 
 241                         $application->setUser($this->getUser()); 
 242                         $application->setCreated(new \
DateTime('now')); 
 243                         $application->setUpdated(new \
DateTime('now')); 
 244                         $manager->persist($application); 
 246                         //Flush to get the ids 
 249                         //Add notice in flash message 
 250                         $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')])); 
 252                         //Redirect to cleanup the form 
 253                         return $this->redirectToRoute('rapsys_air_admin'); 
 257         function test(Request 
$request) { 
 260                 $period = new \
DatePeriod( 
 261                         //Start from first monday of week 
 262                         new \
DateTime('Monday this week'), 
 263                         //Iterate on each day 
 264                         new \
DateInterval('P1D'), 
 265                         //End with next sunday and 4 weeks 
 266                         new \
DateTime('Monday this week + 5 week') 
 270                 $sessions = $doctrine->getRepository(Session
::class)->findAllByDatePeriod($period); 
 278                 //Iterate on each day 
 279                 foreach($period as $date) { 
 280                         //Init day in calendar 
 281                         $calendar[$Ymd = $date->format('Ymd')] = [ 
 282                                 'title' => $date->format('d'), 
 286                         //Append month for first day of month 
 287                         if ($month != $date->format('m')) { 
 288                                 $month = $date->format('m'); 
 289                                 $calendar[$Ymd]['title'] .= '/'.$month; 
 292                         if ($date->format('U') == ($today = strtotime('today'))) { 
 293                                 $calendar[$Ymd]['title'] .= '/'.$month; 
 294                                 $calendar[$Ymd]['current'] = true; 
 295                                 $calendar[$Ymd]['class'][] =  'current'; 
 297                         //Disable passed days 
 298                         if ($date->format('U') < $today) { 
 299                                 $calendar[$Ymd]['disabled'] = true; 
 300                                 $calendar[$Ymd]['class'][] =  'disabled'; 
 302                         //Set next month days 
 303                         if ($date->format('m') > date('m')) { 
 304                                 $calendar[$Ymd]['next'] = true; 
 305                                 $calendar[$Ymd]['class'][] =  'next'; 
 307                         //Iterate on each session to find the one of the day 
 308                         foreach($sessions as $session) { 
 309                                 if (($sessionYmd = $session->getDate()->format('Ymd')) == $Ymd) { 
 310                                         //Count number of application 
 311                                         $count = count($session->getApplications()); 
 315                                         if ($session->getApplication()) { 
 316                                                 $class[] = 'granted'; 
 317                                         } elseif ($count == 0) { 
 318                                                 $class[] = 'orphaned'; 
 319                                         } elseif ($count > 1) { 
 320                                                 $class[] = 'disputed'; 
 322                                                 $class[] = 'pending'; 
 326                                         $calendar[$Ymd]['sessions'][$session->getSlot()->getId().$session->getLocation()->getId()] = [ 
 327                                                 'id' => $session->getId(), 
 328                                                 'title' => ($count > 1?'['.$count.'] ':'').$session->getSlot()->getTitle().' '.$session->getLocation()->getTitle(), 
 335                         ksort($calendar[$Ymd]['sessions']);