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\Bridge\Twig\Mime\TemplatedEmail
;
15 use Symfony\Component\HttpFoundation\Request
;
16 use Symfony\Component\HttpFoundation\Response
;
17 use Symfony\Component\Mailer\Exception\TransportExceptionInterface
;
18 use Symfony\Component\Mime\Address
;
20 use Rapsys\BlogBundle\Entity\Article
;
25 class DefaultController
extends AbstractController
{
29 * Display the about informations
31 * @param Request $request The request instance
32 * @return Response The rendered view or redirection
34 public function about(Request
$request): Response
{
36 $this->context
['title'] = $this->translator
->trans('About');
39 $this->context
['description'] = $this->translator
->trans('Welcome to raphaël\'s developer diary about page');
42 $this->context
['keywords'] = $this->translator
->trans('about');
45 $response = new Response();
48 //XXX: only for public to force revalidation by last modified
49 $response->setEtag(md5(serialize($this->context
)));
52 $response->setLastModified((new \DateTime
)->setTimestamp(getlastmod()));
55 $response->setPublic();
57 //Without role and modification
58 if ($response->isNotModified($request)) {
64 return $this->render('@RapsysBlog/about.html.twig', $this->context
, $response);
70 * Send a contact mail to configured contact
72 * @param Request $request The request instance
73 * @return Response The rendered view or redirection
75 public function contact(Request
$request): Response
{
77 $this->context
['title'] = $this->translator
->trans('Contact');
80 $this->context
['description'] = $this->translator
->trans('Welcome to raphaël\'s developer diary contact page');
83 $this->context
['keywords'] = $this->translator
->trans('contact');
89 if ($user = $this->security
->getUser()) {
92 'name' => $user->getRecipientName(),
93 'mail' => $user->getMail()
98 $response = new Response();
100 //Create the form according to the FormType created previously.
101 //And give the proper parameters
102 $form = $this->createForm('Rapsys\BlogBundle\Form\ContactType', $data, [
103 'action' => $this->generateUrl('rapsys_blog_contact'),
107 if ($request->isMethod('POST')) {
108 // Refill the fields in case the form is not valid.
109 $form->handleRequest($request);
111 if ($form->isSubmitted() && $form->isValid()) {
113 $data = $form->getData();
117 'subject' => $data['subject'],
118 'message' => strip_tags($data['message']),
122 $message = (new TemplatedEmail())
124 ->from(new Address($data['mail'], $data['name']))
126 ->to(new Address($this->config
['contact']['address'], $this->config
['contact']['name']))
128 ->subject($data['subject'])
130 //Set path to twig templates
131 ->htmlTemplate('@RapsysBlog/mail/contact.html.twig')
132 ->textTemplate('@RapsysBlog/mail/contact.text.twig')
137 //Try sending message
138 //XXX: mail delivery may silently fail
141 $this->mailer
->send($message);
143 //Redirect on the same route with sent=1 to cleanup form
144 return $this->redirectToRoute($request->get('_route'), ['sent' => 1]+
$request->get('_route_params'));
145 //Catch obvious transport exception
146 } catch(TransportExceptionInterface
$e) {
147 //Add error message mail unreachable
148 $form->get('mail')->addError(new FormError($this->translator
->trans('Unable to reach account')));
152 } elseif ($this->checker
->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
154 $response->setLastModified(new \
DateTime('-1 year'));
157 $response->setPrivate();
158 //Without logged user
161 //XXX: only for public to force revalidation by last modified
162 $response->setEtag(md5(serialize($this->context
)));
165 $response->setLastModified((new \DateTime
)->setTimestamp(getlastmod()));
168 $response->setPublic();
170 //Without role and modification
171 if ($response->isNotModified($request)) {
172 //Return 304 response
178 return $this->render('@RapsysBlog/form/contact.html.twig', ['contact' => $form->createView(), 'sent' => $request->query
->get('sent', 0)]+
$this->context
, $response);
186 * @param Request $request The request instance
187 * @return Response The rendered view
189 public function index(Request
$request): Response
{
190 //With not enough articles
191 if (($this->count
= $this->doctrine
->getRepository(Article
::class)->findCountAsInt()) < $this->page
* $this->limit
) {
193 throw $this->createNotFoundException($this->translator
->trans('Unable to find articles'));
197 if ($this->context
['articles'] = $this->doctrine
->getRepository(Article
::class)->findAllAsArray($this->page
, $this->limit
)) {
199 $this->modified
= max(array_map(function ($v) { return $v
['modified']; }, $this->context
['articles']));
203 $this->modified
= new \
DateTime('-1 year');
207 $response = new Response();
210 if ($this->checker
->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
212 $response->setLastModified(new \
DateTime('-1 year'));
215 $response->setPrivate();
216 //Without logged user
219 //XXX: only for public to force revalidation by last modified
220 $response->setEtag(md5(serialize($this->context
['articles'])));
223 $response->setLastModified($this->modified
);
226 $response->setPublic();
228 //Without role and modification
229 if ($response->isNotModified($request)) {
230 //Return 304 response
236 $this->context
['head']['keywords'] = implode(
238 //Use closure to extract each unique article keywords sorted
243 //Iterate on articles
246 if (!empty($a['keywords'])) {
247 //Iterate on keywords
248 foreach($a['keywords'] as $k) {
250 $r[$k['title']] = $k['title'];
260 })($this->context
['articles'])
264 $this->context
['title'] = $this->translator
->trans('Home');
267 $this->context
['description'] = $this->translator
->trans('Welcome to raphaël\'s developer diary');
270 return $this->render('@RapsysBlog/index.html.twig', $this->context
, $response);