]> dev.renevier.net Git - syj.git/blob - application/controllers/ContactController.php
highlight for input on errors
[syj.git] / application / controllers / ContactController.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
3     and is published under the AGPL license. */
4
5 class ContactController extends Zend_Controller_Action
6 {
7
8     public function init() {
9         $this->view->headScript()->appendFile('js/prototype.js');
10         $this->view->headScript()->appendFile('js/forms.js');
11         $this->view->headScript()->appendFile('js/highlight.js');
12         $this->view->headScript()->appendFile('js/contact.js');
13         $this->view->headLink()->appendStylesheet('css/generic.css');
14         $this->view->headLink()->appendStylesheet('css/contact.css');
15         $this->view->headTitle($this->view->translate("contact form"));
16     }
17
18     protected function saveToFile(array $data) {
19         $savePath = Zend_Controller_Front::getInstance()->getParam('emailSavingPath');
20         if (!$savePath) {
21             return;
22         }
23         $saveDir = dirname($savePath);
24         if (is_file($saveDir) and !is_dir($saveDir)) {
25             throw new Zend_Exception();
26         }
27         if (!is_dir($saveDir)) {
28             if (@mkdir($saveDir, 0640, true) === false) {
29                 throw new Zend_Exception();
30             }
31         }
32         $handle = @fopen($savePath, 'a');
33         if ($handle === false) {
34             throw new Zend_Exception();
35         }
36         fwrite($handle, "------\n");
37         fwrite($handle, "mail sent by " . $data['contact_email'] . "\n");
38         fwrite($handle, "on " . date('r') . "\n");
39         fwrite($handle, "subject: " . $data['contact_subject'] . "\n");
40         fwrite($handle, "\n");
41         fwrite($handle, $data['contact_content']);
42         fwrite($handle, "\n");
43         fwrite($handle, "\n");
44         fclose($handle);
45     }
46
47     protected function sendMail(array $data) {
48         try {
49             $this->saveToFile($data);
50         } catch(Zend_Exception $e) {
51             return false;
52         }
53         $to = Zend_Controller_Front::getInstance()->getParam('webmasterEmail');
54         $subject = "[SYJ] " . $data["contact_subject"];
55         $content = $data["contact_content"];
56         $from = $data["contact_email"];
57
58         $mail = new Zend_Mail('utf-8');
59         $mail->addTo($to)
60              ->setSubject($subject)
61              ->setBodyText($content)
62              ->setFrom($from);
63
64         try {
65             $mail->send();
66         } catch(Exception $e) {
67             return false;
68         }
69         return true;
70     }
71
72     public function indexAction() {
73         $form = new Syj_Form_Contact(array('name' => 'contactform'));
74
75         $request = $this->getRequest();
76         $formData = $request->getPost();
77
78         if (!empty($formData) and $form->isValid($formData)) {
79             if ($this->sendMail($form->getValues())) {
80                 $this->_helper->ViewRenderer->setViewScriptPathSpec(':controller/success.:suffix');
81                 return;
82             } else {
83                 $this->view->sendError = true;
84             }
85         }
86
87         if (empty($formData)) {
88             $user = $this->view->loggedUser();
89             if ($user) {
90                 $form->contact_email->setValue($user->email)
91                                     ->setAttrib('readonly', 'true');
92             }
93
94             $subject = $request->getQuery('subject');
95             if (isset($subject)) {
96                 foreach ($form->contact_subject->getValidators() as $key => $validator) {
97                     if (!$validator->isValid($subject)) {
98                         $subject = null;
99                         break;
100                     }
101                 }
102             }
103             $form->contact_subject->setValue($subject);
104
105             $content = $request->getQuery('content');
106             if (isset($content)) {
107                 foreach ($form->contact_content->getValidators() as $key => $validator) {
108                     if (!$validator->isValid($content)) {
109                         $content = null;
110                         break;
111                     }
112                 }
113             }
114             $form->contact_content->setValue(isset($content) ? $content : $this->view->translate("Hi,"));
115         }
116
117         $this->_jsLocaleStrings();
118         $this->view->form = $form;
119     }
120
121     protected function _jsLocaleStrings() {
122         $this->view->jslocales = array(
123             'notEmptyField' => __("Value is required and can't be empty"),
124             'invalidMail' => __("Invalid email"));
125     }
126 }