Skip to content

Commit

Permalink
update Magento module
Browse files Browse the repository at this point in the history
  • Loading branch information
ecomail-cz committed Apr 1, 2021
1 parent b388f07 commit 8be0fa3
Show file tree
Hide file tree
Showing 61 changed files with 2,521 additions and 837 deletions.
75 changes: 75 additions & 0 deletions app/code/Ecomail/Ecomail/Block/System/Config/Form/Field/Status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Ecomail\Ecomail\Block\System\Config\Form\Field;

use Ecomail\Ecomail\Helper\Data;
use Ecomail\Ecomail\Model\Api;
use Exception;
use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;

class Status extends Field
{
/**
* @var string
*/
protected $_template = 'Ecomail_Ecomail::system/config/form/field/status.phtml';

/**
* @var Data
*/
private $helper;

/**
* @var Api
*/
private $api;

/**
* Status constructor.
* @param Context $context
* @param Data $helper
* @param Api $api
* @param array $data
*/
public function __construct(
Context $context,
Data $helper,
Api $api,
array $data = []
) {
parent::__construct($context, $data);
$this->helper = $helper;
$this->api = $api;
}

/**
* @return string
*/
public function getStatusCode(): string
{
if (!$this->helper->isEnabled()) {
return 'Inactive';
}

try {
$this->api->getSubscriberLists();
return 'Active';
} catch (Exception $e) {
return 'Error';
}
}

/**
* Return element html
*
* @param AbstractElement $element
* @return string
*
*/
protected function _getElementHtml(AbstractElement $element): string
{
return $this->_toHtml();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Ecomail\Ecomail\Block\System\Config\Form\Field\Subscriber;

use Magento\Backend\Block\Widget\Button;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\Exception\LocalizedException;

class LoadLists extends Field
{

/**
* @var string
*/
protected $_template = 'Ecomail_Ecomail::system/config/form/field/subscriber/load_lists.phtml';

/**
* Remove scope label
*
* @param AbstractElement $element
* @return string
*/
public function render(AbstractElement $element): string
{
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
return parent::render($element);
}

/**
* Return element html
*
* @param AbstractElement $element
* @return string
*
*/
protected function _getElementHtml(AbstractElement $element): string
{
return $this->_toHtml();
}

/**
* @return string
* @throws LocalizedException
*/
public function getButtonHtml(): string
{
$button = $this->getLayout()->createBlock(
Button::class
)->setData(
[
'id' => 'load_lists',
'label' => __('Load Lists')
]
);

return $button->toHtml();
}

/**
* @return string
*/
public function getAjaxUrl(): string
{
return $this->getUrl('*/system_config_ecomail/loadLists');
}
}
4 changes: 4 additions & 0 deletions app/code/Ecomail/Ecomail/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ecomail changelog

## v2.0.0 - 22 Mar, 2021
* initial release
57 changes: 0 additions & 57 deletions app/code/Ecomail/Ecomail/Controller/Adminhtml/Ecomail/Ajax.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Ecomail\Ecomail\Controller\Adminhtml\System\Config\Ecomail;

use Ecomail\Ecomail\Model\Api;
use Exception;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;

class LoadLists extends Action
{
const ADMIN_RESOURCE = 'Ecomail_Ecomail::ecomail_configuration';

/**
* @var Api
*/
private $api;

public function __construct(
Context $context,
Api $api
) {
parent::__construct($context);
$this->api = $api;
}

/**
* @return ResultInterface
*/
public function execute(): ResultInterface
{
$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);

$apiKey = $this->getRequest()->getParam('api_key');

try {
$subscriberLists = $this->api->getSubscriberLists($apiKey);
} catch (Exception $e) {
return $result->setData([]);
}

$response = [];

foreach ($subscriberLists as $list) {
$response[] = [
'label' => $list['name'],
'value' => $list['id'],
];
}

return $result->setData($response);
}
}
52 changes: 52 additions & 0 deletions app/code/Ecomail/Ecomail/CustomerData/Ecomail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Ecomail\Ecomail\CustomerData;

use Ecomail\Ecomail\Helper\Data;
use Magento\Customer\CustomerData\SectionSourceInterface;
use Magento\Customer\Model\Session;
use Magento\Framework\DataObject;

class Ecomail extends DataObject implements SectionSourceInterface
{
/**
* @var \Magento\Customer\Model\Session
*/
private $customerSession;

/**
* @var Data
*/
private $helper;

/**
* Ecomail constructor.
* @param Session $customerSession
* @param Data $helper
* @param array $data
*/
public function __construct(
Session $customerSession,
Data $helper,
array $data = []
) {
parent::__construct($data);
$this->customerSession = $customerSession;
$this->helper = $helper;
}

/**
* @return array
*/
public function getSectionData(): array
{
$sectionData = [];

if ($this->helper->isTrackingEnabled() && $this->customerSession->getEcomailEmail()) {
$sectionData['email'] = $this->customerSession->getEcomailEmail();
$this->customerSession->setEcomailEmail(null);
}

return $sectionData;
}
}
Loading

0 comments on commit 8be0fa3

Please sign in to comment.