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->_helper->SyjMedias->addStyleSheets('contact');
11 $this->view->headTitle($this->view->translate("contact form"));
14 protected function saveToFile(array $data) {
15 $savePath = Zend_Controller_Front::getInstance()->getParam('emailSavingPath');
19 $saveDir = dirname($savePath);
20 if (is_file($saveDir) and !is_dir($saveDir)) {
21 throw new Zend_Exception();
23 if (!is_dir($saveDir)) {
24 if (@mkdir($saveDir, 0640, true) === false) {
25 throw new Zend_Exception();
28 $handle = @fopen($savePath, 'a');
29 if ($handle === false) {
30 throw new Zend_Exception();
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");
43 protected function sendMail(array $data) {
45 $this->saveToFile($data);
46 } catch(Zend_Exception $e) {
49 $to = Zend_Controller_Front::getInstance()->getParam('webmasterEmail');
50 $subject = "[SYJ] " . $data["contact_subject"];
51 $content = $data["contact_content"];
52 $from = $data["contact_email"];
54 $mail = new Zend_Mail('utf-8');
56 ->setSubject($subject)
57 ->setBodyText($content)
62 } catch(Exception $e) {
68 public function indexAction() {
69 $form = new Syj_Form_Contact(array('name' => 'contactform'));
71 $request = $this->getRequest();
72 $formData = $request->getPost();
74 if (!empty($formData) and $form->isValid($formData)) {
75 if ($this->sendMail($form->getValues())) {
76 $this->_helper->ViewRenderer->setViewScriptPathSpec(':controller/success.:suffix');
79 $this->view->sendError = true;
83 if (empty($formData)) {
84 $user = $this->_helper->SyjUserManager->current();
86 $form->contact_email->setValue($user->email)
87 ->setAttrib('readonly', 'true');
90 $subject = $request->getQuery('subject');
91 if (isset($subject)) {
92 foreach ($form->contact_subject->getValidators() as $key => $validator) {
93 if (!$validator->isValid($subject)) {
99 $form->contact_subject->setValue($subject);
101 $content = $request->getQuery('content');
102 if (isset($content)) {
103 foreach ($form->contact_content->getValidators() as $key => $validator) {
104 if (!$validator->isValid($content)) {
110 $form->contact_content->setValue(isset($content) ? $content : $this->view->translate("Hi,"));
113 $this->_jsLocaleStrings();
114 $this->view->form = $form;
117 protected function _jsLocaleStrings() {
118 $this->view->jslocales = array(
119 'notEmptyField' => __("Value is required"),
120 'invalidMail' => __("Invalid email"));