1{#=== OPTIONS ========================================================================================================#}
2
3{% set option = {
4    class:     field.class|default(''),
5    label:     field.label|default(''),
6    multiple:  (field.multiple is defined and field.multiple),
7    values:    field.values|default([]),
8    info:      field.info|default(''),
9    required:  field.required|default(false)
10} %}
11
12{#=== INIT ===========================================================================================================#}
13
14{% if option.values is iterable %}
15    {% set values = option.values %}
16{% else %}
17    {% set lookuptype = option.values|split('/')|first %}
18    {% set lookupfield = option.values|split('/')|last %}
19    {% if ',' in lookupfield %}
20        {% set lookupfield = lookupfield|split(',') %}
21    {% endif %}
22    {% set sortingorder = field.sort|default(lookupfield|first) %}
23    {% set querylimit = field.limit|default(500) %}
24    {% set wherefilter = field.filter|default({}) %}
25    {% setcontent lookups = lookuptype where wherefilter order sortingorder nohydrate limit querylimit %}
26    {% set values = lookups|selectfield(lookupfield, option.multiple, field.keys|default('id')) %}
27{% endif %}
28
29{# get the current selection. Either a single value, or an array. #}
30{% set selection = context.content.get(contentkey)|default(null) %}
31{% if selection is not iterable %}
32    {% set selection = [ selection ] %}
33{% endif %}
34
35{# If the current selection contains an existing id, we must use _only_ the id, and not accept a fallback. #}
36{% if selection|first in values|keys %}
37    {% set onlyids = true %}
38{% else %}
39    {% set onlyids = false %}
40{% endif %}
41
42{% set attr_select = {
43    class:     option.class,
44    id:        key,
45    multiple:  option.multiple,
46    required:  option.required,
47    name:      (option.multiple) ? name ~ '[]' : name,
48} %}
49
50{# If the field has autocomplete, we need to set our own style, and remove the class attribute. #}
51{% if field.autocomplete|default(false) %}
52    {% set attr_select = attr_select|merge({'class': ''}) %}
53    {% set attr_select = attr_select|merge({'style': 'width: 100%;'}) %}
54{% endif %}
55
56{#=== FIELDSET =======================================================================================================#}
57
58<fieldset class="multiselect">
59
60    <label class="col-sm-3 control-label">{{ (option.info) ? macro.infopop(labelkey, option.info) : labelkey }}</label>
61    <div class="col-sm-9">
62        <select{{ macro.attr(attr_select) }}>
63            {% for id, value in values %}
64
65                {% set is_array = (value is iterable and (value | length) > 1) %}
66                {% set attr_opt = {
67                    value:     id,
68                    selected:  id in selection or (not onlyids and (is_array ? value[0] : value) in selection),
69                } %}
70
71                <option{{ macro.attr(attr_opt) }}>{{ is_array ? value[0:]|join(' / ') : value }}</option>
72            {% endfor %}
73        </select>
74
75        {% if option.multiple and not field.autocomplete|default(false) %}
76        <div>{# TODO: move onclick-events to JS #}
77            <a href="#" class="btn btn-default btn-xs" onclick="jQuery('#{{ key }} option').prop('selected', true); return false;">
78                <i class="fa fa-fw fa-check-square-o"></i>{{ __("Select all") }}
79            </a>
80            <a href="#" class="btn btn-default btn-xs" onclick="jQuery('#{{ key }} option').prop('selected', false); return false;">
81                <i class="fa fa-fw fa-square-o"></i>{{ __("Select none") }}
82            </a>
83        </div>
84        {% endif %}
85    </div>
86
87    {% if field.autocomplete|default(false) %}
88    <script>
89        $(function() {
90            $('#{{ key }}').select2({
91                placeholder: "{{ __('(none)') }}",
92                allowClear: true
93            });
94        });
95    </script>
96    {% endif %}
97
98</fieldset>
99