Library /lib/fields/class.DateTextField.php

Language PHP Lines 296
MD5 Hash 33ec426ef61d6196e656371f2905b331
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/**
 * class DateTextField
 *
 * Create a DateTextfield
 *
 * @author Thomas Branius
 * @since 16-03-2010
 * @package FormHandler
 * @subpackage Fields
 * 
 *  validators added by Johan Wiegel
 */
class DateTextField extends TextField
{
	var $_sDateMask;				// string: how to display the fields with mask
	var $_sValParseRegExpr;			// string: how to parse the value
	var $_iDayPos;					// int: position of day in regular expression
	var $_iMonthPos;				// int: position of month in regular expression
	var $_iYearPos;					// int: position of year in regular expression
	var $_bParseOtherPresentations;	// bool: try to parse other presentations of dateformat

	/**
     * Constructor: create a new dateTextField object
     *
     * @param object &$oForm: the form where the datefield is located on
     * @param string $sName: the name of the datefield
     * @param string $sMask: the mask which is used to display the fields
     * @param bool $bParseOtherPresentations: try to parse other presentations of dateformat
     * @return dateTextField
     * @access public
     * @author Thomas Branius
     * @since 16-03-2010
     */
	function DateTextField( &$oForm, $sName, $sMask = null, $bParseOtherPresentations = false )
	{
		// set the default date display
		$this->setMask( !is_null( $sMask ) ? $sMask : FH_DATETEXTFIELD_DEFAULT_DISPLAY );

		$this->_bParseOtherPresentations = $bParseOtherPresentations;

		//$this->setValidator(array(&$this, "validate"));

		// call the constructor of the Field class
		parent::TextField($oForm, $sName);
	}

	/**
     * Set the display of the fields
     *
     * @param string $sMast: how we have to display the datefield (day-month-year combination)
     * @return void
     * @access public
     * @author Thomas Branius]
     * @since 16-03-2010
     */
	function setMask( $sMask )
	{
		// validate the mask
		$regex = '/^([dDmMyY])([\.\-\/])([dDmMyY])\2([dDmMyY])$/';

		if (preg_match($regex, $sMask, $data) == 0
		|| strtolower($data[1]) == strtolower($data[3])
		|| strtolower($data[1]) == strtolower($data[4])
		|| strtolower($data[3]) == strtolower($data[4]))
		trigger_error("Invalid mask ['{$sMask}']. Useable chars: d, D, m, M, y, Y, ., -, /", E_USER_ERROR);

		// set postion of day, month and year
		for ($i = 1; $i < 5; $i++)
		{
			if (strtolower($data[$i]) == "d")
			$this->_iDayPos = $i;
			else if (strtolower($data[$i]) == "m")
			$this->_iMonthPos =$i;
			else if (strtolower($data[$i]) == "y")
			$this->_iYearPos = $i;
		}

		$seperator = str_replace(array('/', '.'), array('\/', '\.'), $data[2]);
		$regExDay = '[0-9]{' . ($data[$this->_iDayPos] == 'D' ? '1,2' : '2' ) . '}';
		$regExMonth = '[0-9]{' . ($data[$this->_iMonthPos] == 'M' ? '1,2' : '2' ) . '}';
		$regExYear = '[0-9]{' . ($data[$this->_iYearPos] == 'y' ? '2' : '4' ) . '}';

		$this->_iDayPos = $this->_iDayPos > 1 ? $this->_iDayPos - 1 : $this->_iDayPos;
		$this->_iMonthPos = $this->_iMonthPos > 1 ? $this->_iMonthPos - 1 : $this->_iMonthPos;
		$this->_iYearPos = $this->_iYearPos > 1 ? $this->_iYearPos - 1 : $this->_iYearPos;

		if ($this->_iYearPos == 1)
		{
			if ($this->_iDayPos == 2)
			$this->_sValParseRegExpr = "/^({$regExYear}){$seperator}({$regExDay}){$seperator}({$regExMonth})$/";
			else
			$this->_sValParseRegExpr = "/^({$regExYear}){$seperator}({$regExMonth}){$seperator}({$regExDay})$/";
		}
		else if ($this->_iYearPos == 2)
		{
			if ($this->_iDayPos == 1)
			$this->_sValParseRegExpr = "/^({$regExDay}){$seperator}({$regExYear}){$seperator}({$regExMonth})$/";
			else
			$this->_sValParseRegExpr = "/^({$regExMonth}){$seperator}({$regExYear}){$seperator}({$regExDay})$/";
		}
		else if ($this->_iYearPos == 3)
		{
			if ($this->_iDayPos == 1)
			$this->_sValParseRegExpr = "/^({$regExDay}){$seperator}({$regExMonth}){$seperator}({$regExYear})$/";
			else
			$this->_sValParseRegExpr = "/^({$regExMonth}){$seperator}({$regExDay}){$seperator}({$regExYear})$/";
		}


		// mask for date-function
		$this->_sDateMask = str_replace(array("D", "M"), array("j", "n"), $sMask);
	}

	/**
     * Get the date value as an array: array(y,m,d)
     *
     * @return array
     * @access public
     * @author Thomas Branius
     * @since 16-03-2010
     */
	function getAsArray()
	{
		if ($this->getValue() == "")
		{
			return array("", "", ""); 
		}
		if (preg_match($this->_sValParseRegExpr, $this->getValue(), $data) == 0)
		trigger_error("Value is not a valid date [" . $this->getValue() . "]", E_USER_ERROR);
		if ($data[$this->_iYearPos] <= 50)
		$data[$this->_iYearPos] = $data[$this->_iYearPos] + 2000;
		if ($data[$this->_iYearPos] <= 100)
		$data[$this->_iYearPos] = $data[$this->_iYearPos] + 1900;

		return array( $data[$this->_iYearPos], $data[$this->_iMonthPos],  $data[$this->_iDayPos]);
	}

	/**
     * Return the value of the field
     *
     * @return mixed: the value of the field
     * @access public
     * @author Thomas Branius
     * @since 16-03-2010
     */
	function getValue()
	{
		$sValue = parent::getValue();

		if (preg_match($this->_sValParseRegExpr, $sValue))
		return $sValue;

		if ($this->_bParseOtherPresentations)
		$sValue = $this->parseOtherPresentations($sValue);

		return $sValue;
	}

	/**
     * Set the value of the field
     *
     * @param mixed $mValue: The new value for the field
     * @return void
     * @access public
     * @author Thomas Branius
     * @since 16-03-2010
     */
	function setValue( $mValue )
	{
		if ($this->_oForm->isPosted())
		return parent::setValue($mValue);

		// parse value from db
		$regex = '/([0-9]{4})-([0-9]{2})-([0-9]{2})/';

		if (preg_match("/0000-00-00/", $mValue))
		{
			$this->_mValue = null;
		}
		else if (preg_match($regex, $mValue, $data))
		{
			$timestamp = mktime(0, 0, 0, $data[2], $data[3], $data[1]);
			$this->_mValue = date($this->_sDateMask, $timestamp);
		}
		else
		{
			$this->_mValue = $mValue;
		}
	}

	/**
     T* try to parse other presentations of dateformat
     *
     * @return mixed: the value of the field
     * @access public
     * @author Thomas Branius
     * @since 16-03-2010
     */
	function parseOtherPresentations($sValue)
	{
		// dd.mm.YYYY, dd/mm/YYYY, dd-mm-YYYY
		// dd.mm.YY, dd/mm/YY, dd-mm-YY
		// d.m.YYYY, d/m/YYYY, d-m-YYYY
		// d.m.YY, d/m/YY, d-m-YY
		$regex1 = '^([0-3]?\d)([\-\.\/])([01]?\d)\2([0-9]{2})(\d\d){0,1}$';

		if (preg_match("/$regex1/", str_replace(' ', '', $this->_mValue), $data))
		{
			if (isset($data[5]))
			$year = $data[4] * 100 + $data[5];
			else if ($data[4] <= 50)
			$year = 2000 + $data[4];
			else
			$year = 1900 + $data[4];

			$day = $data[1];
			$month = $data[3];

			$timestamp = mktime(0, 0, 0, $month, $day, $year);
			$this->_mValue = date($this->_sDateMask, $timestamp);
			return date($this->_sDateMask, $timestamp);
		}

		// YYYY/mm/dd, YYYY-mm-dd
		// YY/mm/dd, YY-mm-dd
		// YYYY/m/d, YYYY-m-y
		// YY/m/d, YY-m-y
		$regex2 = '^([0-9]{2})(\d\d){0,1}([\-\/])([01]?\d)\3([0-3]?\d)$';

		if (preg_match("/$regex2/", str_replace(' ', '', $this->_mValue), $data))
		{
			if (isset($data[2]))
			$year = $data[1] * 100 + $data[2];
			else if ($data[1] <= 50)
			$year = 2000 + $data[1];
			else
			$year = 1900 + $data[1];

			$day = $data[5];
			$month = $data[4];

			$timestamp = mktime(0, 0, 0, $month, $day, $year);
			$this->_mValue = date($this->_sDateMask, $timestamp);
			return date($this->_sDateMask, $timestamp);
		}

		return $sValue;
	}

	/**
     * Check if the date is valid (eg not 31-02-2003)
     *
     * @return boolean: true if the field is correct, false if not
     * @access public
     * @author Thomas Branius
     */
	function isValid()
	{
		// the result has been requested before..
		if( isset( $this->_isValid ) )
		{
			return $this->_isValid;
		}

		if( $this->getValue() != "" )
		{
			if( preg_match( $this->_sValParseRegExpr, $this->getValue(), $data ) )
			{
				$data = $this->getAsArray();
				if( checkdate($data[1], $data[2], $data[0]) == false )
				{
					$this->_isValid = false;
				}
				else
				{
					$timestamp = mktime(0, 0, 0,$data[1], $data[2], $data[0]);
					$this->_mValue = date($this->_sDateMask, $timestamp);
				}
			}
			else
			{
				$this->_isValid = false;
			}
		}

		if( isset( $this->_isValid ) && $this->_isValid == false )
		{
			// set the error message
			$this->_sError = $this->_oForm->_text( 14 );
		}

		return parent::isValid();
	}
}
?>
Back to Top