select.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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 . ' selectpicker show-tick form-control';
  79. $attributes['data-live-search'] = 'true';
  80. if (isset($attributes['disable_js']) && $attributes['disable_js']) {
  81. $attributes['class'] = 'form-control';
  82. $attributes['data-live-search'] = '';
  83. }
  84. if (isset($attributes['placeholder'])) {
  85. $addBlank = $attributes['placeholder'];
  86. }
  87. }
  88. $columnsSize = isset($attributes['cols-size']) ? $attributes['cols-size'] : null;
  89. $this->setColumnsSize($columnsSize);
  90. parent::__construct($elementName, $elementLabel, $attributes);
  91. $this->_persistantFreeze = true;
  92. $this->_type = 'select';
  93. if ($addBlank !== '') {
  94. if (isset($options)) {
  95. $options = ['' => $addBlank] + $options;
  96. } else {
  97. $options = ['' => $addBlank];
  98. }
  99. }
  100. if (isset($options)) {
  101. $this->load($options);
  102. }
  103. }
  104. /**
  105. * Loads options from different types of data sources
  106. *
  107. * This method is a simulated overloaded method. The arguments, other than the
  108. * first are optional and only mean something depending on the type of the first argument.
  109. * If the first argument is an array then all arguments are passed in order to loadArray.
  110. * If the first argument is a db_result then all arguments are passed in order to loadDbResult.
  111. * If the first argument is a string or a DB connection then all arguments are
  112. * passed in order to loadQuery.
  113. * @param mixed $options Options source currently supports assoc array or DB_result
  114. * @param mixed $param1 (optional) See function detail
  115. * @param mixed $param2 (optional) See function detail
  116. * @param mixed $param3 (optional) See function detail
  117. * @param mixed $param4 (optional) See function detail
  118. * @since 1.1
  119. * @access public
  120. * @return PEAR_Error on error or true
  121. * @throws PEAR_Error
  122. */
  123. protected function load(&$options, $param1=null, $param2=null, $param3=null, $param4=null)
  124. {
  125. switch (true) {
  126. case is_array($options):
  127. return $this->loadArray($options, $param1);
  128. break;
  129. }
  130. }
  131. /**
  132. * Loads the options from an associative array
  133. *
  134. * @param array $arr Associative array of options
  135. * @param mixed $values (optional) Array or comma delimited string of selected values
  136. * @since 1.0
  137. * @access public
  138. * @return PEAR_Error on error or true
  139. * @throws PEAR_Error
  140. */
  141. private function loadArray($arr, $values = null)
  142. {
  143. if (!is_array($arr)) {
  144. return false;
  145. }
  146. if (isset($values)) {
  147. $this->setSelected($values);
  148. }
  149. foreach ($arr as $key => $val) {
  150. // Fix in order to use list of entities.
  151. if (is_object($val)) {
  152. $key = $val->getId();
  153. $val = $val->__toString();
  154. }
  155. // Warning: new API since release 2.3
  156. $this->addOption($val, $key);
  157. }
  158. return true;
  159. }
  160. /**
  161. * Returns the current API version
  162. *
  163. * @since 1.0
  164. * @access public
  165. * @return double
  166. */
  167. function apiVersion()
  168. {
  169. return 2.3;
  170. }
  171. /**
  172. * Sets the default values of the select box
  173. *
  174. * @param mixed $values Array or comma delimited string of selected values
  175. * @since 1.0
  176. * @access public
  177. * @return void
  178. */
  179. function setSelected($values)
  180. {
  181. if (is_string($values) && $this->getMultiple()) {
  182. $values = explode('[ ]?,[ ]?', $values);
  183. }
  184. if (is_array($values)) {
  185. $this->_values = array_values($values);
  186. } else {
  187. $this->_values = array($values);
  188. }
  189. }
  190. /**
  191. * Sets the input field name
  192. *
  193. * @param string $name Input field name attribute
  194. * @since 1.0
  195. * @access public
  196. * @return void
  197. */
  198. function setName($name)
  199. {
  200. $this->updateAttributes(array('name' => $name));
  201. }
  202. /**
  203. * Returns the element name
  204. *
  205. * @since 1.0
  206. * @access public
  207. * @return string
  208. */
  209. function getName()
  210. {
  211. return $this->getAttribute('name');
  212. }
  213. /**
  214. * Returns the element name (possibly with brackets appended)
  215. *
  216. * @since 1.0
  217. * @access public
  218. * @return string
  219. */
  220. function getPrivateName()
  221. {
  222. if ($this->getAttribute('multiple')) {
  223. return $this->getName() . '[]';
  224. } else {
  225. return $this->getName();
  226. }
  227. }
  228. /**
  229. * Sets the value of the form element
  230. *
  231. * @param mixed $values Array or comma delimited string of selected values
  232. * @since 1.0
  233. * @access public
  234. * @return void
  235. */
  236. function setValue($value)
  237. {
  238. $this->setSelected($value);
  239. }
  240. /**
  241. * Returns an array of the selected values
  242. *
  243. * @since 1.0
  244. * @access public
  245. * @return array of selected values
  246. */
  247. function getValue()
  248. {
  249. return $this->_values;
  250. }
  251. /**
  252. * Sets the select field size, only applies to 'multiple' selects
  253. *
  254. * @param int $size Size of select field
  255. * @since 1.0
  256. * @access public
  257. * @return void
  258. */
  259. function setSize($size)
  260. {
  261. $this->updateAttributes(array('size' => $size));
  262. }
  263. /**
  264. * Returns the select field size
  265. *
  266. * @since 1.0
  267. * @access public
  268. * @return int
  269. */
  270. function getSize()
  271. {
  272. return $this->getAttribute('size');
  273. }
  274. /**
  275. * Sets the select mutiple attribute
  276. *
  277. * @param bool $multiple Whether the select supports multi-selections
  278. * @since 1.2
  279. * @access public
  280. * @return void
  281. */
  282. function setMultiple($multiple)
  283. {
  284. if ($multiple) {
  285. $this->updateAttributes(array('multiple' => 'multiple'));
  286. } else {
  287. $this->removeAttribute('multiple');
  288. }
  289. }
  290. /**
  291. * Returns the select mutiple attribute
  292. *
  293. * @since 1.2
  294. * @access public
  295. * @return bool true if multiple select, false otherwise
  296. */
  297. function getMultiple()
  298. {
  299. return (bool)$this->getAttribute('multiple');
  300. }
  301. /**
  302. * Adds a new OPTION to the SELECT
  303. *
  304. * @param string $text Display text for the OPTION
  305. * @param string $value Value for the OPTION
  306. * @param mixed $attributes Either a typical HTML attribute string
  307. * or an associative array
  308. * @since 1.0
  309. * @access public
  310. * @return void
  311. */
  312. function addOption($text, $value, $attributes = null, $return_array = false)
  313. {
  314. if (null === $attributes) {
  315. $attributes = array('value' => (string)$value);
  316. } else {
  317. $attributes = $this->_parseAttributes($attributes);
  318. if (isset($attributes['selected'])) {
  319. // the 'selected' attribute will be set in toHtml()
  320. $this->_removeAttr('selected', $attributes);
  321. if (is_null($this->_values)) {
  322. $this->_values = array($value);
  323. } elseif (!in_array($value, $this->_values)) {
  324. $this->_values[] = $value;
  325. }
  326. }
  327. $this->_updateAttrArray($attributes, array('value' => (string)$value));
  328. }
  329. if ($return_array) {
  330. return array('text' => $text, 'attr' => $attributes);
  331. } else {
  332. $this->_options[] = array('text' => $text, 'attr' => $attributes);
  333. }
  334. }
  335. /**
  336. * Adds a new OPTION to the SELECT
  337. *
  338. * @param string $text Display text for the OPTION
  339. * @param string $value Value for the OPTION
  340. * @param mixed $attributes Either a typical HTML attribute string
  341. * or an associative array
  342. * @since 1.0
  343. * @access public
  344. * @return void
  345. */
  346. function addOptGroup($options, $label)
  347. {
  348. foreach ($options as $option) {
  349. $this->addOption($option['text'], $option['value'], $option, true);
  350. }
  351. $this->_optgroups[] = array('label' => $label, 'options' => $options);
  352. }
  353. /**
  354. * Returns the SELECT in HTML
  355. *
  356. * @since 1.0
  357. * @access public
  358. * @return string
  359. */
  360. public function toHtml()
  361. {
  362. if ($this->_flagFrozen) {
  363. return $this->getFrozenHtml();
  364. } else {
  365. $tabs = $this->_getTabs();
  366. $strHtml = '';
  367. if ($this->getComment() != '') {
  368. $strHtml .= $tabs . '<!-- ' . $this->getComment() . " //-->\n";
  369. }
  370. if (!$this->getMultiple()) {
  371. $attrString = $this->_getAttrString($this->_attributes);
  372. } else {
  373. $myName = $this->getName();
  374. $this->setName($myName . '[]');
  375. $attrString = $this->_getAttrString($this->_attributes);
  376. $this->setName($myName);
  377. }
  378. $strHtml .= $tabs . '<select ' . $attrString . ">\n";
  379. $strValues = is_array($this->_values)? array_map('strval', $this->_values): array();
  380. foreach ($this->_options as $option) {
  381. if (!empty($strValues) && in_array($option['attr']['value'], $strValues, true)) {
  382. $option['attr']['selected'] = 'selected';
  383. }
  384. $strHtml .= $tabs . "<option" . $this->_getAttrString($option['attr']) . '>' .
  385. $option['text'] . "</option>";
  386. }
  387. foreach ($this->_optgroups as $optgroup) {
  388. $strHtml .= $tabs . '<optgroup label="' . $optgroup['label'] . '">';
  389. foreach ($optgroup['options'] as $option) {
  390. $text = $option['text'];
  391. unset($option['text']);
  392. if (!empty($strValues) && in_array($option['value'], $strValues)) {
  393. $option['selected'] = 'selected';
  394. }
  395. $strHtml .= $tabs . " <option" . $this->_getAttrString($option) . '>' .$text . "</option>";
  396. }
  397. $strHtml .= "</optgroup>";
  398. }
  399. return $strHtml . $tabs . '</select>';
  400. }
  401. }
  402. /**
  403. * Returns the value of field without HTML tags
  404. *
  405. * @since 1.0
  406. * @access public
  407. * @return string
  408. */
  409. function getFrozenHtml()
  410. {
  411. $value = array();
  412. if (is_array($this->_values)) {
  413. foreach ($this->_values as $key => $val) {
  414. for ($i = 0, $optCount = count($this->_options); $i < $optCount; $i++) {
  415. if (0 == strcmp($val, $this->_options[$i]['attr']['value'])) {
  416. $value[$key] = $this->_options[$i]['text'];
  417. break;
  418. }
  419. }
  420. }
  421. }
  422. $html = empty($value)? '&nbsp;': join('<br />', $value);
  423. if ($this->_persistantFreeze) {
  424. $name = $this->getPrivateName();
  425. // Only use id attribute if doing single hidden input
  426. if (1 == count($value)) {
  427. $id = $this->getAttribute('id');
  428. $idAttr = isset($id)? array('id' => $id): array();
  429. } else {
  430. $idAttr = array();
  431. }
  432. foreach ($value as $key => $item) {
  433. $html .= '<input' . $this->_getAttrString(array(
  434. 'type' => 'hidden',
  435. 'name' => $name,
  436. 'value' => $this->_values[$key]
  437. ) + $idAttr) . ' />';
  438. }
  439. }
  440. return $html;
  441. }
  442. /**
  443. * We check the options and return only the values that _could_ have been
  444. * selected. We also return a scalar value if select is not "multiple"
  445. */
  446. function exportValue(&$submitValues, $assoc = false)
  447. {
  448. $value = $this->_findValue($submitValues);
  449. if (is_null($value)) {
  450. $value = $this->getValue();
  451. } elseif(!is_array($value)) {
  452. $value = array($value);
  453. }
  454. if (is_array($value) && !empty($this->_options)) {
  455. $cleanValue = null;
  456. foreach ($value as $v) {
  457. for ($i = 0, $optCount = count($this->_options); $i < $optCount; $i++) {
  458. if (0 == strcmp($v, $this->_options[$i]['attr']['value'])) {
  459. $cleanValue[] = $v;
  460. break;
  461. }
  462. }
  463. }
  464. } else {
  465. $cleanValue = $value;
  466. }
  467. if (is_array($cleanValue) && !$this->getMultiple()) {
  468. return $this->_prepareValue($cleanValue[0], $assoc);
  469. } else {
  470. return $this->_prepareValue($cleanValue, $assoc);
  471. }
  472. }
  473. function onQuickFormEvent($event, $arg, &$caller)
  474. {
  475. if ('updateValue' == $event) {
  476. $value = $this->_findValue($caller->_constantValues);
  477. if (null === $value) {
  478. $value = $this->_findValue($caller->_submitValues);
  479. // Fix for bug #4465 & #5269
  480. // XXX: should we push this to element::onQuickFormEvent()?
  481. if (null === $value && (!$caller->isSubmitted() || !$this->getMultiple())) {
  482. $value = $this->_findValue($caller->_defaultValues);
  483. }
  484. }
  485. if (null !== $value) {
  486. $this->setValue($value);
  487. }
  488. return true;
  489. } else {
  490. return parent::onQuickFormEvent($event, $arg, $caller);
  491. }
  492. }
  493. /**
  494. * @param FormValidator $form
  495. */
  496. public function updateSelectWithSelectedOption(FormValidator $form)
  497. {
  498. $id = $this->getAttribute('id');
  499. $form->addHtml('<script>
  500. $(function(){
  501. var optionClass = $("#'.$id.'").find("option:checked").attr("class");
  502. $("#'.$id.'").attr("class", "form-control " + optionClass);
  503. $("#'.$id.'").on("change", function() {
  504. var optionClass = ($(this).find("option:checked").attr("class"));
  505. $(this).attr("class", "form-control " + optionClass);
  506. });
  507. });
  508. </script>');
  509. }
  510. /**
  511. * @param string $layout
  512. *
  513. * @return string
  514. */
  515. public function getTemplate($layout)
  516. {
  517. $size = $this->calculateSize();
  518. switch ($layout) {
  519. case FormValidator::LAYOUT_INLINE:
  520. return '
  521. <div class="form-group {error_class}">
  522. <label {label-for} >
  523. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  524. {label}
  525. </label>
  526. {element}
  527. </div>';
  528. break;
  529. case FormValidator::LAYOUT_HORIZONTAL:
  530. return '
  531. <div class="form-group {error_class}">
  532. <label {label-for} class="col-sm-'.$size[0].' control-label {extra_label_class}" >
  533. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  534. {label}
  535. </label>
  536. <div class="col-sm-'.$size[1].'">
  537. {icon}
  538. {element}
  539. <!-- BEGIN label_2 -->
  540. <p class="help-block">{label_2}</p>
  541. <!-- END label_2 -->
  542. <!-- BEGIN error -->
  543. <span class="help-inline help-block">{error}</span>
  544. <!-- END error -->
  545. </div>
  546. <div class="col-sm-'.$size[2].'">
  547. <!-- BEGIN label_3 -->
  548. {label_3}
  549. <!-- END label_3 -->
  550. </div>
  551. </div>';
  552. break;
  553. case FormValidator::LAYOUT_BOX_NO_LABEL:
  554. return '
  555. <div class="input-group">
  556. {icon}
  557. {element}
  558. </div>';
  559. break;
  560. }
  561. }
  562. }