X-Git-Url: https://git.rapsys.eu/blogbundle/blobdiff_plain/f02a46d1bf19bd5169721549cd3b92282e1db604..640ccc90e9242323c2ff1fad1afd44120d2c6fd8:/Controller/DefaultController.php?ds=inline diff --git a/Controller/DefaultController.php b/Controller/DefaultController.php index 9df758f..6333b45 100644 --- a/Controller/DefaultController.php +++ b/Controller/DefaultController.php @@ -11,8 +11,11 @@ namespace Rapsys\BlogBundle\Controller; +use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Mailer\Exception\TransportExceptionInterface; +use Symfony\Component\Mime\Address; use Rapsys\BlogBundle\Entity\Article; @@ -20,10 +23,51 @@ use Rapsys\BlogBundle\Entity\Article; * {@inheritdoc} */ class DefaultController extends AbstractController { + /** + * The about page + * + * Display the about informations + * + * @param Request $request The request instance + * @return Response The rendered view or redirection + */ + public function about(Request $request): Response { + //Set page + $this->context['title'] = $this->translator->trans('About'); + + //Set description + $this->context['description'] = $this->translator->trans('Welcome to raphaël\'s developer diary about page'); + + //Set keywords + $this->context['keywords'] = $this->translator->trans('about'); + + //Create response + $response = new Response(); + + //Set etag + //XXX: only for public to force revalidation by last modified + $response->setEtag(md5(serialize($this->context))); + + //Set last modified + $response->setLastModified((new \DateTime)->setTimestamp(getlastmod())); + + //Set as public + $response->setPublic(); + + //Without role and modification + if ($response->isNotModified($request)) { + //Return 304 response + return $response; + } + + //Render template + return $this->render('@RapsysBlog/about.html.twig', $this->context, $response); + } + /** * The contact page * - * @desc Send a contact mail to configured contact + * Send a contact mail to configured contact * * @param Request $request The request instance * @return Response The rendered view or redirection @@ -42,7 +86,7 @@ class DefaultController extends AbstractController { $data = []; //With user - if ($user = $this->getUser()) { + if ($user = $this->security->getUser()) { //Set data $data = [ 'name' => $user->getRecipientName(), @@ -50,6 +94,9 @@ class DefaultController extends AbstractController { ]; } + //Create response + $response = new Response(); + //Create the form according to the FormType created previously. //And give the proper parameters $form = $this->createForm('Rapsys\BlogBundle\Form\ContactType', $data, [ @@ -65,12 +112,18 @@ class DefaultController extends AbstractController { //Get data $data = $form->getData(); + //Set context + $context = [ + 'subject' => $data['subject'], + 'message' => strip_tags($data['message']), + ]+$this->context; + //Create message $message = (new TemplatedEmail()) //Set sender ->from(new Address($data['mail'], $data['name'])) //Set recipient - ->to(new Address($this->config['contact']['mail'], $this->config['contact']['title'])) + ->to(new Address($this->config['contact']['address'], $this->config['contact']['name'])) //Set subject ->subject($data['subject']) @@ -79,12 +132,7 @@ class DefaultController extends AbstractController { ->textTemplate('@RapsysBlog/mail/contact.text.twig') //Set context - ->context( - [ - 'subject' => $data['subject'], - 'message' => strip_tags($data['message']), - ]+$this->context - ); + ->context($context); //Try sending message //XXX: mail delivery may silently fail @@ -96,20 +144,38 @@ class DefaultController extends AbstractController { return $this->redirectToRoute($request->get('_route'), ['sent' => 1]+$request->get('_route_params')); //Catch obvious transport exception } catch(TransportExceptionInterface $e) { - if ($message = $e->getMessage()) { - //Add error message mail unreachable - $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%: %message%', ['%mail%' => $this->config['contact']['mail'], '%message%' => $this->translator->trans($message)]))); - } else { - //Add error message mail unreachable - $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%', ['%mail%' => $this->config['contact']['mail']]))); - } + //Add error message mail unreachable + $form->get('mail')->addError(new FormError($this->translator->trans('Unable to reach account'))); } } + //With logged user + } elseif ($this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) { + //Set last modified + $response->setLastModified(new \DateTime('-1 year')); + + //Set as private + $response->setPrivate(); + //Without logged user + } else { + //Set etag + //XXX: only for public to force revalidation by last modified + $response->setEtag(md5(serialize($this->context))); + + //Set last modified + $response->setLastModified((new \DateTime)->setTimestamp(getlastmod())); + + //Set as public + $response->setPublic(); + + //Without role and modification + if ($response->isNotModified($request)) { + //Return 304 response + return $response; + } } //Render template - #return $this->render('@RapsysAir/form/contact.html.twig', ['form' => $form->createView(), 'sent' => $request->query->get('sent', 0)]+$this->context); - return $this->render('@RapsysBlog/contact.html.twig', $this->context, $response); + return $this->render('@RapsysBlog/form/contact.html.twig', ['contact' => $form->createView(), 'sent' => $request->query->get('sent', 0)]+$this->context, $response); } /** @@ -121,39 +187,18 @@ class DefaultController extends AbstractController { * @return Response The rendered view */ public function index(Request $request): Response { - //With articles - if ($count = $this->doctrine->getRepository(Article::class)->findCountAsInt()) { - //Negative page or over page - if (($page = (int) $request->get('page', 0)) < 0 || $page > $count / $this->limit) { - //Throw 404 - throw $this->createNotFoundException($this->translator->trans('Unable to find articles (page: %page%)', ['%page%' => $page])); - } - - //Without articles - if (empty($this->context['articles'] = $this->doctrine->getRepository(Article::class)->findAllAsArray($page, $this->limit))) { - //Throw 404 - throw $this->createNotFoundException($this->translator->trans('Unable to find articles')); - } - - //With prev link - if ($page > 0) { - //Set articles older - $this->context['head']['prev'] = $this->context['articles_prev'] = $this->generateUrl($request->attributes->get('_route'), ['page' => $page - 1]+$request->attributes->get('_route_params')); - } - - //With next link - if ($count > ($page + 1) * $this->limit) { - //Set articles newer - $this->context['head']['next'] = $this->context['articles_next'] = $this->generateUrl($request->attributes->get('_route'), ['page' => $page + 1]+$request->attributes->get('_route_params')); - } + //With not enough articles + if (($this->count = $this->doctrine->getRepository(Article::class)->findCountAsInt()) < $this->page * $this->limit) { + //Throw 404 + throw $this->createNotFoundException($this->translator->trans('Unable to find articles')); + } + //Get articles + if ($this->context['articles'] = $this->doctrine->getRepository(Article::class)->findAllAsArray($this->page, $this->limit)) { //Set modified $this->modified = max(array_map(function ($v) { return $v['modified']; }, $this->context['articles'])); - //Without articles + //Without keywords } else { - //Set empty articles - $this->context['articles'] = []; - //Set empty modified $this->modified = new \DateTime('-1 year'); }