Select.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  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. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Select.php,v 1.14 2002/10/24 18:49:30 tal Exp $
  20. require_once 'PEAR.php';
  21. require_once 'HTML/Common.php';
  22. /**
  23. * Class to dynamically create an HTML SELECT
  24. *
  25. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  26. * @version 1.2
  27. * @since PHP4.04pl1
  28. * @access public
  29. */
  30. class HTML_Select extends HTML_Common
  31. {
  32. /**
  33. * Contains the select options
  34. *
  35. * @var array
  36. * @since 1.0
  37. * @access private
  38. */
  39. var $_options = array();
  40. /**
  41. * Default values of the SELECT
  42. *
  43. * @var string
  44. * @since 1.0
  45. * @access private
  46. */
  47. var $_values = array();
  48. /**
  49. * Class constructor
  50. *
  51. * @param string $name (optional)Name attribute of the SELECT
  52. * @param int $size (optional) Size attribute of the SELECT
  53. * @param bool $multiple (optional)Whether the select will allow multiple
  54. * selections or not
  55. * @param mixed $attributes (optional)Either a typical HTML attribute string
  56. * or an associative array
  57. * @param int $tabOffset (optional)Number of tabs to offset HTML source
  58. * @since 1.0
  59. * @access public
  60. * @return void
  61. * @throws
  62. */
  63. function HTML_Select($name = '', $size = 1, $multiple = false, $attributes = null, $tabOffset = 0)
  64. {
  65. HTML_Common::HTML_Common($attributes, $tabOffset);
  66. $attr = array('name' => $name, 'size' => $size);
  67. if ($multiple) {
  68. $attr['multiple'] = 'multiple';
  69. }
  70. $this->updateAttributes($attr);
  71. $this->setSelectedValues(array());
  72. }
  73. /**
  74. * Returns the current API version
  75. *
  76. * @since 1.0
  77. * @access public
  78. * @return double
  79. * @throws
  80. */
  81. function apiVersion()
  82. {
  83. return 1.2;
  84. }
  85. /**
  86. * Sets the default values of the select box
  87. *
  88. * @param mixed $values Array or comma delimited string of selected values
  89. * @since 1.0
  90. * @access public
  91. * @return void
  92. * @throws
  93. */
  94. function setSelectedValues($values)
  95. {
  96. if (is_string($values)) {
  97. $values = split("[ ]?,[ ]?", $values);
  98. }
  99. $this->_values = $values;
  100. }
  101. /**
  102. * Returns an array of the selected values
  103. *
  104. * @since 1.0
  105. * @access public
  106. * @return array of selected values
  107. * @throws
  108. */
  109. function getSelectedValues()
  110. {
  111. return $this->_values;
  112. }
  113. /**
  114. * Adds a new OPTION to the SELECT
  115. *
  116. * @param string $text Display text for the OPTION
  117. * @param string $value Value for the OPTION
  118. * @param bool $selected Whether the option is selected or not
  119. * @param mixed $attributes Either a typical HTML attribute string
  120. * or an associative array
  121. * @since 1.0
  122. * @access public
  123. * @return void
  124. * @throws
  125. */
  126. function addOption($text, $value, $selected = false, $attributes = null)
  127. {
  128. if ($selected && !in_array($value, $this->_values)) {
  129. $this->_values[] = $value;
  130. }
  131. $attributes = $this->_parseAttributes($attributes);
  132. $attr['value'] = $value;
  133. $this->_updateAttrArray($attributes, $attr);
  134. $this->_options[] = array('text' => $text, 'attr' => $attributes);
  135. }
  136. /**
  137. * Loads the options from an associative array
  138. *
  139. * @param array $arr Associative array of options
  140. * @param mixed $values (optional) Array or comma delimited string of selected values
  141. * @since 1.0
  142. * @access public
  143. * @return PEAR_Error on error or true
  144. * @throws PEAR_Error
  145. */
  146. function loadArray($arr, $values=null)
  147. {
  148. if (!is_array($arr)) {
  149. return new PEAR_ERROR('First argument to HTML_Select::loadArray is not a valid array');
  150. }
  151. if (isset($values)) {
  152. $this->setSelectedValues($values);
  153. }
  154. while (list($key, $value) = each($arr)) {
  155. $this->addOption($key, $value);
  156. }
  157. return true;
  158. }
  159. /**
  160. * Loads the options from an array with numeric keys, using the
  161. * array values as the form values as well as labels.
  162. *
  163. * @param array $arr Array of options
  164. * @param mixed $values (optional) Array or comma delimited string of selected values
  165. * @since 1.2
  166. * @access public
  167. * @return PEAR_Error on error or true
  168. * @throws PEAR_Error
  169. */
  170. function loadValueArray($arr, $values = null)
  171. {
  172. if (!is_array($arr)) {
  173. return new PEAR_ERROR("First argument to HTML_Select::loadArray is not a valid array");
  174. }
  175. if (isset($values)) {
  176. $this->setSelectedValues($values);
  177. }
  178. foreach ($arr as $value) {
  179. $this->addOption($value, $value);
  180. }
  181. return true;
  182. }
  183. /**
  184. * Loads the options from DB_result object
  185. *
  186. * If no column names are specified the first two columns of the result are
  187. * used as the text and value columns respectively
  188. * @param object $result DB_result object
  189. * @param string $textCol (optional) Name of column to display as the OPTION text
  190. * @param string $valueCol (optional) Name of column to use as the OPTION value
  191. * @param mixed $values (optional) Array or comma delimited string of selected values
  192. * @since 1.0
  193. * @access public
  194. * @return PEAR_Error on error or true
  195. * @throws PEAR_Error
  196. */
  197. function loadDbResult(&$result, $textCol=null, $valueCol=null, $values=null)
  198. {
  199. include_once 'DB.php';
  200. if (!is_object($result) || (get_class($result) != "db_result" &&
  201. is_subclass_of($result, "db_result"))) {
  202. return new PEAR_ERROR("First argument to HTML_Select::loadDbResult is not a valid DB_result");
  203. }
  204. if (isset($values)) {
  205. $this->setSelectedValues($values);
  206. }
  207. $fetchMode = ($textCol && $valueCol) ? DB_FETCHMODE_ASSOC : DB_FETCHMODE_DEFAULT;
  208. while (is_array($row = $result->fetchRow($fetchMode)) ) {
  209. if ($fetchMode == DB_FETCHMODE_ASSOC) {
  210. $this->addOption($row[$textCol], $row[$valueCol]);
  211. } else {
  212. $this->addOption($row[0], $row[1]);
  213. }
  214. }
  215. return true;
  216. }
  217. /**
  218. * Queries a database and loads the options from the results
  219. *
  220. * @param mixed $conn Either an existing DB connection or a valid dsn
  221. * @param string $sql SQL query string
  222. * @param string $textCol (optional) Name of column to display as the OPTION text
  223. * @param string $valueCol (optional) Name of column to use as the OPTION value
  224. * @param mixed $values (optional) Array or comma delimited string of selected values
  225. * @since 1.1
  226. * @access private
  227. * @return void
  228. * @throws
  229. */
  230. function loadQuery(&$conn, $sql, $textCol=null, $valueCol=null, $values=null)
  231. {
  232. include_once 'DB.php';
  233. if (is_string($conn)) {
  234. $dbConn = &DB::connect($conn, true);
  235. if (DB::isError($dbConn)) return $dbConn;
  236. } elseif (is_subclass_of($conn, "db_common")) {
  237. $dbConn = $conn;
  238. } else {
  239. return $this->raiseError("Argument 1 of HTML_Select::loadQuery is not a valid type");
  240. }
  241. $result = @$dbConn->query($sql);
  242. if (DB::isError($result)) return $result;
  243. return $this->loadDbResult($result, $textCol, $valueCol, $values);
  244. }
  245. /**
  246. * Loads options from different types of data sources
  247. *
  248. * This method is a simulated overloaded method. The arguments, other than the
  249. * first are optional and only mean something depending on the type of the first argument.
  250. * If the first argument is an array then all arguments are passed in order to loadArray.
  251. * If the first argument is a db_result then all arguments are passed in order to loadDbResult.
  252. * If the first argument is a string or a DB connection then all arguments are
  253. * passed in order to loadQuery.
  254. * @param mixed $options Options source currently supports assoc array or DB_result
  255. * @param mixed $param1 (optional) See function detail
  256. * @param mixed $param2 (optional) See function detail
  257. * @param mixed $param3 (optional) See function detail
  258. * @param mixed $param4 (optional) See function detail
  259. * @since 1.1
  260. * @access public
  261. * @return PEAR_Error on error or true
  262. * @throws PEAR_Error
  263. */
  264. function load(&$options, $param1=null, $param2=null, $param3=null, $param4=null)
  265. {
  266. switch (true) {
  267. case is_array($options):
  268. return $this->loadArray($options, $param1);
  269. break;
  270. case (get_class($options) == "db_result" || is_subclass_of($options, "db_result")):
  271. return $this->loadDbResult($options, $param1, $param2, $param3);
  272. break;
  273. case (is_string($options) || is_subclass_of($options, "db_common")):
  274. return $this->loadQuery($options, $param1, $param2, $param3, $param4);
  275. break;
  276. }
  277. }
  278. /**
  279. * Returns the SELECT in HTML
  280. *
  281. * @since 1.0
  282. * @access public
  283. * @return string
  284. * @throws
  285. */
  286. function toHtml()
  287. {
  288. $tabs = $this->_getTabs();
  289. $name = $this->_attributes['name'];
  290. $strHtml = $tabs;
  291. if ($this->_comment) {
  292. $strHtml .= "<!-- $this->_comment -->\n$tabs";
  293. }
  294. $strHtml .=
  295. '<select' . $this->_getAttrString($this->_attributes) . '>';
  296. foreach ($this->_options as $option) {
  297. if (@in_array($option['attr']['value'], $this->_values)) {
  298. $option['attr']['selected'] = 'selected';
  299. }
  300. $attrString = $this->_getAttrString($option['attr']);
  301. $strHtml .=
  302. '<option' . $attrString . '>' .
  303. htmlspecialchars($option['text']) . '</option>';
  304. }
  305. $strHtml .= '</select>';
  306. return $strHtml;
  307. }
  308. }
  309. ?>