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