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: */
53: public function __construct($name, $value, $id = '', $checked = false, $disabled = false, $tabindex = NULL, $accesskey = '', $class = '') {
54: parent::__construct($name, $id, $disabled, $tabindex, $accesskey);
55: $this->_tag = 'input';
56: $this->_value = $value;
57: $this->_contentlessTag = true;
58:
59: $this->setChecked($checked);
60: $this->updateAttribute('type', 'checkbox');
61: $this->updateAttribute('value', $value);
62: $this->setClass($class);
63: }
64:
65: /**
66: * Sets the checked flag.
67: *
68: * @param bool $checked If true, the "checked" attribute will be assigned.
69: * @return cHTMLCheckbox $this
70: */
71: public function setChecked($checked) {
72: if ($checked == true) {
73: return $this->updateAttribute('checked', 'checked');
74: } else {
75: return $this->removeAttribute('checked');
76: }
77: }
78:
79: /**
80: * Sets a custom label text
81: *
82: * @param string $text Text to display
83: * @return cHTMLCheckbox $this
84: */
85: public function setLabelText($text) {
86: $this->_labelText = $text;
87:
88: return $this;
89: }
90:
91: /**
92: * Renders the checkbox element.
93: * Note:
94: *
95: * If this element has an ID, the value (which equals the text displayed)
96: * will be rendered as seperate HTML label, if not, it will be displayed
97: * as regular text. Displaying the value can be turned off via the
98: * parameter.
99: *
100: * @param bool $renderlabel If true, renders a label
101: * @return string Rendered HTML
102: */
103: public function toHtml($renderlabel = true) {
104: $id = $this->getAttribute('id');
105:
106: $renderedLabel = '';
107:
108: if ($renderlabel == true) {
109: if ($id != '') {
110: $label = new cHTMLLabel($this->_value, $this->getAttribute('id'));
111:
112: $label->setClass($this->getAttribute('class'));
113:
114: if ($this->_labelText != '') {
115: $label->text = $this->_labelText;
116: }
117:
118: $renderedLabel = $label->toHtml();
119: } else {
120:
121: $renderedLabel = $this->_value;
122:
123: if ($this->_labelText != '') {
124: $label = new cHTMLLabel($this->_value, $this->getAttribute('id'));
125: $label->text = $this->_labelText;
126: $renderedLabel = $label->toHtml();
127: }
128: }
129:
130: $result = new cHTMLDiv(parent::toHTML() . $renderedLabel);
131: $result->setClass('checkbox_wrapper');
132: return $result->render();
133: } else {
134: return parent::toHTML();
135: }
136: }
137:
138: }
139: