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,160 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Config\Checker;
use Amasty\ElasticSearch\Exception\Missing404Exception;
use Amasty\ElasticSearch\Model\Client\ClientFactory;
use Amasty\ElasticSearch\Model\Client\ClientInterface;
use Amasty\ElasticSearch\Model\Config;
use Amasty\ElasticSearch\Model\GetAmastySearchEngines;
use Amasty\ElasticSearch\Model\Source\CustomAnalyzer;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filter\StripTags;
class Connection extends Action
{
public const ADMIN_RESOURCE = 'Amasty_ElasticSearch::config';
public const EXCEPTION_PATTERN = '/unknown analyzer/i';
public const IS_READ_ONLY_INDEX = 'amasty_is_read_only_flag';
public const CUSTOM_ANALYZER_TEST_INDEX_NAME = 'amasty_custom_analyzer_test_index';
/**
* @var Config
*/
private $config;
/**
* @var JsonFactory
*/
private $resultJsonFactory;
/**
* @var StripTags
*/
private $tagFilter;
/**
* @var GetAmastySearchEngines
*/
private $getAmastySearchEngines;
/**
* @var ClientFactory
*/
private $clientFactory;
public function __construct(
Context $context,
Config $config,
JsonFactory $resultJsonFactory,
StripTags $tagFilter,
GetAmastySearchEngines $getAmastySearchEngines,
ClientFactory $clientFactory
) {
parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
$this->tagFilter = $tagFilter;
$this->config = $config;
$this->getAmastySearchEngines = $getAmastySearchEngines;
$this->clientFactory = $clientFactory;
}
/**
* @return $this|ResponseInterface|ResultInterface
*/
public function execute()
{
$result = [
'success' => false,
'errorMessage' => '',
];
$options = $this->getRequest()->getParams();
try {
if (empty($options['engine'])
|| !in_array($options['engine'], $this->getAmastySearchEngines->execute(), true)
) {
throw new LocalizedException(
__('Test connection can be applied only for Amasty Elastic Search engine.')
);
}
$connectionData = $this->config->prepareConnectionData($options);
$client = $this->clientFactory->create($options['engine'], $connectionData);
$pingResult = $client->ping();
if ($pingResult) {
$indexPrefix = $connectionData['index'] ?? $this->config->getIndexPrefix();
$this->checkIsReadOnly($client, $indexPrefix);
if (isset($options['customAnalyzer'])
&& $options['customAnalyzer'] != CustomAnalyzer::DISABLED
) {
$customAnalyzerIndexName = $indexPrefix . '_' . self::CUSTOM_ANALYZER_TEST_INDEX_NAME;
$client->createIndex(
$customAnalyzerIndexName,
[
'analysis' => [
'analyzer' => [
'default' => ['type' => $options['customAnalyzer']]
]
]
]
);
try {
$client->deleteIndex($customAnalyzerIndexName);
} catch (Missing404Exception $e) {
;// do nothing
}
}
$result['success'] = $pingResult;
} else {
$result['errorMessage'] = __('It is impossible to perform the search.
Please make sure Elasticsearch Engine is installed on the server and configured properly.');
}
// @codingStandardsIgnoreLine
} catch (LocalizedException $e) {
$result['errorMessage'] = $e->getMessage();
} catch (\Exception $e) {
if (preg_match(self::EXCEPTION_PATTERN, $e->getMessage())) {
$result['errorMessage'] = __('To use custom analyzer you have to install matching plugin');
} else {
$result['errorMessage'] = $this->tagFilter->filter(__($e->getMessage()));
}
}
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->resultJsonFactory->create();
return $resultJson->setData($result);
}
private function checkIsReadOnly(ClientInterface $client, string $indexPrefix)
{
$testIndexName = $indexPrefix . '_' . self::IS_READ_ONLY_INDEX;
try {
$client->createIndex($testIndexName, []);
$client->deleteIndex($testIndexName);
} catch (\Exception $e) {
$this->messageManager->addErrorMessage(
__('Please check the state of read_only_allow_delete setting in Elasticsearch server configuration.')
);
}
}
}
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Product\Mui;
use Amasty\ElasticSearch\Controller\Adminhtml\RelevanceRule\AbstractRelevance;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Ui\Controller\Adminhtml\Index\Render as MagentoUiRenderController;
class Render extends MagentoUiRenderController implements HttpGetActionInterface, HttpPostActionInterface
{
public const ADMIN_RESOURCE = AbstractRelevance::ADMIN_RESOURCE;
}
@@ -0,0 +1,82 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\RelevanceRule;
use Amasty\ElasticSearch\Model\RelevanceRuleFactory;
use Amasty\ElasticSearch\Model\RelevanceRuleRepository;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\ForwardFactory;
use Magento\Framework\Registry;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Store\Model\StoreManagerInterface;
abstract class AbstractRelevance extends Action
{
public const ADMIN_RESOURCE = 'Amasty_ElasticSearch::relevance_rules';
public const CURRENT_RULE = 'amasty_elastic_relevance_rule';
/**
* @var ForwardFactory
*/
protected $resultForwardFactory;
/**
* @var RelevanceRuleRepository
*/
protected $ruleRepository;
/**
* @var RelevanceRuleFactory
*/
protected $ruleFactory;
/**
* @var Registry
*/
protected $registry;
/**
* @var TimezoneInterface
*/
protected $timezone;
/**
* @var StoreManagerInterface
*/
protected $storeManager;
public function __construct(
Context $context,
ForwardFactory $resultForwardFactory,
RelevanceRuleRepository $ruleRepository,
RelevanceRuleFactory $ruleFactory,
Registry $registry,
TimezoneInterface $timezone,
StoreManagerInterface $storeManager
) {
parent::__construct($context);
$this->resultForwardFactory = $resultForwardFactory;
$this->ruleRepository = $ruleRepository;
$this->ruleFactory = $ruleFactory;
$this->registry = $registry;
$this->timezone = $timezone;
$this->storeManager = $storeManager;
}
/**
* @param \Magento\Backend\Model\View\Result\Page $resultPage
* @return \Magento\Backend\Model\View\Result\Page
*/
protected function initPage($resultPage)
{
$resultPage->setActiveMenu('Amasty_ElasticSearch::Amasty_ElasticSearch');
$resultPage->getConfig()->getTitle()->prepend(__('Manage Relevance Rules'));
return $resultPage;
}
}
@@ -0,0 +1,77 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\RelevanceRule;
use Amasty\ElasticSearch\Api\Data\RelevanceRuleInterface;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Backend\App\Action;
class Delete extends Action
{
public const ADMIN_RESOURCE = 'Amasty_ElasticSearch::relevance_rules';
/**
* @var \Amasty\ElasticSearch\Model\RelevanceRuleRepository
*/
private $relevanceRuleRepository;
/**
* @var \Amasty\ElasticSearch\Model\ResourceModel\RelevanceRule\CollectionFactory
*/
private $collectionFactory;
/**
* @var Filter
*/
private $filter;
public function __construct(
Action\Context $context,
\Amasty\ElasticSearch\Model\RelevanceRuleRepository $relevanceRuleRepository,
\Amasty\ElasticSearch\Model\ResourceModel\RelevanceRule\CollectionFactory $collectionFactory,
Filter $filter
) {
parent::__construct($context);
$this->relevanceRuleRepository = $relevanceRuleRepository;
$this->collectionFactory = $collectionFactory;
$this->filter = $filter;
}
/**
* @inheritdoc
*/
public function execute()
{
if ($this->getRequest()->getParam(Filter::SELECTED_PARAM)
|| $this->getRequest()->getParam(Filter::EXCLUDED_PARAM)
) {
$ids = $this->filter->getCollection($this->collectionFactory->create())->getAllIds();
} else {
$ids = (array)$this->getRequest()->getParam(RelevanceRuleInterface::RULE_ID);
}
if ($ids) {
foreach ($ids as $id) {
try {
$this->relevanceRuleRepository->deleteById($id);
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
}
$this->messageManager->addSuccessMessage(
__('%1 rule(s) was successfully removed', count($ids))
);
} else {
$this->messageManager->addErrorMessage(__('Please select Relevance Rule(s)'));
}
return $this->resultRedirectFactory->create()->setPath('*/*/');
}
}
@@ -0,0 +1,41 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\RelevanceRule;
use Amasty\ElasticSearch\Api\Data\RelevanceRuleInterface;
use Magento\Framework\Controller\ResultFactory;
class Edit extends AbstractRelevance
{
/**
* @inheritdoc
*/
public function execute()
{
$ruleId = (int)$this->getRequest()->getParam(RelevanceRuleInterface::RULE_ID);
try {
if ($ruleId) {
/** @var \Amasty\ElasticSearch\Model\RelevanceRule $model */
$model = $this->ruleRepository->get($ruleId);
} else {
$model = $this->ruleFactory->create();
}
$this->registry->register(self::CURRENT_RULE, $model);
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
$this->messageManager->addErrorMessage(__('This Relevance Rule is no longer exists.'));
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/');
}
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$text = $model->getId() ? $model->getTitle() : __('New Relevance Rule');
$this->initPage($resultPage)->getConfig()->getTitle()->prepend($text);
return $resultPage;
}
}
@@ -0,0 +1,22 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\RelevanceRule;
use Magento\Framework\Controller\ResultFactory;
class Index extends AbstractRelevance
{
/**
* @inheritdoc
*/
public function execute()
{
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
return $this->initPage($resultPage);
}
}
@@ -0,0 +1,20 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\RelevanceRule;
class NewAction extends AbstractRelevance
{
/**
* @inheritdoc
*/
public function execute()
{
$resultForward = $this->resultForwardFactory->create();
return $resultForward->forward('edit');
}
}
@@ -0,0 +1,42 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\RelevanceRule;
class NewConditionHtml extends AbstractRelevance
{
/**
* @inheritdoc
*/
public function execute()
{
$ruleId = $this->getRequest()->getParam('id');
$formName = $this->getRequest()->getParam('form_namespace');
$typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
$type = $typeArr[0];
$model = $this->_objectManager->create($type)
->setId($ruleId)
->setType($type)
->setRule($this->_objectManager->create(\Magento\CatalogRule\Model\Rule::class))
->setPrefix('conditions');
if (!empty($typeArr[1])) {
$model->setAttribute($typeArr[1]);
}
if ($model instanceof \Magento\Rule\Model\Condition\AbstractCondition) {
$model->setJsFormObject($this->getRequest()->getParam('form'));
$model->setFormName($formName);
$html = $model->asHtmlRecursive();
} else {
$html = '';
}
$this->getResponse()->setBody($html);
}
}
@@ -0,0 +1,115 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\RelevanceRule;
use Amasty\ElasticSearch\Api\Data\RelevanceRuleInterface;
use Amasty\ElasticSearch\Model\Source\RelevanceRuleModificationType;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Stdlib\DateTime;
class Save extends AbstractRelevance
{
/**
* @inheritdoc
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$rule = (int)$this->getRequest()->getParam(RelevanceRuleInterface::RULE_ID);
$data = $this->getRequest()->getPostValue();
try {
if ($rule) {
/** @var \Amasty\ElasticSearch\Model\RelevanceRule $model */
$model = $this->ruleRepository->get($rule);
} else {
$model = $this->ruleFactory->create();
}
$data = $this->prepareData($data, $model);
$model->setData($data);
$this->ruleRepository->save($model);
$this->messageManager->addSuccessMessage(__('You have saved the Rule.'));
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
$this->messageManager->addErrorMessage(
__('Relevance Rule with the same term already exists in an associated store.')
);
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
$this->messageManager->addErrorMessage(__('This Relevance Rule no longer exists.'));
$resultRedirect = $this->resultRedirectFactory->create();
}
return $resultRedirect->setPath('*/*/');
}
/**
* @param array $data
* @param \Amasty\ElasticSearch\Model\RelevanceRule $rule
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function prepareData(array $data, \Amasty\ElasticSearch\Model\RelevanceRule $rule)
{
if (empty($data[RelevanceRuleInterface::RULE_ID])) {
$data[RelevanceRuleInterface::RULE_ID] = null;
}
$data[RelevanceRuleInterface::MULTIPLIER] = max(
min($data[RelevanceRuleInterface::MULTIPLIER], 100),
1
);
if ($data['modification_type'] === RelevanceRuleModificationType::DECREASE) {
$data[RelevanceRuleInterface::MULTIPLIER] *= -1;
}
if (isset($data['rule']) && isset($data['rule']['conditions'])) {
$conditions = $data['rule']['conditions'];
unset($data['rule']);
$data[RelevanceRuleInterface::CONDITIONS] = $rule->getConditionsSerialized($conditions);
}
$data[RelevanceRuleInterface::FROM_DATE] = $this->prepareDates($data[RelevanceRuleInterface::FROM_DATE]);
$data[RelevanceRuleInterface::TO_DATE] = $this->prepareDates($data[RelevanceRuleInterface::TO_DATE]);
$data = $this->prepareRuleStatus($data);
return $data;
}
private function prepareDates(string $date): ?string
{
return $date ? $this->timezone->date($date)->format(DateTime::DATE_PHP_FORMAT) : '';
}
private function prepareRuleStatus(array $data): array
{
$websiteId = $data[RelevanceRuleInterface::WEBSITE_ID] ?? false;
if ($websiteId) {
try {
$website = $this->storeManager->getWebsite($websiteId);
if (!empty($data[RelevanceRuleInterface::FROM_DATE])
&& !empty($data[RelevanceRuleInterface::TO_DATE])
&& !$this->timezone->isScopeDateInInterval(
$website,
$data[RelevanceRuleInterface::FROM_DATE],
$data[RelevanceRuleInterface::TO_DATE]
)
) {
$data[RelevanceRuleInterface::IS_ENABLED] = false;
}
} catch (LocalizedException $e) {
;// do nothing
}
}
return $data;
}
}
@@ -0,0 +1,72 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Stopword;
use Magento\Backend\App\Action\Context;
use Magento\Backend\App\Action;
/**
* Class AbstractStopWord
*/
abstract class AbstractStopWord extends Action
{
public const ADMIN_RESOURCE = 'Amasty_ElasticSearch::stop_words';
/**
* @var \Magento\Backend\Model\View\Result\ForwardFactory
*/
protected $resultForwardFactory;
/**
* @var \Amasty\ElasticSearch\Model\StopWordRepository
*/
protected $stopWordRepository;
/**
* @var \Amasty\ElasticSearch\Model\StopWordFactory
*/
protected $stopWordFactory;
/**
* @var \Magento\Framework\Registry
*/
protected $registry;
/**
* @var \Magento\Framework\Indexer\IndexerRegistry
*/
protected $indexerRegistry;
public function __construct(
Context $context,
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
\Amasty\ElasticSearch\Model\StopWordRepository $ruleRepository,
\Amasty\ElasticSearch\Model\StopWordFactory $stopWordFactory,
\Magento\Framework\Registry $registry,
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
) {
parent::__construct($context);
$this->resultForwardFactory = $resultForwardFactory;
$this->stopWordRepository = $ruleRepository;
$this->stopWordFactory = $stopWordFactory;
$this->registry = $registry;
$this->indexerRegistry = $indexerRegistry;
}
/**
* @param \Magento\Backend\Model\View\Result\Page $resultPage
* @return \Magento\Backend\Model\View\Result\Page
*/
protected function initPage($resultPage)
{
$resultPage->setActiveMenu('Amasty_ElasticSearch::Amasty_ElasticSearch');
$resultPage->getConfig()->getTitle()->prepend(__('Manage Stop Words'));
return $resultPage;
}
}
@@ -0,0 +1,89 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Stopword;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Backend\App\Action;
use Amasty\ElasticSearch\Api\Data\StopWordInterface;
class Delete extends Action
{
public const ADMIN_RESOURCE = 'Amasty_ElasticSearch::stop_words';
/**
* @var \Amasty\ElasticSearch\Model\StopWordRepository
*/
private $stopWordRepository;
/**
* @var \Amasty\ElasticSearch\Model\ResourceModel\StopWord\CollectionFactory
*/
private $collectionFactory;
/**
* @var Filter
*/
private $filter;
/**
* @var \Magento\Framework\Indexer\IndexerRegistry
*/
private $indexerRegistry;
public function __construct(
Action\Context $context,
\Amasty\ElasticSearch\Model\StopWordRepository $stopWordRepository,
\Amasty\ElasticSearch\Model\ResourceModel\StopWord\CollectionFactory $collectionFactory,
Filter $filter,
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
) {
parent::__construct($context);
$this->stopWordRepository = $stopWordRepository;
$this->collectionFactory = $collectionFactory;
$this->filter = $filter;
$this->indexerRegistry = $indexerRegistry;
}
/**
* {@inheritdoc}
*/
public function execute()
{
$idsToRemove = [];
$ids = $this->getRequest()->getParam(StopWordInterface::STOP_WORD_ID);
if ($ids) {
$idsToRemove = [$ids];
}
if ($this->getRequest()->getParam(Filter::SELECTED_PARAM)
|| $this->getRequest()->getParam(Filter::EXCLUDED_PARAM)
) {
$idsToRemove = $this->filter->getCollection($this->collectionFactory->create())->getAllIds();
}
if ($idsToRemove) {
foreach ($idsToRemove as $id) {
try {
$this->stopWordRepository->deleteById($id);
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
}
$this->messageManager->addSuccessMessage(
__('%1 stop word(s) was successfully removed', count($idsToRemove))
);
$this->indexerRegistry->get(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID)->invalidate();
} else {
$this->messageManager->addErrorMessage(__('Please select Stop Word(s)'));
}
return $this->resultRedirectFactory->create()->setPath('*/*/');
}
}
@@ -0,0 +1,129 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Stopword;
use Magento\Backend\App\Action;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Amasty\ElasticSearch\Api\Data\StopWordInterface;
class DoImport extends Action
{
public const ADMIN_RESOURCE = 'Amasty_ElasticSearch::stop_words';
public const MEDIA_PATH = 'amasty/elastic/import';
public const FILE_WAS_NOT_UPLOADED_CODE_ERROR = '666';
/**
* @var \Magento\Framework\Filesystem
*/
private $filesystem;
/**
* @var \Magento\Framework\Filesystem\Io\File
*/
private $ioFile;
/**
* @var \Magento\MediaStorage\Model\File\UploaderFactory
*/
private $fileUploaderFactory;
/**
* @var \Amasty\ElasticSearch\Model\StopWordRepository
*/
private $stopWordRepository;
/**
* @var \Amasty\ElasticSearch\Model\StopWordFactory
*/
private $stopWordFactory;
/**
* @var \Magento\Framework\Indexer\IndexerRegistry
*/
private $indexerRegistry;
public function __construct(
Action\Context $context,
\Magento\Framework\Filesystem $filesystem,
\Magento\Framework\Filesystem\Io\File $ioFile,
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
\Amasty\ElasticSearch\Model\StopWordRepository $stopWordRepository,
\Amasty\ElasticSearch\Model\StopWordFactory $stopWordFactory,
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
) {
parent::__construct($context);
$this->filesystem = $filesystem;
$this->ioFile = $ioFile;
$this->fileUploaderFactory = $fileUploaderFactory;
$this->stopWordRepository = $stopWordRepository;
$this->stopWordFactory = $stopWordFactory;
$this->indexerRegistry = $indexerRegistry;
}
/**
* {@inheritdoc}
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$data = $this->getRequest()->getPostValue();
$storeId = (int) $this->getRequest()->getParam(StopWordInterface::STORE_ID);
if ($data) {
try {
//upload images
$path = $this->filesystem->getDirectoryRead(
DirectoryList::MEDIA
)->getAbsolutePath(
self::MEDIA_PATH
);
$this->ioFile->checkAndCreateFolder($path);
try {
/** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
$uploader = $this->fileUploaderFactory->create(['fileId' => 'amasty_elastic_file_upload']);
$uploader->setAllowedExtensions(['csv']);
$uploader->setAllowRenameFiles(true);
$result = $uploader->save($path);
} catch (\Exception $ex) {
$this->messageManager->addErrorMessage(__($ex->getMessage()));
return $resultRedirect->setPath('*/*/import');
}
if (isset($result['file']) && $this->ioFile->fileExists($path . '/' . $result['file'])) {
$file = $path . '/' . $result['file'];
} else {
$this->messageManager->addErrorMessage(__('Something wend wrong during saving file'));
return $resultRedirect->setPath('*/*/import');
}
$count = $this->stopWordRepository->importStopWords($file, $storeId);
$this->ioFile->rm($file);
} catch (LocalizedException $ex) {
$this->messageManager->addErrorMessage($ex->getMessage());
return $resultRedirect->setPath('*/*/import');
} catch (\Exception $ex) {
$this->messageManager->addErrorMessage(__('Something went wrong. Please try again'));
return $resultRedirect->setPath('*/*/import');
}
if (isset($count)) {
$this->messageManager->addSuccessMessage(__('%1 Stop Word(s) has been imported', $count));
$this->indexerRegistry->get(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID)->invalidate();
} else {
$this->messageManager->addWarningMessage('Imported file was empty or all terms are already exist');
}
} else {
$this->messageManager->addErrorMessage('There are no data to import');
return $resultRedirect->setPath('*/*/import');
}
return $resultRedirect->setPath('*/*/');
}
}
@@ -0,0 +1,44 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Stopword;
use Magento\Framework\Controller\ResultFactory;
use Amasty\ElasticSearch\Api\Data\StopWordInterface;
class Edit extends AbstractStopWord
{
/**
* {@inheritdoc}
*/
public function execute()
{
$stopWordId = (int)$this->getRequest()->getParam(StopWordInterface::STOP_WORD_ID);
try {
if ($stopWordId) {
/** @var \Amasty\ElasticSearch\Model\StopWord $model */
$model = $this->stopWordRepository->getById($stopWordId);
} else {
$model = $this->stopWordFactory->create();
}
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
$this->messageManager->addErrorMessage(__('This Stop Word no longer exists.'));
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/');
}
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$text = $model->getId() ? __('Edit Stop Word "%1"', $model->getTerm()) : __('New Stop Word');
$this->initPage($resultPage)->getConfig()->getTitle()->prepend($text);
return $resultPage;
}
}
@@ -0,0 +1,26 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Stopword;
use Magento\Framework\Controller\ResultFactory;
class Import extends AbstractStopWord
{
/**
* {@inheritdoc}
*/
public function execute()
{
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$this->initPage($resultPage)
->getConfig()->getTitle()->prepend(__('Import Stop Words'));
return $resultPage;
}
}
@@ -0,0 +1,26 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Stopword;
use Magento\Framework\Controller\ResultFactory;
class Index extends AbstractStopWord
{
/**
* {@inheritdoc}
*/
public function execute()
{
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$this->initPage($resultPage)
->getConfig()->getTitle()->prepend(__('Manage Stop Words'));
return $resultPage;
}
}
@@ -0,0 +1,18 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Stopword;
class NewAction extends AbstractStopWord
{
public function execute()
{
/** @var \Magento\Backend\Model\View\Result\Forward $resultForward */
$resultForward = $this->resultForwardFactory->create();
return $resultForward->forward('edit');
}
}
@@ -0,0 +1,46 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Stopword;
use Amasty\ElasticSearch\Api\Data\StopWordInterface;
class Save extends AbstractStopWord
{
/**
* {@inheritdoc}
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$stopWordId = (int)$this->getRequest()->getParam(StopWordInterface::STOP_WORD_ID);
try {
if ($stopWordId) {
/** @var \Amasty\ElasticSearch\Model\StopWord $model */
$model = $this->stopWordRepository->getById($stopWordId);
} else {
$model = $this->stopWordFactory->create();
}
$model->setTerm($this->getRequest()->getParam(StopWordInterface::TERM))
->setStoreId($this->getRequest()->getParam(StopWordInterface::STORE_ID));
$this->stopWordRepository->save($model);
$this->messageManager->addSuccessMessage(__('You have saved the Stop Word.'));
$this->indexerRegistry->get(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID)->invalidate();
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
$this->messageManager->addErrorMessage(
__('A Stop Word with the same term already exists in an associated store.')
);
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
$this->messageManager->addErrorMessage(__('This Stop Word no longer exists.'));
$resultRedirect = $this->resultRedirectFactory->create();
}
return $resultRedirect->setPath('*/*/');
}
}
@@ -0,0 +1,72 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Synonym;
use Magento\Backend\App\Action\Context;
use Magento\Backend\App\Action;
/**
* Class AbstractSynonym
*/
abstract class AbstractSynonym extends Action
{
public const ADMIN_RESOURCE = 'Amasty_ElasticSearch::synonym';
/**
* @var \Magento\Backend\Model\View\Result\ForwardFactory
*/
protected $resultForwardFactory;
/**
* @var \Amasty\ElasticSearch\Model\SynonymRepository
*/
protected $synonymRepository;
/**
* @var \Amasty\ElasticSearch\Model\SynonymFactory
*/
protected $synonymFactory;
/**
* @var \Magento\Framework\Registry
*/
protected $registry;
/**
* @var \Magento\Framework\Indexer\IndexerRegistry
*/
protected $indexerRegistry;
public function __construct(
Context $context,
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
\Amasty\ElasticSearch\Model\SynonymRepository $synonymRepository,
\Amasty\ElasticSearch\Model\SynonymFactory $synonymFactory,
\Magento\Framework\Registry $registry,
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
) {
parent::__construct($context);
$this->resultForwardFactory = $resultForwardFactory;
$this->synonymRepository = $synonymRepository;
$this->synonymFactory = $synonymFactory;
$this->registry = $registry;
$this->indexerRegistry = $indexerRegistry;
}
/**
* @param \Magento\Backend\Model\View\Result\Page $resultPage
* @return \Magento\Backend\Model\View\Result\Page
*/
protected function initPage($resultPage)
{
$resultPage->setActiveMenu('Amasty_ElasticSearch::Amasty_ElasticSearch');
$resultPage->getConfig()->getTitle()->prepend(__('Manage Synonyms'));
return $resultPage;
}
}
@@ -0,0 +1,89 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Synonym;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Backend\App\Action;
use Amasty\ElasticSearch\Api\Data\SynonymInterface;
class Delete extends Action
{
public const ADMIN_RESOURCE = 'Amasty_ElasticSearch::synonym';
/**
* @var \Amasty\ElasticSearch\Model\SynonymRepository
*/
private $synonymRepository;
/**
* @var \Amasty\ElasticSearch\Model\ResourceModel\Synonym\CollectionFactory
*/
private $collectionFactory;
/**
* @var Filter
*/
private $filter;
/**
* @var \Magento\Framework\Indexer\IndexerRegistry
*/
private $indexerRegistry;
public function __construct(
Action\Context $context,
\Amasty\ElasticSearch\Model\SynonymRepository $synonymRepository,
\Amasty\ElasticSearch\Model\ResourceModel\Synonym\CollectionFactory $collectionFactory,
Filter $filter,
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
) {
parent::__construct($context);
$this->synonymRepository = $synonymRepository;
$this->collectionFactory = $collectionFactory;
$this->filter = $filter;
$this->indexerRegistry = $indexerRegistry;
}
/**
* {@inheritdoc}
*/
public function execute()
{
$idsToRemove = [];
$ids = $this->getRequest()->getParam(SynonymInterface::SYNONYM_ID);
if ($ids) {
$idsToRemove = [$ids];
}
if ($this->getRequest()->getParam(Filter::SELECTED_PARAM)
|| $this->getRequest()->getParam(Filter::EXCLUDED_PARAM)
) {
$idsToRemove = $this->filter->getCollection($this->collectionFactory->create())->getAllIds();
}
if ($idsToRemove) {
foreach ($idsToRemove as $id) {
try {
$this->synonymRepository->deleteById($id);
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
}
$this->messageManager->addSuccessMessage(
__('%1 synonym(s) was successfully removed', count($idsToRemove))
);
$this->indexerRegistry->get(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID)->invalidate();
} else {
$this->messageManager->addErrorMessage(__('Please select Synonym(s)'));
}
return $this->resultRedirectFactory->create()->setPath('*/*/');
}
}
@@ -0,0 +1,173 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Synonym;
use Magento\Backend\App\Action;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\LocalizedException;
use Amasty\ElasticSearch\Api\Data\SynonymInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Amasty\ElasticSearch\Model\Indexer\Data\DataMapperResolver;
class DoImport extends Action
{
public const ADMIN_RESOURCE = 'Amasty_ElasticSearch::synonym';
public const MEDIA_PATH = 'amasty/elastic/import';
public const FILE_WAS_NOT_UPLOADED_CODE_ERROR = '666';
/**
* @var \Magento\Framework\Filesystem
*/
private $filesystem;
/**
* @var \Magento\Framework\Filesystem\Io\File
*/
private $ioFile;
/**
* @var \Magento\MediaStorage\Model\File\UploaderFactory
*/
private $fileUploaderFactory;
/**
* @var \Amasty\ElasticSearch\Model\SynonymRepository
*/
private $synonymRepository;
/**
* @var \Amasty\ElasticSearch\Model\SynonymFactory
*/
private $synonymFactory;
/**
* @var \Magento\Framework\File\Csv
*/
private $csv;
/**
* @var \Magento\Indexer\Model\IndexerFactory
*/
protected $indexerFactory;
public function __construct(
Action\Context $context,
\Magento\Framework\Filesystem $filesystem,
\Magento\Framework\Filesystem\Io\File $ioFile,
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
\Amasty\ElasticSearch\Model\SynonymRepository $synonymRepository,
\Amasty\ElasticSearch\Model\SynonymFactory $synonymFactory,
\Magento\Framework\File\Csv $csv,
\Magento\Indexer\Model\IndexerFactory $indexerFactory
) {
parent::__construct($context);
$this->filesystem = $filesystem;
$this->ioFile = $ioFile;
$this->fileUploaderFactory = $fileUploaderFactory;
$this->synonymRepository = $synonymRepository;
$this->synonymFactory = $synonymFactory;
$this->csv = $csv;
$this->indexerFactory = $indexerFactory;
}
/**
* {@inheritdoc}
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$data = $this->getRequest()->getPostValue();
$storeId = (int) $this->getRequest()->getParam(SynonymInterface::STORE_ID);
$count = 0;
if ($data) {
try {
$file = $this->uploadFile();
$csvData = $this->csv->getData($file);
foreach ($csvData as $data) {
$term = ($data && is_array($data)) ? implode(',', $data) : null;
if ($term) {
try {
$model = $this->synonymFactory->create()
->setStoreId($storeId)
->setTerm($term);
$this->synonymRepository->save($model);
} catch (AlreadyExistsException $ex) {
continue;
} catch (CouldNotSaveException $ex) {
continue;
}
$count++;
}
}
$this->invalidateIndex();
$this->ioFile->rm($file);
} catch (LocalizedException $ex) {
$this->messageManager->addErrorMessage($ex->getMessage());
return $resultRedirect->setPath('*/*/import');
} catch (\Exception $ex) {
$this->messageManager->addErrorMessage(__('Something went wrong. Please try again'));
return $resultRedirect->setPath('*/*/import');
}
if ($count > 0) {
$this->messageManager->addSuccessMessage(__('%1 Synonym(s) has been imported', $count));
} else {
$this->messageManager->addWarningMessage('Imported file was empty or all terms are already exist');
}
} else {
$this->messageManager->addErrorMessage('There are no data to import');
return $resultRedirect->setPath('*/*/import');
}
return $resultRedirect->setPath('*/*/');
}
/**
* @return string
* @throws LocalizedException
* @throws \Exception
*/
private function uploadFile()
{
$path = $this->filesystem->getDirectoryRead(
DirectoryList::MEDIA
)->getAbsolutePath(
self::MEDIA_PATH
);
$this->ioFile->checkAndCreateFolder($path);
try {
/** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
$uploader = $this->fileUploaderFactory->create(['fileId' => 'amasty_elastic_file_upload']);
$uploader->setAllowedExtensions(['csv']);
$uploader->setAllowRenameFiles(true);
$result = $uploader->save($path);
} catch (\Exception $ex) {
throw new LocalizedException(__($ex->getMessage()));
}
if (isset($result['file']) && $this->ioFile->fileExists($path . '/' . $result['file'])) {
$file = $path . '/' . $result['file'];
} else {
throw new LocalizedException(__('Something wend wrong during saving file'));
}
return $file;
}
private function invalidateIndex()
{
$indexer = $this->indexerFactory->create()->load(DataMapperResolver::DEFAULT_DATA_INDEXER);
$indexer->invalidate();
}
}
@@ -0,0 +1,43 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Synonym;
use Magento\Framework\Controller\ResultFactory;
use Amasty\ElasticSearch\Api\Data\SynonymInterface;
class Edit extends AbstractSynonym
{
/**
* {@inheritdoc}
*/
public function execute()
{
$stopWordId = (int)$this->getRequest()->getParam(SynonymInterface::SYNONYM_ID);
try {
if ($stopWordId) {
/** @var \Amasty\ElasticSearch\Model\Synonym $model */
$model = $this->synonymRepository->getById($stopWordId);
} else {
$model = $this->synonymFactory->create();
}
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
$this->messageManager->addErrorMessage(__('This Synonym no longer exists.'));
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/');
}
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$text = $model->getId() ? __('Edit Synonym "%1"', $model->getTerm()) : __('New Synonym');
$this->initPage($resultPage)->getConfig()->getTitle()->prepend($text);
return $resultPage;
}
}
@@ -0,0 +1,26 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Synonym;
use Magento\Framework\Controller\ResultFactory;
class Import extends AbstractSynonym
{
/**
* {@inheritdoc}
*/
public function execute()
{
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$this->initPage($resultPage)
->getConfig()->getTitle()->prepend(__('Import Synonyms'));
return $resultPage;
}
}
@@ -0,0 +1,26 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Synonym;
use Magento\Framework\Controller\ResultFactory;
class Index extends AbstractSynonym
{
/**
* {@inheritdoc}
*/
public function execute()
{
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$this->initPage($resultPage)
->getConfig()->getTitle()->prepend(__('Manage Synonyms'));
return $resultPage;
}
}
@@ -0,0 +1,18 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Synonym;
class NewAction extends AbstractSynonym
{
public function execute()
{
/** @var \Magento\Backend\Model\View\Result\Forward $resultForward */
$resultForward = $this->resultForwardFactory->create();
return $resultForward->forward('edit');
}
}
@@ -0,0 +1,55 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Controller\Adminhtml\Synonym;
use Amasty\ElasticSearch\Api\Data\SynonymInterface;
use Magento\Framework\Exception\NoSuchEntityException;
class Save extends AbstractSynonym
{
/**
* {@inheritdoc}
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$stopWordId = (int)$this->getRequest()->getParam(SynonymInterface::SYNONYM_ID);
try {
if ($stopWordId) {
/** @var \Amasty\ElasticSearch\Model\Synonym $model */
$model = $this->synonymRepository->getById($stopWordId);
} else {
$model = $this->synonymFactory->create();
}
$model->setTerm($this->getRequest()->getParam(SynonymInterface::TERM))
->setStoreId($this->getRequest()->getParam(SynonymInterface::STORE_ID));
$this->synonymRepository->save($model);
$this->messageManager->addSuccessMessage(__('You have saved the Synonym.'));
$this->indexerRegistry->get(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID)->invalidate();
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
$this->messageManager->addErrorMessage(
__('A Synonym with the same term already exists in an associated store.')
);
} catch (NoSuchEntityException $exception) {
$this->messageManager->addErrorMessage(__('This Synonym no longer exists.'));
$resultRedirect = $this->resultRedirectFactory->create();
}
$matches = null;
preg_match('/(\+|-|\/|&&|\|\||!|\(|\)|\{|}|\[|]|\^|"|~|\*|\?|:|\\\)/', $model->getTerm(), $matches);
if (count($matches)) {
$this->messageManager->addNoticeMessage(__(
'Synonyms with special symbols only works on elasticsearch engine v6.0 or higher.'
));
}
return $resultRedirect->setPath('*/*/');
}
}