select.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. $option[] = $this->addOption($option['text'], $option['value'], null, true);
  307. }
  308. $this->_optgroups[] = array('label' => $label, 'options' => $options);
  309. } // end func addOption
  310. // }}}
  311. // {{{ loadArray()
  312. /**
  313. * Loads the options from an associative array
  314. *
  315. * @param array $arr Associative array of options
  316. * @param mixed $values (optional) Array or comma delimited string of selected values
  317. * @since 1.0
  318. * @access public
  319. * @return PEAR_Error on error or true
  320. * @throws PEAR_Error
  321. */
  322. function loadArray($arr, $values=null)
  323. {
  324. if (!is_array($arr)) {
  325. return PEAR::raiseError('Argument 1 of HTML_Select::loadArray is not a valid array');
  326. }
  327. if (isset($values)) {
  328. $this->setSelected($values);
  329. }
  330. foreach ($arr as $key => $val) {
  331. // Warning: new API since release 2.3
  332. $this->addOption($val, $key);
  333. }
  334. return true;
  335. } // end func loadArray
  336. // }}}
  337. // {{{ loadDbResult()
  338. /**
  339. * Loads the options from DB_result object
  340. *
  341. * If no column names are specified the first two columns of the result are
  342. * used as the text and value columns respectively
  343. * @param object $result DB_result object
  344. * @param string $textCol (optional) Name of column to display as the OPTION text
  345. * @param string $valueCol (optional) Name of column to use as the OPTION value
  346. * @param mixed $values (optional) Array or comma delimited string of selected values
  347. * @since 1.0
  348. * @access public
  349. * @return PEAR_Error on error or true
  350. * @throws PEAR_Error
  351. */
  352. function loadDbResult(&$result, $textCol=null, $valueCol=null, $values=null)
  353. {
  354. if (!is_object($result) || !is_a($result, 'db_result')) {
  355. return PEAR::raiseError('Argument 1 of HTML_Select::loadDbResult is not a valid DB_result');
  356. }
  357. if (isset($values)) {
  358. $this->setValue($values);
  359. }
  360. $fetchMode = ($textCol && $valueCol) ? DB_FETCHMODE_ASSOC : DB_FETCHMODE_ORDERED;
  361. while (is_array($row = $result->fetchRow($fetchMode)) ) {
  362. if ($fetchMode == DB_FETCHMODE_ASSOC) {
  363. $this->addOption($row[$textCol], $row[$valueCol]);
  364. } else {
  365. $this->addOption($row[0], $row[1]);
  366. }
  367. }
  368. return true;
  369. } // end func loadDbResult
  370. // }}}
  371. // {{{ loadQuery()
  372. /**
  373. * Queries a database and loads the options from the results
  374. *
  375. * @param mixed $conn Either an existing DB connection or a valid dsn
  376. * @param string $sql SQL query string
  377. * @param string $textCol (optional) Name of column to display as the OPTION text
  378. * @param string $valueCol (optional) Name of column to use as the OPTION value
  379. * @param mixed $values (optional) Array or comma delimited string of selected values
  380. * @since 1.1
  381. * @access public
  382. * @return void
  383. * @throws PEAR_Error
  384. */
  385. function loadQuery(&$conn, $sql, $textCol=null, $valueCol=null, $values=null)
  386. {
  387. if (is_string($conn)) {
  388. require_once('DB.php');
  389. $dbConn = &DB::connect($conn, true);
  390. if (DB::isError($dbConn)) {
  391. return $dbConn;
  392. }
  393. } elseif (is_subclass_of($conn, "db_common")) {
  394. $dbConn = &$conn;
  395. } else {
  396. return PEAR::raiseError('Argument 1 of HTML_Select::loadQuery is not a valid type');
  397. }
  398. $result = $dbConn->query($sql);
  399. if (DB::isError($result)) {
  400. return $result;
  401. }
  402. $this->loadDbResult($result, $textCol, $valueCol, $values);
  403. $result->free();
  404. if (is_string($conn)) {
  405. $dbConn->disconnect();
  406. }
  407. return true;
  408. } // end func loadQuery
  409. // }}}
  410. // {{{ load()
  411. /**
  412. * Loads options from different types of data sources
  413. *
  414. * This method is a simulated overloaded method. The arguments, other than the
  415. * first are optional and only mean something depending on the type of the first argument.
  416. * If the first argument is an array then all arguments are passed in order to loadArray.
  417. * If the first argument is a db_result then all arguments are passed in order to loadDbResult.
  418. * If the first argument is a string or a DB connection then all arguments are
  419. * passed in order to loadQuery.
  420. * @param mixed $options Options source currently supports assoc array or DB_result
  421. * @param mixed $param1 (optional) See function detail
  422. * @param mixed $param2 (optional) See function detail
  423. * @param mixed $param3 (optional) See function detail
  424. * @param mixed $param4 (optional) See function detail
  425. * @since 1.1
  426. * @access public
  427. * @return PEAR_Error on error or true
  428. * @throws PEAR_Error
  429. */
  430. function load(&$options, $param1=null, $param2=null, $param3=null, $param4=null)
  431. {
  432. switch (true) {
  433. case is_array($options):
  434. return $this->loadArray($options, $param1);
  435. break;
  436. case (is_a($options, 'db_result')):
  437. return $this->loadDbResult($options, $param1, $param2, $param3);
  438. break;
  439. // Disabled by Chamilo team, 16-MAR-2010.
  440. // TODO: To be verified (why it has been disabled).
  441. //case (is_string($options) && !empty($options) || is_subclass_of($options, "db_common")):
  442. // return $this->loadQuery($options, $param1, $param2, $param3, $param4);
  443. // break;
  444. //
  445. }
  446. } // end func load
  447. // }}}
  448. // {{{ toHtml()
  449. /**
  450. * Returns the SELECT in HTML
  451. *
  452. * @since 1.0
  453. * @access public
  454. * @return string
  455. */
  456. function toHtml()
  457. {
  458. if ($this->_flagFrozen) {
  459. return $this->getFrozenHtml();
  460. } else {
  461. $tabs = $this->_getTabs();
  462. $strHtml = '';
  463. if ($this->getComment() != '') {
  464. $strHtml .= $tabs . '<!-- ' . $this->getComment() . " //-->\n";
  465. }
  466. if (!$this->getMultiple()) {
  467. $attrString = $this->_getAttrString($this->_attributes);
  468. } else {
  469. $myName = $this->getName();
  470. $this->setName($myName . '[]');
  471. $attrString = $this->_getAttrString($this->_attributes);
  472. $this->setName($myName);
  473. }
  474. $strHtml .= $tabs . '<select' . $attrString . ">\n";
  475. if (isset($this->_attributes['data-placeholder'])) {
  476. $strHtml .= "<option></option>";
  477. }
  478. $strValues = is_array($this->_values)? array_map('strval', $this->_values): array();
  479. foreach ($this->_options as $option) {
  480. if (!empty($strValues) && in_array($option['attr']['value'], $strValues, true)) {
  481. $option['attr']['selected'] = 'selected';
  482. }
  483. $strHtml .= $tabs . "\t<option" . $this->_getAttrString($option['attr']) . '>' .
  484. $option['text'] . "</option>\n";
  485. }
  486. foreach ($this->_optgroups as $optgroup) {
  487. $strHtml .= $tabs . "\t<optgroup label=" . $optgroup['label'].">";
  488. foreach($optgroup['options'] as $option) {
  489. $strHtml .= $tabs . "\t<option" . $this->_getAttrString($option['attr']) . '>' .
  490. $option['text'] . "</option>\n";
  491. }
  492. $strHtml .= "</optgroup>\n";
  493. }
  494. return $strHtml . $tabs . '</select>';
  495. }
  496. } //end func toHtml
  497. // }}}
  498. // {{{ getFrozenHtml()
  499. /**
  500. * Returns the value of field without HTML tags
  501. *
  502. * @since 1.0
  503. * @access public
  504. * @return string
  505. */
  506. function getFrozenHtml()
  507. {
  508. $value = array();
  509. if (is_array($this->_values)) {
  510. foreach ($this->_values as $key => $val) {
  511. for ($i = 0, $optCount = count($this->_options); $i < $optCount; $i++) {
  512. if (0 == strcmp($val, $this->_options[$i]['attr']['value'])) {
  513. $value[$key] = $this->_options[$i]['text'];
  514. break;
  515. }
  516. }
  517. }
  518. }
  519. $html = empty($value)? '&nbsp;': join('<br />', $value);
  520. if ($this->_persistantFreeze) {
  521. $name = $this->getPrivateName();
  522. // Only use id attribute if doing single hidden input
  523. if (1 == count($value)) {
  524. $id = $this->getAttribute('id');
  525. $idAttr = isset($id)? array('id' => $id): array();
  526. } else {
  527. $idAttr = array();
  528. }
  529. foreach ($value as $key => $item) {
  530. $html .= '<input' . $this->_getAttrString(array(
  531. 'type' => 'hidden',
  532. 'name' => $name,
  533. 'value' => $this->_values[$key]
  534. ) + $idAttr) . ' />';
  535. }
  536. }
  537. return $html;
  538. } //end func getFrozenHtml
  539. // }}}
  540. // {{{ exportValue()
  541. /**
  542. * We check the options and return only the values that _could_ have been
  543. * selected. We also return a scalar value if select is not "multiple"
  544. */
  545. function exportValue(&$submitValues, $assoc = false)
  546. {
  547. $value = $this->_findValue($submitValues);
  548. if (is_null($value)) {
  549. $value = $this->getValue();
  550. } elseif(!is_array($value)) {
  551. $value = array($value);
  552. }
  553. if (is_array($value) && !empty($this->_options)) {
  554. $cleanValue = null;
  555. foreach ($value as $v) {
  556. for ($i = 0, $optCount = count($this->_options); $i < $optCount; $i++) {
  557. if (0 == strcmp($v, $this->_options[$i]['attr']['value'])) {
  558. $cleanValue[] = $v;
  559. break;
  560. }
  561. }
  562. }
  563. } else {
  564. $cleanValue = $value;
  565. }
  566. if (is_array($cleanValue) && !$this->getMultiple()) {
  567. return $this->_prepareValue($cleanValue[0], $assoc);
  568. } else {
  569. return $this->_prepareValue($cleanValue, $assoc);
  570. }
  571. }
  572. // }}}
  573. // {{{ onQuickFormEvent()
  574. function onQuickFormEvent($event, $arg, &$caller)
  575. {
  576. if ('updateValue' == $event) {
  577. $value = $this->_findValue($caller->_constantValues);
  578. if (null === $value) {
  579. $value = $this->_findValue($caller->_submitValues);
  580. // Fix for bug #4465 & #5269
  581. // XXX: should we push this to element::onQuickFormEvent()?
  582. if (null === $value && (!$caller->isSubmitted() || !$this->getMultiple())) {
  583. $value = $this->_findValue($caller->_defaultValues);
  584. }
  585. }
  586. if (null !== $value) {
  587. $this->setValue($value);
  588. }
  589. return true;
  590. } else {
  591. return parent::onQuickFormEvent($event, $arg, $caller);
  592. }
  593. }
  594. // }}}
  595. } //end class HTML_QuickForm_select
  596. ?>