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: 35: 36: 37: 38: 39: 40:
41: class Mage_Sendfriend_Model_Sendfriend extends Mage_Core_Model_Abstract
42: {
43: 44: 45: 46: 47:
48: protected $_names = array();
49:
50: 51: 52: 53: 54:
55: protected $_emails = array();
56:
57: 58: 59: 60: 61:
62: protected $_sender = array();
63:
64: 65: 66: 67: 68:
69: protected $_product;
70:
71: 72: 73: 74: 75:
76: protected $_sentCount;
77:
78: 79: 80: 81: 82:
83: protected $_lastCookieValue = array();
84:
85: 86: 87: 88:
89: protected function _construct()
90: {
91: $this->_init('sendfriend/sendfriend');
92: }
93:
94: 95: 96: 97: 98:
99: protected function _getHelper()
100: {
101: return Mage::helper('sendfriend');
102: }
103:
104: 105: 106: 107: 108: 109:
110: public function toOptionArray()
111: { return array();
112: }
113:
114: public function send()
115: {
116: if ($this->isExceedLimit()){
117: Mage::throwException(Mage::helper('sendfriend')->__('You have exceeded limit of %d sends in an hour', $this->getMaxSendsToFriend()));
118: }
119:
120:
121: $translate = Mage::getSingleton('core/translate');
122: $translate->setTranslateInline(false);
123:
124:
125: $mailTemplate = Mage::getModel('core/email_template');
126:
127: $message = nl2br(htmlspecialchars($this->getSender()->getMessage()));
128: $sender = array(
129: 'name' => $this->_getHelper()->htmlEscape($this->getSender()->getName()),
130: 'email' => $this->_getHelper()->htmlEscape($this->getSender()->getEmail())
131: );
132:
133: $mailTemplate->setDesignConfig(array(
134: 'area' => 'frontend',
135: 'store' => Mage::app()->getStore()->getId()
136: ));
137:
138: foreach ($this->getRecipients()->getEmails() as $k => $email) {
139: $name = $this->getRecipients()->getNames($k);
140: $mailTemplate->sendTransactional(
141: $this->getTemplate(),
142: $sender,
143: $email,
144: $name,
145: array(
146: 'name' => $name,
147: 'email' => $email,
148: 'product_name' => $this->getProduct()->getName(),
149: 'product_url' => $this->getProduct()->getUrlInStore(),
150: 'message' => $message,
151: 'sender_name' => $sender['name'],
152: 'sender_email' => $sender['email'],
153: 'product_image' => Mage::helper('catalog/image')->init($this->getProduct(),
154: 'small_image')->resize(75),
155: )
156: );
157: }
158:
159: $translate->setTranslateInline(true);
160: $this->_incrementSentCount();
161:
162: return $this;
163: }
164:
165: 166: 167: 168: 169:
170: public function validate()
171: {
172: $errors = array();
173:
174: $name = $this->getSender()->getName();
175: if (empty($name)) {
176: $errors[] = Mage::helper('sendfriend')->__('The sender name cannot be empty.');
177: }
178:
179: $email = $this->getSender()->getEmail();
180: if (empty($email) OR !Zend_Validate::is($email, 'EmailAddress')) {
181: $errors[] = Mage::helper('sendfriend')->__('Invalid sender email.');
182: }
183:
184: $message = $this->getSender()->getMessage();
185: if (empty($message)) {
186: $errors[] = Mage::helper('sendfriend')->__('The message cannot be empty.');
187: }
188:
189: if (!$this->getRecipients()->getEmails()) {
190: $errors[] = Mage::helper('sendfriend')->__('At least one recipient must be specified.');
191: }
192:
193:
194: foreach ($this->getRecipients()->getEmails() as $email) {
195: if (!Zend_Validate::is($email, 'EmailAddress')) {
196: $errors[] = Mage::helper('sendfriend')->__('An invalid email address for recipient was entered.');
197: break;
198: }
199: }
200:
201: $maxRecipients = $this->getMaxRecipients();
202: if (count($this->getRecipients()->getEmails()) > $maxRecipients) {
203: $errors[] = Mage::helper('sendfriend')->__('No more than %d emails can be sent at a time.', $this->getMaxRecipients());
204: }
205:
206: if (empty($errors)) {
207: return true;
208: }
209:
210: return $errors;
211: }
212:
213: 214: 215: 216: 217: 218:
219: public function setCookie($cookie)
220: {
221: return $this->setData('_cookie', $cookie);
222: }
223:
224: 225: 226: 227: 228: 229:
230: public function getCookie()
231: {
232: $cookie = $this->_getData('_cookie');
233: if (!$cookie instanceof Mage_Core_Model_Cookie) {
234: Mage::throwException(Mage::helper('sendfriend')->__('Please define a correct Cookie instance.'));
235: }
236: return $cookie;
237: }
238:
239: 240: 241: 242: 243: 244:
245: public function setRemoteAddr($ipAddr)
246: {
247: $this->setData('_remote_addr', $ipAddr);
248: return $this;
249: }
250:
251: 252: 253: 254: 255:
256: public function getRemoteAddr()
257: {
258: return $this->_getData('_remote_addr');
259: }
260:
261: 262: 263: 264: 265: 266:
267: public function setWebsiteId($id)
268: {
269: $this->setData('_website_id', $id);
270: return $this;
271: }
272:
273: 274: 275: 276: 277:
278: public function getWebsiteId()
279: {
280: return $this->_getData('_website_id');
281: }
282:
283: 284: 285: 286: 287: 288:
289: public function setRecipients($recipients)
290: {
291:
292: if (!is_array($recipients) OR !isset($recipients['email'])
293: OR !isset($recipients['name']) OR !is_array($recipients['email'])
294: OR !is_array($recipients['name'])) {
295: return $this;
296: }
297:
298: $emails = array();
299: $names = array();
300: foreach ($recipients['email'] as $k => $email) {
301: if (!isset($emails[$email]) && isset($recipients['name'][$k])) {
302: $emails[$email] = true;
303: $names[] = $recipients['name'][$k];
304: }
305: }
306:
307: if ($emails) {
308: $emails = array_keys($emails);
309: }
310:
311: return $this->setData('_recipients', new Varien_Object(array(
312: 'emails' => $emails,
313: 'names' => $names
314: )));
315: }
316:
317: 318: 319: 320: 321:
322: public function getRecipients()
323: {
324: $recipients = $this->_getData('_recipients');
325: if (!$recipients instanceof Varien_Object) {
326: $recipients = new Varien_Object(array(
327: 'emails' => array(),
328: 'names' => array()
329: ));
330: $this->setData('_recipients', $recipients);
331: }
332: return $recipients;
333: }
334:
335: 336: 337: 338: 339: 340:
341: public function setProduct($product)
342: {
343: return $this->setData('_product', $product);
344: }
345:
346: 347: 348: 349: 350: 351:
352: public function getProduct()
353: {
354: $product = $this->_getData('_product');
355: if (!$product instanceof Mage_Catalog_Model_Product) {
356: Mage::throwException(Mage::helper('sendfriend')->__('Please define a correct Product instance.'));
357: }
358: return $product;
359: }
360:
361: 362: 363: 364: 365: 366:
367: public function setSender($sender)
368: {
369: if (!is_array($sender)) {
370: Mage::helper('sendfriend')->__('Invalid Sender Information');
371: }
372:
373: return $this->setData('_sender', new Varien_Object($sender));
374: }
375:
376: 377: 378: 379: 380: 381:
382: public function getSender()
383: {
384: $sender = $this->_getData('_sender');
385: if (!$sender instanceof Varien_Object) {
386: Mage::throwException(Mage::helper('sendfriend')->__('Please define the correct Sender information.'));
387: }
388: return $sender;
389: }
390:
391: 392: 393: 394: 395: 396: 397: 398: 399:
400: public function getSendCount($ip = null, $startTime = null)
401: {
402: if (is_null($ip)) {
403: $ip = $this->getRemoteAddr();
404: }
405: if (is_null($startTime)) {
406: $startTime = time() - $this->_getHelper()->getPeriod();
407: }
408:
409: return $this->_getResource()->getSendCount($this, $ip, $startTime);
410: }
411:
412: 413: 414: 415: 416:
417: public function getMaxSendsToFriend()
418: {
419: return $this->_getHelper()->getMaxEmailPerPeriod();
420: }
421:
422: 423: 424: 425: 426:
427: public function getTemplate()
428: {
429: return $this->_getHelper()->getEmailTemplate();
430: }
431:
432: 433: 434: 435: 436:
437: public function getMaxRecipients()
438: {
439: return $this->_getHelper()->getMaxRecipients();
440: }
441:
442: 443: 444: 445: 446:
447: public function canEmailToFriend()
448: {
449: return $this->_getHelper()->isEnabled();
450: }
451:
452: 453: 454: 455: 456:
457: public function isExceedLimit()
458: {
459: return $this->getSentCount() >= $this->getMaxSendsToFriend();
460: }
461:
462: 463: 464: 465: 466: 467:
468: public function getSentCount($useCache = true)
469: {
470: if ($useCache && !is_null($this->_sentCount)) {
471: return $this->_sentCount;
472: }
473:
474: switch ($this->_getHelper()->getLimitBy()) {
475: case Mage_Sendfriend_Helper_Data::CHECK_COOKIE:
476: return $this->_sentCount = $this->_sentCountByCookies(false);
477: case Mage_Sendfriend_Helper_Data::CHECK_IP:
478: return $this->_sentCount = $this->_sentCountByIp(false);
479: default:
480: return 0;
481: }
482: }
483:
484: 485: 486: 487: 488:
489: protected function _incrementSentCount()
490: {
491: switch ($this->_getHelper()->getLimitBy()) {
492: case Mage_Sendfriend_Helper_Data::CHECK_COOKIE:
493: return $this->_sentCount = $this->_sentCountByCookies(true);
494: case Mage_Sendfriend_Helper_Data::CHECK_IP:
495: return $this->_sentCount = $this->_sentCountByIp(true);
496: default:
497: return 0;
498: }
499: }
500:
501: 502: 503: 504: 505: 506:
507: protected function _sentCountByCookies($increment = false)
508: {
509: $cookie = $this->_getHelper()->getCookieName();
510: $time = time();
511: $newTimes = array();
512:
513: if (isset($this->_lastCookieValue[$cookie])) {
514: $oldTimes = $this->_lastCookieValue[$cookie];
515: } else {
516: $oldTimes = $this->getCookie()->get($cookie);
517: }
518:
519: if ($oldTimes) {
520: $oldTimes = explode(',', $oldTimes);
521: foreach ($oldTimes as $oldTime) {
522: $periodTime = $time - $this->_getHelper()->getPeriod();
523: if (is_numeric($oldTime) AND $oldTime >= $periodTime) {
524: $newTimes[] = $oldTime;
525: }
526: }
527: }
528:
529: if ($increment) {
530: $newTimes[] = $time;
531: $newValue = implode(',', $newTimes);
532: $this->getCookie()->set($cookie, $newValue);
533: $this->_lastCookieValue[$cookie] = $newValue;
534: }
535:
536: return count($newTimes);
537: }
538: 539: 540: 541: 542: 543:
544: protected function _sentCountByIp($increment = false)
545: {
546: $time = time();
547: $period = $this->_getHelper()->getPeriod();
548: $websiteId = $this->getWebsiteId();
549:
550: if ($increment) {
551:
552: $this->_getResource()->deleteLogsBefore($time - $period);
553:
554: $this->_getResource()->addSendItem($this->getRemoteAddr(), $time, $websiteId);
555: }
556:
557: return $this->_getResource()->getSendCount($this, $this->getRemoteAddr(), time() - $period, $websiteId);
558: }
559: 560: 561: 562: 563:
564: public function register()
565: {
566: if (!Mage::registry('send_to_friend_model')) {
567: Mage::register('send_to_friend_model', $this);
568: }
569: return $this;
570: }
571:
572: 573: 574: 575: 576: 577:
578: protected function _amountByCookies()
579: {
580: return $this->_sentCountByCookies(true);
581: }
582:
583: 584: 585: 586: 587: 588:
589: protected function _amountByIp()
590: {
591: return $this->_sentCountByIp(true);
592: }
593: }
594: