1: <?php
  2: 
  3: /**
  4:  * This file contains the cHTMLCheckbox 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:  * 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 to create an instance of this class.
 42:      *
 43:      * Creates an HTML checkbox element.
 44:      *
 45:      * @param string $name
 46:      *         Name of the element
 47:      * @param string $value
 48:      *         Value of the checkbox
 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', 'checkbox');
 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 cHTMLCheckbox
 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 cHTMLCheckbox
 96:      *         $this for chaining
 97:      */
 98:     public function setLabelText($text) {
 99:         $this->_labelText = $text;
100: 
101:         return $this;
102:     }
103: 
104:     /**
105:      * Renders the checkbox 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:         $id = $this->getAttribute('id');
120: 
121:         $renderedLabel = '';
122: 
123:         if ($renderlabel == true) {
124:             if ($id != '') {
125:                 $label = new cHTMLLabel($this->_value, $this->getAttribute('id'));
126: 
127:                 $label->setClass($this->getAttribute('class'));
128: 
129:                 if ($this->_labelText != '') {
130:                     $label->text = $this->_labelText;
131:                 }
132: 
133:                 $renderedLabel = $label->toHtml();
134:             } else {
135: 
136:                 $renderedLabel = $this->_value;
137: 
138:                 if ($this->_labelText != '') {
139:                     $label = new cHTMLLabel($this->_value, $this->getAttribute('id'));
140:                     $label->text = $this->_labelText;
141:                     $renderedLabel = $label->toHtml();
142:                 }
143:             }
144: 
145:             $result = new cHTMLDiv(parent::toHtml() . $renderedLabel);
146:             $result->setClass('checkbox_wrapper');
147:             return $result->render();
148:         } else {
149:             return parent::toHtml();
150:         }
151:     }
152: 
153: }
154: