/Zend/View/Helper/FormCheckbox.php

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