select.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 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. } //end func apiVersion
  171. // }}}
  172. // {{{ setSelected()
  173. /**
  174. * Sets the default values of the select box
  175. *
  176. * @param mixed $values Array or comma delimited string of selected values
  177. * @since 1.0
  178. * @access public
  179. * @return void
  180. */
  181. function setSelected($values)
  182. {
  183. if (is_string($values) && $this->getMultiple()) {
  184. $values = explode("[ ]?,[ ]?", $values);
  185. }
  186. if (is_array($values)) {
  187. $this->_values = array_values($values);
  188. } else {
  189. $this->_values = array($values);
  190. }
  191. } //end func setSelected
  192. // }}}
  193. // {{{ getSelected()
  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 getSelected()
  202. {
  203. return $this->_values;
  204. } // end func getSelected
  205. // }}}
  206. // {{{ setName()
  207. /**
  208. * Sets the input field name
  209. *
  210. * @param string $name Input field name attribute
  211. * @since 1.0
  212. * @access public
  213. * @return void
  214. */
  215. function setName($name)
  216. {
  217. $this->updateAttributes(array('name' => $name));
  218. } //end func setName
  219. // }}}
  220. // {{{ getName()
  221. /**
  222. * Returns the element name
  223. *
  224. * @since 1.0
  225. * @access public
  226. * @return string
  227. */
  228. function getName()
  229. {
  230. return $this->getAttribute('name');
  231. } //end func getName
  232. // }}}
  233. // {{{ getPrivateName()
  234. /**
  235. * Returns the element name (possibly with brackets appended)
  236. *
  237. * @since 1.0
  238. * @access public
  239. * @return string
  240. */
  241. function getPrivateName()
  242. {
  243. if ($this->getAttribute('multiple')) {
  244. return $this->getName() . '[]';
  245. } else {
  246. return $this->getName();
  247. }
  248. } //end func getPrivateName
  249. // }}}
  250. // {{{ setValue()
  251. /**
  252. * Sets the value of the form element
  253. *
  254. * @param mixed $values Array or comma delimited string of selected values
  255. * @since 1.0
  256. * @access public
  257. * @return void
  258. */
  259. function setValue($value)
  260. {
  261. $this->setSelected($value);
  262. } // end func setValue
  263. // }}}
  264. // {{{ getValue()
  265. /**
  266. * Returns an array of the selected values
  267. *
  268. * @since 1.0
  269. * @access public
  270. * @return array of selected values
  271. */
  272. function getValue()
  273. {
  274. return $this->_values;
  275. } // end func getValue
  276. // }}}
  277. // {{{ setSize()
  278. /**
  279. * Sets the select field size, only applies to 'multiple' selects
  280. *
  281. * @param int $size Size of select field
  282. * @since 1.0
  283. * @access public
  284. * @return void
  285. */
  286. function setSize($size)
  287. {
  288. $this->updateAttributes(array('size' => $size));
  289. } //end func setSize
  290. // }}}
  291. // {{{ getSize()
  292. /**
  293. * Returns the select field size
  294. *
  295. * @since 1.0
  296. * @access public
  297. * @return int
  298. */
  299. function getSize()
  300. {
  301. return $this->getAttribute('size');
  302. } //end func getSize
  303. // }}}
  304. // {{{ setMultiple()
  305. /**
  306. * Sets the select mutiple attribute
  307. *
  308. * @param bool $multiple Whether the select supports multi-selections
  309. * @since 1.2
  310. * @access public
  311. * @return void
  312. */
  313. function setMultiple($multiple)
  314. {
  315. if ($multiple) {
  316. $this->updateAttributes(array('multiple' => 'multiple'));
  317. } else {
  318. $this->removeAttribute('multiple');
  319. }
  320. } //end func setMultiple
  321. // }}}
  322. // {{{ getMultiple()
  323. /**
  324. * Returns the select mutiple attribute
  325. *
  326. * @since 1.2
  327. * @access public
  328. * @return bool true if multiple select, false otherwise
  329. */
  330. function getMultiple()
  331. {
  332. return (bool)$this->getAttribute('multiple');
  333. } //end func getMultiple
  334. // }}}
  335. // {{{ addOption()
  336. /**
  337. * Adds a new OPTION to the SELECT
  338. *
  339. * @param string $text Display text for the OPTION
  340. * @param string $value Value for the OPTION
  341. * @param mixed $attributes Either a typical HTML attribute string
  342. * or an associative array
  343. * @since 1.0
  344. * @access public
  345. * @return void
  346. */
  347. function addOption($text, $value, $attributes = null, $return_array = false)
  348. {
  349. if (null === $attributes) {
  350. $attributes = array('value' => (string)$value);
  351. } else {
  352. $attributes = $this->_parseAttributes($attributes);
  353. if (isset($attributes['selected'])) {
  354. // the 'selected' attribute will be set in toHtml()
  355. $this->_removeAttr('selected', $attributes);
  356. if (is_null($this->_values)) {
  357. $this->_values = array($value);
  358. } elseif (!in_array($value, $this->_values)) {
  359. $this->_values[] = $value;
  360. }
  361. }
  362. $this->_updateAttrArray($attributes, array('value' => (string)$value));
  363. }
  364. if ($return_array) {
  365. return array('text' => $text, 'attr' => $attributes);
  366. } else {
  367. $this->_options[] = array('text' => $text, 'attr' => $attributes);
  368. }
  369. } // end func addOption
  370. /**
  371. * Adds a new OPTION to the SELECT
  372. *
  373. * @param string $text Display text for the OPTION
  374. * @param string $value Value for the OPTION
  375. * @param mixed $attributes Either a typical HTML attribute string
  376. * or an associative array
  377. * @since 1.0
  378. * @access public
  379. * @return void
  380. */
  381. function addOptGroup($options, $label)
  382. {
  383. foreach ($options as $option) {
  384. $this->addOption($option['text'], $option['value'], $option, true);
  385. }
  386. $this->_optgroups[] = array('label' => $label, 'options' => $options);
  387. }
  388. /**
  389. * Returns the SELECT in HTML
  390. *
  391. * @since 1.0
  392. * @access public
  393. * @return string
  394. */
  395. public function toHtml()
  396. {
  397. if ($this->_flagFrozen) {
  398. return $this->getFrozenHtml();
  399. } else {
  400. $tabs = $this->_getTabs();
  401. $strHtml = '';
  402. if ($this->getComment() != '') {
  403. $strHtml .= $tabs . '<!-- ' . $this->getComment() . " //-->\n";
  404. }
  405. if (!$this->getMultiple()) {
  406. $attrString = $this->_getAttrString($this->_attributes);
  407. } else {
  408. $myName = $this->getName();
  409. $this->setName($myName . '[]');
  410. $attrString = $this->_getAttrString($this->_attributes);
  411. $this->setName($myName);
  412. }
  413. $strHtml .= $tabs . '<select ' . $attrString . ">\n";
  414. $strValues = is_array($this->_values)? array_map('strval', $this->_values): array();
  415. foreach ($this->_options as $option) {
  416. if (!empty($strValues) && in_array($option['attr']['value'], $strValues, true)) {
  417. $option['attr']['selected'] = 'selected';
  418. }
  419. $strHtml .= $tabs . "<option" . $this->_getAttrString($option['attr']) . '>' .
  420. $option['text'] . "</option>";
  421. }
  422. foreach ($this->_optgroups as $optgroup) {
  423. $strHtml .= $tabs . '<optgroup label="' . $optgroup['label'] . '">';
  424. foreach ($optgroup['options'] as $option) {
  425. $text = $option['text'];
  426. unset($option['text']);
  427. if (!empty($strValues) && in_array($option['value'], $strValues)) {
  428. $option['selected'] = 'selected';
  429. }
  430. $strHtml .= $tabs . " <option" . $this->_getAttrString($option) . '>' .$text . "</option>";
  431. }
  432. $strHtml .= "</optgroup>";
  433. }
  434. return $strHtml . $tabs . '</select>';
  435. }
  436. }
  437. /**
  438. * Returns the value of field without HTML tags
  439. *
  440. * @since 1.0
  441. * @access public
  442. * @return string
  443. */
  444. function getFrozenHtml()
  445. {
  446. $value = array();
  447. if (is_array($this->_values)) {
  448. foreach ($this->_values as $key => $val) {
  449. for ($i = 0, $optCount = count($this->_options); $i < $optCount; $i++) {
  450. if (0 == strcmp($val, $this->_options[$i]['attr']['value'])) {
  451. $value[$key] = $this->_options[$i]['text'];
  452. break;
  453. }
  454. }
  455. }
  456. }
  457. $html = empty($value)? '&nbsp;': join('<br />', $value);
  458. if ($this->_persistantFreeze) {
  459. $name = $this->getPrivateName();
  460. // Only use id attribute if doing single hidden input
  461. if (1 == count($value)) {
  462. $id = $this->getAttribute('id');
  463. $idAttr = isset($id)? array('id' => $id): array();
  464. } else {
  465. $idAttr = array();
  466. }
  467. foreach ($value as $key => $item) {
  468. $html .= '<input' . $this->_getAttrString(array(
  469. 'type' => 'hidden',
  470. 'name' => $name,
  471. 'value' => $this->_values[$key]
  472. ) + $idAttr) . ' />';
  473. }
  474. }
  475. return $html;
  476. } //end func getFrozenHtml
  477. // }}}
  478. // {{{ exportValue()
  479. /**
  480. * We check the options and return only the values that _could_ have been
  481. * selected. We also return a scalar value if select is not "multiple"
  482. */
  483. function exportValue(&$submitValues, $assoc = false)
  484. {
  485. $value = $this->_findValue($submitValues);
  486. if (is_null($value)) {
  487. $value = $this->getValue();
  488. } elseif(!is_array($value)) {
  489. $value = array($value);
  490. }
  491. if (is_array($value) && !empty($this->_options)) {
  492. $cleanValue = null;
  493. foreach ($value as $v) {
  494. for ($i = 0, $optCount = count($this->_options); $i < $optCount; $i++) {
  495. if (0 == strcmp($v, $this->_options[$i]['attr']['value'])) {
  496. $cleanValue[] = $v;
  497. break;
  498. }
  499. }
  500. }
  501. } else {
  502. $cleanValue = $value;
  503. }
  504. if (is_array($cleanValue) && !$this->getMultiple()) {
  505. return $this->_prepareValue($cleanValue[0], $assoc);
  506. } else {
  507. return $this->_prepareValue($cleanValue, $assoc);
  508. }
  509. }
  510. function onQuickFormEvent($event, $arg, &$caller)
  511. {
  512. if ('updateValue' == $event) {
  513. $value = $this->_findValue($caller->_constantValues);
  514. if (null === $value) {
  515. $value = $this->_findValue($caller->_submitValues);
  516. // Fix for bug #4465 & #5269
  517. // XXX: should we push this to element::onQuickFormEvent()?
  518. if (null === $value && (!$caller->isSubmitted() || !$this->getMultiple())) {
  519. $value = $this->_findValue($caller->_defaultValues);
  520. }
  521. }
  522. if (null !== $value) {
  523. $this->setValue($value);
  524. }
  525. return true;
  526. } else {
  527. return parent::onQuickFormEvent($event, $arg, $caller);
  528. }
  529. }
  530. /**
  531. * @param string $layout
  532. *
  533. * @return string
  534. */
  535. public function getTemplate($layout)
  536. {
  537. $size = $this->calculateSize();
  538. switch ($layout) {
  539. case FormValidator::LAYOUT_INLINE:
  540. return '
  541. <div class="form-group {error_class}">
  542. <label {label-for} >
  543. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  544. {label}
  545. </label>
  546. {element}
  547. </div>';
  548. break;
  549. case FormValidator::LAYOUT_HORIZONTAL:
  550. return '
  551. <div class="form-group {error_class}">
  552. <label {label-for} class="col-sm-'.$size[0].' control-label {extra_label_class}" >
  553. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  554. {label}
  555. </label>
  556. <div class="col-sm-'.$size[1].'">
  557. {icon}
  558. {element}
  559. <!-- BEGIN label_2 -->
  560. <p class="help-block">{label_2}</p>
  561. <!-- END label_2 -->
  562. <!-- BEGIN error -->
  563. <span class="help-inline help-block">{error}</span>
  564. <!-- END error -->
  565. </div>
  566. <div class="col-sm-'.$size[2].'">
  567. <!-- BEGIN label_3 -->
  568. {label_3}
  569. <!-- END label_3 -->
  570. </div>
  571. </div>';
  572. break;
  573. case FormValidator::LAYOUT_BOX_NO_LABEL:
  574. return '
  575. <div class="input-group">
  576. {icon}
  577. {element}
  578. </div>';
  579. break;
  580. }
  581. }
  582. /**
  583. * Remove all options
  584. */
  585. public function clearOptions()
  586. {
  587. $this->_options = [];
  588. }
  589. }