init
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Ui\DataProvider\Product\Form\Modifier;
|
||||
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Model\Locator\RegistryLocator;
|
||||
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\Serialize\Serializer\Json;
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class DetailContent extends AbstractModifier
|
||||
{
|
||||
private const JSON_ATTRIBUTES = [
|
||||
'esun_industry_applications',
|
||||
'esun_detail_gallery',
|
||||
'esun_creative_video_ids',
|
||||
'esun_product_faqs',
|
||||
'esun_certification_downloads',
|
||||
'esun_mix_step_prices',
|
||||
];
|
||||
|
||||
private const IMAGE_FIELDS = ['icon', 'image'];
|
||||
private const FILE_FIELDS = ['file'];
|
||||
|
||||
public function __construct(
|
||||
private readonly Json $json,
|
||||
private readonly StoreManagerInterface $storeManager,
|
||||
private readonly ProductRepositoryInterface $productRepository,
|
||||
private readonly RegistryLocator $locator
|
||||
) {
|
||||
}
|
||||
|
||||
public function modifyMeta(array $meta): array
|
||||
{
|
||||
return $meta;
|
||||
}
|
||||
|
||||
public function modifyData(array $data): array
|
||||
{
|
||||
if (!$data) {
|
||||
$product = $this->locator->getProduct();
|
||||
if ($product && $product->getId()) {
|
||||
$data[(int)$product->getId()] = [
|
||||
'product' => [
|
||||
'entity_id' => (int)$product->getId(),
|
||||
'current_store_id' => $this->getCurrentStoreId(),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($data as $productId => $productData) {
|
||||
if (!is_array($productData)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data[$productId] = $this->prepareProductData($productData);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function prepareProductData(array $productData): array
|
||||
{
|
||||
$storedValues = $this->loadStoredValues($productData);
|
||||
$productData['product'] = isset($productData['product']) && is_array($productData['product'])
|
||||
? $productData['product']
|
||||
: [];
|
||||
|
||||
foreach (self::JSON_ATTRIBUTES as $attributeCode) {
|
||||
$value = $productData['product'][$attributeCode]
|
||||
?? $productData[$attributeCode]
|
||||
?? $storedValues[$attributeCode]
|
||||
?? [];
|
||||
|
||||
$preparedRows = $this->prepareRows($value);
|
||||
$productData['product'][$attributeCode] = $preparedRows;
|
||||
unset($productData[$attributeCode]);
|
||||
}
|
||||
|
||||
return $productData;
|
||||
}
|
||||
|
||||
private function loadStoredValues(array $productData): array
|
||||
{
|
||||
$productId = (int)($productData['product']['entity_id'] ?? $productData['entity_id'] ?? 0);
|
||||
$locatorProduct = $this->locator->getProduct();
|
||||
if ($productId <= 0 && $locatorProduct instanceof Product && $locatorProduct->getId()) {
|
||||
$productId = (int)$locatorProduct->getId();
|
||||
}
|
||||
if ($productId <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$storeId = (int)($productData['product']['current_store_id'] ?? $productData['current_store_id'] ?? 0);
|
||||
if ($storeId <= 0) {
|
||||
$storeId = $this->getCurrentStoreId();
|
||||
}
|
||||
|
||||
try {
|
||||
$product = $this->productRepository->getById($productId, false, $storeId, true);
|
||||
} catch (NoSuchEntityException) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$values = [];
|
||||
foreach (self::JSON_ATTRIBUTES as $attributeCode) {
|
||||
$values[$attributeCode] = $product->getData($attributeCode);
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
private function getCurrentStoreId(): int
|
||||
{
|
||||
try {
|
||||
return (int)$this->locator->getStore()->getId();
|
||||
} catch (\Throwable) {
|
||||
return (int)$this->storeManager->getStore()->getId();
|
||||
}
|
||||
}
|
||||
|
||||
private function prepareRows(mixed $value): array
|
||||
{
|
||||
$rows = $this->decodeRows($value);
|
||||
foreach ($rows as &$row) {
|
||||
foreach (self::IMAGE_FIELDS as $field) {
|
||||
if (!empty($row[$field]) && is_string($row[$field])) {
|
||||
$row[$field] = [$this->buildImagePreview($row[$field])];
|
||||
}
|
||||
}
|
||||
foreach (self::FILE_FIELDS as $field) {
|
||||
if (!empty($row[$field]) && is_string($row[$field])) {
|
||||
$row[$field] = [$this->buildFilePreview($row[$field])];
|
||||
}
|
||||
}
|
||||
}
|
||||
unset($row);
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
private function decodeRows(mixed $value): array
|
||||
{
|
||||
if (is_array($value)) {
|
||||
return array_values($value);
|
||||
}
|
||||
if (!is_string($value) || trim($value) === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
$decoded = $this->json->unserialize($value);
|
||||
return is_array($decoded) ? array_values($decoded) : [];
|
||||
} catch (\InvalidArgumentException) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private function buildImagePreview(string $path): array
|
||||
{
|
||||
$path = ltrim($path, '/');
|
||||
|
||||
return [
|
||||
'name' => basename($path),
|
||||
'file' => $path,
|
||||
'url' => $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA) . $path,
|
||||
];
|
||||
}
|
||||
|
||||
private function buildFilePreview(string $path): array
|
||||
{
|
||||
$path = ltrim($path, '/');
|
||||
|
||||
return [
|
||||
'name' => basename($path),
|
||||
'file' => $path,
|
||||
'url' => $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA) . $path,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Ui\DataProvider\Product\Form\Modifier;
|
||||
|
||||
use Esun\Catalog\Model\ProductRecommendation\RecommendationRepository;
|
||||
use Esun\Catalog\Model\ProductRecommendation\WebsiteResolver;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Model\Locator\RegistryLocator;
|
||||
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
|
||||
class Recommendations extends AbstractModifier
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RecommendationRepository $recommendationRepository,
|
||||
private readonly WebsiteResolver $websiteResolver,
|
||||
private readonly ProductRepositoryInterface $productRepository,
|
||||
private readonly RegistryLocator $locator
|
||||
) {
|
||||
}
|
||||
|
||||
public function modifyMeta(array $meta): array
|
||||
{
|
||||
$store = $this->locator->getStore();
|
||||
$config = &$meta['esun_product_recommendations']['children']['esun_recommendations']
|
||||
['arguments']['data']['config'];
|
||||
$config['scopeLabel'] = '[WEBSITE]';
|
||||
if ((int)$store->getId() > 0) {
|
||||
$ids = $this->recommendationRepository->getProductIds(
|
||||
(int)$this->locator->getProduct()->getId(),
|
||||
(int)$store->getWebsiteId()
|
||||
);
|
||||
$config['service'] = ['template' => 'ui/form/element/helper/service'];
|
||||
$config['disabled'] = $ids === null;
|
||||
}
|
||||
return $meta;
|
||||
}
|
||||
|
||||
public function modifyData(array $data): array
|
||||
{
|
||||
$product = $this->locator->getProduct();
|
||||
if (!$product) {
|
||||
return $data;
|
||||
}
|
||||
$productId = (int)$product->getId();
|
||||
if (!isset($data[$productId])) {
|
||||
$data[$productId] = ['product' => ['entity_id' => $productId]];
|
||||
}
|
||||
$data[$productId]['product'] = (array)($data[$productId]['product'] ?? []);
|
||||
|
||||
$defaultIds = $this->recommendationRepository->getProductIds($productId, 0);
|
||||
$store = $this->locator->getStore();
|
||||
$ids = (int)$store->getId() > 0
|
||||
? $this->recommendationRepository->getProductIds($productId, (int)$store->getWebsiteId())
|
||||
: null;
|
||||
$data[$productId]['product']['esun_recommendations'] = $this->idsToSkuText($ids ?? $defaultIds ?? []);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function idsToSkuText(array $ids): string
|
||||
{
|
||||
$skus = [];
|
||||
foreach ($ids as $id) {
|
||||
try {
|
||||
$skus[] = (string)$this->productRepository->getById((int)$id, false, 0)->getSku();
|
||||
} catch (NoSuchEntityException) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return implode("\n", $skus);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Ui\DataProvider\SpecificationBrand;
|
||||
|
||||
use Esun\Catalog\Model\ResourceModel\SpecificationBrand\CollectionFactory;
|
||||
use Magento\Framework\Api\Filter;
|
||||
use Magento\Ui\DataProvider\AbstractDataProvider;
|
||||
|
||||
class ListingDataProvider extends AbstractDataProvider
|
||||
{
|
||||
public function __construct(
|
||||
string $name,
|
||||
string $primaryFieldName,
|
||||
string $requestFieldName,
|
||||
CollectionFactory $collectionFactory,
|
||||
array $meta = [],
|
||||
array $data = []
|
||||
) {
|
||||
$this->collection = $collectionFactory->create();
|
||||
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
|
||||
}
|
||||
|
||||
public function addFilter(Filter $filter): void
|
||||
{
|
||||
if ($filter->getField() !== 'fulltext') {
|
||||
parent::addFilter($filter);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->collection->addFieldToFilter('brand_name', ['like' => '%' . (string)$filter->getValue() . '%']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Ui\DataProvider\SpecificationFile;
|
||||
|
||||
use Esun\Catalog\Model\ResourceModel\SpecificationFile\CollectionFactory;
|
||||
use Magento\Framework\Api\Filter;
|
||||
use Magento\Ui\DataProvider\AbstractDataProvider;
|
||||
|
||||
class ListingDataProvider extends AbstractDataProvider
|
||||
{
|
||||
public function __construct(
|
||||
string $name,
|
||||
string $primaryFieldName,
|
||||
string $requestFieldName,
|
||||
CollectionFactory $collectionFactory,
|
||||
array $meta = [],
|
||||
array $data = []
|
||||
) {
|
||||
$this->collection = $collectionFactory->create();
|
||||
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
|
||||
}
|
||||
|
||||
public function addFilter(Filter $filter): void
|
||||
{
|
||||
if ($filter->getField() !== 'fulltext') {
|
||||
parent::addFilter($filter);
|
||||
return;
|
||||
}
|
||||
|
||||
$connection = $this->collection->getConnection();
|
||||
$value = '%' . (string)$filter->getValue() . '%';
|
||||
$this->collection->getSelect()->where(
|
||||
$connection->quoteInto('brand_name LIKE ?', $value)
|
||||
. ' OR '
|
||||
. $connection->quoteInto('model_name LIKE ?', $value)
|
||||
. ' OR '
|
||||
. $connection->quoteInto('file_path LIKE ?', $value)
|
||||
. ' OR '
|
||||
. $connection->quoteInto('file_name LIKE ?', $value)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Ui\DataProvider\SpecificationImport;
|
||||
|
||||
use Esun\Catalog\Model\ResourceModel\SpecificationImport\CollectionFactory;
|
||||
use Magento\Framework\Api\Filter;
|
||||
use Magento\Ui\DataProvider\AbstractDataProvider;
|
||||
|
||||
class ListingDataProvider extends AbstractDataProvider
|
||||
{
|
||||
public function __construct(
|
||||
string $name,
|
||||
string $primaryFieldName,
|
||||
string $requestFieldName,
|
||||
CollectionFactory $collectionFactory,
|
||||
array $meta = [],
|
||||
array $data = []
|
||||
) {
|
||||
$this->collection = $collectionFactory->create();
|
||||
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
|
||||
}
|
||||
|
||||
public function addFilter(Filter $filter): void
|
||||
{
|
||||
if ($filter->getField() !== 'fulltext') {
|
||||
parent::addFilter($filter);
|
||||
return;
|
||||
}
|
||||
|
||||
$value = '%' . (string)$filter->getValue() . '%';
|
||||
$connection = $this->collection->getConnection();
|
||||
$this->collection->getSelect()->where(
|
||||
$connection->quoteInto('brand.brand_name LIKE ?', $value)
|
||||
. ' OR '
|
||||
. $connection->quoteInto('main_table.archive_file LIKE ?', $value)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user