Consent.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model;
  3. class Consent extends \Magento\Framework\Model\AbstractModel
  4. {
  5. const CONSENT_TEXT_LIMIT = '1000';
  6. /**
  7. * Single fields for the consent contact.
  8. *
  9. * @var array
  10. */
  11. public $singleFields = [
  12. 'DATETIMECONSENTED',
  13. 'URL',
  14. 'USERAGENT',
  15. 'IPADDRESS'
  16. ];
  17. /**
  18. * Bulk api import for consent contact fields.
  19. *
  20. * @var array
  21. */
  22. static public $bulkFields = [
  23. 'CONSENTTEXT',
  24. 'CONSENTURL',
  25. 'CONSENTDATETIME',
  26. 'CONSENTIP',
  27. 'CONSENTUSERAGENT'
  28. ];
  29. /**
  30. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  31. */
  32. private $dateTime;
  33. /**
  34. * @var ResourceModel\Consent
  35. */
  36. private $consentResource;
  37. /**
  38. * @var ResourceModel\Contact\CollectionFactory
  39. */
  40. private $contactCollectionFacotry;
  41. /**
  42. * @var \Dotdigitalgroup\Email\Helper\Config
  43. */
  44. private $configHelper;
  45. /**
  46. * @var array
  47. */
  48. private $consentText = [];
  49. /**
  50. * @var array
  51. */
  52. private $customerConsentText = [];
  53. /**
  54. * Constructor.
  55. *
  56. * @return null
  57. */
  58. public function _construct()
  59. {
  60. $this->_init(\Dotdigitalgroup\Email\Model\ResourceModel\Consent::class);
  61. }
  62. /**
  63. * Consent constructor.
  64. * @param \Magento\Framework\Model\Context $context
  65. * @param \Magento\Framework\Registry $registry
  66. * @param \Dotdigitalgroup\Email\Helper\Config $config
  67. * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
  68. * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
  69. * @param array $data
  70. */
  71. public function __construct(
  72. \Magento\Framework\Model\Context $context,
  73. \Magento\Framework\Registry $registry,
  74. \Dotdigitalgroup\Email\Helper\Config $config,
  75. \Dotdigitalgroup\Email\Model\ResourceModel\Consent $consent,
  76. \Dotdigitalgroup\Email\Model\ResourceModel\Contact\CollectionFactory $contactCollectionFactory,
  77. \Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
  78. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  79. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  80. array $data = []
  81. ) {
  82. $this->dateTime = $dateTime;
  83. $this->configHelper = $config;
  84. $this->consentResource = $consent;
  85. $this->contactCollectionFacotry = $contactCollectionFactory;
  86. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  87. }
  88. /**
  89. * @param int $websiteId
  90. * @param string $email
  91. * @return array
  92. * @throws \Magento\Framework\Exception\LocalizedException
  93. */
  94. public function getConsentDataByContact($websiteId, $email)
  95. {
  96. if (! $this->configHelper->isConsentSubscriberEnabled($websiteId)) {
  97. return [];
  98. }
  99. $this->checkModelLoaded($websiteId, $email);
  100. $consentText = $this->getConsentText($websiteId);
  101. $consentDatetime = $this->dateTime->date(\Zend_Date::ISO_8601, $this->getConsentDatetime());
  102. return [
  103. $consentText,
  104. $this->getConsentUrl(),
  105. $consentDatetime,
  106. $this->getConsentIp(),
  107. $this->getConsentUserAgent()
  108. ];
  109. }
  110. /**
  111. * @param int $websiteId
  112. * @param string $email
  113. *
  114. * @return array
  115. */
  116. public function getFormattedConsentDataByContactForApi($websiteId, $email)
  117. {
  118. if (! $this->configHelper->isConsentSubscriberEnabled($websiteId)) {
  119. return [];
  120. }
  121. $this->checkModelLoaded($websiteId, $email);
  122. $consentText = $this->getConsentText($websiteId);
  123. $consentDatetime = $this->dateTime->date(\Zend_Date::ISO_8601, $this->getConsentDatetime());
  124. return [
  125. ['key' => 'TEXT', 'value' => $consentText],
  126. ['key' => 'URL', 'value' => $this->getConsentUrl()],
  127. ['key' => 'DATETIMECONSENTED', 'value' => $consentDatetime],
  128. ['key' => 'IPADDRESS', 'value' => $this->getConsentIp()],
  129. ['key' => 'USERAGENT', 'value' => $this->getConsentUserAgent()]
  130. ];
  131. }
  132. /**
  133. * @param string $consentUrl
  134. * @param int $websiteId
  135. *
  136. * @return string|boolean
  137. */
  138. public function getConsentTextForWebsite($consentUrl, $websiteId)
  139. {
  140. if (! isset($this->consentText[$websiteId])) {
  141. $this->consentText[$websiteId] = $this->configHelper->getConsentSubscriberText($websiteId);
  142. }
  143. $consentText = $this->consentText[$websiteId];
  144. if (! isset($this->customerConsentText[$websiteId])) {
  145. $this->customerConsentText[$websiteId] = $this->configHelper->getConsentCustomerText($websiteId);
  146. }
  147. $customerConsentText = $this->customerConsentText[$websiteId];
  148. //customer checkout and registration if consent text not empty
  149. if ($this->isLinkMatchCustomerRegistrationOrCheckout($consentUrl) && strlen($customerConsentText)) {
  150. $consentText = $customerConsentText;
  151. }
  152. return $consentText;
  153. }
  154. /**
  155. * @param string $consentUrl
  156. * @return bool
  157. */
  158. private function isLinkMatchCustomerRegistrationOrCheckout($consentUrl)
  159. {
  160. if (strpos($consentUrl, 'checkout/') !== false || strpos($consentUrl, 'customer/account/') !== false) {
  161. return true;
  162. }
  163. return false;
  164. }
  165. /**
  166. * @param int $websiteId
  167. * @param string $email
  168. */
  169. private function checkModelLoaded($websiteId, $email)
  170. {
  171. //model not loaded try to load with contact email data
  172. if (!$this->getId()) {
  173. //load model using email and website id
  174. $contactModel = $this->contactCollectionFacotry->create()
  175. ->loadByCustomerEmail($email, $websiteId);
  176. if ($contactModel) {
  177. $this->consentResource->load($this, $contactModel->getEmailContactId(), 'email_contact_id');
  178. }
  179. }
  180. }
  181. /**
  182. * @param int $websiteId
  183. *
  184. * @return false|string
  185. */
  186. private function getConsentText($websiteId)
  187. {
  188. $consentText = $this->configHelper->getConsentSubscriberText($websiteId);
  189. $customerConsentText = $this->configHelper->getConsentCustomerText($websiteId);
  190. //customer checkout and registration if consent text not empty
  191. if ($this->isLinkMatchCustomerRegistrationOrCheckout($this->getConsentUrl()) && strlen($customerConsentText)) {
  192. $consentText = $customerConsentText;
  193. }
  194. return $consentText;
  195. }
  196. }