Skip to content

Commit

Permalink
closes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzaloebiz committed May 4, 2017
1 parent 251adfe commit d013f84
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 8 deletions.
97 changes: 97 additions & 0 deletions Controller/Adminhtml/Errors/Getresponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?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\Errors;

use Magento\Framework\Controller\ResultFactory;

class Getresponse extends \Magento\Backend\App\Action
{
/**
* @var ResultFactory
*/
protected $_resultFactory;
/**
* @var \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory
*/
protected $_errorsFactory;
/**
* @var \Ebizmarts\MailChimp\Model\Api\Result
*/
protected $_result;
/**
* @var \Ebizmarts\MailChimp\Helper\Data
*/
protected $_helper;

/**
* Getresponse constructor.
* @param \Magento\Backend\App\Action\Context $context
* @param ResultFactory $resultFactory
* @param \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory $errorsFactory
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param \Ebizmarts\MailChimp\Model\Api\Result $result
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Controller\ResultFactory $resultFactory,
\Ebizmarts\MailChimp\Model\MailChimpErrorsFactory $errorsFactory,
\Ebizmarts\MailChimp\Helper\Data $helper,
\Ebizmarts\MailChimp\Model\Api\Result $result
) {
parent::__construct($context);
$this->_resultFactory = $resultFactory;
$this->_errorsFactory = $errorsFactory;
$this->_result = $result;
$this->_helper = $helper;
}

public function execute()
{
$errorId = $this->getRequest()->getParam('id');
$errors = $this->_errorsFactory->create();
$errors->getResource()->load($errors, $errorId);
$batchId = $errors->getBatchId();
$files = $this->_result->getBatchResponse($batchId,$errors->getStoreId());
$fileContent = [];
foreach ($files as $file)
{
$items = json_decode(file_get_contents($file));
foreach ($items as $item)
{
$content = array(
'status_code'=>$item->status_code,
'operation_id'=>$item->operation_id,
'response'=>json_decode($item->response)
);
$fileContent[] = $content;
}
unlink($file);
}
$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);
$baseDir = $this->_helper->getBaseDir();
if (is_dir($baseDir . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . \Ebizmarts\MailChimp\Model\Api\Result::MAILCHIMP_TEMP_DIR . DIRECTORY_SEPARATOR . $batchId)) {
rmdir($baseDir . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . \Ebizmarts\MailChimp\Model\Api\Result::MAILCHIMP_TEMP_DIR . DIRECTORY_SEPARATOR . $batchId);
}
return $resultJson;
}
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Ebizmarts_MailChimp::errors_admin_view');
}

}
5 changes: 3 additions & 2 deletions Model/Api/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ public function processResponses($storeId, $isMailChimpStoreId = false, $mailchi
}
}
}
protected function getBatchResponse($batchId, $storeId = 0)
public function getBatchResponse($batchId, $storeId = null)
{
$files = array();
try {
$baseDir = $this->_helper->getBaseDir();
$api = $this->_helper->getApi();
$api = $this->_helper->getApi($storeId);
// check the status of the job
$response = $api->batchOperation->status($batchId);
if (isset($response['status']) && $response['status'] == 'finished') {
Expand Down Expand Up @@ -165,6 +165,7 @@ protected function processEachResponseFile($files, $batchId, $mailchimpStoreId ,
$mailchimpErrors->setMailchimpStoreId($mailchimpStoreId);
$mailchimpErrors->setOriginalId($id);
$mailchimpErrors->setBatchId($batchId);
$mailchimpStoreId->setStoreId($storeId);
$mailchimpErrors->getResource()->save($mailchimpErrors);
}
}
Expand Down
16 changes: 12 additions & 4 deletions Setup/UpgradeSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,18 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con
);

}
if (version_compare($context->getVersion(), '1.0.10') < 0) {
$installer->getConnection()->addColumn(
$installer->getTable('mailchimp_errors'),
'store_id',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
'length' => 11,
'default' => null,
'comment' => 'Magento Store Id'
]
);
}
$installer->endSetup();




}
}
66 changes: 66 additions & 0 deletions Ui/Component/Errors/Grid/Column/Batch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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:10 PM
* @file: Batch.php
*/

namespace Ebizmarts\MailChimp\Ui\Component\Errors\Grid\Column;

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\UrlInterface;

class Batch extends Column
{
/**
* @var UrlInterface
*/
protected $urlBuilder;

/**
* Batch constructor.
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param UrlInterface $urlBuilder
* @param array $components
* @param array $data
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
UrlInterface $urlBuilder,
array $components = [],
array $data = []
) {
$this->urlBuilder = $urlBuilder;
parent::__construct($context, $uiComponentFactory, $components, $data);
}
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {

foreach ($dataSource['data']['items'] as &$item) {
$item[$this->getData('name')]['batch_id'] = [
'href' => $this->urlBuilder->getUrl(
'mailchimp/errors/getresponse',
['id' => $item['id']]
),
'label' => $item['batch_id'],
'hidden' => false,
];
}
}

return $dataSource;
}


}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"description": "Connect MailChimp with Magento",
"type": "magento2-module",
"version": "1.0.9",
"version": "1.0.10",
"authors": [
{
"name": "Ebizmarts Corp",
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Ebizmarts_MailChimp" setup_version="1.0.9">
<module name="Ebizmarts_MailChimp" setup_version="1.0.10">
<sequence>
<module name="Magento_Newsletter"/>
</sequence>
Expand Down
9 changes: 9 additions & 0 deletions view/adminhtml/ui_component/mailchimp_errors_grid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@
</item>
</argument>
</column>
<actionsColumn name="actions" class="\Ebizmarts\MailChimp\Ui\Component\Errors\Grid\Column\Batch">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="indexField" xsi:type="string">id</item>
<item name="label" xsi:type="string" translate="true">Download Response</item>
</item>
</argument>
</actionsColumn>

</columns>
</listing>

0 comments on commit d013f84

Please sign in to comment.