1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xoops\Form;
13:
14: /**
15: * Radio - radio button element
16: *
17: * @category Xoops\Form\Radio
18: * @package Xoops\Form
19: * @author Kazumi Ono <onokazu@xoops.org>
20: * @author Taiwen Jiang <phppp@users.sourceforge.net>
21: * @copyright 2001-2015 XOOPS Project (http://xoops.org)
22: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
23: * @link http://xoops.org
24: */
25: class Radio extends OptionElement
26: {
27: /**
28: * __construct
29: *
30: * @param mixed $caption Caption or array of all attributes
31: * Control attributes:
32: * :inline true to render with inline style
33: * @param string $name name attribute
34: * @param string $value Pre-selected value
35: * @param boolean $inline true to display inline
36: */
37: public function __construct($caption, $name = null, $value = null, $inline = true)
38: {
39: if (is_array($caption)) {
40: parent::__construct($caption);
41: } else {
42: parent::__construct([]);
43: $this->setWithDefaults('caption', $caption, '');
44: $this->setWithDefaults('name', $name, 'name_error');
45: $this->set('value', $value);
46: if ($inline) {
47: $this->set(':inline');
48: }
49: }
50: $this->set('type', 'radio');
51: }
52:
53: /**
54: * Prepare HTML for output
55: *
56: * @return string HTML
57: */
58: public function render()
59: {
60: $ele_options = $this->getOptions();
61: $ele_value = $this->getValue();
62: $ele_name = $this->getName();
63: $extra = ($this->getExtra() != '' ? " " . $this->getExtra() : '');
64: $ret = "";
65: static $id_ele = 0;
66: foreach ($ele_options as $value => $buttonCaption) {
67: $this->remove('checked');
68: if (isset($ele_value) && $value == $ele_value) {
69: $this->set('checked');
70: }
71: $this->set('value', $value);
72: ++$id_ele;
73: $this->set('id', $ele_name . $id_ele);
74: $ret .= '<label class="radio' . ($this->has(':inline') ? ' inline' : '') . '">' . "\n";
75: $ret .= '<input ' . $this->renderAttributeString() . $extra . ">" . "\n";
76: $ret .= $buttonCaption . "\n";
77: $ret .= "</label>" . "\n";
78: }
79: return $ret;
80: }
81: }
82: