PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/fields/class.DateTextField.php

https://github.com/reshadf/Library
PHP | 296 lines | 172 code | 34 blank | 90 comment | 63 complexity | 33ec426ef61d6196e656371f2905b331 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * class DateTextField
  4. *
  5. * Create a DateTextfield
  6. *
  7. * @author Thomas Branius
  8. * @since 16-03-2010
  9. * @package FormHandler
  10. * @subpackage Fields
  11. *
  12. * validators added by Johan Wiegel
  13. */
  14. class DateTextField extends TextField
  15. {
  16. var $_sDateMask; // string: how to display the fields with mask
  17. var $_sValParseRegExpr; // string: how to parse the value
  18. var $_iDayPos; // int: position of day in regular expression
  19. var $_iMonthPos; // int: position of month in regular expression
  20. var $_iYearPos; // int: position of year in regular expression
  21. var $_bParseOtherPresentations; // bool: try to parse other presentations of dateformat
  22. /**
  23. * Constructor: create a new dateTextField object
  24. *
  25. * @param object &$oForm: the form where the datefield is located on
  26. * @param string $sName: the name of the datefield
  27. * @param string $sMask: the mask which is used to display the fields
  28. * @param bool $bParseOtherPresentations: try to parse other presentations of dateformat
  29. * @return dateTextField
  30. * @access public
  31. * @author Thomas Branius
  32. * @since 16-03-2010
  33. */
  34. function DateTextField( &$oForm, $sName, $sMask = null, $bParseOtherPresentations = false )
  35. {
  36. // set the default date display
  37. $this->setMask( !is_null( $sMask ) ? $sMask : FH_DATETEXTFIELD_DEFAULT_DISPLAY );
  38. $this->_bParseOtherPresentations = $bParseOtherPresentations;
  39. //$this->setValidator(array(&$this, "validate"));
  40. // call the constructor of the Field class
  41. parent::TextField($oForm, $sName);
  42. }
  43. /**
  44. * Set the display of the fields
  45. *
  46. * @param string $sMast: how we have to display the datefield (day-month-year combination)
  47. * @return void
  48. * @access public
  49. * @author Thomas Branius]
  50. * @since 16-03-2010
  51. */
  52. function setMask( $sMask )
  53. {
  54. // validate the mask
  55. $regex = '/^([dDmMyY])([\.\-\/])([dDmMyY])\2([dDmMyY])$/';
  56. if (preg_match($regex, $sMask, $data) == 0
  57. || strtolower($data[1]) == strtolower($data[3])
  58. || strtolower($data[1]) == strtolower($data[4])
  59. || strtolower($data[3]) == strtolower($data[4]))
  60. trigger_error("Invalid mask ['{$sMask}']. Useable chars: d, D, m, M, y, Y, ., -, /", E_USER_ERROR);
  61. // set postion of day, month and year
  62. for ($i = 1; $i < 5; $i++)
  63. {
  64. if (strtolower($data[$i]) == "d")
  65. $this->_iDayPos = $i;
  66. else if (strtolower($data[$i]) == "m")
  67. $this->_iMonthPos =$i;
  68. else if (strtolower($data[$i]) == "y")
  69. $this->_iYearPos = $i;
  70. }
  71. $seperator = str_replace(array('/', '.'), array('\/', '\.'), $data[2]);
  72. $regExDay = '[0-9]{' . ($data[$this->_iDayPos] == 'D' ? '1,2' : '2' ) . '}';
  73. $regExMonth = '[0-9]{' . ($data[$this->_iMonthPos] == 'M' ? '1,2' : '2' ) . '}';
  74. $regExYear = '[0-9]{' . ($data[$this->_iYearPos] == 'y' ? '2' : '4' ) . '}';
  75. $this->_iDayPos = $this->_iDayPos > 1 ? $this->_iDayPos - 1 : $this->_iDayPos;
  76. $this->_iMonthPos = $this->_iMonthPos > 1 ? $this->_iMonthPos - 1 : $this->_iMonthPos;
  77. $this->_iYearPos = $this->_iYearPos > 1 ? $this->_iYearPos - 1 : $this->_iYearPos;
  78. if ($this->_iYearPos == 1)
  79. {
  80. if ($this->_iDayPos == 2)
  81. $this->_sValParseRegExpr = "/^({$regExYear}){$seperator}({$regExDay}){$seperator}({$regExMonth})$/";
  82. else
  83. $this->_sValParseRegExpr = "/^({$regExYear}){$seperator}({$regExMonth}){$seperator}({$regExDay})$/";
  84. }
  85. else if ($this->_iYearPos == 2)
  86. {
  87. if ($this->_iDayPos == 1)
  88. $this->_sValParseRegExpr = "/^({$regExDay}){$seperator}({$regExYear}){$seperator}({$regExMonth})$/";
  89. else
  90. $this->_sValParseRegExpr = "/^({$regExMonth}){$seperator}({$regExYear}){$seperator}({$regExDay})$/";
  91. }
  92. else if ($this->_iYearPos == 3)
  93. {
  94. if ($this->_iDayPos == 1)
  95. $this->_sValParseRegExpr = "/^({$regExDay}){$seperator}({$regExMonth}){$seperator}({$regExYear})$/";
  96. else
  97. $this->_sValParseRegExpr = "/^({$regExMonth}){$seperator}({$regExDay}){$seperator}({$regExYear})$/";
  98. }
  99. // mask for date-function
  100. $this->_sDateMask = str_replace(array("D", "M"), array("j", "n"), $sMask);
  101. }
  102. /**
  103. * Get the date value as an array: array(y,m,d)
  104. *
  105. * @return array
  106. * @access public
  107. * @author Thomas Branius
  108. * @since 16-03-2010
  109. */
  110. function getAsArray()
  111. {
  112. if ($this->getValue() == "")
  113. {
  114. return array("", "", "");
  115. }
  116. if (preg_match($this->_sValParseRegExpr, $this->getValue(), $data) == 0)
  117. trigger_error("Value is not a valid date [" . $this->getValue() . "]", E_USER_ERROR);
  118. if ($data[$this->_iYearPos] <= 50)
  119. $data[$this->_iYearPos] = $data[$this->_iYearPos] + 2000;
  120. if ($data[$this->_iYearPos] <= 100)
  121. $data[$this->_iYearPos] = $data[$this->_iYearPos] + 1900;
  122. return array( $data[$this->_iYearPos], $data[$this->_iMonthPos], $data[$this->_iDayPos]);
  123. }
  124. /**
  125. * Return the value of the field
  126. *
  127. * @return mixed: the value of the field
  128. * @access public
  129. * @author Thomas Branius
  130. * @since 16-03-2010
  131. */
  132. function getValue()
  133. {
  134. $sValue = parent::getValue();
  135. if (preg_match($this->_sValParseRegExpr, $sValue))
  136. return $sValue;
  137. if ($this->_bParseOtherPresentations)
  138. $sValue = $this->parseOtherPresentations($sValue);
  139. return $sValue;
  140. }
  141. /**
  142. * Set the value of the field
  143. *
  144. * @param mixed $mValue: The new value for the field
  145. * @return void
  146. * @access public
  147. * @author Thomas Branius
  148. * @since 16-03-2010
  149. */
  150. function setValue( $mValue )
  151. {
  152. if ($this->_oForm->isPosted())
  153. return parent::setValue($mValue);
  154. // parse value from db
  155. $regex = '/([0-9]{4})-([0-9]{2})-([0-9]{2})/';
  156. if (preg_match("/0000-00-00/", $mValue))
  157. {
  158. $this->_mValue = null;
  159. }
  160. else if (preg_match($regex, $mValue, $data))
  161. {
  162. $timestamp = mktime(0, 0, 0, $data[2], $data[3], $data[1]);
  163. $this->_mValue = date($this->_sDateMask, $timestamp);
  164. }
  165. else
  166. {
  167. $this->_mValue = $mValue;
  168. }
  169. }
  170. /**
  171. T* try to parse other presentations of dateformat
  172. *
  173. * @return mixed: the value of the field
  174. * @access public
  175. * @author Thomas Branius
  176. * @since 16-03-2010
  177. */
  178. function parseOtherPresentations($sValue)
  179. {
  180. // dd.mm.YYYY, dd/mm/YYYY, dd-mm-YYYY
  181. // dd.mm.YY, dd/mm/YY, dd-mm-YY
  182. // d.m.YYYY, d/m/YYYY, d-m-YYYY
  183. // d.m.YY, d/m/YY, d-m-YY
  184. $regex1 = '^([0-3]?\d)([\-\.\/])([01]?\d)\2([0-9]{2})(\d\d){0,1}$';
  185. if (preg_match("/$regex1/", str_replace(' ', '', $this->_mValue), $data))
  186. {
  187. if (isset($data[5]))
  188. $year = $data[4] * 100 + $data[5];
  189. else if ($data[4] <= 50)
  190. $year = 2000 + $data[4];
  191. else
  192. $year = 1900 + $data[4];
  193. $day = $data[1];
  194. $month = $data[3];
  195. $timestamp = mktime(0, 0, 0, $month, $day, $year);
  196. $this->_mValue = date($this->_sDateMask, $timestamp);
  197. return date($this->_sDateMask, $timestamp);
  198. }
  199. // YYYY/mm/dd, YYYY-mm-dd
  200. // YY/mm/dd, YY-mm-dd
  201. // YYYY/m/d, YYYY-m-y
  202. // YY/m/d, YY-m-y
  203. $regex2 = '^([0-9]{2})(\d\d){0,1}([\-\/])([01]?\d)\3([0-3]?\d)$';
  204. if (preg_match("/$regex2/", str_replace(' ', '', $this->_mValue), $data))
  205. {
  206. if (isset($data[2]))
  207. $year = $data[1] * 100 + $data[2];
  208. else if ($data[1] <= 50)
  209. $year = 2000 + $data[1];
  210. else
  211. $year = 1900 + $data[1];
  212. $day = $data[5];
  213. $month = $data[4];
  214. $timestamp = mktime(0, 0, 0, $month, $day, $year);
  215. $this->_mValue = date($this->_sDateMask, $timestamp);
  216. return date($this->_sDateMask, $timestamp);
  217. }
  218. return $sValue;
  219. }
  220. /**
  221. * Check if the date is valid (eg not 31-02-2003)
  222. *
  223. * @return boolean: true if the field is correct, false if not
  224. * @access public
  225. * @author Thomas Branius
  226. */
  227. function isValid()
  228. {
  229. // the result has been requested before..
  230. if( isset( $this->_isValid ) )
  231. {
  232. return $this->_isValid;
  233. }
  234. if( $this->getValue() != "" )
  235. {
  236. if( preg_match( $this->_sValParseRegExpr, $this->getValue(), $data ) )
  237. {
  238. $data = $this->getAsArray();
  239. if( checkdate($data[1], $data[2], $data[0]) == false )
  240. {
  241. $this->_isValid = false;
  242. }
  243. else
  244. {
  245. $timestamp = mktime(0, 0, 0,$data[1], $data[2], $data[0]);
  246. $this->_mValue = date($this->_sDateMask, $timestamp);
  247. }
  248. }
  249. else
  250. {
  251. $this->_isValid = false;
  252. }
  253. }
  254. if( isset( $this->_isValid ) && $this->_isValid == false )
  255. {
  256. // set the error message
  257. $this->_sError = $this->_oForm->_text( 14 );
  258. }
  259. return parent::isValid();
  260. }
  261. }
  262. ?>