ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -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\Model\Config\Backend;
|
||||
|
||||
class Debug extends \Magento\Framework\App\Config\Value
|
||||
{
|
||||
public const LIFE_TIME = 300;
|
||||
|
||||
/**
|
||||
* @return $this|\Magento\Framework\App\Config\Value
|
||||
*/
|
||||
public function afterLoad()
|
||||
{
|
||||
if ($this->getValue()) {
|
||||
$this->setValue($this->isActive($this->getValue()));
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float $value
|
||||
* @return bool
|
||||
*/
|
||||
public function isActive($value)
|
||||
{
|
||||
return (microtime(true) - $value) < self::LIFE_TIME;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this|\Magento\Framework\App\Config\Value
|
||||
*/
|
||||
public function beforeSave()
|
||||
{
|
||||
if ($this->getValue()) {
|
||||
$this->setValue(microtime(true));
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Amasty Team
|
||||
* @copyright Copyright (c) Amasty (https://www.amasty.com)
|
||||
* @package Elastic Search Base for Magento 2
|
||||
*/
|
||||
|
||||
namespace Amasty\ElasticSearch\Model\Config;
|
||||
|
||||
class QuerySettings extends \Magento\Framework\App\Config\Value
|
||||
{
|
||||
/**
|
||||
* Keys for array configuration usage
|
||||
*/
|
||||
public const ATTRIBUTE = 'attribute';
|
||||
public const WILDCARD = 'wildcard';
|
||||
public const SPELLING = 'spelling';
|
||||
public const COMBINING = 'combining';
|
||||
|
||||
/**
|
||||
* Keys for DB storage
|
||||
*/
|
||||
public const KEY_WILDCARD = 'w';
|
||||
public const KEY_SPELLING = 's';
|
||||
public const KEY_COMBINING = 'c';
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Serialize\Serializer\Json
|
||||
*/
|
||||
private $serializer;
|
||||
|
||||
/**
|
||||
* @var \Amasty\ElasticSearch\Model\Config\QuerySettingsProcessor
|
||||
*/
|
||||
private $processor;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
parent::_construct();
|
||||
$this->serializer = $this->getData('serializer');
|
||||
$this->processor = $this->getData('processor');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function _afterLoad()
|
||||
{
|
||||
$value = $this->getValue();
|
||||
$value = $this->processor->parseLoad($value);
|
||||
$value = $this->encodeArrayFieldValue($value);
|
||||
$this->setValue($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function beforeSave()
|
||||
{
|
||||
$value = $this->getValue();
|
||||
$value = $this->makeStorableArrayFieldValue($value);
|
||||
if ($value) {
|
||||
$this->setValue($value);
|
||||
} else {
|
||||
$this->_dataSaveAllowed = false;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make value ready for store
|
||||
*
|
||||
* @param string|array $value
|
||||
* @return string
|
||||
*/
|
||||
private function makeStorableArrayFieldValue($value)
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
$value = $this->unserializeValue($value);
|
||||
}
|
||||
$value = $this->decodeArrayFieldValue($value);
|
||||
$value = $this->serializeValue($value);
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare configuration array of all searchable attributes for frontend
|
||||
*
|
||||
* @param array $value
|
||||
* @return array
|
||||
*/
|
||||
private function encodeArrayFieldValue(array $value)
|
||||
{
|
||||
$result = [];
|
||||
$attributeCodes = $this->processor->getSearchableAttributeCodes();
|
||||
foreach ($value as $attributeCode => $data) {
|
||||
if (in_array($attributeCode, $attributeCodes, true)) {
|
||||
$result[$attributeCode] = [
|
||||
self::ATTRIBUTE => $attributeCode,
|
||||
self::WILDCARD => $data[self::WILDCARD],
|
||||
self::SPELLING => $data[self::SPELLING],
|
||||
self::COMBINING => $data[self::COMBINING]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($attributeCodes as $code) {
|
||||
if (!isset($result[$code])) {
|
||||
$result[$code] = [
|
||||
self::ATTRIBUTE => $code,
|
||||
self::WILDCARD => 0,
|
||||
self::SPELLING => 0,
|
||||
self::COMBINING => 0
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a value from a storable representation
|
||||
*
|
||||
* @param int|float|string $value
|
||||
* @return array
|
||||
*/
|
||||
private function unserializeValue($value)
|
||||
{
|
||||
$result = [];
|
||||
if (is_string($value) && !empty($value)) {
|
||||
$result = $this->serializer->unserialize($value);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode value from used in \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
|
||||
*
|
||||
* @param array $value
|
||||
* @return array
|
||||
*/
|
||||
private function decodeArrayFieldValue(array $value)
|
||||
{
|
||||
unset($value['__empty']);
|
||||
foreach ($value as $attribute => $data) {
|
||||
if (!is_array($data) || !array_key_exists(self::COMBINING, $data)) {
|
||||
unset($value[$attribute]);
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a storable representation of a value
|
||||
*
|
||||
* @param int|float|string|array $value
|
||||
* @return string
|
||||
*/
|
||||
private function serializeValue($value): string
|
||||
{
|
||||
if (is_array($value) && !empty($value)) {
|
||||
$result = [];
|
||||
foreach ($value as $attributeCode => $data) {
|
||||
$wildcard = 0;
|
||||
if (isset($data[self::WILDCARD])) {
|
||||
$wildcard = $this->prepareVariable($data[self::WILDCARD]);
|
||||
}
|
||||
|
||||
$spelling = 0;
|
||||
if (isset($data[self::SPELLING])) {
|
||||
$spelling = $this->prepareVariable($data[self::SPELLING]);
|
||||
}
|
||||
|
||||
$combing = (int)($data[self::COMBINING] ?? 0);
|
||||
|
||||
if ($wildcard || $spelling || $combing) {
|
||||
$result[$attributeCode] = [
|
||||
self::KEY_WILDCARD => $wildcard,
|
||||
self::KEY_SPELLING => $spelling,
|
||||
self::KEY_COMBINING => $combing
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $this->serializer->serialize($result);
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $data
|
||||
*/
|
||||
private function prepareVariable($data): int
|
||||
{
|
||||
if (strcasecmp($data, 'on') === 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return (int) $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated moved to separate class
|
||||
* @see \Amasty\ElasticSearch\Model\Config\QuerySettingsProcessor::getConfigValue
|
||||
*
|
||||
* @param string $attributeCode
|
||||
* @return array|null
|
||||
*/
|
||||
public function getConfigValue($attributeCode)
|
||||
{
|
||||
return $this->processor->getConfigValue($attributeCode);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
<?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\Model\Config;
|
||||
|
||||
use Amasty\ElasticSearch\Model\Config as ElasticConfig;
|
||||
use Amasty\ElasticSearch\Model\Source\FulltextAttributes;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\Serialize\Serializer\Json;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
|
||||
class QuerySettingsProcessor
|
||||
{
|
||||
/**
|
||||
* @var int[]|string[]
|
||||
*/
|
||||
private $searchableAttributes;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $value;
|
||||
|
||||
/**
|
||||
* @var ScopeConfigInterface
|
||||
*/
|
||||
private $scopeConfig;
|
||||
|
||||
/**
|
||||
* @var FulltextAttributes
|
||||
*/
|
||||
private $fulltextAttributes;
|
||||
|
||||
/**
|
||||
* @var Json
|
||||
*/
|
||||
private $serializer;
|
||||
|
||||
public function __construct(
|
||||
ScopeConfigInterface $scopeConfig,
|
||||
FulltextAttributes $fulltextAttributes,
|
||||
Json $serializer
|
||||
) {
|
||||
$this->scopeConfig = $scopeConfig;
|
||||
$this->fulltextAttributes = $fulltextAttributes;
|
||||
$this->serializer = $serializer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|null array(
|
||||
* QuerySettings::WILDCARD => 1|0,
|
||||
* QuerySettings::SPELLING => 1|0,
|
||||
* QuerySettings::COMBINING => 1|0
|
||||
* )
|
||||
*/
|
||||
public function getConfigValue(string $attributeCode): ?array
|
||||
{
|
||||
if (!in_array($attributeCode, $this->getSearchableAttributeCodes(), true)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->getValue()[$attributeCode] ?? [
|
||||
QuerySettings::WILDCARD => 0,
|
||||
QuerySettings::SPELLING => 0,
|
||||
QuerySettings::COMBINING => 0
|
||||
];
|
||||
}
|
||||
|
||||
public function getSearchableAttributeCodes(): array
|
||||
{
|
||||
if ($this->searchableAttributes === null) {
|
||||
$this->searchableAttributes = array_keys($this->fulltextAttributes->toArray());
|
||||
}
|
||||
|
||||
return $this->searchableAttributes;
|
||||
}
|
||||
|
||||
private function getValue(): array
|
||||
{
|
||||
if ($this->value === null) {
|
||||
$value = $this->scopeConfig->getValue(
|
||||
ElasticConfig::ELASTIC_SEARCH_ENGINE . '/catalog/query_settings',
|
||||
ScopeInterface::SCOPE_STORE
|
||||
);
|
||||
$this->value = $this->parseLoad($value);
|
||||
}
|
||||
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function parseLoad($value)
|
||||
{
|
||||
$value = $this->unserializeValue($value);
|
||||
|
||||
return $this->convertConfigurations($value);
|
||||
}
|
||||
|
||||
private function unserializeValue($value)
|
||||
{
|
||||
$result = [];
|
||||
if (is_string($value) && !empty($value)) {
|
||||
$result = $this->serializer->unserialize($value);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert configuration from storable format to usable
|
||||
*
|
||||
* Older version of the module didn't have uniq storable format.
|
||||
* Method have resolver for that
|
||||
*/
|
||||
private function convertConfigurations(array $configurations): array
|
||||
{
|
||||
$attributeCodes = $this->getSearchableAttributeCodes();
|
||||
$result = [];
|
||||
foreach ($configurations as $code => $row) {
|
||||
if (in_array($code, $attributeCodes, true)) {
|
||||
$result[$code] = [
|
||||
QuerySettings::WILDCARD => $row[QuerySettings::KEY_WILDCARD] ?? $row[QuerySettings::WILDCARD] ?? 0,
|
||||
QuerySettings::SPELLING => $row[QuerySettings::KEY_SPELLING] ?? $row[QuerySettings::SPELLING] ?? 0,
|
||||
QuerySettings::COMBINING => $row[QuerySettings::KEY_COMBINING] ??
|
||||
$row[QuerySettings::COMBINING] ?? 0,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user