Library /lib/fields/class.TimeField.php

Language PHP Lines 264
MD5 Hash f363644a627421abda56b905439131ff
Repository https://github.com/reshadf/Library.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php

/**
 * class TimeField
 *
 * Create a new TimeField class
 *
 * @author Teye Heimans
 * @package FormHandler
 * @subpackage Fields
 */
class TimeField extends Field
{
    var $_iFormat;   // integer: hour format: {12, 24}
    var $_oHour;     // SelectField: object of the hour selectfield
    var $_oMinute;   // SelectField: object of the minute selectfield
    var $_bRequired; // boolean: if the field is required or if we have to give the option to leave this field empty

    /**
     * TimeField::TimeField()
     *
     * Constructor: create a new TimeField on the given form
     *
     * @param object $oForm: The form where the field is located on
     * @param string $sName: The name of the field
     * @return TimeField
     * @author Teye Heimans
     */
    function TimeField( &$oForm, $sName )
    {
        // set the default hour format
        $this->setHourFormat( FH_TIMEFIELD_DEFAULT_HOUR_FORMAT );

        // set if the field is required
        $this->setRequired( FH_TIMEFIELD_DEFAULT_REQUIRED );

        // make the hour and minute fields
        $this->_oHour   = new SelectField($oForm, $sName.'_hour');
        $this->_oMinute = new SelectField($oForm, $sName.'_minute');

        parent::Field( $oForm, $sName );

        // posted or edit form? Then load the value of the time
        if( $oForm->isPosted() || (isset($oForm->edit) && $oForm->edit) )
        {
            $this->_mValue = $this->_oHour->getValue().':'.$this->_oMinute->getValue();
        }
    }

    /**
     * TimeField::setExtra()
     *
     * Set some extra tag information of the fields
     *
     * @param string $sExtra: The extra information to inglude with the html tag
     * @return void
     * @access public
     * @author Teye Heimans
     */
    function setExtra( $sExtra )
    {
    	$this->_oHour->setExtra  ( $sExtra );
    	$this->_oMinute->setExtra( $sExtra );
    }

    /**
     * TimeField::setHourFormat()
     *
     * Set the hour format (eg. 12 or 24)
     *
     * @param integer $iFormat: The hour format
     * @return void
     * @access public
     * @author Teye Heimans
     */
    function setHourFormat( $iFormat )
    {
        if($iFormat == 12 || $iFormat == 24)
        {
            $this->_iFormat = $iFormat;
        }
        else
        {
        	trigger_error(
        	  'Invalid value as hour format! Only 12 or 24 are allowed!',
        	  E_USER_WARNING
        	);
        }
    }

    /**
     * TimeField::setRequired()
     *
     * PSet if the timefield is required or if we have to give the user
     * the option to select an empty value
     *
     * @param boolean $bStatus: The status
     * @return void
     * @access public
     * @author Teye Heimans
     */
    function setRequired( $bStatus )
    {
        $this->_bRequired = $bStatus;
    }


    /**
     * TimeField::setValue()
     *
     * Set the value of the field
     *
     * @param string $sValue: The new value of the field
     * @return void
     * @access Public
     * @author Teye Heimans
     */
    function setValue( $sValue )
    {
    	if( strpos($sValue,':') !== false)
    	{
            list($sHour, $sMinute) = explode(':', $sValue);

            $this->_oHour->setValue   ( $sHour );
            $this->_oMinute->setValue ( $sMinute );
            $this->_mValue = $sValue;
        }
        // possibility to set "no" value when the field is not required
        elseif( (strtolower($sValue )== "null" || empty( $sValue ) ) && !$this->_bRequired )
        {
            $this->_mValue = "";
        }
    }

    /**
     * TimeField::getValue()
     *
     * Return the current value of the field
     *
     * @return string: the value of the field
     * @access public
     * @author Teye Heimans
     */
    function getValue()
    {
        if($this->_oHour->getValue() == '' && $this->_oMinute->getValue() == '')
        {
            return '';
        }
        else
        {
        	$this->_mValue = $this->_oHour->getValue().':'.$this->_oMinute->getValue();
            return $this->_mValue;
        }
    }


    /**
     * TimeField::getField()
     *
     * Return the HTML of the field
     *
     * @return string: the html of the field
     * @access public
     * @author Teye Heimans
     */
    function getField()
    {
        // view mode enabled ?
        if( $this -> getViewMode() )
        {
            // get the view value..
            return $this -> _getViewValue();
        }


    	// set the currect time if wanted
        if( !$this->_oForm->isPosted() &&
            (!isset($this->_oForm->edit) || !$this->_oForm->edit) &&
            $this->_bRequired &&
            $this->getValue() == '' &&
            FH_TIMEFIELD_SET_CUR_TIME)
        {
        	$this->setValue( date('H').':'.date('i') );
        }

        // generate the hour options
        $aHours = array();
        if(!$this->_bRequired)
        {
            $aHours[''] = '';
        }
        for($i = 0; $i <= ($this->_iFormat-1); $i++ )
        {
            $aHours[sprintf('%02d', $i)] = sprintf('%02d', $i);
        }

        // generate the minutes options
        $aMinutes = array();
        if(!$this->_bRequired)
        {
            $aMinutes[''] = '';
        }
        $i = 0;
        while($i <= 59)
        {
            $aMinutes[sprintf("%02d", $i)] = sprintf("%02d", $i);
            $i += FH_TIMEFIELD_MINUTE_STEPS;
        }

        // set the options
        $this->_oHour->setOptions  ( $aHours );
        $this->_oMinute->setOptions( $aMinutes );

        // make sure that the minutes option can be displayed
        if( $this -> _bRequired ||  $this -> getValue() != "" )
        {
            $this->_oHour->_mValue += $this->_getNearestMinute( $this->_oMinute->_mValue );
            if($this->_oHour->_mValue == 24) $this->_oHour->_mValue = 0;
        }

        //debug
        //print_Var( $this -> _mValue, $this->_oHour->_mValue, $this->_oMinute->_mValue );

        // return the fields
        return
          $this->_oHour->getField() . " : " .
          $this->_oMinute->getField().
          (isset($this->_sExtraAfter) ? $this->_sExtraAfter :'');
    }

    /**
     * TimeField::_getNearestMinute()
     *
     * Get the nearest minute in the minutes list
     *
     * @param int $minute
     * @return int: 1 or 0 if the hour should be increased
     * @access private
     * @author Teye Heimans
     */
    function _getNearestMinute( &$minute )
    {
        // get the nearest value at the minutes...
    	for($i = 0; $i < $minute; $i += FH_TIMEFIELD_MINUTE_STEPS);

    	$i = abs( $minute - $i ) < abs( $minute - ($i - FH_TIMEFIELD_MINUTE_STEPS)) ?
    	$i : ($i - FH_TIMEFIELD_MINUTE_STEPS);

    	$minute = $i;

    	if($minute == 60)
    	{
    	    $minute = 0;
    	    return 1;
    	}
    	else
    	{
    	    return 0;
    	}
    }
}

?>
Back to Top