File size: 10,538 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
{#
  Variables
    - mappedFields (\Mautic\FormBundle\Collection\MappedObjectCollection)
#}
{%- set defaultInputFormClass = 'not-chosen' -%}
{%- set defaultInputClass = 'selectbox' -%}
{%- set containerType = 'select' -%}

{# start: field_helper #}
{%- set defaultInputFormClass = defaultInputFormClass|default('') -%}
{%- set defaultLabelClass = defaultLabelClass|default('label') -%}
{%- set formName = formName|default('') -%}
{%- set defaultInputClass = 'mauticform-' ~ defaultInputClass -%}
{%- set defaultLabelClass = 'mauticform-' ~ defaultLabelClass -%}
{%- set containerClass = containerClass|default(containerType) -%}
{%- set order = field.order|default(0) -%}
{%- set validationMessage = '' -%}

{%- set inputAttributes = htmlAttributesStringToArray(field.inputAttributes|default('')) -%}
{%- set labelAttributes = htmlAttributesStringToArray(field.labelAttributes|default('')) -%}
{%- set containerAttributes = htmlAttributesStringToArray(field.containerAttributes|default('')) -%}

{%- if ignoreName is not defined or (ignoreName is defined and ignoreName is empty) -%}
  {%- set inputName = 'mauticform[' ~ field.alias ~ ']' -%}
  {%- if field.properties.multiple is defined and true == field.properties.multiple -%}
    {%- set inputName = inputName ~ '[]' -%}
  {%- endif -%}
  {%- set inputAttributes = inputAttributes|merge({
          'name': inputName,
  }) -%}
{%- endif -%}

{%- if field.type not in ['checkboxgrp', 'radiogrp', 'textarea'] -%}
  {%- set inputAttributes = inputAttributes|merge({
          'value': field.defaultValue|default(''),
  }) -%}
{%- endif -%}

{%- if ignoreId is not defined or (ignoreId is defined and ignoreId is empty) -%}
  {%- set inputAttributes = inputAttributes|merge({
          'id': 'mauticform_input' ~ formName ~ '_' ~ field.alias,
  }) -%}
  {%- set labelAttributes = labelAttributes|merge({
          'id': 'mauticform_label' ~ formName ~ '_' ~ field.alias,
          'for': 'mauticform_input' ~ formName ~ '_' ~ field.alias,
  }) -%}
{%- endif -%}

{%- if field.properties.placeholder is defined and field.properties.placeholder is not empty -%}
  {%- set inputAttributes = inputAttributes|merge({
          'placeholder': field.properties.placeholder,
  }) -%}
{%- endif -%}

{%- if inForm is defined and (true == inForm or inForm is not empty) -%}
  {%- if field.type in ['button', 'pagebreak'] -%}
    {%- set defaultInputFormClass = defaultInputFormClass ~ ' btn btn-default' -%}
  {%- endif -%}

  {%- set labelAttributes = labelAttributes|merge({
          'class': labelAttributes.class|default([])|merge([defaultLabelClass]),
  }) -%}
  {%- set inputAttributes = inputAttributes|merge({
          'disabled': 'disabled',
          'class': inputAttributes.class|default([])|merge([defaultInputClass, defaultInputFormClass]),
  }) -%}
{%- else -%}
  {%- set labelAttributes = labelAttributes|merge({
          'class': labelAttributes.class|default([])|merge([defaultLabelClass]),
  }) -%}
  {%- set inputAttributes = inputAttributes|merge({
          'class': inputAttributes.class|default([])|merge([defaultInputClass]),
  }) -%}
{%- endif -%}

{%- set containerAttributes = containerAttributes|merge({
        'id': 'mauticform' ~ formName ~ '_' ~ id,
        'class': containerAttributes.class|default([])|merge([
          'mauticform-row',
          'mauticform-' ~ containerClass,
          'mauticform-field-' ~ order,
        ]),
}) -%}
{%- if field.parent and fields[field.parent] is defined -%}
  {%- set values = field.conditions.values|join('|') -%}

  {%- if field.conditions.any != false and 'notIn' != field.conditions.expr -%}
    {%- set values = '*' -%}
  {%- endif -%}

  {%- set containerAttributes = containerAttributes|merge({
          'data-mautic-form-show-on': fields[field.parent].alias ~ ':' ~ values,
          'data-mautic-form-expr': field.conditions.expr,
          'class': containerAttributes.class|merge([
            'mauticform-field-hidden',
          ]),
  }) -%}
{%- endif -%}

{%- if field.isRequired is defined and field.isRequired -%}
  {%- set required = true -%}
  {%- set validationMessage = field.validationMessage -%}
  {%- if validationMessage is empty -%}
        {%- set validationMessage = 'mautic.form.field.generic.required'|trans([], 'validators', language|default(null)) -%}
  {%- endif -%}
  {%- set containerAttributes = containerAttributes|merge({
          'class': containerAttributes.class|default([])|merge([
            'mauticform-required',
          ]),
          'data-validate': field.alias,
          'data-validation-type': field.type,
  }) -%}
  {%- if field.properties.multiple is defined and field.properties.multiple != false -%}
    {%- set containerAttributes = containerAttributes|merge({
            'data-validate-multiple': 'true',
    }) -%}
  {%- endif -%}
{%- elseif required is defined and true == required -%}
  {# Forced to be required #}
  {%- set containerAttributes = containerAttributes|merge({
          'class': containerAttributes.class|default([])|merge([
            'mauticform-required',
          ]),
  }) -%}
{%- endif -%}

{# Setup list parsing #}
{%- if list is defined or field.properties.syncList is defined or field.properties.list is defined or field.properties.optionList is defined -%}
  {%- set parseList = [] -%}
  {%- set isBooleanList = false -%}

  {%- if
        (field.properties.syncList is defined and field.properties.syncList is not empty) and
        field.mappedField is not empty and
        field.mappedObject is not empty and
        mappedFields[field.mappedObject] is defined
  -%}
    {# @var \Mautic\FormBundle\Collection\FieldCollection #}
    {% set fieldCollection = mappedFields[field.mappedObject] %}
    {% if fieldCollection.getFieldByKey(field.mappedField, false) is not empty %}
      {% set mappedField = fieldCollection.getFieldByKey(field.mappedField) %}
      {% set mappedFieldType = mappedField.type %}

      {%- if mappedField.properties.list is defined and mappedField.properties.list is not empty -%}
        {%- set parseList = mappedField.properties.list -%}
      {%- elseif 'boolean' == mappedFieldType -%}
        {%- set parseList = {
                '0': mappedField.properties.no,
                '1': mappedField.properties.yes,
        }-%}
      {%- elseif 'country' == mappedFieldType -%}
        {%- set list = leadFieldCountryChoices() -%}
      {%- elseif 'region' == mappedFieldType -%}
        {%- set list = leadFieldRegionChoices() -%}
      {%- elseif 'timezone' == mappedFieldType -%}
        {%- set list = leadFieldTimezonesChoices() -%}
      {%- elseif 'locale' == mappedFieldType -%}
        {%- set list = leadFieldLocaleChoices() -%}
      {%- endif -%}
    {%- endif -%}
  {%- endif -%}

  {%- if parseList is empty -%}
    {%- if list is defined -%}
      {%- set parseList = list -%}
    {%- elseif field.properties.optionList is defined and field.properties.optionList is not empty -%}
      {%- set parseList = field.properties.optionList -%}
    {%- elseif field.properties.list is defined and field.properties.list is not empty -%}
      {%- set parseList = field.properties.list -%}
    {%- endif -%}

    {%- if parseList.list is defined -%}
      {%- set parseList = parseList.list -%}
    {%- endif -%}
  {%- endif -%}

  {%- if field.mappedField and mappedFields[field.mappedObject] is defined -%}
    {# @var \Mautic\FormBundle\Collection\FieldCollection #}
    {% set fieldCollection = mappedFields[field.mappedObject] %}

    {% if fieldCollection[field.mappedField] is defined %}
      {% set mappedField = fieldCollection[field.mappedField] %}
      {%- if mappedField.type in ['datetime', 'date'] -%}
        {%- for key, aTemp in parseList -%}
          {%- set date = 'datetime' == mappedField.type ? dateToFull(aTemp.label) : dateToDate(aTemp.label) -%}
          {%- if date -%}
              {%- set parseList = parseList|merge({
                      (key): parseList[key]|merge({
                              'label': date,
                      }),
              }) -%}
          {%- endif -%}
        {%- endfor -%}
      {%- endif -%}
    {%- endif -%}
  {%- endif -%}

  {%- if isBooleanList -%}
    {%- set list = formFieldParseBooleanList(parseList) -%}
  {%- else -%}
    {%- set list = formFieldParseList(parseList) -%}
  {%- endif -%}

  {%- set firstListValue = list|first -%}
{%- endif -%}
{# end: field_helper #}

{%- if field.properties.multiple is defined and true == field.properties.multiple -%}
  {%- set inputAttributes = inputAttributes|merge({
          'multiple': 'multiple',
  }) -%}
{%- endif -%}

{%- macro print_options(list, field) -%}
  {% for listValue, listLabel in list %}
    {% if listLabel is iterable %}
      {# this is an option group #}
      <optgroup label="{{ listValue }}">
        {{ _self.print_options(listLabel, field) }}
      </optgroup>
    {% else -%}
      <option value="{{ listValue|e }}" {% if listValue == field.defaultValue %}selected="selected"{% endif %}>{{ listLabel }}</option>
    {%- endif %}
  {% endfor %}
{%- endmacro -%}

<div {% for attrName, attrValue in containerAttributes %}{{ attrName }}="{% if attrValue is iterable %}{{ attrValue|join(' ') }}{% else %}{{ attrValue }}{% endif %}"{% endfor %}>
  {% if field.showLabel -%}<label {% for attrName, attrValue in labelAttributes %}{{ attrName }}="{% if attrValue is iterable %}{{ attrValue|join(' ') }}{% else %}{{ attrValue }}{% endif %}"{% endfor %}>{{ field.label }}</label>{% endif %}
  {% if field.helpMessage is not empty %}<span class="mauticform-helpmessage">{{ field.helpMessage|purify }}</span>{% endif %}

  <select {% for attrName, attrValue in inputAttributes %}{{ attrName }}="{% if attrValue is iterable %}{{ attrValue|join(' ') }}{% else %}{{ attrValue }}{% endif %}"{% endfor %}>
    {# (!empty($properties['placeholder']) || empty($field['defaultValue']) && empty($properties['multiple'])) #}
    {%- if
          (
            field.properties.placeholder is defined or (field.defaultValue is not defined or (field.defaultValue is defined and false == field.defaultValue))
          ) and
          (field.properties.multiple is not defined or (field.properties.multiple is defined and false == field.properties.multiple))
    -%}
        {%- set placeholder = field.properties.placeholder|default('') -%}
        <option value="">{{ placeholder }}</option>
    {%- endif -%}
    {{ _self.print_options(list, field) }}
  </select>
  <span class="mauticform-errormsg" style="display:none;">{{ validationMessage }}</span>
</div>