Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Sendfriend_Block_Send
  • Mage_Sendfriend_Model_Mysql4_Sendfriend
  • Mage_Sendfriend_Model_Mysql4_Sendfriend_Collection
  • Mage_Sendfriend_Model_Mysql4_Setup
  • Mage_Sendfriend_Model_Observer
  • Mage_Sendfriend_Model_Resource_Sendfriend
  • Mage_Sendfriend_Model_Resource_Sendfriend_Collection
  • Mage_Sendfriend_Model_Resource_Setup
  • Mage_Sendfriend_Model_Sendfriend
  • Overview
  • Package
  • Class
  • Tree
  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) 2012 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: /**
 28:  * SendFriend Log
 29:  *
 30:  * @method Mage_Sendfriend_Model_Resource_Sendfriend _getResource()
 31:  * @method Mage_Sendfriend_Model_Resource_Sendfriend getResource()
 32:  * @method int getIp()
 33:  * @method Mage_Sendfriend_Model_Sendfriend setIp(int $value)
 34:  * @method int getTime()
 35:  * @method Mage_Sendfriend_Model_Sendfriend setTime(int $value)
 36:  *
 37:  * @category    Mage
 38:  * @package     Mage_Sendfriend
 39:  * @author      Magento Core Team <core@magentocommerce.com>
 40:  */
 41: class Mage_Sendfriend_Model_Sendfriend extends Mage_Core_Model_Abstract
 42: {
 43:     /**
 44:      * Recipient Names
 45:      *
 46:      * @var array
 47:      */
 48:     protected $_names   = array();
 49: 
 50:     /**
 51:      * Recipient Emails
 52:      *
 53:      * @var array
 54:      */
 55:     protected $_emails  = array();
 56: 
 57:     /**
 58:      * Sender data array
 59:      *
 60:      * @var array
 61:      */
 62:     protected $_sender  = array();
 63: 
 64:     /**
 65:      * Product Instance
 66:      *
 67:      * @var Mage_Catalog_Model_Product
 68:      */
 69:     protected $_product;
 70: 
 71:     /**
 72:      * Count of sent in last period
 73:      *
 74:      * @var int
 75:      */
 76:     protected $_sentCount;
 77: 
 78:     /**
 79:      * Last values for Cookie
 80:      *
 81:      * @var string
 82:      */
 83:     protected $_lastCookieValue = array();
 84: 
 85:     /**
 86:      * Initialize resource model
 87:      *
 88:      */
 89:     protected function _construct()
 90:     {
 91:         $this->_init('sendfriend/sendfriend');
 92:     }
 93: 
 94:     /**
 95:      * Retrieve Data Helper
 96:      *
 97:      * @return Mage_Sendfriend_Helper_Data
 98:      */
 99:     protected function _getHelper()
100:     {
101:         return Mage::helper('sendfriend');
102:     }
103: 
104:     /**
105:      * Retrieve Option Array
106:      *
107:      * @deprecated It Is a not Source model
108:      * @return array
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:         /* @var $translate Mage_Core_Model_Translate */
121:         $translate = Mage::getSingleton('core/translate');
122:         $translate->setTranslateInline(false);
123: 
124:         /* @var $mailTemplate Mage_Core_Model_Email_Template */
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:      * Validate Form data
167:      *
168:      * @return bool|array
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:         // validate recipients email addresses
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:      * Set cookie instance
215:      *
216:      * @param Mage_Core_Model_Cookie $product
217:      * @return Mage_Sendfriend_Model_Sendfriend
218:      */
219:     public function setCookie($cookie)
220:     {
221:         return $this->setData('_cookie', $cookie);
222:     }
223: 
224:     /**
225:      * Retrieve Cookie instance
226:      *
227:      * @throws Mage_Core_Exception
228:      * @return Mage_Core_Model_Cookie
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:      * Set Visitor Remote Address
241:      *
242:      * @param int $ipAddr the IP address on Long Format
243:      * @return Mage_Sendfriend_Model_Sendfriend
244:      */
245:     public function setRemoteAddr($ipAddr)
246:     {
247:         $this->setData('_remote_addr', $ipAddr);
248:         return $this;
249:     }
250: 
251:     /**
252:      * Retrieve Visitor Remote Address
253:      *
254:      * @return int
255:      */
256:     public function getRemoteAddr()
257:     {
258:         return $this->_getData('_remote_addr');
259:     }
260: 
261:     /**
262:      * Set Website Id
263:      *
264:      * @param int $id - website id
265:      * @return Mage_Sendfriend_Model_Sendfriend
266:      */
267:     public function setWebsiteId($id)
268:     {
269:         $this->setData('_website_id', $id);
270:         return $this;
271:     }
272: 
273:     /**
274:      * Retrieve Website Id
275:      *
276:      * @return int
277:      */
278:     public function getWebsiteId()
279:     {
280:         return $this->_getData('_website_id');
281:     }
282: 
283:     /**
284:      * Set Recipients
285:      *
286:      * @param array $recipients
287:      * @return Mage_Sendfriend_Model_Sendfriend
288:      */
289:     public function setRecipients($recipients)
290:     {
291:         // validate array
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:      * Retrieve Recipients object
319:      *
320:      * @return Varien_Object
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:      * Set product instance
337:      *
338:      * @param Mage_Catalog_Model_Product $product
339:      * @return Mage_Sendfriend_Model_Sendfriend
340:      */
341:     public function setProduct($product)
342:     {
343:         return $this->setData('_product', $product);
344:     }
345: 
346:     /**
347:      * Retrieve Product instance
348:      *
349:      * @throws Mage_Core_Exception
350:      * @return Mage_Catalog_Model_Product
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:      * Set Sender Information array
363:      *
364:      * @param array $sender
365:      * @return Mage_Sendfriend_Model_Sendfriend
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:      * Retrieve Sender Information Object
378:      *
379:      * @throws Mage_Core_Exception
380:      * @return Varien_Object
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:      * @deprecated after 1.3.2.4
393:      * For get count sent letters use Mage_Sendfriend_Model_Sendfriend::getSentCount
394:      * or Mage_Sendfriend_Model_Sendfriend::isExceedLimit
395:      * 
396:      * @param int $ip
397:      * @param int $startTime
398:      * @return int
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:      * Get max allowed uses of "Send to Friend" function per hour
414:      *
415:      * @return integer
416:      */
417:     public function getMaxSendsToFriend()
418:     {
419:         return $this->_getHelper()->getMaxEmailPerPeriod();
420:     }
421: 
422:     /**
423:      * Get current Email "Send to friend" template
424:      *
425:      * @return string
426:      */
427:     public function getTemplate()
428:     {
429:         return $this->_getHelper()->getEmailTemplate();
430:     }
431: 
432:     /**
433:      * Get max allowed recipients for "Send to a Friend" function
434:      *
435:      * @return integer
436:      */
437:     public function getMaxRecipients()
438:     {
439:         return $this->_getHelper()->getMaxRecipients();
440:     }
441: 
442:     /**
443:      * Check if user is allowed to email product to a friend
444:      *
445:      * @return boolean
446:      */
447:     public function canEmailToFriend()
448:     {
449:         return $this->_getHelper()->isEnabled();
450:     }
451: 
452:     /**
453:      * Check if user is exceed limit
454:      *
455:      * @return boolean
456:      */
457:     public function isExceedLimit()
458:     {
459:         return $this->getSentCount() >= $this->getMaxSendsToFriend();
460:     }
461: 
462:     /**
463:      * Return count of sent in last period
464:      * 
465:      * @param bool $useCache - flag, is allow to use value of attribute of model if it is processed last time 
466:      * @return int
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:      * Increase count of sent
486:      * 
487:      * @return int
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:      * Return count of sent in last period by cookie
503:      *
504:      * @param bool $increment - flag, increase count before return value 
505:      * @return int
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:      * Return count of sent in last period by IP address
540:      *
541:      * @param bool $increment - flag, increase count before return value
542:      * @return int
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:             // delete expired logs
552:             $this->_getResource()->deleteLogsBefore($time - $period);
553:             // add new item
554:             $this->_getResource()->addSendItem($this->getRemoteAddr(), $time, $websiteId);
555:         }
556: 
557:         return $this->_getResource()->getSendCount($this, $this->getRemoteAddr(), time() - $period, $websiteId); 
558:     }
559:     /**
560:      * Register self in global register with name send_to_friend_model
561:      *
562:      * @return Mage_Sendfriend_Model_Sendfriend
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:      * @deprecated after 1.3.2.4
574:      * use Mage_Sendfriend_Model_Sendfriend::_sentCountByCookies
575:      * 
576:      * @return int
577:      */
578:     protected function _amountByCookies()
579:     {
580:         return $this->_sentCountByCookies(true);
581:     }
582: 
583:     /**
584:      * @deprecated after 1.3.2.4
585:      * use Mage_Sendfriend_Model_Sendfriend::_sentCountByIp
586:      *
587:      * @return int
588:      */
589:     protected function _amountByIp()
590:     {
591:         return $this->_sentCountByIp(true);
592:     }
593: }
594: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0