/standard/trunk/library/Zend/View/Helper/FormCheckbox.php

https://github.com/bhaumik25/zend-framework · PHP · 135 lines · 70 code · 12 blank · 53 comment · 15 complexity · 4435fd9a8f4cc5bbb1b8e195d0892929 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-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 $_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/unchecked values
  61. $checkedValue = null;
  62. $unCheckedValue = null;
  63. if (is_array($checkedOptions)) {
  64. if (array_key_exists('checked', $checkedOptions)) {
  65. $checkedValue = (string) $checkedOptions['checked'];
  66. unset($checkedOptions['checked']);
  67. }
  68. if (array_key_exists('unChecked', $checkedOptions)) {
  69. $unCheckedValue = (string) $checkedOptions['unChecked'];
  70. unset($checkedOptions['unChecked']);
  71. }
  72. if (null === $checkedValue) {
  73. $checkedValue = array_shift($checkedOptions);
  74. }
  75. if (null === $unCheckedValue) {
  76. $unCheckedValue = array_shift($checkedOptions);
  77. }
  78. } elseif ($value !== null) {
  79. $unCheckedValue = $this->_defaultCheckedOptions['unChecked'];
  80. } else {
  81. $checkedValue = $this->_defaultCheckedOptions['checked'];
  82. $unCheckedValue = $this->_defaultCheckedOptions['unChecked'];
  83. }
  84. // is the element checked?
  85. $checked = '';
  86. if (empty($checked) && isset($attribs['checked']) && $attribs['checked']) {
  87. $checked = ' checked="checked"';
  88. unset($attribs['checked']);
  89. } elseif (isset($attribs['checked'])) {
  90. unset($attribs['checked']);
  91. } elseif ($value === $checkedValue) {
  92. $checked = ' checked="checked"';
  93. }
  94. // Checked value should be value if no checked options provided
  95. if ($checkedValue == null) {
  96. $checkedValue = $value;
  97. }
  98. // is the element disabled?
  99. $disabled = '';
  100. if ($disable) {
  101. $disabled = ' disabled="disabled"';
  102. }
  103. // XHTML or HTML end tag?
  104. $endTag = ' />';
  105. if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
  106. $endTag= '>';
  107. }
  108. // build the element
  109. $xhtml = '';
  110. if (!strstr($name, '[]')) {
  111. $xhtml = $this->_hidden($name, $unCheckedValue);
  112. }
  113. $xhtml .= '<input type="checkbox"'
  114. . ' name="' . $this->view->escape($name) . '"'
  115. . ' id="' . $this->view->escape($id) . '"'
  116. . ' value="' . $this->view->escape($checkedValue) . '"'
  117. . $checked
  118. . $disabled
  119. . $this->_htmlAttribs($attribs)
  120. . $endTag;
  121. return $xhtml;
  122. }
  123. }