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_Catalog_Model_Sendfriend extends Mage_Core_Model_Abstract
00028 {
00029     protected $_names = array();
00030     protected $_emails = array();
00031     protected $_sender = array();
00032     protected $_ip = 0;
00033     protected $_product = null;
00034 
00035     protected $_period = 3600; 
00036 
00037     protected $_cookieName = 'stf';
00038 
00039     protected function _construct()
00040     {
00041         $this->_init('catalog/sendfriend');
00042     }
00043 
00044     public function toOptionArray()
00045     {
00046         if(!$collection = Mage::registry('config_system_email_template')) {
00047             $collection = Mage::getResourceModel('core/email_template_collection')
00048                 ->load();
00049 
00050             Mage::register('config_system_email_template', $collection);
00051         }
00052         $options = $collection->toOptionArray();
00053         array_unshift($options, array('value'=>'', 'label'=>''));
00054         return $options;
00055     }
00056 
00057     public function send()
00058     {
00059         $errors = array();
00060 
00061         $this->_emailModel = Mage::getModel('core/email_template');
00062 
00063         $this->_emailModel->load($this->getTemplate());
00064         if (!$this->_emailModel->getId()) {
00065             Mage::throwException(
00066                Mage::helper('catalog')->__('Invalid transactional email code')
00067             );
00068         }
00069 
00070         $this->_emailModel->setSenderName(strip_tags($this->_sender['name']));
00071         $this->_emailModel->setSenderEmail(strip_tags($this->_sender['email']));
00072 
00073         foreach ($this->_emails as $k=>$email) {
00074             if (!$this->_sendOne($email, $this->_names[$k])) {
00075                 $errors[] = $email;
00076             }
00077         }
00078 
00079         if (count($errors)) {
00080             Mage::throwException(
00081                 Mage::helper('catalog')->__('Email to %s was not sent', implode(', ', $errors))
00082             );
00083         }
00084     }
00085 
00086     public function canSend()
00087     {
00088         if (!$this->canEmailToFriend()) {
00089             Mage::throwException(
00090                 Mage::helper('catalog')->__('You cannot email this product to a friend')
00091             );
00092         }
00093 
00094         if ($this->_getSendToFriendCheckType()) {
00095             $amount = $this->_amountByCookies();
00096         } else {
00097             $amount = $this->_amountByIp();
00098         }
00099 
00100         if ($amount >= $this->getMaxSendsToFriend()){
00101             Mage::throwException(
00102                 Mage::helper('catalog')->__('You have exceeded limit of %d sends in an hour', $this->getMaxSendsToFriend())
00103             );
00104         }
00105 
00106         $maxRecipients = $this->getMaxRecipients();
00107         if (count($this->_emails) > $maxRecipients) {
00108             Mage::throwException(
00109                 Mage::helper('catalog')->__('You cannot send more than %d emails at a time', $this->getMaxRecipients())
00110             );
00111         }
00112 
00113         if (count($this->_emails) < 1) {
00114             Mage::throwException(
00115                 Mage::helper('catalog')->__('You have to specify at least one recipient')
00116             );
00117         }
00118 
00119         if (!$this->getTemplate()){
00120             Mage::throwException(
00121                 Mage::helper('catalog')->__('Email template is not specified by administrator')
00122             );
00123         }
00124 
00125         return true;
00126     }
00127 
00128     public function setIp($ip)
00129     {
00130         $this->_ip = $ip;
00131     }
00132 
00133     public function setRecipients($recipients)
00134     {
00135         $this->_emails = array_unique($recipients['email']);
00136         $this->_names = $recipients['name'];
00137     }
00138 
00139     public function setProduct($product){
00140         $this->_product = $product;
00141     }
00142 
00143     public function setSender($sender){
00144         $this->_sender = $sender;
00145     }
00146 
00147     public function getSendCount($ip, $startTime)
00148     {
00149         $count = $this->_getResource()->getSendCount($this, $ip, $startTime);
00150         return $count;
00151     }
00152 
00153 
00154 
00155 
00156 
00157 
00158     public function getMaxSendsToFriend()
00159     {
00160         return max(0, (int) Mage::getStoreConfig('sendfriend/email/max_per_hour'));
00161     }
00162 
00163 
00164 
00165 
00166 
00167 
00168     public function getTemplate()
00169     {
00170         return Mage::getStoreConfig('sendfriend/email/template');
00171     }
00172 
00173 
00174 
00175 
00176 
00177 
00178     public function getMaxRecipients()
00179     {
00180         return max(0, (int) Mage::getStoreConfig('sendfriend/email/max_recipients'));
00181     }
00182 
00183 
00184 
00185 
00186 
00187 
00188     public function canEmailToFriend()
00189     {
00190         if (!Mage::getStoreConfig('sendfriend/email/enabled')) {
00191             return false;
00192         }
00193         if (!Mage::getStoreConfig('sendfriend/email/allow_guest')
00194             && !Mage::getSingleton('customer/session')->isLoggedIn()) {
00195             return false;
00196         }
00197         return true;
00198     }
00199 
00200     private function _sendOne($email, $name){
00201         $email = trim($email);
00202 
00203         $vars = array(
00204            'senderName' => strip_tags($this->_sender['name']),
00205            'senderEmail' => strip_tags($this->_sender['email']),
00206            'receiverName' => strip_tags($name),
00207            'receiverEmail' => strip_tags($email),
00208            'product' => $this->_product,
00209            'message' => strip_tags($this->_sender['message'])
00210            );
00211 
00212         if (!$this->_emailModel->send(strip_tags($email), strip_tags($name), $vars)){
00213             return false;
00214         }
00215 
00216         return true;
00217     }
00218 
00219 
00220 
00221 
00222 
00223 
00224     private function _getSendToFriendCheckType()
00225     {
00226         return max(0, (int) Mage::getStoreConfig('sendfriend/email/check_by'));
00227     }
00228 
00229     private function _amountByCookies()
00230     {
00231         $newTimes = array();
00232         $oldTimes = Mage::app()->getCookie()->get($this->_cookieName);
00233         if ($oldTimes){
00234             $oldTimes = explode(',', $oldTimes);
00235             foreach ($oldTimes as $time){
00236                 if (is_numeric($time) && $time >= time()-$this->_period){
00237                     $newTimes[] = $time;
00238                 }
00239             }
00240         }
00241         $amount = count($newTimes);
00242 
00243         $newTimes[] = time();
00244         Mage::app()->getCookie()
00245             ->set($this->_cookieName, implode(',', $newTimes), $this->_period);
00246 
00247         return $amount;
00248     }
00249 
00250     private function _amountByIp()
00251     {
00252         $this->_deleteLogsBefore(time() - $this->_period);
00253 
00254         $amount = $this->getSendCount($this->_ip, time() - $this->_period);
00255 
00256         $this->setData(array('ip'=>$this->_ip, 'time'=>time()));
00257         $this->save();
00258 
00259         return $amount;
00260     }
00261 
00262     private function _deleteLogsBefore($time)
00263     {
00264         $this->_getResource()->deleteLogsBefore($time);
00265         return $this;
00266     }
00267 }