1: <?php
2: /**
3: * This file contains the cHTMLRadiobutton class.
4: *
5: * @package Core
6: * @subpackage GUI_HTML
7: * @version SVN Revision $Rev:$
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.
42: * Creates an HTML radio button element.
43: *
44: * @param string $name Name of the element
45: * @param string $value Value of the radio button
46: * @param string $id ID of the element
47: * @param bool $checked Is element checked?
48: * @param string $disabled Item disabled flag (non-empty to set disabled)
49: * @param string $tabindex Tab index for form elements
50: * @param string $accesskey Key to access the field
51: * @param string $class the class of this element
52: * @return void
53: */
54: public function __construct($name, $value, $id = '', $checked = false, $disabled = false, $tabindex = NULL, $accesskey = '', $class = '') {
55: parent::__construct($name, $id, $disabled, $tabindex, $accesskey);
56: $this->_tag = 'input';
57: $this->_value = $value;
58: $this->_contentlessTag = true;
59:
60: $this->setChecked($checked);
61: $this->updateAttribute('type', 'radio');
62: $this->updateAttribute('value', $value);
63: $this->setClass($class);
64: }
65:
66: /**
67: * Sets the checked flag.
68: *
69: * @param bool $checked If true, the "checked" attribute will be assigned.
70: * @return cHTMLRadiobutton $this
71: */
72: public function setChecked($checked) {
73: if ($checked == true) {
74: return $this->updateAttribute('checked', 'checked');
75: } else {
76: return $this->removeAttribute('checked');
77: }
78: }
79:
80: /**
81: * Sets a custom label text
82: *
83: * @param string $text Text to display
84: * @return cHTMLRadiobutton $this
85: */
86: public function setLabelText($text) {
87: $this->_labelText = $text;
88:
89: return $this;
90: }
91:
92: /**
93: * Renders the option element.
94: * Note:
95: *
96: * If this element has an ID, the value (which equals the text displayed)
97: * will be rendered as seperate HTML label, if not, it will be displayed
98: * as regular text. Displaying the value can be turned off via the
99: * parameter.
100: *
101: * @param bool $renderlabel If true, renders a label
102: * @return string Rendered HTML
103: */
104: public function toHtml($renderLabel = true) {
105: $attributes = $this->getAttributes(true);
106:
107: if ($renderLabel == false) {
108: return $this->fillSkeleton($attributes);
109: }
110:
111: $id = $this->getAttribute('id');
112:
113: $renderedLabel = '';
114:
115: if ($id != '') {
116: $label = new cHTMLLabel($this->_value, $this->getAttribute('id'));
117:
118: if ($this->_labelText != '') {
119: $label->text = $this->_labelText;
120: }
121:
122: $renderedLabel = $label->toHtml();
123: } else {
124: $renderedLabel = $this->_value;
125: }
126:
127: return $this->fillSkeleton($attributes) . $renderedLabel;
128: }
129:
130: }
131: