/** * @return array */ public function sync() { $response = ['success' => true, 'message' => '']; $this->start = microtime(true); $websites = $this->helper->getWebsites(true); $started = false; foreach ($websites as $website) { //if subscriber is enabled and mapped $apiEnabled = $this->helper->isEnabled($website->getid()); $subscriberEnaled = $this->helper->isSubscriberSyncEnabled($website->getid()); $addressBook = $this->helper->getSubscriberAddressBook($website->getId()); //enabled and mapped if ($apiEnabled && $addressBook && $subscriberEnaled) { //ready to start sync $numUpdated = $this->exportSubscribersPerWebsite($website); if ($this->countSubscriber && !$started) { $this->helper->log('---------------------- Start subscriber sync -------------------'); $started = true; } // show message for any number of customers if ($numUpdated) { $response['message'] .= '</br>' . $website->getName() . ', updated subscribers = ' . $numUpdated; } } } return $response; }
/** * Get addressbook by import type. * * @param $importType * @param $websiteId * * @return mixed|string */ public function _getAddressBook($importType, $websiteId) { switch ($importType) { case \Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_CONTACT: $addressBook = $this->helper->getCustomerAddressBook($websiteId); break; case \Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_SUBSCRIBERS: $addressBook = $this->helper->getSubscriberAddressBook($websiteId); break; case \Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_GUEST: $addressBook = $this->helper->getGuestAddressBook($websiteId); break; default: $addressBook = ''; } return $addressBook; }
/** * Get options. * * @return array */ public function toOptionArray() { $fields[] = ['label' => __('---- Default Option ----'), 'value' => '0']; $website = $this->helper->getWebsite(); $apiEnabled = $this->helper->isEnabled($website); //get address books options if ($apiEnabled) { $addressBooks = $this->getAddressBooks(); //set the error message to the select option if (isset($addressBooks->message)) { $fields[] = ['value' => 0, 'label' => __($addressBooks->message)]; } $subscriberAddressBook = $this->helper->getSubscriberAddressBook($this->helper->getWebsite()); //set up fields with book id and label foreach ($addressBooks as $book) { if (isset($book->id) && $book->visibility == 'Public' && $book->id != $subscriberAddressBook) { $fields[] = ['value' => $book->id, 'label' => $book->name]; } } } return $fields; }
/** * @return \Magento\Framework\App\ResponseInterface */ public function execute() { if (!$this->formKeyValidator->validate($this->getRequest()) or !$this->customerSession->getConnectorContactId()) { return $this->_redirect('customer/account/'); } //params $additionalSubscriptions = $this->getRequest()->getParam('additional_subscriptions'); $paramDataFields = $this->getRequest()->getParam('data_fields'); $customerId = $this->customerSession->getConnectorContactId(); $customerEmail = $this->customerSession->getCustomer()->getEmail(); //client $website = $this->customerSession->getCustomer()->getStore()->getWebsite(); //if enabled if ($this->helper->isEnabled($website)) { $client = $this->helper->getWebsiteApiClient($website); $client->setApiUsername($this->helper->getApiUsername($website))->setApiPassword($this->helper->getApiPassword($website)); $contact = $client->getContactById($customerId); if (isset($contact->id)) { //contact address books $bookError = false; $addressBooks = $client->getContactAddressBooks($contact->id); $subscriberAddressBook = $this->helper->getSubscriberAddressBook($website); $processedAddressBooks = []; if (is_array($addressBooks)) { foreach ($addressBooks as $addressBook) { if ($subscriberAddressBook != $addressBook->id) { $processedAddressBooks[$addressBook->id] = $addressBook->name; } } } if (isset($additionalSubscriptions)) { foreach ($additionalSubscriptions as $additionalSubscription) { if (!isset($processedAddressBooks[$additionalSubscription])) { $bookResponse = $client->postAddressBookContacts($additionalSubscription, $contact); if (isset($bookResponse->message)) { $bookError = true; } } } foreach ($processedAddressBooks as $bookId => $name) { if (!in_array($bookId, $additionalSubscriptions)) { $bookResponse = $client->deleteAddressBookContact($bookId, $contact->id); if (isset($bookResponse->message)) { $bookError = true; } } } } else { foreach ($processedAddressBooks as $bookId => $name) { $bookResponse = $client->deleteAddressBookContact($bookId, $contact->id); if (isset($bookResponse->message)) { $bookError = true; } } } //contact data fields $data = []; $dataFields = $client->getDataFields(); $processedFields = []; foreach ($dataFields as $dataField) { $processedFields[$dataField->name] = $dataField->type; } foreach ($paramDataFields as $key => $value) { if (isset($processedFields[$key]) && $value) { if ($processedFields[$key] == 'Numeric') { $paramDataFields[$key] = (int) $value; } if ($processedFields[$key] == 'String') { $paramDataFields[$key] = (string) $value; } if ($processedFields[$key] == 'Date') { $paramDataFields[$key] = $this->localeDate->date($value)->format(\Zend_Date::ISO_8601); } $data[] = ['Key' => $key, 'Value' => $paramDataFields[$key]]; } } $contactResponse = $client->updateContactDatafieldsByEmail($customerEmail, $data); if (isset($contactResponse->message) && $bookError) { $this->messageManager->addErrorMessage(__('An error occurred while saving your subscription preferences.')); } else { $this->messageManager->addSuccessMessage(__('The subscription preferences has been saved.')); } } else { $this->messageManager->addErrorMessage(__('An error occurred while saving your subscription preferences.')); } } $this->_redirect('customer/account/'); }