select.php 20 KB

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