3 namespace Rapsys\AirBundle\Controller
;
5 #use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController
;
7 use Psr\Container\ContainerInterface
;
8 use Symfony\Bundle\FrameworkBundle\Translation\Translator
;
9 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
10 use Symfony\Component\HttpFoundation\Request
;
11 use Rapsys\AirBundle\Entity\Session
;
12 use Rapsys\AirBundle\Entity\Application
;
13 use Symfony\Component\Form\FormError
;
15 #class DefaultController extends Controller {
16 class DefaultController
extends AbstractController
{
21 protected $translator;
23 public function __construct(ContainerInterface
$container, Translator
$translator) {
25 $this->container
= $container;
28 $this->translator
= $translator;
31 public function contactAction(Request
$request) {
33 $section = $this->translator
->trans('Contact');
36 $title = $section.' - '.$this->translator
->trans($this->container
->getParameter('rapsys_air.title'));
38 //Create the form according to the FormType created previously.
39 //And give the proper parameters
40 $form = $this->createForm('Rapsys\AirBundle\Form\ContactType', null, [
41 // To set the action use $this->generateUrl('route_identifier')
42 'action' => $this->generateUrl('rapsys_air_contact'),
46 if ($request->isMethod('POST')) {
47 // Refill the fields in case the form is not valid.
48 $form->handleRequest($request);
50 if ($form->isValid()) {
52 $data = $form->getData();
55 $contactName = $this->container
->getParameter('rapsys_air.contact_name');
58 $contactMail = $this->container
->getParameter('rapsys_air.contact_mail');
61 $logo = $this->container
->getParameter('rapsys_air.logo');
64 $title = $this->translator
->trans($this->container
->getParameter('rapsys_air.title'));
67 $subtitle = $this->translator
->trans('Hi,').' '.$contactName;
69 //Create sendmail transport
70 $transport = new \
Swift_SendmailTransport();
72 //Create mailer using transport
73 $mailer = new \
Swift_Mailer($transport);
76 ($message = new \
Swift_Message($data['subject']))
77 #->setSubject($data['subject'])
78 ->setFrom([$data['mail'] => $data['name']])
79 ->setTo([$contactMail => $contactName])
80 ->setBody($data['message'])
83 '@RapsysAir/mail/generic.html.twig',
87 'subtitle' => $subtitle,
88 'home' => $this->get('router')->generate('rapsys_air_homepage', [], UrlGeneratorInterface
::ABSOLUTE_URL
),
89 'subject' => $data['subject'],
90 'contact_name' => $contactName,
91 'message' => strip_tags($data['message'])
98 if ($mailer->send($message)) {
99 //Redirect to cleanup the form
100 return $this->redirectToRoute('rapsys_air_contact', ['sent' => 1]);
106 return $this->render('@RapsysAir/form/contact.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'sent' => $request->query
->get('sent', 0)]);
109 public function indexAction() {
111 $section = $this->translator
->trans('Index');
114 $title = $section.' - '.$this->translator
->trans($this->container
->getParameter('rapsys_air.title'));
117 return $this->render('@RapsysAir/page/index.html.twig', ['title' => $title, 'section' => $section]);
120 public function adminAction(Request
$request) {
121 //Prevent non-admin to access here
122 //TODO: maybe check if user is connected 1st ?
123 $this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');
126 $section = $this->translator
->trans('Admin');
129 $title = $section.' - '.$this->translator
->trans($this->container
->getParameter('rapsys_air.title'));
131 //Create the form according to the FormType created previously.
132 //And give the proper parameters
133 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
134 // To set the action use $this->generateUrl('route_identifier')
135 'action' => $this->generateUrl('rapsys_air_admin'),
137 'attr' => [ 'class' => 'form_col' ]
141 $doctrine = $this->getDoctrine();
144 if ($request->isMethod('POST')) {
145 // Refill the fields in case the form is not valid.
146 $form->handleRequest($request);
148 if ($form->isValid()) {
150 $data = $form->getData();
153 $manager = $doctrine->getManager();
155 //Protect session fetching
157 $session = $doctrine->getRepository(Session
::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']);
158 //Catch no session case
159 } catch (\Doctrine\ORM\NoResultException
$e) {
161 $session = new Session();
162 $session->setLocation($data['location']);
163 $session->setSlot($data['slot']);
164 $session->setDate($data['date']);
165 $session->setCreated(new \
DateTime('now'));
166 $session->setUpdated(new \
DateTime('now'));
167 $manager->persist($session);
168 //Flush to get the ids
173 $application = false;
175 //Protect application fetching
177 //TODO: handle admin case where we provide a user in extra
178 $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $this->getUser());
180 //Add error message to mail field
181 $form->get('slot')->addError(new FormError($this->translator
->trans('Application already exists')));
182 //Catch no application cases
183 //XXX: combine these catch when php 7.1 is available
184 } catch (\Doctrine\ORM\NoResultException
$e) {
185 //Catch invalid argument because session is not already persisted
186 } catch(\Doctrine\ORM\ORMInvalidArgumentException
$e) {
189 //Create new application if none found
191 //Create the application
192 $application = new Application();
193 $application->setSession($session);
194 //TODO: handle admin case where we provide a user in extra
195 $application->setUser($this->getUser());
196 $application->setCreated(new \
DateTime('now'));
197 $application->setUpdated(new \
DateTime('now'));
198 $manager->persist($application);
200 //Flush to get the ids
203 //Add notice in flash message
204 $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')]));
206 //Redirect to cleanup the form
207 return $this->redirectToRoute('rapsys_air_admin');
213 $period = new \
DatePeriod(
214 //Start from first monday of week
215 new \
DateTime('Monday this week'),
216 //Iterate on each day
217 new \
DateInterval('P1D'),
218 //End with next sunday and 4 weeks
219 new \
DateTime('Monday this week + 5 week')
223 $sessions = $doctrine->getRepository(Session
::class)->findByDatePeriod($period);
231 //Iterate on each day
232 foreach($period as $date) {
233 //Init day in calendar
234 $calendar[$Ymd = $date->format('Ymd')] = [
235 'title' => $date->format('d'),
239 //Append month for first day of month
240 if ($month != $date->format('m')) {
241 $month = $date->format('m');
242 $calendar[$Ymd]['title'] .= '/'.$month;
245 if ($date->format('U') == ($today = strtotime('today'))) {
246 $calendar[$Ymd]['title'] .= '/'.$month;
247 $calendar[$Ymd]['current'] = true;
248 $calendar[$Ymd]['class'][] = 'current';
250 //Disable passed days
251 if ($date->format('U') < $today) {
252 $calendar[$Ymd]['disabled'] = true;
253 $calendar[$Ymd]['class'][] = 'disabled';
255 //Set next month days
256 if ($date->format('m') > date('m')) {
257 $calendar[$Ymd]['next'] = true;
258 $calendar[$Ymd]['class'][] = 'next';
260 //Iterate on each session to find the one of the day
261 foreach($sessions as $session) {
262 if (($sessionYmd = $session->getDate()->format('Ymd')) == $Ymd) {
263 //Count number of application
264 $count = count($session->getApplications());
268 if ($session->getApplication()) {
269 $class[] = 'granted';
270 } elseif ($count == 0) {
271 $class[] = 'orphaned';
272 } elseif ($count > 1) {
273 $class[] = 'disputed';
275 $class[] = 'pending';
279 $calendar[$Ymd]['sessions'][$session->getSlot()->getId().$session->getLocation()->getId()] = [
280 'id' => $session->getId(),
281 'title' => ($count > 1?'['.$count.'] ':'').$session->getSlot()->getTitle().' '.$session->getLocation()->getTitle(),
288 ksort($calendar[$Ymd]['sessions']);
291 return $this->render('@RapsysAir/admin/index.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'calendar' => $calendar]);
294 public function sessionAction(Request
$request, $id) {
295 /*header('Content-Type: text/plain');
300 $section = $this->translator
->trans('Session %id%', ['%id%' => $id]);
303 $title = $section.' - '.$this->translator
->trans($this->container
->getParameter('rapsys_air.title'));
305 //Create the form according to the FormType created previously.
306 //And give the proper parameters
307 /*$form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
308 // To set the action use $this->generateUrl('route_identifier')
309 'action' => $this->generateUrl('rapsys_air_admin'),
311 'attr' => [ 'class' => 'form_col' ]
315 $doctrine = $this->getDoctrine();
318 $session = $doctrine->getRepository(Session
::class)->findOneById($id);
320 return $this->render('@RapsysAir/admin/session.html.twig', ['title' => $title, 'section' => $section, /*'form' => $form->createView(),*/ 'session' => $session]);