Skip to content

Commit

Permalink
Merge pull request #4 from outeredge/remove-layout-module-and-more
Browse files Browse the repository at this point in the history
Remove layout module and more
  • Loading branch information
davidwindell authored Mar 2, 2022
2 parents 0d3a061 + 3afc9ff commit 90ee51f
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 34 deletions.
39 changes: 27 additions & 12 deletions Block/Form/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
namespace OuterEdge\Multiplenewsletter\Block\Form;

use Magento\Framework\View\Element\Template;
use OuterEdge\Layout\Helper\Data as LayoutHelper;
use Magento\Framework\View\Element\Template\Context;
use Magento\Customer\Model\SessionFactory;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use OuterEdge\Multiplenewsletter\Helper\Data;

class Options extends Template
{
protected $_newsletterOptions;

/**
* @var LayoutHelper
*/
protected $layoutHelper;
protected $newsletterOptions;

/**
* @var SessionFactory
Expand All @@ -27,32 +25,45 @@ class Options extends Template
*/
protected $customerRepositoryInterface;

/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;

/**
* @param Context $context
* @param LayoutHelper $layoutHelper
* @param SessionFactory $sessionFactory
* @param CustomerRepositoryInterface $customerRepositoryInterface
* @param ScopeConfigInterface $scopeConfig
* @param array $data
*/
public function __construct(
Context $context,
LayoutHelper $layoutHelper,
SessionFactory $sessionFactory,
CustomerRepositoryInterface $customerRepositoryInterface,
ScopeConfigInterface $scopeConfig,
array $data = []
) {
$this->layoutHelper = $layoutHelper;
$this->sessionFactory = $sessionFactory;
$this->customerRepositoryInterface = $customerRepositoryInterface;
$this->scopeConfig = $scopeConfig;
parent::__construct($context, $data);
}

public function getNewsletterOptions()
{
if ($this->_newsletterOptions === null) {
$this->_newsletterOptions = $this->layoutHelper->getGroupAndElements('newsletter_options');
if ($this->newsletterOptions === null) {

$data = $this->scopeConfig->getValue('multinewletteroptions/list/options', ScopeInterface::SCOPE_STORE);
foreach (explode(',', $data) as $option) {
$this->newsletterOptions[] = [
'lable' => ucfirst(ltrim($option)),
'code' => str_replace(' ', '_', ltrim($option))
];
}
}
return $this->_newsletterOptions;

return $this->newsletterOptions;
}

public function getNewsletterOptionsByCustomer($newsletterOption)
Expand All @@ -69,6 +80,10 @@ public function getNewsletterOptionsByCustomer($newsletterOption)
}

if (!empty($customerNewsOpt)) {
if ($customerNewsOpt == Data::CORE_NEWSLETTER_SUBSCRIBE) {
return true;
}

if (strpos($customerNewsOpt, $newsletterOption) !== false) {
return true;
}
Expand Down
12 changes: 12 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace OuterEdge\Multiplenewsletter\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{
CONST CORE_NEWSLETTER_SUBSCRIBE = 'all';

CONST CORE_NEWSLETTER_UNSUBSCRIBE = 'null';
}
54 changes: 54 additions & 0 deletions Observer/UpdateFromAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace OuterEdge\Multiplenewsletter\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use OuterEdge\Multiplenewsletter\Helper\Data;

class UpdateFromAdmin implements ObserverInterface
{
/**
* @var CustomerRepositoryInterface
*/
protected $customerRepositoryInterface;

/**
* @param CustomerRepositoryInterface $customerRepositoryInterface
*/
public function __construct(
CustomerRepositoryInterface $customerRepositoryInterface
) {
$this->customerRepositoryInterface = $customerRepositoryInterface;
}

/**
* @param Observer $observer
*/
public function execute(Observer $observer)
{
$customerId = $observer->getCustomer()->getId();
$subscriptionStatus = (array)$observer->getRequest()->getParam('subscription_status');

if (empty($subscriptionStatus)) {
return;
}

$customer = $this->customerRepositoryInterface->getById((int)$customerId);

foreach ($subscriptionStatus as $status) {
if ($status) {
$customer->setCustomAttribute('newsletter_options', Data::CORE_NEWSLETTER_SUBSCRIBE);
} else {
$customer->setCustomAttribute('newsletter_options', Data::CORE_NEWSLETTER_UNSUBSCRIBE);
}

try {
$this->customerRepositoryInterface->save($customer);
} catch (\Exception $e) {
throw new \Exception('Error saving multiple newsletter');
}
}
}
}
22 changes: 9 additions & 13 deletions Observer/UpdateOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\SessionFactory;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use OuterEdge\Multiplenewsletter\Helper\Data;

class UpdateOptions implements ObserverInterface
{
/**
* @var StoreManagerInterface
*/
protected $storeManager;

/**
* @var CustomerRepositoryInterface
*/
Expand All @@ -26,16 +21,13 @@ class UpdateOptions implements ObserverInterface
protected $sessionFactory;

/**
* @param StoreManagerInterface $storeManager
* @param CustomerRepositoryInterface $customerRepositoryInterface
* @param SessionFactory $sessionFactory
*/
public function __construct(
StoreManagerInterface $storeManager,
CustomerRepositoryInterface $customerRepositoryInterface,
SessionFactory $sessionFactory
) {
$this->storeManager = $storeManager;
$this->customerRepositoryInterface = $customerRepositoryInterface;
$this->sessionFactory = $sessionFactory;
}
Expand All @@ -46,11 +38,15 @@ public function __construct(
public function execute(Observer $observer) : void
{
$newsOptions = $observer->getRequest()->getParam('newsletter_options', false);
if ($newsOptions) {
$dataToSave = implode(',', array_keys($newsOptions));
}

$dataToSave = implode(',', array_keys($newsOptions));

$storeId = (int)$this->storeManager->getStore()->getId();
$websiteId = (int)$this->storeManager->getStore($storeId)->getWebsiteId();
if ($observer->getRequest()->getParam('is_subscribed', false)) {
$dataToSave = Data::CORE_NEWSLETTER_SUBSCRIBE;
} elseif (!$newsOptions) {
$dataToSave = Data::CORE_NEWSLETTER_UNSUBSCRIBE;
}

try {
$customer = $this->customerRepositoryInterface->getById($this->getCustomerId());
Expand Down
111 changes: 111 additions & 0 deletions Setup/UpgradeData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace OuterEdge\Multiplenewsletter\Setup;

use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory as NewsCollectionFactory;
use Magento\Customer\Api\CustomerRepositoryInterface;
use OuterEdge\Multiplenewsletter\Helper\Data;
use Magento\Framework\App\State;
use Magento\Framework\App\ResourceConnection;
use Magento\Eav\Model\ResourceModel\Entity\Attribute;

class UpgradeData implements UpgradeDataInterface
{
/**
* @var NewsCollectionFactory
*/
protected $subscriberCollection;

/**
* @var CustomerRepositoryInterface
*/
protected $customerRepositoryInterface;

/**
* @var \Magento\Framework\App\State
*/
private $state;

/**
* @var ResourceConnection
*/
protected $resource;

/**
* @var Attribute
*/
protected $eavAttribute;

/**
* @param NewsCollectionFactory $subscriberCollection
* @param CustomerRepositoryInterface $customerRepositoryInterface
* @param State $state
* @param ResourceConnection $resource
* @param Attribute $eavAttribute
*/
public function __construct(
NewsCollectionFactory $subscriberCollection,
CustomerRepositoryInterface $customerRepositoryInterface,
State $state,
ResourceConnection $resource,
Attribute $eavAttribute
) {
$this->subscriberCollection = $subscriberCollection;
$this->customerRepositoryInterface = $customerRepositoryInterface;
$this->state = $state;
$this->resource = $resource;
$this->eavAttribute = $eavAttribute;
}

public function getSubscriberCollection()
{
return $this->subscriberCollection->create();
}

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$this->state->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);

if (version_compare($context->getVersion(), '1.0.1', '<')) {

$setup->startSetup();
$connection = $this->resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
$customerEntity = $connection->getTableName('customer_entity');
$customerEntityVarchar = $connection->getTableName('customer_entity_varchar');
$attributeId = $this->eavAttribute->getIdByCode(\Magento\Customer\Model\Customer::ENTITY, 'newsletter_options');

foreach ($this->getSubscriberCollection() as $customerNewsletter) {

if (!$customerNewsletter['customer_id']) {
continue;
}

$selectCustomer = "SELECT entity_id FROM $customerEntity WHERE entity_id LIKE ".$customerNewsletter['customer_id'];
$customerExist = $connection->fetchRow($selectCustomer);
if (!$customerExist) {
continue;
}

try {
$customer = $this->customerRepositoryInterface->getById($customerNewsletter['customer_id']);
//Only update if newsletter_options is empty
if ($customer->getCustomAttribute('newsletter_options')) {
continue;
}

$connection->fetchAll("INSERT INTO `".$customerEntityVarchar."`
(`value_id`, `attribute_id`, `entity_id`, `value`)
VALUES (NULL, '".$attributeId."', '".$customer->getId()."', '".Data::CORE_NEWSLETTER_SUBSCRIBE."')");

} catch (\Exception $e) {
throw new \Exception('Error on multiple newsletter with customer: '
.$customerNewsletter['customer_id'].' Reason: '.$e->getMessage());
}
}
$setup->endSetup();
}
}
}
6 changes: 6 additions & 0 deletions etc/adminhtml/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="adminhtml_customer_save_after">
<observer name="update_multinewsletter_field" instance="OuterEdge\Multiplenewsletter\Observer\UpdateFromAdmin" />
</event>
</config>
20 changes: 20 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="outeredge" translate="label" sortOrder="2000" class="outeredge-tab">
<label>outer/edge</label>
</tab>
<section id="multinewletteroptions" translate="label" type="text" sortOrder="11" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Multi Newsletter Emails</label>
<tab>outeredge</tab>
<resource>OuterEdge_Multiplenewsletter::MultiplenewsletterSettings</resource>
<group id="list" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Multi Newsletter Emails list</label>
<field id="options" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Newsletter options</label>
<comment>newsletter one, newsletter two, newsletter three</comment>
</field>
</group>
</section>
</system>
</config>
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="OuterEdge_Multiplenewsletter" setup_version="1.0.0">
<module name="OuterEdge_Multiplenewsletter" setup_version="1.0.1">
<sequence>
<module name="Magento_Newsletter"/>
</sequence>
Expand Down
15 changes: 7 additions & 8 deletions view/frontend/templates/form/newsletter-options.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
$newsletterOptions = $block->getNewsletterOptions();
?>

<?php foreach ($newsletterOptions->getElements() as $newsletters): ?>
<?php $isEnabled = $block->getNewsletterOptionsByCustomer($newsletters->getNewsletterOptionsCode()); ?>
<?php foreach ($newsletterOptions as $newsletters): ?>
<?php $isEnabled = $block->getNewsletterOptionsByCustomer($newsletters['code']); ?>
<div class="field choice">
<input type="checkbox" onClick="unCheckAll(this);" name="newsletter_options[<?php echo $newsletters->getNewsletterOptionsCode()?>]"
id="newsletter_options[<?php echo $newsletters->getNewsletterOptionsCode()?>"
<input type="checkbox" onClick="unCheckAll(this);" name="newsletter_options[<?php echo $newsletters['code']?>]"
id="newsletter_options[<?php echo $newsletters['code']?>]"
value="1"
title="<?= $block->escapeHtml($newsletters->getTitle()) ?>"
title="<?= $block->escapeHtml($newsletters['lable']) ?>"
<?php if($isEnabled): ?> checked="checked"<?php endif; ?> class="checkbox"
/>
<label for="newsletter_options[<?php echo $newsletters->getNewsletterOptionsCode()?>">
<?= $block->escapeHtml($newsletters->getTitle()) ?>
<label for="newsletter_options[<?php echo $newsletters['code']?>]">
<?= $block->escapeHtml($newsletters['lable']) ?>
</label>
</div>
<?php endforeach; ?>
Expand All @@ -31,4 +31,3 @@ $newsletterOptions = $block->getNewsletterOptions();
allSubscription.checked = false;
}
</script>

0 comments on commit 90ee51f

Please sign in to comment.