2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.0 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Author: Adam Daniel <adaniel1@eesus.jnj.com> |
17 // +----------------------------------------------------------------------+
21 require_once 'PEAR.php';
22 require_once 'HTML/Common.php';
25 * Class to dynamically create an HTML SELECT
27 * @author Adam Daniel <adaniel1@eesus.jnj.com>
32 class HTML_Select extends HTML_Common
36 * Contains the select options
42 var $_options = array();
45 * Default values of the SELECT
51 var $_values = array();
56 * @param string $name (optional)Name attribute of the SELECT
57 * @param int $size (optional) Size attribute of the SELECT
58 * @param bool $multiple (optional)Whether the select will allow multiple
60 * @param mixed $attributes (optional)Either a typical HTML attribute string
61 * or an associative array
62 * @param int $tabOffset (optional)Number of tabs to offset HTML source
68 function HTML_Select($name = '', $size = 1, $multiple = false, $attributes = null, $tabOffset = 0)
70 HTML_Common::HTML_Common($attributes, $tabOffset);
71 $attr = array('name' => $name, 'size' => $size);
73 $attr['multiple'] = 'multiple';
75 $this->updateAttributes($attr);
76 $this->setSelectedValues(array());
80 * Returns the current API version
93 * Sets the default values of the select box
95 * @param mixed $values Array or comma delimited string of selected values
101 function setSelectedValues($values)
103 if (is_string($values)) {
104 $values = split("[ ]?,[ ]?", $values);
106 $this->_values = $values;
110 * Returns an array of the selected values
114 * @return array of selected values
117 function getSelectedValues()
119 return $this->_values;
123 * Adds a new OPTION to the SELECT
125 * @param string $text Display text for the OPTION
126 * @param string $value Value for the OPTION
127 * @param bool $selected Whether the option is selected or not
128 * @param mixed $attributes Either a typical HTML attribute string
129 * or an associative array
135 function addOption($text, $value, $selected = false, $attributes = null)
137 if ($selected && !in_array($value, $this->_values)) {
138 $this->_values[] = $value;
141 $attributes = $this->_parseAttributes($attributes);
142 $attr['value'] = $value;
143 $this->_updateAttrArray($attributes, $attr);
144 $this->_options[] = array('text' => $text, 'attr' => $attributes);
148 * Loads the options from an associative array
150 * @param array $arr Associative array of options
151 * @param mixed $values (optional) Array or comma delimited string of selected values
154 * @return PEAR_Error on error or true
157 function loadArray($arr, $values=null)
159 if (!is_array($arr)) {
160 return new PEAR_ERROR('First argument to HTML_Select::loadArray is not a valid array');
162 if (isset($values)) {
163 $this->setSelectedValues($values);
165 while (list($key, $value) = each($arr)) {
166 $this->addOption($key, $value);
172 * Loads the options from an array with numeric keys, using the
173 * array values as the form values as well as labels.
175 * @param array $arr Array of options
176 * @param mixed $values (optional) Array or comma delimited string of selected values
179 * @return PEAR_Error on error or true
182 function loadValueArray($arr, $values = null)
184 if (!is_array($arr)) {
185 return new PEAR_ERROR("First argument to HTML_Select::loadArray is not a valid array");
187 if (isset($values)) {
188 $this->setSelectedValues($values);
190 foreach ($arr as $value) {
191 $this->addOption($value, $value);
197 * Loads the options from DB_result object
199 * If no column names are specified the first two columns of the result are
200 * used as the text and value columns respectively
201 * @param object $result DB_result object
202 * @param string $textCol (optional) Name of column to display as the OPTION text
203 * @param string $valueCol (optional) Name of column to use as the OPTION value
204 * @param mixed $values (optional) Array or comma delimited string of selected values
207 * @return PEAR_Error on error or true
210 function loadDbResult(&$result, $textCol=null, $valueCol=null, $values=null)
212 include_once 'DB.php';
214 if (!is_object($result) || (get_class($result) != "db_result" &&
215 is_subclass_of($result, "db_result"))) {
216 return new PEAR_ERROR("First argument to HTML_Select::loadDbResult is not a valid DB_result");
218 if (isset($values)) {
219 $this->setSelectedValues($values);
221 $fetchMode = ($textCol && $valueCol) ? DB_FETCHMODE_ASSOC : DB_FETCHMODE_DEFAULT;
222 while (is_array($row = $result->fetchRow($fetchMode)) ) {
223 if ($fetchMode == DB_FETCHMODE_ASSOC) {
224 $this->addOption($row[$textCol], $row[$valueCol]);
226 $this->addOption($row[0], $row[1]);
233 * Queries a database and loads the options from the results
235 * @param mixed $conn Either an existing DB connection or a valid dsn
236 * @param string $sql SQL query string
237 * @param string $textCol (optional) Name of column to display as the OPTION text
238 * @param string $valueCol (optional) Name of column to use as the OPTION value
239 * @param mixed $values (optional) Array or comma delimited string of selected values
245 function loadQuery(&$conn, $sql, $textCol=null, $valueCol=null, $values=null)
247 include_once 'DB.php';
249 if (is_string($conn)) {
250 $dbConn = &DB::connect($conn, true);
251 if (DB::isError($dbConn)) return $dbConn;
252 } elseif (is_subclass_of($conn, "db_common")) {
255 return $this->raiseError("Argument 1 of HTML_Select::loadQuery is not a valid type");
257 $result = @$dbConn->query($sql);
258 if (DB::isError($result)) return $result;
259 return $this->loadDbResult($result, $textCol, $valueCol, $values);
263 * Loads options from different types of data sources
265 * This method is a simulated overloaded method. The arguments, other than the
266 * first are optional and only mean something depending on the type of the first argument.
267 * If the first argument is an array then all arguments are passed in order to loadArray.
268 * If the first argument is a db_result then all arguments are passed in order to loadDbResult.
269 * If the first argument is a string or a DB connection then all arguments are
270 * passed in order to loadQuery.
271 * @param mixed $options Options source currently supports assoc array or DB_result
272 * @param mixed $param1 (optional) See function detail
273 * @param mixed $param2 (optional) See function detail
274 * @param mixed $param3 (optional) See function detail
275 * @param mixed $param4 (optional) See function detail
278 * @return PEAR_Error on error or true
281 function load(&$options, $param1=null, $param2=null, $param3=null, $param4=null)
284 case is_array($options):
285 return $this->loadArray($options, $param1);
287 case (get_class($options) == "db_result" || is_subclass_of($options, "db_result")):
288 return $this->loadDbResult($options, $param1, $param2, $param3);
290 case (is_string($options) || is_subclass_of($options, "db_common")):
291 return $this->loadQuery($options, $param1, $param2, $param3, $param4);
297 * Returns the SELECT in HTML
306 $tabs = $this->_getTabs();
307 $name = $this->_attributes['name'];
309 if ($this->_comment) {
310 $strHtml .= "<!-- $this->_comment -->\n$tabs";
313 '<select' . $this->_getAttrString($this->_attributes) . '>';
314 foreach ($this->_options as $option) {
315 $attrString = $this->_getAttrString($option['attr']);
317 '<option' . $attrString
318 . (@in_array($option['attr']['value'], $this->_values)
321 . '>' . htmlspecialchars($option['text']) . '</option>';
323 $strHtml .= '</select>';