checkbox.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /**
  3. * HTML class for a checkbox type field
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: This source file is subject to version 3.01 of the PHP license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  10. * the PHP License and are unable to obtain it through the web, please
  11. * send a note to license@php.net so we can mail you a copy immediately.
  12. *
  13. * @category HTML
  14. * @package HTML_QuickForm
  15. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  16. * @author Bertrand Mansion <bmansion@mamasam.com>
  17. * @author Alexey Borzov <avb@php.net>
  18. * @copyright 2001-2009 The PHP Group
  19. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  20. * @version CVS: $Id: checkbox.php,v 1.23 2009/04/04 21:34:02 avb Exp $
  21. * @link http://pear.php.net/package/HTML_QuickForm
  22. */
  23. /**
  24. * HTML class for a checkbox type field
  25. *
  26. * @category HTML
  27. * @package HTML_QuickForm
  28. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  29. * @author Bertrand Mansion <bmansion@mamasam.com>
  30. * @author Alexey Borzov <avb@php.net>
  31. * @version Release: 3.2.11
  32. * @since 1.0
  33. */
  34. class HTML_QuickForm_checkbox extends HTML_QuickForm_input
  35. {
  36. /**
  37. * Checkbox display text
  38. * @var string
  39. * @since 1.1
  40. * @access private
  41. */
  42. public $_text = '';
  43. public $labelClass;
  44. public $checkboxClass;
  45. // }}}
  46. // {{{ constructor
  47. /**
  48. * Class constructor
  49. *
  50. * @param string $elementName (optional)Input field name attribute
  51. * @param string $elementLabel (optional)Input field value
  52. * @param string $text (optional)Checkbox display text
  53. * @param mixed $attributes (optional)Either a typical HTML attribute string
  54. * or an associative array
  55. * @since 1.0
  56. * @access public
  57. * @return void
  58. */
  59. public function __construct(
  60. $elementName = null,
  61. $elementLabel = null,
  62. $text = '',
  63. $attributes = null
  64. ) {
  65. $this->labelClass = isset($attributes['label-class']) ? $attributes['label-class'] : '';
  66. $this->checkboxClass = isset($attributes['checkbox-class']) ? $attributes['checkbox-class'] : 'checkbox';
  67. if (isset($attributes['label-class'])) {
  68. unset($attributes['label-class']);
  69. }
  70. if (isset($attributes['checkbox-class'])) {
  71. unset($attributes['checkbox-class']);
  72. }
  73. parent::__construct($elementName, $elementLabel, $attributes);
  74. $this->_persistantFreeze = true;
  75. $this->_text = $text;
  76. $this->setType('checkbox');
  77. if (!isset($attributes['value'])) {
  78. $this->updateAttributes(array('value' => 1));
  79. } else {
  80. $this->updateAttributes(array('value' => $attributes['value']));
  81. }
  82. $this->_generateId();
  83. }
  84. /**
  85. * Sets whether a checkbox is checked
  86. *
  87. * @param bool $checked Whether the field is checked or not
  88. * @since 1.0
  89. * @access public
  90. * @return void
  91. */
  92. function setChecked($checked)
  93. {
  94. if (!$checked) {
  95. $this->removeAttribute('checked');
  96. } else {
  97. $this->updateAttributes(array('checked' => 'checked'));
  98. }
  99. } //end func setChecked
  100. // }}}
  101. // {{{ getChecked()
  102. /**
  103. * Returns whether a checkbox is checked
  104. *
  105. * @since 1.0
  106. * @access public
  107. * @return bool
  108. */
  109. function getChecked()
  110. {
  111. return (bool)$this->getAttribute('checked');
  112. } //end func getChecked
  113. // }}}
  114. // {{{ toHtml()
  115. /**
  116. * Returns the checkbox element in HTML
  117. *
  118. * @since 1.0
  119. * @access public
  120. * @return string
  121. */
  122. public function toHtml()
  123. {
  124. if (0 == strlen($this->_text)) {
  125. $label = '';
  126. } elseif ($this->_flagFrozen) {
  127. $label = $this->_text;
  128. } else {
  129. $labelClass = $this->labelClass;
  130. $checkClass = $this->checkboxClass;
  131. $name = $this->_attributes['name'];
  132. $label ='<div id="'.$name.'" class="'.$checkClass.'">
  133. <label class="'.$labelClass.'">' .
  134. HTML_QuickForm_input::toHtml().' '.$this->_text.
  135. '</label>
  136. </div>
  137. ';
  138. return $label;
  139. }
  140. return HTML_QuickForm_input::toHtml() . $label;
  141. }
  142. /**
  143. * @param string $layout
  144. *
  145. * @return string
  146. */
  147. public function getTemplate($layout)
  148. {
  149. $size = $this->calculateSize();
  150. switch ($layout) {
  151. case FormValidator::LAYOUT_INLINE:
  152. return '
  153. <div class="input-group">
  154. <label {label-for} >
  155. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  156. {label}
  157. </label>
  158. </div>
  159. <div class="input-group {error_class}">
  160. {element}
  161. </div>
  162. ';
  163. break;
  164. case FormValidator::LAYOUT_HORIZONTAL:
  165. return '
  166. <div class="form-group row {error_class}">
  167. <label {label-for} class="col-sm-'.$size[0].' col-form-label {extra_label_class}" >
  168. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  169. {label}
  170. </label>
  171. <div class="col-sm-'.$size[1].'">
  172. {icon}
  173. {element}
  174. <!-- BEGIN label_2 -->
  175. <p class="help-block">{label_2}</p>
  176. <!-- END label_2 -->
  177. <!-- BEGIN error -->
  178. <span class="help-inline help-block">{error}</span>
  179. <!-- END error -->
  180. </div>
  181. <div class="col-sm-'.$size[2].'">
  182. <!-- BEGIN label_3 -->
  183. {label_3}
  184. <!-- END label_3 -->
  185. </div>
  186. </div>';
  187. break;
  188. case FormValidator::LAYOUT_BOX_NO_LABEL:
  189. return '
  190. <div class="input-group">
  191. {icon}
  192. {element}
  193. </div>';
  194. break;
  195. }
  196. }
  197. /**
  198. * Returns the value of field without HTML tags
  199. *
  200. * @since 1.0
  201. * @access public
  202. * @return string
  203. */
  204. function getFrozenHtml()
  205. {
  206. if ($this->getChecked()) {
  207. return '<code>[x]</code>' .
  208. $this->_getPersistantData();
  209. } else {
  210. return '<code>[ ]</code>';
  211. }
  212. } //end func getFrozenHtml
  213. // }}}
  214. // {{{ setText()
  215. /**
  216. * Sets the checkbox text
  217. *
  218. * @param string $text
  219. * @since 1.1
  220. * @access public
  221. * @return void
  222. */
  223. function setText($text)
  224. {
  225. $this->_text = $text;
  226. } //end func setText
  227. // }}}
  228. // {{{ getText()
  229. /**
  230. * Returns the checkbox text
  231. *
  232. * @since 1.1
  233. * @access public
  234. * @return string
  235. */
  236. function getText()
  237. {
  238. return $this->_text;
  239. } //end func getText
  240. // }}}
  241. // {{{ setValue()
  242. /**
  243. * Sets the value of the form element
  244. *
  245. * @param string $value Default value of the form element
  246. * @since 1.0
  247. * @access public
  248. * @return void
  249. */
  250. function setValue($value)
  251. {
  252. return $this->setChecked($value);
  253. } // end func setValue
  254. // }}}
  255. // {{{ getValue()
  256. /**
  257. * Returns the value of the form element
  258. *
  259. * @since 1.0
  260. * @access public
  261. * @return bool
  262. */
  263. function getValue()
  264. {
  265. return $this->getChecked();
  266. } // end func getValue
  267. // }}}
  268. // {{{ onQuickFormEvent()
  269. /**
  270. * Called by HTML_QuickForm whenever form event is made on this element
  271. *
  272. * @param string $event Name of event
  273. * @param mixed $arg event arguments
  274. * @param object &$caller calling object
  275. * @since 1.0
  276. * @access public
  277. * @return void
  278. */
  279. function onQuickFormEvent($event, $arg, &$caller)
  280. {
  281. switch ($event) {
  282. case 'updateValue':
  283. // constant values override both default and submitted ones
  284. // default values are overriden by submitted
  285. $value = $this->_findValue($caller->_constantValues);
  286. if (null === $value) {
  287. // if no boxes were checked, then there is no value in the array
  288. // yet we don't want to display default value in this case
  289. if ($caller->isSubmitted()) {
  290. $value = $this->_findValue($caller->_submitValues);
  291. } else {
  292. $value = $this->_findValue($caller->_defaultValues);
  293. }
  294. }
  295. if (null !== $value || $caller->isSubmitted()) {
  296. $this->setChecked($value);
  297. }
  298. break;
  299. case 'setGroupValue':
  300. $this->setChecked($arg);
  301. break;
  302. default:
  303. parent::onQuickFormEvent($event, $arg, $caller);
  304. }
  305. return true;
  306. } // end func onQuickFormEvent
  307. // }}}
  308. // {{{ exportValue()
  309. /**
  310. * Return true if the checkbox is checked, null if it is not checked (getValue() returns false)
  311. */
  312. function exportValue(&$submitValues, $assoc = false)
  313. {
  314. $value = $this->_findValue($submitValues);
  315. if (null === $value) {
  316. $value = $this->getChecked()? true: null;
  317. }
  318. return $this->_prepareValue($value, $assoc);
  319. }
  320. }