1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys BlogBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\BlogBundle\Controller
;
14 use Symfony\Component\HttpFoundation\Request
;
15 use Symfony\Component\HttpFoundation\Response
;
17 use Rapsys\BlogBundle\Entity\Article
;
22 class DefaultController
extends AbstractController
{
26 * @desc Send a contact mail to configured contact
28 * @param Request $request The request instance
29 * @return Response The rendered view or redirection
31 public function contact(Request
$request): Response
{
33 $this->context
['title'] = $this->translator
->trans('Contact');
36 $this->context
['description'] = $this->translator
->trans('Welcome to raphaël\'s developer diary contact page');
39 $this->context
['keywords'] = $this->translator
->trans('contact');
45 if ($user = $this->getUser()) {
48 'name' => $user->getRecipientName(),
49 'mail' => $user->getMail()
53 //Create the form according to the FormType created previously.
54 //And give the proper parameters
55 $form = $this->createForm('Rapsys\BlogBundle\Form\ContactType', $data, [
56 'action' => $this->generateUrl('rapsys_blog_contact'),
60 if ($request->isMethod('POST')) {
61 // Refill the fields in case the form is not valid.
62 $form->handleRequest($request);
64 if ($form->isSubmitted() && $form->isValid()) {
66 $data = $form->getData();
69 $message = (new TemplatedEmail())
71 ->from(new Address($data['mail'], $data['name']))
73 ->to(new Address($this->config
['contact']['mail'], $this->config
['contact']['title']))
75 ->subject($data['subject'])
77 //Set path to twig templates
78 ->htmlTemplate('@RapsysBlog/mail/contact.html.twig')
79 ->textTemplate('@RapsysBlog/mail/contact.text.twig')
84 'subject' => $data['subject'],
85 'message' => strip_tags($data['message']),
90 //XXX: mail delivery may silently fail
93 $this->mailer
->send($message);
95 //Redirect on the same route with sent=1 to cleanup form
96 return $this->redirectToRoute($request->get('_route'), ['sent' => 1]+
$request->get('_route_params'));
97 //Catch obvious transport exception
98 } catch(TransportExceptionInterface
$e) {
99 if ($message = $e->getMessage()) {
100 //Add error message mail unreachable
101 $form->get('mail')->addError(new FormError($this->translator
->trans('Unable to contact: %mail%: %message%', ['%mail%' => $this->config
['contact']['mail'], '%message%' => $this->translator
->trans($message)])));
103 //Add error message mail unreachable
104 $form->get('mail')->addError(new FormError($this->translator
->trans('Unable to contact: %mail%', ['%mail%' => $this->config
['contact']['mail']])));
111 #return $this->render('@RapsysAir/form/contact.html.twig', ['form' => $form->createView(), 'sent' => $request->query->get('sent', 0)]+$this->context);
112 return $this->render('@RapsysBlog/contact.html.twig', $this->context
, $response);
120 * @param Request $request The request instance
121 * @return Response The rendered view
123 public function index(Request
$request): Response
{
125 if ($count = $this->doctrine
->getRepository(Article
::class)->findCountAsInt()) {
126 //Negative page or over page
127 if (($page = (int) $request->get('page', 0)) < 0 || $page > $count / $this->limit
) {
129 throw $this->createNotFoundException($this->translator
->trans('Unable to find articles (page: %page%)', ['%page%' => $page]));
133 if (empty($this->context
['articles'] = $this->doctrine
->getRepository(Article
::class)->findAllAsArray($page, $this->limit
))) {
135 throw $this->createNotFoundException($this->translator
->trans('Unable to find articles'));
141 $this->context
['head']['prev'] = $this->context
['articles_prev'] = $this->generateUrl($request->attributes
->get('_route'), ['page' => $page - 1]+
$request->attributes
->get('_route_params'));
145 if ($count > ($page +
1) * $this->limit
) {
147 $this->context
['head']['next'] = $this->context
['articles_next'] = $this->generateUrl($request->attributes
->get('_route'), ['page' => $page +
1]+
$request->attributes
->get('_route_params'));
151 $this->modified
= max(array_map(function ($v) { return $v
['modified']; }, $this->context
['articles']));
155 $this->context
['articles'] = [];
158 $this->modified
= new \
DateTime('-1 year');
162 $response = new Response();
165 if ($this->checker
->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
167 $response->setLastModified(new \
DateTime('-1 year'));
170 $response->setPrivate();
171 //Without logged user
174 //XXX: only for public to force revalidation by last modified
175 $response->setEtag(md5(serialize($this->context
['articles'])));
178 $response->setLastModified($this->modified
);
181 $response->setPublic();
183 //Without role and modification
184 if ($response->isNotModified($request)) {
185 //Return 304 response
191 $this->context
['head']['keywords'] = implode(
193 //Use closure to extract each unique article keywords sorted
198 //Iterate on articles
201 if (!empty($a['keywords'])) {
202 //Iterate on keywords
203 foreach($a['keywords'] as $k) {
205 $r[$k['title']] = $k['title'];
215 })($this->context
['articles'])
219 $this->context
['title'] = $this->translator
->trans('Home');
222 $this->context
['description'] = $this->translator
->trans('Welcome to raphaël\'s developer diary');
225 return $this->render('@RapsysBlog/index.html.twig', $this->context
, $response);