1: <?php
2: /**
3: * This file contains the cHTMLCheckbox 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: * cHTMLCheckbox class represents a checkbox.
20: *
21: * @package Core
22: * @subpackage GUI_HTML
23: */
24: class cHTMLCheckbox 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 checkbox element.
43: *
44: * @param string $name Name of the element
45: * @param string $value Value of the checkbox
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', 'checkbox');
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 cHTMLCheckbox $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 cHTMLCheckbox $this
85: */
86: public function setLabelText($text) {
87: $this->_labelText = $text;
88:
89: return $this;
90: }
91:
92: /**
93: * Renders the checkbox 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: $id = $this->getAttribute('id');
106:
107: $renderedLabel = '';
108:
109: if ($renderlabel == true) {
110: if ($id != '') {
111: $label = new cHTMLLabel($this->_value, $this->getAttribute('id'));
112:
113: $label->setClass($this->getAttribute('class'));
114:
115: if ($this->_labelText != '') {
116: $label->text = $this->_labelText;
117: }
118:
119: $renderedLabel = $label->toHtml();
120: } else {
121:
122: $renderedLabel = $this->_value;
123:
124: if ($this->_labelText != '') {
125: $label = new cHTMLLabel($this->_value, $this->getAttribute('id'));
126: $label->text = $this->_labelText;
127: $renderedLabel = $label->toHtml();
128: }
129: }
130:
131: $result = new cHTMLDiv(parent::toHTML() . $renderedLabel);
132: $result->setClass('checkbox_wrapper');
133: return $result->render();
134: } else {
135: return parent::toHTML();
136: }
137: }
138:
139: }
140: