Source of file TemplateType.php

Size: 8,843 Bytes - Last Modified: 2023-11-16T22:56:03+01:00

/home/websites/teicee/packagist/site/phpdoc/conf/../vendor/teicee/mail-bundle/src/Form/TemplateType.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
<?php
namespace TIC\MailBundle\Form;

use TIC\CoreBundle\Base\TICFormType as BaseType;
#use Symfony\Component\Form\AbstractType as BaseType;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Intl\Intl;

use TIC\MailBundle\Entity\Template as Entity;

use Symfony\Component\Form\Extension\Core\Type\TextType as StringType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType as EditorType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType as TypeAheadType;
use Symfony\Component\Form\Extension\Core\Type\TextType as PlainType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType as BooleanType;

#use TIC\FormBundle\Form\Type\BooleanType;
#use TIC\FormBundle\Form\Type\ChoiceType;
#use TIC\FormBundle\Form\Type\EditorType;
#use TIC\FormBundle\Form\Type\PlainType;
#use TIC\FormBundle\Form\Type\StringType;
#use TIC\FormBundle\Form\Type\TypeAheadType;

/**
 * Formulaire de création/édition d'un modèle de notification.
 */
class TemplateType extends BaseType
{

	/**
	 * {@inheritdoc}
	 */
	public function configureOptions(OptionsResolver $resolver)
	{
		$resolver->setDefaults(array(
			'data_class' => Entity::class,
			'list_refs'  => [],
			'admins'     => [],
			'return'     => '',
			'sender'     => '',
			'targets'    => [],
			'formats'    => ['HTML','TEXT'],    // HTML, TEXT ou SMS
			'locales'    => [],                 // vide ou null si aucune gestion multilingue
			'deflocale'  => 'fr',               // locale des contenus par défaut (hors TemplateTranslations)
		));
		
		$resolver->setAllowedTypes('list_refs', ['array']);
		$resolver->setAllowedTypes('admins',    ['array']);
		$resolver->setAllowedTypes('return',    ['string']);
		$resolver->setAllowedTypes('sender',    ['string']);
		$resolver->setAllowedTypes('targets',   ['array']);
		$resolver->setAllowedTypes('formats',   ['array']);
		$resolver->setAllowedTypes('locales',   ['array','null']);
		$resolver->setAllowedTypes('deflocale', ['string']);
		
		$resolver->setAllowedValues('formats', function($value){
			if (empty($value)) return false;
			if (! \is_array($value)) return false;
			foreach ($value as $v) if (! \in_array($v, ['HTML','TEXT','SMS'])) return false;
			return true;
		});
	}

	/**
	 * {@inheritdoc}
	 */
	public function buildView(FormView $view, FormInterface $form, array $options)
	{
		if (empty($options['locales'])) return;
		
		$view->vars['paging'] = 'tabs';  // 'none' | 'fieldset' | 'collapse' | 'tabs'
		$view->vars['groups'] = array('main' => ['ref','label','target','sender','return','bccAdmins']);
		$view->vars['labels'] = array('main' => 'ticmail.template.group.main');
		
		foreach ($options['locales'] as $locale) {
			$suffix = ($locale == $options['deflocale']) ? '' : '_' . $locale;
			$view->vars['labels']['text'.$suffix] = \ucfirst(Intl::getLocaleBundle()->getLocaleName($locale));
			$view->vars['groups']['text'.$suffix] = array('subject'.$suffix);
			foreach ($options['formats'] as $format) {
				$view->vars['groups']['text'.$suffix][] = 'body' . \ucfirst(\strtolower($format)) . $suffix;
			}
		}
		$view->vars['groups']['text'][] = 'bodyHelp';
		$view->vars['labels']['text'].= " (default)";
	}

	/**
	 * {@inheritdoc}
	 */
	public function buildForm(FormBuilderInterface $builder, array $options)
	{
		$body_required = (\count($options['formats']) > 1) ? false : true;
		$builder
			->add('ref', ChoiceType::class, array(
				'label'      => true,
#				'attr'       => array('icon'       => 'barcode'),
				'placeholder'=> 'ticmail.template.ph.ref',
				'choices'    => $options['list_refs'],
				'choice_translation_domain' => false,
			))
			->add('label', StringType::class, array(
				'label'      => true,
#				'attr'       => array('icon'       => 'info-circle'),
			))
			->add('target', TypeAheadType::class, array(
				'label'      => true,
#				'prefix'     => '<fa-tags>',
#				'choices'    => $options['targets'],
#				'minLength'  => 0,
#				'maxDisplay' => false,
				'required'   => false,
			))
			->add('sender', StringType::class, array(
				'label'      => true,
#				'mailto'     => true,
#				'prefix'     => '<fa-at>',
#				'suffix'     => \sprintf('default: %s', $options['sender']),
#				'suffix_attr'=> array('style'=>'width:45%;'),
				'required'   => false,
			))
			->add('return', StringType::class, array(
				'label'      => true,
#				'mailto'     => true,
#				'prefix'     => '<fa-at>',
#				'suffix'     => \sprintf('default: %s', $options['return']),
#				'suffix_attr'=> array('style'=>'width:45%;'),
				'required'   => false,
			))
			->add('bccAdmins', BooleanType::class, array(
				'label'      => true,
				'required'   => false,
			))
			->add('bccMore', StringType::class, array(
				'label'      => true,
#				'prefix'     => '<fa-at>',
				'required'   => false,
			))
			->add('enabled', BooleanType::class, array(
				'label'      => true,
				'required'   => false,
			))
			->add('subject', StringType::class, array(
				'label'      => true,
#				'prefix'     => \sprintf('[%s]', \strtoupper($options['deflocale'])),
				'attr'       => ['class' => "ticmail-input"],
				'required'   => true,
			))
		;
		
		if (! $body_required) {
			$mesgtype = '';
			if (\in_array('HTML', $options['formats'])) $mesgtype.= 'h';
			if (\in_array('TEXT', $options['formats'])) $mesgtype.= 't';
			if (\in_array('SMS',  $options['formats'])) $mesgtype.= 's';
			
			$builder->add('bodyHelp', PlainType::class, array(
#				'prefix'     => '<fa-exclamation-triangle>',
				'label'      => ' ',
#				'label'      => 'ticmail.template.help.body_'.$mesgtype,
				'attr'       => array('class'=>'alert alert-info'),
#				'content'    => 'ticmail.template.help.body_'.$mesgtype,
#				'trans'      => true,
#				'html'       => true,
				'mapped'     => false,
				'required'   => false,
				'data'       => \strip_tags($this->trans('ticmail.template.help.body_'.$mesgtype)),
			));
		}
		if (\in_array('HTML', $options['formats'])) $builder
			->add('bodyHtml', EditorType::class, array(
				'label'      => true,
#				'wysiwyg'    => true,
#				'rows'       => 12,
				'attr'       => ['class' => "ticmail-input", 'rows' => 12],
				'required'   => $body_required,
			))
		;
		if (\in_array('TEXT', $options['formats'])) $builder
			->add('bodyText', EditorType::class, array(
				'label'      => true,
#				'wysiwyg'    => false,
#				'rows'       => 12,
				'attr'       => ['class' => "ticmail-input", 'rows' => 12],
				'required'   => $body_required,
			))
		;
		if (\in_array('SMS', $options['formats'])) $builder
			->add('bodySms', EditorType::class, array(
				'label'      => true,
#				'wysiwyg'    => false,
#				'rows'       => 3,
				'attr'       => ['class' => "ticmail-input", 'rows' => 3],
				'required'   => $body_required,
			))
		;
		
		$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $e) {
				$item = $e->getData();
				$form = $e->getForm();
				// ref non modifiable après création
				if (isset($item) && $item->getRef()) $form->add('ref', StringType::class, array(
					'label'      => true,
#					'prefix'     => '<fa-barcode>',
					'disabled'   => true,
					'required'   => false,
				));
			})
		;
		
		if (\is_array($options['locales'])) foreach ($options['locales'] as $locale) {
			if ($locale == $options['deflocale']) continue;
			$builder
				->add('subject_'.$locale, StringType::class, array(
					'label'      => 'ticmail.template.label.subject',
#					'prefix'     => \sprintf('[%s]', \strtoupper($locale)),
					'attr'       => ['class' => "ticmail-input"],
					'required'   => false,
				))
			;
			if (\in_array('HTML', $options['formats'])) $builder
				->add('bodyHtml_'.$locale, EditorType::class, array(
					'label'      => 'ticmail.template.label.bodyHtml',
#					'wysiwyg'    => true,
#					'rows'       => 12,
					'attr'       => ['class' => "ticmail-input", 'rows' => 12],
					'required'   => false,
				))
			;
			if (\in_array('TEXT', $options['formats'])) $builder
				->add('bodyText_'.$locale, EditorType::class, array(
					'label'      => 'ticmail.template.label.bodyText',
#					'wysiwyg'    => false,
#					'rows'       => 12,
					'attr'       => ['class' => "ticmail-input", 'rows' => 12],
					'required'   => false,
				))
			;
			if (\in_array('SMS', $options['formats'])) $builder
				->add('bodySms_'.$locale, EditorType::class, array(
					'label'      => 'ticmail.template.label.bodySms',
#					'wysiwyg'    => false,
#					'rows'       => 3,
					'attr'       => ['class' => "ticmail-input", 'rows' => 3],
					'required'   => false,
				))
			;
		}
	}

}