select.php 23 KB

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