00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 class Mage_Sendfriend_ProductController extends Mage_Core_Controller_Front_Action
00028 {
00029
00030
00031
00032
00033
00034 protected function _initProduct()
00035 {
00036 $productId = (int) $this->getRequest()->getParam('id');
00037 if (!$productId) {
00038 return false;
00039 }
00040 $product = Mage::getModel('catalog/product')
00041 ->load($productId);
00042 if (!$product->getId()) {
00043 return false;
00044 }
00045 Mage::register('product', $product);
00046 return $product;
00047 }
00048
00049
00050
00051
00052
00053
00054 protected function _initSendToFriendModel(){
00055 $sendToFriendModel = Mage::getModel('sendfriend/sendfriend');
00056 Mage::register('send_to_friend_model', $sendToFriendModel);
00057 return $sendToFriendModel;
00058 }
00059
00060 public function sendAction(){
00061 $product = $this->_initProduct();
00062 $this->_initSendToFriendModel();
00063
00064 if (!$product || !$product->isVisibleInCatalog()) {
00065 $this->_forward('noRoute');
00066 return;
00067 }
00068
00069 $productHelper = Mage::helper('catalog/product');
00070 $sendToFriendModel = Mage::registry('send_to_friend_model');
00071
00072
00073
00074
00075 if (!$sendToFriendModel->canEmailToFriend()) {
00076 Mage::getSingleton('catalog/session')->addError(
00077 $this->__('You cannot email this product to a friend')
00078 );
00079 $this->_redirectReferer($product->getProductUrl());
00080 return;
00081 }
00082
00083 $maxSendsToFriend = $sendToFriendModel->getMaxSendsToFriend();
00084 if ($maxSendsToFriend){
00085 Mage::getSingleton('catalog/session')->addNotice(
00086 $this->__('You cannot send more than %d times in an hour', $maxSendsToFriend)
00087 );
00088 }
00089
00090 $this->loadLayout();
00091 $this->_initLayoutMessages('catalog/session');
00092 $this->renderLayout();
00093 Mage::dispatchEvent('sendfriend_product', array('product'=>$product));
00094 }
00095
00096 public function sendmailAction()
00097 {
00098 $product = $this->_initProduct();
00099 $sendToFriendModel = $this->_initSendToFriendModel();
00100 $data = $this->getRequest()->getPost();
00101
00102 if (!$product || !$product->isVisibleInCatalog() || !$data) {
00103 $this->_forward('noRoute');
00104 return;
00105 }
00106
00107 $categoryId = $this->getRequest()->getParam('cat_id', null);
00108 if ($categoryId && $category = Mage::getModel('catalog/category')->load($categoryId)) {
00109 Mage::register('current_category', $category);
00110 }
00111
00112 $sendToFriendModel->setSender($this->getRequest()->getPost('sender'));
00113 $sendToFriendModel->setRecipients($this->getRequest()->getPost('recipients'));
00114 $sendToFriendModel->setIp(Mage::getSingleton('log/visitor')->getRemoteAddr());
00115 $sendToFriendModel->setProduct($product);
00116
00117 try {
00118 $validateRes = $sendToFriendModel->validate();
00119 if (true === $validateRes) {
00120 $sendToFriendModel->send();
00121 Mage::getSingleton('catalog/session')->addSuccess($this->__('Link to a friend was sent.'));
00122 $this->_redirectSuccess($product->getProductUrl());
00123 return;
00124 }
00125 else {
00126 Mage::getSingleton('catalog/session')->setFormData($data);
00127 if (is_array($validateRes)) {
00128 foreach ($validateRes as $errorMessage) {
00129 Mage::getSingleton('catalog/session')->addError($errorMessage);
00130 }
00131 } else {
00132 Mage::getSingleton('catalog/session')->addError($this->__('Some problems with data.'));
00133 }
00134 }
00135 } catch (Mage_Core_Exception $e) {
00136 Mage::getSingleton('catalog/session')->addError($e->getMessage());
00137 } catch (Exception $e) {
00138 Mage::getSingleton('catalog/session')
00139 ->addException($e, $this->__('Some emails was not sent'));
00140 }
00141
00142 $this->_redirectError(Mage::getURL('*/*/send',array('id'=>$product->getId())));
00143 }
00144 }