init
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Translation\Model\Product;
|
||||
|
||||
use Esun\Translation\Model\Config;
|
||||
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
|
||||
|
||||
class TranslatableFieldProvider
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Config $config,
|
||||
private readonly ProductRepositoryInterface $productRepository,
|
||||
private readonly ProductAttributeRepositoryInterface $attributeRepository
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int[] $targetStoreIds
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
public function getFields(int $productId, array $targetStoreIds = []): array
|
||||
{
|
||||
$sourceProduct = $this->productRepository->getById($productId, false, $this->config->getSourceStoreId());
|
||||
$targetProduct = $targetStoreIds !== []
|
||||
? $this->productRepository->getById($productId, false, (int)reset($targetStoreIds))
|
||||
: null;
|
||||
$fields = [];
|
||||
|
||||
foreach ($this->getAttributes() as $attribute) {
|
||||
$code = (string)$attribute->getAttributeCode();
|
||||
$sourceValue = (string)($sourceProduct->getData($code) ?? '');
|
||||
$targetValue = $targetProduct ? (string)($targetProduct->getData($code) ?? '') : '';
|
||||
|
||||
$fields[] = [
|
||||
'code' => $code,
|
||||
'label' => (string)$attribute->getDefaultFrontendLabel() ?: $code,
|
||||
'source_value' => $sourceValue,
|
||||
'target_value' => $targetValue,
|
||||
'is_html' => $this->isHtmlAttribute($attribute),
|
||||
];
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Attribute[]
|
||||
*/
|
||||
public function getAttributes(): array
|
||||
{
|
||||
$attributes = [];
|
||||
foreach ($this->config->getTranslatableAttributeCodes() as $code) {
|
||||
try {
|
||||
$attribute = $this->attributeRepository->get($code);
|
||||
} catch (\Throwable) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$attribute instanceof Attribute) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!in_array((string)$attribute->getFrontendInput(), ['text', 'textarea'], true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($attribute->isScopeGlobal()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$attributes[$code] = $attribute;
|
||||
}
|
||||
|
||||
return array_values($attributes);
|
||||
}
|
||||
|
||||
public function isAttributeAllowed(string $code): bool
|
||||
{
|
||||
foreach ($this->getAttributes() as $attribute) {
|
||||
if ((string)$attribute->getAttributeCode() === $code) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function isHtmlAttributeCode(string $code): bool
|
||||
{
|
||||
foreach ($this->getAttributes() as $attribute) {
|
||||
if ((string)$attribute->getAttributeCode() === $code) {
|
||||
return $this->isHtmlAttribute($attribute);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function isHtmlAttribute(Attribute $attribute): bool
|
||||
{
|
||||
return (bool)$attribute->getIsWysiwygEnabled()
|
||||
|| in_array((string)$attribute->getAttributeCode(), ['description', 'short_description'], true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Translation\Model\Product;
|
||||
|
||||
use Esun\Translation\Model\Html\HtmlTranslationValidator;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
|
||||
class TranslationApplier
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ProductRepositoryInterface $productRepository,
|
||||
private readonly TranslatableFieldProvider $fieldProvider,
|
||||
private readonly HtmlTranslationValidator $htmlValidator
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int[] $storeIds
|
||||
* @param array<string, string> $fields
|
||||
*/
|
||||
public function apply(int $productId, array $storeIds, array $fields): int
|
||||
{
|
||||
$fields = $this->filterAndValidateFields($fields);
|
||||
if ($fields === []) {
|
||||
throw new \RuntimeException('No translatable fields were provided.');
|
||||
}
|
||||
|
||||
$updated = 0;
|
||||
foreach (array_unique(array_map('intval', $storeIds)) as $storeId) {
|
||||
if ($storeId <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$product = $this->productRepository->getById($productId, false, $storeId);
|
||||
$product->setStoreId($storeId);
|
||||
foreach ($fields as $code => $value) {
|
||||
$product->setData($code, $value);
|
||||
}
|
||||
$this->productRepository->save($product);
|
||||
$updated++;
|
||||
}
|
||||
|
||||
if ($updated === 0) {
|
||||
throw new \RuntimeException('No valid target store views were provided.');
|
||||
}
|
||||
|
||||
return $updated;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $fields
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private function filterAndValidateFields(array $fields): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($fields as $code => $value) {
|
||||
$code = (string)$code;
|
||||
if (!$this->fieldProvider->isAttributeAllowed($code)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = (string)$value;
|
||||
if ($this->fieldProvider->isHtmlAttributeCode($code)) {
|
||||
$errors = $this->htmlValidator->validate($value);
|
||||
if ($errors !== []) {
|
||||
throw new \RuntimeException(sprintf(
|
||||
'HTML for "%s" is invalid: %s',
|
||||
$code,
|
||||
implode('; ', $errors)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
$result[$code] = $value;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user