1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Contacts_IndexController extends Mage_Core_Controller_Front_Action
35: {
36:
37: const XML_PATH_EMAIL_RECIPIENT = 'contacts/email/recipient_email';
38: const XML_PATH_EMAIL_SENDER = 'contacts/email/sender_email_identity';
39: const XML_PATH_EMAIL_TEMPLATE = 'contacts/email/email_template';
40: const XML_PATH_ENABLED = 'contacts/contacts/enabled';
41:
42: public function preDispatch()
43: {
44: parent::preDispatch();
45:
46: if( !Mage::getStoreConfigFlag(self::XML_PATH_ENABLED) ) {
47: $this->norouteAction();
48: }
49: }
50:
51: public function indexAction()
52: {
53: $this->loadLayout();
54: $this->getLayout()->getBlock('contactForm')
55: ->setFormAction( Mage::getUrl('*/*/post') );
56:
57: $this->_initLayoutMessages('customer/session');
58: $this->_initLayoutMessages('catalog/session');
59: $this->renderLayout();
60: }
61:
62: public function postAction()
63: {
64: $post = $this->getRequest()->getPost();
65: if ( $post ) {
66: $translate = Mage::getSingleton('core/translate');
67:
68: $translate->setTranslateInline(false);
69: try {
70: $postObject = new Varien_Object();
71: $postObject->setData($post);
72:
73: $error = false;
74:
75: if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
76: $error = true;
77: }
78:
79: if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
80: $error = true;
81: }
82:
83: if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
84: $error = true;
85: }
86:
87: if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
88: $error = true;
89: }
90:
91: if ($error) {
92: throw new Exception();
93: }
94: $mailTemplate = Mage::getModel('core/email_template');
95:
96: $mailTemplate->setDesignConfig(array('area' => 'frontend'))
97: ->setReplyTo($post['email'])
98: ->sendTransactional(
99: Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
100: Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
101: Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
102: null,
103: array('data' => $postObject)
104: );
105:
106: if (!$mailTemplate->getSentSuccess()) {
107: throw new Exception();
108: }
109:
110: $translate->setTranslateInline(true);
111:
112: Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
113: $this->_redirect('*/*/');
114:
115: return;
116: } catch (Exception $e) {
117: $translate->setTranslateInline(true);
118:
119: Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
120: $this->_redirect('*/*/');
121: return;
122: }
123:
124: } else {
125: $this->_redirect('*/*/');
126: }
127: }
128:
129: }
130: