select.php 19 KB

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