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 AbstractController
{
19 protected $translator;
21 public function __construct(ContainerInterface
$container, Translator
$translator) {
23 $this->config
= $container->getParameter($this->getAlias());
26 $this->translator
= $translator;
29 public function contactAction(Request
$request) {
31 $section = $this->translator
->trans('Contact');
34 $title = $section.' - '.$this->translator
->trans($this->config
['title']);
36 //Create the form according to the FormType created previously.
37 //And give the proper parameters
38 $form = $this->createForm('Rapsys\AirBundle\Form\ContactType', null, [
39 // To set the action use $this->generateUrl('route_identifier')
40 'action' => $this->generateUrl('rapsys_air_contact'),
44 if ($request->isMethod('POST')) {
45 // Refill the fields in case the form is not valid.
46 $form->handleRequest($request);
48 if ($form->isValid()) {
50 $data = $form->getData();
53 $contactName = $this->config
['contact_name'];
56 $contactMail = $this->config
['contact_mail'];
59 $logo = $this->config
['logo'];
62 $title = $this->translator
->trans($this->config
['title']);
65 $subtitle = $this->translator
->trans('Hi,').' '.$contactName;
67 //Create sendmail transport
68 $transport = new \
Swift_SendmailTransport();
70 //Create mailer using transport
71 $mailer = new \
Swift_Mailer($transport);
74 ($message = new \
Swift_Message($data['subject']))
75 #->setSubject($data['subject'])
76 ->setFrom([$data['mail'] => $data['name']])
77 ->setTo([$contactMail => $contactName])
78 ->setBody($data['message'])
81 '@RapsysAir/mail/generic.html.twig',
85 'subtitle' => $subtitle,
86 'home' => $this->get('router')->generate('rapsys_air_homepage', [], UrlGeneratorInterface
::ABSOLUTE_URL
),
87 'subject' => $data['subject'],
88 'contact_name' => $contactName,
89 'message' => strip_tags($data['message'])
96 if ($mailer->send($message)) {
97 //Redirect to cleanup the form
98 return $this->redirectToRoute('rapsys_air_contact', ['sent' => 1]);
104 return $this->render('@RapsysAir/form/contact.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'sent' => $request->query
->get('sent', 0)]);
107 public function indexAction() {
109 $section = $this->translator
->trans('Index');
112 $title = $section.' - '.$this->translator
->trans($this->config
['title']);
115 return $this->render('@RapsysAir/page/index.html.twig', ['title' => $title, 'section' => $section]);
118 public function adminAction(Request
$request) {
119 //Prevent non-admin to access here
120 //TODO: maybe check if user is connected 1st ?
121 $this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');
124 $section = $this->translator
->trans('Admin');
127 $title = $section.' - '.$this->translator
->trans($this->config
['title']);
129 //Create the form according to the FormType created previously.
130 //And give the proper parameters
131 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
132 // To set the action use $this->generateUrl('route_identifier')
133 'action' => $this->generateUrl('rapsys_air_admin'),
135 'attr' => [ 'class' => 'form_col' ]
139 $doctrine = $this->getDoctrine();
142 if ($request->isMethod('POST')) {
143 // Refill the fields in case the form is not valid.
144 $form->handleRequest($request);
146 if ($form->isValid()) {
148 $data = $form->getData();
151 $manager = $doctrine->getManager();
153 //Protect session fetching
155 $session = $doctrine->getRepository(Session
::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']);
156 //Catch no session case
157 } catch (\Doctrine\ORM\NoResultException
$e) {
159 $session = new Session();
160 $session->setLocation($data['location']);
161 $session->setSlot($data['slot']);
162 $session->setDate($data['date']);
163 $session->setCreated(new \
DateTime('now'));
164 $session->setUpdated(new \
DateTime('now'));
165 $manager->persist($session);
166 //Flush to get the ids
171 $application = false;
173 //Protect application fetching
175 //TODO: handle admin case where we provide a user in extra
176 $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $this->getUser());
178 //Add error message to mail field
179 $form->get('slot')->addError(new FormError($this->translator
->trans('Application already exists')));
180 //Catch no application cases
181 //XXX: combine these catch when php 7.1 is available
182 } catch (\Doctrine\ORM\NoResultException
$e) {
183 //Catch invalid argument because session is not already persisted
184 } catch(\Doctrine\ORM\ORMInvalidArgumentException
$e) {
187 //Create new application if none found
189 //Create the application
190 $application = new Application();
191 $application->setSession($session);
192 //TODO: handle admin case where we provide a user in extra
193 $application->setUser($this->getUser());
194 $application->setCreated(new \
DateTime('now'));
195 $application->setUpdated(new \
DateTime('now'));
196 $manager->persist($application);
198 //Flush to get the ids
201 //Add notice in flash message
202 $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')]));
204 //Redirect to cleanup the form
205 return $this->redirectToRoute('rapsys_air_admin');
211 $period = new \
DatePeriod(
212 //Start from first monday of week
213 new \
DateTime('Monday this week'),
214 //Iterate on each day
215 new \
DateInterval('P1D'),
216 //End with next sunday and 4 weeks
217 new \
DateTime('Monday this week + 5 week')
221 $sessions = $doctrine->getRepository(Session
::class)->findByDatePeriod($period);
229 //Iterate on each day
230 foreach($period as $date) {
231 //Init day in calendar
232 $calendar[$Ymd = $date->format('Ymd')] = [
233 'title' => $date->format('d'),
237 //Append month for first day of month
238 if ($month != $date->format('m')) {
239 $month = $date->format('m');
240 $calendar[$Ymd]['title'] .= '/'.$month;
243 if ($date->format('U') == ($today = strtotime('today'))) {
244 $calendar[$Ymd]['title'] .= '/'.$month;
245 $calendar[$Ymd]['current'] = true;
246 $calendar[$Ymd]['class'][] = 'current';
248 //Disable passed days
249 if ($date->format('U') < $today) {
250 $calendar[$Ymd]['disabled'] = true;
251 $calendar[$Ymd]['class'][] = 'disabled';
253 //Set next month days
254 if ($date->format('m') > date('m')) {
255 $calendar[$Ymd]['next'] = true;
256 $calendar[$Ymd]['class'][] = 'next';
258 //Iterate on each session to find the one of the day
259 foreach($sessions as $session) {
260 if (($sessionYmd = $session->getDate()->format('Ymd')) == $Ymd) {
261 //Count number of application
262 $count = count($session->getApplications());
266 if ($session->getApplication()) {
267 $class[] = 'granted';
268 } elseif ($count == 0) {
269 $class[] = 'orphaned';
270 } elseif ($count > 1) {
271 $class[] = 'disputed';
273 $class[] = 'pending';
277 $calendar[$Ymd]['sessions'][$session->getSlot()->getId().$session->getLocation()->getId()] = [
278 'id' => $session->getId(),
279 'title' => ($count > 1?'['.$count.'] ':'').$session->getSlot()->getTitle().' '.$session->getLocation()->getTitle(),
286 ksort($calendar[$Ymd]['sessions']);
289 return $this->render('@RapsysAir/admin/index.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'calendar' => $calendar]);
292 public function sessionAction(Request
$request, $id) {
293 /*header('Content-Type: text/plain');
298 $section = $this->translator
->trans('Session %id%', ['%id%' => $id]);
301 $title = $section.' - '.$this->translator
->trans($this->config
['title']);
303 //Create the form according to the FormType created previously.
304 //And give the proper parameters
305 /*$form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
306 // To set the action use $this->generateUrl('route_identifier')
307 'action' => $this->generateUrl('rapsys_air_admin'),
309 'attr' => [ 'class' => 'form_col' ]
313 $doctrine = $this->getDoctrine();
316 $session = $doctrine->getRepository(Session
::class)->findOneById($id);
318 return $this->render('@RapsysAir/admin/session.html.twig', ['title' => $title, 'section' => $section, /*'form' => $form->createView(),*/ 'session' => $session]);
324 public function getAlias() {