PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/core/Mage/Sendfriend/Model/Sendfriend.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 579 lines | 295 code | 61 blank | 223 comment | 30 complexity | c53331e147aaf646eb87d27b607d5632 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Sendfriend_Model_Sendfriend extends Mage_Core_Model_Abstract
  27. {
  28. /**
  29. * Recipient Names
  30. *
  31. * @var array
  32. */
  33. protected $_names = array();
  34. /**
  35. * Recipient Emails
  36. *
  37. * @var array
  38. */
  39. protected $_emails = array();
  40. /**
  41. * Sender data array
  42. *
  43. * @var array
  44. */
  45. protected $_sender = array();
  46. /**
  47. * Product Instance
  48. *
  49. * @var Mage_Catalog_Model_Product
  50. */
  51. protected $_product;
  52. /**
  53. * Count of sent in last period
  54. *
  55. * @var int
  56. */
  57. protected $_sentCount;
  58. /**
  59. * Last values for Cookie
  60. *
  61. * @var string
  62. */
  63. protected $_lastCookieValue = array();
  64. /**
  65. * Initialize resource model
  66. *
  67. */
  68. protected function _construct()
  69. {
  70. $this->_init('sendfriend/sendfriend');
  71. }
  72. /**
  73. * Retrieve Data Helper
  74. *
  75. * @return Mage_Sendfriend_Helper_Data
  76. */
  77. protected function _getHelper()
  78. {
  79. return Mage::helper('sendfriend');
  80. }
  81. /**
  82. * Retrieve Option Array
  83. *
  84. * @deprecated It Is a not Source model
  85. * @return array
  86. */
  87. public function toOptionArray()
  88. { return array();
  89. }
  90. public function send()
  91. {
  92. if ($this->isExceedLimit()){
  93. Mage::throwException(Mage::helper('sendfriend')->__('You have exceeded limit of %d sends in an hour', $this->getMaxSendsToFriend()));
  94. }
  95. /* @var $translate Mage_Core_Model_Translate */
  96. $translate = Mage::getSingleton('core/translate');
  97. $translate->setTranslateInline(false);
  98. /* @var $mailTemplate Mage_Core_Model_Email_Template */
  99. $mailTemplate = Mage::getModel('core/email_template');
  100. $message = nl2br(htmlspecialchars($this->getSender()->getMessage()));
  101. $sender = array(
  102. 'name' => $this->_getHelper()->htmlEscape($this->getSender()->getName()),
  103. 'email' => $this->_getHelper()->htmlEscape($this->getSender()->getEmail())
  104. );
  105. $mailTemplate->setDesignConfig(array(
  106. 'area' => 'frontend',
  107. 'store' => Mage::app()->getStore()->getId()
  108. ));
  109. foreach ($this->getRecipients()->getEmails() as $k => $email) {
  110. $name = $this->getRecipients()->getNames($k);
  111. $mailTemplate->sendTransactional(
  112. $this->getTemplate(),
  113. $sender,
  114. $email,
  115. $name,
  116. array(
  117. 'name' => $name,
  118. 'email' => $email,
  119. 'product_name' => $this->getProduct()->getName(),
  120. 'product_url' => $this->getProduct()->getUrlInStore(),
  121. 'message' => $message,
  122. 'sender_name' => $sender['name'],
  123. 'sender_email' => $sender['email'],
  124. 'product_image' => Mage::helper('catalog/image')->init($this->getProduct(),
  125. 'small_image')->resize(75),
  126. )
  127. );
  128. }
  129. $translate->setTranslateInline(true);
  130. $this->_incrementSentCount();
  131. return $this;
  132. }
  133. /**
  134. * Validate Form data
  135. *
  136. * @return bool|array
  137. */
  138. public function validate()
  139. {
  140. $errors = array();
  141. $name = $this->getSender()->getName();
  142. if (empty($name)) {
  143. $errors[] = Mage::helper('sendfriend')->__('The sender name cannot be empty.');
  144. }
  145. $email = $this->getSender()->getEmail();
  146. if (empty($email) OR !Zend_Validate::is($email, 'EmailAddress')) {
  147. $errors[] = Mage::helper('sendfriend')->__('Invalid sender email.');
  148. }
  149. $message = $this->getSender()->getMessage();
  150. if (empty($message)) {
  151. $errors[] = Mage::helper('sendfriend')->__('The message cannot be empty.');
  152. }
  153. if (!$this->getRecipients()->getEmails()) {
  154. $errors[] = Mage::helper('sendfriend')->__('At least one recipient must be specified.');
  155. }
  156. // validate recipients email addresses
  157. foreach ($this->getRecipients()->getEmails() as $email) {
  158. if (!Zend_Validate::is($email, 'EmailAddress')) {
  159. $errors[] = Mage::helper('sendfriend')->__('An invalid email address for recipient was entered.');
  160. break;
  161. }
  162. }
  163. $maxRecipients = $this->getMaxRecipients();
  164. if (count($this->getRecipients()->getEmails()) > $maxRecipients) {
  165. $errors[] = Mage::helper('sendfriend')->__('No more than %d emails can be sent at a time.', $this->getMaxRecipients());
  166. }
  167. if (empty($errors)) {
  168. return true;
  169. }
  170. return $errors;
  171. }
  172. /**
  173. * Set cookie instance
  174. *
  175. * @param Mage_Core_Model_Cookie $product
  176. * @return Mage_Sendfriend_Model_Sendfriend
  177. */
  178. public function setCookie($cookie)
  179. {
  180. return $this->setData('_cookie', $cookie);
  181. }
  182. /**
  183. * Retrieve Cookie instance
  184. *
  185. * @throws Mage_Core_Exception
  186. * @return Mage_Core_Model_Cookie
  187. */
  188. public function getCookie()
  189. {
  190. $cookie = $this->_getData('_cookie');
  191. if (!$cookie instanceof Mage_Core_Model_Cookie) {
  192. Mage::throwException(Mage::helper('sendfriend')->__('Please define a correct Cookie instance.'));
  193. }
  194. return $cookie;
  195. }
  196. /**
  197. * Set Visitor Remote Address
  198. *
  199. * @param int $ipAddr the IP address on Long Format
  200. * @return Mage_Sendfriend_Model_Sendfriend
  201. */
  202. public function setRemoteAddr($ipAddr)
  203. {
  204. $this->setData('_remote_addr', $ipAddr);
  205. return $this;
  206. }
  207. /**
  208. * Retrieve Visitor Remote Address
  209. *
  210. * @return int
  211. */
  212. public function getRemoteAddr()
  213. {
  214. return $this->_getData('_remote_addr');
  215. }
  216. /**
  217. * Set Website Id
  218. *
  219. * @param int $id - website id
  220. * @return Mage_Sendfriend_Model_Sendfriend
  221. */
  222. public function setWebsiteId($id)
  223. {
  224. $this->setData('_website_id', $id);
  225. return $this;
  226. }
  227. /**
  228. * Retrieve Website Id
  229. *
  230. * @return int
  231. */
  232. public function getWebsiteId()
  233. {
  234. return $this->_getData('_website_id');
  235. }
  236. /**
  237. * Set Recipients
  238. *
  239. * @param array $recipients
  240. * @return Mage_Sendfriend_Model_Sendfriend
  241. */
  242. public function setRecipients($recipients)
  243. {
  244. // validate array
  245. if (!is_array($recipients) OR !isset($recipients['email'])
  246. OR !isset($recipients['name']) OR !is_array($recipients['email'])
  247. OR !is_array($recipients['name'])) {
  248. return $this;
  249. }
  250. $emails = array();
  251. $names = array();
  252. foreach ($recipients['email'] as $k => $email) {
  253. if (!isset($emails[$email]) && isset($recipients['name'][$k])) {
  254. $emails[$email] = true;
  255. $names[] = $recipients['name'][$k];
  256. }
  257. }
  258. if ($emails) {
  259. $emails = array_keys($emails);
  260. }
  261. return $this->setData('_recipients', new Varien_Object(array(
  262. 'emails' => $emails,
  263. 'names' => $names
  264. )));
  265. }
  266. /**
  267. * Retrieve Recipients object
  268. *
  269. * @return Varien_Object
  270. */
  271. public function getRecipients()
  272. {
  273. $recipients = $this->_getData('_recipients');
  274. if (!$recipients instanceof Varien_Object) {
  275. $recipients = new Varien_Object(array(
  276. 'emails' => array(),
  277. 'names' => array()
  278. ));
  279. $this->setData('_recipients', $recipients);
  280. }
  281. return $recipients;
  282. }
  283. /**
  284. * Set product instance
  285. *
  286. * @param Mage_Catalog_Model_Product $product
  287. * @return Mage_Sendfriend_Model_Sendfriend
  288. */
  289. public function setProduct($product)
  290. {
  291. return $this->setData('_product', $product);
  292. }
  293. /**
  294. * Retrieve Product instance
  295. *
  296. * @throws Mage_Core_Exception
  297. * @return Mage_Catalog_Model_Product
  298. */
  299. public function getProduct()
  300. {
  301. $product = $this->_getData('_product');
  302. if (!$product instanceof Mage_Catalog_Model_Product) {
  303. Mage::throwException(Mage::helper('sendfriend')->__('Please define a correct Product instance.'));
  304. }
  305. return $product;
  306. }
  307. /**
  308. * Set Sender Information array
  309. *
  310. * @param array $sender
  311. * @return Mage_Sendfriend_Model_Sendfriend
  312. */
  313. public function setSender($sender)
  314. {
  315. if (!is_array($sender)) {
  316. Mage::helper('sendfriend')->__('Invalid Sender Information');
  317. }
  318. return $this->setData('_sender', new Varien_Object($sender));
  319. }
  320. /**
  321. * Retrieve Sender Information Object
  322. *
  323. * @throws Mage_Core_Exception
  324. * @return Varien_Object
  325. */
  326. public function getSender()
  327. {
  328. $sender = $this->_getData('_sender');
  329. if (!$sender instanceof Varien_Object) {
  330. Mage::throwException(Mage::helper('sendfriend')->__('Please define the correct Sender information.'));
  331. }
  332. return $sender;
  333. }
  334. /**
  335. * @deprecated after 1.3.2.4
  336. * For get count sent letters use Mage_Sendfriend_Model_Sendfriend::getSentCount
  337. * or Mage_Sendfriend_Model_Sendfriend::isExceedLimit
  338. *
  339. * @param int $ip
  340. * @param int $startTime
  341. * @return int
  342. */
  343. public function getSendCount($ip = null, $startTime = null)
  344. {
  345. if (is_null($ip)) {
  346. $ip = $this->getRemoteAddr();
  347. }
  348. if (is_null($startTime)) {
  349. $startTime = time() - $this->_getHelper()->getPeriod();
  350. }
  351. return $this->_getResource()->getSendCount($this, $ip, $startTime);
  352. }
  353. /**
  354. * Get max allowed uses of "Send to Friend" function per hour
  355. *
  356. * @return integer
  357. */
  358. public function getMaxSendsToFriend()
  359. {
  360. return $this->_getHelper()->getMaxEmailPerPeriod();
  361. }
  362. /**
  363. * Get current Email "Send to friend" template
  364. *
  365. * @return string
  366. */
  367. public function getTemplate()
  368. {
  369. return $this->_getHelper()->getEmailTemplate();
  370. }
  371. /**
  372. * Get max allowed recipients for "Send to a Friend" function
  373. *
  374. * @return integer
  375. */
  376. public function getMaxRecipients()
  377. {
  378. return $this->_getHelper()->getMaxRecipients();
  379. }
  380. /**
  381. * Check if user is allowed to email product to a friend
  382. *
  383. * @return boolean
  384. */
  385. public function canEmailToFriend()
  386. {
  387. return $this->_getHelper()->isEnabled();
  388. }
  389. /**
  390. * Check if user is exceed limit
  391. *
  392. * @return boolean
  393. */
  394. public function isExceedLimit()
  395. {
  396. return $this->getSentCount() >= $this->getMaxSendsToFriend();
  397. }
  398. /**
  399. * Return count of sent in last period
  400. *
  401. * @param bool $useCache - flag, is allow to use value of attribute of model if it is processed last time
  402. * @return int
  403. */
  404. public function getSentCount($useCache = true)
  405. {
  406. if ($useCache && !is_null($this->_sentCount)) {
  407. return $this->_sentCount;
  408. }
  409. switch ($this->_getHelper()->getLimitBy()) {
  410. case Mage_Sendfriend_Helper_Data::CHECK_COOKIE:
  411. return $this->_sentCount = $this->_sentCountByCookies(false);
  412. case Mage_Sendfriend_Helper_Data::CHECK_IP:
  413. return $this->_sentCount = $this->_sentCountByIp(false);
  414. default:
  415. return 0;
  416. }
  417. }
  418. /**
  419. * Increase count of sent
  420. *
  421. * @return int
  422. */
  423. protected function _incrementSentCount()
  424. {
  425. switch ($this->_getHelper()->getLimitBy()) {
  426. case Mage_Sendfriend_Helper_Data::CHECK_COOKIE:
  427. return $this->_sentCount = $this->_sentCountByCookies(true);
  428. case Mage_Sendfriend_Helper_Data::CHECK_IP:
  429. return $this->_sentCount = $this->_sentCountByIp(true);
  430. default:
  431. return 0;
  432. }
  433. }
  434. /**
  435. * Return count of sent in last period by cookie
  436. *
  437. * @param bool $increment - flag, increase count before return value
  438. * @return int
  439. */
  440. protected function _sentCountByCookies($increment = false)
  441. {
  442. $cookie = $this->_getHelper()->getCookieName();
  443. $time = time();
  444. $newTimes = array();
  445. if (isset($this->_lastCookieValue[$cookie])) {
  446. $oldTimes = $this->_lastCookieValue[$cookie];
  447. } else {
  448. $oldTimes = $this->getCookie()->get($cookie);
  449. }
  450. if ($oldTimes) {
  451. $oldTimes = explode(',', $oldTimes);
  452. foreach ($oldTimes as $oldTime) {
  453. $periodTime = $time - $this->_getHelper()->getPeriod();
  454. if (is_numeric($oldTime) AND $oldTime >= $periodTime) {
  455. $newTimes[] = $oldTime;
  456. }
  457. }
  458. }
  459. if ($increment) {
  460. $newTimes[] = $time;
  461. $newValue = implode(',', $newTimes);
  462. $this->getCookie()->set($cookie, $newValue);
  463. $this->_lastCookieValue[$cookie] = $newValue;
  464. }
  465. return count($newTimes);
  466. }
  467. /**
  468. * Return count of sent in last period by IP address
  469. *
  470. * @param bool $increment - flag, increase count before return value
  471. * @return int
  472. */
  473. protected function _sentCountByIp($increment = false)
  474. {
  475. $time = time();
  476. $period = $this->_getHelper()->getPeriod();
  477. $websiteId = $this->getWebsiteId();
  478. if ($increment) {
  479. // delete expired logs
  480. $this->_getResource()->deleteLogsBefore($time - $period);
  481. // add new item
  482. $this->_getResource()->addSendItem($this->getRemoteAddr(), $time, $websiteId);
  483. }
  484. return $this->_getResource()->getSendCount($this, $this->getRemoteAddr(), time() - $period, $websiteId);
  485. }
  486. /**
  487. * Register self in global register with name send_to_friend_model
  488. *
  489. * @return Mage_Sendfriend_Model_Sendfriend
  490. */
  491. public function register()
  492. {
  493. if (!Mage::registry('send_to_friend_model')) {
  494. Mage::register('send_to_friend_model', $this);
  495. }
  496. return $this;
  497. }
  498. /**
  499. * @deprecated after 1.3.2.4
  500. * use Mage_Sendfriend_Model_Sendfriend::_sentCountByCookies
  501. *
  502. * @return int
  503. */
  504. protected function _amountByCookies()
  505. {
  506. return $this->_sentCountByCookies(true);
  507. }
  508. /**
  509. * @deprecated after 1.3.2.4
  510. * use Mage_Sendfriend_Model_Sendfriend::_sentCountByIp
  511. *
  512. * @return int
  513. */
  514. protected function _amountByIp()
  515. {
  516. return $this->_sentCountByIp(true);
  517. }
  518. }