select.php 23 KB

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