CheckBox.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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-2015 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-2015 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. * Options that will be passed to the view helper
  49. * @var array
  50. */
  51. public $options = array(
  52. 'checkedValue' => '1',
  53. 'uncheckedValue' => '0',
  54. );
  55. /**
  56. * Value when checked
  57. * @var string
  58. */
  59. protected $_checkedValue = '1';
  60. /**
  61. * Value when not checked
  62. * @var string
  63. */
  64. protected $_uncheckedValue = '0';
  65. /**
  66. * Current value
  67. * @var string 0 or 1
  68. */
  69. protected $_value = '0';
  70. /**
  71. * Set options
  72. *
  73. * Intercept checked and unchecked values and set them early; test stored
  74. * value against checked and unchecked values after configuration.
  75. *
  76. * @param array $options
  77. * @return Zend_Form_Element_Checkbox
  78. */
  79. public function setOptions(array $options)
  80. {
  81. if (array_key_exists('checkedValue', $options)) {
  82. $this->setCheckedValue($options['checkedValue']);
  83. unset($options['checkedValue']);
  84. }
  85. if (array_key_exists('uncheckedValue', $options)) {
  86. $this->setUncheckedValue($options['uncheckedValue']);
  87. unset($options['uncheckedValue']);
  88. }
  89. parent::setOptions($options);
  90. $curValue = $this->getValue();
  91. $test = array($this->getCheckedValue(), $this->getUncheckedValue());
  92. if (!in_array($curValue, $test)) {
  93. $this->setValue($curValue);
  94. }
  95. return $this;
  96. }
  97. /**
  98. * Set value
  99. *
  100. * If value matches checked value, sets to that value, and sets the checked
  101. * flag to true.
  102. *
  103. * Any other value causes the unchecked value to be set as the current
  104. * value, and the checked flag to be set as false.
  105. *
  106. *
  107. * @param mixed $value
  108. * @return Zend_Form_Element_Checkbox
  109. */
  110. public function setValue($value)
  111. {
  112. if ($value == $this->getCheckedValue()) {
  113. parent::setValue($value);
  114. $this->checked = true;
  115. } else {
  116. parent::setValue($this->getUncheckedValue());
  117. $this->checked = false;
  118. }
  119. return $this;
  120. }
  121. /**
  122. * Set checked value
  123. *
  124. * @param string $value
  125. * @return Zend_Form_Element_Checkbox
  126. */
  127. public function setCheckedValue($value)
  128. {
  129. $this->_checkedValue = (string) $value;
  130. $this->options['checkedValue'] = $value;
  131. return $this;
  132. }
  133. /**
  134. * Get value when checked
  135. *
  136. * @return string
  137. */
  138. public function getCheckedValue()
  139. {
  140. return $this->_checkedValue;
  141. }
  142. /**
  143. * Set unchecked value
  144. *
  145. * @param string $value
  146. * @return Zend_Form_Element_Checkbox
  147. */
  148. public function setUncheckedValue($value)
  149. {
  150. $this->_uncheckedValue = (string) $value;
  151. $this->options['uncheckedValue'] = $value;
  152. return $this;
  153. }
  154. /**
  155. * Get value when not checked
  156. *
  157. * @return string
  158. */
  159. public function getUncheckedValue()
  160. {
  161. return $this->_uncheckedValue;
  162. }
  163. /**
  164. * Set checked flag
  165. *
  166. * @param bool $flag
  167. * @return Zend_Form_Element_Checkbox
  168. */
  169. public function setChecked($flag)
  170. {
  171. $this->checked = (bool) $flag;
  172. if ($this->checked) {
  173. $this->setValue($this->getCheckedValue());
  174. } else {
  175. $this->setValue($this->getUncheckedValue());
  176. }
  177. return $this;
  178. }
  179. /**
  180. * Get checked flag
  181. *
  182. * @return bool
  183. */
  184. public function isChecked()
  185. {
  186. return $this->checked;
  187. }
  188. }