1: <?php
2:
3: 4: 5:
6: class Quform_Element_Select extends Quform_Element_Multi implements Quform_Element_Editable
7: {
8: 9: 10: 11: 12: 13:
14: protected function getOptionHtml(array $option)
15: {
16: $output = sprintf(
17: '<option value="%s"%s>%s</option>',
18: Quform::escape($this->getOptionValue($option, 'value')),
19: $this->hasValue($this->getOptionValue($option, 'value')) ? ' selected="selected"' : '',
20: Quform::escape($this->getOptionValue($option, 'label'))
21: );
22:
23: return $output;
24: }
25:
26: 27: 28: 29: 30: 31:
32: protected function getOptgroupHtml(array $optgroup)
33: {
34: $output = sprintf('<optgroup label="%s">', Quform::escape($this->getOptgroupValue($optgroup, 'label')));
35:
36: foreach ($this->getOptgroupValue($optgroup, 'options') as $option) {
37: $output .= $this->getOptionHtml($option);
38: }
39:
40: $output .= '</optgroup>';
41:
42: return $output;
43: }
44:
45: 46: 47: 48: 49: 50:
51: protected function getFieldAttributes(array $context = array())
52: {
53: $attributes = array(
54: 'id' => $this->getUniqueId(),
55: 'name' => $this->getFullyQualifiedName(),
56: 'class' => Quform::sanitizeClass($this->getFieldClasses($context))
57: );
58:
59: if ($this->config('enhancedSelectEnabled')) {
60: $attributes['data-options'] = wp_json_encode(array(
61: 'rtl' => $this->form->isRtl(),
62: 'search' => $this->config('enhancedSelectSearch'),
63: 'noResultsFound' => $this->getTranslation('enhancedSelectNoResultsFound', __('No results found.', 'quform'))
64: ));
65:
66: $attributes['style'] = 'width: 100%;';
67: }
68:
69: if (Quform::isNonEmptyString($this->config('autocomplete'))) {
70: $attributes['autocomplete'] = $this->config('autocomplete');
71: }
72:
73: if (Quform::isNonEmptyString($this->config('aria-labelledby'))) {
74: $attributes['aria-labelledby'] = $this->config('aria-labelledby');
75: }
76:
77: $attributes = apply_filters('quform_field_attributes', $attributes, $this, $this->form, $context);
78: $attributes = apply_filters('quform_field_attributes_' . $this->getIdentifier(), $attributes, $this, $this->form, $context);
79:
80: return $attributes;
81: }
82:
83: 84: 85: 86: 87: 88:
89: protected function getFieldClasses(array $context = array())
90: {
91: $classes = array(
92: 'quform-field',
93: 'quform-field-select',
94: sprintf('quform-field-%s', $this->getIdentifier())
95: );
96:
97: if ($this->config('enhancedSelectEnabled')) {
98: $classes[] = 'quform-field-select-enhanced';
99: }
100:
101: if (Quform::isNonEmptyString($this->config('customClass'))) {
102: $classes[] = $this->config('customClass');
103: }
104:
105: if ($this->config('submitOnChoice')) {
106: $classes[] = 'quform-submit-on-choice';
107: }
108:
109: $classes = apply_filters('quform_field_classes', $classes, $this, $this->form, $context);
110: $classes = apply_filters('quform_field_classes_' . $this->getIdentifier(), $classes, $this, $this->form, $context);
111:
112: return $classes;
113: }
114:
115: 116: 117: 118: 119: 120:
121: protected function getFieldHtml(array $context = array())
122: {
123: return Quform::getHtmlTag('select', $this->getFieldAttributes($context), $this->getOptionsHtml());
124: }
125:
126: 127: 128: 129: 130:
131: protected function getOptionsHtml()
132: {
133: $output = '';
134:
135: if ($this->config('noneOption')) {
136: $output .= $this->getOptionHtml(array(
137: 'label' => $this->getTranslation('noneOptionText', __('Please select', 'quform')),
138: 'value' => ''
139: ));
140: }
141:
142: foreach ($this->getOptions() as $option) {
143: if (isset($option['options'])) {
144: $output .= $this->getOptgroupHtml($option);
145: } else {
146: $output .= $this->getOptionHtml($option);
147: }
148: }
149:
150: return $output;
151: }
152:
153: 154: 155: 156: 157:
158: public function getEditFieldHtml()
159: {
160: return $this->getFieldHtml();
161: }
162:
163: 164: 165: 166: 167: 168:
169: protected function renderCss(array $context = array())
170: {
171: $css = parent::renderCss($context);
172:
173: if ($context['fieldWidth'] == 'custom' && Quform::isNonEmptyString($context['fieldWidthCustom'])) {
174: $css .= sprintf('.quform-input-select.quform-input-%1$s, .quform-input-multiselect.quform-input-%1$s { width: %2$s; }', $this->getIdentifier(), Quform::addCssUnit($context['fieldWidthCustom']));
175: $css .= sprintf('.quform-inner-%s > .quform-error > .quform-error-inner { float: left; min-width: %s; }', $this->getIdentifier(), Quform::addCssUnit($context['fieldWidthCustom']));
176: }
177:
178: return $css;
179: }
180:
181: 182: 183: 184: 185: 186:
187: protected function prepareContext(array $context = array())
188: {
189: $context = parent::prepareContext($context);
190:
191:
192: $context['tooltipType'] = 'icon';
193:
194: return $context;
195: }
196:
197: 198: 199: 200: 201: 202:
203: public static function getDefaultOptgroupConfig($key = null)
204: {
205: $config = array(
206: 'label' => __('Untitled', 'quform'),
207: 'options' => array()
208: );
209:
210: if (Quform::isNonEmptyString($key)) {
211: return Quform::get($config, $key);
212: }
213:
214: return $config;
215: }
216:
217: 218: 219: 220: 221: 222: 223:
224: protected function getOptgroupValue(array $optgroup, $key)
225: {
226: $value = Quform::get($optgroup, $key);
227:
228: if ($value === null) {
229: $value = Quform::get(call_user_func(array(get_class($this), 'getDefaultOptgroupConfig')), $key);
230: }
231:
232: return $value;
233: }
234:
235: 236: 237: 238: 239:
240: protected static function getDefaultOptions()
241: {
242: $options = array();
243: $defaults = array(__('Option 1', 'quform'), __('Option 2', 'quform'), __('Option 3', 'quform'));
244:
245: foreach ($defaults as $key => $value) {
246: $option = self::getDefaultOptionConfig();
247: $option['id'] = $key + 1;
248: $option['label'] = $option['value'] = $value;
249: $options[] = $option;
250: }
251:
252: return $options;
253: }
254:
255: 256: 257: 258: 259: 260:
261: public static function getDefaultConfig($key = null)
262: {
263: $config = apply_filters('quform_default_config_select', array(
264:
265: 'label' => __('Untitled', 'quform'),
266: 'options' => self::getDefaultOptions(),
267: 'nextOptionId' => 4,
268: 'defaultValue' => '',
269: 'customiseValues' => false,
270: 'noneOption' => true,
271: 'description' => '',
272: 'descriptionAbove' => '',
273: 'required' => false,
274:
275:
276: 'labelIcon' => '',
277: 'fieldSize' => 'inherit',
278: 'fieldWidth' => 'inherit',
279: 'fieldWidthCustom' => '',
280: 'enhancedSelectEnabled' => false,
281: 'enhancedSelectSearch' => true,
282: 'customClass' => '',
283: 'customElementClass' => '',
284: 'styles' => array(),
285:
286:
287: 'subLabel' => '',
288: 'subLabelAbove' => '',
289: 'adminLabel' => '',
290: 'tooltip' => '',
291: 'tooltipType' => 'icon',
292: 'tooltipEvent' => 'inherit',
293: 'labelPosition' => 'inherit',
294: 'labelWidth' => '',
295:
296:
297: 'logicEnabled' => false,
298: 'logicAction' => true,
299: 'logicMatch' => 'all',
300: 'logicRules' => array(),
301:
302:
303: 'inArrayValidator' => true,
304: 'dynamicDefaultValue' => false,
305: 'dynamicKey' => '',
306: 'autocomplete' => '',
307: 'submitOnChoice' => false,
308: 'showInEmail' => true,
309: 'saveToDatabase' => true,
310:
311:
312: 'visibility' => '',
313: 'validators' => array(),
314:
315:
316: 'messageRequired' => '',
317: 'noneOptionText' => '',
318: 'enhancedSelectNoResultsFound' => ''
319: ));
320:
321: $config['type'] = 'select';
322:
323: if (Quform::isNonEmptyString($key)) {
324: return Quform::get($config, $key);
325: }
326:
327: return $config;
328: }
329: }
330: