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->_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"));
15 protected function saveToFile(array $data) {
16 $savePath = Zend_Controller_Front::getInstance()->getParam('emailSavingPath');
20 $saveDir = dirname($savePath);
21 if (is_file($saveDir) and !is_dir($saveDir)) {
22 throw new Zend_Exception();
24 if (!is_dir($saveDir)) {
25 if (@mkdir($saveDir, 0640, true) === false) {
26 throw new Zend_Exception();
29 $handle = @fopen($savePath, 'a');
30 if ($handle === false) {
31 throw new Zend_Exception();
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");
44 protected function sendMail(array $data) {
46 $this->saveToFile($data);
47 } catch(Zend_Exception $e) {
50 $to = Zend_Controller_Front::getInstance()->getParam('webmasterEmail');
51 $subject = "[SYJ] " . $data["contact_subject"];
52 $content = $data["contact_content"];
53 $from = $data["contact_email"];
55 $mail = new Zend_Mail('utf-8');
57 ->setSubject($subject)
58 ->setBodyText($content)
63 } catch(Exception $e) {
69 public function indexAction() {
70 $form = new Syj_Form_Contact(array('name' => 'contactform'));
72 $request = $this->getRequest();
73 $formData = $request->getPost();
75 if (!empty($formData) and $form->isValid($formData)) {
76 if ($this->sendMail($form->getValues())) {
77 $this->_helper->ViewRenderer->setViewScriptPathSpec(':controller/success.:suffix');
80 $this->view->sendError = true;
84 if (empty($formData)) {
85 $user = $this->_helper->SyjSession->user();
87 $form->contact_email->setValue($user->email)
88 ->setAttrib('readonly', 'true');
91 $subject = $request->getQuery('subject');
92 if (isset($subject)) {
93 foreach ($form->contact_subject->getValidators() as $key => $validator) {
94 if (!$validator->isValid($subject)) {
100 $form->contact_subject->setValue($subject);
102 $content = $request->getQuery('content');
103 if (isset($content)) {
104 foreach ($form->contact_content->getValidators() as $key => $validator) {
105 if (!$validator->isValid($content)) {
111 $form->contact_content->setValue(isset($content) ? $content : $this->view->translate("Hi,"));
114 $this->_jsLocaleStrings();
115 $this->view->form = $form;
118 protected function _jsLocaleStrings() {
119 $this->view->jslocales = array(
120 'notEmptyField' => __("Value is required"),
121 'invalidMail' => __("Invalid email"));