-
Notifications
You must be signed in to change notification settings - Fork 2
/
DateTimeI18NBehavior.php
230 lines (198 loc) · 6.09 KB
/
DateTimeI18NBehavior.php
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
<?php
/**
* DateTimeI18NBehavior
* Automatically converts date and datetime fields to I18N format
*
* @author Ricardo Grana <[email protected]>, <[email protected]>
* @version 1.1
*
* @author Leo Zandvliet
* @version 1.2
* @release notes
* - added afterSave() function which is needed in case of errors or further processing of model before end of application.
* Credits to Freezy (https://www.yiiframework.com/user/2129)
* - added public accessible functions to convert a specific attribute to locale or database notation
* - renamed local properties
* - added properties for database notation
* - added initFormats() function that will load default values from Yii::app()->locale and Yii::app()->format
*/
class DateTimeI18NBehavior extends CActiveRecordBehavior
{
// Database format in php date time notation
public $databaseDateFormat = null;
public $databaseDateTimeFormat = null;
// Database format in Yii date time notation
public $databaseDateFormatYii = null;
public $databaseDateTimeFormatYii = null;
// Locale format in Yii date time notation
public $localeDateFormat = null;
public $localeDateTimeFormat = null;
// Locale representation width in Yii textual notation
public $localeDateWidth = null;
public $localeDateTimeWidth = null;
private $_initialized = false;
/**
* List of columns by model classes. Contains only date and datetime columns
* cache = array(
* typeName => array(
* 'date' => array() // Columns with 'date' type
* 'datetime' => array() // Columns with 'datetime' type
* )
* )
*
* @var array
* @see DateTimeI18NBehavior::checkCache
*/
private static $cache = array();
public function afterConstruct($event)
{
$this->initFormats();
return true;
}
public function beforeSave($event)
{
$this->convertToDatabaseFormat($event->sender);
return true;
}
/**
* We must reconvert columns after they saved (little hack for CForm)
*/
public function afterSave($event)
{
$this->convertToLocaleFormat($event->sender);
return true;
}
public function afterFind($event)
{
$this->initFormats();
$this->convertToLocaleFormat($event->sender);
return true;
}
/**
* If format and width not set, retrieve them from the app and formatter.
*/
private function initFormats()
{
if($this->_initialized===true)
return;
if($this->databaseDateFormat===null)
$this->databaseDateFormat = 'Y-m-d';
if($this->databaseDateTimeFormat===null)
$this->databaseDateTimeFormat = 'Y-m-d H:i:s';
// The Yii date format is slightly different
if($this->databaseDateFormatYii===null)
$this->databaseDateFormatYii = 'yyyy-MM-dd';
// The Yii date time format is slightly different
if($this->databaseDateTimeFormatYii===null)
$this->databaseDateTimeFormatYii = 'yyyy-MM-dd HH:mm:ss';
if($this->localeDateFormat===null)
$this->localeDateFormat = Yii::app()->locale->getDateFormat(Yii::app()->format->dateFormat);
if($this->localeDateTimeFormat===null)
$this->localeDateTimeFormat = Yii::app()->format->datetimeFormat;
if($this->localeDateWidth===null)
$this->localeDateWidth = Yii::app()->format->dateFormat;
if($this->localeDateTimeWidth===null)
$this->localeDateTimeWidth = Yii::app()->format->timeFormat;
$this->_initialized=true;
}
public function toLocaleDate($attribute)
{
return Yii::app()->dateFormatter->formatDateTime(
CDateTimeParser::parse($this->owner->$attribute, $this->databaseDateFormatYii),
Yii::app()->format->dateFormat,
null
);
}
public function toLocaleDateTime($attribute)
{
return Yii::app()->dateFormatter->formatDateTime(
CDateTimeParser::parse($this->owner->$attribute, $this->databaseDateTimeFormatYii),
Yii::app()->format->dateFormat,
Yii::app()->format->timeFormat
);
}
public function toDatabaseDate($attribute)
{
return date(
$this->databaseDateFormat,
CDateTimeParser::parse($this->owner->$attribute, $this->localeDateFormat)
);
}
public function toDatabaseDateTime($attribute)
{
return date(
$this->databaseDateTimeFormat,
CDateTimeParser::parse(
$this->owner->$attribute,
$this->localeDateTimeFormat
)
);
}
private function convertToLocaleFormat(CActiveRecord $model)
{
$this->checkCache($model);
$type = get_class($model);
$columns = &self::$cache[$type];
// Convert all columns with 'date' type
foreach ($columns['date'] as $columnName)
{
if (strlen($model->$columnName) > 0)
{
$model->$columnName = $this->toLocaleDate($columnName);
}
}
// Convert all columns with 'datetime' type
foreach ($columns['datetime'] as $columnName)
{
if (strlen($model->$columnName) > 0)
{
$model->$columnName = $this->toLocaleDateTime($columnName);
}
}
}
private function convertToDatabaseFormat(CActiveRecord $model)
{
$this->checkCache($model);
$type = get_class($model);
$columns = &self::$cache[$type];
$this->initFormats();
// Convert all columns with 'date' type
foreach ($columns['date'] as $columnName)
{
if (strlen($model->$columnName) > 0)
{
$model->$columnName = $this->toDatabaseDate($columnName);
}
}
// Convert all columns with 'datetime' type
foreach ($columns['datetime'] as $columnName)
{
if (strlen($model->$columnName) > 0)
{
$model->$columnName = $this->toDatabaseDateTime($columnName);
}
}
}
/**
* Check cache for type of $model, and make if need
*
* @param CActiveRecord $model
*/
private function checkCache(CActiveRecord $model)
{
$type = get_class($model);
if (!isset(self::$cache[$type]))
{
self::$cache[$type] = array(
'date' => array(),
'datetime' => array()
);
$columns = &self::$cache[$type];
foreach ($model->tableSchema->columns as $columnName => $column)
{
if ($column->dbType == 'date' || $column->dbType == 'datetime')
$columns[$column->dbType][] = $columnName;
}
}
}
}