Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Block/Adminhtml/System/Config/Fieldset/Hint.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,11 @@ public function getScopeId()
}
return $scopeId;
}
public function getRegisterToken()
{
$scopeId = $this->getScopeId();
$scope = $this->getScope();
$token = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_STATISTICS_TOKEN, $scopeId, $scope);
return $token;
}
}
20 changes: 15 additions & 5 deletions Controller/Adminhtml/Ecommerce/Getaccountdetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use \Ebizmarts\MailChimp\Helper\Data as MailChimpHelper;

class Getaccountdetails extends Action
{
/**
* @var \Ebizmarts\MailChimp\Helper\Data
* @var MailChimpHelper
*/
protected $_helper;
/**
Expand All @@ -33,15 +34,14 @@ class Getaccountdetails extends Action
protected $_storeManager;

/**
* Getaccountdetails constructor.
* @param Context $context
* @param \Magento\Store\Model\StoreManager $storeManager
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param MailChimpHelper $helper
*/
public function __construct(
Context $context,
\Magento\Store\Model\StoreManager $storeManager,
\Ebizmarts\MailChimp\Helper\Data $helper
MailChimpHelper $helper
) {

parent::__construct($context);
Expand All @@ -55,6 +55,8 @@ public function execute()
$apiKey = $param['apikey'];
$store = $param['store'];
$encrypt = $param['encrypt'];
$scope = $param['scope'];
$scopeId = $param['scopeId'];
try {
if ($encrypt == 3) {
$api = $this->_helper->getApi($this->_storeManager->getStore()->getId());
Expand All @@ -64,14 +66,22 @@ public function execute()
$apiInfo = $api->root->info();
$options = [];
if (isset($apiInfo['account_name'])) {
$options['username'] = ['label' => __('User name:'), 'value' => $apiInfo['account_name']];
$options['account_name'] = ['code' => 'account_name','html' => __('Account Name'),'value' => $apiInfo['account_name']];
$options['email'] = ['code' => 'email','html' => __('email'),'value' => $apiInfo['email']];
$options['first_name'] = ['code'=> 'first_name','html' => __('First Name'),'value' => $apiInfo['first_name']];
$options['last_name'] = ['code'=>'last_name','html' => __('Last Name'),'value' => $apiInfo['last_name']];
$options['pricing_plan_type'] = ['code'=>'pricing_plan_type','html' => __('Pricing Plan'),'value' => $apiInfo['pricing_plan_type']];
$options['username'] = ['code'=>'username','html' => __('User name:'), 'value' => $apiInfo['account_name']];
$options['account_id'] = ['code'=> 'account_id','html' => __('Account id:'), 'value' => $apiInfo['account_id']];
$options['total_subscribers'] = ['label' => __('Total Account Subscribers:'), 'value' => $apiInfo['total_subscribers']];
$token = $this->_helper->getConfigValue(MailChimpHelper::XML_STATISTICS_TOKEN, $scopeId, $scope);
if ($store != -1) {
$storeData = $api->ecommerce->stores->get($store);
$options['list_id'] = $storeData['list_id'];
$list = $api->lists->getLists($storeData['list_id']);
$options['list_name'] = $list['name'];
$options['total_list_subscribers'] = ['label' => __('Total List Subscribers:'), 'value' => $list['stats']['member_count']];
$options['token'] = ['label' => __('Registration ID:'), 'value' => $token];
$options['subtitle'] = ['label' => __('Ecommerce Data uploaded to MailChimp:'), 'value' => ''];
$totalCustomers = $api->ecommerce->customers->getAll($store, 'total_items');
$options['total_customers'] = ['label' => __('Total customers:'), 'value' => $totalCustomers['total_items']];
Expand Down
98 changes: 98 additions & 0 deletions Controller/Adminhtml/Ecommerce/Register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Ebizmarts\MailChimp\Controller\Adminhtml\Ecommerce;

use Magento\Backend\App\Action;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Store\Model\StoreManager;
use Magento\Backend\App\Action\Context;
use Magento\Config\Model\ResourceModel\Config;
use Ebizmarts\MailChimp\Helper\Data as Helper;
use Ebizmarts\MailChimp\Helper\Http as MailChimpHttp;

class Register extends Action
{
/**
* @var ResultFactory
*/
protected $_resultFactory;
/**
* @var Helper
*/
protected $_helper;
/**
* @var MailChimpHttp
*/
protected $_http;
/**
* @var Config
*/
protected $_config;
/**
* @var StoreManager
*/
protected $_storeManager;

/**
* @param Context $context
* @param Helper $helper
* @param MailChimpHttp $http
* @param Config $config
* @param StoreManager $storeManager
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function __construct(
Context $context,
Helper $helper,
MailChimpHttp $http,
Config $config,
StoreManager $storeManager
)
{
parent::__construct($context);
$this->_resultFactory = $context->getResultFactory();
$this->_helper = $helper;
$this->_http = $http;
$this->_http->setUrl($helper->getConfigValue(Helper::XML_REGISTER_URL).'/register');
$this->_config = $config;
$this->_storeManager = $storeManager;
}
public function execute()
{
$params = $this->getRequest()->getParams();
$scope = $params['scope'];
$scopeId = $params['scopeId'];
$registerData = [];
$error = true;
$token = $this->_helper->getConfigValue(Helper::XML_STATISTICS_TOKEN,$scopeId, $scope);
foreach ($params['data'] as $index => $value) {
$registerData[$index] = $value['value'];
}
$registerData['store_url'] = stripslashes($this->_storeManager->getStore($scopeId)->getBaseUrl());
$registerDataJson = json_encode($registerData);
$resultJson = $this->_resultFactory->create(ResultFactory::TYPE_JSON);
if ($token) {
$ret = $this->_http->patch($token, $registerDataJson);
} else {
$ret = $this->_http->post($registerDataJson);
}
$ret = json_decode($ret,true);
if ( !$ret['error']) {
$error = false;
$token = $ret['token'];
$this->_helper->saveConfigValue(Helper::XML_STATISTICS_TOKEN, $token, $scopeId, $scope);
}
$resultJson->setData(['error' => $error, 'token' => $token]);
return $resultJson;

}
/**
* @return mixed
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Ebizmarts_MailChimp::config_mailchimp');
}

}
67 changes: 67 additions & 0 deletions Controller/Adminhtml/Ecommerce/SyncLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Ebizmarts\MailChimp\Controller\Adminhtml\Ecommerce;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Ebizmarts\MailChimp\Helper\Data as MailchimpHelper;
use Ebizmarts\MailChimp\Helper\Http as MailChimpHttp;


class SyncLog extends Action
{
/**
* @var ResultFactory
*/
protected $_resultFactory;
/**
* @var MailChimpHttp
*/
protected $_http;
/**
* @var MailchimpHelper
*/
protected $_helper;
public function __construct(
Context $context,
MailchimpHelper $mailchimpHelper,
MailchimpHttp $mailchimpHttp
)
{
parent::__construct($context);
$this->_resultFactory = $context->getResultFactory();
$this->_helper = $mailchimpHelper;
$this->_http = $mailchimpHttp;
$this->_url = $mailchimpHelper->getConfigValue(MailchimpHelper::XML_REGISTER_URL);
}
public function execute()
{
$error = 0;
$message = '';
$params = $this->getRequest()->getParams();
$scope = $params['scope'];
$scopeId = $params['scopeId'];
$onoff = $params['onoff'];
$token = $this->_helper->getConfigValue(MailchimpHelper::XML_STATISTICS_TOKEN, $scopeId, $scope);
if ($token) {
if ($onoff) {
$this->_http->setUrl($this->_url . '/switchon');
} else {
$this->_http->setUrl($this->_url . '/switchoff');
}
$response = $this->_http->put($token, null);
$res = json_decode($response, true);
if (key_exists('error',$res)) {
$error = $res['error'];
$message = $res['message'];
}
} else {
$error = 1;
$message = 'First register your copy';
}
$resultJson = $this->_resultFactory->create(ResultFactory::TYPE_JSON);
$resultJson->setData(['error' => $error, 'message' => $message]);
return $resultJson;
}
}
52 changes: 43 additions & 9 deletions Cron/SyncStatistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Ebizmarts\MailChimp\Cron;

use Magento\Store\Model\StoreManager;
use Ebizmarts\MailChimp\Helper\Data as MailChimpHelper;
use Ebizmarts\MailChimp\Model\ResourceModel\MailchimpNotification\CollectionFactory as MailchimpNotificationCollectionFactory;
use Ebizmarts\MailChimp\Model\ResourceModel\MailchimpNotification;
use Ebizmarts\MailChimp\Helper\Http as MailChimpHttp;
use const _PHPStan_7c8075089\__;

class SyncStatistics
{
Expand All @@ -26,38 +26,63 @@ class SyncStatistics
* @var MailChimpHttp
*/
private $mailchimpHttp;
private $storeManager;
const MAX_NOTIFICATIONS = 100;

public function __construct(
MailChimpHelper $helper,
MailchimpNotificationCollectionFactory $mailchimpNotificationCollectionFactory,
MailchimpNotification $mailchimpNotification,
MailchimpHttp $mailchimpHttp
MailchimpHttp $mailchimpHttp,
StoreManager $storeManager
)
{
$this->helper = $helper;
$this->mailchimpNotificationCollectionFactory = $mailchimpNotificationCollectionFactory;
$this->mailchimpNotification = $mailchimpNotification;
$this->mailchimpHttp = $mailchimpHttp;
$mailchimpHttp->setUrl($helper->getConfigValue(MailChimpHelper::SYNC_NOTIFICATION_URL));
$this->storeManager = $storeManager;
}
public function execute()
{
$count = 0;
$this->helper->log("SyncStatistics");
if ($this->helper->isSupportEnabled())
{
$scopeId = $this->storeManager->getDefaultStoreView()->getId();
$scope = 'default';
$token = $this->helper->getConfigValue(MailChimpHelper::XML_STATISTICS_TOKEN, $scopeId, $scope);
if (!$token) {
$this->helper->log("You must first register your copy to sync statistics");
return;
}
$this->mailchimpHttp->setUrl($this->helper->getConfigValue(MailChimpHelper::XML_REGISTER_URL).'/logenabled');
$response = $this->mailchimpHttp->get($token);
$res = json_decode($response, true);
if (key_exists('error',$res) && $res['error']) {
$this->helper->log("Something went wrong while syncing statistics");
return;
} elseif (key_exists('enabled',$res) && !$res['enabled']) {
$this->helper->log("You are not authorized to sync statistics");
return;
}
$this->helper->log("Processing sync statistics");
$this->mailchimpHttp->setUrl($this->helper->getConfigValue(MailChimpHelper::SYNC_NOTIFICATION_URL)."/$token");
$collection = $this->getCollection();
/**
* @var $collectionItem \Ebizmarts\MailChimp\Model\MailChimpNotification
*/
foreach ($collection as $collectionItem)
{
$this->syncData($collectionItem->getNotificationData());
$collectionItem->setProcessed(true);
$collectionItem->setSyncedAt($this->helper->getGmtDate());
$collectionItem->getResource()->save($collectionItem);
$count++;
if($this->syncData($collectionItem->getNotificationData())) {
$collectionItem->setProcessed(true);
$collectionItem->setSyncedAt($this->helper->getGmtDate());
$collectionItem->getResource()->save($collectionItem);
$count++;
} else {
break;
}
}
$this->helper->log("Sync statistics $count registers processed");
} else {
Expand All @@ -76,10 +101,19 @@ private function getCollection()
}
private function syncData($data)
{
$continue = true;
$response = $this->mailchimpHttp->post($data);
if (!$this->mailchimpHttp->extractResponse($response)) {
$this->helper->log("Invalid JSON, syncing process will continue regardless");
switch($this->mailchimpHttp->extractResponse($response)) {
case MailchimpHttp::ERROR_GENERIC:
break;
case MailChimpHttp::ERROR_AUTH:
$continue = false;
break;
case MailChimpHttp::ERROR_JSON:
$this->helper->log("Invalid JSON, syncing process will continue regardless");
break;
}
return $continue;
}
private function cleanData()
{
Expand Down
8 changes: 8 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
const SYNC_TOKEN = 'mailchimp/statistics/token';
const SYNC_NOTIFICATION_URL = 'mailchimp/statistics/notification_url';
const XML_CLEAN_SUPPORT_PERIOD = 'mailchimp/general/clean_support_period';
const XML_REGISTER_URL = 'mailchimp/statistics/register_url';
const XML_STATISTICS_TOKEN = 'mailchimp/register/token';

const ORDER_STATE_OK = 'complete';

Expand Down Expand Up @@ -1248,6 +1250,12 @@ public function buttonPressed($button, $result)
$data['button']['action'] = $button;
$data['button']['result'] = $result;
$this->saveNotification($data);
}
public function switchLog($on)
{
$storeId = $this->_storeManager->getDefaultStoreView()->getId();
$scope = 'default';
$token = $this->getConfigValue(self::XML_STATISTICS_TOKEN, $storeId, $scope);

}
}
Loading