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