<?php
namespace Products\NotificationsBundle\Form\Forms\Messages;
use Products\NotificationsBundle\Entity\Notifications\Translations\Translation;
use Products\NotificationsBundle\Form\Type\ReactVoiceRecorderType;
use Products\NotificationsBundle\Form\Type\RichTextType;
use Products\NotificationsBundle\Form\Type\SourceTextType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;
/**
* Class MessageTranslationForm
* @package Products\NotificationsBundle\Form\Forms\Messages
*/
class MessageTranslationForm extends AbstractType
{
/**
* {@inheritDoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$translation = $event->getData();
if ( ! $translation instanceof Translation) {
throw new \LogicException();
}
$event->getForm()
->add('title', TextType::class, [
'required' => false,
'label_tooltip' => true,
'label' => 'message_translation_form.labels.title',
])
->add('description', $translation->isHtml() ? SourceTextType::class : RichTextType::class, [
'required' => false,
'label_tooltip' => true,
'label' => 'message_translation_form.labels.description',
]);
if ($translation->supportsVoice()) {
$event->getForm()
->add('script', TextareaType::class, [
'required' => false,
'label_tooltip' => true,
'label' => 'message_translation_form.labels.script',
'attr' => [
'rows' => 4,
],
'constraints' => [
new NotBlank(
['message' => 'Please add a voice transcription for both voice message generation and translation services to be performed.']
),
],
])
->add('recording', ReactVoiceRecorderType::class, [
'required' => true,
'label_tooltip' => false,
'label' => false,
'attr' => [
'class' => 'translation-recorder',
'data-locale' => $translation->getLocale(),
],
'constraints' => [
new NotBlank(
['message' => 'Please convert Voice Transcription to speech or add voice recording from phone or browser.']
),
],
'locale' => $translation->getLocale(),
]);
}
});
}
/**
* {@inheritDoc}
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Translation::class,
'translation_domain' => 'forms',
]);
}
}