123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- <?php
- namespace Dotdigitalgroup\Email\Block\Customer\Account;
- /**
- * Books block
- *
- * @api
- */
- class Books extends \Magento\Framework\View\Element\Template
- {
- /**
- * Apiconnector client.
- *
- * @var \Dotdigitalgroup\Email\Model\Apiconnector\Client
- */
- private $client;
- /**
- * @var \Dotdigitalgroup\Email\Helper\Data
- */
- public $helper;
- /**
- * @var \Magento\Customer\Model\Session
- */
- public $customerSession;
- /**
- * @var \Magento\Newsletter\Model\SubscriberFactory
- */
- private $subscriberFactory;
- /**
- * @var object
- */
- private $contactFromAccount;
- /**
- * @var \Dotdigitalgroup\Email\Model\Contact
- */
- private $contactFromTable;
- /**
- * Books constructor.
- *
- * @param \Magento\Framework\View\Element\Template\Context $context
- * @param \Dotdigitalgroup\Email\Helper\Data $helper
- * @param \Magento\Customer\Model\Session $customerSession
- * @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
- * @param array $data
- */
- public function __construct(
- \Magento\Framework\View\Element\Template\Context $context,
- \Dotdigitalgroup\Email\Helper\Data $helper,
- \Magento\Customer\Model\Session $customerSession,
- \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
- array $data = []
- ) {
- $this->helper = $helper;
- $this->customerSession = $customerSession;
- $this->subscriberFactory = $subscriberFactory;
- parent::__construct($context, $data);
- }
- /**
- * @return string
- */
- public function getCustomerConsentText()
- {
- return $this->helper->configHelperFactory->create()
- ->getConsentCustomerText($this->getCustomer()->getWebsiteId());
- }
- /**
- * @return \Magento\Customer\Model\Customer
- */
- public function getCustomer()
- {
- return $this->customerSession->getCustomer();
- }
- /**
- * Subscription pref save url.
- *
- * @return string
- */
- public function getSaveUrl()
- {
- return $this->getUrl('connector/customer/newsletter');
- }
- /**
- * Get config values.
- *
- * @param string $path
- * @param int $website
- *
- * @return string|boolean
- */
- public function _getWebsiteConfigFromHelper($path, $website)
- {
- return $this->helper->getWebsiteConfig($path, $website);
- }
- /**
- * Get api client.
- *
- * @return \Dotdigitalgroup\Email\Model\Apiconnector\Client
- */
- private function _getApiClient()
- {
- if (empty($this->client)) {
- $website = $this->getCustomer()->getStore()->getWebsite();
- $client = $this->helper->getWebsiteApiClient($website);
- $this->client = $client;
- }
- return $this->client;
- }
- /**
- * Can show additional books?
- *
- * @return string|boolean
- */
- public function getCanShowAdditionalBooks()
- {
- return $this->helper->getCanShowAdditionalSubscriptions(
- $this->getCustomer()->getStore()->getWebsite()
- );
- }
- /**
- * Getter for additional books. Fully processed.
- *
- * @return array
- */
- public function getAdditionalBooksToShow()
- {
- $additionalBooksToShow = [];
- $processedAddressBooks = [];
- $additionalFromConfig = $this->helper->getAddressBookIdsToShow($this->getCustomer()->getStore()->getWebsite());
- $contactFromTable = $this->getContactFromTable();
- if (! empty($additionalFromConfig) && $contactFromTable->getContactId()) {
- $contact = $this->getConnectorContact();
- if (isset($contact->id) && $contact->status !== 'PendingOptIn') {
- $addressBooks = $this->_getApiClient()
- ->getContactAddressBooks(
- $contact->id
- );
- if (is_array($addressBooks)) {
- foreach ($addressBooks as $addressBook) {
- $processedAddressBooks[$addressBook->id]
- = $addressBook->name;
- }
- }
- }
- }
- return $this->getProcessedAdditionalBooks(
- $additionalFromConfig,
- $processedAddressBooks,
- $additionalBooksToShow
- );
- }
- /**
- * Can show data fields?
- *
- * @return string|boolean
- */
- public function getCanShowDataFields()
- {
- return $this->helper->getCanShowDataFields(
- $this->getCustomer()->getStore()->getWebsite()
- );
- }
- /**
- * Getter for datafields to show. Fully processed.
- *
- * @return array
- */
- public function getDataFieldsToShow()
- {
- $datafieldsToShow = [];
- $website = $this->getCustomer()->getStore()->getWebsite();
- $dataFieldsFromConfig = $this->_getWebsiteConfigFromHelper(
- \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_ADDRESSBOOK_PREF_SHOW_FIELDS,
- $website
- );
- if (empty($dataFieldsFromConfig)) {
- return $datafieldsToShow;
- }
- $processedContactDataFields = [];
- $processedConnectorDataFields = [];
- $contactFromTable = $this->getContactFromTable();
- $dataFieldsFromConfig = explode(',', $dataFieldsFromConfig);
- if ($contactFromTable->getContactId()) {
- $contact = $this->getConnectorContact();
- if (isset($contact->id)) {
- $contactDataFields = $contact->dataFields;
- foreach ($contactDataFields as $contactDataField) {
- $processedContactDataFields[$contactDataField->key]
- = $contactDataField->value;
- }
- }
- }
- return $this->getProcessedDataFieldsToShow(
- $processedConnectorDataFields,
- $dataFieldsFromConfig,
- $processedContactDataFields,
- $datafieldsToShow
- );
- }
- /**
- * Find out if anything is true.
- *
- * @return bool
- */
- public function canShowAnything()
- {
- if (! $this->isCustomerSubscriber() || ! $this->helper->isEnabled($this->getCustomer()->getWebsiteId())) {
- return false;
- }
- $showPreferences = $this->getCanShowPreferences();
- $books = $this->getAdditionalBooksToShow();
- $fields = $this->getDataFieldsToShow();
- if ($books || $fields || $showPreferences) {
- if (! empty($books) || ! empty($fields) || $showPreferences) {
- return true;
- }
- }
- return false;
- }
- /**
- * @return bool
- */
- private function isCustomerSubscriber()
- {
- return $this->subscriberFactory->create()
- ->loadByCustomerId($this->getCustomer()->getId())
- ->isSubscribed();
- }
- /**
- * Get connector contact.
- *
- * @return object
- */
- public function getConnectorContact()
- {
- if (! isset($this->contactFromAccount)) {
- $contact = $this->_getApiClient()->getContactByEmail(
- $this->getCustomer()->getEmail()
- );
- if (isset($contact->id)) {
- $this->contactFromAccount = $contact;
- $this->customerSession->setConnectorContactId($contact->id);
- }
- }
- return $this->contactFromAccount;
- }
- /**
- * @return \Dotdigitalgroup\Email\Model\Contact
- */
- private function getContactFromTable()
- {
- if (! isset($this->contactFromTable)) {
- $this->contactFromTable = $this->helper->getContactByEmail(
- $this->getCustomer()->getEmail(),
- $this->getCustomer()->getStore()->getWebsite()->getId()
- );
- }
- return $this->contactFromTable;
- }
- /**
- * @return array
- */
- public function getPreferencesToShow()
- {
- $processedPreferences = [];
- $showPreferences = $this->getCanShowPreferences();
- $contactFromTable = $this->getContactFromTable();
- if ($showPreferences && $contactFromTable->getContactId()) {
- $contact = $this->getConnectorContact();
- if (isset($contact->id)) {
- $preferences = $this->_getApiClient()->getPreferencesForContact($contact->id);
- if (is_array($preferences)) {
- $processedPreferences = $this->processPreferences($preferences, $processedPreferences);
- }
- }
- } elseif ($showPreferences) {
- $preferences = $this->_getApiClient()->getPreferences();
- if (is_array($preferences)) {
- $processedPreferences = $this->processPreferences($preferences, $processedPreferences);
- }
- }
- $this->customerSession->setDmContactPreferences($processedPreferences);
- return $processedPreferences;
- }
- /**
- * @return bool
- */
- public function getCanShowPreferences()
- {
- return $this->_getWebsiteConfigFromHelper(
- \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SHOW_PREFERENCES,
- $this->getCustomer()->getStore()->getWebsite()
- );
- }
- /**
- * @param $preferences
- * @param $processedPreferences
- *
- * @return mixed
- */
- private function processPreferences($preferences, $processedPreferences)
- {
- foreach ($preferences as $preference) {
- $formattedPreference = [];
- $formattedPreference['isPreference'] = $preference->isPreference;
- if (! $preference->isPreference) {
- if (! isset($preference->preferences)) {
- continue;
- }
- $formattedPreference['catLabel'] = $preference->publicName;
- $formattedCatPreferences = [];
- foreach ($preference->preferences as $catPreference) {
- $formattedCatPreference = [];
- $formattedCatPreference['label'] = $catPreference->publicName;
- isset($catPreference->isOptedIn)? $formattedCatPreference['isOptedIn'] = $catPreference->isOptedIn :
- $formattedCatPreference['isOptedIn'] = false;
- $formattedCatPreferences[$catPreference->id] = $formattedCatPreference;
- }
- $formattedPreference['catPreferences'] = $formattedCatPreferences;
- } else {
- $formattedPreference['label'] = $preference->publicName;
- isset($catPreference->isOptedIn)? $formattedPreference['isOptedIn'] = $preference->isOptedIn :
- $formattedPreference['isOptedIn'] = false;
- }
- $processedPreferences[$preference->id] = $formattedPreference;
- }
- return $processedPreferences;
- }
- /**
- * @param $additionalFromConfig
- * @param $processedAddressBooks
- * @param $additionalBooksToShow
- *
- * @return array
- */
- private function getProcessedAdditionalBooks($additionalFromConfig, $processedAddressBooks, $additionalBooksToShow)
- {
- foreach ($additionalFromConfig as $bookId) {
- $connectorBook = $this->_getApiClient()->getAddressBookById(
- $bookId
- );
- if (isset($connectorBook->id)) {
- $subscribed = 0;
- if (isset($processedAddressBooks[$bookId])) {
- $subscribed = 1;
- }
- $additionalBooksToShow[] = [
- 'name' => $connectorBook->name,
- 'value' => $connectorBook->id,
- 'subscribed' => $subscribed,
- ];
- }
- }
- return $additionalBooksToShow;
- }
- /**
- * @param array $processedConnectorDataFields
- * @param array $dataFieldsFromConfig
- * @param array $processedContactDataFields
- * @param array $datafieldsToShow
- *
- * @return array
- */
- private function getProcessedDataFieldsToShow(
- $processedConnectorDataFields,
- $dataFieldsFromConfig,
- $processedContactDataFields,
- $datafieldsToShow
- ) {
- $connectorDataFields = $this->_getApiClient()->getDataFields();
- if (! isset($connectorDataFields->message)) {
- foreach ($connectorDataFields as $connectorDataField) {
- $processedConnectorDataFields[$connectorDataField->name]
- = $connectorDataField;
- }
- foreach ($dataFieldsFromConfig as $dataFieldFromConfig) {
- if (isset($processedConnectorDataFields[$dataFieldFromConfig])) {
- $value = '';
- if (isset($processedContactDataFields[$processedConnectorDataFields[$dataFieldFromConfig]->name])) {
- if ($processedConnectorDataFields[$dataFieldFromConfig]->type
- == 'Date'
- ) {
- $value = $processedContactDataFields[
- $processedConnectorDataFields[$dataFieldFromConfig]->name
- ];
- $value = $this->_localeDate->convertConfigTimeToUtc($value, 'm/d/Y');
- } else {
- $value
- = $processedContactDataFields[
- $processedConnectorDataFields[$dataFieldFromConfig]->name
- ];
- }
- }
- $datafieldsToShow[] = [
- 'name' => $processedConnectorDataFields[$dataFieldFromConfig]->name,
- 'type' => $processedConnectorDataFields[$dataFieldFromConfig]->type,
- 'value' => $value,
- ];
- }
- }
- }
- return $datafieldsToShow;
- }
- /**
- * @return bool
- */
- public function isSubscribed()
- {
- $subscriber = $this->subscriberFactory->create()->loadByCustomerId(
- $this->customerSession->getCustomerId()
- );
- if ($subscriber->getId()) {
- return $subscriber->isSubscribed();
- }
- return false;
- }
- }
|