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_Model_Sendfriend extends Mage_Core_Model_Abstract
00028 {
00029
00030
00031
00032 const XML_PATH_SENDFRIEND_EMAIL_TEMPLATE = 'sendfriend/email/template';
00033
00034 protected $_names = array();
00035 protected $_emails = array();
00036 protected $_sender = array();
00037 protected $_ip = 0;
00038 protected $_product = null;
00039
00040 protected $_period = 3600;
00041
00042 protected $_cookieName = 'stf';
00043
00044 protected function _construct()
00045 {
00046 $this->_init('sendfriend/sendfriend');
00047 }
00048
00049 public function toOptionArray()
00050 {
00051 if(!$collection = Mage::registry('config_system_email_template')) {
00052 $collection = Mage::getResourceModel('core/email_template_collection')
00053 ->load();
00054
00055 Mage::register('config_system_email_template', $collection);
00056 }
00057 $options = $collection->toOptionArray();
00058 array_unshift($options, array('value'=>'', 'label'=>''));
00059 return $options;
00060 }
00061
00062 public function send()
00063 {
00064 $translate = Mage::getSingleton('core/translate');
00065
00066 $translate->setTranslateInline(false);
00067
00068 $errors = array();
00069
00070 $this->_emailModel = Mage::getModel('core/email_template');
00071 $message = nl2br(htmlspecialchars($this->_sender['message']));
00072 $sender = array(
00073 'name' => strip_tags($this->_sender['name']),
00074 'email' => strip_tags($this->_sender['email'])
00075 );
00076
00077 foreach($this->_emails as $key => $email) {
00078 $this->_emailModel->setDesignConfig(array('area'=>'frontend', 'store'=>$this->getStoreId()))
00079 ->sendTransactional(
00080 Mage::getStoreConfig(self::XML_PATH_SENDFRIEND_EMAIL_TEMPLATE),
00081 $sender,
00082 $email,
00083 $this->_names[$key],
00084 array(
00085 'name' => $this->_names[$key],
00086 'email' => $email,
00087 'product_name' => $this->_product->getName(),
00088 'product_url' => $this->_product->getProductUrl(),
00089 'message' => $message,
00090 'sender_name' => strip_tags($this->_sender['name']),
00091 'sender_email' => strip_tags($this->_sender['email']),
00092 'product_image' => Mage::helper('catalog/image')->init($this->_product, 'small_image')->resize(75),
00093 )
00094 );
00095 }
00096
00097 $translate->setTranslateInline(true);
00098
00099 return $this;
00100 }
00101
00102 public function validate()
00103 {
00104 $errors = array();
00105 $helper = Mage::helper('sendfriend');
00106
00107 if (empty($this->_sender['name'])) {
00108 $errors[] = $helper->__('Sender name can\'t be empty');
00109 }
00110
00111 if (!isset($this->_sender['email']) || !Zend_Validate::is($this->_sender['email'], 'EmailAddress')) {
00112 $errors[] = $helper->__('Invalid sender email');
00113 }
00114
00115 if (empty($this->_sender['message'])) {
00116 $errors[] = $helper->__('Message can\'t be empty');
00117 }
00118
00119 foreach ($this->_emails as $email) {
00120 if (!Zend_Validate::is($email, 'EmailAddress')) {
00121 $errors[] = $helper->__('You input invalid email address for recipient');
00122 break;
00123 }
00124 }
00125
00126 if (!$this->canEmailToFriend()) {
00127 $errors[] = $helper->__('You cannot email this product to a friend');
00128 }
00129
00130 if ($this->_getSendToFriendCheckType()) {
00131 $amount = $this->_amountByCookies();
00132 } else {
00133 $amount = $this->_amountByIp();
00134 }
00135
00136 if ($amount >= $this->getMaxSendsToFriend()){
00137 $errors[] = $helper->__('You have exceeded limit of %d sends in an hour', $this->getMaxSendsToFriend());
00138 }
00139
00140 $maxRecipients = $this->getMaxRecipients();
00141 if (count($this->_emails) > $maxRecipients) {
00142 $errors[] = $helper->__('You cannot send more than %d emails at a time', $this->getMaxRecipients());
00143 }
00144
00145 if (count($this->_emails) < 1) {
00146 $errors[] = $helper->__('You have to specify at least one recipient');
00147 }
00148
00149 if (!$this->getTemplate()){
00150 $errors[] = $helper->__('Email template is not specified by administrator');
00151 }
00152
00153
00154 if (empty($errors)) {
00155 return true;
00156 }
00157 return $errors;
00158 }
00159
00160 public function setIp($ip)
00161 {
00162 $this->_ip = $ip;
00163 }
00164
00165 public function setRecipients($recipients)
00166 {
00167 $this->_emails = array_unique($recipients['email']);
00168 $this->_names = $recipients['name'];
00169 }
00170
00171 public function setProduct($product){
00172 $this->_product = $product;
00173 }
00174
00175 public function setSender($sender){
00176 $this->_sender = $sender;
00177 }
00178
00179 public function getSendCount($ip, $startTime)
00180 {
00181 $count = $this->_getResource()->getSendCount($this, $ip, $startTime);
00182 return $count;
00183 }
00184
00185
00186
00187
00188
00189
00190 public function getMaxSendsToFriend()
00191 {
00192 return max(0, (int) Mage::getStoreConfig('sendfriend/email/max_per_hour'));
00193 }
00194
00195
00196
00197
00198
00199
00200 public function getTemplate()
00201 {
00202 return Mage::getStoreConfig('sendfriend/email/template');
00203 }
00204
00205
00206
00207
00208
00209
00210 public function getMaxRecipients()
00211 {
00212 return max(0, (int) Mage::getStoreConfig('sendfriend/email/max_recipients'));
00213 }
00214
00215
00216
00217
00218
00219
00220 public function canEmailToFriend()
00221 {
00222 if (!Mage::getStoreConfig('sendfriend/email/enabled')) {
00223 return false;
00224 }
00225 if (!Mage::getStoreConfig('sendfriend/email/allow_guest')
00226 && !Mage::getSingleton('customer/session')->isLoggedIn()) {
00227 return false;
00228 }
00229 return true;
00230 }
00231
00232
00233
00234
00235
00236
00237 private function _getSendToFriendCheckType()
00238 {
00239 return max(0, (int) Mage::getStoreConfig('sendfriend/email/check_by'));
00240 }
00241
00242
00243
00244
00245
00246 private function _amountByCookies()
00247 {
00248 $newTimes = array();
00249 $oldTimes = Mage::app()->getCookie()
00250 ->get($this->_cookieName);
00251 if ($oldTimes){
00252 $oldTimes = explode(',', $oldTimes);
00253 foreach ($oldTimes as $time){
00254 if (is_numeric($time) && $time >= time()-$this->_period){
00255 $newTimes[] = $time;
00256 }
00257 }
00258 }
00259 $amount = count($newTimes);
00260
00261 $newTimes[] = time();
00262 Mage::app()->getCookie()
00263 ->set($this->_cookieName, implode(',', $newTimes), $this->_period);
00264
00265 return $amount;
00266 }
00267
00268
00269
00270
00271
00272
00273 private function _amountByIp()
00274 {
00275 $this->_deleteLogsBefore(time() - $this->_period);
00276
00277 $amount = $this->getSendCount($this->_ip, time() - $this->_period);
00278
00279 $this->setData(array('ip'=>$this->_ip, 'time'=>time()));
00280 $this->save();
00281
00282 return $amount;
00283 }
00284
00285
00286
00287
00288
00289
00290
00291 private function _deleteLogsBefore($time)
00292 {
00293 $this->_getResource()->deleteLogsBefore($time);
00294 return $this;
00295 }
00296
00297
00298
00299
00300
00301
00302 public function register()
00303 {
00304 if (!Mage::registry('send_to_friend_model')) {
00305 Mage::register('send_to_friend_model', $this);
00306 }
00307 return $this;
00308 }
00309 }