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_Sendfriend_ProductController extends Mage_Core_Controller_Front_Action
35: {
36: 37: 38: 39: 40: 41:
42: public function preDispatch()
43: {
44: parent::preDispatch();
45:
46:
47: $helper = Mage::helper('sendfriend');
48:
49: $session = Mage::getSingleton('customer/session');
50:
51: if (!$helper->isEnabled()) {
52: $this->norouteAction();
53: return $this;
54: }
55:
56: if (!$helper->isAllowForGuest() && !$session->authenticate($this)) {
57: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
58: if ($this->getRequest()->getActionName() == 'sendemail') {
59: $session->setBeforeAuthUrl(Mage::getUrl('*/*/send', array(
60: '_current' => true
61: )));
62: Mage::getSingleton('catalog/session')
63: ->setSendfriendFormData($this->getRequest()->getPost());
64: }
65: }
66:
67: return $this;
68: }
69:
70: 71: 72: 73: 74:
75: protected function _initProduct()
76: {
77: $productId = (int)$this->getRequest()->getParam('id');
78: if (!$productId) {
79: return false;
80: }
81: $product = Mage::getModel('catalog/product')
82: ->load($productId);
83: if (!$product->getId() || !$product->isVisibleInCatalog()) {
84: return false;
85: }
86:
87: Mage::register('product', $product);
88: return $product;
89: }
90:
91: 92: 93: 94: 95:
96: protected function _initSendToFriendModel()
97: {
98: $model = Mage::getModel('sendfriend/sendfriend');
99: $model->setRemoteAddr(Mage::helper('core/http')->getRemoteAddr(true));
100: $model->setCookie(Mage::app()->getCookie());
101: $model->setWebsiteId(Mage::app()->getStore()->getWebsiteId());
102:
103: Mage::register('send_to_friend_model', $model);
104:
105: return $model;
106: }
107:
108: 109: 110: 111:
112: public function sendAction()
113: {
114: $product = $this->_initProduct();
115: $model = $this->_initSendToFriendModel();
116:
117: if (!$product) {
118: $this->_forward('noRoute');
119: return;
120: }
121:
122: if ($model->getMaxSendsToFriend() && $model->isExceedLimit()) {
123: Mage::getSingleton('catalog/session')->addNotice(
124: $this->__('The messages cannot be sent more than %d times in an hour', $model->getMaxSendsToFriend())
125: );
126: }
127:
128: $this->loadLayout();
129: $this->_initLayoutMessages('catalog/session');
130:
131: Mage::dispatchEvent('sendfriend_product', array('product' => $product));
132: $data = Mage::getSingleton('catalog/session')->getSendfriendFormData();
133: if ($data) {
134: Mage::getSingleton('catalog/session')->setSendfriendFormData(true);
135: $block = $this->getLayout()->getBlock('sendfriend.send');
136: if ($block) {
137: $block->setFormData($data);
138: }
139: }
140:
141: $this->renderLayout();
142: }
143:
144: 145: 146: 147:
148: public function sendmailAction()
149: {
150: if (!$this->_validateFormKey()) {
151: return $this->_redirect('*/*/send', array('_current' => true));
152: }
153:
154: $product = $this->_initProduct();
155: $model = $this->_initSendToFriendModel();
156: $data = $this->getRequest()->getPost();
157:
158: if (!$product || !$data) {
159: $this->_forward('noRoute');
160: return;
161: }
162:
163: $categoryId = $this->getRequest()->getParam('cat_id', null);
164: if ($categoryId) {
165: $category = Mage::getModel('catalog/category')
166: ->load($categoryId);
167: $product->setCategory($category);
168: Mage::register('current_category', $category);
169: }
170:
171: $model->setSender($this->getRequest()->getPost('sender'));
172: $model->setRecipients($this->getRequest()->getPost('recipients'));
173: $model->setProduct($product);
174:
175: try {
176: $validate = $model->validate();
177: if ($validate === true) {
178: $model->send();
179: Mage::getSingleton('catalog/session')->addSuccess($this->__('The link to a friend was sent.'));
180: $this->_redirectSuccess($product->getProductUrl());
181: return;
182: }
183: else {
184: if (is_array($validate)) {
185: foreach ($validate as $errorMessage) {
186: Mage::getSingleton('catalog/session')->addError($errorMessage);
187: }
188: }
189: else {
190: Mage::getSingleton('catalog/session')->addError($this->__('There were some problems with the data.'));
191: }
192: }
193: }
194: catch (Mage_Core_Exception $e) {
195: Mage::getSingleton('catalog/session')->addError($e->getMessage());
196: }
197: catch (Exception $e) {
198: Mage::getSingleton('catalog/session')
199: ->addException($e, $this->__('Some emails were not sent.'));
200: }
201:
202:
203: Mage::getSingleton('catalog/session')->setSendfriendFormData($data);
204:
205: $this->_redirectError(Mage::getURL('*/*/send', array('_current' => true)));
206: }
207: }
208: