-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrontController.php
107 lines (92 loc) · 2.96 KB
/
FrontController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class FrontController extends Controller
{
// protected $keys;
//
// public function __construct() {
// $this->keys = ['name', 'city', 'job_title', 'description'];
// }
/**
* @Route("/", name="homepage")
*/
public function indexAction()
{
return $this->renderCache('front/index.html.twig', [
'content' => $this->getContent(),
]);
}
/**
* @Route("/projets", name="projects")
*/
public function projectsAction()
{
$projects = $this->getDoctrine()->getRepository('AppBundle:Project')->findAll();
return $this->renderCache('front/projects.html.twig', [
'content' => $this->getContent(),
'projects' => $projects
]);
}
/**
* @Route("/contact", name="contact")
*/
public function contactAction(Request $request)
{
$success = false;
$form = $this->createForm('AppBundle\Form\ContactType',null,array(
'action' => $this->generateUrl('contact'),
'method' => 'POST'
));
if ($request->isMethod('POST')) {
$form->handleRequest($request);
if($form->isValid()){
$this->sendEmail($form->getData());
$success = true;
}
}
return $this->render('front/contact.html.twig', [
'content' => $this->getContent(),
'success' => $success,
'form' => $form->createView()
]);
}
private function renderCache($template, $output)
{
$response = new Response();
$response->setPublic();
$response->setMaxAge(60);
$response->setSharedMaxAge(60);
$date = new \DateTime();
$date->modify('+ 60 seconds');
$response->setExpires($date);
return $this->render($template, $output, $response);
}
private function getContent()
{
$contents_obj = $this->getDoctrine()->getRepository('AppBundle:Content')->findAll();
$content = [];
foreach($contents_obj as $contents){
$content[$contents->getKey()] = $contents->getValue();
}
return $content;
}
private function sendEmail($data){
$message = \Swift_Message::newInstance()
->setSubject('[txreplay.fr] '. $data['name'] . ' vous a laissé un message')
->setFrom($this->getParameter('mail_from'))
->setTo($this->getParameter('mail_to'))
->setBody(
$this->renderView(
'emails/email_contact.html.twig', [
'data' => $data
]
),
'text/html'
);
$this->get('mailer')->send($message);
}
}