1: <?php
2:
3: 4: 5:
6: class Quform_Element_Multiselect extends Quform_Element_Select
7: {
8: 9: 10:
11: protected $value = array();
12:
13: 14: 15:
16: protected $isMultiple = true;
17:
18: 19: 20: 21: 22: 23:
24: public function prepareDynamicValue($value)
25: {
26: return Quform::isNonEmptyString($value) ? explode(',', $value) : $this->getEmptyValue();
27: }
28:
29: 30: 31: 32: 33: 34:
35: protected function isValidValue($value)
36: {
37: if ( ! is_array($value)) {
38: return false;
39: }
40:
41: foreach ($value as $val) {
42: if ( ! parent::isValidValue($val)) {
43: return false;
44: }
45: }
46:
47: return true;
48: }
49:
50: 51: 52: 53: 54: 55:
56: public function hasValue($value)
57: {
58: return in_array($value, $this->getValue(), true);
59: }
60:
61: 62: 63: 64: 65:
66: public function getEmptyValue()
67: {
68: return array();
69: }
70:
71: 72: 73: 74: 75:
76: public function getValue()
77: {
78: $value = $this->value;
79:
80: $this->filterValueRecursive($value);
81:
82: $value = apply_filters('quform_get_value_' . $this->getIdentifier(), $value, $this, $this->getForm());
83:
84: return $value;
85: }
86:
87: 88: 89: 90: 91:
92: public function getValueHtml()
93: {
94: $value = '';
95:
96: if ( ! $this->isEmpty()) {
97: $ulStyle = apply_filters('quform_value_list_multiselect_ul_style', 'margin:0;padding:0;list-style:disc inside;', $this, $this->getForm());
98: $ulStyle = apply_filters('quform_value_list_multiselect_ul_style_' . $this->getIdentifier(), $ulStyle, $this, $this->getForm());
99:
100: $liStyle = apply_filters('quform_value_list_multiselect_li_style', '', $this, $this->getForm());
101: $liStyle = apply_filters('quform_value_list_multiselect_li_style_' . $this->getIdentifier(), $liStyle, $this, $this->getForm());
102:
103: $value = sprintf(
104: '<ul class="quform-value-list quform-value-list-multiselect"%s>',
105: Quform::isNonEmptyString($ulStyle) ? ' style="' . esc_attr($ulStyle) . '"' : ''
106: );
107:
108: foreach ($this->getValue() as $option) {
109: $value .= sprintf(
110: '<li class="quform-value-list-item quform-value-list-item-multiselect"%s>',
111: Quform::isNonEmptyString($liStyle) ? ' style="' . esc_attr($liStyle) . '"' : ''
112: );
113:
114: $value .= Quform::escape($option);
115:
116: $value .= '</li>';
117: }
118:
119: $value .= '</ul>';
120: }
121:
122: $value = apply_filters('quform_get_value_html_' . $this->getIdentifier(), $value, $this, $this->getForm());
123:
124: return $value;
125: }
126:
127: 128: 129: 130: 131: 132:
133: public function getValueText($separator = ', ')
134: {
135: $value = join($separator, $this->getValue());
136:
137: $value = apply_filters('quform_get_value_text_' . $this->getIdentifier(), $value, $this, $this->getForm());
138:
139: return $value;
140: }
141:
142: 143: 144: 145: 146:
147: protected function getConvertedValueForStorage()
148: {
149: return serialize($this->getValue());
150: }
151:
152: 153: 154: 155: 156: 157:
158: protected function convertValueFromStorage($value)
159: {
160: return is_serialized($value) ? unserialize($value) : $this->getEmptyValue();
161: }
162:
163: 164: 165: 166: 167:
168: public function isEmpty()
169: {
170: return ! count($this->getValue());
171: }
172:
173: 174: 175: 176: 177: 178:
179: protected function getFieldAttributes(array $context = array())
180: {
181: $attributes = array(
182: 'id' => $this->getUniqueId(),
183: 'name' => $this->getFullyQualifiedName(),
184: 'class' => Quform::sanitizeClass($this->getFieldClasses($context)),
185: 'multiple' => true
186: );
187:
188: if ($this->config('enhancedSelectEnabled')) {
189: $attributes['data-options'] = wp_json_encode(array(
190: 'rtl' => $this->form->isRtl(),
191: 'placeholder' => $this->getTranslation('enhancedSelectPlaceholder', __('Please select', 'quform')),
192: 'noResultsFound' => $this->getTranslation('enhancedSelectNoResultsFound', __('No results found.', 'quform')),
193: ));
194:
195: $attributes['style'] = 'width: 100%;';
196: }
197:
198: if (Quform::isNonEmptyString($this->config('aria-labelledby'))) {
199: $attributes['aria-labelledby'] = $this->config('aria-labelledby');
200: }
201:
202: if (Quform::isNonEmptyString($this->config('sizeAttribute'))) {
203: if ($this->config('sizeAttribute') == 'auto') {
204: $attributes['size'] = $this->getOptionsCount();
205: } else {
206: $attributes['size'] = $this->config('sizeAttribute');
207: }
208: }
209:
210: $attributes = apply_filters('quform_field_attributes', $attributes, $this, $this->form, $context);
211: $attributes = apply_filters('quform_field_attributes_' . $this->getIdentifier(), $attributes, $this, $this->form, $context);
212:
213: return $attributes;
214: }
215:
216: 217: 218: 219: 220:
221: protected function getOptionsCount()
222: {
223: $count = count($this->getOptions());
224:
225: foreach ($this->getOptions() as $option) {
226: if (isset($option['options'])) {
227: $count += count($option['options']);
228: }
229: }
230:
231: return $count;
232: }
233:
234: 235: 236: 237: 238: 239:
240: protected function getFieldClasses(array $context = array())
241: {
242: $classes = array(
243: 'quform-field',
244: 'quform-field-multiselect',
245: sprintf('quform-field-%s', $this->getIdentifier())
246: );
247:
248: if ($this->config('enhancedSelectEnabled')) {
249: $classes[] = 'quform-field-multiselect-enhanced';
250: }
251:
252: if (Quform::isNonEmptyString($this->config('customClass'))) {
253: $classes[] = $this->config('customClass');
254: }
255:
256: return $classes;
257: }
258:
259: 260: 261: 262: 263: 264:
265: protected function getFieldHtml(array $context = array())
266: {
267: return Quform::getHtmlTag('select', $this->getFieldAttributes($context), $this->getOptionsHtml());
268: }
269:
270: 271: 272: 273: 274:
275: protected static function getDefaultOptions()
276: {
277: $options = array();
278: $defaults = array(__('Option 1', 'quform'), __('Option 2', 'quform'), __('Option 3', 'quform'));
279:
280: foreach ($defaults as $key => $value) {
281: $option = self::getDefaultOptionConfig();
282: $option['id'] = $key + 1;
283: $option['label'] = $option['value'] = $value;
284: $options[] = $option;
285: }
286:
287: return $options;
288: }
289:
290: 291: 292: 293: 294: 295:
296: public static function getDefaultConfig($key = null)
297: {
298: $config = apply_filters('quform_default_config_multi_select', array(
299:
300: 'label' => __('Untitled', 'quform'),
301: 'options' => self::getDefaultOptions(),
302: 'nextOptionId' => 4,
303: 'defaultValue' => array(),
304: 'customiseValues' => false,
305: 'description' => '',
306: 'descriptionAbove' => '',
307: 'required' => false,
308:
309:
310: 'labelIcon' => '',
311: 'fieldSize' => 'inherit',
312: 'fieldWidth' => 'inherit',
313: 'fieldWidthCustom' => '',
314: 'enhancedSelectEnabled' => false,
315: 'enhancedSelectPlaceholder' => '',
316: 'customClass' => '',
317: 'customElementClass' => '',
318: 'sizeAttribute' => '',
319: 'styles' => array(),
320:
321:
322: 'subLabel' => '',
323: 'subLabelAbove' => '',
324: 'adminLabel' => '',
325: 'tooltip' => '',
326: 'tooltipType' => 'icon',
327: 'tooltipEvent' => 'inherit',
328: 'labelPosition' => 'inherit',
329: 'labelWidth' => '',
330:
331:
332: 'logicEnabled' => false,
333: 'logicAction' => true,
334: 'logicMatch' => 'all',
335: 'logicRules' => array(),
336:
337:
338: 'inArrayValidator' => true,
339: 'dynamicDefaultValue' => false,
340: 'dynamicKey' => '',
341: 'showInEmail' => true,
342: 'saveToDatabase' => true,
343:
344:
345: 'visibility' => '',
346: 'validators' => array(),
347:
348:
349: 'messageRequired' => '',
350: 'enhancedSelectNoResultsFound' => ''
351: ));
352:
353: $config['type'] = 'multiselect';
354:
355: if (Quform::isNonEmptyString($key)) {
356: return Quform::get($config, $key);
357: }
358:
359: return $config;
360: }
361: }
362: