Subscriber.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Newsletter\Model;
  7. use Magento\Customer\Api\AccountManagementInterface;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Framework\Exception\MailException;
  10. use Magento\Framework\Exception\NoSuchEntityException;
  11. use Magento\Customer\Api\Data\CustomerInterfaceFactory;
  12. use Magento\Framework\Api\DataObjectHelper;
  13. use Magento\Framework\App\ObjectManager;
  14. /**
  15. * Subscriber model
  16. *
  17. * @method int getStoreId()
  18. * @method $this setStoreId(int $value)
  19. * @method string getChangeStatusAt()
  20. * @method $this setChangeStatusAt(string $value)
  21. * @method int getCustomerId()
  22. * @method $this setCustomerId(int $value)
  23. * @method string getSubscriberEmail()
  24. * @method $this setSubscriberEmail(string $value)
  25. * @method int getSubscriberStatus()
  26. * @method $this setSubscriberStatus(int $value)
  27. * @method string getSubscriberConfirmCode()
  28. * @method $this setSubscriberConfirmCode(string $value)
  29. * @method int getSubscriberId()
  30. * @method Subscriber setSubscriberId(int $value)
  31. *
  32. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  33. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  34. *
  35. * @api
  36. * @since 100.0.2
  37. */
  38. class Subscriber extends \Magento\Framework\Model\AbstractModel
  39. {
  40. const STATUS_SUBSCRIBED = 1;
  41. const STATUS_NOT_ACTIVE = 2;
  42. const STATUS_UNSUBSCRIBED = 3;
  43. const STATUS_UNCONFIRMED = 4;
  44. const XML_PATH_CONFIRM_EMAIL_TEMPLATE = 'newsletter/subscription/confirm_email_template';
  45. const XML_PATH_CONFIRM_EMAIL_IDENTITY = 'newsletter/subscription/confirm_email_identity';
  46. const XML_PATH_SUCCESS_EMAIL_TEMPLATE = 'newsletter/subscription/success_email_template';
  47. const XML_PATH_SUCCESS_EMAIL_IDENTITY = 'newsletter/subscription/success_email_identity';
  48. const XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE = 'newsletter/subscription/un_email_template';
  49. const XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY = 'newsletter/subscription/un_email_identity';
  50. const XML_PATH_CONFIRMATION_FLAG = 'newsletter/subscription/confirm';
  51. const XML_PATH_ALLOW_GUEST_SUBSCRIBE_FLAG = 'newsletter/subscription/allow_guest_subscribe';
  52. /**
  53. * Prefix of model events names
  54. *
  55. * @var string
  56. */
  57. protected $_eventPrefix = 'newsletter_subscriber';
  58. /**
  59. * Parameter name in event
  60. *
  61. * In observe method you can use $observer->getEvent()->getObject() in this case
  62. *
  63. * @var string
  64. */
  65. protected $_eventObject = 'subscriber';
  66. /**
  67. * True if data changed
  68. *
  69. * @var bool
  70. */
  71. protected $_isStatusChanged = false;
  72. /**
  73. * Newsletter data
  74. *
  75. * @var \Magento\Newsletter\Helper\Data
  76. */
  77. protected $_newsletterData = null;
  78. /**
  79. * Core store config
  80. *
  81. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  82. */
  83. protected $_scopeConfig;
  84. /**
  85. * Customer session
  86. *
  87. * @var \Magento\Customer\Model\Session
  88. */
  89. protected $_customerSession;
  90. /**
  91. * Date
  92. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  93. */
  94. private $dateTime;
  95. /**
  96. * Store manager
  97. *
  98. * @var \Magento\Store\Model\StoreManagerInterface
  99. */
  100. protected $_storeManager;
  101. /**
  102. * @var CustomerRepositoryInterface
  103. */
  104. protected $customerRepository;
  105. /**
  106. * @var AccountManagementInterface
  107. */
  108. protected $customerAccountManagement;
  109. /**
  110. * @var \Magento\Framework\Mail\Template\TransportBuilder
  111. */
  112. protected $_transportBuilder;
  113. /**
  114. * @var \Magento\Framework\Translate\Inline\StateInterface
  115. */
  116. protected $inlineTranslation;
  117. /**
  118. * @var CustomerInterfaceFactory
  119. */
  120. private $customerFactory;
  121. /**
  122. * @var DataObjectHelper
  123. */
  124. private $dataObjectHelper;
  125. /**
  126. * Initialize dependencies.
  127. *
  128. * @param \Magento\Framework\Model\Context $context
  129. * @param \Magento\Framework\Registry $registry
  130. * @param \Magento\Newsletter\Helper\Data $newsletterData
  131. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  132. * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
  133. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  134. * @param \Magento\Customer\Model\Session $customerSession
  135. * @param CustomerRepositoryInterface $customerRepository
  136. * @param AccountManagementInterface $customerAccountManagement
  137. * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
  138. * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
  139. * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
  140. * @param array $data
  141. * @param \Magento\Framework\Stdlib\DateTime\DateTime|null $dateTime
  142. * @param CustomerInterfaceFactory|null $customerFactory
  143. * @param DataObjectHelper|null $dataObjectHelper
  144. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  145. */
  146. public function __construct(
  147. \Magento\Framework\Model\Context $context,
  148. \Magento\Framework\Registry $registry,
  149. \Magento\Newsletter\Helper\Data $newsletterData,
  150. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  151. \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
  152. \Magento\Store\Model\StoreManagerInterface $storeManager,
  153. \Magento\Customer\Model\Session $customerSession,
  154. CustomerRepositoryInterface $customerRepository,
  155. AccountManagementInterface $customerAccountManagement,
  156. \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
  157. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  158. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  159. array $data = [],
  160. \Magento\Framework\Stdlib\DateTime\DateTime $dateTime = null,
  161. CustomerInterfaceFactory $customerFactory = null,
  162. DataObjectHelper $dataObjectHelper = null
  163. ) {
  164. $this->_newsletterData = $newsletterData;
  165. $this->_scopeConfig = $scopeConfig;
  166. $this->_transportBuilder = $transportBuilder;
  167. $this->_storeManager = $storeManager;
  168. $this->_customerSession = $customerSession;
  169. $this->dateTime = $dateTime ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
  170. \Magento\Framework\Stdlib\DateTime\DateTime::class
  171. );
  172. $this->customerFactory = $customerFactory ?: ObjectManager::getInstance()
  173. ->get(CustomerInterfaceFactory::class);
  174. $this->dataObjectHelper = $dataObjectHelper ?: ObjectManager::getInstance()
  175. ->get(DataObjectHelper::class);
  176. $this->customerRepository = $customerRepository;
  177. $this->customerAccountManagement = $customerAccountManagement;
  178. $this->inlineTranslation = $inlineTranslation;
  179. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  180. }
  181. /**
  182. * Initialize resource model
  183. *
  184. * @return void
  185. */
  186. protected function _construct()
  187. {
  188. $this->_init(\Magento\Newsletter\Model\ResourceModel\Subscriber::class);
  189. }
  190. /**
  191. * Alias for getSubscriberId()
  192. *
  193. * @return int
  194. */
  195. public function getId()
  196. {
  197. return $this->getSubscriberId();
  198. }
  199. /**
  200. * Alias for setSubscriberId()
  201. *
  202. * @param int $value
  203. * @return $this
  204. */
  205. public function setId($value)
  206. {
  207. return $this->setSubscriberId($value);
  208. }
  209. /**
  210. * Alias for getSubscriberConfirmCode()
  211. *
  212. * @return string
  213. */
  214. public function getCode()
  215. {
  216. return $this->getSubscriberConfirmCode();
  217. }
  218. /**
  219. * Return link for confirmation of subscription
  220. *
  221. * @return string
  222. */
  223. public function getConfirmationLink()
  224. {
  225. return $this->_newsletterData->getConfirmationUrl($this);
  226. }
  227. /**
  228. * Returns Unsubscribe url
  229. *
  230. * @return string
  231. */
  232. public function getUnsubscriptionLink()
  233. {
  234. return $this->_newsletterData->getUnsubscribeUrl($this);
  235. }
  236. /**
  237. * Alias for setSubscriberConfirmCode()
  238. *
  239. * @param string $value
  240. * @return $this
  241. */
  242. public function setCode($value)
  243. {
  244. return $this->setSubscriberConfirmCode($value);
  245. }
  246. /**
  247. * Alias for getSubscriberStatus()
  248. *
  249. * @return int
  250. */
  251. public function getStatus()
  252. {
  253. return $this->getSubscriberStatus();
  254. }
  255. /**
  256. * Alias for setSubscriberStatus()
  257. *
  258. * @param int $value
  259. * @return $this
  260. */
  261. public function setStatus($value)
  262. {
  263. return $this->setSubscriberStatus($value);
  264. }
  265. /**
  266. * Set the error messages scope for subscription
  267. *
  268. * @param boolean $scope
  269. * @return $this
  270. */
  271. public function setMessagesScope($scope)
  272. {
  273. $this->getResource()->setMessagesScope($scope);
  274. return $this;
  275. }
  276. /**
  277. * Alias for getSubscriberEmail()
  278. *
  279. * @return string
  280. */
  281. public function getEmail()
  282. {
  283. return $this->getSubscriberEmail();
  284. }
  285. /**
  286. * Alias for setSubscriberEmail()
  287. *
  288. * @param string $value
  289. * @return $this
  290. */
  291. public function setEmail($value)
  292. {
  293. return $this->setSubscriberEmail($value);
  294. }
  295. /**
  296. * Set for status change flag
  297. *
  298. * @param boolean $value
  299. * @return $this
  300. */
  301. public function setStatusChanged($value)
  302. {
  303. $this->_isStatusChanged = (boolean) $value;
  304. return $this;
  305. }
  306. /**
  307. * Return status change flag value
  308. *
  309. * @return boolean
  310. */
  311. public function isStatusChanged()
  312. {
  313. return $this->_isStatusChanged;
  314. }
  315. /**
  316. * Return customer subscription status
  317. *
  318. * @return bool
  319. */
  320. public function isSubscribed()
  321. {
  322. if ($this->getId() && $this->getStatus() == self::STATUS_SUBSCRIBED) {
  323. return true;
  324. }
  325. return false;
  326. }
  327. /**
  328. * Load subscriber data from resource model by email
  329. *
  330. * @param string $subscriberEmail
  331. * @return $this
  332. */
  333. public function loadByEmail($subscriberEmail)
  334. {
  335. $storeId = $this->_storeManager->getStore()->getId();
  336. $customerData = ['store_id' => $storeId, 'email'=> $subscriberEmail];
  337. /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
  338. $customer = $this->customerFactory->create();
  339. $this->dataObjectHelper->populateWithArray(
  340. $customer,
  341. $customerData,
  342. \Magento\Customer\Api\Data\CustomerInterface::class
  343. );
  344. $this->addData($this->getResource()->loadByCustomerData($customer));
  345. return $this;
  346. }
  347. /**
  348. * Load subscriber info by customerId
  349. *
  350. * @param int $customerId
  351. * @return $this
  352. */
  353. public function loadByCustomerId($customerId)
  354. {
  355. try {
  356. $customerData = $this->customerRepository->getById($customerId);
  357. $customerData->setStoreId($this->_storeManager->getStore()->getId());
  358. if ($customerData->getWebsiteId() === null) {
  359. $customerData->setWebsiteId($this->_storeManager->getStore()->getWebsiteId());
  360. }
  361. $data = $this->getResource()->loadByCustomerData($customerData);
  362. $this->addData($data);
  363. if (!empty($data) && $customerData->getId() && !$this->getCustomerId()) {
  364. $this->setCustomerId($customerData->getId());
  365. $this->setSubscriberConfirmCode($this->randomSequence());
  366. $this->save();
  367. }
  368. } catch (NoSuchEntityException $e) {
  369. }
  370. return $this;
  371. }
  372. /**
  373. * Returns string of random chars
  374. *
  375. * @param int $length
  376. * @return string
  377. */
  378. public function randomSequence($length = 32)
  379. {
  380. $id = '';
  381. $par = [];
  382. $char = array_merge(range('a', 'z'), range(0, 9));
  383. $charLen = count($char) - 1;
  384. for ($i = 0; $i < $length; $i++) {
  385. $disc = \Magento\Framework\Math\Random::getRandomNumber(0, $charLen);
  386. $par[$i] = $char[$disc];
  387. $id = $id . $char[$disc];
  388. }
  389. return $id;
  390. }
  391. /**
  392. * Subscribes by email
  393. *
  394. * @param string $email
  395. * @throws \Exception
  396. * @return int
  397. *
  398. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  399. * @SuppressWarnings(PHPMD.NPathComplexity)
  400. */
  401. public function subscribe($email)
  402. {
  403. $this->loadByEmail($email);
  404. if ($this->getId() && $this->getStatus() == self::STATUS_SUBSCRIBED) {
  405. return $this->getStatus();
  406. }
  407. if (!$this->getId()) {
  408. $this->setSubscriberConfirmCode($this->randomSequence());
  409. }
  410. $isConfirmNeed = $this->_scopeConfig->getValue(
  411. self::XML_PATH_CONFIRMATION_FLAG,
  412. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  413. ) == 1 ? true : false;
  414. $isSubscribeOwnEmail = $this->_customerSession->isLoggedIn()
  415. && $this->_customerSession->getCustomerDataObject()->getEmail() == $email;
  416. if (!$this->getId() || $this->getStatus() == self::STATUS_UNSUBSCRIBED
  417. || $this->getStatus() == self::STATUS_NOT_ACTIVE
  418. ) {
  419. if ($isConfirmNeed === true) {
  420. $this->setStatus(self::STATUS_NOT_ACTIVE);
  421. } else {
  422. $this->setStatus(self::STATUS_SUBSCRIBED);
  423. }
  424. $this->setSubscriberEmail($email);
  425. }
  426. if ($isSubscribeOwnEmail) {
  427. try {
  428. $customer = $this->customerRepository->getById($this->_customerSession->getCustomerId());
  429. $this->setStoreId($customer->getStoreId());
  430. $this->setCustomerId($customer->getId());
  431. } catch (NoSuchEntityException $e) {
  432. $this->setStoreId($this->_storeManager->getStore()->getId());
  433. $this->setCustomerId(0);
  434. }
  435. } else {
  436. $this->setStoreId($this->_storeManager->getStore()->getId());
  437. $this->setCustomerId(0);
  438. }
  439. $this->setStatusChanged(true);
  440. try {
  441. /* Save model before sending out email */
  442. $this->save();
  443. if ($isConfirmNeed === true) {
  444. $this->sendConfirmationRequestEmail();
  445. } else {
  446. $this->sendConfirmationSuccessEmail();
  447. }
  448. return $this->getStatus();
  449. } catch (\Exception $e) {
  450. throw new \Exception($e->getMessage());
  451. }
  452. }
  453. /**
  454. * Unsubscribes loaded subscription
  455. *
  456. * @throws \Magento\Framework\Exception\LocalizedException
  457. * @return $this
  458. */
  459. public function unsubscribe()
  460. {
  461. if ($this->hasCheckCode() && $this->getCode() != $this->getCheckCode()) {
  462. throw new \Magento\Framework\Exception\LocalizedException(
  463. __('This is an invalid subscription confirmation code.')
  464. );
  465. }
  466. if ($this->getSubscriberStatus() != self::STATUS_UNSUBSCRIBED) {
  467. $this->setSubscriberStatus(self::STATUS_UNSUBSCRIBED)->save();
  468. $this->sendUnsubscriptionEmail();
  469. }
  470. return $this;
  471. }
  472. /**
  473. * Subscribe the customer with the id provided
  474. *
  475. * @param int $customerId
  476. * @return $this
  477. */
  478. public function subscribeCustomerById($customerId)
  479. {
  480. return $this->_updateCustomerSubscription($customerId, true);
  481. }
  482. /**
  483. * Unsubscribe the customer with the id provided
  484. *
  485. * @param int $customerId
  486. * @return $this
  487. */
  488. public function unsubscribeCustomerById($customerId)
  489. {
  490. return $this->_updateCustomerSubscription($customerId, false);
  491. }
  492. /**
  493. * Update the subscription based on latest information of associated customer.
  494. *
  495. * @param int $customerId
  496. * @return $this
  497. */
  498. public function updateSubscription($customerId)
  499. {
  500. $this->loadByCustomerId($customerId);
  501. $this->_updateCustomerSubscription($customerId, $this->isSubscribed());
  502. return $this;
  503. }
  504. /**
  505. * Saving customer subscription status
  506. *
  507. * @param int $customerId
  508. * @param bool $subscribe indicates whether the customer should be subscribed or unsubscribed
  509. * @return $this
  510. *
  511. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  512. * @SuppressWarnings(PHPMD.NPathComplexity)
  513. */
  514. protected function _updateCustomerSubscription($customerId, $subscribe)
  515. {
  516. try {
  517. $customerData = $this->customerRepository->getById($customerId);
  518. } catch (NoSuchEntityException $e) {
  519. return $this;
  520. }
  521. $this->loadByCustomerId($customerId);
  522. if (!$subscribe && !$this->getId()) {
  523. return $this;
  524. }
  525. if (!$this->getId()) {
  526. $this->setSubscriberConfirmCode($this->randomSequence());
  527. }
  528. $sendInformationEmail = false;
  529. $status = self::STATUS_SUBSCRIBED;
  530. $isConfirmNeed = $this->_scopeConfig->getValue(
  531. self::XML_PATH_CONFIRMATION_FLAG,
  532. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  533. ) == 1 ? true : false;
  534. if ($subscribe) {
  535. if (AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED
  536. == $this->customerAccountManagement->getConfirmationStatus($customerId)
  537. ) {
  538. if ($this->getId() && $this->getStatus() == self::STATUS_SUBSCRIBED) {
  539. // if a customer was already subscribed then keep the subscribed
  540. $status = self::STATUS_SUBSCRIBED;
  541. } else {
  542. $status = self::STATUS_UNCONFIRMED;
  543. }
  544. } elseif ($isConfirmNeed) {
  545. if ($this->getStatus() != self::STATUS_SUBSCRIBED) {
  546. $status = self::STATUS_NOT_ACTIVE;
  547. }
  548. }
  549. } elseif (($this->getStatus() == self::STATUS_UNCONFIRMED) && ($customerData->getConfirmation() === null)) {
  550. $status = self::STATUS_SUBSCRIBED;
  551. $sendInformationEmail = true;
  552. } elseif (($this->getStatus() == self::STATUS_NOT_ACTIVE) && ($customerData->getConfirmation() === null)) {
  553. $status = self::STATUS_NOT_ACTIVE;
  554. } else {
  555. $status = self::STATUS_UNSUBSCRIBED;
  556. }
  557. /**
  558. * If subscription status has been changed then send email to the customer
  559. */
  560. if ($status != self::STATUS_UNCONFIRMED && $status != $this->getStatus()) {
  561. $sendInformationEmail = true;
  562. }
  563. if ($status != $this->getStatus()) {
  564. $this->setStatusChanged(true);
  565. }
  566. $this->setStatus($status);
  567. $storeId = $customerData->getStoreId();
  568. if ((int)$customerData->getStoreId() === 0) {
  569. $storeId = $this->_storeManager->getWebsite($customerData->getWebsiteId())->getDefaultStore()->getId();
  570. }
  571. if (!$this->getId()) {
  572. $this->setStoreId($storeId)
  573. ->setCustomerId($customerData->getId())
  574. ->setEmail($customerData->getEmail());
  575. } else {
  576. $this->setStoreId($storeId)
  577. ->setEmail($customerData->getEmail());
  578. }
  579. $this->save();
  580. $sendSubscription = $sendInformationEmail;
  581. if ($sendSubscription === null xor $sendSubscription && $this->isStatusChanged()) {
  582. try {
  583. switch ($status) {
  584. case self::STATUS_UNSUBSCRIBED:
  585. $this->sendUnsubscriptionEmail();
  586. break;
  587. case self::STATUS_SUBSCRIBED:
  588. $this->sendConfirmationSuccessEmail();
  589. break;
  590. case self::STATUS_NOT_ACTIVE:
  591. if ($isConfirmNeed) {
  592. $this->sendConfirmationRequestEmail();
  593. }
  594. break;
  595. }
  596. } catch (MailException $e) {
  597. // If we are not able to send a new account email, this should be ignored
  598. $this->_logger->critical($e);
  599. }
  600. }
  601. return $this;
  602. }
  603. /**
  604. * Confirms subscriber newsletter
  605. *
  606. * @param string $code
  607. * @return boolean
  608. */
  609. public function confirm($code)
  610. {
  611. if ($this->getCode() == $code) {
  612. $this->setStatus(self::STATUS_SUBSCRIBED)
  613. ->setStatusChanged(true)
  614. ->save();
  615. $this->sendConfirmationSuccessEmail();
  616. return true;
  617. }
  618. return false;
  619. }
  620. /**
  621. * Mark receiving subscriber of queue newsletter
  622. *
  623. * @param \Magento\Newsletter\Model\Queue $queue
  624. * @return boolean
  625. */
  626. public function received(\Magento\Newsletter\Model\Queue $queue)
  627. {
  628. $this->getResource()->received($this, $queue);
  629. return $this;
  630. }
  631. /**
  632. * Sends out confirmation email
  633. *
  634. * @return $this
  635. */
  636. public function sendConfirmationRequestEmail()
  637. {
  638. if ($this->getImportMode()) {
  639. return $this;
  640. }
  641. if (!$this->_scopeConfig->getValue(
  642. self::XML_PATH_CONFIRM_EMAIL_TEMPLATE,
  643. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  644. ) || !$this->_scopeConfig->getValue(
  645. self::XML_PATH_CONFIRM_EMAIL_IDENTITY,
  646. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  647. )
  648. ) {
  649. return $this;
  650. }
  651. $this->inlineTranslation->suspend();
  652. $this->_transportBuilder->setTemplateIdentifier(
  653. $this->_scopeConfig->getValue(
  654. self::XML_PATH_CONFIRM_EMAIL_TEMPLATE,
  655. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  656. )
  657. )->setTemplateOptions(
  658. [
  659. 'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
  660. 'store' => $this->_storeManager->getStore()->getId(),
  661. ]
  662. )->setTemplateVars(
  663. ['subscriber' => $this, 'store' => $this->_storeManager->getStore()]
  664. )->setFrom(
  665. $this->_scopeConfig->getValue(
  666. self::XML_PATH_CONFIRM_EMAIL_IDENTITY,
  667. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  668. )
  669. )->addTo(
  670. $this->getEmail(),
  671. $this->getName()
  672. );
  673. $transport = $this->_transportBuilder->getTransport();
  674. $transport->sendMessage();
  675. $this->inlineTranslation->resume();
  676. return $this;
  677. }
  678. /**
  679. * Sends out confirmation success email
  680. *
  681. * @return $this
  682. */
  683. public function sendConfirmationSuccessEmail()
  684. {
  685. if ($this->getImportMode()) {
  686. return $this;
  687. }
  688. if (!$this->_scopeConfig->getValue(
  689. self::XML_PATH_SUCCESS_EMAIL_TEMPLATE,
  690. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  691. ) || !$this->_scopeConfig->getValue(
  692. self::XML_PATH_SUCCESS_EMAIL_IDENTITY,
  693. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  694. )
  695. ) {
  696. return $this;
  697. }
  698. $this->inlineTranslation->suspend();
  699. $this->_transportBuilder->setTemplateIdentifier(
  700. $this->_scopeConfig->getValue(
  701. self::XML_PATH_SUCCESS_EMAIL_TEMPLATE,
  702. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  703. )
  704. )->setTemplateOptions(
  705. [
  706. 'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
  707. 'store' => $this->_storeManager->getStore()->getId(),
  708. ]
  709. )->setTemplateVars(
  710. ['subscriber' => $this]
  711. )->setFrom(
  712. $this->_scopeConfig->getValue(
  713. self::XML_PATH_SUCCESS_EMAIL_IDENTITY,
  714. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  715. )
  716. )->addTo(
  717. $this->getEmail(),
  718. $this->getName()
  719. );
  720. $transport = $this->_transportBuilder->getTransport();
  721. $transport->sendMessage();
  722. $this->inlineTranslation->resume();
  723. return $this;
  724. }
  725. /**
  726. * Sends out unsubscription email
  727. *
  728. * @return $this
  729. */
  730. public function sendUnsubscriptionEmail()
  731. {
  732. if ($this->getImportMode()) {
  733. return $this;
  734. }
  735. if (!$this->_scopeConfig->getValue(
  736. self::XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE,
  737. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  738. ) || !$this->_scopeConfig->getValue(
  739. self::XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY,
  740. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  741. )
  742. ) {
  743. return $this;
  744. }
  745. $this->inlineTranslation->suspend();
  746. $this->_transportBuilder->setTemplateIdentifier(
  747. $this->_scopeConfig->getValue(
  748. self::XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE,
  749. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  750. )
  751. )->setTemplateOptions(
  752. [
  753. 'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
  754. 'store' => $this->_storeManager->getStore()->getId(),
  755. ]
  756. )->setTemplateVars(
  757. ['subscriber' => $this]
  758. )->setFrom(
  759. $this->_scopeConfig->getValue(
  760. self::XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY,
  761. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  762. )
  763. )->addTo(
  764. $this->getEmail(),
  765. $this->getName()
  766. );
  767. $transport = $this->_transportBuilder->getTransport();
  768. $transport->sendMessage();
  769. $this->inlineTranslation->resume();
  770. return $this;
  771. }
  772. /**
  773. * Retrieve Subscribers Full Name if it was set
  774. *
  775. * @return string|null
  776. */
  777. public function getSubscriberFullName()
  778. {
  779. $name = null;
  780. if ($this->hasFirstname() || $this->hasLastname()) {
  781. $name = $this->getFirstname() . ' ' . $this->getLastname();
  782. }
  783. return $name;
  784. }
  785. /**
  786. * Set date of last changed status
  787. *
  788. * @return $this
  789. * @since 100.2.1
  790. */
  791. public function beforeSave()
  792. {
  793. parent::beforeSave();
  794. if ($this->dataHasChangedFor('subscriber_status')) {
  795. $this->setChangeStatusAt($this->dateTime->gmtDate());
  796. }
  797. return $this;
  798. }
  799. }