Example #1
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     // Populate settings
     $cleanMasks = ['labelAttributes' => 'string', 'inputAttributes' => 'string', 'containerAttributes' => 'string', 'label' => 'strict_html'];
     $addHelpMessage = $addShowLabel = $allowCustomAlias = $addDefaultValue = $addLabelAttributes = $addInputAttributes = $addContainerAttributes = $addLeadFieldList = $addSaveResult = $addBehaviorFields = $addIsRequired = true;
     if (!empty($options['customParameters'])) {
         $type = 'custom';
         $customParams = $options['customParameters'];
         $formTypeOptions = ['required' => false, 'label' => false];
         if (!empty($customParams['formTypeOptions'])) {
             $formTypeOptions = array_merge($formTypeOptions, $customParams['formTypeOptions']);
         }
         $addFields = ['labelText', 'addHelpMessage', 'addShowLabel', 'labelText', 'addDefaultValue', 'addLabelAttributes', 'labelAttributesText', 'addInputAttributes', 'inputAttributesText', 'addContainerAttributes', 'containerAttributesText', 'addLeadFieldList', 'addSaveResult', 'addBehaviorFields', 'addIsRequired', 'addHtml'];
         foreach ($addFields as $f) {
             if (isset($customParams['builderOptions'][$f])) {
                 ${$f} = (bool) $customParams['builderOptions'][$f];
             }
         }
     } else {
         $type = $options['data']['type'];
         switch ($type) {
             case 'freetext':
                 $addHelpMessage = $addDefaultValue = $addIsRequired = $addLeadFieldList = $addSaveResult = $addBehaviorFields = false;
                 $labelText = 'mautic.form.field.form.header';
                 $showLabelText = 'mautic.form.field.form.showheader';
                 $inputAttributesText = 'mautic.form.field.form.freetext_attributes';
                 $labelAttributesText = 'mautic.form.field.form.header_attributes';
                 // Allow html
                 $cleanMasks['properties'] = 'html';
                 break;
             case 'button':
                 $addHelpMessage = $addShowLabel = $addDefaultValue = $addLabelAttributes = $addIsRequired = $addLeadFieldList = $addSaveResult = $addBehaviorFields = false;
                 break;
             case 'hidden':
                 $addHelpMessage = $addShowLabel = $addLabelAttributes = $addIsRequired = false;
                 break;
             case 'captcha':
                 $addShowLabel = $addIsRequired = $addDefaultValue = $addLeadFieldList = $addSaveResult = $addBehaviorFields = false;
                 break;
             case 'pagebreak':
                 $addShowLabel = $allowCustomAlias = $addHelpMessage = $addIsRequired = $addDefaultValue = $addLeadFieldList = $addSaveResult = $addBehaviorFields = false;
                 break;
             case 'email':
                 $addBehaviorFields = false;
                 break;
             case 'select':
                 $cleanMasks['properties']['list']['list']['label'] = 'strict_html';
                 break;
             case 'checkboxgrp':
             case 'radiogrp':
                 $cleanMasks['properties']['optionlist']['list']['label'] = 'strict_html';
                 break;
         }
     }
     // Build form fields
     $builder->add('label', 'text', ['label' => !empty($labelText) ? $labelText : 'mautic.form.field.form.label', 'label_attr' => ['class' => 'control-label'], 'attr' => ['class' => 'form-control'], 'constraints' => [new Assert\NotBlank(['message' => 'mautic.form.field.label.notblank'])]]);
     if ($allowCustomAlias) {
         $builder->add('alias', 'text', ['label' => 'mautic.form.field.form.alias', 'label_attr' => ['class' => 'control-label'], 'attr' => ['class' => 'form-control', 'tooltip' => 'mautic.form.field.form.alias.tooltip'], 'disabled' => !empty($options['data']['id']) && strpos($options['data']['id'], 'new') === false ? true : false, 'required' => false]);
     }
     if ($addShowLabel) {
         $default = !isset($options['data']['showLabel']) ? true : (bool) $options['data']['showLabel'];
         $builder->add('showLabel', 'yesno_button_group', ['label' => !empty($showLabelText) ? $showLabelText : 'mautic.form.field.form.showlabel', 'data' => $default]);
     }
     if ($addDefaultValue) {
         $builder->add('defaultValue', $type == 'textarea' ? 'textarea' : 'text', ['label' => 'mautic.core.defaultvalue', 'label_attr' => ['class' => 'control-label'], 'attr' => ['class' => 'form-control'], 'required' => false]);
     }
     if ($addHelpMessage) {
         $builder->add('helpMessage', 'text', ['label' => 'mautic.form.field.form.helpmessage', 'label_attr' => ['class' => 'control-label'], 'attr' => ['class' => 'form-control', 'tooltip' => 'mautic.form.field.help.helpmessage'], 'required' => false]);
     }
     if ($addIsRequired) {
         $default = !isset($options['data']['isRequired']) ? false : (bool) $options['data']['isRequired'];
         $builder->add('isRequired', 'yesno_button_group', ['label' => 'mautic.core.required', 'data' => $default]);
         $builder->add('validationMessage', 'text', ['label' => 'mautic.form.field.form.validationmsg', 'label_attr' => ['class' => 'control-label'], 'attr' => ['class' => 'form-control'], 'required' => false]);
     }
     if ($addLabelAttributes) {
         $builder->add('labelAttributes', 'text', ['label' => !empty($labelAttributesText) ? $labelAttributesText : 'mautic.form.field.form.labelattr', 'label_attr' => ['class' => 'control-label'], 'attr' => ['class' => 'form-control', 'tooltip' => 'mautic.form.field.help.attr', 'maxlength' => '255'], 'required' => false]);
     }
     if ($addInputAttributes) {
         $builder->add('inputAttributes', 'text', ['label' => !empty($inputAttributesText) ? $inputAttributesText : 'mautic.form.field.form.inputattr', 'label_attr' => ['class' => 'control-label'], 'attr' => ['class' => 'form-control', 'tooltip' => 'mautic.form.field.help.attr', 'maxlength' => '255'], 'required' => false]);
     }
     if ($addContainerAttributes) {
         $builder->add('containerAttributes', 'text', ['label' => !empty($containerAttributesText) ? $containerAttributesText : 'mautic.form.field.form.container_attr', 'label_attr' => ['class' => 'control-label'], 'attr' => ['class' => 'form-control', 'tooltip' => 'mautic.form.field.help.container_attr', 'maxlength' => '255'], 'required' => false]);
     }
     if ($addSaveResult) {
         $default = !isset($options['data']['saveResult']) || $options['data']['saveResult'] === null ? true : (bool) $options['data']['saveResult'];
         $builder->add('saveResult', 'yesno_button_group', ['label' => 'mautic.form.field.form.saveresult', 'data' => $default, 'attr' => ['tooltip' => 'mautic.form.field.help.saveresult']]);
     }
     if ($addBehaviorFields) {
         $default = !isset($options['data']['showWhenValueExists']) || $options['data']['showWhenValueExists'] === null ? true : (bool) $options['data']['showWhenValueExists'];
         $builder->add('showWhenValueExists', 'yesno_button_group', ['label' => 'mautic.form.field.form.show.when.value.exists', 'data' => $default, 'attr' => ['tooltip' => 'mautic.form.field.help.show.when.value.exists']]);
         $builder->add('showAfterXSubmissions', 'text', ['label' => 'mautic.form.field.form.show.after.x.submissions', 'label_attr' => ['class' => 'control-label'], 'attr' => ['class' => 'form-control', 'tooltip' => 'mautic.form.field.help.show.after.x.submissions'], 'required' => false]);
         $isAutoFillValue = !isset($options['data']['isAutoFill']) ? false : (bool) $options['data']['isAutoFill'];
         $builder->add('isAutoFill', 'yesno_button_group', ['label' => 'mautic.form.field.form.auto_fill', 'data' => $isAutoFillValue, 'attr' => ['class' => 'auto-fill-data', 'tooltip' => 'mautic.form.field.help.auto_fill']]);
     }
     if ($addLeadFieldList) {
         if (!isset($options['data']['leadField'])) {
             switch ($type) {
                 case 'email':
                     $data = 'email';
                     break;
                 case 'country':
                     $data = 'country';
                     break;
                 case 'tel':
                     $data = 'phone';
                     break;
                 default:
                     $data = '';
                     break;
             }
         } elseif (isset($options['data']['leadField'])) {
             $data = $options['data']['leadField'];
         } else {
             $data = '';
         }
         $builder->add('leadField', 'choice', ['choices' => $options['leadFields'], 'choice_attr' => function ($val, $key, $index) use($options) {
             if (!empty($options['leadFieldProperties'][$val]) && (in_array($options['leadFieldProperties'][$val]['type'], FormFieldHelper::getListTypes()) || !empty($options['leadFieldProperties'][$val]['properties']['list']) || !empty($options['leadFieldProperties'][$val]['properties']['optionlist']))) {
                 return ['data-list-type' => 1];
             }
             return [];
         }, 'label' => 'mautic.form.field.form.lead_field', 'label_attr' => ['class' => 'control-label'], 'attr' => ['class' => 'form-control', 'tooltip' => 'mautic.form.field.help.lead_field'], 'required' => false, 'data' => $data]);
     }
     $builder->add('type', 'hidden');
     $update = !empty($options['data']['id']) ? true : false;
     if (!empty($update)) {
         $btnValue = 'mautic.core.form.update';
         $btnIcon = 'fa fa-pencil';
     } else {
         $btnValue = 'mautic.core.form.add';
         $btnIcon = 'fa fa-plus';
     }
     $builder->add('buttons', 'form_buttons', ['save_text' => $btnValue, 'save_icon' => $btnIcon, 'apply_text' => false, 'container_class' => 'bottom-form-buttons']);
     $builder->add('formId', 'hidden', ['mapped' => false]);
     // Put properties last so that the other values are available to form events
     if (!empty($options['customParameters'])) {
         $builder->add('properties', $customParams['formType'], $formTypeOptions);
     } else {
         $propertiesData = isset($options['data']['properties']) ? $options['data']['properties'] : [];
         switch ($type) {
             case 'select':
             case 'country':
                 $builder->add('properties', 'formfield_select', ['field_type' => $type, 'label' => false, 'parentData' => $options['data'], 'data' => $propertiesData]);
                 break;
             case 'checkboxgrp':
             case 'radiogrp':
                 $builder->add('properties', 'formfield_group', ['label' => false, 'data' => $propertiesData]);
                 break;
             case 'freetext':
                 $builder->add('properties', 'formfield_text', ['required' => false, 'label' => false, 'editor' => true, 'data' => $propertiesData]);
                 break;
             case 'date':
             case 'email':
             case 'number':
             case 'tel':
             case 'text':
             case 'url':
                 $builder->add('properties', 'formfield_placeholder', ['label' => false, 'data' => $propertiesData]);
                 break;
             case 'captcha':
                 $builder->add('properties', 'formfield_captcha', ['label' => false, 'data' => $propertiesData]);
                 break;
             case 'pagebreak':
                 $builder->add('properties', FormFieldPageBreakType::class, ['label' => false, 'data' => $propertiesData]);
                 break;
         }
     }
     $builder->addEventSubscriber(new CleanFormSubscriber($cleanMasks));
     if (!empty($options['action'])) {
         $builder->setAction($options['action']);
     }
 }