/Zend/View/Helper/FormCheckbox.php

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