PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/View/Helper/FormSelect.php

http://grupal.googlecode.com/
PHP | 200 lines | 93 code | 24 blank | 83 comment | 20 complexity | 3145e9885f646420d23311a4d06b90c9 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: FormSelect.php 25186 2013-01-08 08:18:19Z frosch $
  21. */
  22. /**
  23. * Abstract class for extension
  24. */
  25. require_once 'Zend/View/Helper/FormElement.php';
  26. /**
  27. * Helper to generate "select" list of options
  28. *
  29. * @category Zend
  30. * @package Zend_View
  31. * @subpackage Helper
  32. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_View_Helper_FormSelect extends Zend_View_Helper_FormElement
  36. {
  37. /**
  38. * Generates 'select' list of options.
  39. *
  40. * @access public
  41. *
  42. * @param string|array $name If a string, the element name. If an
  43. * array, all other parameters are ignored, and the array elements
  44. * are extracted in place of added parameters.
  45. *
  46. * @param mixed $value The option value to mark as 'selected'; if an
  47. * array, will mark all values in the array as 'selected' (used for
  48. * multiple-select elements).
  49. *
  50. * @param array|string $attribs Attributes added to the 'select' tag.
  51. * the optional 'optionClasses' attribute is used to add a class to
  52. * the options within the select (associative array linking the option
  53. * value to the desired class)
  54. *
  55. * @param array $options An array of key-value pairs where the array
  56. * key is the radio value, and the array value is the radio text.
  57. *
  58. * @param string $listsep When disabled, use this list separator string
  59. * between list values.
  60. *
  61. * @return string The select tag and options XHTML.
  62. */
  63. public function formSelect($name, $value = null, $attribs = null,
  64. $options = null, $listsep = "<br />\n")
  65. {
  66. $info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
  67. extract($info); // name, id, value, attribs, options, listsep, disable
  68. // force $value to array so we can compare multiple values to multiple
  69. // options; also ensure it's a string for comparison purposes.
  70. $value = array_map('strval', (array) $value);
  71. // check if element may have multiple values
  72. $multiple = '';
  73. if (substr($name, -2) == '[]') {
  74. // multiple implied by the name
  75. $multiple = ' multiple="multiple"';
  76. }
  77. if (isset($attribs['multiple'])) {
  78. // Attribute set
  79. if ($attribs['multiple']) {
  80. // True attribute; set multiple attribute
  81. $multiple = ' multiple="multiple"';
  82. // Make sure name indicates multiple values are allowed
  83. if (!empty($multiple) && (substr($name, -2) != '[]')) {
  84. $name .= '[]';
  85. }
  86. } else {
  87. // False attribute; ensure attribute not set
  88. $multiple = '';
  89. }
  90. unset($attribs['multiple']);
  91. }
  92. // handle the options classes
  93. $optionClasses = array();
  94. if (isset($attribs['optionClasses'])) {
  95. $optionClasses = $attribs['optionClasses'];
  96. unset($attribs['optionClasses']);
  97. }
  98. // now start building the XHTML.
  99. $disabled = '';
  100. if (true === $disable) {
  101. $disabled = ' disabled="disabled"';
  102. }
  103. // Build the surrounding select element first.
  104. $xhtml = '<select'
  105. . ' name="' . $this->view->escape($name) . '"'
  106. . ' id="' . $this->view->escape($id) . '"'
  107. . $multiple
  108. . $disabled
  109. . $this->_htmlAttribs($attribs)
  110. . ">\n ";
  111. // build the list of options
  112. $list = array();
  113. $translator = $this->getTranslator();
  114. foreach ((array) $options as $opt_value => $opt_label) {
  115. if (is_array($opt_label)) {
  116. $opt_disable = '';
  117. if (is_array($disable) && in_array($opt_value, $disable)) {
  118. $opt_disable = ' disabled="disabled"';
  119. }
  120. if (null !== $translator) {
  121. $opt_value = $translator->translate($opt_value);
  122. }
  123. $opt_id = ' id="' . $this->view->escape($id) . '-optgroup-'
  124. . $this->view->escape($opt_value) . '"';
  125. $list[] = '<optgroup'
  126. . $opt_disable
  127. . $opt_id
  128. . ' label="' . $this->view->escape($opt_value) .'">';
  129. foreach ($opt_label as $val => $lab) {
  130. $list[] = $this->_build($val, $lab, $value, $disable, $optionClasses);
  131. }
  132. $list[] = '</optgroup>';
  133. } else {
  134. $list[] = $this->_build($opt_value, $opt_label, $value, $disable, $optionClasses);
  135. }
  136. }
  137. // add the options to the xhtml and close the select
  138. $xhtml .= implode("\n ", $list) . "\n</select>";
  139. return $xhtml;
  140. }
  141. /**
  142. * Builds the actual <option> tag
  143. *
  144. * @param string $value Options Value
  145. * @param string $label Options Label
  146. * @param array $selected The option value(s) to mark as 'selected'
  147. * @param array|bool $disable Whether the select is disabled, or individual options are
  148. * @param array $optionClasses The classes to associate with each option value
  149. * @return string Option Tag XHTML
  150. */
  151. protected function _build($value, $label, $selected, $disable, $optionClasses = array())
  152. {
  153. if (is_bool($disable)) {
  154. $disable = array();
  155. }
  156. $class = null;
  157. if (array_key_exists($value, $optionClasses)) {
  158. $class = $optionClasses[$value];
  159. }
  160. $opt = '<option'
  161. . ' value="' . $this->view->escape($value) . '"';
  162. if ($class) {
  163. $opt .= ' class="' . $class . '"';
  164. }
  165. // selected?
  166. if (in_array((string) $value, $selected)) {
  167. $opt .= ' selected="selected"';
  168. }
  169. // disabled?
  170. if (in_array($value, $disable)) {
  171. $opt .= ' disabled="disabled"';
  172. }
  173. $opt .= '>' . $this->view->escape($label) . "</option>";
  174. return $opt;
  175. }
  176. }