Books.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. namespace Dotdigitalgroup\Email\Block\Customer\Account;
  3. /**
  4. * Books block
  5. *
  6. * @api
  7. */
  8. class Books extends \Magento\Framework\View\Element\Template
  9. {
  10. /**
  11. * Apiconnector client.
  12. *
  13. * @var \Dotdigitalgroup\Email\Model\Apiconnector\Client
  14. */
  15. private $client;
  16. /**
  17. * @var \Dotdigitalgroup\Email\Helper\Data
  18. */
  19. public $helper;
  20. /**
  21. * @var \Magento\Customer\Model\Session
  22. */
  23. public $customerSession;
  24. /**
  25. * @var \Magento\Newsletter\Model\SubscriberFactory
  26. */
  27. private $subscriberFactory;
  28. /**
  29. * @var object
  30. */
  31. private $contactFromAccount;
  32. /**
  33. * @var \Dotdigitalgroup\Email\Model\Contact
  34. */
  35. private $contactFromTable;
  36. /**
  37. * Books constructor.
  38. *
  39. * @param \Magento\Framework\View\Element\Template\Context $context
  40. * @param \Dotdigitalgroup\Email\Helper\Data $helper
  41. * @param \Magento\Customer\Model\Session $customerSession
  42. * @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
  43. * @param array $data
  44. */
  45. public function __construct(
  46. \Magento\Framework\View\Element\Template\Context $context,
  47. \Dotdigitalgroup\Email\Helper\Data $helper,
  48. \Magento\Customer\Model\Session $customerSession,
  49. \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
  50. array $data = []
  51. ) {
  52. $this->helper = $helper;
  53. $this->customerSession = $customerSession;
  54. $this->subscriberFactory = $subscriberFactory;
  55. parent::__construct($context, $data);
  56. }
  57. /**
  58. * @return string
  59. */
  60. public function getCustomerConsentText()
  61. {
  62. return $this->helper->configHelperFactory->create()
  63. ->getConsentCustomerText($this->getCustomer()->getWebsiteId());
  64. }
  65. /**
  66. * @return \Magento\Customer\Model\Customer
  67. */
  68. public function getCustomer()
  69. {
  70. return $this->customerSession->getCustomer();
  71. }
  72. /**
  73. * Subscription pref save url.
  74. *
  75. * @return string
  76. */
  77. public function getSaveUrl()
  78. {
  79. return $this->getUrl('connector/customer/newsletter');
  80. }
  81. /**
  82. * Get config values.
  83. *
  84. * @param string $path
  85. * @param int $website
  86. *
  87. * @return string|boolean
  88. */
  89. public function _getWebsiteConfigFromHelper($path, $website)
  90. {
  91. return $this->helper->getWebsiteConfig($path, $website);
  92. }
  93. /**
  94. * Get api client.
  95. *
  96. * @return \Dotdigitalgroup\Email\Model\Apiconnector\Client
  97. */
  98. private function _getApiClient()
  99. {
  100. if (empty($this->client)) {
  101. $website = $this->getCustomer()->getStore()->getWebsite();
  102. $client = $this->helper->getWebsiteApiClient($website);
  103. $this->client = $client;
  104. }
  105. return $this->client;
  106. }
  107. /**
  108. * Can show additional books?
  109. *
  110. * @return string|boolean
  111. */
  112. public function getCanShowAdditionalBooks()
  113. {
  114. return $this->helper->getCanShowAdditionalSubscriptions(
  115. $this->getCustomer()->getStore()->getWebsite()
  116. );
  117. }
  118. /**
  119. * Getter for additional books. Fully processed.
  120. *
  121. * @return array
  122. */
  123. public function getAdditionalBooksToShow()
  124. {
  125. $additionalBooksToShow = [];
  126. $processedAddressBooks = [];
  127. $additionalFromConfig = $this->helper->getAddressBookIdsToShow($this->getCustomer()->getStore()->getWebsite());
  128. $contactFromTable = $this->getContactFromTable();
  129. if (! empty($additionalFromConfig) && $contactFromTable->getContactId()) {
  130. $contact = $this->getConnectorContact();
  131. if (isset($contact->id) && $contact->status !== 'PendingOptIn') {
  132. $addressBooks = $this->_getApiClient()
  133. ->getContactAddressBooks(
  134. $contact->id
  135. );
  136. if (is_array($addressBooks)) {
  137. foreach ($addressBooks as $addressBook) {
  138. $processedAddressBooks[$addressBook->id]
  139. = $addressBook->name;
  140. }
  141. }
  142. }
  143. }
  144. return $this->getProcessedAdditionalBooks(
  145. $additionalFromConfig,
  146. $processedAddressBooks,
  147. $additionalBooksToShow
  148. );
  149. }
  150. /**
  151. * Can show data fields?
  152. *
  153. * @return string|boolean
  154. */
  155. public function getCanShowDataFields()
  156. {
  157. return $this->helper->getCanShowDataFields(
  158. $this->getCustomer()->getStore()->getWebsite()
  159. );
  160. }
  161. /**
  162. * Getter for datafields to show. Fully processed.
  163. *
  164. * @return array
  165. */
  166. public function getDataFieldsToShow()
  167. {
  168. $datafieldsToShow = [];
  169. $website = $this->getCustomer()->getStore()->getWebsite();
  170. $dataFieldsFromConfig = $this->_getWebsiteConfigFromHelper(
  171. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_ADDRESSBOOK_PREF_SHOW_FIELDS,
  172. $website
  173. );
  174. if (empty($dataFieldsFromConfig)) {
  175. return $datafieldsToShow;
  176. }
  177. $processedContactDataFields = [];
  178. $processedConnectorDataFields = [];
  179. $contactFromTable = $this->getContactFromTable();
  180. $dataFieldsFromConfig = explode(',', $dataFieldsFromConfig);
  181. if ($contactFromTable->getContactId()) {
  182. $contact = $this->getConnectorContact();
  183. if (isset($contact->id)) {
  184. $contactDataFields = $contact->dataFields;
  185. foreach ($contactDataFields as $contactDataField) {
  186. $processedContactDataFields[$contactDataField->key]
  187. = $contactDataField->value;
  188. }
  189. }
  190. }
  191. return $this->getProcessedDataFieldsToShow(
  192. $processedConnectorDataFields,
  193. $dataFieldsFromConfig,
  194. $processedContactDataFields,
  195. $datafieldsToShow
  196. );
  197. }
  198. /**
  199. * Find out if anything is true.
  200. *
  201. * @return bool
  202. */
  203. public function canShowAnything()
  204. {
  205. if (! $this->isCustomerSubscriber() || ! $this->helper->isEnabled($this->getCustomer()->getWebsiteId())) {
  206. return false;
  207. }
  208. $showPreferences = $this->getCanShowPreferences();
  209. $books = $this->getAdditionalBooksToShow();
  210. $fields = $this->getDataFieldsToShow();
  211. if ($books || $fields || $showPreferences) {
  212. if (! empty($books) || ! empty($fields) || $showPreferences) {
  213. return true;
  214. }
  215. }
  216. return false;
  217. }
  218. /**
  219. * @return bool
  220. */
  221. private function isCustomerSubscriber()
  222. {
  223. return $this->subscriberFactory->create()
  224. ->loadByCustomerId($this->getCustomer()->getId())
  225. ->isSubscribed();
  226. }
  227. /**
  228. * Get connector contact.
  229. *
  230. * @return object
  231. */
  232. public function getConnectorContact()
  233. {
  234. if (! isset($this->contactFromAccount)) {
  235. $contact = $this->_getApiClient()->getContactByEmail(
  236. $this->getCustomer()->getEmail()
  237. );
  238. if (isset($contact->id)) {
  239. $this->contactFromAccount = $contact;
  240. $this->customerSession->setConnectorContactId($contact->id);
  241. }
  242. }
  243. return $this->contactFromAccount;
  244. }
  245. /**
  246. * @return \Dotdigitalgroup\Email\Model\Contact
  247. */
  248. private function getContactFromTable()
  249. {
  250. if (! isset($this->contactFromTable)) {
  251. $this->contactFromTable = $this->helper->getContactByEmail(
  252. $this->getCustomer()->getEmail(),
  253. $this->getCustomer()->getStore()->getWebsite()->getId()
  254. );
  255. }
  256. return $this->contactFromTable;
  257. }
  258. /**
  259. * @return array
  260. */
  261. public function getPreferencesToShow()
  262. {
  263. $processedPreferences = [];
  264. $showPreferences = $this->getCanShowPreferences();
  265. $contactFromTable = $this->getContactFromTable();
  266. if ($showPreferences && $contactFromTable->getContactId()) {
  267. $contact = $this->getConnectorContact();
  268. if (isset($contact->id)) {
  269. $preferences = $this->_getApiClient()->getPreferencesForContact($contact->id);
  270. if (is_array($preferences)) {
  271. $processedPreferences = $this->processPreferences($preferences, $processedPreferences);
  272. }
  273. }
  274. } elseif ($showPreferences) {
  275. $preferences = $this->_getApiClient()->getPreferences();
  276. if (is_array($preferences)) {
  277. $processedPreferences = $this->processPreferences($preferences, $processedPreferences);
  278. }
  279. }
  280. $this->customerSession->setDmContactPreferences($processedPreferences);
  281. return $processedPreferences;
  282. }
  283. /**
  284. * @return bool
  285. */
  286. public function getCanShowPreferences()
  287. {
  288. return $this->_getWebsiteConfigFromHelper(
  289. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SHOW_PREFERENCES,
  290. $this->getCustomer()->getStore()->getWebsite()
  291. );
  292. }
  293. /**
  294. * @param $preferences
  295. * @param $processedPreferences
  296. *
  297. * @return mixed
  298. */
  299. private function processPreferences($preferences, $processedPreferences)
  300. {
  301. foreach ($preferences as $preference) {
  302. $formattedPreference = [];
  303. $formattedPreference['isPreference'] = $preference->isPreference;
  304. if (! $preference->isPreference) {
  305. if (! isset($preference->preferences)) {
  306. continue;
  307. }
  308. $formattedPreference['catLabel'] = $preference->publicName;
  309. $formattedCatPreferences = [];
  310. foreach ($preference->preferences as $catPreference) {
  311. $formattedCatPreference = [];
  312. $formattedCatPreference['label'] = $catPreference->publicName;
  313. isset($catPreference->isOptedIn)? $formattedCatPreference['isOptedIn'] = $catPreference->isOptedIn :
  314. $formattedCatPreference['isOptedIn'] = false;
  315. $formattedCatPreferences[$catPreference->id] = $formattedCatPreference;
  316. }
  317. $formattedPreference['catPreferences'] = $formattedCatPreferences;
  318. } else {
  319. $formattedPreference['label'] = $preference->publicName;
  320. isset($catPreference->isOptedIn)? $formattedPreference['isOptedIn'] = $preference->isOptedIn :
  321. $formattedPreference['isOptedIn'] = false;
  322. }
  323. $processedPreferences[$preference->id] = $formattedPreference;
  324. }
  325. return $processedPreferences;
  326. }
  327. /**
  328. * @param $additionalFromConfig
  329. * @param $processedAddressBooks
  330. * @param $additionalBooksToShow
  331. *
  332. * @return array
  333. */
  334. private function getProcessedAdditionalBooks($additionalFromConfig, $processedAddressBooks, $additionalBooksToShow)
  335. {
  336. foreach ($additionalFromConfig as $bookId) {
  337. $connectorBook = $this->_getApiClient()->getAddressBookById(
  338. $bookId
  339. );
  340. if (isset($connectorBook->id)) {
  341. $subscribed = 0;
  342. if (isset($processedAddressBooks[$bookId])) {
  343. $subscribed = 1;
  344. }
  345. $additionalBooksToShow[] = [
  346. 'name' => $connectorBook->name,
  347. 'value' => $connectorBook->id,
  348. 'subscribed' => $subscribed,
  349. ];
  350. }
  351. }
  352. return $additionalBooksToShow;
  353. }
  354. /**
  355. * @param array $processedConnectorDataFields
  356. * @param array $dataFieldsFromConfig
  357. * @param array $processedContactDataFields
  358. * @param array $datafieldsToShow
  359. *
  360. * @return array
  361. */
  362. private function getProcessedDataFieldsToShow(
  363. $processedConnectorDataFields,
  364. $dataFieldsFromConfig,
  365. $processedContactDataFields,
  366. $datafieldsToShow
  367. ) {
  368. $connectorDataFields = $this->_getApiClient()->getDataFields();
  369. if (! isset($connectorDataFields->message)) {
  370. foreach ($connectorDataFields as $connectorDataField) {
  371. $processedConnectorDataFields[$connectorDataField->name]
  372. = $connectorDataField;
  373. }
  374. foreach ($dataFieldsFromConfig as $dataFieldFromConfig) {
  375. if (isset($processedConnectorDataFields[$dataFieldFromConfig])) {
  376. $value = '';
  377. if (isset($processedContactDataFields[$processedConnectorDataFields[$dataFieldFromConfig]->name])) {
  378. if ($processedConnectorDataFields[$dataFieldFromConfig]->type
  379. == 'Date'
  380. ) {
  381. $value = $processedContactDataFields[
  382. $processedConnectorDataFields[$dataFieldFromConfig]->name
  383. ];
  384. $value = $this->_localeDate->convertConfigTimeToUtc($value, 'm/d/Y');
  385. } else {
  386. $value
  387. = $processedContactDataFields[
  388. $processedConnectorDataFields[$dataFieldFromConfig]->name
  389. ];
  390. }
  391. }
  392. $datafieldsToShow[] = [
  393. 'name' => $processedConnectorDataFields[$dataFieldFromConfig]->name,
  394. 'type' => $processedConnectorDataFields[$dataFieldFromConfig]->type,
  395. 'value' => $value,
  396. ];
  397. }
  398. }
  399. }
  400. return $datafieldsToShow;
  401. }
  402. /**
  403. * @return bool
  404. */
  405. public function isSubscribed()
  406. {
  407. $subscriber = $this->subscriberFactory->create()->loadByCustomerId(
  408. $this->customerSession->getCustomerId()
  409. );
  410. if ($subscriber->getId()) {
  411. return $subscriber->isSubscribed();
  412. }
  413. return false;
  414. }
  415. }