3 namespace Rapsys\AirBundle\Controller
; 
   5 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController 
as Controller
; 
   6 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
; 
   7 use Symfony\Component\HttpFoundation\Request
; 
   8 use Rapsys\AirBundle\Entity\Session
; 
   9 use Rapsys\AirBundle\Entity\Application
; 
  10 use Symfony\Component\Form\FormError
; 
  12 class DefaultController 
extends Controller 
{ 
  13         public function contactAction(Request 
$request) { 
  15                 $trans = $this->get('translator'); 
  18                 $section = $trans->trans('Contact'); 
  21                 $title = $section.' - '.$trans->trans($this->getParameter('rapsys_air.title')); 
  23                 //Create the form according to the FormType created previously. 
  24                 //And give the proper parameters 
  25                 $form = $this->createForm('Rapsys\AirBundle\Form\ContactType', null, [ 
  26                         // To set the action use $this->generateUrl('route_identifier') 
  27                         'action' => $this->generateUrl('rapsys_air_contact'), 
  31                 if ($request->isMethod('POST')) { 
  32                         // Refill the fields in case the form is not valid. 
  33                         $form->handleRequest($request); 
  35                         if ($form->isValid()) { 
  37                                 $data = $form->getData(); 
  40                                 $contactName = $this->getParameter('rapsys_air.contact_name'); 
  43                                 $contactMail = $this->getParameter('rapsys_air.contact_mail'); 
  46                                 $logo = $this->getParameter('rapsys_air.logo'); 
  49                                 $title = $trans->trans($this->getParameter('rapsys_air.title')); 
  52                                 $subtitle = $trans->trans('Hi,').' '.$contactName; 
  54                                 $message = \Swift_Message
::newInstance() 
  55                                         ->setSubject($data['subject']) 
  56                                         ->setFrom([$data['mail'] => $data['name']]) 
  57                                         ->setTo([$contactMail => $contactName]) 
  58                                         ->setBody($data['message']) 
  61                                                         '@RapsysAir/mail/generic.html.twig', 
  65                                                                 'subtitle' => $subtitle, 
  66                                                                 'home' => $this->get('router')->generate('rapsys_air_homepage', [], UrlGeneratorInterface
::ABSOLUTE_URL
), 
  67                                                                 'subject' => $data['subject'], 
  68                                                                 'contact_name' => $contactName, 
  69                                                                 'message' => strip_tags($data['message']) 
  75                                 if ($this->get('mailer')->send($message)) { 
  76                                         //Redirect to cleanup the form 
  77                                         return $this->redirectToRoute('rapsys_air_contact', ['sent' => 1]); 
  83                 return $this->render('@RapsysAir/form/contact.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'sent' => $request->query
->get('sent', 0)]); 
  86         public function indexAction() { 
  88                 $trans = $this->get('translator'); 
  91                 $section = $trans->trans('Index'); 
  94                 $title = $section.' - '.$trans->trans($this->getParameter('rapsys_air.title')); 
  96                 return $this->render('@RapsysAir/page/index.html.twig', ['title' => $title, 'section' => $section]); 
  99         public function adminAction(Request 
$request) { 
 100                 $this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!'); 
 103                 $trans = $this->get('translator'); 
 106                 $section = $trans->trans('Admin'); 
 109                 $title = $section.' - '.$trans->trans($this->getParameter('rapsys_air.title')); 
 111                 //Create the form according to the FormType created previously. 
 112                 //And give the proper parameters 
 113                 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ 
 114                         // To set the action use $this->generateUrl('route_identifier') 
 115                         'action' => $this->generateUrl('rapsys_air_admin'), 
 117                         'attr' => [ 'class' => 'form_col' ] 
 121                 $doctrine = $this->getDoctrine(); 
 124                 if ($request->isMethod('POST')) { 
 125                         // Refill the fields in case the form is not valid. 
 126                         $form->handleRequest($request); 
 128                         if ($form->isValid()) { 
 130                                 $data = $form->getData(); 
 133                                 $manager = $doctrine->getManager(); 
 135                                 //Protect session fetching 
 137                                         $session = $doctrine->getRepository(Session
::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']); 
 138                                 //Catch no session case 
 139                                 } catch (\Doctrine\ORM\NoResultException 
$e) { 
 141                                         $session = new Session(); 
 142                                         $session->setLocation($data['location']); 
 143                                         $session->setSlot($data['slot']); 
 144                                         $session->setDate($data['date']); 
 145                                         $session->setCreated(new \
DateTime('now')); 
 146                                         $session->setUpdated(new \
DateTime('now')); 
 147                                         $manager->persist($session); 
 148                                         //Flush to get the ids 
 153                                 $application = false; 
 155                                 //Protect application fetching 
 157                                         //TODO: handle admin case where we provide a user in extra 
 158                                         $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $this->getUser()); 
 160                                         //Add error message to mail field 
 161                                         $form->get('slot')->addError(new FormError($trans->trans('Application already exists'))); 
 162                                 //Catch no application cases 
 163                                 //XXX: combine these catch when php 7.1 is available 
 164                                 } catch (\Doctrine\ORM\NoResultException 
$e) { 
 165                                 //Catch invalid argument because session is not already persisted 
 166                                 } catch(\Doctrine\ORM\ORMInvalidArgumentException 
$e) { 
 169                                 //Create new application if none found 
 171                                         //Create the application 
 172                                         $application = new Application(); 
 173                                         $application->setSession($session); 
 174                                         //TODO: handle admin case where we provide a user in extra 
 175                                         $application->setUser($this->getUser()); 
 176                                         $application->setCreated(new \
DateTime('now')); 
 177                                         $application->setUpdated(new \
DateTime('now')); 
 178                                         $manager->persist($application); 
 180                                         //Flush to get the ids 
 183                                         //Add notice in flash message 
 184                                         $this->addFlash('notice', $trans->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')])); 
 186                                         //Redirect to cleanup the form 
 187                                         return $this->redirectToRoute('rapsys_air_admin'); 
 193                 $period = new \
DatePeriod( 
 194                         //Start from first monday of week 
 195                         new \
DateTime('Monday this week'), 
 196                         //Iterate on each day 
 197                         new \
DateInterval('P1D'), 
 198                         //End with next sunday and 4 weeks 
 199                         new \
DateTime('Monday this week + 5 week') 
 203                 $sessions = $doctrine->getRepository(Session
::class)->findByDatePeriod($period); 
 211                 //Iterate on each day 
 212                 foreach($period as $date) { 
 213                         //Init day in calendar 
 214                         $calendar[$Ymd = $date->format('Ymd')] = [ 
 215                                 'title' => $date->format('d'), 
 219                         //Append month for first day of month 
 220                         if ($month != $date->format('m')) { 
 221                                 $month = $date->format('m'); 
 222                                 $calendar[$Ymd]['title'] .= '/'.$month; 
 225                         if ($date->format('U') == ($today = strtotime('today'))) { 
 226                                 $calendar[$Ymd]['title'] .= '/'.$month; 
 227                                 $calendar[$Ymd]['current'] = true; 
 228                                 $calendar[$Ymd]['class'][] =  'current'; 
 230                         //Disable passed days 
 231                         if ($date->format('U') < $today) { 
 232                                 $calendar[$Ymd]['disabled'] = true; 
 233                                 $calendar[$Ymd]['class'][] =  'disabled'; 
 235                         //Set next month days 
 236                         if ($date->format('m') > date('m')) { 
 237                                 $calendar[$Ymd]['next'] = true; 
 238                                 $calendar[$Ymd]['class'][] =  'next'; 
 240                         //Iterate on each session to find the one of the day 
 241                         foreach($sessions as $session) { 
 242                                 if (($sessionYmd = $session->getDate()->format('Ymd')) == $Ymd) { 
 243                                         //Count number of application 
 244                                         $count = count($session->getApplications()); 
 248                                         if ($session->getApplication()) { 
 249                                                 $class[] = 'granted'; 
 250                                         } elseif ($count == 0) { 
 251                                                 $class[] = 'orphaned'; 
 252                                         } elseif ($count > 1) { 
 253                                                 $class[] = 'disputed'; 
 255                                                 $class[] = 'pending'; 
 259                                         $calendar[$Ymd]['sessions'][$session->getSlot()->getId().$session->getLocation()->getId()] = [ 
 260                                                 'id' => $session->getId(), 
 261                                                 'title' => ($count > 1?'['.$count.'] ':'').$session->getSlot()->getTitle().' '.$session->getLocation()->getTitle(), 
 268                         ksort($calendar[$Ymd]['sessions']); 
 271                 return $this->render('@RapsysAir/admin/index.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'calendar' => $calendar]); 
 274         public function sessionAction(Request 
$request, $id) { 
 275                 /*header('Content-Type: text/plain'); 
 280                 $trans = $this->get('translator'); 
 283                 $section = $trans->trans('Session %id%', ['%id%' => $id]); 
 286                 $title = $section.' - '.$trans->trans($this->getParameter('rapsys_air.title')); 
 288                 //Create the form according to the FormType created previously. 
 289                 //And give the proper parameters 
 290                 /*$form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ 
 291                         // To set the action use $this->generateUrl('route_identifier') 
 292                         'action' => $this->generateUrl('rapsys_air_admin'), 
 294                         'attr' => [ 'class' => 'form_col' ] 
 298                 $doctrine = $this->getDoctrine(); 
 301                 $session = $doctrine->getRepository(Session
::class)->findOneById($id); 
 303                 return $this->render('@RapsysAir/admin/session.html.twig', ['title' => $title, 'section' => $section, /*'form' => $form->createView(),*/ 'session' => $session]);