ltsecurityinc安防产品在线商城代码
This commit is contained in:
Executable
+92
@@ -0,0 +1,92 @@
|
||||
<?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\Ui\RelevanceRule\DataProvider\Product\Filter;
|
||||
|
||||
use Amasty\ElasticSearch\Model\RuleFactory as RelevanceRuleConditionsFactory;
|
||||
use Magento\Framework\Data\Collection;
|
||||
use Magento\Ui\DataProvider\AddFilterToCollectionInterface;
|
||||
use Laminas\Uri\Uri as LaminasUri;
|
||||
|
||||
class RuleConditionFilter implements AddFilterToCollectionInterface
|
||||
{
|
||||
public const MATCHED_FLAG = 'matched_products';
|
||||
public const DEFAULT_WEBSITE = 0;
|
||||
|
||||
/**
|
||||
* @var RelevanceRuleConditionsFactory
|
||||
*/
|
||||
private $relevanceRuleFactory;
|
||||
|
||||
/**
|
||||
* @var LaminasUri
|
||||
*/
|
||||
private $laminasUri;
|
||||
|
||||
public function __construct(
|
||||
RelevanceRuleConditionsFactory $relevanceRuleFactory,
|
||||
LaminasUri $laminasUri
|
||||
) {
|
||||
$this->relevanceRuleFactory = $relevanceRuleFactory;
|
||||
$this->laminasUri = $laminasUri;
|
||||
}
|
||||
|
||||
public function addFilter(Collection $collection, $field, $condition = null)
|
||||
{
|
||||
$conditions = $this->conditionArray($condition['eq'] ?? '');
|
||||
|
||||
if (empty($condition)) {
|
||||
$collection->addIdFilter(null);
|
||||
} else {
|
||||
$matchedIds = $this->getMatchedProductsIds($conditions);
|
||||
|
||||
if (!empty($matchedIds)) {
|
||||
$collection->addIdFilter($matchedIds);
|
||||
} else {
|
||||
$collection->getSelect()->where('null');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array[] $conditions
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
public function getMatchedProductsIds(array $conditions): array
|
||||
{
|
||||
$matchedProductIds = [];
|
||||
$rule = $this->relevanceRuleFactory->create();
|
||||
$rule->loadPost($conditions['rule'] ?? []);
|
||||
$website = $conditions['websites'] ?? [self::DEFAULT_WEBSITE];
|
||||
$rule->setWebsiteIds(join(',', $website));
|
||||
|
||||
if (!$this->isRuleEmpty($rule)) {
|
||||
$matchedProducts = $rule->getMatchingProductIds();
|
||||
$matchedProductIds = array_map('intval', array_unique(array_keys($matchedProducts)));
|
||||
}
|
||||
|
||||
return $matchedProductIds;
|
||||
}
|
||||
|
||||
private function isRuleEmpty(\Magento\CatalogRule\Model\Rule $rule): bool
|
||||
{
|
||||
$conditionsArray = $rule->getConditions()->asArray();
|
||||
|
||||
return empty($conditionsArray['conditions']);
|
||||
}
|
||||
|
||||
private function conditionArray(string $serializedConditions): array
|
||||
{
|
||||
$this->laminasUri->setQuery($serializedConditions);
|
||||
|
||||
return $this->laminasUri->getQueryAsArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Ui\RelevanceRule\Form;
|
||||
|
||||
use Amasty\ElasticSearch\Api\Data\RelevanceRuleInterface;
|
||||
use Amasty\ElasticSearch\Model\Source\RelevanceRuleModificationType;
|
||||
use Magento\Ui\DataProvider\AbstractDataProvider;
|
||||
|
||||
class DataProvider extends AbstractDataProvider
|
||||
{
|
||||
public function __construct(
|
||||
\Amasty\ElasticSearch\Model\ResourceModel\RelevanceRule\CollectionFactory $collectionFactory,
|
||||
$name,
|
||||
$primaryFieldName,
|
||||
$requestFieldName,
|
||||
array $meta = [],
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
|
||||
$this->collection = $collectionFactory->create();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
$result = [];
|
||||
|
||||
/** @var RelevanceRuleInterface $item */
|
||||
foreach ($this->collection as $item) {
|
||||
$result[$item->getId()] = $item->getData();
|
||||
if ($item->getMultiplier() < 0) {
|
||||
$result[$item->getId()][RelevanceRuleInterface::MULTIPLIER] = abs($item->getMultiplier());
|
||||
$result[$item->getId()]['modification_type'] = RelevanceRuleModificationType::DECREASE;
|
||||
} else {
|
||||
$result[$item->getId()]['modification_type'] = RelevanceRuleModificationType::INCREASE;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Ui\RelevanceRule\Listing\Column;
|
||||
|
||||
use Amasty\ElasticSearch\Api\Data\RelevanceRuleInterface;
|
||||
use Magento\Framework\View\Element\UiComponentFactory;
|
||||
use Magento\Ui\Component\Listing\Columns\Column;
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Magento\Framework\View\Element\UiComponent\ContextInterface;
|
||||
|
||||
class Actions extends Column
|
||||
{
|
||||
/**
|
||||
* @var UrlInterface
|
||||
*/
|
||||
private $urlBuilder;
|
||||
|
||||
public function __construct(
|
||||
UrlInterface $urlBuilder,
|
||||
ContextInterface $context,
|
||||
UiComponentFactory $uiComponentFactory,
|
||||
array $components = [],
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $uiComponentFactory, $components, $data);
|
||||
$this->urlBuilder = $urlBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function prepareDataSource(array $dataSource)
|
||||
{
|
||||
if (isset($dataSource['data']['items'])) {
|
||||
foreach ($dataSource['data']['items'] as &$item) {
|
||||
$item[$this->getData('name')] = [
|
||||
'edit' => [
|
||||
'href' => $this->urlBuilder->getUrl(
|
||||
'amasty_elastic/relevancerule/edit',
|
||||
[RelevanceRuleInterface::RULE_ID => $item[RelevanceRuleInterface::RULE_ID],]
|
||||
),
|
||||
'label' => __('Edit'),
|
||||
],
|
||||
'delete' => [
|
||||
'href' => $this->urlBuilder->getUrl(
|
||||
'amasty_elastic/relevancerule/delete',
|
||||
[RelevanceRuleInterface::RULE_ID => $item[RelevanceRuleInterface::RULE_ID]]
|
||||
),
|
||||
'label' => __('Delete'),
|
||||
'confirm' => [
|
||||
'title' => __('Are you sure you want to delete the rule?'),
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $dataSource;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Ui\RelevanceRule\Listing;
|
||||
|
||||
use Magento\Framework\Api\Search\SearchResultInterface;
|
||||
use Amasty\ElasticSearch\Api\Data\RelevanceRuleInterface;
|
||||
use Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider as MagentoDataProvider;
|
||||
|
||||
class DataProvider extends MagentoDataProvider
|
||||
{
|
||||
/**
|
||||
* @param SearchResultInterface $searchResult
|
||||
* @return array
|
||||
*/
|
||||
protected function searchResultToOutput(SearchResultInterface $searchResult)
|
||||
{
|
||||
$result = [
|
||||
'items' => [],
|
||||
'totalRecords' => $searchResult->getTotalCount(),
|
||||
];
|
||||
|
||||
/** @var RelevanceRuleInterface $item */
|
||||
foreach ($searchResult->getItems() as $item) {
|
||||
$result['items'][] = $item->getData();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user