wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.MySQLDateConverter.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.MySQLDateConverter.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/util/class.Message.php");
20 require_once(BASE."wcmf/lib/persistence/converter/class.DataConverter.php");
21 
22 /**
23  * @class MySQLDateConverter
24  * @ingroup Converter
25  * @brief MySQLDateConverter converts MySQL dates to a date localized to the users or
26  * application settings.
27  *
28  * @author ingo herwig <ingo@wemove.com>
29  */
31 {
32  /**
33  * @see DataConverter::convertStorageToApplication()
34  */
35  function convertStorageToApplication($data, $type, $name)
36  {
37  $type = strtolower($type);
38  if ($data != '' && ($type == 'datetime' || $type == 'date'))
39  {
40  global $MESSAGE_LANGUAGE;
41  $locale = strtolower($MESSAGE_LANGUAGE);
42 
43  // handle empty dates
44  if (strpos($data, "0000-00-00") === 0)
45  return "";
46 
47  // convert localized date/time to language format
48  // we don't rely on setLocale, strftime and strtotime
49  $convertFunction = "storageTo_".$locale;
50  $methods = get_class_methods($this);
51  if (in_array($convertFunction, $methods))
52  $data = $this->$convertFunction($data, $type);
53  }
54  return $data;
55  }
56  /**
57  * @see DataConverter::convertApplicationToStorage()
58  * This method uses methods named localeToEnglish (e.g. de_deToEnglish) to
59  * convert a localized date/time string back to english format. Localization
60  * was done using strftime("%x %X", time) resp. strftime("%x", time).
61  * To support a special locale the appropriate method must be implemented.
62  */
63  function convertApplicationToStorage($data, $type, $name)
64  {
65  if ($data != '')
66  {
67  global $MESSAGE_LANGUAGE;
68  $locale = strtolower($MESSAGE_LANGUAGE);
69  $type = strtolower($type);
70 
71  if ($type == 'datetime' || $type == 'date')
72  {
73  // convert localized date/time to english format
74  $convertFunction = $locale."ToEnglish";
75  $methods = get_class_methods($this);
76  if (in_array($convertFunction, $methods))
77  $date = $this->$convertFunction($data, $type);
78 
79  // convert date/time to mysql
80  if ($type == 'datetime')
81  $data = strftime("%Y-%m-%d %H:%M:%S", strtotime($date));
82  if ($type == 'date')
83  $data = strftime("%Y-%m-%d", strtotime($date));
84  }
85  }
86  return $data;
87  }
88  /**
89  * Convert a german date/time string to english format
90  * @param date The localized date/time
91  * @param type One of these: date or datetime
92  */
93  function de_deToEnglish($date, $type)
94  {
95  // test german date format, return original if not matching
96  $testFormat = preg_split("/[\.: ]/", $date);
97  if (sizeof($testFormat) != 6 && sizeof($testFormat) != 5 && sizeof($testFormat) != 3)
98  return $date;
99 
100  list($d, $m, $Y, $H, $M, $S) = preg_split("/[\.: ]/", $date);
101  if ($type == 'datetime')
102  {
103  // make sure we get a valid date format even if only a date is supplied with the datetime type
104  if ($H=="") $H = "00";
105  if ($M=="") $M = "00";
106  if ($S=="") $S = "00";
107  return $m."/".$d."/".$Y." ".$H.":".$M.":".$S;
108  }
109  elseif ($type == 'date')
110  return $m."/".$d."/".$Y;
111  else
112  return $date;
113  }
114  /**
115  * Convert a english date/time string to english format
116  * @param date The localized date/time
117  * @param type One of these: date or datetime
118  */
119  function en_enToEnglish($date, $type)
120  {
121  return $date;
122  }
123  /**
124  * Convert a storage date format to german date/time string
125  * @param date The date/time in storage format
126  * @param type One of these: date or datetime
127  */
128  function storageTo_de_de($date, $type)
129  {
130  if ($type == 'datetime')
131  return strftime("%d.%m.%Y %H:%M:%S", strtotime($date));
132  elseif ($type == 'date')
133  return strftime("%d.%m.%Y", strtotime($date));
134  else
135  return $date;
136  }
137  /**
138  * Convert a storage date format to english date/time string
139  * @param date The date/time in storage format
140  * @param type One of these: date or datetime
141  */
142  function storageTo_en_en($date, $type)
143  {
144  if ($type == 'datetime')
145  return strftime("%m/%d/%Y %H:%M:%S", strtotime($date));
146  elseif ($type == 'date')
147  return strftime("%m/%d/%Y", strtotime($date));
148  else
149  return $date;
150  }
151 }
152 ?>
convertApplicationToStorage($data, $type, $name)
MySQLDateConverter converts MySQL dates to a date localized to the users or application settings...
convertStorageToApplication($data, $type, $name)
DataConverter is the base class for all converter classes. It defines the interface for converting da...