33namespace Pim \Bundle \CustomEntityBundle \Normalizer \Flat ;
44
55use Akeneo \Pim \Enrichment \Component \Product \Model \ReferenceDataInterface ;
6+ use Akeneo \Channel \Infrastructure \Component \Repository \LocaleRepositoryInterface ;
7+ use Akeneo \Tool \Component \Localization \Model \TranslatableInterface ;
68use Akeneo \Tool \Component \Localization \Model \TranslationInterface ;
79use Doctrine \Common \Collections \Collection ;
810use 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 *
0 commit comments