From d013f84087e67ebdc2d7cbf4c5d227e794e2a63c Mon Sep 17 00:00:00 2001 From: gonzalo Date: Thu, 4 May 2017 15:41:23 -0300 Subject: [PATCH] closes #8 --- Controller/Adminhtml/Errors/Getresponse.php | 97 +++++++++++++++++++ Model/Api/Result.php | 5 +- Setup/UpgradeSchema.php | 16 ++- Ui/Component/Errors/Grid/Column/Batch.php | 66 +++++++++++++ composer.json | 2 +- etc/module.xml | 2 +- .../ui_component/mailchimp_errors_grid.xml | 9 ++ 7 files changed, 189 insertions(+), 8 deletions(-) create mode 100644 Controller/Adminhtml/Errors/Getresponse.php create mode 100644 Ui/Component/Errors/Grid/Column/Batch.php diff --git a/Controller/Adminhtml/Errors/Getresponse.php b/Controller/Adminhtml/Errors/Getresponse.php new file mode 100644 index 00000000..4d2b4f7f --- /dev/null +++ b/Controller/Adminhtml/Errors/Getresponse.php @@ -0,0 +1,97 @@ + + * @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'); + } + +} \ No newline at end of file diff --git a/Model/Api/Result.php b/Model/Api/Result.php index 4f93005c..30ecaa3f 100644 --- a/Model/Api/Result.php +++ b/Model/Api/Result.php @@ -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') { @@ -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); } } diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index bce527e8..d5dccdd6 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -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(); - - - - } } \ No newline at end of file diff --git a/Ui/Component/Errors/Grid/Column/Batch.php b/Ui/Component/Errors/Grid/Column/Batch.php new file mode 100644 index 00000000..30cb263f --- /dev/null +++ b/Ui/Component/Errors/Grid/Column/Batch.php @@ -0,0 +1,66 @@ + + * @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; + } + + +} diff --git a/composer.json b/composer.json index 75b482e9..f945ded3 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ }, "description": "Connect MailChimp with Magento", "type": "magento2-module", - "version": "1.0.9", + "version": "1.0.10", "authors": [ { "name": "Ebizmarts Corp", diff --git a/etc/module.xml b/etc/module.xml index ed4df02a..6746a632 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -11,7 +11,7 @@ */ --> - + diff --git a/view/adminhtml/ui_component/mailchimp_errors_grid.xml b/view/adminhtml/ui_component/mailchimp_errors_grid.xml index 5e95bb5c..c0aa49cb 100644 --- a/view/adminhtml/ui_component/mailchimp_errors_grid.xml +++ b/view/adminhtml/ui_component/mailchimp_errors_grid.xml @@ -113,6 +113,15 @@ + + + + id + Download Response + + + +