PageRenderTime 33ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/View/Helper/FormSelect.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 181 lines | 82 code | 21 blank | 78 comment | 17 complexity | 46a80bfc300bead1ba036825651fdd23 MD5 | raw file
  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-2011 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 24158 2011-06-27 15:31:54Z ezimuel $
  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-2011 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. *
  52. * @param array $options An array of key-value pairs where the array
  53. * key is the radio value, and the array value is the radio text.
  54. *
  55. * @param string $listsep When disabled, use this list separator string
  56. * between list values.
  57. *
  58. * @return string The select tag and options XHTML.
  59. */
  60. public function formSelect($name, $value = null, $attribs = null,
  61. $options = null, $listsep = "<br />\n")
  62. {
  63. $info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
  64. extract($info); // name, id, value, attribs, options, listsep, disable
  65. // force $value to array so we can compare multiple values to multiple
  66. // options; also ensure it's a string for comparison purposes.
  67. $value = array_map('strval', (array) $value);
  68. // check if element may have multiple values
  69. $multiple = '';
  70. if (substr($name, -2) == '[]') {
  71. // multiple implied by the name
  72. $multiple = ' multiple="multiple"';
  73. }
  74. if (isset($attribs['multiple'])) {
  75. // Attribute set
  76. if ($attribs['multiple']) {
  77. // True attribute; set multiple attribute
  78. $multiple = ' multiple="multiple"';
  79. // Make sure name indicates multiple values are allowed
  80. if (!empty($multiple) && (substr($name, -2) != '[]')) {
  81. $name .= '[]';
  82. }
  83. } else {
  84. // False attribute; ensure attribute not set
  85. $multiple = '';
  86. }
  87. unset($attribs['multiple']);
  88. }
  89. // now start building the XHTML.
  90. $disabled = '';
  91. if (true === $disable) {
  92. $disabled = ' disabled="disabled"';
  93. }
  94. // Build the surrounding select element first.
  95. $xhtml = '<select'
  96. . ' name="' . $this->view->escape($name) . '"'
  97. . ' id="' . $this->view->escape($id) . '"'
  98. . $multiple
  99. . $disabled
  100. . $this->_htmlAttribs($attribs)
  101. . ">\n ";
  102. // build the list of options
  103. $list = array();
  104. $translator = $this->getTranslator();
  105. foreach ((array) $options as $opt_value => $opt_label) {
  106. if (is_array($opt_label)) {
  107. $opt_disable = '';
  108. if (is_array($disable) && in_array($opt_value, $disable)) {
  109. $opt_disable = ' disabled="disabled"';
  110. }
  111. if (null !== $translator) {
  112. $opt_value = $translator->translate($opt_value);
  113. }
  114. $opt_id = ' id="' . $this->view->escape($id) . '-optgroup-'
  115. . $this->view->escape($opt_value) . '"';
  116. $list[] = '<optgroup'
  117. . $opt_disable
  118. . $opt_id
  119. . ' label="' . $this->view->escape($opt_value) .'">';
  120. foreach ($opt_label as $val => $lab) {
  121. $list[] = $this->_build($val, $lab, $value, $disable);
  122. }
  123. $list[] = '</optgroup>';
  124. } else {
  125. $list[] = $this->_build($opt_value, $opt_label, $value, $disable);
  126. }
  127. }
  128. // add the options to the xhtml and close the select
  129. $xhtml .= implode("\n ", $list) . "\n</select>";
  130. return $xhtml;
  131. }
  132. /**
  133. * Builds the actual <option> tag
  134. *
  135. * @param string $value Options Value
  136. * @param string $label Options Label
  137. * @param array $selected The option value(s) to mark as 'selected'
  138. * @param array|bool $disable Whether the select is disabled, or individual options are
  139. * @return string Option Tag XHTML
  140. */
  141. protected function _build($value, $label, $selected, $disable)
  142. {
  143. if (is_bool($disable)) {
  144. $disable = array();
  145. }
  146. $opt = '<option'
  147. . ' value="' . $this->view->escape($value) . '"'
  148. . ' label="' . $this->view->escape($label) . '"';
  149. // selected?
  150. if (in_array((string) $value, $selected)) {
  151. $opt .= ' selected="selected"';
  152. }
  153. // disabled?
  154. if (in_array($value, $disable)) {
  155. $opt .= ' disabled="disabled"';
  156. }
  157. $opt .= '>' . $this->view->escape($label) . "</option>";
  158. return $opt;
  159. }
  160. }