Skip to content

Commit

Permalink
Closes mailchimp#1465 for 2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
agustinbv committed Nov 7, 2022
1 parent cfc101f commit b06225f
Show file tree
Hide file tree
Showing 11 changed files with 569 additions and 1 deletion.
121 changes: 121 additions & 0 deletions Controller/Adminhtml/Batch/GetResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
/**
* mc-magento2 Magento Component
*
* @category Ebizmarts
* @package mc-magento2
* @author Ebizmarts Team <[email protected]>
* @copyright Ebizmarts (http://ebizmarts.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @date: 5/3/17 3:28 PM
* @file: Getresponse.php
*/

namespace Ebizmarts\MailChimp\Controller\Adminhtml\Batch;

use Magento\Framework\Controller\ResultFactory;

class Getresponse extends \Magento\Backend\App\Action
{
const MAX_RETRIES = 5;
/**
* @var ResultFactory
*/
protected $_resultFactory;
/**
* @var \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory
*/
protected $_batchFactory;
/**
* @var \Ebizmarts\MailChimp\Model\Api\Result
*/
protected $_result;
/**
* @var \Ebizmarts\MailChimp\Helper\Data
*/
protected $_helper;
/**
* @var \Magento\Framework\Filesystem\Driver\File
*/
protected $_driver;

/**
* Getresponse constructor.
* @param \Magento\Backend\App\Action\Context $context
* @param \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory $errorsFactory
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param \Ebizmarts\MailChimp\Model\Api\Result $result
* @param \Magento\Framework\Filesystem\Driver\File $driver
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Ebizmarts\MailChimp\Model\MailChimpSyncBatchesFactory $_batchFactory,
\Ebizmarts\MailChimp\Helper\Data $helper,
\Ebizmarts\MailChimp\Model\Api\Result $result,
\Magento\Framework\Filesystem\Driver\File $driver
) {
parent::__construct($context);
$this->_resultFactory = $context->getResultFactory();
$this->_batchFactory = $_batchFactory;
$this->_result = $result;
$this->_helper = $helper;
$this->_driver = $driver;
}

public function execute()
{
$batchId = $this->getRequest()->getParam('id');
$batches = $this->_batchFactory->create();
$batches->getResource()->load($batches, $batchId);
$batchId = $batches->getBatchId();
$fileContent = [];
$counter = 0;

do {
$counter++;
$files = $this->_result->getBatchResponse($batchId, $batches->getStoreId());
if ($files===false) {
$fileContent = "Response was deleted from MailChimp servers";
break;
}
foreach ($files as &$file) {

$items = json_decode($this->_driver->fileGetContents($file));

foreach ($items as &$item) {

$content = [
'status_code' => $item->status_code,
'operation_id' => $item->operation_id,
'response' => json_decode($item->response)
];
$fileContent[] = $content;

}

$this->_driver->deleteFile($file);
}

$baseDir = $this->_helper->getBaseDir();
if ($this->_driver->isDirectory($baseDir . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR .
\Ebizmarts\MailChimp\Model\Api\Result::MAILCHIMP_TEMP_DIR . DIRECTORY_SEPARATOR . $batchId)) {
$this->_driver->deleteDirectory(
$baseDir . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR .
\Ebizmarts\MailChimp\Model\Api\Result::MAILCHIMP_TEMP_DIR . DIRECTORY_SEPARATOR . $batchId
);
}

} while (!count($fileContent) && $counter<self::MAX_RETRIES);

$resultJson =$this->_resultFactory->create(ResultFactory::TYPE_JSON);
$resultJson->setHeader('Content-disposition', 'attachment; filename='.$batchId.'.json');
$resultJson->setHeader('Content-type', 'application/json');
$data = json_encode($fileContent, JSON_PRETTY_PRINT);
$resultJson->setJsonData($data);
return $resultJson;
}
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Ebizmarts_MailChimp::batch_grid');
}
}
47 changes: 47 additions & 0 deletions Controller/Adminhtml/Batch/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* mc-magento2 Magento Component
*
* @category Ebizmarts
* @package mc-magento2
* @author Ebizmarts Team <[email protected]>
* @copyright Ebizmarts (http://ebizmarts.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @date: 10/28/16 4:58 PM
* @file: Index.php
*/
namespace Ebizmarts\MailChimp\Controller\Adminhtml\Batch;

class Index extends \Magento\Backend\App\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;

/**
* Index constructor.
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {

$this->resultPageFactory = $resultPageFactory;
return parent::__construct($context);
}
public function execute()
{
$page = $this->resultPageFactory->create();
$page->getConfig()->getTitle()->prepend(__('Mailchimp Batches'));
return $page;
}
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Ebizmarts_MailChimp::batch_grid');
}
}


15 changes: 15 additions & 0 deletions Cron/Ecommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,27 @@ protected function _processStore($storeId, $mailchimpStoreId, $listId)
if (!isset($batchResponse['id'])) {
$this->_helper->log('error in the call to batch');
} else {
$batchCounters = $this->_helper->getCounters();
$syncBatches = $this->_mailChimpSyncBatchesFactory->create();
$syncBatches->setStoreId($storeId);
$syncBatches->setBatchId($batchResponse['id']);
$syncBatches->setStatus(\Ebizmarts\MailChimp\Helper\Data::BATCH_PENDING);
$syncBatches->setMailchimpStoreId($mailchimpStoreId);
$syncBatches->setModifiedDate($this->_helper->getGmtDate());
/**
*@var Data is \Ebizmarts\MailChimp\Helper\Data
*/
$syncBatches->setSubscribersNewCount($batchCounters[\Ebizmarts\MailChimp\Helper\Data::SUB_NEW]);
$syncBatches->setProductsNewCount($batchCounters[\Ebizmarts\MailChimp\Helper\Data::PRO_NEW]);
$syncBatches->setCustomersNewCount($batchCounters[\Ebizmarts\MailChimp\Helper\Data::CUS_NEW]);
$syncBatches->setCartsNewCount($batchCounters[\Ebizmarts\MailChimp\Helper\Data::QUO_NEW]);
$syncBatches->setOrdersNewCount($batchCounters[\Ebizmarts\MailChimp\Helper\Data::ORD_NEW]);
$syncBatches->setSubscribersModifiedCount($batchCounters[\Ebizmarts\MailChimp\Helper\Data::SUB_MOD]);
$syncBatches->setProductsModifiedCount($batchCounters[\Ebizmarts\MailChimp\Helper\Data::PRO_MOD]);
$syncBatches->setCustomersModifiedCount($batchCounters[\Ebizmarts\MailChimp\Helper\Data::CUS_MOD]);
$syncBatches->setCartsModifiedCount($batchCounters[\Ebizmarts\MailChimp\Helper\Data::QUO_MOD]);
$syncBatches->setOrdersModifiedCount($batchCounters[\Ebizmarts\MailChimp\Helper\Data::ORD_MOD]);

$syncBatches->getResource()->save($syncBatches);
$batchId = $batchResponse['id'];
$this->_showResume($batchId, $storeId);
Expand Down
13 changes: 12 additions & 1 deletion Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,18 @@ public function modifyCounter($index, $increment = 1)
}
public function resetCounters()
{
$this->counters = [];
$this->counters = [
self::SUB_NEW => 0,
self::SUB_MOD => 0,
self::ORD_NEW => 0,
self::ORD_MOD => 0,
self::PRO_NEW => 0,
self::PRO_MOD => 0,
self::CUS_NEW => 0,
self::CUS_MOD => 0,
self::QUO_NEW => 0,
self::QUO_MOD => 0
];
}
public function getCounters()
{
Expand Down
70 changes: 70 additions & 0 deletions Setup/InstallSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,76 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con
10,
[],
'Status'
)
->addColumn(
'carts_new_count',
\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
null,
[],
'Number of new carts'
)
->addColumn(
'customers_new_count',
\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
null,
[],
'Number of new customers'
)
->addColumn(
'orders_new_count',
\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
null,
[],
'Number of new orders'
)
->addColumn(
'products_new_count',
\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
null,
[],
'Number of new products'
)
->addColumn(
'subscribers_new_count',
\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
null,
[],
'Number of new carts'
)
->addColumn(
'carts_modified_count',
\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
null,
[],
'Number of modified carts'
)
->addColumn(
'customers_modified_count',
\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
null,
[],
'Number of modified customers'
)
->addColumn(
'orders_modified_count',
\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
null,
[],
'Number of modified orders'
)
->addColumn(
'products_modified_count',
\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
null,
[],
'Number of modified products'
)
->addColumn(
'subscribers_modified_count',
\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
null,
[],
'Number of modified carts'
);

$connection->createTable($table);
Expand Down
Loading

0 comments on commit b06225f

Please sign in to comment.