select.php 20 KB

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