select.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Class to dynamically create an HTML SELECT
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.01 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category HTML
  15. * @package HTML_QuickForm
  16. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  17. * @author Bertrand Mansion <bmansion@mamasam.com>
  18. * @author Alexey Borzov <avb@php.net>
  19. * @copyright 2001-2009 The PHP Group
  20. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  21. * @version CVS: $Id: select.php,v 1.34 2009/04/04 21:34:04 avb Exp $
  22. * @link http://pear.php.net/package/HTML_QuickForm
  23. */
  24. /**
  25. * Base class for form elements
  26. */
  27. require_once 'HTML/QuickForm/element.php';
  28. /**
  29. * Class to dynamically create an HTML SELECT
  30. *
  31. * @category HTML
  32. * @package HTML_QuickForm
  33. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  34. * @author Bertrand Mansion <bmansion@mamasam.com>
  35. * @author Alexey Borzov <avb@php.net>
  36. * @version Release: 3.2.11
  37. * @since 1.0
  38. */
  39. class HTML_QuickForm_select extends HTML_QuickForm_element {
  40. // {{{ properties
  41. /**
  42. * Contains the select options
  43. *
  44. * @var array
  45. * @since 1.0
  46. * @access private
  47. */
  48. var $_options = array();
  49. var $_optgroups = array();
  50. /**
  51. * Default values of the SELECT
  52. *
  53. * @var string
  54. * @since 1.0
  55. * @access private
  56. */
  57. var $_values = null;
  58. // }}}
  59. // {{{ constructor
  60. /**
  61. * Class constructor
  62. *
  63. * @param string Select name attribute
  64. * @param mixed Label(s) for the select
  65. * @param mixed Data to be used to populate options
  66. * @param mixed Either a typical HTML attribute string or an associative array
  67. * @since 1.0
  68. * @access public
  69. * @return void
  70. */
  71. function HTML_QuickForm_select($elementName=null, $elementLabel=null, $options=null, $attributes=null)
  72. {
  73. HTML_QuickForm_element::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
  74. $this->_persistantFreeze = true;
  75. $this->_type = 'select';
  76. if (isset($options)) {
  77. $this->load($options);
  78. }
  79. } //end constructor
  80. // }}}
  81. // {{{ apiVersion()
  82. /**
  83. * Returns the current API version
  84. *
  85. * @since 1.0
  86. * @access public
  87. * @return double
  88. */
  89. function apiVersion()
  90. {
  91. return 2.3;
  92. } //end func apiVersion
  93. // }}}
  94. // {{{ setSelected()
  95. /**
  96. * Sets the default values of the select box
  97. *
  98. * @param mixed $values Array or comma delimited string of selected values
  99. * @since 1.0
  100. * @access public
  101. * @return void
  102. */
  103. function setSelected($values)
  104. {
  105. if (is_string($values) && $this->getMultiple()) {
  106. $values = split("[ ]?,[ ]?", $values);
  107. }
  108. if (is_array($values)) {
  109. $this->_values = array_values($values);
  110. } else {
  111. $this->_values = array($values);
  112. }
  113. } //end func setSelected
  114. // }}}
  115. // {{{ getSelected()
  116. /**
  117. * Returns an array of the selected values
  118. *
  119. * @since 1.0
  120. * @access public
  121. * @return array of selected values
  122. */
  123. function getSelected()
  124. {
  125. return $this->_values;
  126. } // end func getSelected
  127. // }}}
  128. // {{{ setName()
  129. /**
  130. * Sets the input field name
  131. *
  132. * @param string $name Input field name attribute
  133. * @since 1.0
  134. * @access public
  135. * @return void
  136. */
  137. function setName($name)
  138. {
  139. $this->updateAttributes(array('name' => $name));
  140. } //end func setName
  141. // }}}
  142. // {{{ getName()
  143. /**
  144. * Returns the element name
  145. *
  146. * @since 1.0
  147. * @access public
  148. * @return string
  149. */
  150. function getName()
  151. {
  152. return $this->getAttribute('name');
  153. } //end func getName
  154. // }}}
  155. // {{{ getPrivateName()
  156. /**
  157. * Returns the element name (possibly with brackets appended)
  158. *
  159. * @since 1.0
  160. * @access public
  161. * @return string
  162. */
  163. function getPrivateName()
  164. {
  165. if ($this->getAttribute('multiple')) {
  166. return $this->getName() . '[]';
  167. } else {
  168. return $this->getName();
  169. }
  170. } //end func getPrivateName
  171. // }}}
  172. // {{{ setValue()
  173. /**
  174. * Sets the value of the form element
  175. *
  176. * @param mixed $values Array or comma delimited string of selected values
  177. * @since 1.0
  178. * @access public
  179. * @return void
  180. */
  181. function setValue($value)
  182. {
  183. $this->setSelected($value);
  184. } // end func setValue
  185. // }}}
  186. // {{{ getValue()
  187. /**
  188. * Returns an array of the selected values
  189. *
  190. * @since 1.0
  191. * @access public
  192. * @return array of selected values
  193. */
  194. function getValue()
  195. {
  196. return $this->_values;
  197. } // end func getValue
  198. // }}}
  199. // {{{ setSize()
  200. /**
  201. * Sets the select field size, only applies to 'multiple' selects
  202. *
  203. * @param int $size Size of select field
  204. * @since 1.0
  205. * @access public
  206. * @return void
  207. */
  208. function setSize($size)
  209. {
  210. $this->updateAttributes(array('size' => $size));
  211. } //end func setSize
  212. // }}}
  213. // {{{ getSize()
  214. /**
  215. * Returns the select field size
  216. *
  217. * @since 1.0
  218. * @access public
  219. * @return int
  220. */
  221. function getSize()
  222. {
  223. return $this->getAttribute('size');
  224. } //end func getSize
  225. // }}}
  226. // {{{ setMultiple()
  227. /**
  228. * Sets the select mutiple attribute
  229. *
  230. * @param bool $multiple Whether the select supports multi-selections
  231. * @since 1.2
  232. * @access public
  233. * @return void
  234. */
  235. function setMultiple($multiple)
  236. {
  237. if ($multiple) {
  238. $this->updateAttributes(array('multiple' => 'multiple'));
  239. } else {
  240. $this->removeAttribute('multiple');
  241. }
  242. } //end func setMultiple
  243. // }}}
  244. // {{{ getMultiple()
  245. /**
  246. * Returns the select mutiple attribute
  247. *
  248. * @since 1.2
  249. * @access public
  250. * @return bool true if multiple select, false otherwise
  251. */
  252. function getMultiple()
  253. {
  254. return (bool)$this->getAttribute('multiple');
  255. } //end func getMultiple
  256. // }}}
  257. // {{{ addOption()
  258. /**
  259. * Adds a new OPTION to the SELECT
  260. *
  261. * @param string $text Display text for the OPTION
  262. * @param string $value Value for the OPTION
  263. * @param mixed $attributes Either a typical HTML attribute string
  264. * or an associative array
  265. * @since 1.0
  266. * @access public
  267. * @return void
  268. */
  269. function addOption($text, $value, $attributes = null, $return_array = false)
  270. {
  271. if (null === $attributes) {
  272. $attributes = array('value' => (string)$value);
  273. } else {
  274. $attributes = $this->_parseAttributes($attributes);
  275. if (isset($attributes['selected'])) {
  276. // the 'selected' attribute will be set in toHtml()
  277. $this->_removeAttr('selected', $attributes);
  278. if (is_null($this->_values)) {
  279. $this->_values = array($value);
  280. } elseif (!in_array($value, $this->_values)) {
  281. $this->_values[] = $value;
  282. }
  283. }
  284. $this->_updateAttrArray($attributes, array('value' => (string)$value));
  285. }
  286. if ($return_array) {
  287. return array('text' => $text, 'attr' => $attributes);
  288. } else {
  289. $this->_options[] = array('text' => $text, 'attr' => $attributes);
  290. }
  291. } // end func addOption
  292. /**
  293. * Adds a new OPTION to the SELECT
  294. *
  295. * @param string $text Display text for the OPTION
  296. * @param string $value Value for the OPTION
  297. * @param mixed $attributes Either a typical HTML attribute string
  298. * or an associative array
  299. * @since 1.0
  300. * @access public
  301. * @return void
  302. */
  303. function addOptGroup($options, $label)
  304. {
  305. foreach ($options as $option) {
  306. $this->addOption($option['text'], $option['value'], $option, true);
  307. }
  308. $this->_optgroups[] = array('label' => $label, 'options' => $options);
  309. }
  310. /**
  311. * Loads the options from an associative array
  312. *
  313. * @param array $arr Associative array of options
  314. * @param mixed $values (optional) Array or comma delimited string of selected values
  315. * @since 1.0
  316. * @access public
  317. * @return PEAR_Error on error or true
  318. * @throws PEAR_Error
  319. */
  320. function loadArray($arr, $values=null)
  321. {
  322. if (!is_array($arr)) {
  323. return PEAR::raiseError('Argument 1 of HTML_Select::loadArray is not a valid array');
  324. }
  325. if (isset($values)) {
  326. $this->setSelected($values);
  327. }
  328. foreach ($arr as $key => $val) {
  329. // Warning: new API since release 2.3
  330. $this->addOption($val, $key);
  331. }
  332. return true;
  333. } // end func loadArray
  334. // }}}
  335. // {{{ loadDbResult()
  336. /**
  337. * Loads the options from DB_result object
  338. *
  339. * If no column names are specified the first two columns of the result are
  340. * used as the text and value columns respectively
  341. * @param object $result DB_result object
  342. * @param string $textCol (optional) Name of column to display as the OPTION text
  343. * @param string $valueCol (optional) Name of column to use as the OPTION value
  344. * @param mixed $values (optional) Array or comma delimited string of selected values
  345. * @since 1.0
  346. * @access public
  347. * @return PEAR_Error on error or true
  348. * @throws PEAR_Error
  349. */
  350. function loadDbResult(&$result, $textCol=null, $valueCol=null, $values=null)
  351. {
  352. if (!is_object($result) || !is_a($result, 'db_result')) {
  353. return PEAR::raiseError('Argument 1 of HTML_Select::loadDbResult is not a valid DB_result');
  354. }
  355. if (isset($values)) {
  356. $this->setValue($values);
  357. }
  358. $fetchMode = ($textCol && $valueCol) ? DB_FETCHMODE_ASSOC : DB_FETCHMODE_ORDERED;
  359. while (is_array($row = $result->fetchRow($fetchMode)) ) {
  360. if ($fetchMode == DB_FETCHMODE_ASSOC) {
  361. $this->addOption($row[$textCol], $row[$valueCol]);
  362. } else {
  363. $this->addOption($row[0], $row[1]);
  364. }
  365. }
  366. return true;
  367. } // end func loadDbResult
  368. // }}}
  369. // {{{ loadQuery()
  370. /**
  371. * Queries a database and loads the options from the results
  372. *
  373. * @param mixed $conn Either an existing DB connection or a valid dsn
  374. * @param string $sql SQL query string
  375. * @param string $textCol (optional) Name of column to display as the OPTION text
  376. * @param string $valueCol (optional) Name of column to use as the OPTION value
  377. * @param mixed $values (optional) Array or comma delimited string of selected values
  378. * @since 1.1
  379. * @access public
  380. * @return void
  381. * @throws PEAR_Error
  382. */
  383. function loadQuery(&$conn, $sql, $textCol=null, $valueCol=null, $values=null)
  384. {
  385. if (is_string($conn)) {
  386. require_once('DB.php');
  387. $dbConn = &DB::connect($conn, true);
  388. if (DB::isError($dbConn)) {
  389. return $dbConn;
  390. }
  391. } elseif (is_subclass_of($conn, "db_common")) {
  392. $dbConn = &$conn;
  393. } else {
  394. return PEAR::raiseError('Argument 1 of HTML_Select::loadQuery is not a valid type');
  395. }
  396. $result = $dbConn->query($sql);
  397. if (DB::isError($result)) {
  398. return $result;
  399. }
  400. $this->loadDbResult($result, $textCol, $valueCol, $values);
  401. $result->free();
  402. if (is_string($conn)) {
  403. $dbConn->disconnect();
  404. }
  405. return true;
  406. } // end func loadQuery
  407. // }}}
  408. // {{{ load()
  409. /**
  410. * Loads options from different types of data sources
  411. *
  412. * This method is a simulated overloaded method. The arguments, other than the
  413. * first are optional and only mean something depending on the type of the first argument.
  414. * If the first argument is an array then all arguments are passed in order to loadArray.
  415. * If the first argument is a db_result then all arguments are passed in order to loadDbResult.
  416. * If the first argument is a string or a DB connection then all arguments are
  417. * passed in order to loadQuery.
  418. * @param mixed $options Options source currently supports assoc array or DB_result
  419. * @param mixed $param1 (optional) See function detail
  420. * @param mixed $param2 (optional) See function detail
  421. * @param mixed $param3 (optional) See function detail
  422. * @param mixed $param4 (optional) See function detail
  423. * @since 1.1
  424. * @access public
  425. * @return PEAR_Error on error or true
  426. * @throws PEAR_Error
  427. */
  428. function load(&$options, $param1=null, $param2=null, $param3=null, $param4=null)
  429. {
  430. switch (true) {
  431. case is_array($options):
  432. return $this->loadArray($options, $param1);
  433. break;
  434. case (is_a($options, 'db_result')):
  435. return $this->loadDbResult($options, $param1, $param2, $param3);
  436. break;
  437. // Disabled by Chamilo team, 16-MAR-2010.
  438. // TODO: To be verified (why it has been disabled).
  439. //case (is_string($options) && !empty($options) || is_subclass_of($options, "db_common")):
  440. // return $this->loadQuery($options, $param1, $param2, $param3, $param4);
  441. // break;
  442. //
  443. }
  444. } // end func load
  445. // }}}
  446. // {{{ toHtml()
  447. /**
  448. * Returns the SELECT in HTML
  449. *
  450. * @since 1.0
  451. * @access public
  452. * @return string
  453. */
  454. function toHtml()
  455. {
  456. if ($this->_flagFrozen) {
  457. return $this->getFrozenHtml();
  458. } else {
  459. $tabs = $this->_getTabs();
  460. $strHtml = '';
  461. if ($this->getComment() != '') {
  462. $strHtml .= $tabs . '<!-- ' . $this->getComment() . " //-->\n";
  463. }
  464. if (!$this->getMultiple()) {
  465. $attrString = $this->_getAttrString($this->_attributes);
  466. } else {
  467. $myName = $this->getName();
  468. $this->setName($myName . '[]');
  469. $attrString = $this->_getAttrString($this->_attributes);
  470. $this->setName($myName);
  471. }
  472. $strHtml .= $tabs . '<select' . $attrString . ">\n";
  473. $strValues = is_array($this->_values)? array_map('strval', $this->_values): array();
  474. foreach ($this->_options as $option) {
  475. if (!empty($strValues) && in_array($option['attr']['value'], $strValues, true)) {
  476. $option['attr']['selected'] = 'selected';
  477. }
  478. $strHtml .= $tabs . "\t<option" . $this->_getAttrString($option['attr']) . '>' .
  479. $option['text'] . "</option>\n";
  480. }
  481. foreach ($this->_optgroups as $optgroup) {
  482. $strHtml .= $tabs . "<optgroup label=" . $optgroup['label'].">";
  483. foreach ($optgroup['options'] as $option) {
  484. $text = $option['text'];
  485. unset($option['text']);
  486. $strHtml .= $tabs . " <option" . $this->_getAttrString($option) . '>' .$text . "</option>";
  487. }
  488. $strHtml .= "</optgroup>";
  489. }
  490. return $strHtml . $tabs . '</select>';
  491. }
  492. }
  493. /**
  494. * Returns the value of field without HTML tags
  495. *
  496. * @since 1.0
  497. * @access public
  498. * @return string
  499. */
  500. function getFrozenHtml()
  501. {
  502. $value = array();
  503. if (is_array($this->_values)) {
  504. foreach ($this->_values as $key => $val) {
  505. for ($i = 0, $optCount = count($this->_options); $i < $optCount; $i++) {
  506. if (0 == strcmp($val, $this->_options[$i]['attr']['value'])) {
  507. $value[$key] = $this->_options[$i]['text'];
  508. break;
  509. }
  510. }
  511. }
  512. }
  513. $html = empty($value)? '&nbsp;': join('<br />', $value);
  514. if ($this->_persistantFreeze) {
  515. $name = $this->getPrivateName();
  516. // Only use id attribute if doing single hidden input
  517. if (1 == count($value)) {
  518. $id = $this->getAttribute('id');
  519. $idAttr = isset($id)? array('id' => $id): array();
  520. } else {
  521. $idAttr = array();
  522. }
  523. foreach ($value as $key => $item) {
  524. $html .= '<input' . $this->_getAttrString(array(
  525. 'type' => 'hidden',
  526. 'name' => $name,
  527. 'value' => $this->_values[$key]
  528. ) + $idAttr) . ' />';
  529. }
  530. }
  531. return $html;
  532. } //end func getFrozenHtml
  533. // }}}
  534. // {{{ exportValue()
  535. /**
  536. * We check the options and return only the values that _could_ have been
  537. * selected. We also return a scalar value if select is not "multiple"
  538. */
  539. function exportValue(&$submitValues, $assoc = false)
  540. {
  541. $value = $this->_findValue($submitValues);
  542. if (is_null($value)) {
  543. $value = $this->getValue();
  544. } elseif(!is_array($value)) {
  545. $value = array($value);
  546. }
  547. if (is_array($value) && !empty($this->_options)) {
  548. $cleanValue = null;
  549. foreach ($value as $v) {
  550. for ($i = 0, $optCount = count($this->_options); $i < $optCount; $i++) {
  551. if (0 == strcmp($v, $this->_options[$i]['attr']['value'])) {
  552. $cleanValue[] = $v;
  553. break;
  554. }
  555. }
  556. }
  557. } else {
  558. $cleanValue = $value;
  559. }
  560. if (is_array($cleanValue) && !$this->getMultiple()) {
  561. return $this->_prepareValue($cleanValue[0], $assoc);
  562. } else {
  563. return $this->_prepareValue($cleanValue, $assoc);
  564. }
  565. }
  566. // }}}
  567. // {{{ onQuickFormEvent()
  568. function onQuickFormEvent($event, $arg, &$caller)
  569. {
  570. if ('updateValue' == $event) {
  571. $value = $this->_findValue($caller->_constantValues);
  572. if (null === $value) {
  573. $value = $this->_findValue($caller->_submitValues);
  574. // Fix for bug #4465 & #5269
  575. // XXX: should we push this to element::onQuickFormEvent()?
  576. if (null === $value && (!$caller->isSubmitted() || !$this->getMultiple())) {
  577. $value = $this->_findValue($caller->_defaultValues);
  578. }
  579. }
  580. if (null !== $value) {
  581. $this->setValue($value);
  582. }
  583. return true;
  584. } else {
  585. return parent::onQuickFormEvent($event, $arg, $caller);
  586. }
  587. }
  588. // }}}
  589. } //end class HTML_QuickForm_select
  590. ?>