Skip to content

Commit 1332a2e

Browse files
committed
added: support for empty localizable field export
1 parent 5ecaf44 commit 1332a2e

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

Connector/Processor/Denormalization/ReferenceDataProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ private function convertTranslatableFields(array $item): array
215215
foreach($item as $key => $value) {
216216
$translatableField = explode('-', $key);
217217
if(in_array($locale, $translatableField)) {
218-
$item[$translatableField[0] . 's'][$locale] = $value;
218+
$item[$translatableField[0]][$locale] = $value;
219219
unset($item[$key]);
220220
}
221221

Normalizer/Flat/ReferenceDataNormalizer.php

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Pim\Bundle\CustomEntityBundle\Normalizer\Flat;
44

55
use Akeneo\Pim\Enrichment\Component\Product\Model\ReferenceDataInterface;
6+
use Akeneo\Channel\Infrastructure\Component\Repository\LocaleRepositoryInterface;
7+
use Akeneo\Tool\Component\Localization\Model\TranslatableInterface;
68
use Akeneo\Tool\Component\Localization\Model\TranslationInterface;
79
use Doctrine\Common\Collections\Collection;
810
use Pim\Bundle\CustomEntityBundle\Metadata\ClassMetadataRegistry;
@@ -30,25 +32,34 @@ class ReferenceDataNormalizer implements NormalizerInterface
3032
/** @var NormalizerInterface */
3133
protected $transNormalizer;
3234

35+
/** @var LocaleRepositoryInterface|null */
36+
private $localeRepository;
37+
3338
/** @var string[] */
3439
protected $skippedFields = [];
3540

41+
/** @var string[] */
42+
private $translationSkippedFields = ['id', 'locale', 'foreignKey'];
43+
3644
/**
3745
* @param TargetEntityResolver $targetEntityResolver
3846
* @param ClassMetadataRegistry $classMetadataRegistry
3947
* @param PropertyAccessorInterface $propertyAccessor
4048
* @param NormalizerInterface $transNormalizer
49+
* @param LocaleRepositoryInterface|null $localeRepository
4150
*/
4251
public function __construct(
4352
TargetEntityResolver $targetEntityResolver,
4453
ClassMetadataRegistry $classMetadataRegistry,
4554
PropertyAccessorInterface $propertyAccessor,
46-
NormalizerInterface $transNormalizer
55+
NormalizerInterface $transNormalizer,
56+
LocaleRepositoryInterface $localeRepository = null
4757
) {
4858
$this->targetEntityResolver = $targetEntityResolver;
4959
$this->classMetadataRegistry = $classMetadataRegistry;
5060
$this->propertyAccessor = $propertyAccessor;
5161
$this->transNormalizer = $transNormalizer;
62+
$this->localeRepository = $localeRepository;
5263
}
5364

5465
/**
@@ -64,6 +75,11 @@ public function normalize($object, $format = null, array $context = []): array
6475
foreach ($properties as $property) {
6576
$propertyValue = $this->propertyAccessor->getValue($object, $property);
6677

78+
if ($propertyValue instanceof \DateTimeInterface) {
79+
$csvData[$property] = $propertyValue->format('Y-m-d');
80+
continue;
81+
}
82+
6783
if (is_object($propertyValue)) {
6884
$normalizedData = $this->normalizeLinkedObject($object, $property, $propertyValue, $format, $context);
6985
if (is_array($normalizedData)) {
@@ -76,6 +92,11 @@ public function normalize($object, $format = null, array $context = []): array
7692
}
7793
}
7894

95+
if ($object instanceof TranslatableInterface) {
96+
$csvData = $this->mergeTranslations($object, $csvData, $format);
97+
$csvData = $this->ensureTranslationColumns($object, $csvData);
98+
}
99+
79100
return $csvData;
80101
}
81102

@@ -121,6 +142,48 @@ protected function normalizeLinkedObject($object, $property, $propertyValue, $fo
121142
return null;
122143
}
123144

145+
private function mergeTranslations(TranslatableInterface $object, array $csvData, $format): array
146+
{
147+
foreach ($object->getTranslations() as $translation) {
148+
if (!$translation instanceof TranslationInterface) {
149+
continue;
150+
}
151+
152+
$translationData = $this->transNormalizer->normalize($translation, $format);
153+
$csvData = array_merge($csvData, $translationData);
154+
}
155+
156+
return $csvData;
157+
}
158+
159+
private function ensureTranslationColumns(TranslatableInterface $object, array $csvData): array
160+
{
161+
if (null === $this->localeRepository) {
162+
return $csvData;
163+
}
164+
165+
$localeCodes = $this->localeRepository->getActivatedLocaleCodes();
166+
if (empty($localeCodes)) {
167+
return $csvData;
168+
}
169+
170+
$translationClass = $object->getTranslationFQCN();
171+
$translationPrototype = new $translationClass();
172+
$properties = $this->classMetadataRegistry->getReadableProperties($translationPrototype);
173+
$properties = array_diff($properties, $this->translationSkippedFields);
174+
175+
foreach ($properties as $property) {
176+
foreach ($localeCodes as $localeCode) {
177+
$column = sprintf('%s-%s', $property, $localeCode);
178+
if (!array_key_exists($column, $csvData)) {
179+
$csvData[$column] = '';
180+
}
181+
}
182+
}
183+
184+
return $csvData;
185+
}
186+
124187
/**
125188
* {@inheritdoc}
126189
*

Resources/config/serializer.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ services:
3434
- '@pim_custom_entity.metadata.class_metadata_registry'
3535
- '@property_accessor'
3636
- '@pim_custom_entity.normalizer.flat.translation'
37+
- '@pim_catalog.repository.locale'
3738
calls:
3839
- ['setSkippedFields', [ ['id', 'created', 'updated', 'locale'] ] ]
3940

0 commit comments

Comments
 (0)