1: <?php
2:
3: /**
4: * This file contains the cHTMLOptionElement 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: * cHTMLOptionElement class represents a select option element.
20: *
21: * @package Core
22: * @subpackage GUI_HTML
23: */
24: class cHTMLOptionElement extends cHTMLFormElement {
25:
26: /**
27: * Title to display
28: *
29: * @var string
30: */
31: protected $_title;
32:
33: /**
34: * Constructor to create an instance of this class.
35: *
36: * Creates an HTML option element.
37: *
38: * @param string $title
39: * Displayed title of the element
40: * @param string $value
41: * Value of the option
42: * @param bool $selected [optional]
43: * If true, element is selected
44: * @param bool $disabled [optional]
45: * If true, element is disabled
46: * @param string $class [optional]
47: * the class of this element
48: */
49: public function __construct($title, $value, $selected = false, $disabled = false, $class = '') {
50: cHTML::__construct();
51: $this->_tag = 'option';
52: $this->_title = $title;
53:
54: $this->updateAttribute('value', $value);
55: $this->_contentlessTag = false;
56:
57: $this->setSelected($selected);
58: $this->setDisabled($disabled);
59: $this->setClass($class);
60: }
61:
62: /**
63: * Sets the selected flag
64: *
65: * @param bool $selected
66: * If true, adds the "selected" attribute
67: * @return cHTMLOptionElement
68: * $this for chaining
69: */
70: public function setSelected($selected) {
71: if ($selected == true) {
72: return $this->updateAttribute('selected', 'selected');
73: } else {
74: return $this->removeAttribute('selected');
75: }
76: }
77:
78: /**
79: * Checks whether this option element is selected.
80: *
81: * @return bool
82: * whether this option element is selected
83: */
84: public function isSelected() {
85: return $this->getAttribute('selected') === 'selected';
86: }
87:
88: /**
89: * Sets the disabled flag
90: *
91: * @param bool $disabled
92: * If true, adds the "disabled" attribute
93: * @return cHTMLOptionElement
94: * $this for chaining
95: */
96: public function setDisabled($disabled) {
97: if ($disabled == true) {
98: return $this->updateAttribute('disabled', 'disabled');
99: } else {
100: return $this->removeAttribute('disabled');
101: }
102: }
103:
104: /**
105: * Renders the option element.
106: * Note:
107: * the cHTMLSelectElement renders the options by itself.
108: *
109: * @return string
110: * Rendered HTML
111: */
112: public function toHtml() {
113: $this->_setContent($this->_title);
114:
115: return parent::toHtml();
116: }
117:
118: }
119: