PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/fields/class.CheckBox.php

https://github.com/reshadf/Library
PHP | 249 lines | 133 code | 25 blank | 91 comment | 23 complexity | 1f8153c91e0c6941a550479d86c439ea MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * class CheckBox
  4. *
  5. * Create a checkbox on the given form object
  6. *
  7. * @author Teye Heimans
  8. * @package FormHandler
  9. * @subpackage Fields
  10. */
  11. class CheckBox extends Field
  12. {
  13. var $_aOptions; // array: contains all the options!
  14. // $this->_mValue contains the values which are selected!
  15. var $_bUseArrayKeyAsValue; // boolean: if the keys of the array should be used as values
  16. var $_sMask; // string: what kind of "glue" should be used to merge the checkboxes
  17. var $_oLoader; // object: The maskLoader
  18. /**
  19. * CheckBox::CheckBox()
  20. *
  21. * Constructor: Create a new checkbox object
  22. *
  23. * @param object $oForm: The form where this field is located on
  24. * @param string $sName: The name of the field
  25. * @param mixed: array|string $aOptions - The options for the field
  26. * @return CheckBox
  27. * @access public
  28. * @author Teye Heimans
  29. */
  30. function CheckBox( &$oForm, $sName, $aOptions )
  31. {
  32. $this->_mValue = '';
  33. $sName = str_replace('[]','', $sName);
  34. $this->_aOptions = $aOptions;
  35. // call the constructor of the Field class
  36. parent::Field( $oForm, $sName );
  37. $this->setMask ( FH_DEFAULT_GLUE_MASK );
  38. $this->useArrayKeyAsValue( FH_DEFAULT_USEARRAYKEY );
  39. }
  40. /**
  41. * CheckBox::setValue()
  42. *
  43. * Set the value of the field
  44. *
  45. * @param string / array $mValue: the value to set
  46. * @return void
  47. * @access public
  48. * @author Teye Heimans
  49. */
  50. function setValue( $aValue )
  51. {
  52. // make an array from the value
  53. if( !is_array($aValue) && is_array($this->_aOptions) )
  54. {
  55. $aValue = explode(',', $aValue);
  56. foreach($aValue as $iKey => $sValue)
  57. {
  58. $sValue = trim($sValue);
  59. // dont save an empty value when it does not exists in the
  60. // options array!
  61. if( !empty($sValue) ||
  62. ((is_array($this->_aOptions) &&
  63. ( in_array( $sValue, $this->_aOptions ) ||
  64. array_key_exists( $sValue, $this->_aOptions )
  65. )
  66. ) ||
  67. $sValue == $this->_aOptions ))
  68. {
  69. $aValue[$iKey] = $sValue;
  70. }
  71. else
  72. {
  73. unset( $aValue[$iKey] );
  74. }
  75. }
  76. }
  77. $this->_mValue = $aValue;
  78. }
  79. /**
  80. * CheckBox::useArrayKeyAsValue()
  81. *
  82. * Set if the array keys of the options has to be used as values for the field
  83. *
  84. * @param boolean $bMode
  85. * @return void
  86. * @access public
  87. * @author Teye Heimans
  88. */
  89. function useArrayKeyAsValue( $bMode )
  90. {
  91. $this->_bUseArrayKeyAsValue = $bMode;
  92. }
  93. /**
  94. * CheckBox::setMask()
  95. *
  96. * Set the glue used to glue multiple checkboxes. This can be a mask
  97. * where %field% is replaced with a checkbox!
  98. *
  99. * @param string $sMask
  100. * @return void
  101. * @author Teye Heimans
  102. * @access Public
  103. */
  104. function setMask( $sMask )
  105. {
  106. // when there is no %field% used, put it in front of the mask/glue
  107. if( strpos( $sMask, '%field%' ) === false )
  108. {
  109. $sMask = '%field%' . $sMask;
  110. }
  111. $this->_sMask = $sMask;
  112. }
  113. /**
  114. * CheckBox::getField()
  115. *
  116. * Return the HTML of the field
  117. *
  118. * @return string: the html of the field
  119. * @access Public
  120. * @author Teye Heimans
  121. */
  122. function getField()
  123. {
  124. // view mode enabled ?
  125. if( $this -> getViewMode() )
  126. {
  127. // get the view value..
  128. return $this -> _getViewValue();
  129. }
  130. // multiple checkboxes ?
  131. if( is_array( $this->_aOptions ) && count( $this->_aOptions )>0 )
  132. {
  133. $sResult = '';
  134. // get the checkboxes
  135. foreach( $this->_aOptions as $iKey => $sValue )
  136. {
  137. // use the array key as value?
  138. if(!$this->_bUseArrayKeyAsValue)
  139. {
  140. $iKey = $sValue;
  141. }
  142. // get the checbox
  143. $sResult .= $this->_getCheckBox( $iKey, $sValue, true );
  144. }
  145. // get a possible half filled mask
  146. $sResult .= $this -> _oLoader -> fill();
  147. }
  148. elseif( is_array( $this->_aOptions ) && count( $this->_aOptions ) === 0 )
  149. {
  150. $sResult = '';
  151. }
  152. // just 1 checkbox...
  153. else
  154. {
  155. $sResult = $this->_getCheckBox( $this->_aOptions, '' );
  156. }
  157. return $sResult.
  158. (isset($this->_sExtraAfter) ? $this->_sExtraAfter :'');
  159. }
  160. /**
  161. * CheckBox::_getCheckBox()
  162. *
  163. * Return an option of the checkbox with the given value
  164. *
  165. * @param string $sValue: the value for the checkbox
  166. * @param string $sTitle: the title for the checkbox
  167. * @param bool $bUseMask: do we have to use the mask after the field?
  168. * @return string: the HTML for the checkbox
  169. * @access private
  170. * @author Teye Heimans
  171. */
  172. function _getCheckBox( $sValue, $sTitle, $bUseMask = false )
  173. {
  174. static $iCounter = 1;
  175. // create a MaskLoader object when it does not exists yet
  176. if( !isset( $this->_oLoader ) || is_null( $this->_oLoader ) )
  177. {
  178. $this -> _oLoader = new MaskLoader();
  179. $this -> _oLoader -> setMask( $this->_sMask );
  180. $this -> _oLoader -> setSearch( '/%field%/' );
  181. }
  182. // remove unwanted spaces
  183. $sValue = trim( $sValue );
  184. $sTitle = trim( $sTitle );
  185. // get the field HTML
  186. if( $sTitle == '' )
  187. {
  188. $sField = sprintf(
  189. '<input type="checkbox" name="%s" id="%s_%d" value="%s" %s'. FH_XHTML_CLOSE .'>',
  190. $this->_sName.(is_array($this->_aOptions)?'[]':''),
  191. $this->_sName,
  192. $iCounter++,
  193. htmlspecialchars($sValue),
  194. (isset($this->_iTabIndex) ? 'tabindex="'.$this->_iTabIndex.'" ' : '').
  195. ((isset($this->_mValue) && ((is_array($this->_mValue) && in_array($sValue, $this->_mValue)) || $sValue == $this->_mValue) ) ?
  196. 'checked="checked" ':'').
  197. (isset($this->_sExtra) ? $this->_sExtra.' ':''),
  198. $sTitle
  199. );
  200. }
  201. else
  202. {
  203. $sField = sprintf(
  204. '<input type="checkbox" name="%s" id="%s_%d" value="%s" %s'. FH_XHTML_CLOSE .'><label for="%2$s_%3$d" class="noStyle">%s</label>',
  205. $this->_sName.(is_array($this->_aOptions)?'[]':''),
  206. $this->_sName,
  207. $iCounter++,
  208. htmlspecialchars($sValue),
  209. (isset($this->_iTabIndex) ? 'tabindex="'.$this->_iTabIndex.'" ' : '').
  210. ((isset($this->_mValue) && ((is_array($this->_mValue) && in_array($sValue, $this->_mValue)) || $sValue == $this->_mValue) ) ?
  211. 'checked="checked" ':'').
  212. (isset($this->_sExtra) ? $this->_sExtra.' ':''),
  213. $sTitle
  214. );
  215. }
  216. // do we have to use the mask ?
  217. if( $bUseMask )
  218. {
  219. $sField = $this -> _oLoader -> fill( $sField );
  220. }
  221. return $sField;
  222. }
  223. }
  224. ?>