ltsecurityinc安防产品在线商城代码

This commit is contained in:
2026-09-14 17:47:10 +08:00
parent 57d8444c15
commit 6a3c9c5bd8
14539 changed files with 1775310 additions and 0 deletions
@@ -0,0 +1,337 @@
<?php
namespace Olegnax\Athlete2\Controller\Adminhtml\Import;
use Exception;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\View\Result\PageFactory;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Olegnax\BannerSlider\Model\ResourceModel\Slides\CollectionFactory;
class Export extends Action
{
const ADMIN_RESOURCE = 'Olegnax_Athlete2::export';
protected $_filesystem;
protected $_storeManager;
private $website;
private $store;
/**
* Constructor
*
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
Filesystem $filesystem
) {
$this->_filesystem = $filesystem;
parent::__construct($context);
}
/**
* Execute view action
*
* @return ResultInterface
*/
public function execute()
{
$data = $this->getRequest()->getPostValue();
$filename = $this->getFileName();
$document = [];
try {
if (is_array($data) && !empty($data)) {
foreach ($data as $key => $value) {
$methodName = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($key))));
if (method_exists($this, $methodName)) {
$content = call_user_func([$this, $methodName], $value);
if (is_array($content) && !empty($content)) {
$document[$key] = $content;
}
}
}
if (!empty($document)) {
$this->loadFile()->filePutContents($this->getDemoPath(), "<root>\n" . $this->prepareToXML($document) . "</root>\n");
} else {
throw new Exception(__('No content to export!'));
}
$this->messageManager->addSuccess(__('%1 was successfully exported.', $filename));
}
} catch (Exception $e) {
$this->messageManager->addError($e->getMessage());
}
return $this->_redirect($this->_redirect->getRefererUrl());
}
protected function getFileName()
{
$title = $this->getTitle();
$title = strtolower($title);
$title = str_replace(' ', '-', $title);
$title = sprintf('demo-%s.xml', $title);
return $title;
}
protected function getTitle()
{
$title = 'Default Config';
$storeManager = $this->_loadObject(StoreManagerInterface::class);
if ($storeId = (int)$this->getRequest()->getParam('store')) {
$title = $storeManager->getStore($storeId)->getName();
} elseif ($websiteId = (int)$this->getRequest()->getParam('website')) {
$title = $storeManager->getWebsite($websiteId)->getName();
}
return $title;
}
protected function _loadObject($object)
{
return $this->_getObjectManager()->get($object);
}
protected function _getObjectManager()
{
return ObjectManager::getInstance();
}
protected function loadFile()
{
return $this->_loadObject(File::class);
}
protected function getDemoPath()
{
return $this->getAbsolutePath(Import::DEMO_DIR) . DIRECTORY_SEPARATOR . $this->getFileName();
}
public function getAbsolutePath($path)
{
return $this->_filesystem->getDirectoryRead(DirectoryList::APP)->getAbsolutePath($path);
}
protected function prepareToXML($document, $parent_key = null, $level = 0)
{
$content = '';
foreach ($document as $key => $value) {
if (!is_string($key)) {
$key = 'item';
}
$tab_level = str_repeat(' ', $level + 1);
if (is_array($value)) {
$value = $this->prepareToXML($value, $key, $level + 1);
$content .= sprintf('%1$s<%2$s>
%3$s%1$s</%2$s>
', $tab_level, $key, $value);
} elseif (!empty($value) || is_numeric($value)) {
if (preg_match('/[<>&]/i', $value)) {
$value = '<![CDATA[' . $value . ']]>';
}
$content .= sprintf('%1$s<%2$s>%3$s</%2$s>
', $tab_level, $key, $value);
} else {
$content .= sprintf('%1$s<%2$s></%2$s>
', $tab_level, $key);
}
}
return $content;
}
public function getConfig($identifiers)
{
if (is_array($identifiers) && empty($identifiers)) {
return [];
}
$_identifiers = $identifiers;
foreach ($_identifiers as $__identifier) {
$reg_exp = '/^' . str_replace('/', '\/', $__identifier) . '\//i';
foreach ($identifiers as $key => $identifier) {
if (is_string($identifier)) {
if (preg_match($reg_exp, $identifier)) {
unset($identifiers[$key]);
} else {
$identifiers[$key] = explode('/', $identifier);
}
}
}
}
unset($_identifiers);
$scopeConfig = $this->getScopeConfig();
$config = [];
foreach ($identifiers as $identifier) {
$config = $this->mergeConfig($config, $this->_getSectionConfig($scopeConfig, $identifier));
}
return $config;
}
public function getScopeConfig()
{
$scopeConfig = $this->_loadObject(ScopeConfigInterface::class);
if ($websiteId = $this->getRequest()->getParam('website')) {
return $scopeConfig->getValue('', ScopeInterface::SCOPE_WEBSITE, $websiteId);
}
if ($storeId = $this->getRequest()->getParam('store')) {
return $scopeConfig->getValue('', ScopeInterface::SCOPE_STORE, $storeId);
}
return $scopeConfig->getValue('');
}
private function mergeConfig($array1, $array2)
{
foreach ($array2 as $key => $value) {
if (array_key_exists($key, $array1) && is_array($array1[$key]) && is_array($value)) {
$value = $this->mergeConfig($array1[$key], $value);
}
$array1[$key] = $value;
}
return $array1;
}
private function _getSectionConfig($section, $key)
{
$_section = [];
if (is_array($section) && !empty($section) && is_array($key) && !empty($key)) {
$_key = array_shift($key);
$value = null;
if (array_key_exists($_key, $section)) {
$value = $section[$_key];
}
if (!empty($key)) {
$_section[$_key] = $this->_getSectionConfig($value, $key);
} else {
$_section[$_key] = $value;
}
}
return $_section;
}
public function getBlocks($identifiers)
{
if (is_array($identifiers) && empty($identifiers)) {
return [];
}
$model = $this->_loadObject(\Magento\Cms\Model\ResourceModel\Block\CollectionFactory::class);
$itemCollection = $model->create()->addFieldToFilter('identifier', ['in' => [$identifiers]]);
$items = [];
if (count($itemCollection) > 0) {
foreach ($itemCollection as $item) {
$item = $item->getData();
$items[] = $this->removeAttrs($item, ['_first_store_id', 'block_id', 'creation_time', 'is_active', 'store_code', 'store_id', 'update_time']);
}
}
return $items;
}
private function removeAttrs($array, $attrs = [])
{
$_array = [];
foreach ($array as $key => $value) {
if (!in_array($key, $attrs)) {
$_array[$key] = $value;
}
}
return $_array;
}
public function getPages($identifiers)
{
if (is_array($identifiers) && empty($identifiers)) {
return [];
}
$model = $this->_loadObject(\Magento\Cms\Model\ResourceModel\Page\CollectionFactory::class);
$itemCollection = $model->create()->addFieldToFilter('identifier', ['in' => [$identifiers]]);
$items = [];
if (count($itemCollection) > 0) {
foreach ($itemCollection as $item) {
$item = $item->getData();
$items[] = $this->removeAttrs($item, ['_first_store_id', 'creation_time', 'is_active', 'page_id', 'store_code', 'store_id', 'update_time']);
}
}
return $items;
}
public function getBannersliders($identifiers)
{
if (is_array($identifiers) && empty($identifiers)) {
return [];
}
$model = $this->_loadObject(\Olegnax\BannerSlider\Model\ResourceModel\Group\CollectionFactory::class);
$itemCollection = $model->create()->addFieldToFilter('identifier', ['in' => [$identifiers]]);
$items = [];
if (count($itemCollection) > 0) {
foreach ($itemCollection as $item) {
$item = $item->getData();
$item['slides'] = $this->_getBannersliders($item['group_id']);
$items[] = $this->removeAttrs($item, ['id', 'group_id', 'created_time', 'update_time']);
}
}
return $items;
}
private function _getBannersliders($id)
{
$model = $this->_loadObject(CollectionFactory::class);
$itemCollection = $model->create()->addFieldToFilter('slide_group', $id);
$items = [];
if (count($itemCollection) > 0) {
foreach ($itemCollection as $item) {
$item = $item->getData();
$items[] = $this->removeAttrs($item, ['id', 'slider_id', 'store_id', 'slide_group', 'status', 'created_time', 'update_time']);
}
}
return $items;
}
public function getCarousels($identifiers)
{
if (is_array($identifiers) && empty($identifiers)) {
return [];
}
$model = $this->_loadObject(\Olegnax\Carousel\Model\ResourceModel\Carousel\CollectionFactory::class);
$itemCollection = $model->create()->addFieldToFilter('identifier', ['in' => [$identifiers]]);
$items = [];
if (count($itemCollection) > 0) {
foreach ($itemCollection as $item) {
$item = $item->getData();
$item['slides'] = $this->_getCarousels($item['identifier']);
$items[] = $this->removeAttrs($item, ['carousel_id', 'creation_time', 'update_time']);
}
}
return $items;
}
private function _getCarousels($id)
{
$model = $this->_loadObject(\Olegnax\Carousel\Model\ResourceModel\Slide\CollectionFactory::class);
$itemCollection = $model->create()->addFieldToFilter('carousel', $id);
$items = [];
if (count($itemCollection) > 0) {
foreach ($itemCollection as $item) {
$item = $item->getData();
$items[] = $this->removeAttrs($item, ['slide_id', 'store_id', 'creation_time', 'update_time']);
}
}
return $items;
}
}
@@ -0,0 +1,44 @@
<?php
namespace Olegnax\Athlete2\Controller\Adminhtml\Import;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\View\Result\PageFactory;
class Exporter extends Action
{
const ADMIN_RESOURCE = 'Olegnax_Athlete2::export';
protected $resultPageFactory;
/**
* Constructor
*
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
/**
* Execute view action
*
* @return ResultInterface
*/
public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->prepend((__('Athlete2 Demo Export')));
return $resultPage;
}
}
@@ -0,0 +1,424 @@
<?php /** @noinspection PhpMissingFieldTypeInspection */
namespace Olegnax\Athlete2\Controller\Adminhtml\Import;
use Exception;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Cms\Model\Block;
use Magento\Cms\Model\Page;
use Magento\Config\Model\Config\Factory;
use Magento\Framework\App\Cache\Frontend\Pool;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Config\ConfigResource\ConfigInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Filesystem;
use Magento\Framework\Registry;
use Magento\Framework\Xml\Parser;
use Magento\Store\Model\ScopeInterface;
use Olegnax\Athlete2\Model\DynamicStyle\Generator as DynamicStyleGenerator;
use Olegnax\BannerSlider\Model\Group;
use Olegnax\BannerSlider\Model\Slides;
use Olegnax\Carousel\Model\Carousel;
use Olegnax\Carousel\Model\Slide;
use Olegnax\Core\Plugin\Backend\Magento\Cms\Model\Wysiwyg\Validator;
class Import extends Action
{
const ADMIN_RESOURCE = 'Olegnax_Athlete2::import';
const DEMO_DIR = 'code/Olegnax/Athlete2/Demos';
protected $_filesystem;
protected $_storeManager;
/**
* Dynamic Style generator
*
* @var DynamicStyleGenerator
*/
protected $_DynamicStyleGenerator;
private $_demo;
private $_replace;
/**
* @var Registry
*/
protected $_coreRegistry;
/**
* @var Parser
*/
protected $_parser;
/**
* Constructor
*/
public function __construct(
Context $context,
Filesystem $filesystem,
Parser $parser,
DynamicStyleGenerator $generator,
Registry $coreRegistry
) {
$this->_filesystem = $filesystem;
$this->_parser = $parser;
$this->_DynamicStyleGenerator = $generator;
$this->_coreRegistry = $coreRegistry;
parent::__construct($context);
}
public function execute()
{
$subDir = $this->getRequest()->getParam('subdir', '');
$this->_demo = $this->getRequest()->getParam('demo');
$this->_replace = !(bool)$this->getRequest()->getParam('notreplace', '');
if (!empty($this->_demo) && $this->demoFileExists($this->_demo, $subDir)) {
if ($this->demoIsReadable($this->_demo, $subDir)) {
$demoPath = $this->getDemoPath($this->_demo, $subDir);
try {
$xmlArray = $this->_parser->load($demoPath)->xmlToArray();
if (is_array($xmlArray) && !empty($xmlArray) && isset($xmlArray['root'])) {
foreach ($xmlArray['root'] as $key => $value) {
$methodName = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($key))));
if (method_exists($this, $methodName)) {
call_user_func([$this, $methodName], $value, $this->_replace);
}
}
$this->clearCache();
$this->messageManager->addSuccess(__('%1 was successfully imported.', $this->convertString($this->_demo)));
}
} catch (Exception $e) {
$this->messageManager->addError($e->getMessage());
}
} else {
$this->messageManager->addError(__('Cannot import this Demo.'));
}
} else {
$this->messageManager->addError(__('This Demo no longer exists.'));
}
return $this->_redirect($this->_redirect->getRefererUrl());
}
protected function demoFileExists($demoId, $subDir = '')
{
$demoPath = $this->getDemoPath($demoId, $subDir);
return file_exists($demoPath);
}
protected function getDemoPath($demoId, $subDir = '')
{
if (!empty($subDir)) {
$subDir = $subDir . DIRECTORY_SEPARATOR;
} else {
$subDir = '';
}
return $this->getAbsolutePath(self::DEMO_DIR) . DIRECTORY_SEPARATOR . $subDir . $demoId . '.xml';
}
protected function getAbsolutePath($path)
{
return $this->_filesystem->getDirectoryRead(DirectoryList::APP)->getAbsolutePath($path);
}
protected function demoIsReadable($demoId, $subDir = '')
{
$demoPath = $this->getDemoPath($demoId, $subDir);
return is_readable($demoPath);
}
public function clearCache()
{
/** @var TypeListInterface $cacheTypeList */
$cacheTypeList = $this->_loadObject(TypeListInterface::class);
/** @var Pool $CacheFrontendPool */
$CacheFrontendPool = $this->_loadObject(Pool::class);
$types = [
'config',
'layout',
'block_html',
'collections',
'reflection',
'db_ddl',
'eav',
'config_integration',
'config_integration_api',
'full_page',
'translate',
'config_webservice',
];
foreach ($types as $type) {
$cacheTypeList->cleanType($type);
}
foreach ($CacheFrontendPool as $cacheFrontend) {
$cacheFrontend->getBackend()->clean();
}
}
protected function _loadObject($object)
{
return $this->_getObjectManager()->get($object);
}
protected function _getObjectManager()
{
return ObjectManager::getInstance();
}
public function convertString($demoId)
{
return ucwords(strtolower(str_replace(['-', '_', '\\', '/'], ' ', $demoId)));
}
public function setConfig($demoContent)
{
if (is_array($demoContent) && !empty($demoContent)) {
$website = $this->getRequest()->getParam('website');
$store = $this->getRequest()->getParam('store');
/** @var Factory $config */
$config = $this->_loadObject(Factory::class);
foreach ($demoContent as $section => $groups) {
$configData = [
'section' => $section,
'website' => $website,
'store' => $store,
'groups' => $this->_prepareGroup($groups, $section),
];
$config->create(['data' => $configData])->save();
$this->_eventManager->dispatch('admin_system_config_save', [
'configData' => $configData,
'request' => $this->getRequest(),
]);
}
}
}
public function _prepareGroup($content, $section)
{
$groups = [];
if (!empty($content)) {
foreach ($content as $group_name => $group_fields) {
$fields = [];
if (!empty($group_fields)) {
foreach ($group_fields as $field => $value) {
if (null === $value) {
$value = '';
}
if (!is_string($value)) {
continue;
}
if (preg_match('/\.[a-z0-9]{3,4}$/i', $value)) {
$this->_setConfigImage([$section, $group_name, $field], $value);
}
$fields[$field] = ['value' => $value];
}
}
$groups[$group_name] = [
'fields' => $fields,
];
}
}
return $groups;
}
private function _setConfigImage($fieldPath, $value)
{
$store = (int)$this->getRequest()->getParam('store', 0);
$website = (int)$this->getRequest()->getParam('website', 0);
/** @var ConfigInterface $config */
$config = $this->_loadObject(ConfigInterface::class);
if (is_array($fieldPath)) {
$fieldPath = implode('/', $fieldPath);
}
if ($website) {
$config->saveConfig($fieldPath, $value, ScopeInterface::SCOPE_WEBSITES, $website);
} elseif ($store) {
$config->saveConfig($fieldPath, $value, ScopeInterface::SCOPE_STORES, $store);
} else {
$config->saveConfig($fieldPath, $value);
}
}
public function setBlocks($demoContent, $replace = true)
{
if (is_array($demoContent) && isset($demoContent['item']) && !empty($demoContent['item'])) {
if (isset($demoContent['item']['identifier'])) {
$demoContent = [$demoContent['item']];
} else {
$demoContent = $demoContent['item'];
}
/** @var Block $model */
$model = $this->_loadObject(Block::class);
$this->_coreRegistry->register(Validator::VARIABLE_TO_DISABLE, true);
foreach ($demoContent as $item) {
if (!array_key_exists('identifier', $item) || empty($item['identifier'])) {
continue;
}
$identifier = $item['identifier'];
$collection = $model->getCollection()->addFieldToFilter('identifier', $identifier)->load();
if ($replace) {
if ($collection->count()) {
foreach ($collection as $_block) {
$_block->delete();
}
}
} else {
$index = 1;
while ($collection->count()) {
$index++;
$identifier = $item['identifier'] . '-' . $index;
$collection = $model->getCollection()->addFieldToFilter('identifier', $identifier)->load();
}
$item['identifier'] = $identifier;
}
$model->setData($item)->setIsActive(1)->setStores([0])->save();
}
$this->_coreRegistry->unregister(Validator::VARIABLE_TO_DISABLE);
}
}
public function setPages($demoContent, $replace = true)
{
if (is_array($demoContent) && isset($demoContent['item']) && !empty($demoContent['item'])) {
if (isset($demoContent['item']['identifier'])) {
$demoContent = [$demoContent['item']];
} else {
$demoContent = $demoContent['item'];
}
/** @var Page $model */
$model = $this->_loadObject(Page::class);
$this->_coreRegistry->register(Validator::VARIABLE_TO_DISABLE, true);
foreach ($demoContent as $item) {
if (!array_key_exists('identifier', $item) || empty($item['identifier'])) {
continue;
}
$identifier = $item['identifier'];
$collection = $model->getCollection()->addFieldToFilter('identifier', $identifier)->load();
if ($replace) {
if ($collection->count()) {
foreach ($collection as $_block) {
$_block->delete();
}
}
} else {
$index = 1;
while ($collection->count()) {
$index++;
$identifier = $item['identifier'] . '-' . $index;
$collection = $model->getCollection()->addFieldToFilter('identifier', $identifier)->load();
}
$item['identifier'] = $identifier;
}
$model->setData($item)->setIsActive(1)->setStores([0])->save();
}
$this->_coreRegistry->unregister(Validator::VARIABLE_TO_DISABLE);
}
}
public function setBannersliders($demoContent)
{
if (is_array($demoContent) && isset($demoContent['item']) && !empty($demoContent['item'])) {
if (isset($demoContent['item']['group_name'])) {
$demoContent = [$demoContent['item']];
} else {
$demoContent = $demoContent['item'];
}
/** @var Group $model */
$model = $this->_loadObject(Group::class);
/** @var Slides $model2 */
$model2 = $this->_loadObject(Slides::class);
foreach ($demoContent as $item) {
if (!array_key_exists('group_name', $item)) {
continue;
}
$groupCollection = $model->getCollection()->addFieldToFilter('identifier', $item['identifier'])->load();
if (!empty($groupCollection)) {
foreach ($groupCollection as $_group) {
$slidesCollection = $model2->getCollection()->addFieldToFilter('slide_group', $_group->getId())->load();
if (!empty($slidesCollection)) {
foreach ($slidesCollection as $_slides) {
$_slides->delete();
}
}
$_group->delete();
}
}
$slides = [];
if (array_key_exists('slides', $item)) {
if (!empty($item['slides']) && array_key_exists('item', $item['slides'])) {
$slides = $item['slides']['item'];
}
unset($item['slides']);
}
$current_model = $model->setData($item)->save();
if (!empty($slides) && $group_id = $current_model->getId()) {
foreach ($slides as $_item) {
$model2->setData($_item)->setData('status', 1)->setData('store_id', 0)->setData('slide_group', $group_id)->save();
}
}
}
}
}
public function setCarousels($demoContent)
{
if (is_array($demoContent) && isset($demoContent['item']) && !empty($demoContent['item'])) {
if (isset($demoContent['item']['title'])) {
$demoContent = [$demoContent['item']];
} else {
$demoContent = $demoContent['item'];
}
/** @var Carousel $model */
$model = $this->_loadObject(Carousel::class);
/** @var Slide $model2 */
$model2 = $this->_loadObject(Slide::class);
foreach ($demoContent as $item) {
if (!array_key_exists('title', $item)) {
continue;
}
$carouselCollection = $model->getCollection()->addFieldToFilter('identifier', $item['identifier'])->load();
if (!empty($carouselCollection)) {
foreach ($carouselCollection as $carousel) {
$slidesCollection = $model2->getCollection()->addFieldToFilter('carousel', $carousel->getIdentifier())->load();
if (!empty($slidesCollection)) {
foreach ($slidesCollection as $slides) {
$slides->delete();
}
}
$carousel->delete();
}
}
$slides = [];
if (array_key_exists('slides', $item)) {
if (!empty($item['slides']) && array_key_exists('item', $item['slides'])) {
if (array_key_exists('carousel', $item['slides']['item'])) {
$slides[] = $item['slides']['item'];
} else {
$slides = $item['slides']['item'];
}
}
unset($item['slides']);
}
$current_model = $model->setData($item)->save();
if (!empty($slides)) {
foreach ($slides as $_item) {
$model2->setData($_item)->setData('store_id', 0)->save();
}
}
}
}
}
}
@@ -0,0 +1,42 @@
<?php
namespace Olegnax\Athlete2\Controller\Adminhtml\Import;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\View\Result\PageFactory;
class Index extends Action
{
const ADMIN_RESOURCE = 'Olegnax_Athlete2::import';
protected $resultPageFactory;
/**
* Constructor
*
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
/**
* Execute view action
*
* @return ResultInterface
*/
public function execute() {
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->prepend( (__( 'Athlete2 Demo Import' ) ) );
return $resultPage;
}
}