select.php 20 KB

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