1: <?php
2:
3: 4: 5:
6: class Quform_Element_Checkbox extends Quform_Element_Multi implements Quform_Element_Editable
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_checkbox_ul_style', 'margin:0;padding:0;list-style:disc inside;', $this, $this->getForm());
98: $ulStyle = apply_filters('quform_value_list_checkbox_ul_style_' . $this->getIdentifier(), $ulStyle, $this, $this->getForm());
99:
100: $liStyle = apply_filters('quform_value_list_checkbox_li_style', '', $this, $this->getForm());
101: $liStyle = apply_filters('quform_value_list_checkbox_li_style_' . $this->getIdentifier(), $liStyle, $this, $this->getForm());
102:
103: $value = sprintf(
104: '<ul class="quform-value-list quform-value-list-checkbox"%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-checkbox"%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: 180:
181: protected function getLabelHtml(array $context = array(), $forAttribute = true, $id = false)
182: {
183: return parent::getLabelHtml($context, false, true);
184: }
185:
186: 187: 188: 189: 190: 191: 192:
193: protected function getFieldAttributes(array $option, array $context = array())
194: {
195: $attributes = array(
196: 'type' => 'checkbox',
197: 'name' => $this->getFullyQualifiedName(),
198: 'id' => sprintf('%s_%d', $this->getUniqueId(), $this->getOptionValue($option, 'id')),
199: 'class' => Quform::sanitizeClass($this->getFieldClasses($option)),
200: 'value' => $this->getOptionValue($option, 'value')
201: );
202:
203: if ($this->hasValue($attributes['value'])) {
204: $attributes['checked'] = true;
205: }
206:
207: $attributes = apply_filters('quform_field_attributes', $attributes, $this, $this->form, $context, $option);
208: $attributes = apply_filters('quform_field_attributes_' . $this->getIdentifier(), $attributes, $this, $this->form, $context, $option);
209:
210: return $attributes;
211: }
212:
213: 214: 215: 216: 217: 218: 219:
220: protected function getFieldClasses(array $option, array $context = array())
221: {
222: $classes = array(
223: 'quform-field',
224: 'quform-field-checkbox',
225: sprintf('quform-field-%s', $this->getIdentifier()),
226: sprintf('quform-field-%s_%d', $this->getIdentifier(), $this->getOptionValue($option, 'id'))
227: );
228:
229: if (Quform::isNonEmptyString($this->config('customClass'))) {
230: $classes[] = $this->config('customClass');
231: }
232:
233: $classes = apply_filters('quform_field_classes', $classes, $option, $this, $this->form, $context);
234: $classes = apply_filters('quform_field_classes_' . $this->getIdentifier(), $classes, $option, $this, $this->form, $context);
235:
236: return $classes;
237: }
238:
239: 240: 241: 242: 243: 244:
245: protected function getFieldHtml(array $context = array())
246: {
247: $output = '';
248:
249: foreach ($this->getOptions() as $option) {
250: $attributes = $this->getFieldAttributes($option, $context);
251:
252: $output .= sprintf(
253: '<div class="quform-option%s">',
254: Quform::isNonEmptyString($this->getOptionValue($option, 'label')) ? '' : ' quform-option-label-empty'
255: );
256:
257: $output .= Quform::getHtmlTag('input', $attributes);
258:
259: $output .= sprintf(
260: '<label for="%s" class="quform-option-label quform-option-label-%s_%d">',
261: esc_attr($attributes['id']),
262: esc_attr($this->getIdentifier()),
263: $this->getOptionValue($option, 'id')
264: );
265:
266: if (Quform::isNonEmptyString($this->getOptionValue($option, 'icon'))) {
267: $output .= sprintf('<span class="quform-option-icon"><i class="%s"></i></span>', Quform::escape($this->getOptionValue($option, 'icon')));
268: }
269:
270: if (Quform::isNonEmptyString($this->getOptionValue($option, 'iconSelected'))) {
271: $output .= sprintf('<span class="quform-option-icon-selected"><i class="%s"></i></span>', Quform::escape($this->getOptionValue($option, 'iconSelected')));
272: }
273:
274: if (Quform::isNonEmptyString($this->getOptionValue($option, 'label'))) {
275: $output .= sprintf('<span class="quform-option-text">%s</span>', do_shortcode($this->getOptionValue($option, 'label')));
276: }
277:
278: $output .= '</label>';
279:
280: $output .= '</div>';
281: }
282:
283: return $output;
284: }
285:
286: 287: 288: 289: 290: 291:
292: protected function getInputHtml(array $context = array())
293: {
294: $output = sprintf('<div class="%s">', Quform::escape(Quform::sanitizeClass($this->getInputClasses($context))));
295:
296: $optionsClasses = array('quform-options', 'quform-cf');
297:
298: if (is_numeric($this->config('optionsLayout'))) {
299: $optionsClasses[] = 'quform-options-columns';
300: $optionsClasses[] = sprintf('quform-%d-columns', $this->config('optionsLayout'));
301:
302: if (Quform::isNonEmptyString($this->config('optionsLayoutResponsiveColumns')) && $this->config('optionsLayoutResponsiveColumns') != 'custom') {
303: $optionsClasses[] = sprintf('quform-responsive-columns-%s', $this->config('optionsLayoutResponsiveColumns'));
304: }
305: } else {
306: $optionsClasses[] = sprintf('quform-options-%s', $this->config('optionsLayout'));
307: }
308:
309: if (Quform::isNonEmptyString($context['optionsStyle'])) {
310: $optionsClasses[] = sprintf('quform-options-style-%s', $context['optionsStyle']);
311:
312: if ($context['optionsStyle'] == 'button') {
313: if (Quform::isNonEmptyString($context['optionsButtonStyle'])) {
314: $optionsClasses[] = sprintf('quform-button-style-%s', $context['optionsButtonStyle']);
315: }
316:
317: if (Quform::isNonEmptyString($context['optionsButtonSize'])) {
318: $optionsClasses[] = sprintf('quform-button-size-%s', $context['optionsButtonSize']);
319: }
320:
321: if (Quform::isNonEmptyString($context['optionsButtonWidth']) && $context['optionsButtonWidth'] != 'custom') {
322: $optionsClasses[] = sprintf('quform-button-width-%s', $context['optionsButtonWidth']);
323: }
324:
325: if (Quform::isNonEmptyString($context['optionsButtonIconPosition'])) {
326: $optionsClasses[] = sprintf('quform-button-icon-%s', $context['optionsButtonIconPosition']);
327: }
328: }
329: }
330:
331: if ($this->hasOnlySimpleOptions($context)) {
332: $optionsClasses[] = 'quform-options-simple';
333: }
334:
335: $output .= sprintf(
336: '<div class="%s"%s>',
337: Quform::escape(Quform::sanitizeClass($optionsClasses)),
338: Quform::isNonEmptyString($this->config('label')) ? ' role="group" aria-labelledby="' . Quform::escape($this->getUniqueId() . '_label') . '"' : ''
339: );
340:
341: $output .= $this->getFieldHtml();
342:
343: $output .= '</div></div>';
344:
345: return $output;
346: }
347:
348: 349: 350: 351: 352: 353:
354: protected function hasOnlySimpleOptions(array $context = array())
355: {
356: if (Quform::isNonEmptyString($context['optionsStyle'])) {
357: return false;
358: }
359:
360: $keys = array('image', 'imageSelected', 'width', 'height', 'icon', 'iconSelected');
361:
362: foreach ($this->getOptions() as $option) {
363: foreach ($keys as $key) {
364: if (Quform::isNonEmptyString($this->getOptionValue($option, $key))) {
365: return false;
366: }
367: }
368: }
369:
370: return true;
371: }
372:
373: 374: 375: 376: 377:
378: public function getEditFieldHtml()
379: {
380: return $this->getFieldHtml();
381: }
382:
383: 384: 385: 386: 387: 388:
389: protected function renderCss(array $context = array())
390: {
391: $css = parent::renderCss($context);
392:
393: if ($this->config('optionsLayoutResponsiveColumns') == 'custom' && Quform::isNonEmptyString($this->config('optionsLayoutResponsiveColumnsCustom'))) {
394: $css .= sprintf(
395: '@media (max-width: %s) { .quform-input-%s > .quform-options-columns > .quform-option { float: none; width: 100%% !important; } }',
396: Quform::addCssUnit($this->config('optionsLayoutResponsiveColumnsCustom')),
397: $this->getIdentifier()
398: );
399: }
400:
401: if ($context['optionsButtonWidth'] == 'custom' && Quform::isNonEmptyString($context['optionsButtonWidthCustom'])) {
402: $css .= sprintf(
403: '.quform-input-%s .quform-option .quform-option-label { width: %s;}',
404: $this->getIdentifier(),
405: Quform::addCssUnit($context['optionsButtonWidthCustom'])
406: );
407: }
408:
409: foreach ($this->getOptions() as $option) {
410: if (Quform::isNonEmptyString($this->getOptionValue($option, 'image'))) {
411: $css .= sprintf(
412: '.quform-option-label-%s_%d { background-image: url(%s); }',
413: $this->getIdentifier(),
414: $this->getOptionValue($option, 'id'),
415: esc_url($this->getOptionValue($option, 'image'))
416: );
417: }
418:
419: if (Quform::isNonEmptyString($this->getOptionValue($option, 'imageSelected'))) {
420: $css .= sprintf(
421: '.quform-field-checkbox:checked + .quform-option-label-%s_%d { background-image: url(%s); }',
422: $this->getIdentifier(),
423: $this->getOptionValue($option, 'id'),
424: esc_url($this->getOptionValue($option, 'imageSelected'))
425: );
426: }
427:
428: if (Quform::isNonEmptyString($this->getOptionValue($option, 'width'))) {
429: $css .= sprintf(
430: '.quform-option .quform-option-label-%s_%d { width: %s; }',
431: $this->getIdentifier(),
432: $this->getOptionValue($option, 'id'),
433: Quform::addCssUnit($this->getOptionValue($option, 'width'))
434: );
435: }
436:
437: if (Quform::isNonEmptyString($this->getOptionValue($option, 'height'))) {
438: $css .= sprintf(
439: '.quform-option .quform-option-label-%s_%d { height: %s; }',
440: $this->getIdentifier(),
441: $this->getOptionValue($option, 'id'),
442: Quform::addCssUnit($this->getOptionValue($option, 'height'))
443: );
444: }
445: }
446:
447: return $css;
448: }
449:
450: 451: 452: 453: 454: 455:
456: public static function getDefaultOptionConfig($key = null)
457: {
458: $config = array(
459: 'label' => '',
460: 'value' => '',
461: 'image' => '',
462: 'imageSelected' => '',
463: 'width' => '',
464: 'height' => '',
465: 'icon' => '',
466: 'iconSelected' => ''
467: );
468:
469: if (Quform::isNonEmptyString($key)) {
470: return Quform::get($config, $key);
471: }
472:
473: return $config;
474: }
475:
476: 477: 478: 479: 480:
481: protected static function getDefaultOptions()
482: {
483: $options = array();
484: $defaults = array(__('Option 1', 'quform'), __('Option 2', 'quform'), __('Option 3', 'quform'));
485:
486: foreach ($defaults as $key => $value) {
487: $option = self::getDefaultOptionConfig();
488: $option['id'] = $key + 1;
489: $option['label'] = $option['value'] = $value;
490: $options[] = $option;
491: }
492:
493: return $options;
494: }
495:
496: 497: 498: 499: 500: 501:
502: public static function getDefaultConfig($key = null)
503: {
504: $config = apply_filters('quform_default_config_checkbox', array(
505:
506: 'label' => __('Untitled', 'quform'),
507: 'options' => self::getDefaultOptions(),
508: 'nextOptionId' => 4,
509: 'defaultValue' => array(),
510: 'customiseValues' => false,
511: 'description' => '',
512: 'descriptionAbove' => '',
513: 'required' => false,
514:
515:
516: 'labelIcon' => '',
517: 'optionsLayout' => 'block',
518: 'optionsLayoutResponsiveColumns' => 'phone-landscape',
519: 'optionsLayoutResponsiveColumnsCustom' => '',
520: 'optionsStyle' => 'inherit',
521: 'optionsButtonStyle' => 'inherit',
522: 'optionsButtonSize' => 'inherit',
523: 'optionsButtonWidth' => 'inherit',
524: 'optionsButtonWidthCustom' => '',
525: 'optionsButtonIconPosition' => 'inherit',
526: 'customClass' => '',
527: 'customElementClass' => '',
528: 'styles' => array(),
529:
530:
531: 'subLabel' => '',
532: 'subLabelAbove' => '',
533: 'adminLabel' => '',
534: 'tooltip' => '',
535: 'tooltipType' => 'icon',
536: 'tooltipEvent' => 'inherit',
537: 'labelPosition' => 'inherit',
538: 'labelWidth' => '',
539:
540:
541: 'logicEnabled' => false,
542: 'logicAction' => true,
543: 'logicMatch' => 'all',
544: 'logicRules' => array(),
545:
546:
547: 'inArrayValidator' => true,
548: 'dynamicDefaultValue' => false,
549: 'dynamicKey' => '',
550: 'showInEmail' => true,
551: 'saveToDatabase' => true,
552:
553:
554: 'visibility' => '',
555: 'validators' => array(),
556:
557:
558: 'messageRequired' => ''
559: ));
560:
561: $config['type'] = 'checkbox';
562:
563: if (Quform::isNonEmptyString($key)) {
564: return Quform::get($config, $key);
565: }
566:
567: return $config;
568: }
569: }
570: