Esempio n. 1
0
 /**
  * Persists data into DB in single transaction
  *
  * @param string $locale
  * @param array  $data translations strings, format same as MassageCatalog::all() returns
  *
  * @throws \Exception
  */
 public function persist($locale, array $data)
 {
     $writeCount = 0;
     try {
         $this->em->beginTransaction();
         foreach ($data as $domain => $domainData) {
             foreach ($domainData as $key => $translation) {
                 if (strlen($key) > MySqlPlatform::LENGTH_LIMIT_TINYTEXT) {
                     continue;
                 }
                 $writeCount++;
                 $this->toWrite[] = $this->getTranslationObject($key, $locale, $domain, $translation);
                 if (0 === $writeCount % $this->batchSize) {
                     $this->write($this->toWrite);
                     $this->toWrite = [];
                 }
             }
         }
         if (count($this->toWrite) > 0) {
             $this->write($this->toWrite);
         }
         $this->em->commit();
     } catch (\Exception $exception) {
         $this->em->rollback();
         throw $exception;
     }
     // update timestamp in case when persist succeed
     $this->metadataCache->updateTimestamp($locale);
 }
 /**
  * @param ConfigUpdateEvent $event
  */
 public function onConfigUpdate(ConfigUpdateEvent $event)
 {
     if (!$event->isChanged('oro_locale.language')) {
         return;
     }
     // mark translation cache dirty
     $this->dbTranslationMetadataCache->updateTimestamp($event->getNewValue('oro_locale.language'));
 }
 /**
  * @param array $translations
  */
 public function saveTranslations(array $translations)
 {
     if (!$translations) {
         return;
     }
     $locale = $this->translator->getLocale();
     $entities = [];
     foreach ($translations as $key => $value) {
         $entities[] = $this->createTranslationEntity($key, $value, $locale);
     }
     // mark translation cache dirty
     $this->translationCache->updateTimestamp($locale);
     $this->getTranslationManager()->flush($entities);
 }
 /**
  * @param string $enumValueClassName
  * @param array  $options
  * @param string $locale
  *
  * @throws \InvalidArgumentException
  */
 public function applyEnumOptions($enumValueClassName, array $options, $locale)
 {
     if (empty($enumValueClassName)) {
         throw new \InvalidArgumentException('$enumValueClassName must not be empty.');
     }
     if (empty($locale)) {
         throw new \InvalidArgumentException('$locale must not be empty.');
     }
     /** @var EntityManager $em */
     $em = $this->doctrine->getManagerForClass($enumValueClassName);
     /** @var EnumValueRepository $enumRepo */
     $enumRepo = $em->getRepository($enumValueClassName);
     /** @var AbstractEnumValue[] $values */
     $values = $enumRepo->createQueryBuilder('o')->getQuery()->setHint(TranslatableListener::HINT_TRANSLATABLE_LOCALE, $locale)->getResult();
     $ids = [];
     /** @var AbstractEnumValue[] $changes */
     $changes = [];
     foreach ($values as $value) {
         $id = $value->getId();
         $optionKey = $this->getEnumOptionKey($id, $options);
         if ($optionKey !== null) {
             $ids[] = $id;
             if ($this->setEnumValueProperties($value, $options[$optionKey])) {
                 $changes[] = $value;
             }
             unset($options[$optionKey]);
         } else {
             $em->remove($value);
             $changes[] = $value;
         }
     }
     foreach ($options as $option) {
         $id = $this->generateEnumValueId($option['label'], $ids);
         $ids[] = $id;
         $value = $enumRepo->createEnumValue($option['label'], $option['priority'], $option['is_default'], $id);
         $em->persist($value);
         $changes[] = $value;
     }
     if (!empty($changes)) {
         if ($locale !== Translation::DEFAULT_LOCALE) {
             foreach ($changes as $value) {
                 $value->setLocale($locale);
             }
         }
         $em->flush($changes);
         // mark translation cache dirty
         $this->dbTranslationMetadataCache->updateTimestamp($locale);
     }
 }
Esempio n. 5
0
 /**
  * @param FormEvent $event
  */
 public function postSubmit(FormEvent $event)
 {
     $form = $event->getForm();
     $configModel = $form->getConfig()->getOption('config_model');
     $data = $event->getData();
     $labelsToBeUpdated = [];
     foreach ($this->configManager->getProviders() as $provider) {
         $scope = $provider->getScope();
         if (isset($data[$scope])) {
             $configId = $this->configManager->getConfigIdByModel($configModel, $scope);
             $config = $provider->getConfigById($configId);
             $translatable = $provider->getPropertyConfig()->getTranslatableValues($configId);
             foreach ($data[$scope] as $code => $value) {
                 if (in_array($code, $translatable, true)) {
                     // check if a label text was changed
                     $labelKey = $config->get($code);
                     if (!$configModel->getId()) {
                         $labelsToBeUpdated[$labelKey] = $value;
                     } elseif ($value != $this->translator->trans($labelKey)) {
                         $labelsToBeUpdated[$labelKey] = $value;
                     }
                     // replace label text with label name in $value variable
                     $value = $config->get($code);
                 }
                 $config->set($code, $value);
             }
             $this->configManager->persist($config);
         }
     }
     if ($form->isValid()) {
         // update changed labels if any
         if (!empty($labelsToBeUpdated)) {
             /** @var EntityManager $translationEm */
             $translationEm = $this->doctrine->getManagerForClass(Translation::ENTITY_NAME);
             /** @var TranslationRepository $translationRepo */
             $translationRepo = $translationEm->getRepository(Translation::ENTITY_NAME);
             $values = [];
             $locale = $this->translator->getLocale();
             foreach ($labelsToBeUpdated as $labelKey => $labelText) {
                 // save into translation table
                 $values[] = $translationRepo->saveValue($labelKey, $labelText, $locale, TranslationRepository::DEFAULT_DOMAIN, Translation::SCOPE_UI);
             }
             // mark translation cache dirty
             $this->dbTranslationMetadataCache->updateTimestamp($locale);
             $translationEm->flush($values);
         }
         $this->configManager->flush();
     }
 }
 protected function assertTranslationServicesCalled()
 {
     $this->translator->expects($this->once())->method('getLocale')->willReturn(self::LOCALE);
     $this->translationCache->expects($this->once())->method('updateTimestamp')->with(self::LOCALE);
 }
 /**
  * {@inheritdoc}
  */
 public function isFresh($timestamp)
 {
     $cacheTimestamp = $this->metadataCache->getTimestamp($this->locale);
     return $cacheTimestamp === false || $cacheTimestamp < $timestamp;
 }