| 1: | <?php |
| 2: | namespace TIC\MailBundle\Form; |
| 3: | |
| 4: | use TIC\CoreBundle\Base\TICFormType as BaseType; |
| 5: | |
| 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: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | class TemplateType extends BaseType |
| 35: | { |
| 36: | |
| 37: | |
| 38: | |
| 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'], |
| 50: | 'locales' => [], |
| 51: | 'deflocale' => 'fr', |
| 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: | |
| 73: | |
| 74: | public function buildView(FormView $view, FormInterface $form, array $options) |
| 75: | { |
| 76: | if (empty($options['locales'])) return; |
| 77: | |
| 78: | $view->vars['paging'] = '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: | |
| 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: | |
| 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: | |
| 111: | )) |
| 112: | ->add('target', TypeAheadType::class, array( |
| 113: | 'label' => true, |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | 'required' => false, |
| 119: | )) |
| 120: | ->add('sender', StringType::class, array( |
| 121: | 'label' => true, |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | 'required' => false, |
| 127: | )) |
| 128: | ->add('return', StringType::class, array( |
| 129: | 'label' => true, |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 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: | |
| 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: | |
| 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: | |
| 165: | 'label' => ' ', |
| 166: | |
| 167: | 'attr' => array('class'=>'alert alert-info'), |
| 168: | |
| 169: | |
| 170: | |
| 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: | |
| 180: | |
| 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: | |
| 189: | |
| 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: | |
| 198: | |
| 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: | |
| 208: | if (isset($item) && $item->getRef()) $form->add('ref', StringType::class, array( |
| 209: | 'label' => true, |
| 210: | |
| 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: | |
| 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: | |
| 231: | |
| 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: | |
| 240: | |
| 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: | |
| 249: | |
| 250: | 'attr' => ['class' => "ticmail-input", 'rows' => 3], |
| 251: | 'required' => false, |
| 252: | )) |
| 253: | ; |
| 254: | } |
| 255: | } |
| 256: | |
| 257: | } |
| 258: | |