FormRadio.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 a set of radio button elements
  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_FormRadio extends Zend_View_Helper_FormElement
  35. {
  36. /**
  37. * Input type to use
  38. * @var string
  39. */
  40. protected $_inputType = 'radio';
  41. /**
  42. * Whether or not this element represents an array collection by default
  43. * @var bool
  44. */
  45. protected $_isArray = false;
  46. /**
  47. * Generates a set of radio button elements.
  48. *
  49. * @access public
  50. *
  51. * @param string|array $name If a string, the element name. If an
  52. * array, all other parameters are ignored, and the array elements
  53. * are extracted in place of added parameters.
  54. *
  55. * @param mixed $value The radio value to mark as 'checked'.
  56. *
  57. * @param array $options An array of key-value pairs where the array
  58. * key is the radio value, and the array value is the radio text.
  59. *
  60. * @param array|string $attribs Attributes added to each radio.
  61. *
  62. * @return string The radio buttons XHTML.
  63. */
  64. public function formRadio($name, $value = null, $attribs = null,
  65. $options = null, $listsep = "<br />\n")
  66. {
  67. $info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
  68. extract($info); // name, value, attribs, options, listsep, disable
  69. // retrieve attributes for labels (prefixed with 'label_' or 'label')
  70. $label_attribs = array();
  71. foreach ($attribs as $key => $val) {
  72. $tmp = false;
  73. $keyLen = strlen($key);
  74. if ((6 < $keyLen) && (substr($key, 0, 6) == 'label_')) {
  75. $tmp = substr($key, 6);
  76. } elseif ((5 < $keyLen) && (substr($key, 0, 5) == 'label')) {
  77. $tmp = substr($key, 5);
  78. }
  79. if ($tmp) {
  80. // make sure first char is lowercase
  81. $tmp[0] = strtolower($tmp[0]);
  82. $label_attribs[$tmp] = $val;
  83. unset($attribs[$key]);
  84. }
  85. }
  86. $labelPlacement = 'append';
  87. foreach ($label_attribs as $key => $val) {
  88. switch (strtolower($key)) {
  89. case 'placement':
  90. unset($label_attribs[$key]);
  91. $val = strtolower($val);
  92. if (in_array($val, array('prepend', 'append'))) {
  93. $labelPlacement = $val;
  94. }
  95. break;
  96. }
  97. }
  98. // the radio button values and labels
  99. $options = (array) $options;
  100. // build the element
  101. $xhtml = '';
  102. $list = array();
  103. // should the name affect an array collection?
  104. $name = $this->view->escape($name);
  105. if ($this->_isArray && ('[]' != substr($name, -2))) {
  106. $name .= '[]';
  107. }
  108. // ensure value is an array to allow matching multiple times
  109. $value = (array) $value;
  110. // XHTML or HTML end tag?
  111. $endTag = ' />';
  112. if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
  113. $endTag= '>';
  114. }
  115. // add radio buttons to the list.
  116. require_once 'Zend/Filter/Alnum.php';
  117. $filter = new Zend_Filter_Alnum();
  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) . ' for="' . $optId . '">'
  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. . $endTag
  149. . (('append' == $labelPlacement) ? $opt_label : '')
  150. . '</label>';
  151. // add to the array of radio buttons
  152. $list[] = $radio;
  153. }
  154. // done!
  155. $xhtml .= implode($listsep, $list);
  156. return $xhtml;
  157. }
  158. }