FormSelect.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage Helper
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * Abstract class for extension
  23. */
  24. require_once 'Zend/View/Helper/FormElement.php';
  25. /**
  26. * Helper to generate "select" list of options
  27. *
  28. * @category Zend
  29. * @package Zend_View
  30. * @subpackage Helper
  31. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_View_Helper_FormSelect extends Zend_View_Helper_FormElement
  35. {
  36. /**
  37. * Generates 'select' list of options.
  38. *
  39. * @access public
  40. *
  41. * @param string|array $name If a string, the element name. If an
  42. * array, all other parameters are ignored, and the array elements
  43. * are extracted in place of added parameters.
  44. *
  45. * @param mixed $value The option value to mark as 'selected'; if an
  46. * array, will mark all values in the array as 'selected' (used for
  47. * multiple-select elements).
  48. *
  49. * @param array|string $attribs Attributes added to the 'select' tag.
  50. *
  51. * @param array $options An array of key-value pairs where the array
  52. * key is the radio value, and the array value is the radio text.
  53. *
  54. * @param string $listsep When disabled, use this list separator string
  55. * between list values.
  56. *
  57. * @return string The select tag and options XHTML.
  58. */
  59. public function formSelect($name, $value = null, $attribs = null,
  60. $options = null, $listsep = "<br />\n")
  61. {
  62. $info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
  63. extract($info); // name, id, value, attribs, options, listsep, disable
  64. // force $value to array so we can compare multiple values to multiple
  65. // options; also ensure it's a string for comparison purposes.
  66. $value = array_map('strval', (array) $value);
  67. // check if element may have multiple values
  68. $multiple = '';
  69. if (substr($name, -2) == '[]') {
  70. // multiple implied by the name
  71. $multiple = ' multiple="multiple"';
  72. }
  73. if (isset($attribs['multiple'])) {
  74. // Attribute set
  75. if ($attribs['multiple']) {
  76. // True attribute; set multiple attribute
  77. $multiple = ' multiple="multiple"';
  78. // Make sure name indicates multiple values are allowed
  79. if (!empty($multiple) && (substr($name, -2) != '[]')) {
  80. $name .= '[]';
  81. }
  82. } else {
  83. // False attribute; ensure attribute not set
  84. $multiple = '';
  85. }
  86. unset($attribs['multiple']);
  87. }
  88. // now start building the XHTML.
  89. $disabled = '';
  90. if (true === $disable) {
  91. $disabled = ' disabled="disabled"';
  92. }
  93. // Build the surrounding select element first.
  94. $xhtml = '<select'
  95. . ' name="' . $this->view->escape($name) . '"'
  96. . ' id="' . $this->view->escape($id) . '"'
  97. . $multiple
  98. . $disabled
  99. . $this->_htmlAttribs($attribs)
  100. . ">\n ";
  101. // build the list of options
  102. $list = array();
  103. $translator = $this->getTranslator();
  104. foreach ((array) $options as $opt_value => $opt_label) {
  105. if (is_array($opt_label)) {
  106. $opt_disable = '';
  107. if (is_array($disable) && in_array($opt_value, $disable)) {
  108. $opt_disable = ' disabled="disabled"';
  109. }
  110. if (null !== $translator) {
  111. $opt_value = $translator->translate($opt_value);
  112. }
  113. $list[] = '<optgroup'
  114. . $opt_disable
  115. . ' label="' . $this->view->escape($opt_value) .'">';
  116. foreach ($opt_label as $val => $lab) {
  117. $list[] = $this->_build($val, $lab, $value, $disable);
  118. }
  119. $list[] = '</optgroup>';
  120. } else {
  121. $list[] = $this->_build($opt_value, $opt_label, $value, $disable);
  122. }
  123. }
  124. // add the options to the xhtml and close the select
  125. $xhtml .= implode("\n ", $list) . "\n</select>";
  126. return $xhtml;
  127. }
  128. /**
  129. * Builds the actual <option> tag
  130. *
  131. * @param string $value Options Value
  132. * @param string $label Options Label
  133. * @param array $selected The option value(s) to mark as 'selected'
  134. * @param array|bool $disable Whether the select is disabled, or individual options are
  135. * @return string Option Tag XHTML
  136. */
  137. protected function _build($value, $label, $selected, $disable)
  138. {
  139. if (is_bool($disable)) {
  140. $disable = array();
  141. }
  142. $opt = '<option'
  143. . ' value="' . $this->view->escape($value) . '"'
  144. . ' label="' . $this->view->escape($label) . '"';
  145. // selected?
  146. if (in_array((string) $value, $selected)) {
  147. $opt .= ' selected="selected"';
  148. }
  149. // disabled?
  150. if (in_array($value, $disable)) {
  151. $opt .= ' disabled="disabled"';
  152. }
  153. $opt .= '>' . $this->view->escape($label) . "</option>";
  154. return $opt;
  155. }
  156. }