Skip to content

[draft] 12965-Custo-System-Seaction-for-Bundle-Product #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Block/Adminhtml/System/Config/BundleProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/

declare(strict_types=1);

namespace Magefan\Community\Block\Adminhtml\System\Config;

use Magento\Directory\Helper\Data as DirectoryHelper;
use Magento\Framework\Json\Helper\Data as JsonHelper;

class BundleProduct extends \Magento\Backend\Block\Template
{
private $moduleManager;

public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magefan\Community\Model\ModuleManager $moduleManager,
array $data = [],
?JsonHelper $jsonHelper = null,
?DirectoryHelper $directoryHelper = null)
{
$this->moduleManager = $moduleManager;
parent::__construct($context, $data, $jsonHelper, $directoryHelper);
}

public function getModules()
{
return $this->moduleManager->getAllSections();
}
}
89 changes: 89 additions & 0 deletions Model/ModuleManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/

declare(strict_types=1);

namespace Magefan\Community\Model;

class ModuleManager
{
private $moduleManager = [
'mfseo' => [
'plus' => ['mfrichsnippets','mfxmlsitemap','mfhs'],
'extra' => ['alternatehreflang','mfogt','mftwittercards']
],
'mfspeedoptimizations' => [
'base' => ['mflazyzoad','mfrocketjavascript'],
'plus' => ['mfwebp'],
'extra' => ['mfpagecachewarmer']
],
];

/**
* @var GetModuleVersion
*/
private $getModuleVersion;

/**
* @var SectionFactory
*/
private $sectionFactory;

/**
* @param GetModuleVersion $getModuleVersion
* @param SectionFactory $sectionFactory
*/
public function __construct(
GetModuleVersion $getModuleVersion,
SectionFactory $sectionFactory
)
{
$this->getModuleVersion = $getModuleVersion;
$this->sectionFactory = $sectionFactory;
}

/**
* @return array
*/
public function getAllSections()
{
$allInstModule = [];
foreach ($this->moduleManager as $section => $plans) {
$extensionName = $this->sectionFactory->create(['name' => $section])->getModuleName();
$extensionName = str_replace(['Extra','Plus'],'', $extensionName);
foreach ($plans as $key => $modules) {
if ($key == 'base' ||$this->getModuleVersion->execute('Magefan_' . $extensionName . ucfirst($key))) {
$allInstModule[$section] = array_merge($allInstModule[$section] ?? [], $modules);
}
}
}

return $allInstModule;
}

/**
* @param $name
* @return array|null
*/
public function getSectionByName($name)
{
if (isset($this->moduleManager[$name])) {
$sections = [];
foreach ($this->moduleManager[$name] as $plan => $data) {
foreach ($data as $section) {
$extensionName = $this->sectionFactory->create(['name' => $section])->getModuleName();
$extensionName = str_replace(['Extra','Plus'],'', $extensionName);
if ($plan == 'base' || $this->getModuleVersion->execute('Magefan_' . $extensionName . ucfirst($plan))) {
$sections[] = $section;
}
}

}
return $sections;
}
return null;
}
}
86 changes: 86 additions & 0 deletions Model/SystemConfigAutoKeyManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/

declare(strict_types=1);

namespace Magefan\Community\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Store\Model\ScopeInterface;

class SystemConfigAutoKeyManager
{

/**
* @var WriterInterface
*/
private $configWriter;

/**
* @var ModuleManager
*/
private $moduleManager;

/**
* @var GetModuleVersion
*/
private $getModuleVersion;

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

/**
* @var SectionFactory
*/
private $sectionFactory;

/**
* @param WriterInterface $configWriter
* @param GetModuleVersion $getModuleVersion
* @param ModuleManager $moduleManager
* @param ScopeConfigInterface $scopeConfig
* @param SectionFactory $sectionFactory
*/
public function __construct(
WriterInterface $configWriter,
GetModuleVersion $getModuleVersion,
ModuleManager $moduleManager,
ScopeConfigInterface $scopeConfig,
SectionFactory $sectionFactory
) {
$this->configWriter = $configWriter;
$this->getModuleVersion = $getModuleVersion;
$this->moduleManager = $moduleManager;
$this->scopeConfig = $scopeConfig;
$this->sectionFactory = $sectionFactory;
}

/**
* @param string $section
* @param string $key
* @return void
*/
public function execute(string $section, string $key) {
$sections = $this->moduleManager->getSectionByName($section);

if ($sections) {
foreach ($sections as $section) {
$sectionData = $this->sectionFactory->create(['name' => $section]);
$alreadyExist = $this->scopeConfig->getValue(
$sectionData->getName() . '/g'.'en'.'er'.'al'.'/k'.'e'.'y',
ScopeInterface::SCOPE_STORE
);

if ($sectionData->getModule() && !$alreadyExist) {
$this->configWriter->save($section . '/g'.'en'.'er'.'al'.'/k'.'e'.'y', $key);
}
}
}
}
}
14 changes: 13 additions & 1 deletion Observer/ConfigObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Magefan\Community\Observer;

use Magefan\Community\Model\SystemConfigAutoKeyManager;
use Magento\Framework\Event\ObserverInterface;
use Magefan\Community\Model\SectionFactory;
use Magefan\Community\Model\Section\Info;
Expand Down Expand Up @@ -44,26 +45,34 @@ class ConfigObserver implements ObserverInterface
*/
private $config;

/**
* @var SystemConfigAutoKeyManager
*/
private $autoKeyManager;

/**
* ConfigObserver constructor.
* @param SectionFactory $sectionFactory
* @param Info $info
* @param ManagerInterface $messageManager
* @param SetLinvFlag $setLinvFlag
* @param Config $config
* @param SystemConfigAutoKeyManager $autoKeyManager
*/
final public function __construct(
SectionFactory $sectionFactory,
Info $info,
ManagerInterface $messageManager,
SetLinvFlag $setLinvFlag,
Config $config
Config $config,
SystemConfigAutoKeyManager $autoKeyManager
) {
$this->sectionFactory = $sectionFactory;
$this->info = $info;
$this->messageManager = $messageManager;
$this->setLinvFlag = $setLinvFlag;
$this->config = $config;
$this->autoKeyManager = $autoKeyManager;
}

/**
Expand Down Expand Up @@ -125,6 +134,9 @@ final public function execute(\Magento\Framework\Event\Observer $observer)
);
} else {
$this->setLinvFlag->execute($module, 0);
if ($key) {
$this->autoKeyManager->execute($section->getName(),$key);
}
}
}
}
8 changes: 8 additions & 0 deletions view/adminhtml/layout/adminhtml_system_config_edit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magefan\Community\Block\Adminhtml\System\Config\BundleProduct" name="mf.config.bundle.product.section" template="Magefan_Community::mfconfig-section.phtml"/>
</referenceContainer>
</body>
</page>
Loading