1: <?php
2: /**
3: * This file contains the cHTMLSelectElement 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: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: /**
18: * cHTMLSelectElement class represents a select element.
19: *
20: * @package Core
21: * @subpackage GUI_HTML
22: */
23: class cHTMLSelectElement extends cHTMLFormElement {
24:
25: /**
26: * All cHTMLOptionElements
27: *
28: * @var array
29: */
30: protected $_options = array();
31:
32: /**
33: * Constructor.
34: * Creates an HTML select field (aka "DropDown").
35: *
36: * @param string $name Name of the element
37: * @param int $width Width of the select element
38: * @param string $id ID of the element
39: * @param bool $disabled Item disabled flag (non-empty to set disabled)
40: * @param string $tabindex Tab index for form elements
41: * @param string $accesskey Key to access the field
42: * @param string $class the class of this element
43: */
44: public function __construct($name, $width = '', $id = '', $disabled = false, $tabindex = NULL, $accesskey = '', $class = '') {
45: parent::__construct($name, $id, $disabled, $tabindex, $accesskey);
46: $this->_tag = 'select';
47: $this->_contentlessTag = false;
48: $this->setClass($class);
49:
50: if ($width != "") {
51: $this->appendStyleDefinition("width", $width);
52: }
53: }
54:
55: /**
56: * Automatically creates and fills cHTMLOptionElements
57: *
58: * Array format:
59: * $stuff = array(
60: * array('value', 'title'),
61: * array('value', 'title')
62: * );
63: *
64: * or regular key => value arrays:
65: * $stuff = array(
66: * 'value' => 'title',
67: * 'value' => 'title'
68: * );
69: *
70: * @param array $stuff Array with all items
71: * @return cHTMLSelectElement $this
72: */
73: public function autoFill(array $stuff) {
74: foreach ($stuff as $key => $row) {
75: if (is_array($row)) {
76: $option = new cHTMLOptionElement($row[1], $row[0]);
77: $this->addOptionElement($row[0], $option);
78: } else {
79: $option = new cHTMLOptionElement($row, $key);
80: $this->addOptionElement($key, $option);
81: }
82: }
83: return $this;
84: }
85:
86: /**
87: * Adds an cHTMLOptionElement to the number of choices at the specified
88: * position.
89: *
90: * @param string $index Index of the element
91: * @param cHTMLOptionElement $element Filled cHTMLOptionElement to add
92: * @return cHTMLSelectElement $this
93: */
94: public function addOptionElement($index, cHTMLOptionElement $element) {
95: $this->_options[$index] = $element;
96: return $this;
97: }
98:
99: /**
100: * Appends a cHTMLOptionElement to the number of choices.
101: *
102: * @param cHTMLOptionElement $element Filled cHTMLOptionElement to add
103: * @return cHTMLSelectElement $this
104: */
105: public function appendOptionElement(cHTMLOptionElement $element) {
106: $this->_options[] = $element;
107: return $this;
108: }
109:
110: /**
111: * Defines that this select element is a multiselect element.
112: *
113: * @return cHTMLSelectElement $this
114: */
115: public function setMultiselect() {
116: $name = $this->getAttribute('name');
117: $strLength = strlen($name);
118: if (substr($name, $strLength - 2, $strLength) != '[]') {
119: $this->updateAttribute('name', $name . '[]');
120: }
121: return $this->updateAttribute('multiple', 'multiple');
122: }
123:
124: /**
125: * Defines the size of this select element.
126: *
127: * @return cHTMLSelectElement $this
128: */
129: public function setSize($size) {
130: return $this->updateAttribute('size', $size);
131: }
132:
133: /**
134: * Sets a specific cHTMLOptionElement to the selected state.
135: *
136: * @param string $lvalue Specifies the "value" of the cHTMLOptionElement to
137: * set
138: * @return cHTMLSelectElement $this
139: */
140: public function setDefault($lvalue) {
141: if (is_array($lvalue)) {
142: foreach ($this->_options as $key => $value) {
143: if (in_array($value->getAttribute('value'), $lvalue)) {
144: $value->setSelected(true);
145: $this->_options[$key] = $value;
146: } else {
147: $value->setSelected(false);
148: $this->_options[$key] = $value;
149: }
150: }
151: } else {
152: foreach ($this->_options as $key => $value) {
153: if (strcmp($value->getAttribute('value'), $lvalue) == 0) {
154: $value->setSelected(true);
155: $this->_options[$key] = $value;
156: } else {
157: $value->setSelected(false);
158: $this->_options[$key] = $value;
159: }
160: }
161: }
162: return $this;
163: }
164:
165: /**
166: * Search for the selected elements
167: *
168: * @return string bool "lvalue" or false
169: */
170: public function getDefault() {
171: foreach ($this->_options as $key => $value) {
172: if ($value->isSelected()) {
173: return $key;
174: }
175: }
176: return false;
177: }
178:
179: /**
180: * Sets specified elements as selected (and all others as unselected)
181: *
182: * @param array $elements Array with "values" of the cHTMLOptionElement to
183: * set
184: * @return cHTMLSelectElement $this for chaining
185: */
186: public function setSelected(array $elements) {
187: foreach ($this->_options as $key => $option) {
188: if (in_array($option->getAttribute('value'), $elements)) {
189: $option->setSelected(true);
190: $this->_options[$key] = $option;
191: } else {
192: $option->setSelected(false);
193: $this->_options[$key] = $option;
194: }
195: }
196: return $this;
197: }
198:
199: /**
200: * Renders the select box
201: *
202: * @return string Rendered HTML
203: */
204: public function toHtml() {
205: $this->_setContent($this->_options);
206: return parent::toHTML();
207: }
208:
209: }
210: