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