1: <?php
2:
3: 4: 5:
6: class Quform_Element_Radio extends Quform_Element_Multi implements Quform_Element_Editable
7: {
8: 9: 10: 11: 12: 13: 14: 15:
16: protected function getLabelHtml(array $context = array(), $forAttribute = true, $id = false)
17: {
18: return parent::getLabelHtml($context, false, true);
19: }
20:
21: 22: 23: 24: 25: 26: 27:
28: protected function getFieldAttributes(array $option, array $context = array())
29: {
30: $attributes = array(
31: 'type' => 'radio',
32: 'name' => $this->getFullyQualifiedName(),
33: 'id' => sprintf('%s_%d', $this->getUniqueId(), $this->getOptionValue($option, 'id')),
34: 'class' => Quform::sanitizeClass($this->getFieldClasses($option, $context)),
35: 'value' => $this->getOptionValue($option, 'value')
36: );
37:
38: if ($this->hasValue($attributes['value'])) {
39: $attributes['checked'] = true;
40: }
41:
42: $attributes = apply_filters('quform_field_attributes', $attributes, $this, $this->form, $context, $option);
43: $attributes = apply_filters('quform_field_attributes_' . $this->getIdentifier(), $attributes, $this, $this->form, $context, $option);
44:
45: return $attributes;
46: }
47:
48: 49: 50: 51: 52: 53: 54:
55: protected function getFieldClasses(array $option, array $context = array())
56: {
57: $classes = array(
58: 'quform-field',
59: 'quform-field-radio',
60: sprintf('quform-field-%s', $this->getIdentifier()),
61: sprintf('quform-field-%s_%d', $this->getIdentifier(), $this->getOptionValue($option, 'id'))
62: );
63:
64: if (Quform::isNonEmptyString($this->config('customClass'))) {
65: $classes[] = $this->config('customClass');
66: }
67:
68: if ($this->config('submitOnChoice')) {
69: $classes[] = 'quform-submit-on-choice';
70: }
71:
72: $classes = apply_filters('quform_field_classes', $classes, $option, $this, $this->form, $context);
73: $classes = apply_filters('quform_field_classes_' . $this->getIdentifier(), $classes, $option, $this, $this->form, $context);
74:
75: return $classes;
76: }
77:
78: 79: 80: 81: 82: 83:
84: protected function getFieldHtml(array $context = array())
85: {
86: $output = '';
87:
88: foreach ($this->getOptions() as $option) {
89: $attributes = $this->getFieldAttributes($option, $context);
90:
91: $output .= sprintf(
92: '<div class="quform-option%s">',
93: Quform::isNonEmptyString($this->getOptionValue($option, 'label')) ? '' : ' quform-option-label-empty'
94: );
95:
96: $output .= Quform::getHtmlTag('input', $attributes);
97:
98: $output .= sprintf(
99: '<label for="%s" class="quform-option-label quform-option-label-%s_%d">',
100: esc_attr($attributes['id']),
101: esc_attr($this->getIdentifier()),
102: $this->getOptionValue($option, 'id')
103: );
104:
105: if (Quform::isNonEmptyString($this->getOptionValue($option, 'icon'))) {
106: $output .= sprintf('<span class="quform-option-icon"><i class="%s"></i></span>', Quform::escape($this->getOptionValue($option, 'icon')));
107: }
108:
109: if (Quform::isNonEmptyString($this->getOptionValue($option, 'iconSelected'))) {
110: $output .= sprintf('<span class="quform-option-icon-selected"><i class="%s"></i></span>', Quform::escape($this->getOptionValue($option, 'iconSelected')));
111: }
112:
113: if (Quform::isNonEmptyString($this->getOptionValue($option, 'label'))) {
114: $output .= sprintf('<span class="quform-option-text">%s</span>', do_shortcode($this->getOptionValue($option, 'label')));
115: }
116:
117: $output .= '</label>';
118:
119: $output .= '</div>';
120: }
121:
122: return $output;
123: }
124:
125: 126: 127: 128: 129: 130:
131: protected function getInputHtml(array $context = array())
132: {
133: $output = sprintf('<div class="%s">', Quform::escape(Quform::sanitizeClass($this->getInputClasses($context))));
134:
135: $optionsClasses = array('quform-options', 'quform-cf');
136:
137: if (is_numeric($this->config('optionsLayout'))) {
138: $optionsClasses[] = 'quform-options-columns';
139: $optionsClasses[] = sprintf('quform-%d-columns', $this->config('optionsLayout'));
140:
141: if (Quform::isNonEmptyString($this->config('optionsLayoutResponsiveColumns')) && $this->config('optionsLayoutResponsiveColumns') != 'custom') {
142: $optionsClasses[] = sprintf('quform-responsive-columns-%s', $this->config('optionsLayoutResponsiveColumns'));
143: }
144: } else {
145: $optionsClasses[] = sprintf('quform-options-%s', $this->config('optionsLayout'));
146: }
147:
148: if (Quform::isNonEmptyString($context['optionsStyle'])) {
149: $optionsClasses[] = sprintf('quform-options-style-%s', $context['optionsStyle']);
150:
151: if ($context['optionsStyle'] == 'button') {
152: if (Quform::isNonEmptyString($context['optionsButtonStyle'])) {
153: $optionsClasses[] = sprintf('quform-button-style-%s', $context['optionsButtonStyle']);
154: }
155:
156: if (Quform::isNonEmptyString($context['optionsButtonSize'])) {
157: $optionsClasses[] = sprintf('quform-button-size-%s', $context['optionsButtonSize']);
158: }
159:
160: if (Quform::isNonEmptyString($context['optionsButtonWidth']) && $context['optionsButtonWidth'] != 'custom') {
161: $optionsClasses[] = sprintf('quform-button-width-%s', $context['optionsButtonWidth']);
162: }
163:
164: if (Quform::isNonEmptyString($context['optionsButtonIconPosition'])) {
165: $optionsClasses[] = sprintf('quform-button-icon-%s', $context['optionsButtonIconPosition']);
166: }
167: }
168: }
169:
170: if ($this->hasOnlySimpleOptions($context)) {
171: $optionsClasses[] = 'quform-options-simple';
172: }
173:
174: $output .= sprintf(
175: '<div class="%s"%s>',
176: Quform::escape(Quform::sanitizeClass($optionsClasses)),
177: Quform::isNonEmptyString($this->config('label')) ? ' role="radiogroup" aria-labelledby="' . Quform::escape($this->getUniqueId() . '_label') . '"' : ''
178: );
179:
180: $output .= $this->getFieldHtml();
181:
182: $output .= '</div></div>';
183:
184: return $output;
185: }
186:
187: 188: 189: 190: 191: 192:
193: protected function hasOnlySimpleOptions(array $context = array())
194: {
195: if (Quform::isNonEmptyString($context['optionsStyle'])) {
196: return false;
197: }
198:
199: $keys = array('image', 'imageSelected', 'width', 'height', 'icon', 'iconSelected');
200:
201: foreach ($this->getOptions() as $option) {
202: foreach ($keys as $key) {
203: if (Quform::isNonEmptyString($this->getOptionValue($option, $key))) {
204: return false;
205: }
206: }
207: }
208:
209: return true;
210: }
211:
212: 213: 214: 215: 216:
217: public function getEditFieldHtml()
218: {
219: return $this->getFieldHtml();
220: }
221:
222: 223: 224: 225: 226: 227:
228: protected function renderCss(array $context = array())
229: {
230: $css = parent::renderCss($context);
231:
232: if ($this->config('optionsLayoutResponsiveColumns') == 'custom' && Quform::isNonEmptyString($this->config('optionsLayoutResponsiveColumnsCustom'))) {
233: $css .= sprintf(
234: '@media (max-width: %s) { .quform-input-%s > .quform-options-columns > .quform-option { float: none; width: 100%% !important; } }',
235: Quform::addCssUnit($this->config('optionsLayoutResponsiveColumnsCustom')),
236: $this->getIdentifier()
237: );
238: }
239:
240: if ($context['optionsButtonWidth'] == 'custom' && Quform::isNonEmptyString($context['optionsButtonWidthCustom'])) {
241: $css .= sprintf(
242: '.quform-input-%s .quform-option .quform-option-label { width: %s;}',
243: $this->getIdentifier(),
244: Quform::addCssUnit($context['optionsButtonWidthCustom'])
245: );
246: }
247:
248: foreach ($this->getOptions() as $option) {
249: if (Quform::isNonEmptyString($this->getOptionValue($option, 'image'))) {
250: $css .= sprintf(
251: '.quform-option-label-%s_%d { background-image: url(%s); }',
252: $this->getIdentifier(),
253: $this->getOptionValue($option, 'id'),
254: esc_url($this->getOptionValue($option, 'image'))
255: );
256: }
257:
258: if (Quform::isNonEmptyString($this->getOptionValue($option, 'imageSelected'))) {
259: $css .= sprintf(
260: '.quform-field-radio:checked + .quform-option-label-%s_%d { background-image: url(%s); }',
261: $this->getIdentifier(),
262: $this->getOptionValue($option, 'id'),
263: esc_url($this->getOptionValue($option, 'imageSelected'))
264: );
265: }
266:
267: if (Quform::isNonEmptyString($this->getOptionValue($option, 'width'))) {
268: $css .= sprintf(
269: '.quform-option .quform-option-label-%s_%d { width: %s; }',
270: $this->getIdentifier(),
271: $this->getOptionValue($option, 'id'),
272: Quform::addCssUnit($this->getOptionValue($option, 'width'))
273: );
274: }
275:
276: if (Quform::isNonEmptyString($this->getOptionValue($option, 'height'))) {
277: $css .= sprintf(
278: '.quform-option .quform-option-label-%s_%d { height: %s; }',
279: $this->getIdentifier(),
280: $this->getOptionValue($option, 'id'),
281: Quform::addCssUnit($this->getOptionValue($option, 'height'))
282: );
283: }
284: }
285:
286: return $css;
287: }
288:
289: 290: 291: 292: 293: 294:
295: public static function getDefaultOptionConfig($key = null)
296: {
297: $config = array(
298: 'label' => '',
299: 'value' => '',
300: 'image' => '',
301: 'imageSelected' => '',
302: 'width' => '',
303: 'height' => '',
304: 'icon' => '',
305: 'iconSelected' => ''
306: );
307:
308: if (Quform::isNonEmptyString($key)) {
309: return Quform::get($config, $key);
310: }
311:
312: return $config;
313: }
314:
315: 316: 317: 318: 319:
320: protected static function getDefaultOptions()
321: {
322: $options = array();
323: $defaults = array(__('Option 1', 'quform'), __('Option 2', 'quform'), __('Option 3', 'quform'));
324:
325: foreach ($defaults as $key => $value) {
326: $option = self::getDefaultOptionConfig();
327: $option['id'] = $key + 1;
328: $option['label'] = $option['value'] = $value;
329: $options[] = $option;
330: }
331:
332: return $options;
333: }
334:
335: 336: 337: 338: 339: 340:
341: public static function getDefaultConfig($key = null)
342: {
343: $config = apply_filters('quform_default_config_radio', array(
344:
345: 'label' => __('Untitled', 'quform'),
346: 'options' => self::getDefaultOptions(),
347: 'nextOptionId' => 4,
348: 'defaultValue' => '',
349: 'customiseValues' => false,
350: 'description' => '',
351: 'descriptionAbove' => '',
352: 'required' => false,
353:
354:
355: 'labelIcon' => '',
356: 'optionsLayout' => 'block',
357: 'optionsLayoutResponsiveColumns' => 'phone-landscape',
358: 'optionsLayoutResponsiveColumnsCustom' => '',
359: 'optionsStyle' => 'inherit',
360: 'optionsButtonStyle' => 'inherit',
361: 'optionsButtonSize' => 'inherit',
362: 'optionsButtonWidth' => 'inherit',
363: 'optionsButtonWidthCustom' => '',
364: 'optionsButtonIconPosition' => 'inherit',
365: 'customClass' => '',
366: 'customElementClass' => '',
367: 'styles' => array(),
368:
369:
370: 'subLabel' => '',
371: 'subLabelAbove' => '',
372: 'adminLabel' => '',
373: 'tooltip' => '',
374: 'tooltipType' => 'icon',
375: 'tooltipEvent' => 'inherit',
376: 'labelPosition' => 'inherit',
377: 'labelWidth' => '',
378:
379:
380: 'logicEnabled' => false,
381: 'logicAction' => true,
382: 'logicMatch' => 'all',
383: 'logicRules' => array(),
384:
385:
386: 'inArrayValidator' => true,
387: 'dynamicDefaultValue' => false,
388: 'dynamicKey' => '',
389: 'submitOnChoice' => false,
390: 'showInEmail' => true,
391: 'saveToDatabase' => true,
392:
393:
394: 'visibility' => '',
395: 'validators' => array(),
396:
397:
398: 'messageRequired' => ''
399: ));
400:
401: $config['type'] = 'radio';
402:
403: if (Quform::isNonEmptyString($key)) {
404: return Quform::get($config, $key);
405: }
406:
407: return $config;
408: }
409: }
410: