PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Newsletter/Model/Subscriber.php

https://github.com/lboaretto/magento-pt_br
PHP | 535 lines | 307 code | 71 blank | 157 comment | 55 complexity | d0e96c9ae8a5c30d9d985cbacb5bd48c MD5 | raw file
Possible License(s): GPL-2.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_Newsletter
  23. * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Subscriber model
  28. *
  29. * @category Mage
  30. * @package Mage_Newsletter
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Newsletter_Model_Subscriber extends Varien_Object
  34. {
  35. const STATUS_SUBSCRIBED = 1;
  36. const STATUS_NOT_ACTIVE = 2;
  37. const STATUS_UNSUBSCRIBED = 3;
  38. const XML_PATH_CONFIRM_EMAIL_TEMPLATE = 'newsletter/subscription/confirm_email_template';
  39. const XML_PATH_CONFIRM_EMAIL_IDENTITY = 'newsletter/subscription/confirm_email_identity';
  40. const XML_PATH_SUCCESS_EMAIL_TEMPLATE = 'newsletter/subscription/success_email_template';
  41. const XML_PATH_SUCCESS_EMAIL_IDENTITY = 'newsletter/subscription/success_email_identity';
  42. const XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE = 'newsletter/subscription/un_email_template';
  43. const XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY = 'newsletter/subscription/un_email_identity';
  44. const XML_PATH_CONFIRMATION_FLAG = 'newsletter/subscription/confirm';
  45. const XML_PATH_SENDING_SET_RETURN_PATH = 'newsletter/sending/set_return_path';
  46. protected $_isStatusChanged = false;
  47. /**
  48. * Alias for getSubscriberId()
  49. *
  50. * @return int
  51. */
  52. public function getId()
  53. {
  54. return $this->getSubscriberId();
  55. }
  56. /**
  57. * Alias for setSubscriberId()
  58. *
  59. * @param int $value
  60. */
  61. public function setId($value)
  62. {
  63. return $this->setSubscriberId($value);
  64. }
  65. /**
  66. * Alias for getSubscriberConfirmCode()
  67. *
  68. * @return string
  69. */
  70. public function getCode()
  71. {
  72. return $this->getSubscriberConfirmCode();
  73. }
  74. /**
  75. * Return link for confirmation of subscription
  76. *
  77. * @return string
  78. */
  79. public function getConfirmationLink() {
  80. return Mage::helper('newsletter')->getConfirmationUrl($this);
  81. }
  82. public function getUnsubscriptionLink() {
  83. return Mage::helper('newsletter')->getUnsubscribeUrl($this);
  84. }
  85. /**
  86. * Alias for setSubscriberConfirmCode()
  87. *
  88. * @param string $value
  89. */
  90. public function setCode($value)
  91. {
  92. return $this->setSubscriberConfirmCode($value);
  93. }
  94. /**
  95. * Alias for getSubscriberStatus()
  96. *
  97. * @return int
  98. */
  99. public function getStatus()
  100. {
  101. return $this->getSubscriberStatus();
  102. }
  103. /**
  104. * Alias for setSubscriberStatus()
  105. *
  106. * @param int
  107. */
  108. public function setStatus($value)
  109. {
  110. return $this->setSubscriberStatus($value);
  111. }
  112. /**
  113. * Set the error messages scope for subscription
  114. *
  115. * @param boolean $scope
  116. * @return unknown
  117. */
  118. public function setMessagesScope($scope)
  119. {
  120. $this->getResource()->setMessagesScope($scope);
  121. return $this;
  122. }
  123. /**
  124. * Alias for getSubscriberEmail()
  125. *
  126. * @return string
  127. */
  128. public function getEmail()
  129. {
  130. return $this->getSubscriberEmail();
  131. }
  132. /**
  133. * Alias for setSubscriberEmail()
  134. *
  135. * @param string $value
  136. */
  137. public function setEmail($value)
  138. {
  139. return $this->setSubscriberEmail($value);
  140. }
  141. /**
  142. * Set for status change flag
  143. *
  144. * @param boolean $value
  145. */
  146. public function setIsStatusChanged($value)
  147. {
  148. $this->_isStatusChanged = (boolean) $value;
  149. return $this;
  150. }
  151. /**
  152. * Return status change flag value
  153. *
  154. * @return boolean
  155. */
  156. public function getIsStatusChanged()
  157. {
  158. return $this->_isStatusChanged;
  159. }
  160. /**
  161. * Return customer subscription status
  162. *
  163. * @return bool
  164. */
  165. public function isSubscribed()
  166. {
  167. if($this->getId() && $this->getStatus()==self::STATUS_SUBSCRIBED) {
  168. return true;
  169. }
  170. return false;
  171. }
  172. /**
  173. * Return resource model
  174. *
  175. * @return Mage_Subscriber_Model_Mysql4_Subscriber
  176. */
  177. public function getResource()
  178. {
  179. return Mage::getResourceSingleton('newsletter/subscriber');
  180. }
  181. /**
  182. * Load subscriber data from resource model
  183. *
  184. * @param int $subscriberId
  185. */
  186. public function load($subscriberId)
  187. {
  188. $this->addData($this->getResource()->load($subscriberId));
  189. return $this;
  190. }
  191. /**
  192. * Load subscriber data from resource model by email
  193. *
  194. * @param int $subscriberId
  195. */
  196. public function loadByEmail($subscriberEmail)
  197. {
  198. $this->addData($this->getResource()->loadByEmail($subscriberEmail));
  199. return $this;
  200. }
  201. /**
  202. * Load subscriber info by customer
  203. *
  204. * @param Mage_Customer_Model_Customer $customer
  205. * @return Mage_Newsletter_Model_Subscriber
  206. */
  207. public function loadByCustomer(Mage_Customer_Model_Customer $customer)
  208. {
  209. $data = $this->getResource()->loadByCustomer($customer);
  210. $this->addData($data);
  211. if (!empty($data) && $customer->getId() && !$this->getCustomerId()) {
  212. $this->setCustomerId($customer->getId());
  213. $this->setSubscriberConfirmCode($this->randomSequence());
  214. if ($this->getStatus()==self::STATUS_NOT_ACTIVE) {
  215. $this->setStatus($customer->getIsSubscribed() ? self::STATUS_SUBSCRIBED : self::STATUS_UNSUBSCRIBED);
  216. }
  217. $this->save();
  218. }
  219. return $this;
  220. }
  221. /**
  222. * Save subscriber data to resource model
  223. *
  224. */
  225. public function save()
  226. {
  227. return $this->getResource()->save($this);
  228. }
  229. /**
  230. * Deletes subscriber data
  231. */
  232. public function delete()
  233. {
  234. $this->getResource()->delete($this->getId());
  235. $this->setId(null);
  236. }
  237. public function randomSequence($length=32)
  238. {
  239. $id = '';
  240. $par = array();
  241. $char = array_merge(range('a','z'),range(0,9));
  242. $charLen = count($char)-1;
  243. for ($i=0;$i<$length;$i++){
  244. $disc = mt_rand(0, $charLen);
  245. $par[$i] = $char[$disc];
  246. $id = $id.$char[$disc];
  247. }
  248. return $id;
  249. }
  250. public function subscribe($email)
  251. {
  252. $this->loadByEmail($email);
  253. $customer = Mage::getModel('customer/customer')
  254. ->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
  255. ->loadByEmail($email);
  256. $isNewSubscriber = false;
  257. $customerSession = Mage::getSingleton('customer/session');
  258. if(!$this->getId()) {
  259. $this->setSubscriberConfirmCode($this->randomSequence());
  260. }
  261. // if(($this->getCustomerId() && !$customerSession->isLoggedIn())
  262. // || ($this->getCustomerId()
  263. // && $customerSession->getCustomerId() != $this->getCustomerId()
  264. // )) {
  265. // return $this->getSubscriberStatus();
  266. // }
  267. if (!$this->getId() || $this->getStatus()==self::STATUS_UNSUBSCRIBED || $this->getStatus()==self::STATUS_NOT_ACTIVE) {
  268. if (Mage::getStoreConfig(self::XML_PATH_CONFIRMATION_FLAG) == 1) {
  269. $this->setStatus(self::STATUS_NOT_ACTIVE);
  270. } else {
  271. $this->setStatus(self::STATUS_SUBSCRIBED);
  272. }
  273. $this->setSubscriberEmail($email);
  274. }
  275. if ($customerSession->isLoggedIn()) {
  276. $this->setStoreId($customerSession->getCustomer()->getStoreId());
  277. $this->setStatus(self::STATUS_SUBSCRIBED);
  278. $this->setCustomerId($customerSession->getCustomerId());
  279. } else if ($customer->getId()) {
  280. $this->setStoreId($customer->getStoreId());
  281. $this->setSubscriberStatus(self::STATUS_SUBSCRIBED);
  282. $this->setCustomerId($customer->getId());
  283. } else {
  284. $this->setStoreId(Mage::app()->getStore()->getId());
  285. $this->setCustomerId(0);
  286. $isNewSubscriber = true;
  287. }
  288. $this->setIsStatusChanged(true);
  289. try {
  290. $this->save();
  291. if (Mage::getStoreConfig(self::XML_PATH_CONFIRMATION_FLAG) == 1
  292. && $this->getSubscriberStatus()==self::STATUS_NOT_ACTIVE) {
  293. $this->sendConfirmationRequestEmail();
  294. } else {
  295. $this->sendConfirmationSuccessEmail();
  296. }
  297. return $this->getStatus();
  298. } catch (Exception $e) {
  299. throw new Exception($e->getMessage());
  300. }
  301. }
  302. public function unsubscribe()
  303. {
  304. if ($this->hasCheckCode() && $this->getCode() != $this->getCheckCode()) {
  305. Mage::throwException(Mage::helper('newsletter')->__('Invalid subscription confirmation code'));
  306. }
  307. $this->setSubscriberStatus(self::STATUS_UNSUBSCRIBED)
  308. ->save();
  309. $this->sendUnsubscriptionEmail();
  310. return $this;
  311. }
  312. /**
  313. * Saving customer cubscription status
  314. *
  315. * @param Mage_Customer_Model_Customer $customer
  316. * @return Mage_Newsletter_Model_Subscriber
  317. */
  318. public function subscribeCustomer($customer)
  319. {
  320. $this->loadByCustomer($customer);
  321. if ($customer->getImportMode()) {
  322. $this->setImportMode(true);
  323. }
  324. if (!$customer->getIsSubscribed() && !$this->getId()) {
  325. // If subscription flag not seted or customer not subscriber
  326. // and no subscribe bellow
  327. return $this;
  328. }
  329. if(!$this->getId()) {
  330. $this->setSubscriberConfirmCode($this->randomSequence());
  331. }
  332. if($customer->hasIsSubscribed()) {
  333. $status = $customer->getIsSubscribed() ? self::STATUS_SUBSCRIBED : self::STATUS_UNSUBSCRIBED;
  334. } else {
  335. $status = ($this->getStatus() == self::STATUS_NOT_ACTIVE ? self::STATUS_UNSUBSCRIBED : $this->getStatus());
  336. }
  337. if($status != $this->getStatus()) {
  338. $this->setIsStatusChanged(true);
  339. }
  340. $this->setStatus($status);
  341. if(!$this->getId()) {
  342. $this->setStoreId($customer->getStoreId())
  343. ->setCustomerId($customer->getId())
  344. ->setEmail($customer->getEmail());
  345. } else {
  346. $this->setEmail($customer->getEmail());
  347. }
  348. $this->save();
  349. $sendSubscription = $customer->getData('sendSubscription');
  350. if (is_null($sendSubscription) xor $sendSubscription) {
  351. if ($this->getIsStatusChanged() && $status == self::STATUS_UNSUBSCRIBED) {
  352. $this->sendUnsubscriptionEmail();
  353. } elseif ($this->getIsStatusChanged() && $status == self::STATUS_SUBSCRIBED) {
  354. $this->sendConfirmationSuccessEmail();
  355. }
  356. }
  357. return $this;
  358. }
  359. /**
  360. * Confirms subscriber newsletter
  361. *
  362. * @param string $code
  363. * @return boolean
  364. */
  365. public function confirm($code)
  366. {
  367. if($this->getCode()==$code) {
  368. $this->setStatus(self::STATUS_SUBSCRIBED)
  369. ->setIsStatusChanged(true)
  370. ->save();
  371. return true;
  372. }
  373. return false;
  374. }
  375. /**
  376. * Mark receiving subscriber of queue newsletter
  377. *
  378. * @param Mage_Newsletter_Model_Queue $queue
  379. * @return boolean
  380. */
  381. public function received(Mage_Newsletter_Model_Queue $queue)
  382. {
  383. $this->getResource()->received($this,$queue);
  384. return $this;
  385. }
  386. public function sendConfirmationRequestEmail()
  387. {
  388. if ($this->getImportMode()) {
  389. return $this;
  390. }
  391. if(!Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_TEMPLATE) || !Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_IDENTITY)) {
  392. return $this;
  393. }
  394. $translate = Mage::getSingleton('core/translate');
  395. /* @var $translate Mage_Core_Model_Translate */
  396. $translate->setTranslateInline(false);
  397. $email = Mage::getModel('core/email_template');
  398. /* @var $email Mage_Core_Model_Email_Template */
  399. if (Mage::getStoreConfigFlag(self::XML_PATH_SENDING_SET_RETURN_PATH)) {
  400. $email->setReturnPath(Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_IDENTITY));
  401. }
  402. $email->sendTransactional(
  403. Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_TEMPLATE),
  404. Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_IDENTITY),
  405. $this->getEmail(),
  406. $this->getName(),
  407. array('subscriber'=>$this)
  408. );
  409. $translate->setTranslateInline(true);
  410. return $this;
  411. }
  412. public function sendConfirmationSuccessEmail()
  413. {
  414. if ($this->getImportMode()) {
  415. return $this;
  416. }
  417. if(!Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_TEMPLATE) || !Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_IDENTITY)) {
  418. return $this;
  419. }
  420. $translate = Mage::getSingleton('core/translate');
  421. /* @var $translate Mage_Core_Model_Translate */
  422. $translate->setTranslateInline(false);
  423. $email = Mage::getModel('core/email_template');
  424. /* @var $email Mage_Core_Model_Email_Template */
  425. if (Mage::getStoreConfigFlag(self::XML_PATH_SENDING_SET_RETURN_PATH)) {
  426. $email->setReturnPath(Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_IDENTITY));
  427. }
  428. $email->sendTransactional(
  429. Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_TEMPLATE),
  430. Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_IDENTITY),
  431. $this->getEmail(),
  432. $this->getName(),
  433. array('subscriber'=>$this)
  434. );
  435. $translate->setTranslateInline(true);
  436. return $this;
  437. }
  438. public function sendUnsubscriptionEmail()
  439. {
  440. if ($this->getImportMode()) {
  441. return $this;
  442. }
  443. if(!Mage::getStoreConfig(self::XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE) || !Mage::getStoreConfig(self::XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY)) {
  444. return $this;
  445. }
  446. $translate = Mage::getSingleton('core/translate');
  447. /* @var $translate Mage_Core_Model_Translate */
  448. $translate->setTranslateInline(false);
  449. $email = Mage::getModel('core/email_template');
  450. /* @var $email Mage_Core_Model_Email_Template */
  451. if (Mage::getStoreConfigFlag(self::XML_PATH_SENDING_SET_RETURN_PATH)) {
  452. $email->setReturnPath(Mage::getStoreConfig(self::XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY));
  453. }
  454. $email->sendTransactional(
  455. Mage::getStoreConfig(self::XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE),
  456. Mage::getStoreConfig(self::XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY),
  457. $this->getEmail(),
  458. $this->getName(),
  459. array('subscriber'=>$this)
  460. );
  461. $translate->setTranslateInline(true);
  462. return $this;
  463. }
  464. }