2 /* This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
3 and is published under the AGPL license. */
5 class ContactController extends Zend_Controller_Action
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"));
18 protected function saveToFile(array $data) {
19 $savePath = Zend_Controller_Front::getInstance()->getParam('emailSavingPath');
23 $saveDir = dirname($savePath);
24 if (is_file($saveDir) and !is_dir($saveDir)) {
25 throw new Zend_Exception();
27 if (!is_dir($saveDir)) {
28 if (@mkdir($saveDir, 0640, true) === false) {
29 throw new Zend_Exception();
32 $handle = @fopen($savePath, 'a');
33 if ($handle === false) {
34 throw new Zend_Exception();
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");
47 protected function sendMail(array $data) {
49 $this->saveToFile($data);
50 } catch(Zend_Exception $e) {
53 $to = Zend_Controller_Front::getInstance()->getParam('webmasterEmail');
54 $subject = "[SYJ] " . $data["contact_subject"];
55 $content = $data["contact_content"];
56 $from = $data["contact_email"];
58 $mail = new Zend_Mail('utf-8');
60 ->setSubject($subject)
61 ->setBodyText($content)
66 } catch(Exception $e) {
72 public function indexAction() {
73 $form = new Syj_Form_Contact(array('name' => 'contactform'));
75 $request = $this->getRequest();
76 $formData = $request->getPost();
78 if (!empty($formData) and $form->isValid($formData)) {
79 if ($this->sendMail($form->getValues())) {
80 $this->_helper->ViewRenderer->setViewScriptPathSpec(':controller/success.:suffix');
83 $this->view->sendError = true;
87 if (empty($formData)) {
88 $user = $this->view->loggedUser();
90 $form->contact_email->setValue($user->email)
91 ->setAttrib('readonly', 'true');
94 $subject = $request->getQuery('subject');
95 if (isset($subject)) {
96 foreach ($form->contact_subject->getValidators() as $key => $validator) {
97 if (!$validator->isValid($subject)) {
103 $form->contact_subject->setValue($subject);
105 $content = $request->getQuery('content');
106 if (isset($content)) {
107 foreach ($form->contact_content->getValidators() as $key => $validator) {
108 if (!$validator->isValid($content)) {
114 $form->contact_content->setValue(isset($content) ? $content : $this->view->translate("Hi,"));
117 $this->_jsLocaleStrings();
118 $this->view->form = $form;
121 protected function _jsLocaleStrings() {
122 $this->view->jslocales = array(
123 'notEmptyField' => __("Value is required and can't be empty"),
124 'invalidMail' => __("Invalid email"));