Skip to content

Commit

Permalink
Merge branch '2.4-develop' into 2.4
Browse files Browse the repository at this point in the history
# Conflicts:
#	etc/adminhtml/system.xml
  • Loading branch information
JackerNgo committed Feb 26, 2021
2 parents 872f0bb + e1792ee commit 9a8b1d5
Show file tree
Hide file tree
Showing 13 changed files with 739 additions and 165 deletions.
90 changes: 90 additions & 0 deletions Block/Adminhtml/System/Config/SyncSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Smtp
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/

namespace Mageplaza\Smtp\Block\Adminhtml\System\Config;

/**
* Class SyncSubscriber
* @package Mageplaza\Smtp\Block\Adminhtml\System\Config
*/
class SyncSubscriber extends Button
{
/**
* @var string
*/
protected $_template = 'system/config/sync-template.phtml';

/**
* @return string
*/
public function getEstimateUrl()
{
return $this->getUrl('adminhtml/smtp_sync/estimatesubscriber', ['_current' => true]);
}

/**
* @return mixed
*/
public function getWebsiteId()
{
return $this->getRequest()->getParam('website');
}

/**
* @return mixed
*/
public function getStoreId()
{
return $this->getRequest()->getParam('store');
}

/**
* @return mixed
*/
public function getSyncSuccessMessage()
{
return __('Subscriber synchronization has been completed.');
}

/**
* @return string
*/
public function getElementId()
{
return 'mp-sync-subscriber';
}

/**
* @return string
*/
public function getComponent()
{
return 'Mageplaza_Smtp/js/sync/subscriber';
}

/**
* @return bool
*/
public function isRenderCss()
{
return true;
}
}
14 changes: 12 additions & 2 deletions Controller/Adminhtml/Smtp/Sync/AbstractEstimate.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ abstract class AbstractEstimate extends Action
*/
protected $emailMarketing;

/**
* @var string
*/
protected $websiteIdField = 'website_id';

/**
* @var string
*/
protected $storeIdField = 'store_id';

/**
* AbstractEstimate constructor.
*
Expand Down Expand Up @@ -79,11 +89,11 @@ public function execute()
$storeId = $this->getRequest()->getParam('storeId');
$websiteId = $this->getRequest()->getParam('websiteId');
if ($storeId) {
$collection->addFieldToFilter('store_id', $storeId);
$collection->addFieldToFilter($this->storeIdField, $storeId);
}

if ($websiteId) {
$collection->addFieldToFilter('website_id', $websiteId);
$collection->addFieldToFilter($this->websiteIdField, $websiteId);
}

$ids = $collection->getAllIds();
Expand Down
14 changes: 13 additions & 1 deletion Controller/Adminhtml/Smtp/Sync/EstimateOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,19 @@ public function __construct(
*/
public function prepareCollection()
{
return $this->orderCollectionFactory->create();
$orderCollection = $this->orderCollectionFactory->create();
$storeTable = $orderCollection->getTable('store');
$this->websiteIdField = 'store_table.website_id';
$this->storeIdField = 'main_table.store_id';
$orderCollection->getSelect()->join(
['store_table' => $storeTable],
'main_table.store_id = store_table.store_id',
[
$this->websiteIdField
]
);

return $orderCollection;
}

/**
Expand Down
82 changes: 82 additions & 0 deletions Controller/Adminhtml/Smtp/Sync/EstimateSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Smtp
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/

namespace Mageplaza\Smtp\Controller\Adminhtml\Smtp\Sync;

use Magento\Backend\App\Action\Context;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Framework\Phrase;
use Magento\Newsletter\Model\ResourceModel\Subscriber\Collection;
use Mageplaza\Smtp\Helper\EmailMarketing;
use Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory as SubscriberCollectionFactory;
use Mageplaza\Smtp\Model\Config\Source\Newsletter;
use Magento\Newsletter\Model\Subscriber;

/**
* Class EstimateSubscriber
* @package Mageplaza\Smtp\Controller\Adminhtml\Smtp\Sync
*/
class EstimateSubscriber extends AbstractEstimate
{
/**
* @var SubscriberCollectionFactory
*/
protected $subscriberCollectionFactory;

/**
* EstimateSubscriber constructor.
*
* @param Context $context
* @param SubscriberCollectionFactory $subscriberCollectionFactory
* @param EmailMarketing $emailMarketing
*/
public function __construct(
Context $context,
SubscriberCollectionFactory $subscriberCollectionFactory,
EmailMarketing $emailMarketing
) {
$this->subscriberCollectionFactory = $subscriberCollectionFactory;

parent::__construct($context, $emailMarketing);
}

/**
* @return AbstractCollection|Collection
*/
public function prepareCollection()
{
$collection = $this->subscriberCollectionFactory->create();

if ($this->emailMarketing->getSubscriberConfig() === Newsletter::SUBSCRIBED) {
$collection->addFieldToFilter('subscriber_status', ['eq' => Subscriber::STATUS_SUBSCRIBED]);
}

return $collection;
}

/**
* @return Phrase
*/
public function getZeroMessage()
{
return __('No subscriber to synchronize.');
}
}
157 changes: 157 additions & 0 deletions Controller/Adminhtml/Smtp/Sync/Subscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Smtp
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/

namespace Mageplaza\Smtp\Controller\Adminhtml\Smtp\Sync;

use Exception;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory as SubscriberCollectionFactory;
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Mageplaza\Smtp\Helper\EmailMarketing;
use Mageplaza\Smtp\Model\Config\Source\Newsletter;
use Magento\Newsletter\Model\Subscriber as ModelSubscriber;

/**
* Class Subscriber
* @package Mageplaza\Smtp\Controller\Adminhtml\Smtp\Sync
*/
class Subscriber extends Action
{
const SUB = 'subscribed';
const UNSUB = 'unsub';
const NOTSUB = 'notsub';

/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Mageplaza_Smtp::email_marketing';

/**
* @var EmailMarketing
*/
protected $helperEmailMarketing;

/**
* @var SubscriberCollectionFactory
*/
protected $subscriberCollectionFactory;

/**
* @var CustomerCollectionFactory
*/
protected $customerCollectionFactory;

/**
* Subscriber constructor.
*
* @param Context $context
* @param EmailMarketing $helperEmailMarketing
* @param SubscriberCollectionFactory $subscriberCollectionFactory
* @param CustomerCollectionFactory $customerCollectionFactory
*/
public function __construct(
Context $context,
EmailMarketing $helperEmailMarketing,
SubscriberCollectionFactory $subscriberCollectionFactory,
CustomerCollectionFactory $customerCollectionFactory
) {
$this->helperEmailMarketing = $helperEmailMarketing;
$this->subscriberCollectionFactory = $subscriberCollectionFactory;
$this->customerCollectionFactory = $customerCollectionFactory;
parent::__construct($context);
}

/**
* @return ResponseInterface|ResultInterface
*/
public function execute()
{
$result = [];

try {
$collection = $this->subscriberCollectionFactory->create();
$ids = $this->getRequest()->getParam('ids');

if ($this->helperEmailMarketing->getSubscriberConfig() === Newsletter::SUBSCRIBED) {
$collection->addFieldToFilter('subscriber_status', ['eq' => ModelSubscriber::STATUS_SUBSCRIBED]);
}

$data = [];

$subscribers = $collection->addFieldToFilter('subscriber_id', ['in' => $ids]);

foreach ($subscribers as $subscriber) {
switch ($subscriber->getSubscriberStatus()) {
case ModelSubscriber::STATUS_SUBSCRIBED:
$status = self::SUB;
break;
case ModelSubscriber::STATUS_UNSUBSCRIBED:
$status = self::UNSUB;
break;
default:
$status = self::NOTSUB;
break;
}

if ($subscriber->getCustomerId()) {
$customerCollection = $this->customerCollectionFactory->create();
$customerCollection->addFieldToFilter('entity_id', ['eq' => $subscriber->getCustomerId()]);

foreach ($customerCollection as $customer) {
$customerData = $this->helperEmailMarketing->getCustomerData(
$customer,
false,
true
);
$customerData['status'] = $status;
$customerData['tags'] = 'newsletter';
$customerData['isSubscriber'] = true;
$data[] = $customerData;
}

} else {
$data[] = [
'email' => $subscriber->getSubscriberEmail(),
'status' => $status,
'source' => 'Magento',
'tags' => 'newsletter',
'isSubscriber' => true
];
}
}

$result['status'] = true;
$result['total'] = count($ids);
$this->helperEmailMarketing->syncCustomers($data);

} catch (Exception $e) {
$result['status'] = false;
$result['message'] = $e->getMessage();
}

return $this->getResponse()->representJson(EmailMarketing::jsonEncode($result));
}
}
2 changes: 1 addition & 1 deletion Controller/Adminhtml/Smtp/TestConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function execute()
} catch (Exception $e) {
$result = [
'status' => false,
'content' => __('Can\'t connect to the email marketing app. Please check the app id and secret key.')
'content' => __('Can\'t connect to the email marketing app. %1', $e->getMessage())
];
}

Expand Down
Loading

0 comments on commit 9a8b1d5

Please sign in to comment.