1: <?php
2:
3: /**
4: * This file contains the cHTMLRadiobutton class.
5: *
6: * @package Core
7: * @subpackage GUI_HTML
8: *
9: * @author Simon Sprankel
10: * @copyright four for business AG <www.4fb.de>
11: * @license http://www.contenido.org/license/LIZENZ.txt
12: * @link http://www.4fb.de
13: * @link http://www.contenido.org
14: */
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: /**
19: * cHTMLRadiobutton class represents a radio button.
20: *
21: * @package Core
22: * @subpackage GUI_HTML
23: */
24: class cHTMLRadiobutton extends cHTMLFormElement {
25:
26: /**
27: * Values for the check box
28: *
29: * @var string
30: */
31: protected $_value;
32:
33: /**
34: * The text for the corresponding label
35: *
36: * @var string
37: */
38: protected $_labelText;
39:
40: /**
41: * Constructor to create an instance of this class.
42: *
43: * Creates an HTML radio button element.
44: *
45: * @param string $name
46: * Name of the element
47: * @param string $value
48: * Value of the radio button
49: * @param string $id [optional]
50: * ID of the element
51: * @param bool $checked [optional]
52: * Is element checked?
53: * @param string $disabled [optional]
54: * Item disabled flag (non-empty to set disabled)
55: * @param string $tabindex [optional]
56: * Tab index for form elements
57: * @param string $accesskey [optional]
58: * Key to access the field
59: * @param string $class [optional]
60: * the class of this element
61: */
62: public function __construct($name, $value, $id = '', $checked = false, $disabled = false, $tabindex = NULL, $accesskey = '', $class = '') {
63: parent::__construct($name, $id, $disabled, $tabindex, $accesskey);
64: $this->_tag = 'input';
65: $this->_value = $value;
66: $this->_contentlessTag = true;
67:
68: $this->setChecked($checked);
69: $this->updateAttribute('type', 'radio');
70: $this->updateAttribute('value', $value);
71: $this->setClass($class);
72: }
73:
74: /**
75: * Sets the checked flag.
76: *
77: * @param bool $checked
78: * If true, the "checked" attribute will be assigned.
79: * @return cHTMLRadiobutton
80: * $this for chaining
81: */
82: public function setChecked($checked) {
83: if ($checked == true) {
84: return $this->updateAttribute('checked', 'checked');
85: } else {
86: return $this->removeAttribute('checked');
87: }
88: }
89:
90: /**
91: * Sets a custom label text
92: *
93: * @param string $text
94: * Text to display
95: * @return cHTMLRadiobutton
96: * $this for chaining
97: */
98: public function setLabelText($text) {
99: $this->_labelText = $text;
100:
101: return $this;
102: }
103:
104: /**
105: * Renders the option element.
106: * Note:
107: *
108: * If this element has an ID, the value (which equals the text displayed)
109: * will be rendered as seperate HTML label, if not, it will be displayed
110: * as regular text. Displaying the value can be turned off via the
111: * parameter.
112: *
113: * @param bool $renderLabel [optional]
114: * If true, renders a label
115: * @return string
116: * Rendered HTML
117: */
118: public function toHtml($renderLabel = true) {
119: $attributes = $this->getAttributes(true);
120:
121: if ($renderLabel == false) {
122: return $this->fillSkeleton($attributes);
123: }
124:
125: $id = $this->getAttribute('id');
126:
127: $renderedLabel = '';
128:
129: if ($id != '') {
130: $label = new cHTMLLabel($this->_value, $this->getAttribute('id'));
131:
132: if ($this->_labelText != '') {
133: $label->text = $this->_labelText;
134: }
135:
136: $renderedLabel = $label->toHtml();
137: } else {
138: $renderedLabel = $this->_value;
139: }
140:
141: return $this->fillSkeleton($attributes) . $renderedLabel;
142: }
143:
144: }
145: