]> Raphaƫl G. Git Repositories - blogbundle/blob - Controller/PageController.php
Fix pre to break word on overflow
[blogbundle] / Controller / PageController.php
1 <?php
2
3 namespace Rapsys\BlogBundle\Controller;
4
5 use Rapsys\BlogBundle\Entity\Article;
6 use Rapsys\BlogBundle\Entity\Keyword;
7 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
8
9 class PageController extends \Symfony\Bundle\FrameworkBundle\Controller\Controller {
10 /// Localized homepage
11 //TODO: rewrite with canonical ?
12 public function indexAction($_locale) {
13 //Fetch articles list
14 $articles = $this->getDoctrine()->getRepository(Article::class)->findArticles($_locale);
15
16 //Set title
17 $title = $this->get('translator')->trans($this->getParameter('blog.welcome'));
18
19 //Render template
20 return $this->render('RapsysBlogBundle::index.html.twig', array('title' => $title, 'articles' => $articles));
21 }
22
23 /// Legal informations
24 public function aboutAction() {
25 //Get translator
26 $trans = $this->get('translator');
27
28 //Set title
29 $title = $trans->trans('About').' - '.$trans->trans($this->getParameter('blog.title'));
30
31 //Render template
32 return $this->render('RapsysBlogBundle::page/about.html.twig', array('title' => $title));
33 }
34
35 /// Contact form
36 public function contactAction($_locale, \Symfony\Component\HttpFoundation\Request $request) {
37 //Get translator
38 $trans = $this->get('translator');
39
40 //Set title
41 $title = $trans->trans('Contact').' - '.$trans->trans($this->getParameter('blog.title'));
42
43 //Create the form according to the FormType created previously.
44 //And give the proper parameters
45 $form = $this->createForm('Rapsys\BlogBundle\Form\ContactType', null, array(
46 // To set the action use $this->generateUrl('route_identifier')
47 'action' => $this->generateUrl('contact'),
48 'method' => 'POST'
49 ));
50
51 if ($request->isMethod('POST')) {
52 // Refill the fields in case the form is not valid.
53 $form->handleRequest($request);
54
55 if ($form->isValid()) {
56 $data = $form->getData();
57 $message = \Swift_Message::newInstance()
58 ->setSubject($data['subject'])
59 ->setFrom(array($data['email'] => $data['name']))
60 ->setTo(array($this->getParameter('blog.contact_mail') => $this->getParameter('blog.contact_name')))
61 ->setBody($data['message'])
62 ->addPart(
63 $this->renderView(
64 'RapsysBlogBundle::mail/contact.html.twig',
65 array(
66 'blog_logo' => file_get_contents($this->getParameter('blog.logo'), false, null),
67 'blog_title' => $this->getParameter('blog.title'),
68 'blog_url' => $this->get('router')->generate('homepage', array('_locale' => $_locale), UrlGeneratorInterface::ABSOLUTE_URL),
69 'subject' => $data['subject'],
70 'contact_name' => $this->getParameter('blog.contact_name'),
71 'message' => $data['message']
72 )
73 ),
74 'text/html'
75 );
76 //Send message
77 if ($this->get('mailer')->send($message)) {
78 //Redirect to cleanup the form
79 return $this->redirectToRoute('contact', array('sent' => 1));
80 }
81 }
82 }
83
84 //Render template
85 return $this->render('RapsysBlogBundle::page/contact.html.twig', array('title' => $title, 'form' => $form->createView(), 'sent' => $request->query->get('sent', 0)));
86 }
87
88 /// Article list
89 public function articleIndexAction($_locale) {
90 //Fetch articles list
91 $articles = $this->getDoctrine()->getRepository(Article::class)->findArticles($_locale);
92
93 //Get translator
94 $trans = $this->get('translator');
95
96 //Set title
97 $title = $trans->trans('Articles list').' - '.$trans->trans($this->getParameter('blog.title'));
98
99 //Render template
100 return $this->render('RapsysBlogBundle::article/index.html.twig', array('title' => $title, 'articles' => $articles));
101 }
102
103 /// Article read
104 public function articleReadAction($_locale, $_article) {
105 //Protect article fetching
106 try {
107 $article = $this->getDoctrine()->getRepository(Article::class)->findArticle($_locale, $_article);
108 //Catch no article case
109 } catch (\Doctrine\ORM\NoResultException $e) {
110 //Throw exception
111 throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException('Not found!');
112 }
113
114 //Set title
115 $title = $article['title'].' - '.$this->get('translator')->trans($this->getParameter('blog.title'));
116
117 //Render template
118 return $this->render('RapsysBlogBundle::article/read.html.twig', array('title' => $title, 'article' => $article));
119 }
120
121 /// Keyword list
122 public function keywordIndexAction($_locale) {
123 //Fetch keywords list
124 $keywords = $this->getDoctrine()->getRepository(Keyword::class)->findKeywords($_locale);
125
126 //Get translator
127 $trans = $this->get('translator');
128
129 //Set title
130 $title = $trans->trans('Keywords list').' - '.$trans->trans($this->getParameter('blog.title'));
131
132 //Render template
133 return $this->render('RapsysBlogBundle::keyword/index.html.twig', array('title' => $title, 'keywords' => $keywords));
134 }
135
136 /// Keyword read
137 function keywordReadAction($_locale, $_keyword) {
138 //Protect keyword fetching
139 try {
140 $keyword = $this->getDoctrine()->getRepository(Keyword::class)->findKeyword($_locale, $_keyword);
141 //Catch no keyword case
142 } catch (\Doctrine\ORM\NoResultException $e) {
143 //Throw exception
144 throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException('Not found!');
145 }
146
147 //Set title
148 $title = $keyword['title'].' - '.$this->get('translator')->trans($this->getParameter('blog.title'));
149
150 //Render template
151 return $this->render('RapsysBlogBundle::keyword/read.html.twig', array('title' => $title, 'keyword' => $keyword));
152 }
153 }