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