src/Products/NotificationsBundle/Form/Forms/Messages/MessageTranslationForm.php line 22

Open in your IDE?
  1. <?php
  2. namespace Products\NotificationsBundle\Form\Forms\Messages;
  3. use Products\NotificationsBundle\Entity\Notifications\Translations\Translation;
  4. use Products\NotificationsBundle\Form\Type\ReactVoiceRecorderType;
  5. use Products\NotificationsBundle\Form\Type\RichTextType;
  6. use Products\NotificationsBundle\Form\Type\SourceTextType;
  7. use Symfony\Component\Form\AbstractType;
  8. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  9. use Symfony\Component\Form\Extension\Core\Type\TextType;
  10. use Symfony\Component\Form\FormBuilderInterface;
  11. use Symfony\Component\Form\FormEvent;
  12. use Symfony\Component\Form\FormEvents;
  13. use Symfony\Component\OptionsResolver\OptionsResolver;
  14. use Symfony\Component\Validator\Constraints\NotBlank;
  15. /**
  16.  * Class MessageTranslationForm
  17.  * @package Products\NotificationsBundle\Form\Forms\Messages
  18.  */
  19. class MessageTranslationForm extends AbstractType
  20. {
  21.     /**
  22.      * {@inheritDoc}
  23.      */
  24.     public function buildForm(FormBuilderInterface $builder, array $options): void
  25.     {
  26.         $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
  27.             $translation $event->getData();
  28.             if ( ! $translation instanceof Translation) {
  29.                 throw new \LogicException();
  30.             }
  31.             $event->getForm()
  32.                 ->add('title'TextType::class, [
  33.                     'required' => false,
  34.                     'label_tooltip' => true,
  35.                     'label' => 'message_translation_form.labels.title',
  36.                 ])
  37.                 ->add('description'$translation->isHtml() ? SourceTextType::class : RichTextType::class, [
  38.                     'required' => false,
  39.                     'label_tooltip' => true,
  40.                     'label' => 'message_translation_form.labels.description',
  41.                 ]);
  42.             if ($translation->supportsVoice()) {
  43.                 $event->getForm()
  44.                     ->add('script'TextareaType::class, [
  45.                         'required' => false,
  46.                         'label_tooltip' => true,
  47.                         'label' => 'message_translation_form.labels.script',
  48.                         'attr' => [
  49.                             'rows' => 4,
  50.                         ],
  51.                         'constraints' => [
  52.                             new NotBlank(
  53.                                 ['message' => 'Please add a voice transcription for both voice message generation and translation services to be performed.']
  54.                             ),
  55.                         ],
  56.                     ])
  57.                     ->add('recording'ReactVoiceRecorderType::class, [
  58.                         'required' => true,
  59.                         'label_tooltip' => false,
  60.                         'label' => false,
  61.                         'attr' => [
  62.                             'class' => 'translation-recorder',
  63.                             'data-locale' => $translation->getLocale(),
  64.                         ],
  65.                         'constraints' => [
  66.                             new NotBlank(
  67.                                 ['message' => 'Please convert Voice Transcription to speech or add voice recording from phone or browser.']
  68.                             ),
  69.                         ],
  70.                         'locale' => $translation->getLocale(),
  71.                     ]);
  72.             }
  73.         });
  74.     }
  75.     /**
  76.      * {@inheritDoc}
  77.      */
  78.     public function configureOptions(OptionsResolver $resolver): void
  79.     {
  80.         $resolver->setDefaults([
  81.             'data_class' => Translation::class,
  82.             'translation_domain' => 'forms',
  83.         ]);
  84.     }
  85. }