select.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. <?php
  2. /**
  3. * Class to dynamically create an HTML SELECT
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: This source file is subject to version 3.01 of the PHP license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  10. * the PHP License and are unable to obtain it through the web, please
  11. * send a note to license@php.net so we can mail you a copy immediately.
  12. *
  13. * @category HTML
  14. * @package HTML_QuickForm
  15. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  16. * @author Bertrand Mansion <bmansion@mamasam.com>
  17. * @author Alexey Borzov <avb@php.net>
  18. * @copyright 2001-2009 The PHP Group
  19. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  20. * @version CVS: $Id: select.php,v 1.34 2009/04/04 21:34:04 avb Exp $
  21. * @link http://pear.php.net/package/HTML_QuickForm
  22. */
  23. /**
  24. * Class to dynamically create an HTML SELECT
  25. *
  26. * @category HTML
  27. * @package HTML_QuickForm
  28. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  29. * @author Bertrand Mansion <bmansion@mamasam.com>
  30. * @author Alexey Borzov <avb@php.net>
  31. * @version Release: 3.2.11
  32. * @since 1.0
  33. */
  34. class HTML_QuickForm_select extends HTML_QuickForm_element
  35. {
  36. /**
  37. * Contains the select options
  38. *
  39. * @var array
  40. * @since 1.0
  41. * @access private
  42. */
  43. protected $_options = array();
  44. private $_optgroups = array();
  45. /**
  46. * Default values of the SELECT
  47. *
  48. * @var array
  49. * @since 1.0
  50. * @access private
  51. */
  52. protected $_values = [];
  53. /**
  54. * Class constructor
  55. *
  56. * @param string $elementName Select name attribute
  57. * @param mixed $elementLabel Label(s) for the select
  58. * @param mixed $options Data to be used to populate options
  59. * @param mixed $attributes Either a typical HTML attribute string or an associative array
  60. * @since 1.0
  61. * @access public
  62. */
  63. public function __construct(
  64. $elementName,
  65. $elementLabel = '',
  66. $options = null,
  67. $attributes = null
  68. ) {
  69. $addBlank = '';
  70. if (is_array($attributes) || empty($attributes)) {
  71. $oldClass = '';
  72. if (!empty($attributes['class'])) {
  73. $oldClass = $attributes['class'];
  74. }
  75. if (empty($attributes)) {
  76. $attributes = []; // Initialize variable to avoid warning in PHP 7.1
  77. }
  78. $attributes['class'] = $oldClass . 'form-control selectpicker show-tick ';
  79. $attributes['data-style'] = 'custom-select';
  80. $attributes['data-live-search'] = 'true';
  81. if (isset($attributes['disable_js']) && $attributes['disable_js']) {
  82. $attributes['class'] = 'form-control';
  83. $attributes['data-live-search'] = '';
  84. }
  85. if (isset($attributes['extra_class']) && $attributes['extra_class']) {
  86. $attributes['class'] .= ' '.$attributes['extra_class'];
  87. unset($attributes['extra_class']);
  88. }
  89. if (isset($attributes['placeholder'])) {
  90. $addBlank = $attributes['placeholder'];
  91. }
  92. }
  93. $columnsSize = isset($attributes['cols-size']) ? $attributes['cols-size'] : null;
  94. $this->setColumnsSize($columnsSize);
  95. $icon = isset($attributes['icon']) ? $attributes['icon'] : null;
  96. $this->setIcon($icon);
  97. if (!empty($icon)) {
  98. unset($attributes['icon']);
  99. }
  100. parent::__construct($elementName, $elementLabel, $attributes);
  101. $this->_persistantFreeze = true;
  102. $this->_type = 'select';
  103. if ($addBlank !== '') {
  104. if (isset($options)) {
  105. $options = ['' => $addBlank] + $options;
  106. } else {
  107. $options = ['' => $addBlank];
  108. }
  109. }
  110. if (isset($options)) {
  111. $this->load($options);
  112. }
  113. }
  114. /**
  115. * Loads options from different types of data sources
  116. *
  117. * This method is a simulated overloaded method. The arguments, other than the
  118. * first are optional and only mean something depending on the type of the first argument.
  119. * If the first argument is an array then all arguments are passed in order to loadArray.
  120. * If the first argument is a db_result then all arguments are passed in order to loadDbResult.
  121. * If the first argument is a string or a DB connection then all arguments are
  122. * passed in order to loadQuery.
  123. * @param mixed $options Options source currently supports assoc array or DB_result
  124. * @param mixed $param1 (optional) See function detail
  125. * @param mixed $param2 (optional) See function detail
  126. * @param mixed $param3 (optional) See function detail
  127. * @param mixed $param4 (optional) See function detail
  128. * @since 1.1
  129. * @access public
  130. * @return PEAR_Error on error or true
  131. * @throws PEAR_Error
  132. */
  133. protected function load(&$options, $param1=null, $param2=null, $param3=null, $param4=null)
  134. {
  135. switch (true) {
  136. case is_array($options):
  137. return $this->loadArray($options, $param1);
  138. break;
  139. }
  140. }
  141. /**
  142. * Loads the options from an associative array
  143. *
  144. * @param array $arr Associative array of options
  145. * @param mixed $values (optional) Array or comma delimited string of selected values
  146. * @since 1.0
  147. * @access public
  148. * @return PEAR_Error on error or true
  149. * @throws PEAR_Error
  150. */
  151. private function loadArray($arr, $values = null)
  152. {
  153. if (!is_array($arr)) {
  154. return false;
  155. }
  156. if (isset($values)) {
  157. $this->setSelected($values);
  158. }
  159. foreach ($arr as $key => $val) {
  160. // Fix in order to use list of entities.
  161. if (is_object($val)) {
  162. $key = $val->getId();
  163. $val = $val->__toString();
  164. }
  165. // Warning: new API since release 2.3
  166. $this->addOption($val, $key);
  167. }
  168. return true;
  169. }
  170. /**
  171. * Returns the current API version
  172. *
  173. * @since 1.0
  174. * @access public
  175. * @return double
  176. */
  177. function apiVersion()
  178. {
  179. return 2.3;
  180. }
  181. /**
  182. * Sets the default values of the select box
  183. *
  184. * @param mixed $values Array or comma delimited string of selected values
  185. * @since 1.0
  186. * @access public
  187. * @return void
  188. */
  189. function setSelected($values)
  190. {
  191. if (is_string($values) && $this->getMultiple()) {
  192. $values = explode('[ ]?,[ ]?', $values);
  193. }
  194. if (is_array($values)) {
  195. $this->_values = array_values($values);
  196. } else {
  197. $this->_values = array($values);
  198. }
  199. }
  200. /**
  201. * Sets the input field name
  202. *
  203. * @param string $name Input field name attribute
  204. * @since 1.0
  205. * @access public
  206. * @return void
  207. */
  208. function setName($name)
  209. {
  210. $this->updateAttributes(array('name' => $name));
  211. }
  212. /**
  213. * Returns the element name
  214. *
  215. * @since 1.0
  216. * @access public
  217. * @return string
  218. */
  219. function getName()
  220. {
  221. return $this->getAttribute('name');
  222. }
  223. /**
  224. * Returns the element name (possibly with brackets appended)
  225. *
  226. * @since 1.0
  227. * @access public
  228. * @return string
  229. */
  230. function getPrivateName()
  231. {
  232. if ($this->getAttribute('multiple')) {
  233. return $this->getName() . '[]';
  234. } else {
  235. return $this->getName();
  236. }
  237. }
  238. /**
  239. * Sets the value of the form element
  240. *
  241. * @param mixed $values Array or comma delimited string of selected values
  242. * @since 1.0
  243. * @access public
  244. * @return void
  245. */
  246. function setValue($value)
  247. {
  248. $this->setSelected($value);
  249. }
  250. /**
  251. * Returns an array of the selected values
  252. *
  253. * @since 1.0
  254. * @access public
  255. * @return array of selected values
  256. */
  257. function getValue()
  258. {
  259. return $this->_values;
  260. }
  261. /**
  262. * Sets the select field size, only applies to 'multiple' selects
  263. *
  264. * @param int $size Size of select field
  265. * @since 1.0
  266. * @access public
  267. * @return void
  268. */
  269. function setSize($size)
  270. {
  271. $this->updateAttributes(array('size' => $size));
  272. }
  273. /**
  274. * Returns the select field size
  275. *
  276. * @since 1.0
  277. * @access public
  278. * @return int
  279. */
  280. function getSize()
  281. {
  282. return $this->getAttribute('size');
  283. }
  284. /**
  285. * Sets the select mutiple attribute
  286. *
  287. * @param bool $multiple Whether the select supports multi-selections
  288. * @since 1.2
  289. * @access public
  290. * @return void
  291. */
  292. function setMultiple($multiple)
  293. {
  294. if ($multiple) {
  295. $this->updateAttributes(array('multiple' => 'multiple'));
  296. } else {
  297. $this->removeAttribute('multiple');
  298. }
  299. }
  300. /**
  301. * Returns the select mutiple attribute
  302. *
  303. * @since 1.2
  304. * @access public
  305. * @return bool true if multiple select, false otherwise
  306. */
  307. function getMultiple()
  308. {
  309. return (bool)$this->getAttribute('multiple');
  310. }
  311. /**
  312. * Adds a new OPTION to the SELECT
  313. *
  314. * @param string $text Display text for the OPTION
  315. * @param string $value Value for the OPTION
  316. * @param mixed $attributes Either a typical HTML attribute string
  317. * or an associative array
  318. * @since 1.0
  319. * @access public
  320. * @return void
  321. */
  322. function addOption($text, $value, $attributes = null, $return_array = false)
  323. {
  324. if (null === $attributes) {
  325. $attributes = array('value' => (string)$value);
  326. } else {
  327. $attributes = $this->_parseAttributes($attributes);
  328. if (isset($attributes['selected'])) {
  329. // the 'selected' attribute will be set in toHtml()
  330. $this->_removeAttr('selected', $attributes);
  331. if (is_null($this->_values)) {
  332. $this->_values = array($value);
  333. } elseif (!in_array($value, $this->_values)) {
  334. $this->_values[] = $value;
  335. }
  336. }
  337. $this->_updateAttrArray($attributes, array('value' => (string)$value));
  338. }
  339. if ($return_array) {
  340. return array('text' => $text, 'attr' => $attributes);
  341. } else {
  342. $this->_options[] = array('text' => $text, 'attr' => $attributes);
  343. }
  344. }
  345. /**
  346. * Adds a new OPTION to the SELECT
  347. *
  348. * @param string $text Display text for the OPTION
  349. * @param string $value Value for the OPTION
  350. * @param mixed $attributes Either a typical HTML attribute string
  351. * or an associative array
  352. * @since 1.0
  353. * @access public
  354. * @return void
  355. */
  356. function addOptGroup($options, $label)
  357. {
  358. foreach ($options as $option) {
  359. $this->addOption($option['text'], $option['value'], $option, true);
  360. }
  361. $this->_optgroups[] = array('label' => $label, 'options' => $options);
  362. }
  363. /**
  364. * Returns the SELECT in HTML
  365. *
  366. * @since 1.0
  367. * @access public
  368. * @return string
  369. */
  370. public function toHtml()
  371. {
  372. if ($this->_flagFrozen) {
  373. return $this->getFrozenHtml();
  374. } else {
  375. $tabs = $this->_getTabs();
  376. $strHtml = '';
  377. if ($this->getComment() != '') {
  378. $strHtml .= $tabs . '<!-- ' . $this->getComment() . " //-->\n";
  379. }
  380. if (!$this->getMultiple()) {
  381. $attrString = $this->_getAttrString($this->_attributes);
  382. } else {
  383. $myName = $this->getName();
  384. $this->setName($myName . '[]');
  385. $attrString = $this->_getAttrString($this->_attributes);
  386. $this->setName($myName);
  387. }
  388. $strHtml .= $tabs . '<select ' . $attrString . ">\n";
  389. $strValues = is_array($this->_values)? array_map('strval', $this->_values): array();
  390. foreach ($this->_options as $option) {
  391. if (!empty($strValues) && in_array($option['attr']['value'], $strValues, true)) {
  392. $option['attr']['selected'] = 'selected';
  393. }
  394. $strHtml .= $tabs . "<option" . $this->_getAttrString($option['attr']) . '>' .
  395. $option['text'] . "</option>";
  396. }
  397. foreach ($this->_optgroups as $optgroup) {
  398. $strHtml .= $tabs . '<optgroup label="' . $optgroup['label'] . '">';
  399. foreach ($optgroup['options'] as $option) {
  400. $text = $option['text'];
  401. unset($option['text']);
  402. if (!empty($strValues) && in_array($option['value'], $strValues)) {
  403. $option['selected'] = 'selected';
  404. }
  405. $strHtml .= $tabs . " <option" . $this->_getAttrString($option) . '>' .$text . "</option>";
  406. }
  407. $strHtml .= "</optgroup>";
  408. }
  409. return $strHtml . $tabs . '</select>';
  410. }
  411. }
  412. /**
  413. * Returns the value of field without HTML tags
  414. *
  415. * @since 1.0
  416. * @access public
  417. * @return string
  418. */
  419. function getFrozenHtml()
  420. {
  421. $value = array();
  422. if (is_array($this->_values)) {
  423. foreach ($this->_values as $key => $val) {
  424. for ($i = 0, $optCount = count($this->_options); $i < $optCount; $i++) {
  425. if (0 == strcmp($val, $this->_options[$i]['attr']['value'])) {
  426. $value[$key] = $this->_options[$i]['text'];
  427. break;
  428. }
  429. }
  430. }
  431. }
  432. $html = empty($value)? '&nbsp;': join('<br />', $value);
  433. if ($this->_persistantFreeze) {
  434. $name = $this->getPrivateName();
  435. // Only use id attribute if doing single hidden input
  436. if (1 == count($value)) {
  437. $id = $this->getAttribute('id');
  438. $idAttr = isset($id)? array('id' => $id): array();
  439. } else {
  440. $idAttr = array();
  441. }
  442. foreach ($value as $key => $item) {
  443. $html .= '<input' . $this->_getAttrString(array(
  444. 'type' => 'hidden',
  445. 'name' => $name,
  446. 'value' => $this->_values[$key]
  447. ) + $idAttr) . ' />';
  448. }
  449. }
  450. return $html;
  451. }
  452. /**
  453. * We check the options and return only the values that _could_ have been
  454. * selected. We also return a scalar value if select is not "multiple"
  455. */
  456. function exportValue(&$submitValues, $assoc = false)
  457. {
  458. $value = $this->_findValue($submitValues);
  459. if (is_null($value)) {
  460. $value = $this->getValue();
  461. } elseif(!is_array($value)) {
  462. $value = array($value);
  463. }
  464. if (is_array($value) && !empty($this->_options)) {
  465. $cleanValue = null;
  466. foreach ($value as $v) {
  467. for ($i = 0, $optCount = count($this->_options); $i < $optCount; $i++) {
  468. if (0 == strcmp($v, $this->_options[$i]['attr']['value'])) {
  469. $cleanValue[] = $v;
  470. break;
  471. }
  472. }
  473. }
  474. } else {
  475. $cleanValue = $value;
  476. }
  477. if (is_array($cleanValue) && !$this->getMultiple()) {
  478. return $this->_prepareValue($cleanValue[0], $assoc);
  479. } else {
  480. return $this->_prepareValue($cleanValue, $assoc);
  481. }
  482. }
  483. function onQuickFormEvent($event, $arg, &$caller)
  484. {
  485. if ('updateValue' == $event) {
  486. $value = $this->_findValue($caller->_constantValues);
  487. if (null === $value) {
  488. $value = $this->_findValue($caller->_submitValues);
  489. // Fix for bug #4465 & #5269
  490. // XXX: should we push this to element::onQuickFormEvent()?
  491. if (null === $value && (!$caller->isSubmitted() || !$this->getMultiple())) {
  492. $value = $this->_findValue($caller->_defaultValues);
  493. }
  494. }
  495. if (null !== $value) {
  496. $this->setValue($value);
  497. }
  498. return true;
  499. } else {
  500. return parent::onQuickFormEvent($event, $arg, $caller);
  501. }
  502. }
  503. /**
  504. * @param FormValidator $form
  505. */
  506. public function updateSelectWithSelectedOption(FormValidator $form)
  507. {
  508. $id = $this->getAttribute('id');
  509. $form->addHtml('<script>
  510. $(function(){
  511. var optionClass = $("#'.$id.'").find("option:checked").attr("class");
  512. $("#'.$id.'").attr("class", "form-control " + optionClass);
  513. $("#'.$id.'").on("change", function() {
  514. var optionClass = ($(this).find("option:checked").attr("class"));
  515. $(this).attr("class", "form-control " + optionClass);
  516. });
  517. });
  518. </script>');
  519. }
  520. /**
  521. * Show an icon at the left side of an input
  522. * @return string
  523. */
  524. public function getIconToHtml()
  525. {
  526. $icon = $this->getIcon();
  527. $isButton = $this->getButton();
  528. if (empty($icon)) {
  529. return '';
  530. }
  531. $element = '<span class="input-group-text"><em class="fa fa-' . $icon . '"></em></span>';
  532. if ($isButton) {
  533. $element = '<button class="btn btn-outline-secondary" type="reset">
  534. <em class="fa fa-' . $icon . '"></em>
  535. </button>';
  536. }
  537. return '<div class="input-group-append">
  538. ' . $element . '
  539. </div>';
  540. }
  541. /**
  542. * @param string $layout
  543. *
  544. * @return string
  545. */
  546. public function getTemplate($layout)
  547. {
  548. $size = $this->calculateSize();
  549. switch ($layout) {
  550. case FormValidator::LAYOUT_INLINE:
  551. return '
  552. <label class="sr-only" {label-for} >
  553. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  554. {label}
  555. </label>
  556. {element}
  557. ';
  558. break;
  559. case FormValidator::LAYOUT_HORIZONTAL:
  560. return '
  561. <div class="form-group row {error_class}">
  562. <label {label-for} class="col-sm-'.$size[0].' col-form-label {extra_label_class}" >
  563. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  564. {label}
  565. </label>
  566. <div class="col-sm-'.$size[1].'">
  567. {icon}
  568. {element}
  569. <!-- BEGIN label_2 -->
  570. <p class="help-block">{label_2}</p>
  571. <!-- END label_2 -->
  572. <!-- BEGIN error -->
  573. <span class="help-inline help-block">{error}</span>
  574. <!-- END error -->
  575. </div>
  576. <div class="col-sm-'.$size[2].'">
  577. <!-- BEGIN label_3 -->
  578. {label_3}
  579. <!-- END label_3 -->
  580. </div>
  581. </div>';
  582. break;
  583. case FormValidator::LAYOUT_BOX_NO_LABEL:
  584. return '
  585. <div class="input-group">
  586. {icon}
  587. {element}
  588. </div>';
  589. case FormValidator::LAYOUT_BOX_SEARCH:
  590. return '
  591. <div class="form-group row">
  592. <label class="col-sm-2 col-form-label" {label-for}>{label}</label>
  593. <div class="col-sm-8">
  594. <div class="input-group">
  595. {element}
  596. {icon}
  597. </div>
  598. </div>
  599. <div class="col-sm-2"></div>
  600. </div>';
  601. break;
  602. }
  603. }
  604. }