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