checkbox.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. $checkboxClass = $this->checkboxClass;
  131. $label ='
  132. <div class="'.$checkboxClass.'">
  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->getColumnsSize();
  150. if (empty($size)) {
  151. $size = array(2, 8, 2);
  152. } else {
  153. if (is_array($size)) {
  154. if (count($size) == 1) {
  155. $size = array(2, intval($size[0]), 2);
  156. } elseif (count($size) != 3) {
  157. $size = array(2, 8, 2);
  158. }
  159. // else just keep the $size array as received
  160. } else {
  161. $size = array(2, intval($size), 2);
  162. }
  163. }
  164. switch ($layout) {
  165. case FormValidator::LAYOUT_INLINE:
  166. return '
  167. <div class="input-group">
  168. <label {label-for} >
  169. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  170. {label}
  171. </label>
  172. </div>
  173. <div class="input-group {error_class}">
  174. {element}
  175. </div>
  176. ';
  177. break;
  178. case FormValidator::LAYOUT_HORIZONTAL:
  179. return '
  180. <div class="form-group {error_class}">
  181. <label {label-for} class="col-sm-'.$size[0].' control-label {extra_label_class}" >
  182. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  183. {label}
  184. </label>
  185. <div class="col-sm-'.$size[1].'">
  186. {icon}
  187. {element}
  188. <!-- BEGIN label_2 -->
  189. <p class="help-block">{label_2}</p>
  190. <!-- END label_2 -->
  191. <!-- BEGIN error -->
  192. <span class="help-inline">{error}</span>
  193. <!-- END error -->
  194. </div>
  195. <div class="col-sm-'.$size[2].'">
  196. <!-- BEGIN label_3 -->
  197. {label_3}
  198. <!-- END label_3 -->
  199. </div>
  200. </div>';
  201. break;
  202. case FormValidator::LAYOUT_BOX_NO_LABEL:
  203. return '
  204. <div class="input-group">
  205. {icon}
  206. {element}
  207. </div>';
  208. break;
  209. }
  210. }
  211. /**
  212. * Returns the value of field without HTML tags
  213. *
  214. * @since 1.0
  215. * @access public
  216. * @return string
  217. */
  218. function getFrozenHtml()
  219. {
  220. if ($this->getChecked()) {
  221. return '<code>[x]</code>' .
  222. $this->_getPersistantData();
  223. } else {
  224. return '<code>[ ]</code>';
  225. }
  226. } //end func getFrozenHtml
  227. // }}}
  228. // {{{ setText()
  229. /**
  230. * Sets the checkbox text
  231. *
  232. * @param string $text
  233. * @since 1.1
  234. * @access public
  235. * @return void
  236. */
  237. function setText($text)
  238. {
  239. $this->_text = $text;
  240. } //end func setText
  241. // }}}
  242. // {{{ getText()
  243. /**
  244. * Returns the checkbox text
  245. *
  246. * @since 1.1
  247. * @access public
  248. * @return string
  249. */
  250. function getText()
  251. {
  252. return $this->_text;
  253. } //end func getText
  254. // }}}
  255. // {{{ setValue()
  256. /**
  257. * Sets the value of the form element
  258. *
  259. * @param string $value Default value of the form element
  260. * @since 1.0
  261. * @access public
  262. * @return void
  263. */
  264. function setValue($value)
  265. {
  266. return $this->setChecked($value);
  267. } // end func setValue
  268. // }}}
  269. // {{{ getValue()
  270. /**
  271. * Returns the value of the form element
  272. *
  273. * @since 1.0
  274. * @access public
  275. * @return bool
  276. */
  277. function getValue()
  278. {
  279. return $this->getChecked();
  280. } // end func getValue
  281. // }}}
  282. // {{{ onQuickFormEvent()
  283. /**
  284. * Called by HTML_QuickForm whenever form event is made on this element
  285. *
  286. * @param string $event Name of event
  287. * @param mixed $arg event arguments
  288. * @param object &$caller calling object
  289. * @since 1.0
  290. * @access public
  291. * @return void
  292. */
  293. function onQuickFormEvent($event, $arg, &$caller)
  294. {
  295. switch ($event) {
  296. case 'updateValue':
  297. // constant values override both default and submitted ones
  298. // default values are overriden by submitted
  299. $value = $this->_findValue($caller->_constantValues);
  300. if (null === $value) {
  301. // if no boxes were checked, then there is no value in the array
  302. // yet we don't want to display default value in this case
  303. if ($caller->isSubmitted()) {
  304. $value = $this->_findValue($caller->_submitValues);
  305. } else {
  306. $value = $this->_findValue($caller->_defaultValues);
  307. }
  308. }
  309. if (null !== $value || $caller->isSubmitted()) {
  310. $this->setChecked($value);
  311. }
  312. break;
  313. case 'setGroupValue':
  314. $this->setChecked($arg);
  315. break;
  316. default:
  317. parent::onQuickFormEvent($event, $arg, $caller);
  318. }
  319. return true;
  320. } // end func onQuickFormEvent
  321. // }}}
  322. // {{{ exportValue()
  323. /**
  324. * Return true if the checkbox is checked, null if it is not checked (getValue() returns false)
  325. */
  326. function exportValue(&$submitValues, $assoc = false)
  327. {
  328. $value = $this->_findValue($submitValues);
  329. if (null === $value) {
  330. $value = $this->getChecked()? true: null;
  331. }
  332. return $this->_prepareValue($value, $assoc);
  333. }
  334. }