3 namespace Rapsys\AirBundle\Controller
; 
   5 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController
; 
   6 use Symfony\Component\DependencyInjection\ContainerInterface
; 
   7 use Symfony\Bundle\FrameworkBundle\Translation\Translator
; 
   8 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
; 
   9 use Symfony\Component\HttpFoundation\Request
; 
  10 use Rapsys\AirBundle\Entity\Session
; 
  11 use Rapsys\AirBundle\Entity\Application
; 
  12 use Symfony\Component\Form\FormError
; 
  14 #class DefaultController extends Controller { 
  15 class DefaultController 
extends AbstractController 
{ 
  20         protected $translator; 
  22         public function __construct(ContainerInterface 
$container, Translator 
$translator) { 
  24                 $this->config 
= $container->getParameter('rapsys_air'); 
  27                 $this->translator 
= $translator; 
  30         public function contactAction(Request 
$request) { 
  32                 $section = $this->translator
->trans('Contact'); 
  35                 $title = $section.' - '.$this->translator
->trans($this->config
['title']); 
  37                 //Create the form according to the FormType created previously. 
  38                 //And give the proper parameters 
  39                 $form = $this->createForm('Rapsys\AirBundle\Form\ContactType', null, [ 
  40                         // To set the action use $this->generateUrl('route_identifier') 
  41                         'action' => $this->generateUrl('rapsys_air_contact'), 
  45                 if ($request->isMethod('POST')) { 
  46                         // Refill the fields in case the form is not valid. 
  47                         $form->handleRequest($request); 
  49                         if ($form->isValid()) { 
  51                                 $data = $form->getData(); 
  54                                 $contactName = $this->config
['contact_name']; 
  57                                 $contactMail = $this->config
['contact_mail']; 
  60                                 $logo = $this->config
['logo']; 
  63                                 $title = $this->translator
->trans($this->config
['title']); 
  66                                 $subtitle = $this->translator
->trans('Hi,').' '.$contactName; 
  68                                 //Create sendmail transport 
  69                                 $transport = new \
Swift_SendmailTransport(); 
  71                                 //Create mailer using transport 
  72                                 $mailer = new \
Swift_Mailer($transport); 
  75                                 ($message = new \
Swift_Message($data['subject'])) 
  76                                         #->setSubject($data['subject']) 
  77                                         ->setFrom([$data['mail'] => $data['name']]) 
  78                                         ->setTo([$contactMail => $contactName]) 
  79                                         ->setBody($data['message']) 
  82                                                         '@RapsysAir/mail/generic.html.twig', 
  86                                                                 'subtitle' => $subtitle, 
  87                                                                 'home' => $this->get('router')->generate('rapsys_air_homepage', [], UrlGeneratorInterface
::ABSOLUTE_URL
), 
  88                                                                 'subject' => $data['subject'], 
  89                                                                 'contact_name' => $contactName, 
  90                                                                 'message' => strip_tags($data['message']) 
  97                                 if ($mailer->send($message)) { 
  98                                         //Redirect to cleanup the form 
  99                                         return $this->redirectToRoute('rapsys_air_contact', ['sent' => 1]); 
 105                 return $this->render('@RapsysAir/form/contact.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'sent' => $request->query
->get('sent', 0)]); 
 108         public function indexAction() { 
 110                 $section = $this->translator
->trans('Index'); 
 113                 $title = $section.' - '.$this->translator
->trans($this->config
['title']); 
 116                 return $this->render('@RapsysAir/page/index.html.twig', ['title' => $title, 'section' => $section]); 
 119         public function adminAction(Request 
$request) { 
 120                 //Prevent non-admin to access here 
 121                 //TODO: maybe check if user is connected 1st ? 
 122                 $this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!'); 
 125                 $section = $this->translator
->trans('Admin'); 
 128                 $title = $section.' - '.$this->translator
->trans($this->config
['title']); 
 130                 //Create the form according to the FormType created previously. 
 131                 //And give the proper parameters 
 132                 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ 
 133                         // To set the action use $this->generateUrl('route_identifier') 
 134                         'action' => $this->generateUrl('rapsys_air_admin'), 
 136                         'attr' => [ 'class' => 'form_col' ] 
 140                 $doctrine = $this->getDoctrine(); 
 143                 if ($request->isMethod('POST')) { 
 144                         // Refill the fields in case the form is not valid. 
 145                         $form->handleRequest($request); 
 147                         if ($form->isValid()) { 
 149                                 $data = $form->getData(); 
 152                                 $manager = $doctrine->getManager(); 
 154                                 //Protect session fetching 
 156                                         $session = $doctrine->getRepository(Session
::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']); 
 157                                 //Catch no session case 
 158                                 } catch (\Doctrine\ORM\NoResultException 
$e) { 
 160                                         $session = new Session(); 
 161                                         $session->setLocation($data['location']); 
 162                                         $session->setSlot($data['slot']); 
 163                                         $session->setDate($data['date']); 
 164                                         $session->setCreated(new \
DateTime('now')); 
 165                                         $session->setUpdated(new \
DateTime('now')); 
 166                                         $manager->persist($session); 
 167                                         //Flush to get the ids 
 172                                 $application = false; 
 174                                 //Protect application fetching 
 176                                         //TODO: handle admin case where we provide a user in extra 
 177                                         $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $this->getUser()); 
 179                                         //Add error message to mail field 
 180                                         $form->get('slot')->addError(new FormError($this->translator
->trans('Application already exists'))); 
 181                                 //Catch no application cases 
 182                                 //XXX: combine these catch when php 7.1 is available 
 183                                 } catch (\Doctrine\ORM\NoResultException 
$e) { 
 184                                 //Catch invalid argument because session is not already persisted 
 185                                 } catch(\Doctrine\ORM\ORMInvalidArgumentException 
$e) { 
 188                                 //Create new application if none found 
 190                                         //Create the application 
 191                                         $application = new Application(); 
 192                                         $application->setSession($session); 
 193                                         //TODO: handle admin case where we provide a user in extra 
 194                                         $application->setUser($this->getUser()); 
 195                                         $application->setCreated(new \
DateTime('now')); 
 196                                         $application->setUpdated(new \
DateTime('now')); 
 197                                         $manager->persist($application); 
 199                                         //Flush to get the ids 
 202                                         //Add notice in flash message 
 203                                         $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')])); 
 205                                         //Redirect to cleanup the form 
 206                                         return $this->redirectToRoute('rapsys_air_admin'); 
 212                 $period = new \
DatePeriod( 
 213                         //Start from first monday of week 
 214                         new \
DateTime('Monday this week'), 
 215                         //Iterate on each day 
 216                         new \
DateInterval('P1D'), 
 217                         //End with next sunday and 4 weeks 
 218                         new \
DateTime('Monday this week + 5 week') 
 222                 $sessions = $doctrine->getRepository(Session
::class)->findByDatePeriod($period); 
 230                 //Iterate on each day 
 231                 foreach($period as $date) { 
 232                         //Init day in calendar 
 233                         $calendar[$Ymd = $date->format('Ymd')] = [ 
 234                                 'title' => $date->format('d'), 
 238                         //Append month for first day of month 
 239                         if ($month != $date->format('m')) { 
 240                                 $month = $date->format('m'); 
 241                                 $calendar[$Ymd]['title'] .= '/'.$month; 
 244                         if ($date->format('U') == ($today = strtotime('today'))) { 
 245                                 $calendar[$Ymd]['title'] .= '/'.$month; 
 246                                 $calendar[$Ymd]['current'] = true; 
 247                                 $calendar[$Ymd]['class'][] =  'current'; 
 249                         //Disable passed days 
 250                         if ($date->format('U') < $today) { 
 251                                 $calendar[$Ymd]['disabled'] = true; 
 252                                 $calendar[$Ymd]['class'][] =  'disabled'; 
 254                         //Set next month days 
 255                         if ($date->format('m') > date('m')) { 
 256                                 $calendar[$Ymd]['next'] = true; 
 257                                 $calendar[$Ymd]['class'][] =  'next'; 
 259                         //Iterate on each session to find the one of the day 
 260                         foreach($sessions as $session) { 
 261                                 if (($sessionYmd = $session->getDate()->format('Ymd')) == $Ymd) { 
 262                                         //Count number of application 
 263                                         $count = count($session->getApplications()); 
 267                                         if ($session->getApplication()) { 
 268                                                 $class[] = 'granted'; 
 269                                         } elseif ($count == 0) { 
 270                                                 $class[] = 'orphaned'; 
 271                                         } elseif ($count > 1) { 
 272                                                 $class[] = 'disputed'; 
 274                                                 $class[] = 'pending'; 
 278                                         $calendar[$Ymd]['sessions'][$session->getSlot()->getId().$session->getLocation()->getId()] = [ 
 279                                                 'id' => $session->getId(), 
 280                                                 'title' => ($count > 1?'['.$count.'] ':'').$session->getSlot()->getTitle().' '.$session->getLocation()->getTitle(), 
 287                         ksort($calendar[$Ymd]['sessions']); 
 290                 return $this->render('@RapsysAir/admin/index.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'calendar' => $calendar]); 
 293         public function sessionAction(Request 
$request, $id) { 
 294                 /*header('Content-Type: text/plain'); 
 299                 $section = $this->translator
->trans('Session %id%', ['%id%' => $id]); 
 302                 $title = $section.' - '.$this->translator
->trans($this->config
['title']); 
 304                 //Create the form according to the FormType created previously. 
 305                 //And give the proper parameters 
 306                 /*$form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ 
 307                         // To set the action use $this->generateUrl('route_identifier') 
 308                         'action' => $this->generateUrl('rapsys_air_admin'), 
 310                         'attr' => [ 'class' => 'form_col' ] 
 314                 $doctrine = $this->getDoctrine(); 
 317                 $session = $doctrine->getRepository(Session
::class)->findOneById($id); 
 319                 return $this->render('@RapsysAir/admin/session.html.twig', ['title' => $title, 'section' => $section, /*'form' => $form->createView(),*/ 'session' => $session]);