From 96b245f54bf41447e3a8c8fe5b3b15bdcd7fc922 Mon Sep 17 00:00:00 2001 From: "ivan.hrytsai" Date: Fri, 7 Mar 2025 11:40:04 +0200 Subject: [PATCH 1/2] 12965-Custo-System-Seaction-for-Bundle-Product --- Model/ModulePool.php | 61 +++++++++ Model/SystemConfigAutoKeyManager.php | 76 +++++++++++ .../layout/adminhtml_system_config_edit.xml | 8 ++ .../templates/mfconfig-section.phtml | 126 ++++++++++++++++++ 4 files changed, 271 insertions(+) create mode 100644 Model/ModulePool.php create mode 100644 Model/SystemConfigAutoKeyManager.php create mode 100644 view/adminhtml/layout/adminhtml_system_config_edit.xml create mode 100644 view/adminhtml/templates/mfconfig-section.phtml diff --git a/Model/ModulePool.php b/Model/ModulePool.php new file mode 100644 index 0000000..e3618c3 --- /dev/null +++ b/Model/ModulePool.php @@ -0,0 +1,61 @@ +modules = $modules; + } + + /** + * @return array + */ + public function getAll():array + { + $structuredModules = []; + + foreach ($this->modules as $parentModule => $childModules) { + foreach ($childModules as $childModule) { + $structuredModules[$parentModule] = array_merge( + $structuredModules[$parentModule] ?? [], + array_map('trim', explode(',', $childModule)) + ); + } + } + + return $structuredModules ?? []; + } + + /** + * @param string $section + * @return string + */ + public function getTemplate(string $section) + { + if (isset($this->modules[$section]) && + (is_array($this->modules[$section]) || $this->modules[$section] instanceof Countable) + ) { + return $this->modules[$section]; + } + return null; + } +} diff --git a/Model/SystemConfigAutoKeyManager.php b/Model/SystemConfigAutoKeyManager.php new file mode 100644 index 0000000..0e8f613 --- /dev/null +++ b/Model/SystemConfigAutoKeyManager.php @@ -0,0 +1,76 @@ +configWriter = $configWriter; + $this->getModuleVersion = $getModuleVersion; + $this->modulePool = $modulePool; + } + + /** + * @param string $section + * @param string $key + * @return void + */ + public function execute(string $section, string $key) { + $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); + $moduleData = $this->modulePool->getTemplate($section); + $moduleName = $objectManager->create(Section::class, ['name' => $section])->getModuleName(true); + + $sections = []; + if ($moduleData) { + foreach ($moduleData as $plan => $data) { + if ($plan == 'base' || $this->getModuleVersion->execute('Magefan_' . $moduleName . ucfirst($plan))) { + $sections = array_merge( + $sections, + is_string($data) ? array_map('trim', explode(',', $data)) : (array) $data + ); + } + } + } + if ($sections) { + foreach ($sections as $section) { + $sectionData = $objectManager->create(Section::class, ['name' => $section]); + if ($sectionData->getModule()) { + $this->configWriter->save($section . '/g'.'en'.'er'.'al'.'/k'.'e'.'y', $key); + } + } + } + } +} \ No newline at end of file diff --git a/view/adminhtml/layout/adminhtml_system_config_edit.xml b/view/adminhtml/layout/adminhtml_system_config_edit.xml new file mode 100644 index 0000000..a370cee --- /dev/null +++ b/view/adminhtml/layout/adminhtml_system_config_edit.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/view/adminhtml/templates/mfconfig-section.phtml b/view/adminhtml/templates/mfconfig-section.phtml new file mode 100644 index 0000000..42fb7db --- /dev/null +++ b/view/adminhtml/templates/mfconfig-section.phtml @@ -0,0 +1,126 @@ +get(Magefan\Community\Model\ModulePool::class)->getAll(); +?> + + + From 798e2d69edb4b1f1183efe2d18ddcb6181bda690 Mon Sep 17 00:00:00 2001 From: "ivan.hrytsai" Date: Tue, 11 Mar 2025 11:59:16 +0200 Subject: [PATCH 2/2] Custo-System-Seaction-for-Bundle-Product --- .../Adminhtml/System/Config/BundleProduct.php | 33 +++++++ Model/ModuleManager.php | 89 +++++++++++++++++++ Model/ModulePool.php | 61 ------------- Model/SystemConfigAutoKeyManager.php | 52 ++++++----- Observer/ConfigObserver.php | 14 ++- .../layout/adminhtml_system_config_edit.xml | 2 +- .../templates/mfconfig-section.phtml | 43 +++++---- 7 files changed, 188 insertions(+), 106 deletions(-) create mode 100644 Block/Adminhtml/System/Config/BundleProduct.php create mode 100644 Model/ModuleManager.php delete mode 100644 Model/ModulePool.php diff --git a/Block/Adminhtml/System/Config/BundleProduct.php b/Block/Adminhtml/System/Config/BundleProduct.php new file mode 100644 index 0000000..7aa7b94 --- /dev/null +++ b/Block/Adminhtml/System/Config/BundleProduct.php @@ -0,0 +1,33 @@ +moduleManager = $moduleManager; + parent::__construct($context, $data, $jsonHelper, $directoryHelper); + } + + public function getModules() + { + return $this->moduleManager->getAllSections(); + } +} \ No newline at end of file diff --git a/Model/ModuleManager.php b/Model/ModuleManager.php new file mode 100644 index 0000000..7c89962 --- /dev/null +++ b/Model/ModuleManager.php @@ -0,0 +1,89 @@ + [ + '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; + } +} \ No newline at end of file diff --git a/Model/ModulePool.php b/Model/ModulePool.php deleted file mode 100644 index e3618c3..0000000 --- a/Model/ModulePool.php +++ /dev/null @@ -1,61 +0,0 @@ -modules = $modules; - } - - /** - * @return array - */ - public function getAll():array - { - $structuredModules = []; - - foreach ($this->modules as $parentModule => $childModules) { - foreach ($childModules as $childModule) { - $structuredModules[$parentModule] = array_merge( - $structuredModules[$parentModule] ?? [], - array_map('trim', explode(',', $childModule)) - ); - } - } - - return $structuredModules ?? []; - } - - /** - * @param string $section - * @return string - */ - public function getTemplate(string $section) - { - if (isset($this->modules[$section]) && - (is_array($this->modules[$section]) || $this->modules[$section] instanceof Countable) - ) { - return $this->modules[$section]; - } - return null; - } -} diff --git a/Model/SystemConfigAutoKeyManager.php b/Model/SystemConfigAutoKeyManager.php index 0e8f613..57aa8fd 100644 --- a/Model/SystemConfigAutoKeyManager.php +++ b/Model/SystemConfigAutoKeyManager.php @@ -8,7 +8,9 @@ namespace Magefan\Community\Model; +use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\App\Config\Storage\WriterInterface; +use Magento\Store\Model\ScopeInterface; class SystemConfigAutoKeyManager { @@ -19,28 +21,44 @@ class SystemConfigAutoKeyManager private $configWriter; /** - * @var ModulePool + * @var ModuleManager */ - private $modulePool; + private $moduleManager; /** * @var GetModuleVersion */ private $getModuleVersion; + /** + * @var ScopeConfigInterface + */ + private $scopeConfig; + + /** + * @var SectionFactory + */ + private $sectionFactory; + /** * @param WriterInterface $configWriter * @param GetModuleVersion $getModuleVersion - * @param ModulePool $modulePool + * @param ModuleManager $moduleManager + * @param ScopeConfigInterface $scopeConfig + * @param SectionFactory $sectionFactory */ public function __construct( WriterInterface $configWriter, GetModuleVersion $getModuleVersion, - ModulePool $modulePool + ModuleManager $moduleManager, + ScopeConfigInterface $scopeConfig, + SectionFactory $sectionFactory ) { $this->configWriter = $configWriter; $this->getModuleVersion = $getModuleVersion; - $this->modulePool = $modulePool; + $this->moduleManager = $moduleManager; + $this->scopeConfig = $scopeConfig; + $this->sectionFactory = $sectionFactory; } /** @@ -49,25 +67,17 @@ public function __construct( * @return void */ public function execute(string $section, string $key) { - $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); - $moduleData = $this->modulePool->getTemplate($section); - $moduleName = $objectManager->create(Section::class, ['name' => $section])->getModuleName(true); + $sections = $this->moduleManager->getSectionByName($section); - $sections = []; - if ($moduleData) { - foreach ($moduleData as $plan => $data) { - if ($plan == 'base' || $this->getModuleVersion->execute('Magefan_' . $moduleName . ucfirst($plan))) { - $sections = array_merge( - $sections, - is_string($data) ? array_map('trim', explode(',', $data)) : (array) $data - ); - } - } - } if ($sections) { foreach ($sections as $section) { - $sectionData = $objectManager->create(Section::class, ['name' => $section]); - if ($sectionData->getModule()) { + $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); } } diff --git a/Observer/ConfigObserver.php b/Observer/ConfigObserver.php index 6aaf61b..18fa088 100644 --- a/Observer/ConfigObserver.php +++ b/Observer/ConfigObserver.php @@ -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; @@ -44,6 +45,11 @@ class ConfigObserver implements ObserverInterface */ private $config; + /** + * @var SystemConfigAutoKeyManager + */ + private $autoKeyManager; + /** * ConfigObserver constructor. * @param SectionFactory $sectionFactory @@ -51,19 +57,22 @@ class ConfigObserver implements ObserverInterface * @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; } /** @@ -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); + } } } } diff --git a/view/adminhtml/layout/adminhtml_system_config_edit.xml b/view/adminhtml/layout/adminhtml_system_config_edit.xml index a370cee..712c6c1 100644 --- a/view/adminhtml/layout/adminhtml_system_config_edit.xml +++ b/view/adminhtml/layout/adminhtml_system_config_edit.xml @@ -2,7 +2,7 @@ - + diff --git a/view/adminhtml/templates/mfconfig-section.phtml b/view/adminhtml/templates/mfconfig-section.phtml index 42fb7db..7f2a8e0 100644 --- a/view/adminhtml/templates/mfconfig-section.phtml +++ b/view/adminhtml/templates/mfconfig-section.phtml @@ -1,10 +1,17 @@ get(Magefan\Community\Model\ModulePool::class)->getAll(); +/** + * Copyright © Magefan (support@magefan.com). All rights reserved. + * Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement). + */ +?> +