select.php 23 KB

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