PageRenderTime 65ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/fields/class.RadioButton.php

https://github.com/reshadf/Library
PHP | 176 lines | 87 code | 21 blank | 68 comment | 12 complexity | 3d90690ffc7bd1c394a8305640cbb3f9 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * class RadioButton
  4. *
  5. * Create a RadioButton
  6. *
  7. * @author Teye Heimans
  8. * @package FormHandler
  9. * @subpackage Fields
  10. */
  11. class RadioButton extends Field
  12. {
  13. var $_aOptions; // string: the value with is selected
  14. var $_bUseArrayKeyAsValue; // boolean: if the keys of the array should be used as values
  15. var $_sMask; // string: what kind of "glue" should be used to merge the fields
  16. var $_oLoader; // object: a maskloader object
  17. /**
  18. * RadioButton::RadioButton()
  19. *
  20. * Constructor: Create a new radiobutton object
  21. *
  22. * @param object $oForm: The form where this field is located on
  23. * @param string $sName: The name of the field
  24. * @param array|string $aOptions: The options for the field
  25. * @return RadioButton
  26. * @author Teye Heimans
  27. */
  28. function RadioButton( &$oForm, $sName, $aOptions )
  29. {
  30. // call the constructor of the Field class
  31. parent::Field( $oForm, $sName );
  32. $this->_aOptions = $aOptions;
  33. $this->setMask ( FH_DEFAULT_GLUE_MASK );
  34. $this->useArrayKeyAsValue( FH_DEFAULT_USEARRAYKEY );
  35. }
  36. /**
  37. * RadioButton::useArrayKeyAsValue()
  38. *
  39. * Set if the array keys of the options has to be used as values for the field
  40. *
  41. * @param boolean $bMode: The mode
  42. * @return void
  43. * @access public
  44. * @author Teye Heimans
  45. */
  46. function useArrayKeyAsValue( $bMode )
  47. {
  48. $this->_bUseArrayKeyAsValue = $bMode;
  49. }
  50. /**
  51. * RadioButton::setMask()
  52. *
  53. * Set the "glue" used to glue multiple radiobuttons
  54. *
  55. * @param string $sMask
  56. * @return void
  57. * @author Teye Heimans
  58. * @access public
  59. */
  60. function setMask( $sMask )
  61. {
  62. // when there is no %field% used, put it in front of the mask/glue
  63. if( strpos( $sMask, '%field%' ) === false )
  64. {
  65. $sMask = '%field%' . $sMask;
  66. }
  67. $this->_sMask = $sMask;
  68. }
  69. /**
  70. * RadioButton::getField()
  71. *
  72. * Return the HTML of the field
  73. *
  74. * @return string: the html of the field
  75. * @access Public
  76. * @author Teye Heimans
  77. */
  78. function getField()
  79. {
  80. // view mode enabled ?
  81. if( $this -> getViewMode() )
  82. {
  83. // get the view value..
  84. return $this -> _getViewValue();
  85. }
  86. if( is_array( $this->_aOptions ) && count( $this->_aOptions )>0 )
  87. {
  88. $sResult = '';
  89. foreach( $this->_aOptions as $iKey => $sValue )
  90. {
  91. if(!$this->_bUseArrayKeyAsValue)
  92. {
  93. $iKey = $sValue;
  94. }
  95. $sResult .= $this->_getRadioButton( $iKey, $sValue, true );
  96. }
  97. }
  98. elseif( $this->_aOptions == '' || count( $this->_aOptions )===0 )
  99. {
  100. $sResult = ' ';
  101. }
  102. else
  103. {
  104. $sResult = $this->_getRadioButton( $this->_aOptions, '' );
  105. }
  106. // when we still got nothing, the mask is not filled yet.
  107. // get the mask anyway
  108. if( empty( $sResult ) )
  109. {
  110. $sResult = $this -> _oLoader -> fill();
  111. }
  112. return $sResult . (isset($this->_sExtraAfter) ? $this->_sExtraAfter :'');
  113. }
  114. /**
  115. * RadioButton::_getRadioButton()
  116. *
  117. * Return the radiobutton with the given title and value
  118. *
  119. * @param string $sValue: the value for the checkbox
  120. * @param string $sTitle: the title for the checkbox
  121. * @param bool $bUseMask: Do we need to use the mask ?
  122. * @return string: the HTML for the checkbox
  123. * @access Private
  124. * @author Teye Heimans
  125. */
  126. function _getRadioButton( $sValue, $sTitle, $bUseMask = false )
  127. {
  128. static $counter = 1;
  129. $sValue = trim( $sValue );
  130. $sTitle = trim( $sTitle );
  131. if( !isset( $this -> _oLoader ) ||is_null( $this -> _oLoader ) )
  132. {
  133. $this -> _oLoader = new MaskLoader();
  134. $this -> _oLoader -> setMask( $this->_sMask );
  135. $this -> _oLoader -> setSearch( '/%field%/' );
  136. }
  137. $sField = sprintf(
  138. '<input type="radio" name="%s" id="%1$s_%d" value="%s" %s'. FH_XHTML_CLOSE .'><label for="%1$s_%2$d" class="noStyle">%s</label>',
  139. $this->_sName,
  140. $counter++,
  141. htmlspecialchars($sValue),
  142. (isset($this->_mValue) && $sValue == $this->_mValue ? 'checked="checked" ':'').
  143. (isset($this->_iTabIndex) ? 'tabindex="'.$this->_iTabIndex.'" ' : '').
  144. (!empty($this->_sExtra) ? $this->_sExtra.' ':''),
  145. $sTitle
  146. );
  147. // do we have to use the mask ?
  148. if( $bUseMask )
  149. {
  150. $sField = $this -> _oLoader -> fill( $sField );
  151. }
  152. return $sField;
  153. }
  154. }
  155. ?>