init
This commit is contained in:
@@ -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