2
0

FormRadio.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. * @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-2009 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. // XHTML or HTML end tag?
  112. $endTag = ' />';
  113. if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
  114. $endTag= '>';
  115. }
  116. // add radio buttons to the list.
  117. require_once 'Zend/Filter/Alnum.php';
  118. $filter = new Zend_Filter_Alnum();
  119. foreach ($options as $opt_value => $opt_label) {
  120. // Should the label be escaped?
  121. if ($escape) {
  122. $opt_label = $this->view->escape($opt_label);
  123. }
  124. // is it disabled?
  125. $disabled = '';
  126. if (true === $disable) {
  127. $disabled = ' disabled="disabled"';
  128. } elseif (is_array($disable) && in_array($opt_value, $disable)) {
  129. $disabled = ' disabled="disabled"';
  130. }
  131. // is it checked?
  132. $checked = '';
  133. if (in_array($opt_value, $value)) {
  134. $checked = ' checked="checked"';
  135. }
  136. // generate ID
  137. $optId = $id . '-' . $filter->filter($opt_value);
  138. // Wrap the radios in labels
  139. $radio = '<label'
  140. . $this->_htmlAttribs($label_attribs) . ' for="' . $optId . '">'
  141. . (('prepend' == $labelPlacement) ? $opt_label : '')
  142. . '<input type="' . $this->_inputType . '"'
  143. . ' name="' . $name . '"'
  144. . ' id="' . $optId . '"'
  145. . ' value="' . $this->view->escape($opt_value) . '"'
  146. . $checked
  147. . $disabled
  148. . $this->_htmlAttribs($attribs)
  149. . $endTag
  150. . (('append' == $labelPlacement) ? $opt_label : '')
  151. . '</label>';
  152. // add to the array of radio buttons
  153. $list[] = $radio;
  154. }
  155. // done!
  156. $xhtml .= implode($listsep, $list);
  157. return $xhtml;
  158. }
  159. }