atiweb/src/EventSubscriber/TransSubscriber.php line 77

Open in your IDE?
  1. <?php
  2. namespace Atiweb\EventSubscriber;
  3. use App\Entity\Www\Textes;
  4. use Atiweb\Form\TextareaTransType;
  5. use Atiweb\Form\TextTransType;
  6. use Atiweb\Form\TinyMceTransType;
  7. use Atiweb\Form\TinyMceType;
  8. use Atiweb\Translation\Translation;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  12. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  13. use Symfony\Component\Form\FormEvent;
  14. use Symfony\Component\Form\FormEvents;
  15. /**
  16.  * Class TransSubscriber
  17.  * @package Atiweb\Translation
  18.  */
  19. class TransSubscriber implements EventSubscriberInterface
  20. {
  21.     private $_locales;
  22.     private $_translationService;
  23.     private $arrayKeyTrans = [];
  24.     private $em;
  25.     private $translationEntities = [];
  26.     /**
  27.      * TransSubscriber constructor.
  28.      * @param Translation $translationService
  29.      * @param array $locales
  30.      * @param EntityManagerInterface $em
  31.      */
  32.     public function __construct(
  33.         Translation $translationService,
  34.         array $locales,
  35.         EntityManagerInterface $em
  36.     ) {
  37.         $this->_translationService $translationService;
  38.         $this->_locales $locales;
  39.         $this->em $em;
  40.     }
  41.     /**
  42.      * @param $property
  43.      * @param $keyTrans
  44.      */
  45.     public function setKeyTrans($property$keyTrans) {
  46.         $this->arrayKeyTrans[$property] = $keyTrans;
  47.     }
  48.     /**
  49.      * @param $property
  50.      * @param Textes $translationEntity
  51.      */
  52.     public function setTranslationEntity($propertyTextes $translationEntity) {
  53.         $this->translationEntities[$property] = $translationEntity;
  54.     }
  55.     /**
  56.      * @return string[]
  57.      */
  58.     public static function getSubscribedEvents()
  59.     {
  60.         return [
  61.             FormEvents::PRE_SET_DATA => 'preSetData',
  62.             FormEvents::POST_SUBMIT => 'postSubmit',
  63.         ];
  64.     }
  65.     /**
  66.      * @param FormEvent $event
  67.      */
  68.     public function preSetData(FormEvent $event)
  69.     {
  70.         if($event->getData()) {
  71.             $form $event->getForm();
  72.             foreach ($form as $propertyName => $propertyInfo) {
  73.                 $isTrans $this->_translationService->isTrans($event->getData(), $propertyName);
  74.                 if ($isTrans and !array_key_exists($propertyName$this->arrayKeyTrans)) {
  75.                     if($propertyInfo->getConfig()->getOption("locale") !== null) {
  76.                         continue;
  77.                     }
  78.                     $this->setKeyTrans($propertyName$event->getData()->{'get' ucfirst($propertyName)}());
  79.                     $translationEntity $event->getData()->{'get' ucfirst($propertyName)}();
  80.                     if(
  81.                         $propertyInfo->getConfig()->getType()->getInnerType() instanceof TextareaTransType
  82.                         or $propertyInfo->getConfig()->getType()->getInnerType() instanceof TextareaType
  83.                     ) {
  84.                         $entryType TextareaTransType::class;
  85.                     } else if (
  86.                         $propertyInfo->getConfig()->getType()->getInnerType() instanceof TinyMceType
  87.                         or $propertyInfo->getConfig()->getType()->getInnerType() instanceof TinyMceTransType
  88.                     ) {
  89.                         $entryType TinyMceTransType::class;
  90.                     }
  91.                     else  {
  92.                         $entryType TextTransType::class;
  93.                     }
  94.                     $data = [];
  95.                     // WYSIWYG options
  96.                     $options = [];
  97.                     if(null !== $form->get($propertyName)->getConfig()->getAttribute('data_collector/passed_options')['config_name']) {
  98.                         $options["config_name"] = $form->get($propertyName)->getConfig()->getAttribute('data_collector/passed_options')['config_name'];
  99.                     }
  100.                     if(null !== $form->get($propertyName)->getConfig()->getAttribute('data_collector/passed_options')['config']) {
  101.                         $options["config"] = $form->get($propertyName)->getConfig()->getAttribute('data_collector/passed_options')['config'];
  102.                     }
  103.                     if(null !== $form->get($propertyName)->getConfig()->getAttribute('data_collector/passed_options')['attr']) {
  104.                         $options["attr"] = $form->get($propertyName)->getConfig()->getAttribute('data_collector/passed_options')['attr'];
  105.                     }
  106.                     foreach ($this->_locales as $lang) {
  107.                         if($lang === 'fr') { // Needed (mapped:false) to move field to translation:field
  108.                             $form->add($propertyName$entryTypearray_merge([
  109.                                 'data' => $translationEntity $translationEntity->{'get' ucfirst($lang)}() : '',
  110.                                 'mapped' => false,
  111.                                 'constraints' => $form->get($propertyName)->getConfig()->getAttribute('data_collector/passed_options')['constraints'],
  112.                                 'required' => $form->get($propertyName)->getConfig()->getAttribute('data_collector/passed_options')['required'],
  113.                             ], $options));
  114.                         } else {
  115.                             $data[] = $translationEntity $translationEntity->{'get' ucfirst($lang)}() : '';
  116.                         }
  117.                     }
  118.                     $form->add($propertyName '_translation'CollectionType::class, [
  119.                         'entry_type' => $entryType,
  120.                         'data' => $data,
  121.                         'mapped' => false,
  122.                         'required' => false,
  123.                         'constraints' => [], // TODO: send param if legacy constaints about realField
  124.                     ]);
  125.                 }
  126.             }
  127.         }
  128.     }
  129.     /**
  130.      * @param FormEvent $event
  131.      * @return null
  132.      */
  133.     public function postSubmit(FormEvent $event// Is called for each field !!!
  134.     {
  135.         foreach($this->arrayKeyTrans as $propertyKeyTrans => $valueKeyTrans) {
  136.             $form $event->getForm();
  137.             if ($form and ($form->getName() == $propertyKeyTrans or $form->getName() == ($propertyKeyTrans '_translation'))) {
  138.                 $realFieldName str_replace('_translation'''$form->getName());
  139.                 if(array_key_exists($realFieldName$this->translationEntities)) {
  140.                     $translationEntity $this->translationEntities[$realFieldName];
  141.                 }
  142.                 else {
  143.                     if($valueKeyTrans == '') {
  144.                         $translationEntity = new Textes();
  145.                     } else {
  146.                         $translationEntity $valueKeyTrans ?? new Textes();
  147.                     }
  148.                     $this->em->persist($translationEntity); // Not needed if field => cascade={"persist"}
  149.                     $this->setTranslationEntity($propertyKeyTrans$translationEntity);
  150.                 }
  151.                 if($form->getName() == $propertyKeyTrans) { // If current = untranslatable field
  152.                     $translationEntity->setFr($form->getData() ?? '');
  153.                     try {
  154.                         $event->getForm()->getParent()->getData()->{'set' ucfirst($propertyKeyTrans)}($translationEntity);
  155.                     } catch (\Error $e) {
  156.                         return null;
  157.                     }
  158.                 } else {
  159.                     $loop 0;
  160.                     foreach ($this->_locales as $k => $lang) {
  161.                         if($lang !== 'fr') {
  162.                             $translationEntity->{'set' $lang}($form->getData()[$loop] ?? '');
  163.                             $loop++;
  164.                         }
  165.                     }
  166.                 }
  167.             }
  168.         }
  169.     }
  170. }