104 lines
3.6 KiB
PHP
104 lines
3.6 KiB
PHP
<?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;
|
|
}
|
|
}
|