PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Sendfriend/controllers/ProductController.php

https://bitbucket.org/acidel/buykoala
PHP | 207 lines | 125 code | 24 blank | 58 comment | 21 complexity | dbbc9b76bb5c547b4726190a52c81cf4 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Sendfriend
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Email to a Friend Product Controller
  28. *
  29. * @category Mage
  30. * @package Mage_Sedfriend
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Sendfriend_ProductController extends Mage_Core_Controller_Front_Action
  34. {
  35. /**
  36. * Predispatch: check is enable module
  37. * If allow only for customer - redirect to login page
  38. *
  39. * @return Mage_Sendfriend_ProductController
  40. */
  41. public function preDispatch()
  42. {
  43. parent::preDispatch();
  44. /* @var $helper Mage_Sendfriend_Helper_Data */
  45. $helper = Mage::helper('sendfriend');
  46. /* @var $session Mage_Customer_Model_Session */
  47. $session = Mage::getSingleton('customer/session');
  48. if (!$helper->isEnabled()) {
  49. $this->norouteAction();
  50. return $this;
  51. }
  52. if (!$helper->isAllowForGuest() && !$session->authenticate($this)) {
  53. $this->setFlag('', self::FLAG_NO_DISPATCH, true);
  54. if ($this->getRequest()->getActionName() == 'sendemail') {
  55. $session->setBeforeAuthUrl(Mage::getUrl('*/*/send', array(
  56. '_current' => true
  57. )));
  58. Mage::getSingleton('catalog/session')
  59. ->setSendfriendFormData($this->getRequest()->getPost());
  60. }
  61. }
  62. return $this;
  63. }
  64. /**
  65. * Initialize Product Instance
  66. *
  67. * @return Mage_Catalog_Model_Product
  68. */
  69. protected function _initProduct()
  70. {
  71. $productId = (int)$this->getRequest()->getParam('id');
  72. if (!$productId) {
  73. return false;
  74. }
  75. $product = Mage::getModel('catalog/product')
  76. ->load($productId);
  77. if (!$product->getId() || !$product->isVisibleInCatalog()) {
  78. return false;
  79. }
  80. Mage::register('product', $product);
  81. return $product;
  82. }
  83. /**
  84. * Initialize send friend model
  85. *
  86. * @return Mage_Sendfriend_Model_Sendfriend
  87. */
  88. protected function _initSendToFriendModel()
  89. {
  90. $model = Mage::getModel('sendfriend/sendfriend');
  91. $model->setRemoteAddr(Mage::helper('core/http')->getRemoteAddr(true));
  92. $model->setCookie(Mage::app()->getCookie());
  93. $model->setWebsiteId(Mage::app()->getStore()->getWebsiteId());
  94. Mage::register('send_to_friend_model', $model);
  95. return $model;
  96. }
  97. /**
  98. * Show Send to a Friend Form
  99. *
  100. */
  101. public function sendAction()
  102. {
  103. $product = $this->_initProduct();
  104. $model = $this->_initSendToFriendModel();
  105. if (!$product) {
  106. $this->_forward('noRoute');
  107. return;
  108. }
  109. if ($model->getMaxSendsToFriend() && $model->isExceedLimit()) {
  110. Mage::getSingleton('catalog/session')->addNotice(
  111. $this->__('The messages cannot be sent more than %d times in an hour', $model->getMaxSendsToFriend())
  112. );
  113. }
  114. $this->loadLayout();
  115. $this->_initLayoutMessages('catalog/session');
  116. Mage::dispatchEvent('sendfriend_product', array('product' => $product));
  117. $data = Mage::getSingleton('catalog/session')->getSendfriendFormData();
  118. if ($data) {
  119. Mage::getSingleton('catalog/session')->setSendfriendFormData(true);
  120. $block = $this->getLayout()->getBlock('sendfriend.send');
  121. if ($block) {
  122. $block->setFormData($data);
  123. }
  124. }
  125. $this->renderLayout();
  126. }
  127. /**
  128. * Send Email Post Action
  129. *
  130. */
  131. public function sendmailAction()
  132. {
  133. if (!$this->_validateFormKey()) {
  134. return $this->_redirect('*/*/send', array('_current' => true));
  135. }
  136. $product = $this->_initProduct();
  137. $model = $this->_initSendToFriendModel();
  138. $data = $this->getRequest()->getPost();
  139. if (!$product || !$data) {
  140. $this->_forward('noRoute');
  141. return;
  142. }
  143. $categoryId = $this->getRequest()->getParam('cat_id', null);
  144. if ($categoryId) {
  145. $category = Mage::getModel('catalog/category')
  146. ->load($categoryId);
  147. $product->setCategory($category);
  148. Mage::register('current_category', $category);
  149. }
  150. $model->setSender($this->getRequest()->getPost('sender'));
  151. $model->setRecipients($this->getRequest()->getPost('recipients'));
  152. $model->setProduct($product);
  153. try {
  154. $validate = $model->validate();
  155. if ($validate === true) {
  156. $model->send();
  157. Mage::getSingleton('catalog/session')->addSuccess($this->__('The link to a friend was sent.'));
  158. $this->_redirectSuccess($product->getProductUrl());
  159. return;
  160. }
  161. else {
  162. if (is_array($validate)) {
  163. foreach ($validate as $errorMessage) {
  164. Mage::getSingleton('catalog/session')->addError($errorMessage);
  165. }
  166. }
  167. else {
  168. Mage::getSingleton('catalog/session')->addError($this->__('There were some problems with the data.'));
  169. }
  170. }
  171. }
  172. catch (Mage_Core_Exception $e) {
  173. Mage::getSingleton('catalog/session')->addError($e->getMessage());
  174. }
  175. catch (Exception $e) {
  176. Mage::getSingleton('catalog/session')
  177. ->addException($e, $this->__('Some emails were not sent.'));
  178. }
  179. // save form data
  180. Mage::getSingleton('catalog/session')->setSendfriendFormData($data);
  181. $this->_redirectError(Mage::getURL('*/*/send', array('_current' => true)));
  182. }
  183. }