Skip to content

Commit

Permalink
closes #68
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzaloebiz committed Aug 28, 2017
1 parent 25683fb commit b9f45ab
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 39 deletions.
3 changes: 3 additions & 0 deletions Controller/Adminhtml/Stores/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public function execute()
$result = [];
foreach ($stores['stores'] as $store) {
if ($store['platform'] == \Ebizmarts\MailChimp\Helper\Data::PLATFORM) {
if($store['list_id']=='') {
continue;
}
$list = $api->lists->getLists($store['list_id']);
$result[] = ['id' => $store['id'], 'name' => $store['name'], 'list_name' => $list['name'], 'list_id' => $store['list_id']];
}
Expand Down
1 change: 1 addition & 0 deletions Controller/Adminhtml/Stores/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ protected function _updateMailchimp($formData)
} else {
$date = $this->_helper->getDateMicrotime();
$mailchimpStoreId = md5($name. '_' . $date);
$is_sync = true;
$ret =$api->ecommerce->stores->add(
$mailchimpStoreId,
$formData['list_id'],
Expand Down
6 changes: 4 additions & 2 deletions Cron/Ecommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,12 @@ protected function _processStore($storeId, $mailchimpStoreId, $listId)
}
}
$countTotal = $countCustomers + $countProducts + $countOrders + $countSubscribers;
if ($countTotal == 0 && $this->_helper->getMCMinSyncing($storeId)) {
$alreadySynced = $this->_helper->getMCMinSyncing($storeId);
$this->_helper->log("Already Synced $alreadySynced total $countTotal");
if ($countTotal == 0 && !$alreadySynced) {
$api = $this->_helper->getApi($storeId);
$api->ecommerce->stores->edit($mailchimpStoreId, null, null, null, null, null, null, null, null, null, null, false);
$this->_helper->saveConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_IS_SYNC, true, $storeId);
$this->_helper->saveConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_IS_SYNC, date('Y-m-d His'), $storeId);
}
}

Expand Down
25 changes: 10 additions & 15 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,12 @@ public function getApiByApiKey($apiKey)
*/
public function getConfigValue($path, $storeId = null, $scope = null)
{
switch ($scope) {
case 'website':
$value = $this->_scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE, $storeId);
break;
default:
$value = $this->_scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORES, $storeId);
break;
if($scope) {
$value = $this->_scopeConfig->getValue($path, $scope, $storeId);
}
else {
$value = $this->_scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORES, $storeId);
}

return $value;
}
public function deleteConfig($path, $storeId = null, $scope = null)
Expand All @@ -275,13 +272,11 @@ public function deleteConfig($path, $storeId = null, $scope = null)

public function saveConfigValue($path, $value, $storeId = null, $scope = null)
{
switch ($scope) {
case 'website':
$this->_config->saveConfig($path, $value, \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE, $storeId);
break;
default:
$this->_config->saveConfig($path, $value, \Magento\Store\Model\ScopeInterface::SCOPE_STORES, $storeId);
break;
if($scope) {
$this->_config->saveConfig($path, $value, $scope, $storeId);
}
else {
$this->_config->saveConfig($path, $value, \Magento\Store\Model\ScopeInterface::SCOPE_STORES, $storeId);
}
}
public function getMCMinSyncing($storeId)
Expand Down
24 changes: 13 additions & 11 deletions Model/Config/Backend/MonkeyList.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,17 @@ public function __construct(
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
}

public function afterSave()
{
$generalData = $this->getData();
$data = $this->getData('groups');
$oldListId = $this->getOldValue();
$this->_registry->register('oldListId', $oldListId);
if(isset($data['general']['fields']['apikey'])) {
$this->_registry->register('apiKey', $data['general']['fields']['apikey']['value']);
}
return parent::afterSave();
}
// public function afterSave()
// {
// $generalData = $this->getData();
// $data = $this->getData('groups');
// $oldListId = $this->getOldValue();
// $this->_registry->register('oldListId', $oldListId);
// $apiKey = $this->getDataByPath(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_APIKEY);
//// if(isset($data['general']['fields']['apikey'])) {
// if($apiKey) {
// $this->_registry->register('apiKey', $apiKey);
// }
// return parent::afterSave();
// }
}
27 changes: 24 additions & 3 deletions Model/Config/Backend/MonkeyStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class MonkeyStore extends \Magento\Framework\App\Config\Value
private $_storeManager;

private $oldListId = null;
const MAX_LISTS = 200;


/**
Expand Down Expand Up @@ -72,18 +73,30 @@ public function __construct(

public function beforeSave()
{
$this->_helper->log(__METHOD__);
$data = $this->getData('groups');
$found = 0;
$newListId = null;
$this->_helper->log($this->getDataByPath(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_ACTIVE));
if (isset($data['ecommerce']['fields']['active']['value'])) {
$active = $data['ecommerce']['fields']['active']['value'];
} elseif ($data['ecommerce']['fields']['active']['inherit']) {
$active = $data['ecommerce']['fields']['active']['inherit'];
}
if ($active && $this->isValueChanged()) {
$mailchimpStore = $this->getOldValue();
if(isset($data['general']['fields']['monkeylist'])) {
// charge the $newListId
if (isset($data['general']['fields']['apikey']['value'])) {
$apiKey = $data['general']['fields']['apikey']['value'];
} else {
$apiKey = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_APIKEY, $this->getScopeId());
}
if(isset($data['general']['fields']['monkeylist']['value'])) {
$newListId = $data['general']['fields']['monkeylist']['value'];
} else {
$newListId = $this->getStore($apiKey,$this->getValue());
$this->_helper->log('list '.$newListId.'Score :'.$this->getScope().' Id '.$this->getScopeId());
$this->_helper->saveConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_LIST,$newListId,$this->getScopeId(),$this->getScope());
}
$this->oldListId = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_LIST, $this->getScopeId());

Expand All @@ -103,10 +116,18 @@ public function beforeSave()
$this->_helper->markAllBatchesAs($mailchimpStore, 'canceled');
$this->_helper->resetErrors($mailchimpStore);
}
if ($createWebhook&&isset($data['general']['fields']['apikey'])) {
$this->_helper->createWebHook($data['general']['fields']['apikey']['value'], $newListId);
if ($createWebhook) {
$this->_helper->createWebHook($apiKey, $newListId);
//$this->_helper->createWebHook($data['general']['fields']['apikey']['value'], $newListId);
}
}
return parent::beforeSave();
}
private function getStore($apiKey,$store)
{
$this->_helper->log(__METHOD__);
$api = $this->_helper->getApiByApiKey($apiKey);
$store = $api->ecommerce->stores->get($store);
return $store['list_id'];
}
}
23 changes: 17 additions & 6 deletions Model/Config/Source/Details.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,37 @@ class Details implements \Magento\Framework\Option\ArrayInterface
*/
private $_message;
private $_error = '';
private $storeId;

/**
* Details constructor.
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param \Magento\Framework\Message\ManagerInterface $message
* @param \Magento\Framework\App\RequestInterface $request
*/
public function __construct(
\Ebizmarts\MailChimp\Helper\Data $helper,
\Magento\Framework\Message\ManagerInterface $message
\Magento\Framework\Message\ManagerInterface $message,
\Magento\Framework\App\RequestInterface $request
) {
$this->_message = $message;
$this->_helper = $helper;
$this->storeId = (int) $request->getParam("store", 0);

if ($this->_helper->getApiKey()) {
$api = $this->_helper->getApi();
try {
$this->_options = $api->root->info();
$mailchimpStoreId = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE);
if ($mailchimpStoreId && $mailchimpStoreId!=-1 && $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_ECOMMERCE_ACTIVE)) {
$mailchimpStoreId = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE,$this->storeId);
if ($mailchimpStoreId && $mailchimpStoreId!=-1 && $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_ECOMMERCE_ACTIVE,$this->storeId)) {
$storeInfo = $api->ecommerce->stores->get($mailchimpStoreId);
$this->_options['is_syncing'] = $storeInfo['is_syncing'];
if(!$storeInfo['is_syncing']) {
$this->_options['is_syncing'] = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_IS_SYNC,$this->storeId);
$this->_helper->log($this->_options['is_syncing']);
}
else {
$this->_options['is_syncing'] = false;
}
$this->_options['store_exists'] = true;
$totalCustomers = $api->ecommerce->customers->getAll($mailchimpStoreId, 'total_items');
$this->_options['total_customers'] = $totalCustomers['total_items'];
Expand Down Expand Up @@ -88,13 +99,13 @@ public function toOptionArray()
['label' => ' Total Orders', 'value' => $this->_options['total_orders']],
['label' => ' Total Carts', 'value' => $this->_options['total_carts']]
]);
if ($this->_options['is_syncing']) {
if (!$this->_options['is_syncing']) {
$ret = array_merge($ret, [
['label'=> __('This account is currently syncing'), 'value'=>'']
]);
} else {
$ret = array_merge($ret, [
['label'=> __('Synced since'), 'value'=>'']
['label'=> __('Synced since'), 'value'=>$this->_options['is_syncing']]
]);
}
} else {
Expand Down
13 changes: 12 additions & 1 deletion Model/Config/Source/MonkeyList.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,21 @@ public function __construct(
\Magento\Framework\App\RequestInterface $request
) {
$storeId = (int) $request->getParam("store", 0);
if($request->getParam('website',0)) {
$scope = 'websites';
$storeId = $request->getParam('website',0);
}
elseif($request->getParam('store',0)) {
$scope = 'stores';
$storeId = $request->getParam('store',0);
}
else {
$scope = 'default';
}

if ($helper->getApiKey($storeId)) {
try {
$this->options = $helper->getApi()->lists->getLists($helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_LIST, $storeId));
$this->options = $helper->getApi()->lists->getLists($helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_LIST, $storeId,$scope));
} catch (\Exception $e) {
$helper->log($e->getMessage());
}
Expand Down
2 changes: 1 addition & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<field id="monkeylist" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Mailchimp List</label>
<source_model>Ebizmarts\MailChimp\Model\Config\Source\MonkeyList</source_model>
<backend_model>Ebizmarts\MailChimp\Model\Config\Backend\MonkeyList</backend_model>
<!--<backend_model>Ebizmarts\MailChimp\Model\Config\Backend\MonkeyList</backend_model>-->
<depends>
<field id="*/*/active">1</field>
</depends>
Expand Down

0 comments on commit b9f45ab

Please sign in to comment.