2 /* This file is part of Syj, Copyright (c) 2010-2011 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/form.css', 'all');
12 $this->view->headLink()->appendStylesheet('css/contact.css', 'all');
13 $this->view->headTitle($this->view->translate("contact form"));
16 protected function saveToFile(array $data) {
17 $savePath = Zend_Controller_Front::getInstance()->getParam('emailSavingPath');
21 $saveDir = dirname($savePath);
22 if (is_file($saveDir) and !is_dir($saveDir)) {
23 throw new Zend_Exception();
25 if (!is_dir($saveDir)) {
26 if (@mkdir($saveDir, 0640, true) === false) {
27 throw new Zend_Exception();
30 $handle = @fopen($savePath, 'a');
31 if ($handle === false) {
32 throw new Zend_Exception();
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");
45 protected function sendMail(array $data) {
47 $this->saveToFile($data);
48 } catch(Zend_Exception $e) {
51 $to = Zend_Controller_Front::getInstance()->getParam('webmasterEmail');
52 $subject = "[SYJ] " . $data["contact_subject"];
53 $content = $data["contact_content"];
54 $from = $data["contact_email"];
56 $mail = new Zend_Mail('utf-8');
58 ->setSubject($subject)
59 ->setBodyText($content)
64 } catch(Exception $e) {
70 public function indexAction() {
71 $form = new Syj_Form_Contact(array('name' => 'contactform'));
73 $request = $this->getRequest();
74 $formData = $request->getPost();
76 if (!empty($formData) and $form->isValid($formData)) {
77 if ($this->sendMail($form->getValues())) {
78 $this->_helper->ViewRenderer->setViewScriptPathSpec(':controller/success.:suffix');
81 $this->view->sendError = true;
85 if (empty($formData)) {
86 $user = $this->_helper->SyjUserManager->current();
88 $form->contact_email->setValue($user->email)
89 ->setAttrib('readonly', 'true');
92 $subject = $request->getQuery('subject');
93 if (isset($subject)) {
94 foreach ($form->contact_subject->getValidators() as $key => $validator) {
95 if (!$validator->isValid($subject)) {
101 $form->contact_subject->setValue($subject);
103 $content = $request->getQuery('content');
104 if (isset($content)) {
105 foreach ($form->contact_content->getValidators() as $key => $validator) {
106 if (!$validator->isValid($content)) {
112 $form->contact_content->setValue(isset($content) ? $content : $this->view->translate("Hi,"));
115 $this->_jsLocaleStrings();
116 $this->view->form = $form;
119 protected function _jsLocaleStrings() {
120 $this->view->jslocales = array(
121 'notEmptyField' => __("Value is required"),
122 'invalidMail' => __("Invalid email"));