PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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