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/utils.js');
11 $this->view->headScript()->appendFile('js/contact.js');
12 $this->view->headLink()->appendStylesheet('css/generic.css');
13 $this->view->headLink()->appendStylesheet('css/contact.css');
14 $this->view->headTitle($this->view->translate("contact form"));
17 protected function saveToFile(array $data) {
18 $savePath = Zend_Controller_Front::getInstance()->getParam('emailSavingPath');
22 $saveDir = dirname($savePath);
23 if (is_file($saveDir) and !is_dir($saveDir)) {
24 throw new Zend_Exception();
26 if (!is_dir($saveDir)) {
27 if (@mkdir($saveDir, 0640, true) === false) {
28 throw new Zend_Exception();
31 $handle = @fopen($savePath, 'a');
32 if ($handle === false) {
33 throw new Zend_Exception();
35 fwrite($handle, "------\n");
36 fwrite($handle, "mail sent by " . $data['contact_email'] . "\n");
37 fwrite($handle, "on " . date('r') . "\n");
38 fwrite($handle, "subject: " . $data['contact_subject'] . "\n");
39 fwrite($handle, "\n");
40 fwrite($handle, $data['contact_content']);
41 fwrite($handle, "\n");
42 fwrite($handle, "\n");
46 protected function sendMail(array $data) {
48 $this->saveToFile($data);
49 } catch(Zend_Exception $e) {
52 $to = Zend_Controller_Front::getInstance()->getParam('webmasterEmail');
53 $subject = "[SYJ] " . $data["contact_subject"];
54 $content = $data["contact_content"];
55 $from = $data["contact_email"];
57 $mail = new Zend_Mail('utf-8');
59 ->setSubject($subject)
60 ->setBodyText($content)
65 } catch(Exception $e) {
71 public function indexAction() {
72 $form = new Syj_Form_Contact(array('name' => 'contactform'));
74 $request = $this->getRequest();
75 $formData = $request->getPost();
77 if (!empty($formData) and $form->isValid($formData)) {
78 if ($this->sendMail($form->getValues())) {
79 $this->_helper->ViewRenderer->setViewScriptPathSpec(':controller/success.:suffix');
82 $this->view->sendError = true;
86 if (empty($formData)) {
87 $user = $this->_helper->SyjSession->user();
89 $form->contact_email->setValue($user->email)
90 ->setAttrib('readonly', 'true');
93 $subject = $request->getQuery('subject');
94 if (isset($subject)) {
95 foreach ($form->contact_subject->getValidators() as $key => $validator) {
96 if (!$validator->isValid($subject)) {
102 $form->contact_subject->setValue($subject);
104 $content = $request->getQuery('content');
105 if (isset($content)) {
106 foreach ($form->contact_content->getValidators() as $key => $validator) {
107 if (!$validator->isValid($content)) {
113 $form->contact_content->setValue(isset($content) ? $content : $this->view->translate("Hi,"));
116 $this->_jsLocaleStrings();
117 $this->view->form = $form;
120 protected function _jsLocaleStrings() {
121 $this->view->jslocales = array(
122 'notEmptyField' => __("Value is required and can't be empty"),
123 'invalidMail' => __("Invalid email"));