init
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\MultiLocaleTranslation\Model\Entity;
|
||||
|
||||
use Esun\MultiLocaleTranslation\Model\CmsTranslationRepository;
|
||||
use Esun\MultiLocaleTranslation\Model\BlogPostFields;
|
||||
use Esun\MultiLocaleTranslation\Model\Config\SkippedFields;
|
||||
use Esun\MultiLocaleTranslation\Model\TranslatableEntityInterface;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Mageplaza\Blog\Model\Post;
|
||||
use Mageplaza\Blog\Model\PostFactory;
|
||||
|
||||
class BlogPostEntity implements TranslatableEntityInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly PostFactory $postFactory,
|
||||
private readonly CmsTranslationRepository $translationRepository,
|
||||
private readonly SkippedFields $skippedFields
|
||||
) {
|
||||
}
|
||||
|
||||
public function getFields(int $entityId, int $sourceStoreId, int $targetStoreId): array
|
||||
{
|
||||
$post = $this->loadPost($entityId);
|
||||
$baseValues = $this->getBaseValues($post);
|
||||
$sourceValues = $sourceStoreId > 0
|
||||
? array_replace($baseValues, $this->getTranslations($entityId, $sourceStoreId))
|
||||
: $baseValues;
|
||||
$targetTranslations = $this->getTranslations($entityId, $targetStoreId);
|
||||
|
||||
$fields = [];
|
||||
foreach (BlogPostFields::FIELDS as $field => $meta) {
|
||||
if ($this->skippedFields->isSkipped('blog_post', $field)) {
|
||||
continue;
|
||||
}
|
||||
$fields[] = [
|
||||
'code' => $field,
|
||||
'label' => $meta['label'],
|
||||
'source_value' => (string)($sourceValues[$field] ?? ''),
|
||||
'target_value' => (string)($targetTranslations[$field] ?? $baseValues[$field] ?? ''),
|
||||
'is_html' => (bool)$meta['is_html'],
|
||||
'use_default' => !array_key_exists($field, $targetTranslations),
|
||||
];
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
public function apply(int $entityId, int $targetStoreId, array $fields, array $useDefault = []): void
|
||||
{
|
||||
$this->loadPost($entityId);
|
||||
$defaultFields = [];
|
||||
$translations = [];
|
||||
|
||||
foreach (BlogPostFields::codes() as $field) {
|
||||
if ($this->skippedFields->isSkipped('blog_post', $field)) {
|
||||
continue;
|
||||
}
|
||||
if (!empty($useDefault[$field])) {
|
||||
$defaultFields[] = $field;
|
||||
continue;
|
||||
}
|
||||
if (array_key_exists($field, $fields)) {
|
||||
$translations[$field] = (string)$fields[$field];
|
||||
}
|
||||
}
|
||||
|
||||
$this->translationRepository->deleteTranslations(
|
||||
$entityId,
|
||||
$targetStoreId,
|
||||
CmsTranslationRepository::ENTITY_BLOG_POST,
|
||||
$defaultFields
|
||||
);
|
||||
$this->translationRepository->saveTranslations(
|
||||
$entityId,
|
||||
$targetStoreId,
|
||||
CmsTranslationRepository::ENTITY_BLOG_POST,
|
||||
$translations
|
||||
);
|
||||
}
|
||||
|
||||
private function loadPost(int $entityId): Post
|
||||
{
|
||||
$post = $this->postFactory->create()->load($entityId);
|
||||
if (!$post->getId()) {
|
||||
throw NoSuchEntityException::singleField('post_id', $entityId);
|
||||
}
|
||||
|
||||
return $post;
|
||||
}
|
||||
|
||||
private function getBaseValues(Post $post): array
|
||||
{
|
||||
$values = [];
|
||||
foreach (BlogPostFields::codes() as $field) {
|
||||
$values[$field] = BlogPostFields::baseValue($field, $post->getData($field));
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
private function getTranslations(int $entityId, int $storeId): array
|
||||
{
|
||||
return $this->translationRepository->getTranslations(
|
||||
$entityId,
|
||||
$storeId,
|
||||
CmsTranslationRepository::ENTITY_BLOG_POST,
|
||||
BlogPostFields::codes()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\MultiLocaleTranslation\Model\Entity;
|
||||
|
||||
use Esun\MultiLocaleTranslation\Model\Config\SkippedFields;
|
||||
use Esun\MultiLocaleTranslation\Model\TranslatableEntityInterface;
|
||||
use Magento\Catalog\Api\Data\CategoryInterface;
|
||||
use Magento\Catalog\Model\Category;
|
||||
use Magento\Catalog\Model\CategoryFactory;
|
||||
use Magento\Catalog\Model\Attribute\ScopeOverriddenValue;
|
||||
use Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
|
||||
class CategoryEntity implements TranslatableEntityInterface
|
||||
{
|
||||
private const TRANSLATABLE_FIELDS = [
|
||||
'name' => ['label' => 'Category Name', 'is_html' => false],
|
||||
'description' => ['label' => 'Description', 'is_html' => true],
|
||||
'meta_title' => ['label' => 'Meta Title', 'is_html' => false],
|
||||
'meta_keywords' => ['label' => 'Meta Keywords', 'is_html' => false],
|
||||
'meta_description' => ['label' => 'Meta Description', 'is_html' => false],
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private readonly CategoryFactory $categoryFactory,
|
||||
private readonly CategoryResource $categoryResource,
|
||||
private readonly ScopeOverriddenValue $scopeOverriddenValue,
|
||||
private readonly SkippedFields $skippedFields
|
||||
) {
|
||||
}
|
||||
|
||||
public function getFields(int $entityId, int $sourceStoreId, int $targetStoreId): array
|
||||
{
|
||||
$source = $this->getCategory($entityId, $sourceStoreId);
|
||||
$target = $this->getCategory($entityId, $targetStoreId);
|
||||
|
||||
$fields = [];
|
||||
foreach (self::TRANSLATABLE_FIELDS as $field => $meta) {
|
||||
if ($this->skippedFields->isSkipped('category', $field)) {
|
||||
continue;
|
||||
}
|
||||
$fields[] = [
|
||||
'code' => $field,
|
||||
'label' => $meta['label'],
|
||||
'source_value' => (string)($source->getData($field) ?? ''),
|
||||
'target_value' => (string)($target->getData($field) ?? ''),
|
||||
'is_html' => (bool)$meta['is_html'],
|
||||
'use_default' => $targetStoreId > 0 && !$this->scopeOverriddenValue->containsValue(
|
||||
CategoryInterface::class,
|
||||
$target,
|
||||
$field,
|
||||
$targetStoreId
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
public function apply(int $entityId, int $targetStoreId, array $fields, array $useDefault = []): void
|
||||
{
|
||||
$category = $this->getCategory($entityId, $targetStoreId);
|
||||
|
||||
foreach (array_keys(self::TRANSLATABLE_FIELDS) as $field) {
|
||||
if ($this->skippedFields->isSkipped('category', $field)) {
|
||||
continue;
|
||||
}
|
||||
if (!empty($useDefault[$field])) {
|
||||
$category->setData($field, null);
|
||||
continue;
|
||||
}
|
||||
if (!array_key_exists($field, $fields)) {
|
||||
continue;
|
||||
}
|
||||
$category->setData($field, (string)$fields[$field]);
|
||||
}
|
||||
|
||||
$this->categoryResource->save($category);
|
||||
}
|
||||
|
||||
private function getCategory(int $entityId, int $storeId): CategoryInterface
|
||||
{
|
||||
/** @var Category $category */
|
||||
$category = $this->categoryFactory->create();
|
||||
$category->setStoreId(max(0, $storeId))->load($entityId);
|
||||
if (!$category->getId()) {
|
||||
throw NoSuchEntityException::singleField('id', $entityId);
|
||||
}
|
||||
|
||||
return $category;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\MultiLocaleTranslation\Model\Entity;
|
||||
|
||||
use Esun\MultiLocaleTranslation\Model\CmsTranslationRepository;
|
||||
use Esun\MultiLocaleTranslation\Model\Config\SkippedFields;
|
||||
use Esun\MultiLocaleTranslation\Model\TranslatableEntityInterface;
|
||||
use Magento\Cms\Model\Block;
|
||||
use Magento\Cms\Model\BlockFactory;
|
||||
|
||||
class CmsBlockEntity implements TranslatableEntityInterface
|
||||
{
|
||||
private const FIELDS = [
|
||||
'title' => ['label' => 'Block Title', 'is_html' => false],
|
||||
'content' => ['label' => 'Content', 'is_html' => true],
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private readonly BlockFactory $blockFactory,
|
||||
private readonly CmsTranslationRepository $translationRepository,
|
||||
private readonly SkippedFields $skippedFields
|
||||
) {
|
||||
}
|
||||
|
||||
public function getFields(int $entityId, int $sourceStoreId, int $targetStoreId): array
|
||||
{
|
||||
$source = $this->loadBlock($entityId, $sourceStoreId);
|
||||
$targetValues = $this->translationRepository->getTranslations(
|
||||
$entityId,
|
||||
$targetStoreId,
|
||||
CmsTranslationRepository::ENTITY_BLOCK,
|
||||
array_keys(self::FIELDS)
|
||||
);
|
||||
|
||||
$fields = [];
|
||||
foreach (self::FIELDS as $field => $meta) {
|
||||
if ($this->skippedFields->isSkipped('cms_block', $field)) {
|
||||
continue;
|
||||
}
|
||||
$fields[] = [
|
||||
'code' => $field,
|
||||
'label' => $meta['label'],
|
||||
'source_value' => (string)($source->getData($field) ?? ''),
|
||||
'target_value' => (string)($targetValues[$field] ?? $source->getData($field) ?? ''),
|
||||
'is_html' => (bool)$meta['is_html'],
|
||||
'use_default' => !array_key_exists($field, $targetValues),
|
||||
];
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
public function apply(int $entityId, int $targetStoreId, array $fields, array $useDefault = []): void
|
||||
{
|
||||
$defaultFields = [];
|
||||
foreach (array_keys(array_filter($useDefault)) as $field) {
|
||||
if (isset(self::FIELDS[$field]) && !$this->skippedFields->isSkipped('cms_block', $field)) {
|
||||
$defaultFields[] = $field;
|
||||
}
|
||||
}
|
||||
$this->translationRepository->deleteTranslations(
|
||||
$entityId,
|
||||
$targetStoreId,
|
||||
CmsTranslationRepository::ENTITY_BLOCK,
|
||||
$defaultFields
|
||||
);
|
||||
|
||||
$fields = array_diff_key($fields, array_flip($defaultFields));
|
||||
$this->translationRepository->saveTranslations(
|
||||
$entityId,
|
||||
$targetStoreId,
|
||||
CmsTranslationRepository::ENTITY_BLOCK,
|
||||
$this->filterFields($fields)
|
||||
);
|
||||
}
|
||||
|
||||
private function loadBlock(int $entityId, int $storeId): Block
|
||||
{
|
||||
$block = $this->blockFactory->create();
|
||||
$block->setStoreId($storeId > 0 ? $storeId : 0)->load($entityId);
|
||||
return $block;
|
||||
}
|
||||
|
||||
private function filterFields(array $fields): array
|
||||
{
|
||||
$result = [];
|
||||
foreach (array_keys(self::FIELDS) as $field) {
|
||||
if ($this->skippedFields->isSkipped('cms_block', $field)
|
||||
|| !array_key_exists($field, $fields)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
$result[$field] = (string)$fields[$field];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\MultiLocaleTranslation\Model\Entity;
|
||||
|
||||
use Esun\MultiLocaleTranslation\Model\CmsTranslationRepository;
|
||||
use Esun\MultiLocaleTranslation\Model\Config\SkippedFields;
|
||||
use Esun\MultiLocaleTranslation\Model\TranslatableEntityInterface;
|
||||
use Magento\Cms\Model\Page;
|
||||
use Magento\Cms\Model\PageFactory;
|
||||
|
||||
class CmsPageEntity implements TranslatableEntityInterface
|
||||
{
|
||||
private const FIELDS = [
|
||||
'title' => ['label' => 'Page Title', 'is_html' => false],
|
||||
'content_heading' => ['label' => 'Content Heading', 'is_html' => false],
|
||||
'content' => ['label' => 'Content', 'is_html' => true],
|
||||
'meta_title' => ['label' => 'Meta Title', 'is_html' => false],
|
||||
'meta_keywords' => ['label' => 'Meta Keywords', 'is_html' => false],
|
||||
'meta_description' => ['label' => 'Meta Description', 'is_html' => false],
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private readonly PageFactory $pageFactory,
|
||||
private readonly CmsTranslationRepository $translationRepository,
|
||||
private readonly SkippedFields $skippedFields
|
||||
) {
|
||||
}
|
||||
|
||||
public function getFields(int $entityId, int $sourceStoreId, int $targetStoreId): array
|
||||
{
|
||||
$source = $this->loadPage($entityId, $sourceStoreId);
|
||||
$targetValues = $this->translationRepository->getTranslations(
|
||||
$entityId,
|
||||
$targetStoreId,
|
||||
CmsTranslationRepository::ENTITY_PAGE,
|
||||
array_keys(self::FIELDS)
|
||||
);
|
||||
|
||||
$fields = [];
|
||||
foreach (self::FIELDS as $field => $meta) {
|
||||
if ($this->skippedFields->isSkipped('cms_page', $field)) {
|
||||
continue;
|
||||
}
|
||||
$fields[] = [
|
||||
'code' => $field,
|
||||
'label' => $meta['label'],
|
||||
'source_value' => (string)($source->getData($field) ?? ''),
|
||||
'target_value' => (string)($targetValues[$field] ?? $source->getData($field) ?? ''),
|
||||
'is_html' => (bool)$meta['is_html'],
|
||||
'use_default' => !array_key_exists($field, $targetValues),
|
||||
];
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
public function apply(int $entityId, int $targetStoreId, array $fields, array $useDefault = []): void
|
||||
{
|
||||
$defaultFields = [];
|
||||
foreach (array_keys(array_filter($useDefault)) as $field) {
|
||||
if (isset(self::FIELDS[$field]) && !$this->skippedFields->isSkipped('cms_page', $field)) {
|
||||
$defaultFields[] = $field;
|
||||
}
|
||||
}
|
||||
$this->translationRepository->deleteTranslations(
|
||||
$entityId,
|
||||
$targetStoreId,
|
||||
CmsTranslationRepository::ENTITY_PAGE,
|
||||
$defaultFields
|
||||
);
|
||||
|
||||
$fields = array_diff_key($fields, array_flip($defaultFields));
|
||||
$this->translationRepository->saveTranslations(
|
||||
$entityId,
|
||||
$targetStoreId,
|
||||
CmsTranslationRepository::ENTITY_PAGE,
|
||||
$this->filterFields($fields)
|
||||
);
|
||||
}
|
||||
|
||||
private function loadPage(int $entityId, int $storeId): Page
|
||||
{
|
||||
$page = $this->pageFactory->create();
|
||||
$page->setStoreId($storeId > 0 ? $storeId : 0)->load($entityId);
|
||||
return $page;
|
||||
}
|
||||
|
||||
private function filterFields(array $fields): array
|
||||
{
|
||||
$result = [];
|
||||
foreach (array_keys(self::FIELDS) as $field) {
|
||||
if ($this->skippedFields->isSkipped('cms_page', $field)
|
||||
|| !array_key_exists($field, $fields)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
$result[$field] = (string)$fields[$field];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\MultiLocaleTranslation\Model\Entity;
|
||||
|
||||
use Esun\MultiLocaleTranslation\Model\Config\SkippedFields;
|
||||
use Esun\MultiLocaleTranslation\Model\TranslatableEntityInterface;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Api\Data\ProductInterface;
|
||||
use Magento\Catalog\Model\Attribute\ScopeOverriddenValue;
|
||||
|
||||
class ProductEntity implements TranslatableEntityInterface
|
||||
{
|
||||
private const TRANSLATABLE_FIELDS = [
|
||||
'name' => ['label' => 'Name', 'is_html' => false],
|
||||
'description' => ['label' => 'Description', 'is_html' => true],
|
||||
'short_description' => ['label' => 'Short Description', 'is_html' => true],
|
||||
'meta_title' => ['label' => 'Meta Title', 'is_html' => false],
|
||||
'meta_keyword' => ['label' => 'Meta Keywords', 'is_html' => false],
|
||||
'meta_description' => ['label' => 'Meta Description', 'is_html' => false],
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private readonly ProductRepositoryInterface $productRepository,
|
||||
private readonly ScopeOverriddenValue $scopeOverriddenValue,
|
||||
private readonly SkippedFields $skippedFields
|
||||
) {
|
||||
}
|
||||
|
||||
public function getFields(int $entityId, int $sourceStoreId, int $targetStoreId): array
|
||||
{
|
||||
$source = $this->getProduct($entityId, $sourceStoreId);
|
||||
$target = $this->getProduct($entityId, $targetStoreId);
|
||||
|
||||
$fields = [];
|
||||
foreach (self::TRANSLATABLE_FIELDS as $field => $meta) {
|
||||
if ($this->skippedFields->isSkipped('product', $field)) {
|
||||
continue;
|
||||
}
|
||||
$fields[] = [
|
||||
'code' => $field,
|
||||
'label' => $meta['label'],
|
||||
'source_value' => (string)($source->getData($field) ?? ''),
|
||||
'target_value' => (string)($target->getData($field) ?? ''),
|
||||
'is_html' => (bool)$meta['is_html'],
|
||||
'use_default' => $targetStoreId > 0 && !$this->scopeOverriddenValue->containsValue(
|
||||
ProductInterface::class,
|
||||
$target,
|
||||
$field,
|
||||
$targetStoreId
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
public function apply(int $entityId, int $targetStoreId, array $fields, array $useDefault = []): void
|
||||
{
|
||||
$product = $this->getProduct($entityId, $targetStoreId, true);
|
||||
|
||||
foreach (array_keys(self::TRANSLATABLE_FIELDS) as $field) {
|
||||
if ($this->skippedFields->isSkipped('product', $field)) {
|
||||
continue;
|
||||
}
|
||||
if (!empty($useDefault[$field])) {
|
||||
$product->setData($field, null);
|
||||
continue;
|
||||
}
|
||||
if (!array_key_exists($field, $fields)) {
|
||||
continue;
|
||||
}
|
||||
$product->setData($field, (string)$fields[$field]);
|
||||
}
|
||||
|
||||
$this->productRepository->save($product);
|
||||
}
|
||||
|
||||
private function getProduct(int $entityId, int $storeId, bool $forceReload = false): ProductInterface
|
||||
{
|
||||
$storeId = $storeId > 0 ? $storeId : null;
|
||||
$product = $this->productRepository->getById($entityId, true, $storeId, $forceReload);
|
||||
|
||||
return $product;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\MultiLocaleTranslation\Model\Entity;
|
||||
|
||||
use Esun\MultiLocaleTranslation\Model\CmsTranslationRepository;
|
||||
use Esun\MultiLocaleTranslation\Model\Config\SkippedFields;
|
||||
use Esun\MultiLocaleTranslation\Model\TranslatableEntityInterface;
|
||||
use Esun\MultiLocaleTranslation\Model\WikiFields;
|
||||
use Esun\Wiki\Model\Category;
|
||||
use Esun\Wiki\Model\CategoryFactory;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
|
||||
class WikiCategoryEntity implements TranslatableEntityInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CategoryFactory $categoryFactory,
|
||||
private readonly CmsTranslationRepository $translationRepository,
|
||||
private readonly SkippedFields $skippedFields
|
||||
) {
|
||||
}
|
||||
|
||||
public function getFields(int $entityId, int $sourceStoreId, int $targetStoreId): array
|
||||
{
|
||||
$category = $this->loadCategory($entityId);
|
||||
$baseValues = $this->getBaseValues($category);
|
||||
$sourceValues = $sourceStoreId > 0
|
||||
? array_replace($baseValues, $this->getTranslations($entityId, $sourceStoreId))
|
||||
: $baseValues;
|
||||
$targetTranslations = $this->getTranslations($entityId, $targetStoreId);
|
||||
$fields = [];
|
||||
|
||||
foreach (WikiFields::CATEGORY as $field => $meta) {
|
||||
if ($this->skippedFields->isSkipped('wiki_category', $field)) {
|
||||
continue;
|
||||
}
|
||||
$fields[] = [
|
||||
'code' => $field,
|
||||
'label' => $meta['label'],
|
||||
'source_value' => (string)($sourceValues[$field] ?? ''),
|
||||
'target_value' => (string)($targetTranslations[$field] ?? $baseValues[$field] ?? ''),
|
||||
'is_html' => (bool)$meta['is_html'],
|
||||
'use_default' => !array_key_exists($field, $targetTranslations),
|
||||
];
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
public function apply(int $entityId, int $targetStoreId, array $fields, array $useDefault = []): void
|
||||
{
|
||||
$this->loadCategory($entityId);
|
||||
$defaultFields = [];
|
||||
$translations = [];
|
||||
foreach (WikiFields::categoryCodes() as $field) {
|
||||
if ($this->skippedFields->isSkipped('wiki_category', $field)) {
|
||||
continue;
|
||||
}
|
||||
if (!empty($useDefault[$field])) {
|
||||
$defaultFields[] = $field;
|
||||
} elseif (array_key_exists($field, $fields)) {
|
||||
$translations[$field] = (string)$fields[$field];
|
||||
}
|
||||
}
|
||||
|
||||
$this->translationRepository->deleteTranslations(
|
||||
$entityId,
|
||||
$targetStoreId,
|
||||
CmsTranslationRepository::ENTITY_WIKI_CATEGORY,
|
||||
$defaultFields
|
||||
);
|
||||
$this->translationRepository->saveTranslations(
|
||||
$entityId,
|
||||
$targetStoreId,
|
||||
CmsTranslationRepository::ENTITY_WIKI_CATEGORY,
|
||||
$translations
|
||||
);
|
||||
}
|
||||
|
||||
private function loadCategory(int $entityId): Category
|
||||
{
|
||||
$category = $this->categoryFactory->create()->load($entityId);
|
||||
if (!$category->getId()) {
|
||||
throw NoSuchEntityException::singleField('category_id', $entityId);
|
||||
}
|
||||
return $category;
|
||||
}
|
||||
|
||||
private function getBaseValues(Category $category): array
|
||||
{
|
||||
$values = [];
|
||||
foreach (WikiFields::categoryCodes() as $field) {
|
||||
$values[$field] = (string)($category->getData($field) ?? '');
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
private function getTranslations(int $entityId, int $storeId): array
|
||||
{
|
||||
return $this->translationRepository->getTranslations(
|
||||
$entityId,
|
||||
$storeId,
|
||||
CmsTranslationRepository::ENTITY_WIKI_CATEGORY,
|
||||
WikiFields::categoryCodes()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\MultiLocaleTranslation\Model\Entity;
|
||||
|
||||
use Esun\MultiLocaleTranslation\Model\CmsTranslationRepository;
|
||||
use Esun\MultiLocaleTranslation\Model\Config\SkippedFields;
|
||||
use Esun\MultiLocaleTranslation\Model\TranslatableEntityInterface;
|
||||
use Esun\MultiLocaleTranslation\Model\WikiFields;
|
||||
use Esun\Wiki\Model\Homepage;
|
||||
use Esun\Wiki\Model\ResourceModel\Homepage\CollectionFactory;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
|
||||
class WikiHomepageEntity implements TranslatableEntityInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CollectionFactory $collectionFactory,
|
||||
private readonly CmsTranslationRepository $translationRepository,
|
||||
private readonly SkippedFields $skippedFields
|
||||
) {
|
||||
}
|
||||
|
||||
public function getFields(int $entityId, int $sourceStoreId, int $targetStoreId): array
|
||||
{
|
||||
$homepage = $this->loadHomepage($entityId);
|
||||
$baseValues = $this->getBaseValues($homepage);
|
||||
$sourceValues = $sourceStoreId > 0
|
||||
? array_replace($baseValues, $this->getTranslations($entityId, $sourceStoreId))
|
||||
: $baseValues;
|
||||
$targetTranslations = $this->getTranslations($entityId, $targetStoreId);
|
||||
$fields = [];
|
||||
|
||||
foreach (WikiFields::HOMEPAGE as $field => $meta) {
|
||||
if ($this->skippedFields->isSkipped('wiki_homepage', $field)) {
|
||||
continue;
|
||||
}
|
||||
$fields[] = [
|
||||
'code' => $field,
|
||||
'label' => $meta['label'],
|
||||
'source_value' => (string)($sourceValues[$field] ?? ''),
|
||||
'target_value' => (string)($targetTranslations[$field] ?? $baseValues[$field] ?? ''),
|
||||
'is_html' => (bool)$meta['is_html'],
|
||||
'use_default' => !array_key_exists($field, $targetTranslations),
|
||||
];
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
public function apply(int $entityId, int $targetStoreId, array $fields, array $useDefault = []): void
|
||||
{
|
||||
$this->loadHomepage($entityId);
|
||||
$defaultFields = [];
|
||||
$translations = [];
|
||||
foreach (WikiFields::homepageCodes() as $field) {
|
||||
if ($this->skippedFields->isSkipped('wiki_homepage', $field)) {
|
||||
continue;
|
||||
}
|
||||
if (!empty($useDefault[$field])) {
|
||||
$defaultFields[] = $field;
|
||||
} elseif (array_key_exists($field, $fields)) {
|
||||
$translations[$field] = (string)$fields[$field];
|
||||
}
|
||||
}
|
||||
|
||||
$this->translationRepository->deleteTranslations(
|
||||
$entityId,
|
||||
$targetStoreId,
|
||||
CmsTranslationRepository::ENTITY_WIKI_HOMEPAGE,
|
||||
$defaultFields
|
||||
);
|
||||
$this->translationRepository->saveTranslations(
|
||||
$entityId,
|
||||
$targetStoreId,
|
||||
CmsTranslationRepository::ENTITY_WIKI_HOMEPAGE,
|
||||
$translations
|
||||
);
|
||||
}
|
||||
|
||||
private function loadHomepage(int $entityId): Homepage
|
||||
{
|
||||
$homepage = $this->collectionFactory->create()
|
||||
->addFieldToFilter('homepage_id', $entityId)
|
||||
->setPageSize(1)
|
||||
->getFirstItem();
|
||||
if (!$homepage->getId()) {
|
||||
throw NoSuchEntityException::singleField('homepage_id', $entityId);
|
||||
}
|
||||
return $homepage;
|
||||
}
|
||||
|
||||
private function getBaseValues(Homepage $homepage): array
|
||||
{
|
||||
$values = [];
|
||||
foreach (WikiFields::homepageCodes() as $field) {
|
||||
$values[$field] = (string)($homepage->getData($field) ?? '');
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
private function getTranslations(int $entityId, int $storeId): array
|
||||
{
|
||||
return $this->translationRepository->getTranslations(
|
||||
$entityId,
|
||||
$storeId,
|
||||
CmsTranslationRepository::ENTITY_WIKI_HOMEPAGE,
|
||||
WikiFields::homepageCodes()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user