SelectBox::isMultiple()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace fieldwork\components;
4
5
/**
6
 * Select2 powered select box
7
 * @package fieldwork\components
8
 */
9
class SelectBox extends Field
10
{
11
12
    const SELECT2_OPTION_MULTIPLE                   = "multiple";
13
    const SELECT2_OPTION_PLACEHOLDER                = "placeholder";
14
    const SELECT2_OPTION_ALLOW_CLEAR                = "allowClear";
15
    const SELECT2_OPTION_MINIMUM_RESULTS_FOR_SEARCH = "minimumResultsForSearch";
16
17
    protected $options;
18
19
    /**
20
     * The value to return if the "empty" option is selected
21
     * @var string|null
22
     */
23
    private $emptyValue;
24
25
    /**
26
     * Array of options passed to the select2 constructor as a javascript object
27
     * @var array
28
     */
29
    private $select2options;
30
31
    /**
32
     * @var boolean
33
     */
34
    private $usePlaceholder;
35
36
    /**
37
     * @var boolean
38
     */
39
    private $isMultipleAllowed;
40
41
    /**
42
     * Constructs a new SelectBox
43
     *
44
     * @param string                    $slug              The field identifier
45
     * @param string                    $label             The label to display
46
     * @param array                     $options           The available options for the field
47
     * @param string|string[]           $value             The current value or values
48
     * @param bool                      $isMultipleAllowed Whether multiple fields can be selected
49
     * @param string|string[]|bool|null $emptyValue        The value to return when an empty option is selected (this
50
     *                                                     is useful for relational fields in the database where you
51
     *                                                     should provide NULL ). Set to FALSE if you wish to use the
52
     *                                                     default empty value
53
     * @param int                       $storeValueLocally
54
     */
55
    public function __construct ($slug, $label, array $options = array(), $value = '', $isMultipleAllowed = false, $emptyValue = false, $storeValueLocally = 0)
56
    {
57
        parent::__construct($slug, $label, $value, $storeValueLocally);
58
        $this->options           = $options;
59
        $this->select2options    = [];
60
        $this->isMultipleAllowed = $isMultipleAllowed;
61
        $this->emptyValue        = $emptyValue === false ? ($this->isMultipleAllowed ? [] : '') : $emptyValue;
62
        $this->setPlaceholder();
63
    }
64
65
    public function getClasses ()
66
    {
67
        return array_merge(
68
            parent::getClasses(), array(
69
                'select',
70
                'selectbox')
71
        );
72
    }
73
74
    public function getAttributes ()
75
    {
76
        $attributes = parent::getAttributes();
77
        if ($this->isMultipleAllowed)
78
            $attributes['multiple'] = 'multiple';
79
        return $attributes;
80
    }
81
82
    public function getHTML ($showLabel = true)
83
    {
84
        $r = "<select " . $this->getAttributesString() . ">";
85
        if ($this->usePlaceholder) {
86
            $r .= "<option></option>";
87
        }
88
        foreach ($this->options as $v => $l) {
89
            $selected = $this->isSelected($v) ? " selected=\"selected\"" : "";
90
            $r .= "<option value=\"$v\"$selected>$l</option>";
91
        }
92
        $r .= "</select>";
93
        return $r;
94
    }
95
96
    public function getValue ($condense = true)
97
    {
98
        if ($this->value === "" || is_array($this->value) && !count($this->value))
99
            return $this->emptyValue;
100
        return parent::getValue();
101
    }
102
103
    public function getJsonData ()
104
    {
105
        return array_merge(parent::getJsonData(), [
106
            'select2' => $this->select2options
107
        ]);
108
    }
109
110
    /**
111
     * Merges the options
112
     *
113
     * @param array $select2options
114
     *
115
     * @return $this
116
     */
117
    private function setSelect2Options ($select2options)
118
    {
119
        foreach ($select2options as $key => $option)
120
            $this->select2options[$key] = $option;
121
        return $this;
122
    }
123
124
    /**
125
     * Determines the placeholder behaviour. Set to FALSE if the first option should be selected on default. Set to
126
     * NULL if the label should be used. Otherwise specify a placeholder.
127
     *
128
     * @param null|boolean|string $placeholder
129
     *
130
     * @return $this
131
     */
132
    public function setPlaceholder ($placeholder = null)
133
    {
134
        if ($placeholder === false)
135
            $this->usePlaceholder = false;
136
        else {
137
            $this->setSelect2Options(array(
138
                self::SELECT2_OPTION_PLACEHOLDER => $placeholder === null ? $this->label : $placeholder
139
            ));
140
            $this->usePlaceholder = true;
141
        }
142
        return $this;
143
    }
144
145
    /**
146
     * Determines the selection behaviour. If enabled, the user will be allowed to select more than one option.
147
     *
148
     * @param bool $multiple
149
     *
150
     * @return $this
151
     */
152
    public function setMultipleAllowed ($multiple = true)
153
    {
154
        $this->isMultipleAllowed = $multiple;
155
        return $this;
156
    }
157
158
    public function isMultiple ()
159
    {
160
        return $this->isMultipleAllowed;
161
    }
162
163
    /**
164
     * Whether to allow the user to clear the value once it has been selected
165
     *
166
     * @param $allowClear
167
     *
168
     * @return $this
169
     */
170
    public function allowClear ($allowClear)
171
    {
172
        $this->setSelect2Options(array(
173
            self::SELECT2_OPTION_ALLOW_CLEAR => $allowClear
174
        ));
175
        return $this;
176
    }
177
178
    /**
179
     * If set, the search box is only displayed if the amount of results in the select box is above $minimum
180
     *
181
     * @param int $minimum
182
     *
183
     * @return $this
184
     */
185
    public function setMinimumResultsForSearchBox ($minimum)
186
    {
187
        $this->setSelect2Options(array(
188
            self::SELECT2_OPTION_MINIMUM_RESULTS_FOR_SEARCH => $minimum
189
        ));
190
        return $this;
191
    }
192
193
    /**
194
     * Determines whether the given value is currently selected
195
     *
196
     * @param $v
197
     *
198
     * @return bool
199
     */
200
    protected function isSelected ($v)
201
    {
202
        return $this->value === $v || (is_array($this->value) && in_array($v, $this->value));
203
    }
204
205
    protected function getRestoreDefault ()
206
    {
207
        // TODO find out what this is good for
208
        return $this->emptyValue;
209
    }
210
}