Skip to content

Commit

Permalink
Edit and clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoducnhuiit committed Jul 8, 2021
1 parent 465e391 commit 6f29e1a
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 475 deletions.
53 changes: 0 additions & 53 deletions Block/Js.php

This file was deleted.

53 changes: 0 additions & 53 deletions Block/Message.php

This file was deleted.

137 changes: 34 additions & 103 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,131 +23,62 @@
* Class Data
* @package Magepow\AjaxCompare\Helper
*/

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
* @var Registry
* @var array
*/
protected $_coreRegistry;
protected $configModule;

/**
* @var StoreManagerInterface
* @var \Magento\Framework\Module\Manager
*/
protected $_storeManager;
protected $moduleManager;

/**
* @var CustomerSession
*/
protected $_customerSession;

/**
* @var LayoutFactory
*/
protected $_layoutFactory;

/**
* @var EncoderInterface
*/
protected $_jsonEncoder;

/**
* @var DecoderInterface
*/
protected $_jsonDecoder;


/**
* Data constructor.
* @param Context $context
* @param StoreManagerInterface $storeManager
* @param Registry $coreRegistry
* @param CustomerSession $customerSession
* @param LayoutFactory $layoutFactory
* @param EncoderInterface $jsonEncoder
* @param DecoderInterface $jsonDecoder
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
Registry $coreRegistry,
CustomerSession $customerSession,
LayoutFactory $layoutFactory,
EncoderInterface $jsonEncoder,
DecoderInterface $jsonDecoder
) {
\Magento\Framework\App\Helper\Context $context,
\Magento\Framework\Module\Manager $moduleManager
)
{
parent::__construct($context);
$this->_storeManager = $storeManager;
$this->_coreRegistry = $coreRegistry;
$this->_customerSession = $customerSession;
$this->_layoutFactory = $layoutFactory;
$this->_jsonEncoder = $jsonEncoder;
$this->_jsonDecoder = $jsonDecoder;

$this->moduleManager = $moduleManager;
$this->configModule = $this->getConfig(strtolower($this->_getModuleName()));
}
public function getConfig($cfg='')

public function getConfig($cfg='')
{
if($cfg) return $this->scopeConfig->getValue( $cfg, \Magento\Store\Model\ScopeInterface::SCOPE_STORE );
return $this->scopeConfig;
}

/**
* @return string
*/
public function getAjaxCompareInitOptions()
{
$options = [
'ajaxCompare' => [
'enabled' => $this->isEnabledAjaxCompare(),
'ajaxCompareUrl' => $this->_getUrl('catalog/product_compare/add'),
'popupTTL' => $this->getConfig("magepow_ajaxcompare/general/popupttl"),
'showLoader' => (bool) $this->getConfig('magepow_ajaxcompare/general/loader'),
],
];

return $this->_jsonEncoder->encode($options);
}

/**
* @return bool
*/
public function isEnabledAjaxCompare()
public function getConfigModule($cfg='', $value=null)
{
return (bool)$this->getConfig('magepow_ajaxcompare/general/enabled');
$values = $this->configModule;
if( !$cfg ) return $values;
$config = explode('/', $cfg);
$end = count($config) - 1;
foreach ($config as $key => $vl) {
if( isset($values[$vl]) ){
if( $key == $end ) {
$value = $values[$vl];
}else {
$values = $values[$vl];
}
}

}
return $value;
}

/**
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getSuccessHtml()
public function isEnabledModule($moduleName)
{
$layout = $this->_layoutFactory->create(['cacheable' => false]);
$layout->getUpdate()->addHandle('ajaxcompare_success_message')->load();
$layout->generateXml();
$layout->generateElements();
$result = $layout->getOutput();
$layout->__destruct();
return $result;
return $this->moduleManager->isEnabled($moduleName);
}

/**
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getErrorHtml()
public function getModuleName()
{
$layout = $this->_layoutFactory->create(['cacheable' => false]);
$layout->getUpdate()->addHandle('ajaxcompare_error_message')->load();
$layout->generateXml();
$layout->generateElements();
$result = $layout->getOutput();
$layout->__destruct();
return $result;
return $this->_getModuleName();
}

public function getBaseUrlMedia()
{
return $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
}
}
}
Loading

0 comments on commit 6f29e1a

Please sign in to comment.