diff --git a/app/code/Ecomail/Ecomail/Block/System/Config/Form/Field/Status.php b/app/code/Ecomail/Ecomail/Block/System/Config/Form/Field/Status.php new file mode 100644 index 0000000..f6256de --- /dev/null +++ b/app/code/Ecomail/Ecomail/Block/System/Config/Form/Field/Status.php @@ -0,0 +1,75 @@ +helper = $helper; + $this->api = $api; + } + + /** + * @return string + */ + public function getStatusCode(): string + { + if (!$this->helper->isEnabled()) { + return 'Inactive'; + } + + try { + $this->api->getSubscriberLists(); + return 'Active'; + } catch (Exception $e) { + return 'Error'; + } + } + + /** + * Return element html + * + * @param AbstractElement $element + * @return string + * + */ + protected function _getElementHtml(AbstractElement $element): string + { + return $this->_toHtml(); + } +} diff --git a/app/code/Ecomail/Ecomail/Block/System/Config/Form/Field/Subscriber/LoadLists.php b/app/code/Ecomail/Ecomail/Block/System/Config/Form/Field/Subscriber/LoadLists.php new file mode 100644 index 0000000..52c408e --- /dev/null +++ b/app/code/Ecomail/Ecomail/Block/System/Config/Form/Field/Subscriber/LoadLists.php @@ -0,0 +1,67 @@ +unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue(); + return parent::render($element); + } + + /** + * Return element html + * + * @param AbstractElement $element + * @return string + * + */ + protected function _getElementHtml(AbstractElement $element): string + { + return $this->_toHtml(); + } + + /** + * @return string + * @throws LocalizedException + */ + public function getButtonHtml(): string + { + $button = $this->getLayout()->createBlock( + Button::class + )->setData( + [ + 'id' => 'load_lists', + 'label' => __('Load Lists') + ] + ); + + return $button->toHtml(); + } + + /** + * @return string + */ + public function getAjaxUrl(): string + { + return $this->getUrl('*/system_config_ecomail/loadLists'); + } +} diff --git a/app/code/Ecomail/Ecomail/CHANGELOG.md b/app/code/Ecomail/Ecomail/CHANGELOG.md new file mode 100644 index 0000000..7f44e5a --- /dev/null +++ b/app/code/Ecomail/Ecomail/CHANGELOG.md @@ -0,0 +1,4 @@ +# Ecomail changelog + +## v2.0.0 - 22 Mar, 2021 +* initial release diff --git a/app/code/Ecomail/Ecomail/Controller/Adminhtml/Ecomail/Ajax.php b/app/code/Ecomail/Ecomail/Controller/Adminhtml/Ecomail/Ajax.php deleted file mode 100644 index bc6642c..0000000 --- a/app/code/Ecomail/Ecomail/Controller/Adminhtml/Ecomail/Ajax.php +++ /dev/null @@ -1,57 +0,0 @@ -resultJsonFactory = $resultJsonFactory; - $this->helper = $helper; - } - - /** - * @return \Magento\Framework\Controller\Result\Json - */ - public function execute() { - /** @var \Magento\Framework\Controller\Result\Json $result */ - $resultJson = $this->resultJsonFactory->create(); - - $isAjax = $this->getRequest() - ->isAjax(); - if( $isAjax ) { - - $result = array(); - - $cmd = $this->getRequest() - ->getParam( 'cmd' ); - if( $cmd == 'getLists' ) { - - $APIKey = $this->getRequest() - ->getParam( 'APIKey' ); - if( $APIKey ) { - $listsCollection = $this->helper->getAPI() - ->setAPIKey( $APIKey ) - ->getListsCollection(); - if( $listsCollection ) { - foreach( $listsCollection as $list ) { - $result[] = array( - 'id' => $list->id, - 'name' => $list->name - ); - } - } - } - - } - - return $resultJson->setData( $result ); - } - } - } \ No newline at end of file diff --git a/app/code/Ecomail/Ecomail/Controller/Adminhtml/System/Config/Ecomail/LoadLists.php b/app/code/Ecomail/Ecomail/Controller/Adminhtml/System/Config/Ecomail/LoadLists.php new file mode 100644 index 0000000..bf01a29 --- /dev/null +++ b/app/code/Ecomail/Ecomail/Controller/Adminhtml/System/Config/Ecomail/LoadLists.php @@ -0,0 +1,55 @@ +api = $api; + } + + /** + * @return ResultInterface + */ + public function execute(): ResultInterface + { + $result = $this->resultFactory->create(ResultFactory::TYPE_JSON); + + $apiKey = $this->getRequest()->getParam('api_key'); + + try { + $subscriberLists = $this->api->getSubscriberLists($apiKey); + } catch (Exception $e) { + return $result->setData([]); + } + + $response = []; + + foreach ($subscriberLists as $list) { + $response[] = [ + 'label' => $list['name'], + 'value' => $list['id'], + ]; + } + + return $result->setData($response); + } +} diff --git a/app/code/Ecomail/Ecomail/CustomerData/Ecomail.php b/app/code/Ecomail/Ecomail/CustomerData/Ecomail.php new file mode 100644 index 0000000..be6acfa --- /dev/null +++ b/app/code/Ecomail/Ecomail/CustomerData/Ecomail.php @@ -0,0 +1,52 @@ +customerSession = $customerSession; + $this->helper = $helper; + } + + /** + * @return array + */ + public function getSectionData(): array + { + $sectionData = []; + + if ($this->helper->isTrackingEnabled() && $this->customerSession->getEcomailEmail()) { + $sectionData['email'] = $this->customerSession->getEcomailEmail(); + $this->customerSession->setEcomailEmail(null); + } + + return $sectionData; + } +} diff --git a/app/code/Ecomail/Ecomail/Helper/Data.php b/app/code/Ecomail/Ecomail/Helper/Data.php old mode 100755 new mode 100644 index 70a26ad..bf40997 --- a/app/code/Ecomail/Ecomail/Helper/Data.php +++ b/app/code/Ecomail/Ecomail/Helper/Data.php @@ -1,36 +1,206 @@ scopeConfig; - } + const XML_PATH_ECOMAIL_PERSONAL_INFORMATION_SEND_NAME = 'ecomail/personal_information/send_name'; + const XML_PATH_ECOMAIL_PERSONAL_INFORMATION_SEND_ADDRESS = 'ecomail/personal_information/send_address'; + const XML_PATH_ECOMAIL_PERSONAL_INFORMATION_ADDRESS_TYPE = 'ecomail/personal_information/address_type'; + const XML_PATH_ECOMAIL_PERSONAL_INFORMATION_SEND_DOB = 'ecomail/personal_information/send_dob'; + const XML_PATH_ECOMAIL_PERSONAL_INFORMATION_SEND_ORDERS = 'ecomail/personal_information/send_orders'; + const XML_PATH_ECOMAIL_PERSONAL_INFORMATION_SEND_CART_ITEMS = 'ecomail/personal_information/send_cart_items'; - public function getRequest() { - return $this->_getRequest(); - } + const XML_PATH_ECOMAIL_TRACKING_ENABLED = 'ecomail/tracking/enabled'; + const XML_PATH_ECOMAIL_TRACKING_APP_ID = 'ecomail/tracking/app_id'; - public function getAPI() { + /** + * @param null $store + * @return bool + */ + public function isEnabled($store = null): bool + { + return (bool)$this->scopeConfig->getValue( + self::XML_PATH_ECOMAIL_GENERAL_ENABLED, + ScopeInterface::SCOPE_STORE, + $store + ); + } - require_once __DIR__ . '/../lib/api.php'; + /** + * Check if solution is enabled and configured properly. + * + * @param null $store + * @return bool + */ + public function isAvailable($store = null): bool + { + return $this->isEnabled($store) && $this->getSubscriberList($store); + } - $obj = new EcomailAPI(); - $obj->setAPIKey( - $this->scopeConfig->getValue( - 'ecomail_options/properties/api_key', - \Magento\Store\Model\ScopeInterface::SCOPE_STORE - ) - ); + /** + * @param null $store + * @return bool + */ + public function skipDoubleOptin($store = null): bool + { + return (bool)$this->scopeConfig->getValue( + self::XML_PATH_ECOMAIL_GENERAL_SKIP_DOUBLE_OPTIN, + ScopeInterface::SCOPE_STORE, + $store + ); + } - return $obj; - } + /** + * @param null $store + * @return bool + */ + public function triggerAutoresponders($store = null): bool + { + return (bool)$this->scopeConfig->getValue( + self::XML_PATH_ECOMAIL_GENERAL_TRIGGER_AUTORESPONDERS, + ScopeInterface::SCOPE_STORE, + $store + ); + } - public function getCookieNameTrackStructEvent() { - return 'Ecomail'; - } + /** + * @param null $store + * @return string|null + */ + public function getApiKey($store = null) + { + return $this->scopeConfig->getValue( + self::XML_PATH_ECOMAIL_GENERAL_API_KEY, + ScopeInterface::SCOPE_STORE, + $store + ); + } - } \ No newline at end of file + /** + * @param null $store + * @return string|null + */ + public function getSubscriberList($store = null) + { + return $this->scopeConfig->getValue( + self::XML_PATH_ECOMAIL_GENERAL_SUBSCRIBER_LIST, + ScopeInterface::SCOPE_STORE, + $store + ); + } + + /** + * @param null $store + * @return bool + */ + public function sendName($store = null): bool + { + return (bool)$this->scopeConfig->getValue( + self::XML_PATH_ECOMAIL_PERSONAL_INFORMATION_SEND_NAME, + ScopeInterface::SCOPE_STORE, + $store + ); + } + + /** + * @param null $store + * @return bool + */ + public function sendAddress($store = null): bool + { + return (bool)$this->scopeConfig->getValue( + self::XML_PATH_ECOMAIL_PERSONAL_INFORMATION_SEND_ADDRESS, + ScopeInterface::SCOPE_STORE, + $store + ); + } + + /** + * @param null $store + * @return bool + */ + public function useShippingAddress($store = null): bool + { + return Address::SHIPPING_ADDRESS === (int)$this->scopeConfig->getValue( + self::XML_PATH_ECOMAIL_PERSONAL_INFORMATION_ADDRESS_TYPE, + ScopeInterface::SCOPE_STORE, + $store + ); + } + + /** + * @param null $store + * @return bool + */ + public function sendDob($store = null): bool + { + return (bool)$this->scopeConfig->getValue( + self::XML_PATH_ECOMAIL_PERSONAL_INFORMATION_SEND_DOB, + ScopeInterface::SCOPE_STORE, + $store + ); + } + + /** + * @param null $store + * @return bool + */ + public function sendOrders($store = null): bool + { + return (bool)$this->scopeConfig->getValue( + self::XML_PATH_ECOMAIL_PERSONAL_INFORMATION_SEND_ORDERS, + ScopeInterface::SCOPE_STORE, + $store + ); + } + + /** + * @param null $store + * @return bool + */ + public function sendCartItems($store = null): bool + { + return (bool)$this->scopeConfig->getValue( + self::XML_PATH_ECOMAIL_PERSONAL_INFORMATION_SEND_CART_ITEMS, + ScopeInterface::SCOPE_STORE, + $store + ); + } + + /** + * @param null $store + * @return bool + */ + public function isTrackingEnabled($store = null): bool + { + return (bool)$this->scopeConfig->getValue( + self::XML_PATH_ECOMAIL_TRACKING_ENABLED, + ScopeInterface::SCOPE_STORE, + $store + ); + } + + /** + * @param null $store + * @return string|null + */ + public function getAppId($store = null) + { + return $this->scopeConfig->getValue( + self::XML_PATH_ECOMAIL_TRACKING_APP_ID, + ScopeInterface::SCOPE_STORE, + $store + ); + } +} diff --git a/app/code/Ecomail/Ecomail/Model/Api.php b/app/code/Ecomail/Ecomail/Model/Api.php new file mode 100644 index 0000000..ab620bb --- /dev/null +++ b/app/code/Ecomail/Ecomail/Model/Api.php @@ -0,0 +1,207 @@ +clientFactory = $clientFactory; + $this->jsonSerializer = $jsonSerializer; + $this->helper = $helper; + } + + /** + * @param array $subscriberData + * @return array + * @throws IntegrationException + * @throws Zend_Http_Client_Exception + */ + public function addSubscriberToList(array $subscriberData): array + { + $client = $this->getClient(); + $client->setUri($this->buildListUrl('subscribe')); + $client->setParameterPost($subscriberData); + $response = $client->request(ZendClient::POST); + + return $this->processResponse($response); + } + + /** + * @param string $email + * @return array + * @throws IntegrationException + * @throws Zend_Http_Client_Exception + */ + public function removeSubscriberFromList(string $email): array + { + $client = $this->getClient(); + + $client->setUri($this->buildListUrl('unsubscribe')); + $client->setParameterPost(['email' => $email]); + $response = $client->request(ZendClient::DELETE); + + return $this->processResponse($response); + } + + /** + * @param null $apiKey + * @return array + * @throws IntegrationException + * @throws Zend_Http_Client_Exception + */ + public function getSubscriberLists($apiKey = null): array + { + $client = $this->getClient($apiKey); + $client->setUri(self::API_URL . 'lists'); + $response = $client->request(ZendClient::GET); + + return $this->processResponse($response); + } + + /** + * @param array $data + * @return array + * @throws IntegrationException + * @throws Zend_Http_Client_Exception + */ + public function createTransaction(array $data): array + { + $client = $this->getClient(); + $client->setUri(self::API_URL . 'tracker/transaction'); + $client->setParameterPost($data); + $response = $client->request(ZendClient::POST); + + return $this->processResponse($response); + } + + /** + * @param $data + * @return array + * @throws IntegrationException + * @throws Zend_Http_Client_Exception + */ + public function updateCart($data): array + { + $client = $this->getClient(); + $client->setUri(self::API_URL . 'tracker/events'); + $client->setParameterPost($data); + $response = $client->request(ZendClient::POST); + + return $this->processResponse($response); + } + + /** + * @param Zend_Http_Response $response + * @return array + * @throws IntegrationException + */ + private function processResponse(Zend_Http_Response $response): array + { + $status = $response->getStatus(); + $response = $this->jsonSerializer->unserialize($response->getBody()); + + if ($status !== self::STATUS_OK && $status !== self::STATUS_CREATED) { + throw new IntegrationException(__($this->getErrorMessage($response))); + } + + return $response; + } + + /** + * @param null $apiKey + * @return ZendClient + * @throws Zend_Http_Client_Exception + */ + private function getClient($apiKey = null): ZendClient + { + if ($apiKey === null) { + $apiKey = $this->helper->getApiKey(); + } + + /** @var ZendClient $client */ + $client = $this->clientFactory->create(); + + $client->setConfig(['timeout' => self::CLIENT_TIMEOUT]); + $client->setHeaders([ + 'Content-Type' => 'application/json', + 'Key' => $apiKey + ]); + + return $client; + } + + /** + * @param string $path + * @return string + */ + private function buildListUrl(string $path): string + { + return self::API_URL . 'lists' . DIRECTORY_SEPARATOR . $this->helper->getSubscriberList() . + DIRECTORY_SEPARATOR . $path; + } + + /** + * @param array $response + * @return string + */ + private function getErrorMessage(array $response): string + { + if (isset($response['message'])) { + return $response['message']; + } + + if (isset($response['errors'])) { + $messages = []; + + foreach ($response['errors'] as $errorType) { + foreach ($errorType as $error) { + $messages[] = $error; + } + } + + return __('Ecomail api error: ') . implode(', ', $messages); + } + + return self::UNKNOWN_ERROR_MESSAGE; + } +} diff --git a/app/code/Ecomail/Ecomail/Model/BasketEventMapper.php b/app/code/Ecomail/Ecomail/Model/BasketEventMapper.php new file mode 100644 index 0000000..75abb6e --- /dev/null +++ b/app/code/Ecomail/Ecomail/Model/BasketEventMapper.php @@ -0,0 +1,78 @@ +imageHelper = $imageHelper; + $this->jsonSerializer = $jsonSerializer; + } + + /** + * @param Quote $quote + * @return array + */ + public function map(Quote $quote): array + { + $data = []; + $data['event'] = [ + 'email' => $quote->getCustomerEmail(), + 'category' => 'ua', + 'action' => 'Basket', + 'label' => 'Basket', + ]; + + $items = []; + + foreach ($quote->getAllVisibleItems() as $quoteItem) { + if ($quoteItem->getParentItem()) { + continue; + } + + /** @var ProductInterface $product */ + $product = $quoteItem->getProduct(); + + $items[] = [ + 'productId' => $quoteItem->getSku(), + 'img_url' => $this->imageHelper->init($product, 'product_thumbnail_image')->getUrl(), + 'url' => $product->getProductUrl(), + 'name' => $quoteItem->getName(), + 'price' => $quoteItem->getPriceInclTax(), + 'description' => $product->getShortDescription() + ]; + } + + $eventValue = []; + $eventValue['data']['data'] = [ + 'action' => 'Basket', + 'products' => $items + ]; + + $data['event']['value'] = $this->jsonSerializer->serialize($eventValue); + + return $data; + } +} diff --git a/app/code/Ecomail/Ecomail/Model/Config/Source/Address.php b/app/code/Ecomail/Ecomail/Model/Config/Source/Address.php new file mode 100644 index 0000000..4e6abe6 --- /dev/null +++ b/app/code/Ecomail/Ecomail/Model/Config/Source/Address.php @@ -0,0 +1,28 @@ + __('Shipping Address'), + 'value' => self::SHIPPING_ADDRESS + ], + [ + 'label' => __('Billing Address'), + 'value' => self::BILLING_ADDRESS + ] + ]; + } +} diff --git a/app/code/Ecomail/Ecomail/Model/Config/Source/SubscriberList.php b/app/code/Ecomail/Ecomail/Model/Config/Source/SubscriberList.php new file mode 100644 index 0000000..68bca67 --- /dev/null +++ b/app/code/Ecomail/Ecomail/Model/Config/Source/SubscriberList.php @@ -0,0 +1,40 @@ +api = $api; + } + + public function toOptionArray(): array + { + $options = []; + + try { + $subscriberLists = $this->api->getSubscriberLists(); + + foreach ($subscriberLists as $list) { + $options[] = [ + 'label' => $list['name'], + 'value' => $list['id'], + ]; + } + } catch (Exception $e) { + return []; + } + + return $options; + } +} diff --git a/app/code/Ecomail/Ecomail/Model/EventManager.php b/app/code/Ecomail/Ecomail/Model/EventManager.php new file mode 100644 index 0000000..f25a14a --- /dev/null +++ b/app/code/Ecomail/Ecomail/Model/EventManager.php @@ -0,0 +1,78 @@ +api = $api; + $this->logger = $logger; + $this->helper = $helper; + $this->checkoutSession = $checkoutSession; + $this->basketEventMapper = $basketEventMapper; + } + + /** + * @throws LocalizedException + * @throws NoSuchEntityException + */ + public function updateCartContent() + { + if (!$this->helper->sendCartItems() || !$this->checkoutSession->getQuote()->getCustomerEmail()) { + return; + } + + try { + $this->api->updateCart($this->basketEventMapper->map($this->checkoutSession->getQuote())); + } catch (Exception $e) { + $this->logger->error('Failed to update Ecomail cart items.', [$e]); + } + } +} diff --git a/app/code/Ecomail/Ecomail/Model/HTTP/Adapter/Curl.php b/app/code/Ecomail/Ecomail/Model/HTTP/Adapter/Curl.php new file mode 100644 index 0000000..ce52240 --- /dev/null +++ b/app/code/Ecomail/Ecomail/Model/HTTP/Adapter/Curl.php @@ -0,0 +1,80 @@ +_applyConfig(); + + // set url to post to + curl_setopt($this->_getResource(), CURLOPT_URL, $url); + curl_setopt($this->_getResource(), CURLOPT_RETURNTRANSFER, true); + if ($method === Zend_Http_Client::POST) { + curl_setopt($this->_getResource(), CURLOPT_POST, true); + curl_setopt($this->_getResource(), CURLOPT_CUSTOMREQUEST, 'POST'); + curl_setopt($this->_getResource(), CURLOPT_POSTFIELDS, $body); + } elseif ($method === Zend_Http_Client::PUT) { + curl_setopt($this->_getResource(), CURLOPT_CUSTOMREQUEST, 'PUT'); + curl_setopt($this->_getResource(), CURLOPT_POSTFIELDS, $body); + } elseif ($method === Zend_Http_Client::GET) { + curl_setopt($this->_getResource(), CURLOPT_HTTPGET, true); + curl_setopt($this->_getResource(), CURLOPT_CUSTOMREQUEST, 'GET'); + } elseif ($method === Zend_Http_Client::DELETE) { + curl_setopt($this->_getResource(), CURLOPT_CUSTOMREQUEST, 'DELETE'); + curl_setopt($this->_getResource(), CURLOPT_POSTFIELDS, $body); + } + + if ($http_ver === Zend_Http_Client::HTTP_1) { + curl_setopt($this->_getResource(), CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); + } elseif ($http_ver === Zend_Http_Client::HTTP_0) { + curl_setopt($this->_getResource(), CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); + } + + if (is_array($headers)) { + curl_setopt($this->_getResource(), CURLOPT_HTTPHEADER, $headers); + } + + /** + * @internal Curl options setter have to be re-factored + */ + $header = isset($this->_config['header']) ? $this->_config['header'] : true; + curl_setopt($this->_getResource(), CURLOPT_HEADER, $header); + + return $body; + } + + /** + * Get default options + * + * @return array + */ + private function getDefaultConfig() + { + $config = []; + foreach (array_keys($this->_config) as $param) { + if (array_key_exists($param, $this->_allowedParams)) { + $config[$this->_allowedParams[$param]] = $this->_config[$param]; + } + } + return $config; + } +} diff --git a/app/code/Ecomail/Ecomail/Model/HTTP/ZendClient.php b/app/code/Ecomail/Ecomail/Model/HTTP/ZendClient.php new file mode 100644 index 0000000..63d32f5 --- /dev/null +++ b/app/code/Ecomail/Ecomail/Model/HTTP/ZendClient.php @@ -0,0 +1,25 @@ +setAdapter(new Adapter\Curl()); + } + return $this; + } +} diff --git a/app/code/Ecomail/Ecomail/Model/OptionsListId.php b/app/code/Ecomail/Ecomail/Model/OptionsListId.php deleted file mode 100644 index b067d25..0000000 --- a/app/code/Ecomail/Ecomail/Model/OptionsListId.php +++ /dev/null @@ -1,40 +0,0 @@ -helper = $helper; - } - - public function toOptionArray() { - - $options = array(); - - if( $this->helper->getScopeConfig() - ->getValue( - 'ecomail_options/properties/api_key', - \Magento\Store\Model\ScopeInterface::SCOPE_STORE - ) - ) { - $listsCollection = $this->helper->getAPI() - ->getListsCollection(); - - - foreach( $listsCollection as $list ) { - $options[] = array( - 'value' => $list->id, - 'label' => $list->name - ); - } - } - - return $options; - } - } \ No newline at end of file diff --git a/app/code/Ecomail/Ecomail/Model/QuoteManager.php b/app/code/Ecomail/Ecomail/Model/QuoteManager.php new file mode 100644 index 0000000..bf2d03b --- /dev/null +++ b/app/code/Ecomail/Ecomail/Model/QuoteManager.php @@ -0,0 +1,65 @@ +checkoutSession = $checkoutSession; + $this->logger = $logger; + $this->cartRepository = $cartRepository; + } + + /** + * @param string $email + * @throws LocalizedException + * @throws NoSuchEntityException + */ + public function setQuoteEmail(string $email) + { + /** @var Quote $quote */ + $quote = $this->checkoutSession->getQuote(); + + if ($quote->getCustomerEmail() === null) { + try { + $quote->setCustomerEmail($email); + $this->cartRepository->save($quote); + } catch (Exception $e) { + $this->logger->error('Failed to set quote email', [$e]); + } + } + } +} diff --git a/app/code/Ecomail/Ecomail/Model/SubscriberDataMapper.php b/app/code/Ecomail/Ecomail/Model/SubscriberDataMapper.php new file mode 100644 index 0000000..5c01882 --- /dev/null +++ b/app/code/Ecomail/Ecomail/Model/SubscriberDataMapper.php @@ -0,0 +1,156 @@ +customerRepository = $customerRepository; + $this->addressRepository = $addressRepository; + $this->helper = $helper; + } + + /** + * @param Subscriber $subscriber + * @return array + * @throws LocalizedException + * @throws NoSuchEntityException + */ + public function map(Subscriber $subscriber): array + { + $data['resubscribe'] = true; + $data['skip_confirmation'] = $this->helper->skipDoubleOptin($subscriber->getStoreId()); + $data['trigger_autoresponders'] = $this->helper->triggerAutoresponders($subscriber->getStoreId()); + $data['subscriber_data']['email'] = $subscriber->getEmail(); + + if ($subscriber->getCustomerId()) { + $customer = $this->customerRepository->getById($subscriber->getCustomerId()); + + if ($this->helper->sendName($subscriber->getStoreId())) { + $data['subscriber_data']['name'] = $customer->getFirstname(); + $data['subscriber_data']['surname'] = $customer->getLastname(); + } + + if ($this->helper->sendDob($subscriber->getStoreId()) && $customer->getDob()) { + $data['subscriber_data']['birthday'] = $customer->getDob(); + } + + /** @var null|int $addressId */ + $addressId = $this->helper->useShippingAddress($subscriber->getStoreId()) ? + $customer->getDefaultShipping() : $customer->getDefaultBilling(); + + if ($this->helper->sendAddress($subscriber->getStoreId()) && $addressId) { + /** @var AddressInterface $address */ + $address = $this->getDefaultAddress($addressId); + + if ($address !== null) { + $data = $this->generateAddressData($address, $data); + } + } + } + + return $data; + } + + /** + * @param OrderInterface $order + * @return array + */ + public function mapFromOrder(OrderInterface $order): array + { + $data['resubscribe'] = true; + $data['skip_confirmation'] = $this->helper->skipDoubleOptin($order->getStoreId()); + $data['trigger_autoresponders'] = $this->helper->triggerAutoresponders($order->getStoreId()); + $data['subscriber_data']['email'] = $order->getCustomerEmail(); + + if ($this->helper->sendDob($order->getStoreId()) && $order->getCustomerDob()) { + $data['subscriber_data']['birthday'] = $order->getCustomerDob(); + } + + /** @var AddressInterface $address */ + $address = $this->helper->useShippingAddress($order->getId()) ? + $order->getShippingAddress() : $order->getBillingAddress(); + + if ($this->helper->sendName($order->getStoreId())) { + $data['subscriber_data']['name'] = $address->getFirstname(); + $data['subscriber_data']['surname'] = $address->getLastname(); + } + + if ($this->helper->sendAddress($order->getStoreId())) { + $data = $this->generateAddressData($address, $data); + } + + return $data; + } + + /** + * @param $address + * @param $data + * @return array + */ + private function generateAddressData($address, $data): array + { + if ($address->getCompany()) { + $data['subscriber_data']['company'] = $address->getCompany(); + } + + $data['subscriber_data']['street'] = implode(', ', $address->getStreet()); + $data['subscriber_data']['city'] = $address->getCity(); + $data['subscriber_data']['zip'] = $address->getPostcode(); + $data['subscriber_data']['country'] = $address->getCountryId(); + + if ($address->getTelephone()) { + $data['subscriber_data']['phone'] = $address->getTelephone(); + } + + return $data; + } + + /** + * @param $addressId + * @return \Magento\Customer\Api\Data\AddressInterface|null + */ + private function getDefaultAddress($addressId) + { + try { + return $this->addressRepository->getById($addressId); + } catch (Exception $e) { + return null; + } + } +} diff --git a/app/code/Ecomail/Ecomail/Model/SubscriptionManager.php b/app/code/Ecomail/Ecomail/Model/SubscriptionManager.php new file mode 100644 index 0000000..7f01cf3 --- /dev/null +++ b/app/code/Ecomail/Ecomail/Model/SubscriptionManager.php @@ -0,0 +1,148 @@ +api = $api; + $this->logger = $logger; + $this->subscriberDataMapper = $subscriberDataMapper; + $this->subscriberFactory = $subscriberFactory; + $this->scopeConfig = $scopeConfig; + } + + /** + * @param Subscriber $subscriber + */ + public function subscribe(Subscriber $subscriber) + { + try { + $this->api->addSubscriberToList($this->subscriberDataMapper->map($subscriber)); + } catch (Exception $e) { + $this->logger->error('Failed to add Ecomail subscription.', [$e]); + } + } + + /** + * @param OrderInterface $order + */ + public function subscribeFromOrder(OrderInterface $order) + { + try { + if (!$this->subscriberExists($order->getCustomerEmail())) { + $this->createLocalSubscriber($order); + } + + $this->api->addSubscriberToList($this->subscriberDataMapper->mapFromOrder($order)); + } catch (Exception $e) { + $this->logger->error('Failed to add Ecomail subscription.', [$e]); + } + } + + /** + * @param Subscriber $subscriber + */ + public function unsubscribe(Subscriber $subscriber) + { + try { + $this->api->removeSubscriberFromList($subscriber->getEmail()); + } catch (Exception $e) { + $this->logger->error('Failed to remove Ecomail subscription.', [$e]); + } + } + + /** + * @param string $customerEmail + * @return bool + */ + public function subscriberExists(string $customerEmail): bool + { + $subscriber = $this->subscriberFactory->create(); + $subscriber->loadByEmail($customerEmail); + + return $subscriber->isSubscribed(); + } + + /** + * @param OrderInterface $order + */ + private function createLocalSubscriber(OrderInterface $order) + { + $subscriber = $this->subscriberFactory->create(); + $subscriber->setSubscriberConfirmCode($subscriber->randomSequence()); + + $isConfirmationNeeded = (bool)$this->scopeConfig->getValue( + Subscriber::XML_PATH_CONFIRMATION_FLAG, + ScopeInterface::SCOPE_STORE, + $order->getStoreId() + ); + + $subscriber->setStatus( + $isConfirmationNeeded ? + Subscriber::STATUS_NOT_ACTIVE : + Subscriber::STATUS_SUBSCRIBED + ); + + $subscriber->setSubscriberEmail($order->getCustomerEmail()); + $subscriber->setStoreId($order->getStoreId()); + + if ($order->getCustomerId()) { + $subscriber->setCustomerId($order->getCustomerId()); + } + + try { + $subscriber->save(); + } catch (Exception $e) { + $this->logger->error('Failed to create local subscriber.', [$e]); + } + } +} diff --git a/app/code/Ecomail/Ecomail/Model/TransactionManager.php b/app/code/Ecomail/Ecomail/Model/TransactionManager.php new file mode 100644 index 0000000..a7d4069 --- /dev/null +++ b/app/code/Ecomail/Ecomail/Model/TransactionManager.php @@ -0,0 +1,54 @@ +api = $api; + $this->logger = $logger; + $this->transactionMapper = $transactionMapper; + } + + /** + * @param OrderInterface $order + */ + public function createTransaction(OrderInterface $order) + { + try { + $this->api->createTransaction($this->transactionMapper->map($order)); + } catch (Exception $e) { + $this->logger->error('Failed to create Ecomail transaction.', [$e]); + } + } +} diff --git a/app/code/Ecomail/Ecomail/Model/TransactionMapper.php b/app/code/Ecomail/Ecomail/Model/TransactionMapper.php new file mode 100644 index 0000000..226bf80 --- /dev/null +++ b/app/code/Ecomail/Ecomail/Model/TransactionMapper.php @@ -0,0 +1,114 @@ +helper = $helper; + $this->categoryCollectionFactory = $categoryCollectionFactory; + } + + /** + * @param OrderInterface $order + * @return array + * @throws LocalizedException + */ + public function map(OrderInterface $order): array + { + $data = []; + + $data['transaction']['order_id'] = $order->getIncrementId(); + $data['transaction']['email'] = $order->getCustomerEmail(); + $data['transaction']['shop'] = $order->getStore()->getName(); + $data['transaction']['amount'] = $order->getGrandTotal(); + $data['transaction']['tax'] = $order->getTaxAmount(); + $data['transaction']['shipping'] = $order->getShippingInclTax(); + + if ($this->helper->sendAddress($order->getStoreId())) { + /** @var AddressInterface $address */ + $address = $this->helper->useShippingAddress($order->getId()) ? + $order->getShippingAddress() : $order->getBillingAddress(); + + $data['transaction']['street'] = implode(', ', $address->getStreet()); + $data['transaction']['city'] = $address->getCity(); + $data['transaction']['zip'] = $address->getPostcode(); + $data['transaction']['country'] = $address->getCountryId(); + + if ($address->getTelephone()) { + $data['transaction']['phone'] = $address->getTelephone(); + } + } + + $items = []; + + /** @var OrderItemInterface $orderItem */ + foreach ($order->getAllVisibleItems() as $orderItem) { + // We are not interested in child products + if ($orderItem->getParentItem()) { + continue; + } + + /** @var ProductInterface $product */ + $product = $orderItem->getProduct(); + $categoryString = $this->buildCategoryString($product->getCategoryIds()); + + $items[] = [ + 'code' => $orderItem->getSku(), + 'title' => $orderItem->getName(), + 'category' => $categoryString, + 'price' => $orderItem->getPriceInclTax(), + 'amount' => $orderItem->getQtyOrdered() + ]; + } + + $data['transaction_items'] = $items; + + return $data; + } + + /** + * @param array $categoryIds + * @return string + * @throws LocalizedException + */ + private function buildCategoryString(array $categoryIds): string + { + if (empty($categoryIds)) { + return ''; + } + + $categoryCollection = $this->categoryCollectionFactory + ->create() + ->addFieldToFilter('entity_id', ['in' => $categoryIds]) + ->addAttributeToSelect('name'); + + return implode(' | ', $categoryCollection->getColumnValues('name')); + } +} diff --git a/app/code/Ecomail/Ecomail/Observer/CheckoutSubmitAllAfter.php b/app/code/Ecomail/Ecomail/Observer/CheckoutSubmitAllAfter.php deleted file mode 100644 index 831c633..0000000 --- a/app/code/Ecomail/Ecomail/Observer/CheckoutSubmitAllAfter.php +++ /dev/null @@ -1,82 +0,0 @@ -helper = $helper; - $this->categoryRepository = $categoryRepository; - } - - public function execute( \Magento\Framework\Event\Observer $observer ) { - $event = $observer->getEvent(); - $order = $observer->getOrder(); - - $addressDelivery = $order->getShippingAddress(); - - /** - * @var \Magento\Sales\Model\Order $order - * @var \Magento\Sales\Model\Order\Item $orderProduct - * @var \Magento\Catalog\Model\Product $product - */ - - $arr = array(); - foreach( $order->getAllItems() as $orderProduct ) { - $product = $orderProduct->getProduct(); - $categoryIds = $product->getCategoryIds(); - - if( count( $categoryIds ) ) { - $firstCategoryId = $categoryIds[0]; - $category = $this->categoryRepository->get( $firstCategoryId ); - - } - - if( empty( $orderProduct['price_incl_tax'] ) ) { - continue; - } - - $arr[] = array( - 'code' => $orderProduct['sku'], - 'title' => $orderProduct['name'], - 'category' => $category->getName(), - 'price' => $orderProduct['price_incl_tax'], - 'amount' => $orderProduct['qty_ordered'], - 'timestamp' => strtotime( $orderProduct['created_at'] ) - ); - } - - $data = array( - 'transaction' => array( - 'order_id' => $order->getId(), - 'email' => $order['customer_email'], - 'shop' => $order->getStore() - ->getBaseUrl( UrlInterface::URL_TYPE_LINK ), - 'amount' => $order['grand_total'], - 'tax' => $order['tax_amount'], - 'shipping' => $order['shipping_incl_tax'], - 'city' => $addressDelivery['city'], - 'county' => '', - 'country' => $addressDelivery->getCountryId(), - 'timestamp' => strtotime( $order['created_at'] ) - ), - 'transaction_items' => $arr - ); - - $r = Mage::helper( 'ecomail' ) - ->getApi() - ->createTransaction( $data ); - - return $observer; - } - } \ No newline at end of file diff --git a/app/code/Ecomail/Ecomail/Observer/CustomerLogin.php b/app/code/Ecomail/Ecomail/Observer/CustomerLogin.php new file mode 100644 index 0000000..10cbc42 --- /dev/null +++ b/app/code/Ecomail/Ecomail/Observer/CustomerLogin.php @@ -0,0 +1,47 @@ +customerSession = $customerSession; + $this->helper = $helper; + } + + /** + * @param Observer $observer + */ + public function execute(Observer $observer) + { + /** @var CustomerInterface $customer */ + $customer = $observer->getCustomer(); + + if ($this->helper->isAvailable() && $this->helper->isTrackingEnabled()) { + $this->customerSession->setEcomailEmail($customer->getEmail()); + } + } +} diff --git a/app/code/Ecomail/Ecomail/Observer/OrderPlaced.php b/app/code/Ecomail/Ecomail/Observer/OrderPlaced.php new file mode 100644 index 0000000..45bb8a4 --- /dev/null +++ b/app/code/Ecomail/Ecomail/Observer/OrderPlaced.php @@ -0,0 +1,58 @@ +helper = $helper; + $this->transactionManager = $transactionManager; + $this->subscriptionManager = $subscriptionManager; + } + + /** + * @param Observer $observer + */ + public function execute(Observer $observer) + { + /** @var OrderInterface $order */ + $order = $observer->getOrder(); + + if ($this->helper->isAvailable() && $this->helper->sendOrders()) { + $this->subscriptionManager->subscribeFromOrder($order); + $this->transactionManager->createTransaction($order); + } + } +} diff --git a/app/code/Ecomail/Ecomail/Observer/ProcessAddToCart.php b/app/code/Ecomail/Ecomail/Observer/ProcessAddToCart.php deleted file mode 100644 index e7e3ba1..0000000 --- a/app/code/Ecomail/Ecomail/Observer/ProcessAddToCart.php +++ /dev/null @@ -1,53 +0,0 @@ -helper = $helper; - } - - public function execute( \Magento\Framework\Event\Observer $observer ) { - $event = $observer->getEvent(); - /** - * @var \Magento\Catalog\Model\Product $product - */ - $product = $observer->getProduct(); - - $params = $this->helper->getRequest() - ->getParams(); - $quantity = $params['qty']; - $id_product = $product->getId(); - - setcookie( - $this->helper->getCookieNameTrackStructEvent(), - json_encode( - array( - 'category' => 'Product', - 'action' => 'AddToCart', - 'tag' => implode( - '|', - array( - $id_product - ) - ), - 'property' => 'quantity', - 'value' => $quantity - ) - ), - null, - $this->helper->getRequest() - ->getBasePath() - ); - - return $observer; - } - } \ No newline at end of file diff --git a/app/code/Ecomail/Ecomail/Observer/SubscribedToNewsletter.php b/app/code/Ecomail/Ecomail/Observer/SubscribedToNewsletter.php deleted file mode 100644 index c899791..0000000 --- a/app/code/Ecomail/Ecomail/Observer/SubscribedToNewsletter.php +++ /dev/null @@ -1,59 +0,0 @@ -helper = $helper; - $this->customerRegistry = $customerRegistry; - } - - public function execute( \Magento\Framework\Event\Observer $observer ) { - $event = $observer->getEvent(); - $subscriber = $event->getDataObject(); - $data = $subscriber->getData(); - - $statusChange = $subscriber->isStatusChanged(); - - // Trigger if user is now subscribed and there has been a status change: - if( $data['subscriber_status'] == "1" && $statusChange == true ) { - - if( $this->helper->getScopeConfig() - ->getValue( 'ecomail_options/properties/api_key' ) - ) { - - $email = $data['subscriber_email']; - $name = ''; - - $id = $subscriber['customer_id']; - if( $id ) { - $customer = $this->customerRegistry->retrieve( $id ); - $name = $customer->getName(); - } - - $this->helper->getApi() - ->subscribeToList( - $this->helper->getScopeConfig() - ->getValue( 'ecomail_options/properties/list_id' ), - array( - 'email' => $email, - 'name' => $name - ) - ); - } - - } - - return $observer; - } - } \ No newline at end of file diff --git a/app/code/Ecomail/Ecomail/Plugin/Checkout/Controller/Cart/Add.php b/app/code/Ecomail/Ecomail/Plugin/Checkout/Controller/Cart/Add.php new file mode 100644 index 0000000..e73303b --- /dev/null +++ b/app/code/Ecomail/Ecomail/Plugin/Checkout/Controller/Cart/Add.php @@ -0,0 +1,36 @@ +eventManager = $eventManager; + } + + /** + * @param \Magento\Checkout\Controller\Cart\Add $subject + * @param $result + * @return mixed + */ + public function afterExecute( + \Magento\Checkout\Controller\Cart\Add $subject, + $result + ) { + $this->eventManager->updateCartContent(); + return $result; + } +} diff --git a/app/code/Ecomail/Ecomail/Plugin/Checkout/Controller/Cart/Addgroup.php b/app/code/Ecomail/Ecomail/Plugin/Checkout/Controller/Cart/Addgroup.php new file mode 100644 index 0000000..88535bb --- /dev/null +++ b/app/code/Ecomail/Ecomail/Plugin/Checkout/Controller/Cart/Addgroup.php @@ -0,0 +1,36 @@ +eventManager = $eventManager; + } + + /** + * @param \Magento\Checkout\Controller\Cart\Addgroup $subject + * @param $result + * @return mixed + */ + public function afterExecute( + \Magento\Checkout\Controller\Cart\Addgroup $subject, + $result + ) { + $this->eventManager->updateCartContent(); + return $result; + } +} diff --git a/app/code/Ecomail/Ecomail/Plugin/Checkout/Controller/Cart/Delete.php b/app/code/Ecomail/Ecomail/Plugin/Checkout/Controller/Cart/Delete.php new file mode 100644 index 0000000..569b0fa --- /dev/null +++ b/app/code/Ecomail/Ecomail/Plugin/Checkout/Controller/Cart/Delete.php @@ -0,0 +1,36 @@ +eventManager = $eventManager; + } + + /** + * @param \Magento\Checkout\Controller\Cart\Delete $subject + * @param $result + * @return mixed + */ + public function afterExecute( + \Magento\Checkout\Controller\Cart\Delete $subject, + $result + ) { + $this->eventManager->updateCartContent(); + return $result; + } +} diff --git a/app/code/Ecomail/Ecomail/Plugin/Checkout/Controller/Cart/UpdatePost.php b/app/code/Ecomail/Ecomail/Plugin/Checkout/Controller/Cart/UpdatePost.php new file mode 100644 index 0000000..ba11fa6 --- /dev/null +++ b/app/code/Ecomail/Ecomail/Plugin/Checkout/Controller/Cart/UpdatePost.php @@ -0,0 +1,41 @@ +eventManager = $eventManager; + } + + /** + * @param OriginalUpdatePost $subject + * @param $result + * @return mixed + * @throws LocalizedException + * @throws NoSuchEntityException + */ + public function afterExecute( + OriginalUpdatePost $subject, + $result + ) { + $this->eventManager->updateCartContent(); + return $result; + } +} diff --git a/app/code/Ecomail/Ecomail/Plugin/Checkout/Controller/Sidebar/RemoveItem.php b/app/code/Ecomail/Ecomail/Plugin/Checkout/Controller/Sidebar/RemoveItem.php new file mode 100644 index 0000000..92e16ae --- /dev/null +++ b/app/code/Ecomail/Ecomail/Plugin/Checkout/Controller/Sidebar/RemoveItem.php @@ -0,0 +1,36 @@ +eventManager = $eventManager; + } + + /** + * @param \Magento\Checkout\Controller\Sidebar\RemoveItem $subject + * @param $result + * @return mixed + */ + public function afterExecute( + \Magento\Checkout\Controller\Sidebar\RemoveItem $subject, + $result + ) { + $this->eventManager->updateCartContent(); + return $result; + } +} diff --git a/app/code/Ecomail/Ecomail/Plugin/Customer/Model/AccountManagement.php b/app/code/Ecomail/Ecomail/Plugin/Customer/Model/AccountManagement.php new file mode 100644 index 0000000..f5a0736 --- /dev/null +++ b/app/code/Ecomail/Ecomail/Plugin/Customer/Model/AccountManagement.php @@ -0,0 +1,60 @@ +eventManager = $eventManager; + $this->subscriptionManager = $subscriptionManager; + $this->quoteManager = $quoteManager; + } + + /** + * @param \Magento\Customer\Model\AccountManagement $subject + * @param bool $result + * @param $customerEmail + * @return bool + */ + public function afterIsEmailAvailable( + \Magento\Customer\Model\AccountManagement $subject, + bool $result, + string $customerEmail + ): bool { + if ($this->subscriptionManager->subscriberExists($customerEmail)) { + $this->quoteManager->setQuoteEmail($customerEmail); + $this->eventManager->updateCartContent(); + } + + return $result; + } +} diff --git a/app/code/Ecomail/Ecomail/Plugin/Newsletter/Model/Subscriber.php b/app/code/Ecomail/Ecomail/Plugin/Newsletter/Model/Subscriber.php new file mode 100644 index 0000000..e03e4d6 --- /dev/null +++ b/app/code/Ecomail/Ecomail/Plugin/Newsletter/Model/Subscriber.php @@ -0,0 +1,124 @@ +subscriptionManager = $subscriptionManager; + $this->eventManager = $eventManager; + $this->quoteManager = $quoteManager; + $this->helper = $helper; + } + + /** + * @param SubscriberModel $subject + * @param int $result + * @return int|string + * @throws LocalizedException + * @throws NoSuchEntityException + */ + public function afterSubscribe( + SubscriberModel $subject, + $result + ) { + if ($this->helper->isAvailable() && $result == SubscriberModel::STATUS_SUBSCRIBED) { + $this->subscriptionManager->subscribe($subject); + $this->quoteManager->setQuoteEmail($subject->getEmail()); + $this->eventManager->updateCartContent(); + } + + return $result; + } + + /** + * @param SubscriberModel $subject + * @param SubscriberModel $result + * @return SubscriberModel + * @throws LocalizedException + * @throws NoSuchEntityException + */ + public function afterSubscribeCustomerById( + SubscriberModel $subject, + SubscriberModel $result + ) { + if ($this->helper->isAvailable() && $result->getStatus() == SubscriberModel::STATUS_SUBSCRIBED) { + $this->subscriptionManager->subscribe($subject); + $this->quoteManager->setQuoteEmail($subject->getEmail()); + $this->eventManager->updateCartContent(); + } + + return $result; + } + + /** + * @param SubscriberModel $subject + * @param SubscriberModel $result + * @return SubscriberModel + */ + public function afterUnsubscribe( + SubscriberModel $subject, + SubscriberModel $result + ): SubscriberModel { + if ($this->helper->isAvailable() && $result->getStatus() == SubscriberModel::STATUS_UNSUBSCRIBED) { + $this->subscriptionManager->unsubscribe($result); + } + + return $result; + } + + /** + * @param SubscriberModel $subject + * @param SubscriberModel $result + * @return SubscriberModel + */ + public function afterUnsubscribeCustomerById( + SubscriberModel $subject, + SubscriberModel $result + ): SubscriberModel { + if ($this->helper->isAvailable() && $result->getStatus() == SubscriberModel::STATUS_UNSUBSCRIBED) { + $this->subscriptionManager->unsubscribe($result); + } + + return $result; + } +} diff --git a/app/code/Ecomail/Ecomail/Plugin/Newsletter/Model/SubscriptionManager.php b/app/code/Ecomail/Ecomail/Plugin/Newsletter/Model/SubscriptionManager.php new file mode 100644 index 0000000..c432ea7 --- /dev/null +++ b/app/code/Ecomail/Ecomail/Plugin/Newsletter/Model/SubscriptionManager.php @@ -0,0 +1,93 @@ +subscriptionManager = $subscriptionManager; + $this->eventManager = $eventManager; + $this->quoteManager = $quoteManager; + $this->helper = $helper; + } + + /** + * @param SubscriptionManagerInterface $subject + * @param Subscriber $result + * @return Subscriber + * @throws LocalizedException + * @throws NoSuchEntityException + */ + public function afterSubscribe( + SubscriptionManagerInterface $subject, + Subscriber $result + ): Subscriber { + if ($this->helper->isAvailable()) { + $this->subscriptionManager->subscribe($result); + $this->quoteManager->setQuoteEmail($result->getEmail()); + $this->eventManager->updateCartContent(); + } + + return $result; + } + + /** + * @param SubscriptionManagerInterface $subject + * @param Subscriber $result + * @return Subscriber + * @throws LocalizedException + * @throws NoSuchEntityException + */ + public function afterSubscribeCustomer( + SubscriptionManagerInterface $subject, + Subscriber $result + ) { + if ($this->helper->isAvailable()) { + $this->subscriptionManager->subscribe($result); + $this->quoteManager->setQuoteEmail($result->getEmail()); + $this->eventManager->updateCartContent(); + } + + return $result; + } +} diff --git a/app/code/Ecomail/Ecomail/Setup/InstallSchema.php b/app/code/Ecomail/Ecomail/Setup/InstallSchema.php deleted file mode 100755 index 093cfa3..0000000 --- a/app/code/Ecomail/Ecomail/Setup/InstallSchema.php +++ /dev/null @@ -1,17 +0,0 @@ -startSetup(); - - $installer->endSetup(); - } - } diff --git a/app/code/Ecomail/Ecomail/composer.json b/app/code/Ecomail/Ecomail/composer.json old mode 100755 new mode 100644 index a6bfcd6..2c6b596 --- a/app/code/Ecomail/Ecomail/composer.json +++ b/app/code/Ecomail/Ecomail/composer.json @@ -1,29 +1,29 @@ { - "name": "ecomail/ecomail-magento2", - "description": "", - "authors": [ - { - "name": "Jan Cinert", - "email": "jan.cinert@gmail.com" + "name": "ecomain/ecomail", + "description": "System integration with Ecomail.cz services", + "license": [ + "OSL-3.0", + "AFL-3.0" + ], + "authors": [ + { + "name": "Maghos dev team", + "email": "support@maghos.com" + } + ], + "require": { + "php": "~7.0.13|~7.1.0|~7.2.0|~7.3.0|~7.4.0", + "magento/module-newsletter": "100.*.*", + "magento/module-sales": "101.*.*|102.*.*|103.*.*", + "ext-curl": "*" + }, + "type": "magento2-module", + "autoload": { + "files": [ + "registration.php" + ], + "psr-4": { + "Ecomail\\Ecomail\\": "" + } } - ], - "require": { - "php": "~5.5.0|~5.6.0", - "magento/module-config": "100.0.*", - "magento/module-store": "100.0.*", - "magento/module-sales": "100.0.*", - "magento/framework": "100.0.*" - }, - "type": "magento2-module", - "version": "1.0.0", - "license": [ - ], - "extra": { - "map": [ - [ - "*", "Ecomail/Ecomail" - ] - ] - } - } diff --git a/app/code/Ecomail/Ecomail/etc/acl.xml b/app/code/Ecomail/Ecomail/etc/acl.xml old mode 100755 new mode 100644 index 641f529..b4a0938 --- a/app/code/Ecomail/Ecomail/etc/acl.xml +++ b/app/code/Ecomail/Ecomail/etc/acl.xml @@ -1,13 +1,14 @@ - + - - + diff --git a/app/code/Ecomail/Ecomail/etc/adminhtml/routes.xml b/app/code/Ecomail/Ecomail/etc/adminhtml/routes.xml index 343e83b..97bb661 100644 --- a/app/code/Ecomail/Ecomail/etc/adminhtml/routes.xml +++ b/app/code/Ecomail/Ecomail/etc/adminhtml/routes.xml @@ -2,7 +2,7 @@ - + - \ No newline at end of file + diff --git a/app/code/Ecomail/Ecomail/etc/adminhtml/system.xml b/app/code/Ecomail/Ecomail/etc/adminhtml/system.xml old mode 100755 new mode 100644 index 3176e90..1e31567 --- a/app/code/Ecomail/Ecomail/etc/adminhtml/system.xml +++ b/app/code/Ecomail/Ecomail/etc/adminhtml/system.xml @@ -1,34 +1,133 @@ - - + - +
- -
- - ecomail_config - Ecomail_Ecomail::ecomail_config - - - - - - - - Vyberte list do kterého budou zapsáni noví zákazníci - Ecomail\Ecomail\Model\OptionsListId - - - - Tento údaj slouží pro aktivaci funkce Trackovací kód + customer + Ecomail_Ecomail::ecomail_configuration + + + + + Ecomail\Ecomail\Block\System\Config\Form\Field\Status + + + + Magento\Config\Model\Config\Source\Yesno + + + + required-entry + + 1 + + + + + Choose a subscriber list from your Ecomail account. Please note it can take up to 30 minutes for the newly created list to be available. + required-entry + + 1 + + Ecomail\Ecomail\Model\Config\Source\SubscriberList + + + Ecomail\Ecomail\Block\System\Config\Form\Field\Subscriber\LoadLists + + 1 + + + + + + 1 + + Magento\Config\Model\Config\Source\Yesno + + + + Runs automated campaigns after adding a new subscription. + + 1 + + Magento\Config\Model\Config\Source\Yesno + + + + + Choose which information should be shared with Ecomail. + + 1 + + + + Magento\Config\Model\Config\Source\Yesno + + + + Magento\Config\Model\Config\Source\Yesno + + + + required-entry + + 1 + 1 + + Ecomail\Ecomail\Model\Config\Source\Address + + + + Magento\Config\Model\Config\Source\Yesno + + + + Magento\Config\Model\Config\Source\Yesno + + + + Cart items will be sent to Ecomail to support abandoned cart features. + Magento\Config\Model\Config\Source\Yesno + + + + + + 1 + + + + Magento\Config\Model\Config\Source\Yesno + + + + required-entry + + 1 + 1 +
- \ No newline at end of file + diff --git a/app/code/Ecomail/Ecomail/etc/config.xml b/app/code/Ecomail/Ecomail/etc/config.xml new file mode 100644 index 0000000..384d5e8 --- /dev/null +++ b/app/code/Ecomail/Ecomail/etc/config.xml @@ -0,0 +1,24 @@ + + + + + + 0 + 1 + 0 + + + 1 + 1 + 1 + 1 + 1 + 1 + + + 0 + + + + diff --git a/app/code/Ecomail/Ecomail/etc/di.xml b/app/code/Ecomail/Ecomail/etc/di.xml old mode 100755 new mode 100644 index a50f79e..639b9e6 --- a/app/code/Ecomail/Ecomail/etc/di.xml +++ b/app/code/Ecomail/Ecomail/etc/di.xml @@ -1,9 +1,16 @@ - - \ No newline at end of file + + + + + Magento\Checkout\Model\Session\Proxy + + + + + + + + + diff --git a/app/code/Ecomail/Ecomail/etc/events.xml b/app/code/Ecomail/Ecomail/etc/events.xml index 448d85c..ad66661 100644 --- a/app/code/Ecomail/Ecomail/etc/events.xml +++ b/app/code/Ecomail/Ecomail/etc/events.xml @@ -1,18 +1,6 @@ - - - - - - - - - + + diff --git a/app/code/Ecomail/Ecomail/etc/frontend/di.xml b/app/code/Ecomail/Ecomail/etc/frontend/di.xml new file mode 100644 index 0000000..05e7385 --- /dev/null +++ b/app/code/Ecomail/Ecomail/etc/frontend/di.xml @@ -0,0 +1,40 @@ + + + + + + Ecomail\Ecomail\CustomerData\Ecomail + + + + + + Magento\Customer\Model\Session\Proxy + + + + + Magento\Customer\Model\Session\Proxy + + + + + Magento\Checkout\Model\Session\Proxy + + + + + + + + + + + + + + + + + + diff --git a/app/code/Ecomail/Ecomail/etc/frontend/events.xml b/app/code/Ecomail/Ecomail/etc/frontend/events.xml new file mode 100644 index 0000000..025c1df --- /dev/null +++ b/app/code/Ecomail/Ecomail/etc/frontend/events.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/code/Ecomail/Ecomail/etc/frontend/sections.xml b/app/code/Ecomail/Ecomail/etc/frontend/sections.xml new file mode 100644 index 0000000..2cf5aa1 --- /dev/null +++ b/app/code/Ecomail/Ecomail/etc/frontend/sections.xml @@ -0,0 +1,7 @@ + + + +
+ + diff --git a/app/code/Ecomail/Ecomail/etc/module.xml b/app/code/Ecomail/Ecomail/etc/module.xml old mode 100755 new mode 100644 index 3d63498..6fa3db9 --- a/app/code/Ecomail/Ecomail/etc/module.xml +++ b/app/code/Ecomail/Ecomail/etc/module.xml @@ -1,13 +1,8 @@ - - - + + - + diff --git a/app/code/Ecomail/Ecomail/etc/webapi_rest/di.xml b/app/code/Ecomail/Ecomail/etc/webapi_rest/di.xml new file mode 100644 index 0000000..52a53ff --- /dev/null +++ b/app/code/Ecomail/Ecomail/etc/webapi_rest/di.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/code/Ecomail/Ecomail/i18n/cs_CZ.csv b/app/code/Ecomail/Ecomail/i18n/cs_CZ.csv new file mode 100644 index 0000000..d76f005 --- /dev/null +++ b/app/code/Ecomail/Ecomail/i18n/cs_CZ.csv @@ -0,0 +1,28 @@ +"Load Lists","Načíst seznamy" +"Ecomail api error: ","Ecomail api error: " +"Loading ...","Načítám ..." +"Ecomail Section","Ecomail Sekce" +Ecomail,Ecomail +General,Obecné +Enabled,Zapnout +"API Key","API Klíč" +"Subscriber List","Seznam kontaktů" +"Choose a subscriber list from your Ecomail account. Please note it can take up to 30 minutes for the newly created list to be available.","Zvolte seznam kontaktů z vašeho Ecomail účtu. Upozorňujeme, že může trvat až 30 minut než se nově vytvořený seznam objeví." +"Skip Double opt-in","Přeskočit Double opt-in" +"Personal Information","Osobní informace" +"Choose which information should be shared with Ecomail.","Zvolte které informace chcete sdílet se službou Ecomail." +"Customer name","Jméno zákazníka" +"Customer address","Adresa" +"Customer DOB","Datum narození" +"Order data","Data objednávek" +"Cart items","Položky košíku" +"Cart items will be sent to Ecomail to support abandoned cart features.","Cart items will be sent to Ecomail to support abandoned cart features." +"Behavior Tracking","Sledování chování" +"App ID","App ID" +"Status","Stav" +"Active","Aktivní" +"Inactive","Neaktivní" +"Error","Chyba" +"Address type","Typ adresy" +"Trigger Autoresponders","Spouštět automatizované odpovědi" +"Runs automated campaigns after adding a new subscription.","Spustí automatizované kampaně po přidání nového kontaktu." diff --git a/app/code/Ecomail/Ecomail/i18n/en_US.csv b/app/code/Ecomail/Ecomail/i18n/en_US.csv new file mode 100644 index 0000000..29682cc --- /dev/null +++ b/app/code/Ecomail/Ecomail/i18n/en_US.csv @@ -0,0 +1,28 @@ +"Load Lists","Load Lists" +"Ecomail api error: ","Ecomail api error: " +"Loading ...","Loading ..." +"Ecomail Section","Ecomail Section" +Ecomail,Ecomail +General,General +Enabled,Enabled +"API Key","API Key" +"Subscriber List","Subscriber List" +"Choose a subscriber list from your Ecomail account. Please note it can take up to 30 minutes for the newly created list to be available.","Choose a subscriber list from your Ecomail account. Please note it can take up to 30 minutes for the newly created list to be available." +"Skip Double opt-in","Skip Double opt-in" +"Personal Information","Personal Information" +"Choose which information should be shared with Ecomail.","Choose which information should be shared with Ecomail." +"Customer name","Customer name" +"Customer address","Customer address" +"Customer DOB","Customer DOB" +"Order data","Order data" +"Cart items","Cart items" +"Cart items will be sent to Ecomail to support abandoned cart features.","Cart items will be sent to Ecomail to support abandoned cart features." +"Behavior Tracking","Behavior Tracking" +"App ID","App ID" +"Status","Status" +"Active","Active" +"Inactive","Inactive" +"Error","Error" +"Address type","Address type" +"Trigger Autoresponders","Trigger Autoresponders" +"Runs automated campaigns after adding a new subscription.","Runs automated campaigns after adding a new subscription." diff --git a/app/code/Ecomail/Ecomail/lib/api.php b/app/code/Ecomail/Ecomail/lib/api.php deleted file mode 100644 index aea0925..0000000 --- a/app/code/Ecomail/Ecomail/lib/api.php +++ /dev/null @@ -1,99 +0,0 @@ -APIKey = $arg; - - return $this; - } - - public function getListsCollection() { - - return $this->call( 'lists' ); - - } - - public function subscribeToList( $listId, $customerData ) { - - return $this->call( - sprintf( - 'lists/%d/subscribe', - $listId - ), - 'POST', - array( - 'subscriber_data' => $customerData - ) - ); - - } - - public function createTransaction( $data ) { - - return $this->call( - 'tracker/transaction', - 'POST', - $data - ); - - } - - protected function call( $url, $method = 'GET', $data = null ) { - $ch = curl_init(); - - curl_setopt( - $ch, - CURLOPT_URL, - "http://api2.ecomailapp.cz/" . $url - ); - curl_setopt( - $ch, - CURLOPT_RETURNTRANSFER, - TRUE - ); - curl_setopt( - $ch, - CURLOPT_HEADER, - FALSE - ); - curl_setopt( - $ch, - CURLOPT_HTTPHEADER, - array( - "Content-Type: application/json", - 'Key: ' . $this->APIKey - ) - ); - - if( in_array( - $method, - array( - 'POST', - 'PUT' - ) - ) ) { - - curl_setopt( - $ch, - CURLOPT_CUSTOMREQUEST, - $method - ); - - curl_setopt( - $ch, - CURLOPT_POSTFIELDS, - json_encode( $data ) - ); - - } - - $response = curl_exec( $ch ); - curl_close( $ch ); - - return json_decode( $response ); - } - - } \ No newline at end of file diff --git a/app/code/Ecomail/Ecomail/registration.php b/app/code/Ecomail/Ecomail/registration.php old mode 100755 new mode 100644 index 516efbe..c596353 --- a/app/code/Ecomail/Ecomail/registration.php +++ b/app/code/Ecomail/Ecomail/registration.php @@ -1,4 +1,4 @@ - - - - - - - - diff --git a/app/code/Ecomail/Ecomail/view/adminhtml/requirejs-config.js b/app/code/Ecomail/Ecomail/view/adminhtml/requirejs-config.js deleted file mode 100644 index 27f4a8c..0000000 --- a/app/code/Ecomail/Ecomail/view/adminhtml/requirejs-config.js +++ /dev/null @@ -1,7 +0,0 @@ -var config = { - map: { - '*': { - EcomailBackOffice: 'Ecomail_Ecomail/back' - } - } -}; \ No newline at end of file diff --git a/app/code/Ecomail/Ecomail/view/adminhtml/templates/footer.phtml b/app/code/Ecomail/Ecomail/view/adminhtml/templates/footer.phtml deleted file mode 100644 index d779fe5..0000000 --- a/app/code/Ecomail/Ecomail/view/adminhtml/templates/footer.phtml +++ /dev/null @@ -1,41 +0,0 @@ -helper( '\Magento\Backend\Helper\Data' ); - - $html = << - require(['EcomailBackOffice'], function(EcomailBackOffice) { - EcomailBackOffice.init({1}); - }); - -HTML; - - $html = strtr( - $html, - array( - '{1}' => json_encode( - array( - 'formFieldAPIKey' => 'ecomail_options_properties_api_key', - 'formFieldList' => 'ecomail_options_properties_list_id', - 'formFieldRowSelector' => 'tr', - 'ajaxUrl' => $helper->getUrl( 'adminhtml/ecomail/ajax' ), - 'templates' => array( - 'connect' => << - - - - - - - - -HTML - - ) - ) - ) - ) - ); - - echo $html; \ No newline at end of file diff --git a/app/code/Ecomail/Ecomail/view/adminhtml/templates/system/config/form/field/status.phtml b/app/code/Ecomail/Ecomail/view/adminhtml/templates/system/config/form/field/status.phtml new file mode 100644 index 0000000..829e2e9 --- /dev/null +++ b/app/code/Ecomail/Ecomail/view/adminhtml/templates/system/config/form/field/status.phtml @@ -0,0 +1,25 @@ + +getStatusCode() ?> + + + + diff --git a/app/code/Ecomail/Ecomail/view/adminhtml/templates/system/config/form/field/subscriber/load_lists.phtml b/app/code/Ecomail/Ecomail/view/adminhtml/templates/system/config/form/field/subscriber/load_lists.phtml new file mode 100644 index 0000000..b8b80df --- /dev/null +++ b/app/code/Ecomail/Ecomail/view/adminhtml/templates/system/config/form/field/subscriber/load_lists.phtml @@ -0,0 +1,49 @@ + + +getButtonHtml() ?> + + \ No newline at end of file diff --git a/app/code/Ecomail/Ecomail/view/adminhtml/web/back.js b/app/code/Ecomail/Ecomail/view/adminhtml/web/back.js deleted file mode 100644 index ea08b20..0000000 --- a/app/code/Ecomail/Ecomail/view/adminhtml/web/back.js +++ /dev/null @@ -1,100 +0,0 @@ -define( [ - "jquery" -], function( jQuery ) { - - var EcomailBackOffice = (function() { - - var hereDoc = function( f ) { - return f.toString().replace( /^[^\/]+\/\*!?/, '' ).replace( /\*\/[^\/]+$/, '' ); - }; - - var _module = { - - init: function( config ) { - var config = jQuery.extend( { - formFieldAPIKey: null, - formFieldList: null, - formFieldRowSelector: '.form-group', - buttonConnect: 'ecConnect', - ajaxUrl: null, - templates: { - connect: hereDoc( function() {/*! -
- -
- -
-
-*/ - } ) - } - }, config ); - - var $ = jQuery; - - $( function() { - var ecApiKey = $( '#' + config.formFieldAPIKey ); - var ecSubmitButton = $( '.panel-footer button[type="submit"]' ); - var ecSelectList = $( '#' + config.formFieldList ); - var ecSelectListGroup = ecSelectList.parents( config.formFieldRowSelector ); - - var remoteFormShown = false; - - if( ecApiKey.val() == '' ) { - ecInitRemoteForm(); - } - - ecApiKey.on( 'keyup', function() { - ecInitRemoteForm(); - } ); - - function ecInitRemoteForm() { - if( remoteFormShown == false ) { - ecSubmitButton.hide(); - ecSelectListGroup.hide(); - - var html = config.templates.connect; - html = html.replace( '{BUTTON_CONNECT}', config.buttonConnect ); - $( html ).insertAfter( ecApiKey.closest( config.formFieldRowSelector ) ); - $( '#' + config.buttonConnect ).on( 'click', function( e ) { - - var $this = $( this ); - - e.preventDefault(); - - $this.val( 'Připojuji...' ); - - var params = { - cmd: 'getLists', APIKey: ecApiKey.val() - }; - if( window.FORM_KEY ) { - params['form_key'] = window.FORM_KEY; - } - - $.ajax( { - url: config.ajaxUrl, data: params, type: 'get', dataType: 'json', success: function( data ) { - ecSelectList.html( '' ); - $.each( data, function( key, val ) { - ecSelectList.append( '' ); - } ); - ecSelectListGroup.show(); - ecSubmitButton.show(); - $this.parents( config.formFieldRowSelector ).remove(); - remoteFormShown = false; - } - } ); - } ); - - remoteFormShown = true; - } - } - } ); - } - }; - - return _module; - - })(); - - return EcomailBackOffice; -} ); \ No newline at end of file diff --git a/app/code/Ecomail/Ecomail/view/frontend/layout/default.xml b/app/code/Ecomail/Ecomail/view/frontend/layout/default.xml index f8e96b8..980a4c5 100644 --- a/app/code/Ecomail/Ecomail/view/frontend/layout/default.xml +++ b/app/code/Ecomail/Ecomail/view/frontend/layout/default.xml @@ -1,11 +1,16 @@ - + - - + + + + + 86400 + + + diff --git a/app/code/Ecomail/Ecomail/view/frontend/requirejs-config.js b/app/code/Ecomail/Ecomail/view/frontend/requirejs-config.js deleted file mode 100644 index e34992c..0000000 --- a/app/code/Ecomail/Ecomail/view/frontend/requirejs-config.js +++ /dev/null @@ -1,7 +0,0 @@ -var config = { - map: { - '*': { - EcomailFront: 'Ecomail_Ecomail/front' - } - } -}; \ No newline at end of file diff --git a/app/code/Ecomail/Ecomail/view/frontend/templates/footer.phtml b/app/code/Ecomail/Ecomail/view/frontend/templates/footer.phtml deleted file mode 100644 index ee1aa72..0000000 --- a/app/code/Ecomail/Ecomail/view/frontend/templates/footer.phtml +++ /dev/null @@ -1,59 +0,0 @@ -helper( '\Ecomail\Ecomail\Helper\Data' ); - $appId = $helper->getScopeConfig() - ->getValue( 'ecomail_options/properties/app_id' ); - - if( $appId ) { - - $html = << - require(['EcomailFront'], function(EcomailFront){ - EcomailFront.init({1}); - }); - -HTML; - - $html = strtr( - $html, - array( - '{1}' => json_encode( - array( - 'basePath' => $helper->getRequest() - ->getBasePath(), - 'cookieNameTrackStructEvent' => $helper->getCookieNameTrackStructEvent() - ) - ) - ) - ); - - echo $html; - - $html = << - - -HTML; - - $html = strtr( - $html, - array( - '{1}' => json_encode( - array( - 'appId' => $appId - ) - ) - ) - ); - - echo $html; - } \ No newline at end of file diff --git a/app/code/Ecomail/Ecomail/view/frontend/templates/tracking.phtml b/app/code/Ecomail/Ecomail/view/frontend/templates/tracking.phtml new file mode 100644 index 0000000..8eb51cd --- /dev/null +++ b/app/code/Ecomail/Ecomail/view/frontend/templates/tracking.phtml @@ -0,0 +1,32 @@ +helper(\Ecomail\Ecomail\Helper\Data::class); +?> +isTrackingEnabled()): ?> + + + + diff --git a/app/code/Ecomail/Ecomail/view/frontend/web/front.js b/app/code/Ecomail/Ecomail/view/frontend/web/front.js deleted file mode 100644 index df92b7d..0000000 --- a/app/code/Ecomail/Ecomail/view/frontend/web/front.js +++ /dev/null @@ -1,87 +0,0 @@ -define( [ - "jquery" -], function( jQuery ) { - - var EcomailFront = (function() { - - var _module = { - - init: function( config ) { - var config = jQuery.extend( { - basePath: null, ajaxUrl: null, cookieNameTrackStructEvent: null - }, config ); - - basePath = config.basePath; - - var $ = jQuery; - - $( document ).ajaxComplete( function() { - processTrackStructEvent( config ); - } ); - - processTrackStructEvent( config ); - } - - }; - - var basePath; - var processTrackStructEvent = function( config ) { - - var v = readCookie( config.cookieNameTrackStructEvent ); - if( v ) { - - try { - v = jQuery.parseJSON( v ); - if( v ) { - console.log( 'ecotrack' ); - window.ecotrack( 'trackStructEvent', v.category, v.action, v.tag, v.property, v.value ); - } - } - catch( e ) { - - } - - eraseCookie( config.cookieNameTrackStructEvent ); - } - }; - - function createCookie( name, value, days ) { - var expires; - - if( days ) { - var date = new Date(); - date.setTime( date.getTime() + (days*24*60*60*1000) ); - expires = "; expires=" + date.toGMTString(); - } - else { - expires = ""; - } - - var path = basePath.replace( /\/$/, '' ); - if( !path ) { - path = '/'; - } - document.cookie = encodeURIComponent( name ) + "=" + encodeURIComponent( value ) + expires + "; path=" + path; - } - - function readCookie( name ) { - var nameEQ = name + "="; - var ca = document.cookie.split( ';' ); - for( var i = 0; i < ca.length; i++ ) { - var c = ca[i]; - while( c.charAt( 0 ) == ' ' ) c = c.substring( 1, c.length ); - if( c.indexOf( nameEQ ) == 0 ) return decodeURIComponent( c.substring( nameEQ.length, c.length ) ); - } - return null; - } - - function eraseCookie( name ) { - createCookie( name, "", -1 ); - } - - return _module; - - })(); - - return EcomailFront; -} ); \ No newline at end of file