/
ContactController.php
87 lines (72 loc) · 2.12 KB
/
ContactController.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
<?php
namespace PhlyContact\Controller;
use PhlyContact\Form\ContactForm;
use Zend\Mail\Transport;
use Zend\Mail\Message as Message;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class ContactController extends AbstractActionController
{
protected $form;
protected $message;
protected $transport;
public function setMessage(Message $message)
{
$this->message = $message;
}
public function setMailTransport(Transport\TransportInterface $transport)
{
$this->transport = $transport;
}
public function indexAction()
{
return array(
'form' => $this->form,
);
}
public function processAction()
{
if (!$this->request->isPost()) {
return $this->redirect()->toRoute('contact');
}
$post = $this->request->getPost();
$form = $this->form;
$form->setData($post);
if (!$form->isValid()) {
$model = new ViewModel(array(
'error' => true,
'form' => $form,
));
$model->setTemplate('phly-contact/contact/index');
return $model;
}
// send email...
$this->sendEmail($form->getData());
return $this->redirect()->toRoute('contact/thank-you');
}
public function thankYouAction()
{
$headers = $this->request->getHeaders();
if (!$headers->has('Referer')
|| !preg_match('#/contact$#', $headers->get('Referer')->getFieldValue())
) {
return $this->redirect()->toRoute('contact');
}
return array();
}
public function setContactForm(ContactForm $form)
{
$this->form = $form;
}
protected function sendEmail(array $data)
{
$from = $data['from'];
$subject = '[Contact Form] ' . $data['subject'];
$body = $data['body'];
$this->message->addFrom($from)
->addReplyTo($from)
->setSubject($subject)
->setBody($body);
$this->transport->send($this->message);
}
}