]> Raphaƫl G. Git Repositories - airbundle/blob - Controller/DefaultController.php
Add manifest.json
[airbundle] / Controller / DefaultController.php
1 <?php
2
3 namespace Rapsys\AirBundle\Controller;
4
5 use Symfony\Bundle\FrameworkBundle\Controller\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;
11
12 class DefaultController extends Controller {
13 public function contactAction(Request $request) {
14 //Get translator
15 $trans = $this->get('translator');
16
17 //Set section
18 $section = $trans->trans('Contact');
19
20 //Set title
21 $title = $section.' - '.$trans->trans($this->getParameter('rapsys_air.title'));
22
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'),
28 'method' => 'POST'
29 ]);
30
31 if ($request->isMethod('POST')) {
32 // Refill the fields in case the form is not valid.
33 $form->handleRequest($request);
34
35 if ($form->isValid()) {
36 //Get data
37 $data = $form->getData();
38
39 //Get contact name
40 $contactName = $this->getParameter('rapsys_air.contact_name');
41
42 //Get contact mail
43 $contactMail = $this->getParameter('rapsys_air.contact_mail');
44
45 //Get logo
46 $logo = $this->getParameter('rapsys_air.logo');
47
48 //Get title
49 $title = $trans->trans($this->getParameter('rapsys_air.title'));
50
51 //Get subtitle
52 $subtitle = $trans->trans('Hi,').' '.$contactName;
53
54 $message = \Swift_Message::newInstance()
55 ->setSubject($data['subject'])
56 ->setFrom([$data['mail'] => $data['name']])
57 ->setTo([$contactMail => $contactName])
58 ->setBody($data['message'])
59 ->addPart(
60 $this->renderView(
61 '@RapsysAir/mail/generic.html.twig',
62 [
63 'logo' => $logo,
64 'title' => $title,
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'])
70 ]
71 ),
72 'text/html'
73 );
74 //Send message
75 if ($this->get('mailer')->send($message)) {
76 //Redirect to cleanup the form
77 return $this->redirectToRoute('rapsys_air_contact', ['sent' => 1]);
78 }
79 }
80 }
81
82 //Render template
83 return $this->render('@RapsysAir/form/contact.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'sent' => $request->query->get('sent', 0)]);
84 }
85
86 public function indexAction() {
87 //Get translator
88 $trans = $this->get('translator');
89
90 //Set section
91 $section = $trans->trans('Index');
92
93 //Set title
94 $title = $section.' - '.$trans->trans($this->getParameter('rapsys_air.title'));
95
96 return $this->render('@RapsysAir/page/index.html.twig', ['title' => $title, 'section' => $section]);
97 }
98
99 public function adminAction(Request $request) {
100 $this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');
101
102 //Get translator
103 $trans = $this->get('translator');
104
105 //Set section
106 $section = $trans->trans('Admin');
107
108 //Set title
109 $title = $section.' - '.$trans->trans($this->getParameter('rapsys_air.title'));
110
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'),
116 'method' => 'POST',
117 'attr' => [ 'class' => 'form_col' ]
118 ]);
119
120 //Get doctrine
121 $doctrine = $this->getDoctrine();
122
123 //Handle request
124 if ($request->isMethod('POST')) {
125 // Refill the fields in case the form is not valid.
126 $form->handleRequest($request);
127
128 if ($form->isValid()) {
129 //Get data
130 $data = $form->getData();
131
132 //Get manager
133 $manager = $doctrine->getManager();
134
135 //Protect session fetching
136 try {
137 $session = $doctrine->getRepository(Session::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']);
138 //Catch no session case
139 } catch (\Doctrine\ORM\NoResultException $e) {
140 //Create the session
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
149 #$manager->flush();
150 }
151
152 //Init application
153 $application = false;
154
155 //Protect application fetching
156 try {
157 //TODO: handle admin case where we provide a user in extra
158 $application = $doctrine->getRepository(Application::class)->findOneBySessionUser($session, $this->getUser());
159
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) {
167 }
168
169 //Create new application if none found
170 if (!$application) {
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);
179
180 //Flush to get the ids
181 $manager->flush();
182
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')]));
185
186 //Redirect to cleanup the form
187 return $this->redirectToRoute('rapsys_air_admin');
188 }
189 }
190 }
191
192 //Compute period
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')
200 );
201
202 //Fetch sessions
203 $sessions = $doctrine->getRepository(Session::class)->findByDatePeriod($period);
204
205 //Init calendar
206 $calendar = [];
207
208 //Init month
209 $month = null;
210
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'),
216 'class' => [],
217 'sessions' => []
218 ];
219 //Append month for first day of month
220 if ($month != $date->format('m')) {
221 $month = $date->format('m');
222 $calendar[$Ymd]['title'] .= '/'.$month;
223 }
224 //Deal with today
225 if ($date->format('U') == ($today = strtotime('today'))) {
226 $calendar[$Ymd]['title'] .= '/'.$month;
227 $calendar[$Ymd]['current'] = true;
228 $calendar[$Ymd]['class'][] = 'current';
229 }
230 //Disable passed days
231 if ($date->format('U') < $today) {
232 $calendar[$Ymd]['disabled'] = true;
233 $calendar[$Ymd]['class'][] = 'disabled';
234 }
235 //Set next month days
236 if ($date->format('m') > date('m')) {
237 $calendar[$Ymd]['next'] = true;
238 $calendar[$Ymd]['class'][] = 'next';
239 }
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());
245
246 //Compute classes
247 $class = [];
248 if ($session->getApplication()) {
249 $class[] = 'granted';
250 } elseif ($count == 0) {
251 $class[] = 'orphaned';
252 } elseif ($count > 1) {
253 $class[] = 'disputed';
254 } else {
255 $class[] = 'pending';
256 }
257
258 //Add the session
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(),
262 'class' => $class
263 ];
264 }
265 }
266
267 //Sort sessions
268 ksort($calendar[$Ymd]['sessions']);
269 }
270
271 return $this->render('@RapsysAir/admin/index.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'calendar' => $calendar]);
272 }
273
274 public function sessionAction(Request $request, $id) {
275 /*header('Content-Type: text/plain');
276 var_dump($calendar);
277 exit;*/
278
279 //Get translator
280 $trans = $this->get('translator');
281
282 //Set section
283 $section = $trans->trans('Session %id%', ['%id%' => $id]);
284
285 //Set title
286 $title = $section.' - '.$trans->trans($this->getParameter('rapsys_air.title'));
287
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'),
293 'method' => 'POST',
294 'attr' => [ 'class' => 'form_col' ]
295 ]);*/
296
297 //Get doctrine
298 $doctrine = $this->getDoctrine();
299
300 //Fetch session
301 $session = $doctrine->getRepository(Session::class)->findOneById($id);
302
303 return $this->render('@RapsysAir/admin/session.html.twig', ['title' => $title, 'section' => $section, /*'form' => $form->createView(),*/ 'session' => $session]);
304 }
305 }