2
0

FormRadio.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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$
  21. */
  22. /**
  23. * Abstract class for extension
  24. */
  25. require_once 'Zend/View/Helper/FormElement.php';
  26. /**
  27. * Helper to generate a set of radio button elements
  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_FormRadio extends Zend_View_Helper_FormElement
  36. {
  37. /**
  38. * Input type to use
  39. * @var string
  40. */
  41. protected $_inputType = 'radio';
  42. /**
  43. * Whether or not this element represents an array collection by default
  44. * @var bool
  45. */
  46. protected $_isArray = false;
  47. /**
  48. * Generates a set of radio button elements.
  49. *
  50. * @access public
  51. *
  52. * @param string|array $name If a string, the element name. If an
  53. * array, all other parameters are ignored, and the array elements
  54. * are extracted in place of added parameters.
  55. *
  56. * @param mixed $value The radio value to mark as 'checked'.
  57. *
  58. * @param array $options An array of key-value pairs where the array
  59. * key is the radio value, and the array value is the radio text.
  60. *
  61. * @param array|string $attribs Attributes added to each radio.
  62. *
  63. * @return string The radio buttons XHTML.
  64. */
  65. public function formRadio($name, $value = null, $attribs = null,
  66. $options = null, $listsep = "<br />\n")
  67. {
  68. $info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
  69. extract($info); // name, value, attribs, options, listsep, disable
  70. // retrieve attributes for labels (prefixed with 'label_' or 'label')
  71. $label_attribs = array();
  72. foreach ($attribs as $key => $val) {
  73. $tmp = false;
  74. $keyLen = strlen($key);
  75. if ((6 < $keyLen) && (substr($key, 0, 6) == 'label_')) {
  76. $tmp = substr($key, 6);
  77. } elseif ((5 < $keyLen) && (substr($key, 0, 5) == 'label')) {
  78. $tmp = substr($key, 5);
  79. }
  80. if ($tmp) {
  81. // make sure first char is lowercase
  82. $tmp[0] = strtolower($tmp[0]);
  83. $label_attribs[$tmp] = $val;
  84. unset($attribs[$key]);
  85. }
  86. }
  87. $labelPlacement = 'append';
  88. foreach ($label_attribs as $key => $val) {
  89. switch (strtolower($key)) {
  90. case 'placement':
  91. unset($label_attribs[$key]);
  92. $val = strtolower($val);
  93. if (in_array($val, array('prepend', 'append'))) {
  94. $labelPlacement = $val;
  95. }
  96. break;
  97. }
  98. }
  99. // the radio button values and labels
  100. $options = (array) $options;
  101. // build the element
  102. $xhtml = '';
  103. $list = array();
  104. // should the name affect an array collection?
  105. $name = $this->view->escape($name);
  106. if ($this->_isArray && ('[]' != substr($name, -2))) {
  107. $name .= '[]';
  108. }
  109. // ensure value is an array to allow matching multiple times
  110. $value = (array) $value;
  111. // Set up the filter - Alnum + hyphen + underscore
  112. require_once 'Zend/Filter/PregReplace.php';
  113. $pattern = @preg_match('/\pL/u', 'a')
  114. ? '/[^\p{L}\p{N}\-\_]/u' // Unicode
  115. : '/[^a-zA-Z0-9\-\_]/'; // No Unicode
  116. $filter = new Zend_Filter_PregReplace($pattern, "");
  117. // add radio buttons to the list.
  118. foreach ($options as $opt_value => $opt_label) {
  119. // Should the label be escaped?
  120. if ($escape) {
  121. $opt_label = $this->view->escape($opt_label);
  122. }
  123. // is it disabled?
  124. $disabled = '';
  125. if (true === $disable) {
  126. $disabled = ' disabled="disabled"';
  127. } elseif (is_array($disable) && in_array($opt_value, $disable)) {
  128. $disabled = ' disabled="disabled"';
  129. }
  130. // is it checked?
  131. $checked = '';
  132. if (in_array($opt_value, $value)) {
  133. $checked = ' checked="checked"';
  134. }
  135. // generate ID
  136. $optId = $id . '-' . $filter->filter($opt_value);
  137. // Wrap the radios in labels
  138. $radio = '<label'
  139. . $this->_htmlAttribs($label_attribs) . '>'
  140. . (('prepend' == $labelPlacement) ? $opt_label : '')
  141. . '<input type="' . $this->_inputType . '"'
  142. . ' name="' . $name . '"'
  143. . ' id="' . $optId . '"'
  144. . ' value="' . $this->view->escape($opt_value) . '"'
  145. . $checked
  146. . $disabled
  147. . $this->_htmlAttribs($attribs)
  148. . $this->getClosingBracket()
  149. . (('append' == $labelPlacement) ? $opt_label : '')
  150. . '</label>';
  151. // add to the array of radio buttons
  152. $list[] = $radio;
  153. }
  154. // XHTML or HTML for standard list separator?
  155. if (!$this->_isXhtml() && false !== strpos($listsep, '<br />')) {
  156. $listsep = str_replace('<br />', '<br>', $listsep);
  157. }
  158. // done!
  159. $xhtml .= implode($listsep, $list);
  160. return $xhtml;
  161. }
  162. }