]> Raphaƫl G. Git Repositories - airbundle/blob - Controller/DefaultController.php
Remove X-Robot: noindex header
[airbundle] / Controller / DefaultController.php
1 <?php
2
3 namespace Rapsys\AirBundle\Controller;
4
5 use Rapsys\AirBundle\Entity\Application;
6 use Rapsys\AirBundle\Entity\Location;
7 use Rapsys\AirBundle\Entity\Session;
8 use Rapsys\AirBundle\Entity\Slot;
9 use Rapsys\AirBundle\Entity\User;
10 use Symfony\Bridge\Twig\Mime\TemplatedEmail;
11 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Symfony\Component\Form\FormError;
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
16 use Symfony\Component\Mailer\MailerInterface;
17 use Symfony\Component\Mime\Address;
18 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
19 use Symfony\Component\Routing\RouterInterface;
20 use Symfony\Component\Translation\TranslatorInterface;
21 use Rapsys\UserBundle\Utils\Slugger;
22
23
24 class DefaultController extends AbstractController {
25 //Config array
26 protected $config;
27
28 //Context array
29 protected $context;
30
31 //Router instance
32 protected $router;
33
34 //Slugger instance
35 protected $slugger;
36
37 //Translator instance
38 protected $translator;
39
40 /**
41 * Inject container and translator interface
42 *
43 * @param ContainerInterface $container The container instance
44 * @param RouterInterface $router The router instance
45 * @param Slugger $slugger The slugger instance
46 * @param TranslatorInterface $translator The translator instance
47 */
48 public function __construct(ContainerInterface $container, RouterInterface $router, Slugger $slugger, TranslatorInterface $translator) {
49 //Retrieve config
50 $this->config = $container->getParameter($this->getAlias());
51
52 //Set the router
53 $this->router = $router;
54
55 //Set the slugger
56 $this->slugger = $slugger;
57
58 //Set the translator
59 $this->translator = $translator;
60
61 //Set the context
62 $this->context = [
63 'copy_long' => $translator->trans($this->config['copy']['long']),
64 'copy_short' => $translator->trans($this->config['copy']['short']),
65 'site_ico' => $this->config['site']['ico'],
66 'site_logo' => $this->config['site']['logo'],
67 'site_png' => $this->config['site']['png'],
68 'site_svg' => $this->config['site']['svg'],
69 'site_title' => $translator->trans($this->config['site']['title']),
70 'site_url' => $router->generate($this->config['site']['url'])
71 ];
72 }
73
74 /**
75 * The contact page
76 *
77 * @desc Send a contact mail to configured contact
78 *
79 * @param Request $request The request instance
80 * @param MailerInterface $mailer The mailer instance
81 *
82 * @return Response The rendered view or redirection
83 */
84 public function contact(Request $request, MailerInterface $mailer) {
85 //Set section
86 $section = $this->translator->trans('Contact');
87
88 //Set title
89 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
90
91 //Create the form according to the FormType created previously.
92 //And give the proper parameters
93 $form = $this->createForm('Rapsys\AirBundle\Form\ContactType', null, [
94 'action' => $this->generateUrl('rapsys_air_contact'),
95 'method' => 'POST'
96 ]);
97
98 if ($request->isMethod('POST')) {
99 // Refill the fields in case the form is not valid.
100 $form->handleRequest($request);
101
102 if ($form->isValid()) {
103 //Get data
104 $data = $form->getData();
105
106 //Create message
107 $message = (new TemplatedEmail())
108 //Set sender
109 ->from(new Address($data['mail'], $data['name']))
110 //Set recipient
111 //XXX: remove the debug set in vendor/symfony/mime/Address.php +46
112 ->to(new Address($this->config['contact']['mail'], $this->config['contact']['name']))
113 //Set subject
114 ->subject($data['subject'])
115
116 //Set path to twig templates
117 ->htmlTemplate('@RapsysAir/mail/contact.html.twig')
118 ->textTemplate('@RapsysAir/mail/contact.text.twig')
119
120 //Set context
121 ->context(
122 [
123 'subject' => $data['subject'],
124 'message' => strip_tags($data['message']),
125 ]+$this->context
126 );
127
128 //Try sending message
129 //XXX: mail delivery may silently fail
130 try {
131 //Send message
132 $mailer->send($message);
133
134 //Redirect on the same route with sent=1 to cleanup form
135 return $this->redirectToRoute($request->get('_route'), ['sent' => 1]+$request->get('_route_params'));
136 //Catch obvious transport exception
137 } catch(TransportExceptionInterface $e) {
138 if ($message = $e->getMessage()) {
139 //Add error message mail unreachable
140 $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%: %message%', ['%mail%' => $this->config['contact']['mail'], '%message%' => $this->translator->trans($message)])));
141 } else {
142 //Add error message mail unreachable
143 $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%', ['%mail%' => $this->config['contact']['mail']])));
144 }
145 }
146 }
147 }
148
149 //Render template
150 return $this->render('@RapsysAir/form/contact.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'sent' => $request->query->get('sent', 0)]+$this->context);
151 }
152
153 /**
154 * The index page
155 *
156 * @desc Display all granted sessions with an application or login form
157 *
158 * @param Request $request The request instance
159 *
160 * @return Response The rendered view
161 */
162 public function index(Request $request = null) {
163 //Fetch doctrine
164 $doctrine = $this->getDoctrine();
165
166 //Set section
167 $section = $this->translator->trans('Index');
168
169 //Set title
170 $title = $section.' - '.$this->context['site_title'];
171
172 //Init context
173 $context = [];
174
175 //Create application form for role_guest
176 if ($this->isGranted('ROLE_GUEST')) {
177 //Create ApplicationType form
178 $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
179 //Set the action
180 'action' => $this->generateUrl('rapsys_air_application_add'),
181 //Set the form attribute
182 'attr' => [ 'class' => 'col' ],
183 //Set admin
184 'admin' => $this->isGranted('ROLE_ADMIN'),
185 //Set default user to current
186 'user' => $this->getUser()->getId(),
187 //Set default slot to evening
188 //XXX: default to Evening (3)
189 'slot' => $doctrine->getRepository(Slot::class)->findOneById(3)
190 ]);
191
192 //Add form to context
193 $context['application'] = $application->createView();
194 //Create login form for anonymous
195 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
196 //Create ApplicationType form
197 $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
198 //Set the action
199 'action' => $this->generateUrl('rapsys_user_login'),
200 //Set the form attribute
201 'attr' => [ 'class' => 'col' ]
202 ]);
203
204 //Add form to context
205 $context['login'] = $login->createView();
206 }
207
208 //Compute period
209 $period = new \DatePeriod(
210 //Start from first monday of week
211 new \DateTime('Monday this week'),
212 //Iterate on each day
213 new \DateInterval('P1D'),
214 //End with next sunday and 4 weeks
215 new \DateTime('Monday this week + 5 week')
216 );
217
218 //Fetch calendar
219 $calendar = $doctrine->getRepository(Session::class)->fetchCalendarByDatePeriod($this->translator, $period, null, $request->get('session'), true);
220
221 //Fetch locations
222 $locations = $doctrine->getRepository(Location::class)->fetchTranslatedLocationByDatePeriod($this->translator, $period, true);
223
224 //Render the view
225 return $this->render('@RapsysAir/default/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$context+$this->context);
226 }
227
228
229 /**
230 * The regulation page
231 *
232 * @desc Display the regulation policy
233 *
234 * @return Response The rendered view
235 */
236 public function regulation() {
237 //Set section
238 $section = $this->translator->trans('Regulation');
239
240 //Set title
241 $title = $section.' - '.$this->context['site_title'];
242
243 //Render template
244 return $this->render('@RapsysAir/default/regulation.html.twig', ['title' => $title, 'section' => $section]+$this->context);
245 }
246
247 /**
248 * Return the bundle alias
249 *
250 * {@inheritdoc}
251 */
252 public function getAlias() {
253 return 'rapsys_air';
254 }
255 }