Source for file WCheckBox.class.php

Documentation is available at WCheckBox.class.php

  1. <?php
  2. /**
  3. * @since Jul 21, 2005
  4. * @package polyphony.wizard.components
  5. *
  6. * @copyright Copyright &copy; 2005, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: WCheckBox.class.php,v 1.14 2007/09/19 14:04:51 adamfranco Exp $
  10. */
  11.  
  12. require_once(POLYPHONY.'/main/library/Wizard/WizardComponent.abstract.php');
  13.  
  14. /**
  15. * This class allows for the creation of a input type='checkbox' element.
  16. *
  17. * @since Jul 21, 2005
  18. * @package polyphony.wizard.components
  19. *
  20. * @copyright Copyright &copy; 2005, Middlebury College
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  22. *
  23. * @version $Id: WCheckBox.class.php,v 1.14 2007/09/19 14:04:51 adamfranco Exp $
  24. */
  25. class WCheckBox
  26. extends WizardComponent
  27. {
  28.  
  29. var $_value;
  30. var $_style = null;
  31. var $_label = '';
  32. /**
  33. * Virtual Constructor
  34. * @param string $label
  35. * @access public
  36. * @return ref object
  37. * @static
  38. */
  39. function withLabel ($label) {
  40. $obj = new WCheckBox();
  41. $obj->_label = $label;
  42. return $obj;
  43. }
  44. /**
  45. * Constructor
  46. * @access public
  47. * @return WCheckBox
  48. */
  49. function WCheckBox () {
  50. $this->_value = false;
  51. }
  52. /**
  53. * Sets the CSS style of the label for this element.
  54. * @param string $style
  55. * @access public
  56. * @return void
  57. */
  58. function setStyle ($style) {
  59. $this->_style = $style;
  60. }
  61. /**
  62. * Sets if this checkbox should be checked or not as a default value.
  63. * @param boolean $checked
  64. * @access public
  65. * @return void
  66. */
  67. function setValue ($checked) {
  68. $this->_value = $checked;
  69. }
  70.  
  71. /**
  72. * Sets if this checkbox should be checked or not as a default value.
  73. * @param boolean $checked
  74. * @access public
  75. * @return void
  76. */
  77. function setChecked ($checked) {
  78. $this->setValue($checked);
  79. }
  80. /**
  81. * Sets the label for this checkbox element.
  82. * @param string $label;
  83. * @access public
  84. * @return void
  85. */
  86. function setLabel ($label) {
  87. $this->_label = $label;
  88. }
  89. /**
  90. * Tells the wizard component to update itself - this may include getting
  91. * form post data or validation - whatever this particular component wants to
  92. * do every pageload.
  93. * @param string $fieldName The field name to use when outputting form data or
  94. * similar parameters/information.
  95. * @access public
  96. * @return boolean - TRUE if everything is OK
  97. */
  98. function update ($fieldName) {
  99. $val = RequestContext::value($fieldName);
  100. if ($val == '1') $this->_value = true;
  101. else if ($val == '0') $this->_value = false;
  102. }
  103. /**
  104. * Returns the values of wizard-components. Should return an array if children are involved,
  105. * otherwise a whatever type of object is expected.
  106. *
  107. * In this case, a "1" or a "0" is returned, depending on the checked state of the checkbox.
  108. * @access public
  109. * @return mixed
  110. */
  111. function getAllValues () {
  112. return $this->_value?"1":"0";
  113. }
  114. /**
  115. * Add a confirmation question that will be present in a javascript 'confirm'
  116. * dialog onchange press.
  117. *
  118. * @param string $confirmText
  119. * @return void
  120. * @access public
  121. * @since 6/14/06
  122. */
  123. function addConfirm ($confirmText) {
  124. if (!isset($this->_confirms))
  125. $this->_confirms = array();
  126. $this->_confirms[] = $confirmText;
  127. }
  128. /**
  129. * Returns a block of XHTML-valid code that contains markup for this specific
  130. * component.
  131. * @param string $fieldName The field name to use when outputting form data or
  132. * similar parameters/information.
  133. * @access public
  134. * @return string
  135. */
  136. function getMarkup ($fieldName) {
  137. $name = RequestContext::name($fieldName);
  138. $dummyName = $fieldName . "_dummy";
  139. $val = $this->_value?"1":"0";
  140. $checked = $this->_value?" checked='checked'":"";
  141. $style = " style='cursor: pointer;'";
  142. if ($this->_style) $style = " style=\"cursor: pointer; ".htmlspecialchars($this->_style)."\"";
  143. $m = "\n\t\t\t<input type='hidden' \n\t\t\t\tname='$name' \n\t\t\t\tid='$fieldName' \n\t\t\t\tvalue='$val' />";
  144. $m .= "\n\t\t\t<input type='checkbox' ";
  145. if (!$this->isEnabled())
  146. $m .= "\n\t\t\t\tdisabled=\"disabled\"";
  147. else {
  148. $m .= "\n\t\t\t\tonclick=\"";
  149. if (isset($this->_confirms) && count($this->_confirms)) {
  150. $m .= "var confirmed = (confirm('";
  151. $m .= implode("') && confirm('", $this->_confirms);
  152. $m .= "'));";
  153. } else {
  154. $m .= "var confirmed = true; ";
  155. }
  156. $m .= " if (confirmed) { ";
  157. $m .= $this->getSetJS($fieldName);
  158. $m .= " } else { ";
  159. $m .= " this.checked = ".($this->_value?"true":"false").";";
  160. $m .= " }\"";
  161. }
  162. $m .= "\n\t\t\t\tid='$dummyName'$checked />";
  163. $m .= "\n\t\t\t<label$style ";
  164. if ($this->isEnabled()) {
  165. $m .= "\n\t\t\t\tonclick=\"";
  166. if (isset($this->_confirms) && count($this->_confirms)) {
  167. $m .= "var confirmed = (confirm('";
  168. $m .= implode("') && confirm('", $this->_confirms);
  169. $m .= "'));";
  170. } else {
  171. $m .= "var confirmed = true; ";
  172. }
  173. $m .= " if (confirmed) { ";
  174. $m .= $this->getToggleJS($fieldName);
  175. $m .= " }\" ";
  176. }
  177. $m .= "\n\t\t\t>".$this->_label."</label>";
  178. return $m;
  179. }
  180. /**
  181. * Answer the javascript commands to execute when the checkbox is clicked.
  182. *
  183. * @param string $fieldName
  184. * @return string
  185. * @access public
  186. * @since 10/20/05
  187. */
  188. function getToggleJS ($fieldName) {
  189. $dummyName = $fieldName . "_dummy";
  190. $js = "document.getElementById('$dummyName').checked = (document.getElementById('$dummyName').checked? false : true); ";
  191. $js .= $this->getSetJS($fieldName);
  192. return $js;
  193. }
  194. /**
  195. * Answer the javascript commands to execute when the checkbox is clicked.
  196. *
  197. * @param string $fieldName
  198. * @return string
  199. * @access public
  200. * @since 10/20/05
  201. */
  202. function getSetJS ($fieldName) {
  203. $dummyName = $fieldName . "_dummy";
  204. $js = "document.getElementById('$fieldName').value = (document.getElementById('$dummyName').checked? '1' : '0'); ";
  205. return $js;
  206. }
  207. /**
  208. * Answer the javascript commands to check the checkbox.
  209. *
  210. * @return string
  211. * @access public
  212. * @since 10/20/05
  213. */
  214. function getCheckJS ($fieldName) {
  215. $dummyName = $fieldName . "_dummy";
  216. $js = "document.getElementById('$dummyName').checked = true; ";
  217. $js .= $this->getSetJS($fieldName);
  218. return $js;
  219. }
  220. /**
  221. * Answer the javascript commands to check the checkbox.
  222. *
  223. * @return string
  224. * @access public
  225. * @since 10/20/05
  226. */
  227. function getUncheckJS ($fieldName) {
  228. $dummyName = $fieldName . "_dummy";
  229. $js = "document.getElementById('$dummyName').checked = false; ";
  230. $js .= $this->getSetJS($fieldName);
  231. return $js;
  232. }
  233. }
  234.  
  235. ?>

Documentation generated on Wed, 19 Sep 2007 10:27:53 -0400 by phpDocumentor 1.3.0RC3