Skip to content

Commit

Permalink
closes #1475 for magento 2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzaloebiz committed Jul 26, 2022
1 parent 6909c5d commit 8a53bea
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 2 deletions.
50 changes: 50 additions & 0 deletions Block/Adminhtml/System/CleanEcommerce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Created by PhpStorm.
* User: gonzalo
* Date: 3/12/18
* Time: 2:12 PM
*/
namespace Ebizmarts\MailChimp\Block\Adminhtml\System\Config;

class CleanEcommerce 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/cleanecommerce.phtml');
}

protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{
$originalData = $element->getOriginalData();
$this->addData(
[
'button_label' => __($originalData['button_label']),
'html_id' => $element->getHtmlId(),
]
);
return $this->_toHtml();
}
}
92 changes: 92 additions & 0 deletions Controller/Adminhtml/Ecommerce/CheckEcommerce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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 Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncEcommerce\CollectionFactory;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Exception\ValidatorException;
use Symfony\Component\Config\Definition\Exception\Exception;

class CheckEcommerce extends \Magento\Backend\App\Action
{
/**
* @var JsonFactory
*/
protected $resultJsonFactory;
/**
* @var \Ebizmarts\MailChimp\Helper\Data
*/
protected $helper;
/**
* @var CollectionFactory
*/
protected $collectionFactory;
/**
* @var \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce
*/
protected $chimpSyncEcommerce;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param JsonFactory $resultJsonFactory
* @param CollectionFactory $collectionFactory
* @param \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $chimpSyncEcommerce
* @param \Ebizmarts\MailChimp\Helper\Data $helper
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
JsonFactory $resultJsonFactory,
\Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncEcommerce\CollectionFactory $collectionFactory,
\Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $chimpSyncEcommerce,
\Ebizmarts\MailChimp\Helper\Data $helper
) {

parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
$this->helper = $helper;
$this->collectionFactory = $collectionFactory;
$this->chimpSyncEcommerce = $chimpSyncEcommerce;
}

public function execute()
{
$valid = 1;
$message = '';

$resultJson = $this->resultJsonFactory->create();
try {
$collection = $this->collectionFactory->create();
$collection->getSelect()->joinLeft(
['core_config' => 'core_config_data'],
'value = mailchimp_store_id'
);
$collection->getSelect()->reset(\Magento\Framework\DB\Select::COLUMNS)->columns(['mailchimp_store_id']);
$collection->getSelect()->where('value is null');
$collection->getSelect()->group('mailchimp_store_id');
$valid = $collection->count();

} catch (ValidatorException $e) {
$valid = -1;
$message = $e->getMessage();
}
return $resultJson->setData([
'valid' => (int)$valid,
'message' => $message,
]);
}
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Ebizmarts_MailChimp::config_mailchimp');
}
}
99 changes: 99 additions & 0 deletions Controller/Adminhtml/Ecommerce/CleanEcommerce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?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 Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncEcommerce\CollectionFactory;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Exception\ValidatorException;
use Symfony\Component\Config\Definition\Exception\Exception;

class CleanEcommerce extends \Magento\Backend\App\Action
{
/**
* @var JsonFactory
*/
protected $resultJsonFactory;
/**
* @var \Ebizmarts\MailChimp\Helper\Data
*/
protected $helper;
/**
* @var CollectionFactory
*/
protected $collectionFactory;
/**
* @var \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce
*/
protected $chimpSyncEcommerce;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param JsonFactory $resultJsonFactory
* @param CollectionFactory $collectionFactory
* @param \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $chimpSyncEcommerce
* @param \Ebizmarts\MailChimp\Helper\Data $helper
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
JsonFactory $resultJsonFactory,
\Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncEcommerce\CollectionFactory $collectionFactory,
\Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $chimpSyncEcommerce,
\Ebizmarts\MailChimp\Helper\Data $helper
) {

parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
$this->helper = $helper;
$this->collectionFactory = $collectionFactory;
$this->chimpSyncEcommerce = $chimpSyncEcommerce;
}

public function execute()
{
$valid = 1;
$message = '';

$resultJson = $this->resultJsonFactory->create();
try {
$collection = $this->collectionFactory->create();
$collection->getSelect()->joinLeft(
['core_config' => 'core_config_data'],
'value = mailchimp_store_id'
);
$collection->getSelect()->reset(\Magento\Framework\DB\Select::COLUMNS)->columns(['mailchimp_store_id']);
$collection->getSelect()->where('value is null');
$collection->getSelect()->group('mailchimp_store_id');
foreach($collection as $item) {
$mailchimpStoreId = $item->getMailchimpStoreId();
$connection = $this->chimpSyncEcommerce->getResource()->getConnection();
$tableName = $this->chimpSyncEcommerce->getResource()->getMainTable();
$connection->delete(
$tableName,
"mailchimp_store_id = '$mailchimpStoreId'"
);
}
} catch (ValidatorException $e) {
$valid = 0;
$message = $e->getMessage();
}
return $resultJson->setData([
'valid' => (int)$valid,
'message' => $message,
]);
}
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Ebizmarts_MailChimp::config_mailchimp');
}
}
7 changes: 7 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@
<field id="*/*/active">1</field>
</depends>
</field>
<field id="clean_ecommerce" translate="button_label" type="button" sortOrder="87" showInDefault="1" showInWebsite="1" showInStore="1">
<button_label>Remove Unused Registers</button_label>
<frontend_model>Ebizmarts\MailChimp\Block\Adminhtml\System\Config\CleanEcommerce</frontend_model>
<depends>
<field id="*/*/active">1</field>
</depends>
</field>
<field id="interest" translate="label" type="multiselect" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Available Groups</label>
<source_model>Ebizmarts\MailChimp\Model\Config\Source\Interest</source_model>
Expand Down
6 changes: 6 additions & 0 deletions view/adminhtml/templates/system/config/cleanecommerce.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="actions actions-clean-ecommerce">
<div id="validation_result" class="message-validation hidden"></div>
<button class="action-clean-ecommerce" type="button" id="<?php echo $block->getHtmlId() ?>">
<span><?php echo $block->escapeHtml($block->getButtonLabel()) ?></span>
</button>
</div>
4 changes: 3 additions & 1 deletion view/adminhtml/templates/system/config/fieldset/hint.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"getInterestUrl": "<?php echo $this->getUrl('mailchimp/ecommerce/getInterest');?>",
"resyncSubscribersUrl": "<?php echo $this->getUrl('mailchimp/ecommerce/resyncSubscribers');?>",
"resyncProductsUrl": "<?php echo $this->getUrl('mailchimp/ecommerce/resyncProducts');?>",
"cleanEcommerceUrl": "<?php echo $this->getUrl('mailchimp/ecommerce/cleanEcommerce');?>",
"checkEcommerceUrl": "<?php echo $this->getUrl('mailchimp/ecommerce/checkEcommerce');?>",
"scope": "<?php echo $this->getScope();?>",
"scopeId": "<?php echo $this->getScopeId(); ?>"
}}'>
Expand All @@ -34,4 +36,4 @@
<?php endif; ?>
</div>
</h4>
</div>
</div>
44 changes: 43 additions & 1 deletion view/adminhtml/web/js/configapikey.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ define(
"getInterestUrl": "",
"resyncSubscribersUrl": "",
"resyncProductsUrl": "",
"cleanEcommerceUrl": "",
"checkEcommerceUrl": "",
"scope": "",
"scopeId": ""
},
Expand Down Expand Up @@ -55,6 +57,46 @@ define(
var mailchimpStoreId = $('#mailchimp_general_monkeystore').find(':selected').val();
self._resyncProducts(mailchimpStoreId);
});
self._checkCleanButton();
$('#mailchimp_general_clean_ecommerce').click(function () {
self._cleanEcommerce();
});


},
_checkCleanButton: function () {
var checkEcommerceUrl = this.options.checkEcommerceUrl;
$.ajax({
url: checkEcommerceUrl,
data: {'form_key': window.FORM_KEY},
type: 'GET',
dataType: 'json',
showLoader: true
}).done(function (data) {
if (data.valid == -1) {
alert({content: 'Error: can\'t check the unused registers'});
} else if (data.valid == 0) {
$('#row_mailchimp_general_clean_ecommerce').hide();
$('#mailchimp_general_clean_ecommerce').hide();
}
});

},
_cleanEcommerce: function () {
var cleanEcommerceUrl = this.options.cleanEcommerceUrl;
$.ajax({
url: cleanEcommerceUrl,
data: {'form_key': window.FORM_KEY},
type: 'GET',
dataType: 'json',
showLoader: true
}).done(function (data) {
if (data.valid == 0) {
alert({content: 'Error: can\'t remove the unused registers'});
} else if (data.valid == 1) {
alert({content: 'All unused registers are deleted'});
}
});

},
_resyncSubscribers: function (mailchimpStoreId) {
Expand Down Expand Up @@ -219,4 +261,4 @@ define(
});
return $.mage.configmonkeyapikey;
}
);
);

0 comments on commit 8a53bea

Please sign in to comment.