Skip to content

Commit

Permalink
add Reset button
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzaloebiz committed Feb 21, 2017
1 parent c20d3f7 commit 90894b1
Show file tree
Hide file tree
Showing 13 changed files with 641 additions and 96 deletions.
90 changes: 90 additions & 0 deletions Block/Adminhtml/System/Config/ResetErrors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* mc-magento2 Magento Component
*
* @category Ebizmarts
* @package mc-magento2
* @author Ebizmarts Team <info@ebizmarts.com>
* @copyright Ebizmarts (http://ebizmarts.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @date: 2/20/17 3:25 PM
* @file: ResetErrors.php
*/

namespace Ebizmarts\MailChimp\Block\Adminhtml\System\Config;

class ResetErrors extends \Magento\Config\Block\System\Config\Form\Field
{
/**
* @var \Ebizmarts\MailChimp\Helper\Data
*/
private $_helper;

/**
* ResetErrors constructor.
* @param \Magento\Backend\Block\Template\Context $context
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Ebizmarts\MailChimp\Helper\Data $helper,
array $data=[])
{
$this->_helper = $helper;
parent::__construct($context, $data);
}

protected function _construct()
{
parent::_construct();
$this->setTemplate('system/config/reseterrors.phtml');
}

// protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
// {
// return $this->_toHtml();
// }
protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{
$originalData = $element->getOriginalData();
$this->addData(
[
'button_label' => __($originalData['button_label']),
'button_url' => $this->getAjaxCheckUrl(),
'html_id' => $element->getHtmlId(),
]
);
return $this->_toHtml();
}

public function getButtonHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{


$originalData = $element->getOriginalData();
$label = $originalData['button_label'];
$this->addData([
'button_label' => __($label),
'button_url' => $this->getAjaxCheckUrl(),
'html_id' => $element->getHtmlId(),
]);
return $this->_toHtml();


// $button = $this->getLayout()->createBlock('adminhtml/widget_button')
// ->setData(
// array(
// 'id' => 'reseterrors_button',
// 'label' => $this->_helper->__('Reset Local Errors'),
// 'onclick' => 'javascript:reseterrors(); return false;'
// )
// );
//
// return $button->toHtml();
}
public function getAjaxCheckUrl()
{
return $this->_urlBuilder->getUrl('mailchimp/ecommerce/ResetLocalErrors');
}
}
46 changes: 46 additions & 0 deletions Controller/Adminhtml/Ecommerce/ResetLocalErrors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* mc-magento2 Magento Component
*
* @category Ebizmarts
* @package mc-magento2
* @author Ebizmarts Team <info@ebizmarts.com>
* @copyright Ebizmarts (http://ebizmarts.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @date: 2/21/17 5:07 PM
* @file: ResetLocalErrors.php
*/

namespace Ebizmarts\MailChimp\Controller\Adminhtml\Ecommerce;

use Magento\Framework\Controller\Result\JsonFactory;

class ResetLocalErrors extends \Magento\Backend\App\Action
{
protected $resultJsonFactory;
protected $helper;

public function __construct(
\Magento\Backend\App\Action\Context $context,
JsonFactory $resultJsonFactory,
\Ebizmarts\MailChimp\Helper\Data $helper
)
{
parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
$this->helper = $helper;
}

public function execute()
{
$resultJson = $this->resultJsonFactory->create();
return $resultJson->setData([
'valid' => (int)1,
'message' => 'OK',
]);
}
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Magento_EncryptionKey::crypt_key');
}
}
4 changes: 2 additions & 2 deletions Cron/Ecommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ protected function _processStore($storeId)
$orders = $this->_apiOrder->sendOrders($storeId);
$results = array_merge($results,$orders);
}
$this->_helper->log(var_export($results,true));
if (!empty($results)) {
try {
$batchArray['operations'] = $results;
Expand All @@ -105,7 +104,8 @@ protected function _processStore($storeId)
} else {
$api = $this->_helper->getApi();
$batchResponse =$api->batchOperation->add($batchArray);
// $this->_helper->log(var_export($batchResponse,true));
$this->_helper->log(var_export($results,true),null,$batchResponse['id']);
$this->_helper->log(var_export($batchResponse,true));
$this->_mailChimpSyncBatches->setStoreId($storeId);
$this->_mailChimpSyncBatches->setBatchId($batchResponse['id']);
$this->_mailChimpSyncBatches->setStatus($batchResponse['status']);
Expand Down
23 changes: 15 additions & 8 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
const XML_PATH_ECOMMERCE_ACTIVE = 'mailchimp/ecommerce/active';
const XML_PATH_SYNC_DATE = 'mailchimp/general/mcminsyncdateflag';
const XML_ECOMMERCE_OPTIN = 'mailchimp/ecommerce/customer_optin';
const XML_ECOMMERCE_FIRSTDATE = 'mailchimp/ecommerce/firstdate';

const ORDER_STATE_OK = 'complete';

Expand Down Expand Up @@ -219,10 +220,10 @@ public function getLogger()
* @param $message
* @param null $store
*/
public function log($message, $store = null)
public function log($message, $store = null, $file = null)
{
if ($this->getConfigValue(self::XML_PATH_LOG, $store)) {
$this->_mlogger->mailchimpLog($message);
$this->_mlogger->mailchimpLog($message, $file);
}
}

Expand All @@ -249,18 +250,24 @@ public function deleteStore()

}
}
public function createStore($listId=null)
public function getMCStoreName($storeId)
{
return $this->_storeManager->getStore($storeId)->getFrontendName();
}
public function createStore($listId=null, $storeId)
{
if ($listId) {
//generate store id
$date = date('Y-m-d-His');
$baseUrl = $this->_storeManager->getStore()->getBaseUrl();
$storeId = parse_url($baseUrl, PHP_URL_HOST) . '_' . $date;
$currencyCode = $this->_storeManager->getStore()->getDefaultCurrencyCode();
$baseUrl = $this->_storeManager->getStore($storeId)->getName();
$mailchimpStoreId = md5(parse_url($baseUrl, PHP_URL_HOST) . '_' . $date);
$currencyCode = $this->_storeManager->getStore($storeId)->getDefaultCurrencyCode();
$name = $this->getMCStoreName($storeId);

//create store in mailchimp
try {
$this->getApi()->ecommerce->stores->add($storeId,$listId,$storeId,$currencyCode,'Magento');
return $storeId;
$this->getApi()->ecommerce->stores->add($mailchimpStoreId,$listId,$name,$currencyCode,'Magento');
return $mailchimpStoreId;

} catch (Exception $e) {
return null;
Expand Down
Loading

0 comments on commit 90894b1

Please sign in to comment.