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