3 namespace Rapsys\AirBundle\Controller
;
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
;
26 class DefaultController
{
41 ///Translator instance
42 protected $translator;
45 * @var ContainerInterface
50 * Inject container and translator interface
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
57 public function __construct(ContainerInterface
$container, RouterInterface
$router, Slugger
$slugger, TranslatorInterface
$translator) {
59 $this->config
= $container->getParameter($this->getAlias());
62 $this->container
= $container;
65 $this->router
= $router;
68 $this->slugger
= $slugger;
71 $this->translator
= $translator;
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'])
89 * @desc Send a contact mail to configured contact
91 * @param Request $request The request instance
92 * @param MailerInterface $mailer The mailer instance
94 * @return Response The rendered view or redirection
96 public function contact(Request
$request, MailerInterface
$mailer) {
98 $section = $this->translator
->trans('Contact');
101 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']);
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'),
110 if ($request->isMethod('POST')) {
111 // Refill the fields in case the form is not valid.
112 $form->handleRequest($request);
114 if ($form->isValid()) {
116 $data = $form->getData();
119 $message = (new TemplatedEmail())
121 ->from(new Address($data['mail'], $data['name']))
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']))
126 ->subject($data['subject'])
128 //Set path to twig templates
129 ->htmlTemplate('@RapsysAir/mail/contact.html.twig')
130 ->textTemplate('@RapsysAir/mail/contact.text.twig')
135 'subject' => $data['subject'],
136 'message' => strip_tags($data['message']),
140 //Try sending message
141 //XXX: mail delivery may silently fail
144 $mailer->send($message);
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)])));
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']])));
162 return $this->render('@RapsysAir/form/contact.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'sent' => $request->query
->get('sent', 0)]+
$this->context
);
168 * @desc Display all granted sessions with an application or login form
170 * @param Request $request The request instance
172 * @return Response The rendered view
174 public function index(Request
$request = null) {
176 $doctrine = $this->getDoctrine();
179 $section = $this->translator
->trans('Index');
182 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']);
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, [
192 'action' => $this->generateUrl('rapsys_air_application_add'),
193 //Set the form attribute
194 'attr' => [ 'class' => 'col' ],
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)
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, [
211 'action' => $this->generateUrl('rapsys_user_login'),
212 //Set the form attribute
213 'attr' => [ 'class' => 'col' ]
216 //Add form to context
217 $context['login'] = $login->createView();
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')
231 $calendar = $doctrine->getRepository(Session
::class)->fetchCalendarByDatePeriod($this->translator
, $period, null, $request->get('session'), !$this->isGranted('IS_AUTHENTICATED_REMEMBERED'));
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')*/);
238 return $this->render('@RapsysAir/default/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+
$context+
$this->context
);
243 * The regulation page
245 * @desc Display the regulation policy
247 * @return Response The rendered view
249 public function regulation() {
251 $section = $this->translator
->trans('Regulation');
254 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']);
257 return $this->render('@RapsysAir/default/regulation.html.twig', ['title' => $title, 'section' => $section]+
$this->context
);
261 * Return the bundle alias
265 public function getAlias() {