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