2
0

FormCheckbox.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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-2008 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 "checkbox" element
  27. *
  28. * @category Zend
  29. * @package Zend_View
  30. * @subpackage Helper
  31. * @copyright Copyright (c) 2005-2008 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_FormCheckbox extends Zend_View_Helper_FormElement
  35. {
  36. /**
  37. * Default checked/unchecked options
  38. * @var array
  39. */
  40. protected static $_defaultCheckedOptions = array(
  41. 'checked' => '1',
  42. 'unChecked' => '0'
  43. );
  44. /**
  45. * Generates a 'checkbox' element.
  46. *
  47. * @access public
  48. *
  49. * @param string|array $name If a string, the element name. If an
  50. * array, all other parameters are ignored, and the array elements
  51. * are extracted in place of added parameters.
  52. * @param mixed $value The element value.
  53. * @param array $attribs Attributes for the element tag.
  54. * @return string The element XHTML.
  55. */
  56. public function formCheckbox($name, $value = null, $attribs = null, array $checkedOptions = null)
  57. {
  58. $info = $this->_getInfo($name, $value, $attribs);
  59. extract($info); // name, id, value, attribs, options, listsep, disable
  60. $checked = false;
  61. if (isset($attribs['checked']) && $attribs['checked']) {
  62. $checked = true;
  63. unset($attribs['checked']);
  64. } elseif (isset($attribs['checked'])) {
  65. $checked = false;
  66. unset($attribs['checked']);
  67. }
  68. $checkedOptions = self::determineCheckboxInfo($value, $checked, $checkedOptions);
  69. // is the element disabled?
  70. $disabled = '';
  71. if ($disable) {
  72. $disabled = ' disabled="disabled"';
  73. }
  74. // XHTML or HTML end tag?
  75. $endTag = ' />';
  76. if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
  77. $endTag= '>';
  78. }
  79. // build the element
  80. $xhtml = '';
  81. if (!strstr($name, '[]')) {
  82. $xhtml = $this->_hidden($name, $checkedOptions['unCheckedValue']);
  83. }
  84. $xhtml .= '<input type="checkbox"'
  85. . ' name="' . $this->view->escape($name) . '"'
  86. . ' id="' . $this->view->escape($id) . '"'
  87. . ' value="' . $this->view->escape($checkedOptions['checkedValue']) . '"'
  88. . $checkedOptions['checkedString']
  89. . $disabled
  90. . $this->_htmlAttribs($attribs)
  91. . $endTag;
  92. return $xhtml;
  93. }
  94. /**
  95. * Determine checkbox information
  96. *
  97. * @param string $value
  98. * @param bool $checked
  99. * @param array|null $checkedOptions
  100. * @return array
  101. */
  102. public static function determineCheckboxInfo($value, $checked, array $checkedOptions = null)
  103. {
  104. // Checked/unchecked values
  105. $checkedValue = null;
  106. $unCheckedValue = null;
  107. if (is_array($checkedOptions)) {
  108. if (array_key_exists('checked', $checkedOptions)) {
  109. $checkedValue = (string) $checkedOptions['checked'];
  110. unset($checkedOptions['checked']);
  111. }
  112. if (array_key_exists('unChecked', $checkedOptions)) {
  113. $unCheckedValue = (string) $checkedOptions['unChecked'];
  114. unset($checkedOptions['unChecked']);
  115. }
  116. if (null === $checkedValue) {
  117. $checkedValue = array_shift($checkedOptions);
  118. }
  119. if (null === $unCheckedValue) {
  120. $unCheckedValue = array_shift($checkedOptions);
  121. }
  122. } elseif ($value !== null) {
  123. $unCheckedValue = self::$_defaultCheckedOptions['unChecked'];
  124. } else {
  125. $checkedValue = self::$_defaultCheckedOptions['checked'];
  126. $unCheckedValue = self::$_defaultCheckedOptions['unChecked'];
  127. }
  128. // is the element checked?
  129. $checkedString = '';
  130. if ($checked || ($value === $checkedValue)) {
  131. $checkedString = ' checked="checked"';
  132. $checked = true;
  133. } else {
  134. $checked = false;
  135. }
  136. // Checked value should be value if no checked options provided
  137. if ($checkedValue == null) {
  138. $checkedValue = $value;
  139. }
  140. return array(
  141. 'checked' => $checked,
  142. 'checkedString' => $checkedString,
  143. 'checkedValue' => $checkedValue,
  144. 'unCheckedValue' => $unCheckedValue,
  145. );
  146. }
  147. }