PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/fields/class.TimeField.php

https://github.com/reshadf/Library
PHP | 264 lines | 132 code | 28 blank | 104 comment | 29 complexity | f363644a627421abda56b905439131ff MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * class TimeField
  4. *
  5. * Create a new TimeField class
  6. *
  7. * @author Teye Heimans
  8. * @package FormHandler
  9. * @subpackage Fields
  10. */
  11. class TimeField extends Field
  12. {
  13. var $_iFormat; // integer: hour format: {12, 24}
  14. var $_oHour; // SelectField: object of the hour selectfield
  15. var $_oMinute; // SelectField: object of the minute selectfield
  16. var $_bRequired; // boolean: if the field is required or if we have to give the option to leave this field empty
  17. /**
  18. * TimeField::TimeField()
  19. *
  20. * Constructor: create a new TimeField on the given form
  21. *
  22. * @param object $oForm: The form where the field is located on
  23. * @param string $sName: The name of the field
  24. * @return TimeField
  25. * @author Teye Heimans
  26. */
  27. function TimeField( &$oForm, $sName )
  28. {
  29. // set the default hour format
  30. $this->setHourFormat( FH_TIMEFIELD_DEFAULT_HOUR_FORMAT );
  31. // set if the field is required
  32. $this->setRequired( FH_TIMEFIELD_DEFAULT_REQUIRED );
  33. // make the hour and minute fields
  34. $this->_oHour = new SelectField($oForm, $sName.'_hour');
  35. $this->_oMinute = new SelectField($oForm, $sName.'_minute');
  36. parent::Field( $oForm, $sName );
  37. // posted or edit form? Then load the value of the time
  38. if( $oForm->isPosted() || (isset($oForm->edit) && $oForm->edit) )
  39. {
  40. $this->_mValue = $this->_oHour->getValue().':'.$this->_oMinute->getValue();
  41. }
  42. }
  43. /**
  44. * TimeField::setExtra()
  45. *
  46. * Set some extra tag information of the fields
  47. *
  48. * @param string $sExtra: The extra information to inglude with the html tag
  49. * @return void
  50. * @access public
  51. * @author Teye Heimans
  52. */
  53. function setExtra( $sExtra )
  54. {
  55. $this->_oHour->setExtra ( $sExtra );
  56. $this->_oMinute->setExtra( $sExtra );
  57. }
  58. /**
  59. * TimeField::setHourFormat()
  60. *
  61. * Set the hour format (eg. 12 or 24)
  62. *
  63. * @param integer $iFormat: The hour format
  64. * @return void
  65. * @access public
  66. * @author Teye Heimans
  67. */
  68. function setHourFormat( $iFormat )
  69. {
  70. if($iFormat == 12 || $iFormat == 24)
  71. {
  72. $this->_iFormat = $iFormat;
  73. }
  74. else
  75. {
  76. trigger_error(
  77. 'Invalid value as hour format! Only 12 or 24 are allowed!',
  78. E_USER_WARNING
  79. );
  80. }
  81. }
  82. /**
  83. * TimeField::setRequired()
  84. *
  85. * PSet if the timefield is required or if we have to give the user
  86. * the option to select an empty value
  87. *
  88. * @param boolean $bStatus: The status
  89. * @return void
  90. * @access public
  91. * @author Teye Heimans
  92. */
  93. function setRequired( $bStatus )
  94. {
  95. $this->_bRequired = $bStatus;
  96. }
  97. /**
  98. * TimeField::setValue()
  99. *
  100. * Set the value of the field
  101. *
  102. * @param string $sValue: The new value of the field
  103. * @return void
  104. * @access Public
  105. * @author Teye Heimans
  106. */
  107. function setValue( $sValue )
  108. {
  109. if( strpos($sValue,':') !== false)
  110. {
  111. list($sHour, $sMinute) = explode(':', $sValue);
  112. $this->_oHour->setValue ( $sHour );
  113. $this->_oMinute->setValue ( $sMinute );
  114. $this->_mValue = $sValue;
  115. }
  116. // possibility to set "no" value when the field is not required
  117. elseif( (strtolower($sValue )== "null" || empty( $sValue ) ) && !$this->_bRequired )
  118. {
  119. $this->_mValue = "";
  120. }
  121. }
  122. /**
  123. * TimeField::getValue()
  124. *
  125. * Return the current value of the field
  126. *
  127. * @return string: the value of the field
  128. * @access public
  129. * @author Teye Heimans
  130. */
  131. function getValue()
  132. {
  133. if($this->_oHour->getValue() == '' && $this->_oMinute->getValue() == '')
  134. {
  135. return '';
  136. }
  137. else
  138. {
  139. $this->_mValue = $this->_oHour->getValue().':'.$this->_oMinute->getValue();
  140. return $this->_mValue;
  141. }
  142. }
  143. /**
  144. * TimeField::getField()
  145. *
  146. * Return the HTML of the field
  147. *
  148. * @return string: the html of the field
  149. * @access public
  150. * @author Teye Heimans
  151. */
  152. function getField()
  153. {
  154. // view mode enabled ?
  155. if( $this -> getViewMode() )
  156. {
  157. // get the view value..
  158. return $this -> _getViewValue();
  159. }
  160. // set the currect time if wanted
  161. if( !$this->_oForm->isPosted() &&
  162. (!isset($this->_oForm->edit) || !$this->_oForm->edit) &&
  163. $this->_bRequired &&
  164. $this->getValue() == '' &&
  165. FH_TIMEFIELD_SET_CUR_TIME)
  166. {
  167. $this->setValue( date('H').':'.date('i') );
  168. }
  169. // generate the hour options
  170. $aHours = array();
  171. if(!$this->_bRequired)
  172. {
  173. $aHours[''] = '';
  174. }
  175. for($i = 0; $i <= ($this->_iFormat-1); $i++ )
  176. {
  177. $aHours[sprintf('%02d', $i)] = sprintf('%02d', $i);
  178. }
  179. // generate the minutes options
  180. $aMinutes = array();
  181. if(!$this->_bRequired)
  182. {
  183. $aMinutes[''] = '';
  184. }
  185. $i = 0;
  186. while($i <= 59)
  187. {
  188. $aMinutes[sprintf("%02d", $i)] = sprintf("%02d", $i);
  189. $i += FH_TIMEFIELD_MINUTE_STEPS;
  190. }
  191. // set the options
  192. $this->_oHour->setOptions ( $aHours );
  193. $this->_oMinute->setOptions( $aMinutes );
  194. // make sure that the minutes option can be displayed
  195. if( $this -> _bRequired || $this -> getValue() != "" )
  196. {
  197. $this->_oHour->_mValue += $this->_getNearestMinute( $this->_oMinute->_mValue );
  198. if($this->_oHour->_mValue == 24) $this->_oHour->_mValue = 0;
  199. }
  200. //debug
  201. //print_Var( $this -> _mValue, $this->_oHour->_mValue, $this->_oMinute->_mValue );
  202. // return the fields
  203. return
  204. $this->_oHour->getField() . " : " .
  205. $this->_oMinute->getField().
  206. (isset($this->_sExtraAfter) ? $this->_sExtraAfter :'');
  207. }
  208. /**
  209. * TimeField::_getNearestMinute()
  210. *
  211. * Get the nearest minute in the minutes list
  212. *
  213. * @param int $minute
  214. * @return int: 1 or 0 if the hour should be increased
  215. * @access private
  216. * @author Teye Heimans
  217. */
  218. function _getNearestMinute( &$minute )
  219. {
  220. // get the nearest value at the minutes...
  221. for($i = 0; $i < $minute; $i += FH_TIMEFIELD_MINUTE_STEPS);
  222. $i = abs( $minute - $i ) < abs( $minute - ($i - FH_TIMEFIELD_MINUTE_STEPS)) ?
  223. $i : ($i - FH_TIMEFIELD_MINUTE_STEPS);
  224. $minute = $i;
  225. if($minute == 60)
  226. {
  227. $minute = 0;
  228. return 1;
  229. }
  230. else
  231. {
  232. return 0;
  233. }
  234. }
  235. }
  236. ?>