1: <?php
2: namespace TIC\MailBundle\Form;
3:
4: use TIC\CoreBundle\Base\TICFormType as BaseType;
5: #use Symfony\Component\Form\AbstractType as BaseType;
6:
7: use Symfony\Component\Form\FormBuilderInterface;
8: use Symfony\Component\Form\FormInterface;
9: use Symfony\Component\Form\FormView;
10: use Symfony\Component\Form\FormEvents;
11: use Symfony\Component\Form\FormEvent;
12: use Symfony\Component\OptionsResolver\OptionsResolver;
13: use Symfony\Component\Intl\Intl;
14:
15: use TIC\MailBundle\Entity\Template as Entity;
16:
17: use Symfony\Component\Form\Extension\Core\Type\TextType as StringType;
18: use Symfony\Component\Form\Extension\Core\Type\TextareaType as EditorType;
19: use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
20: use Symfony\Component\Form\Extension\Core\Type\TextType as TypeAheadType;
21: use Symfony\Component\Form\Extension\Core\Type\TextType as PlainType;
22: use Symfony\Component\Form\Extension\Core\Type\CheckboxType as BooleanType;
23:
24: #use TIC\FormBundle\Form\Type\BooleanType;
25: #use TIC\FormBundle\Form\Type\ChoiceType;
26: #use TIC\FormBundle\Form\Type\EditorType;
27: #use TIC\FormBundle\Form\Type\PlainType;
28: #use TIC\FormBundle\Form\Type\StringType;
29: #use TIC\FormBundle\Form\Type\TypeAheadType;
30:
31: /**
32: * Formulaire de création/édition d'un modèle de notification.
33: */
34: class TemplateType extends BaseType
35: {
36:
37: /**
38: * {@inheritdoc}
39: */
40: public function configureOptions(OptionsResolver $resolver)
41: {
42: $resolver->setDefaults(array(
43: 'data_class' => Entity::class,
44: 'list_refs' => [],
45: 'admins' => [],
46: 'return' => '',
47: 'sender' => '',
48: 'targets' => [],
49: 'formats' => ['HTML','TEXT'], // HTML, TEXT ou SMS
50: 'locales' => [], // vide ou null si aucune gestion multilingue
51: 'deflocale' => 'fr', // locale des contenus par défaut (hors TemplateTranslations)
52: ));
53:
54: $resolver->setAllowedTypes('list_refs', ['array']);
55: $resolver->setAllowedTypes('admins', ['array']);
56: $resolver->setAllowedTypes('return', ['string']);
57: $resolver->setAllowedTypes('sender', ['string']);
58: $resolver->setAllowedTypes('targets', ['array']);
59: $resolver->setAllowedTypes('formats', ['array']);
60: $resolver->setAllowedTypes('locales', ['array','null']);
61: $resolver->setAllowedTypes('deflocale', ['string']);
62:
63: $resolver->setAllowedValues('formats', function($value){
64: if (empty($value)) return false;
65: if (! \is_array($value)) return false;
66: foreach ($value as $v) if (! \in_array($v, ['HTML','TEXT','SMS'])) return false;
67: return true;
68: });
69: }
70:
71: /**
72: * {@inheritdoc}
73: */
74: public function buildView(FormView $view, FormInterface $form, array $options)
75: {
76: if (empty($options['locales'])) return;
77:
78: $view->vars['paging'] = 'tabs'; // 'none' | 'fieldset' | 'collapse' | 'tabs'
79: $view->vars['groups'] = array('main' => ['ref','label','target','sender','return','bccAdmins']);
80: $view->vars['labels'] = array('main' => 'ticmail.template.group.main');
81:
82: foreach ($options['locales'] as $locale) {
83: $suffix = ($locale == $options['deflocale']) ? '' : '_' . $locale;
84: $view->vars['labels']['text'.$suffix] = \ucfirst(Intl::getLocaleBundle()->getLocaleName($locale));
85: $view->vars['groups']['text'.$suffix] = array('subject'.$suffix);
86: foreach ($options['formats'] as $format) {
87: $view->vars['groups']['text'.$suffix][] = 'body' . \ucfirst(\strtolower($format)) . $suffix;
88: }
89: }
90: $view->vars['groups']['text'][] = 'bodyHelp';
91: $view->vars['labels']['text'].= " (default)";
92: }
93:
94: /**
95: * {@inheritdoc}
96: */
97: public function buildForm(FormBuilderInterface $builder, array $options)
98: {
99: $body_required = (\count($options['formats']) > 1) ? false : true;
100: $builder
101: ->add('ref', ChoiceType::class, array(
102: 'label' => true,
103: # 'attr' => array('icon' => 'barcode'),
104: 'placeholder'=> 'ticmail.template.ph.ref',
105: 'choices' => $options['list_refs'],
106: 'choice_translation_domain' => false,
107: ))
108: ->add('label', StringType::class, array(
109: 'label' => true,
110: # 'attr' => array('icon' => 'info-circle'),
111: ))
112: ->add('target', TypeAheadType::class, array(
113: 'label' => true,
114: # 'prefix' => '<fa-tags>',
115: # 'choices' => $options['targets'],
116: # 'minLength' => 0,
117: # 'maxDisplay' => false,
118: 'required' => false,
119: ))
120: ->add('sender', StringType::class, array(
121: 'label' => true,
122: # 'mailto' => true,
123: # 'prefix' => '<fa-at>',
124: # 'suffix' => \sprintf('default: %s', $options['sender']),
125: # 'suffix_attr'=> array('style'=>'width:45%;'),
126: 'required' => false,
127: ))
128: ->add('return', StringType::class, array(
129: 'label' => true,
130: # 'mailto' => true,
131: # 'prefix' => '<fa-at>',
132: # 'suffix' => \sprintf('default: %s', $options['return']),
133: # 'suffix_attr'=> array('style'=>'width:45%;'),
134: 'required' => false,
135: ))
136: ->add('bccAdmins', BooleanType::class, array(
137: 'label' => true,
138: 'required' => false,
139: ))
140: ->add('bccMore', StringType::class, array(
141: 'label' => true,
142: # 'prefix' => '<fa-at>',
143: 'required' => false,
144: ))
145: ->add('enabled', BooleanType::class, array(
146: 'label' => true,
147: 'required' => false,
148: ))
149: ->add('subject', StringType::class, array(
150: 'label' => true,
151: # 'prefix' => \sprintf('[%s]', \strtoupper($options['deflocale'])),
152: 'attr' => ['class' => "ticmail-input"],
153: 'required' => true,
154: ))
155: ;
156:
157: if (! $body_required) {
158: $mesgtype = '';
159: if (\in_array('HTML', $options['formats'])) $mesgtype.= 'h';
160: if (\in_array('TEXT', $options['formats'])) $mesgtype.= 't';
161: if (\in_array('SMS', $options['formats'])) $mesgtype.= 's';
162:
163: $builder->add('bodyHelp', PlainType::class, array(
164: # 'prefix' => '<fa-exclamation-triangle>',
165: 'label' => ' ',
166: # 'label' => 'ticmail.template.help.body_'.$mesgtype,
167: 'attr' => array('class'=>'alert alert-info'),
168: # 'content' => 'ticmail.template.help.body_'.$mesgtype,
169: # 'trans' => true,
170: # 'html' => true,
171: 'mapped' => false,
172: 'required' => false,
173: 'data' => \strip_tags($this->trans('ticmail.template.help.body_'.$mesgtype)),
174: ));
175: }
176: if (\in_array('HTML', $options['formats'])) $builder
177: ->add('bodyHtml', EditorType::class, array(
178: 'label' => true,
179: # 'wysiwyg' => true,
180: # 'rows' => 12,
181: 'attr' => ['class' => "ticmail-input", 'rows' => 12],
182: 'required' => $body_required,
183: ))
184: ;
185: if (\in_array('TEXT', $options['formats'])) $builder
186: ->add('bodyText', EditorType::class, array(
187: 'label' => true,
188: # 'wysiwyg' => false,
189: # 'rows' => 12,
190: 'attr' => ['class' => "ticmail-input", 'rows' => 12],
191: 'required' => $body_required,
192: ))
193: ;
194: if (\in_array('SMS', $options['formats'])) $builder
195: ->add('bodySms', EditorType::class, array(
196: 'label' => true,
197: # 'wysiwyg' => false,
198: # 'rows' => 3,
199: 'attr' => ['class' => "ticmail-input", 'rows' => 3],
200: 'required' => $body_required,
201: ))
202: ;
203:
204: $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $e) {
205: $item = $e->getData();
206: $form = $e->getForm();
207: // ref non modifiable après création
208: if (isset($item) && $item->getRef()) $form->add('ref', StringType::class, array(
209: 'label' => true,
210: # 'prefix' => '<fa-barcode>',
211: 'disabled' => true,
212: 'required' => false,
213: ));
214: })
215: ;
216:
217: if (\is_array($options['locales'])) foreach ($options['locales'] as $locale) {
218: if ($locale == $options['deflocale']) continue;
219: $builder
220: ->add('subject_'.$locale, StringType::class, array(
221: 'label' => 'ticmail.template.label.subject',
222: # 'prefix' => \sprintf('[%s]', \strtoupper($locale)),
223: 'attr' => ['class' => "ticmail-input"],
224: 'required' => false,
225: ))
226: ;
227: if (\in_array('HTML', $options['formats'])) $builder
228: ->add('bodyHtml_'.$locale, EditorType::class, array(
229: 'label' => 'ticmail.template.label.bodyHtml',
230: # 'wysiwyg' => true,
231: # 'rows' => 12,
232: 'attr' => ['class' => "ticmail-input", 'rows' => 12],
233: 'required' => false,
234: ))
235: ;
236: if (\in_array('TEXT', $options['formats'])) $builder
237: ->add('bodyText_'.$locale, EditorType::class, array(
238: 'label' => 'ticmail.template.label.bodyText',
239: # 'wysiwyg' => false,
240: # 'rows' => 12,
241: 'attr' => ['class' => "ticmail-input", 'rows' => 12],
242: 'required' => false,
243: ))
244: ;
245: if (\in_array('SMS', $options['formats'])) $builder
246: ->add('bodySms_'.$locale, EditorType::class, array(
247: 'label' => 'ticmail.template.label.bodySms',
248: # 'wysiwyg' => false,
249: # 'rows' => 3,
250: 'attr' => ['class' => "ticmail-input", 'rows' => 3],
251: 'required' => false,
252: ))
253: ;
254: }
255: }
256:
257: }
258: