CheckBox.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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_Dojo
  17. * @subpackage Form_Element
  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. /** Zend_Dojo_Form_Element_Dijit */
  22. require_once 'Zend/Dojo/Form/Element/Dijit.php';
  23. /**
  24. * CheckBox dijit
  25. *
  26. * Note: this would be easier with mixins or traits...
  27. *
  28. * @uses Zend_Dojo_Form_Element_Dijit
  29. * @package Zend_Dojo
  30. * @subpackage Form_Element
  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. * @version $Id$
  34. */
  35. class Zend_Dojo_Form_Element_CheckBox extends Zend_Dojo_Form_Element_Dijit
  36. {
  37. /**
  38. * Is the checkbox checked?
  39. * @var bool
  40. */
  41. public $checked = false;
  42. /**
  43. * Use formCheckbox view helper by default
  44. * @var string
  45. */
  46. public $helper = 'CheckBox';
  47. /**
  48. * Value when checked
  49. * @var string
  50. */
  51. protected $_checkedValue = '1';
  52. /**
  53. * Value when not checked
  54. * @var string
  55. */
  56. protected $_uncheckedValue = '0';
  57. /**
  58. * Current value
  59. * @var string 0 or 1
  60. */
  61. protected $_value = '0';
  62. /**
  63. * Set options
  64. *
  65. * Intercept checked and unchecked values and set them early; test stored
  66. * value against checked and unchecked values after configuration.
  67. *
  68. * @param array $options
  69. * @return Zend_Form_Element_Checkbox
  70. */
  71. public function setOptions(array $options)
  72. {
  73. if (array_key_exists('checkedValue', $options)) {
  74. $this->setCheckedValue($options['checkedValue']);
  75. unset($options['checkedValue']);
  76. }
  77. if (array_key_exists('uncheckedValue', $options)) {
  78. $this->setUncheckedValue($options['uncheckedValue']);
  79. unset($options['uncheckedValue']);
  80. }
  81. parent::setOptions($options);
  82. $curValue = $this->getValue();
  83. $test = array($this->getCheckedValue(), $this->getUncheckedValue());
  84. if (!in_array($curValue, $test)) {
  85. $this->setValue($curValue);
  86. }
  87. return $this;
  88. }
  89. /**
  90. * Set value
  91. *
  92. * If value matches checked value, sets to that value, and sets the checked
  93. * flag to true.
  94. *
  95. * Any other value causes the unchecked value to be set as the current
  96. * value, and the checked flag to be set as false.
  97. *
  98. *
  99. * @param mixed $value
  100. * @return Zend_Form_Element_Checkbox
  101. */
  102. public function setValue($value)
  103. {
  104. if ($value == $this->getCheckedValue()) {
  105. parent::setValue($value);
  106. $this->checked = true;
  107. } else {
  108. parent::setValue($this->getUncheckedValue());
  109. $this->checked = false;
  110. }
  111. return $this;
  112. }
  113. /**
  114. * Set checked value
  115. *
  116. * @param string $value
  117. * @return Zend_Form_Element_Checkbox
  118. */
  119. public function setCheckedValue($value)
  120. {
  121. $this->_checkedValue = (string) $value;
  122. return $this;
  123. }
  124. /**
  125. * Get value when checked
  126. *
  127. * @return string
  128. */
  129. public function getCheckedValue()
  130. {
  131. return $this->_checkedValue;
  132. }
  133. /**
  134. * Set unchecked value
  135. *
  136. * @param string $value
  137. * @return Zend_Form_Element_Checkbox
  138. */
  139. public function setUncheckedValue($value)
  140. {
  141. $this->_uncheckedValue = (string) $value;
  142. return $this;
  143. }
  144. /**
  145. * Get value when not checked
  146. *
  147. * @return string
  148. */
  149. public function getUncheckedValue()
  150. {
  151. return $this->_uncheckedValue;
  152. }
  153. /**
  154. * Set checked flag
  155. *
  156. * @param bool $flag
  157. * @return Zend_Form_Element_Checkbox
  158. */
  159. public function setChecked($flag)
  160. {
  161. $this->checked = (bool) $flag;
  162. if ($this->checked) {
  163. $this->setValue($this->getCheckedValue());
  164. } else {
  165. $this->setValue($this->getUncheckedValue());
  166. }
  167. return $this;
  168. }
  169. /**
  170. * Get checked flag
  171. *
  172. * @return bool
  173. */
  174. public function isChecked()
  175. {
  176. return $this->checked;
  177. }
  178. /**
  179. * Render
  180. *
  181. * Ensure that options property is set when rendering.
  182. *
  183. * @param Zend_View_Interface $view
  184. * @return string
  185. */
  186. public function render(Zend_View_Interface $view = null)
  187. {
  188. $this->options = array(
  189. 'checked' => $this->getCheckedValue(),
  190. 'unChecked' => $this->getUncheckedValue(),
  191. );
  192. return parent::render($view);
  193. }
  194. }