forked from flourishlib/flourish-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fORMDate.php
598 lines (512 loc) · 19.8 KB
/
fORMDate.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<?php
/**
* Provides additional date/time functionality for fActiveRecord classes
*
* @copyright Copyright (c) 2008-2010 Will Bond
* @author Will Bond [wb] <[email protected]>
* @license http://flourishlib.com/license
*
* @package Flourish
* @link http://flourishlib.com/fORMDate
*
* @version 1.0.0b9
* @changes 1.0.0b9 Updated code to work with the new fORM API [wb, 2010-08-06]
* @changes 1.0.0b8 Changed validation messages array to use column name keys [wb, 2010-05-26]
* @changes 1.0.0b7 Fixed the `set` methods to return the record object in order to be consistent with all other `set` methods [wb, 2010-03-15]
* @changes 1.0.0b6 Fixed an issue with calling a non-existent method on fTimestamp instances [wb, 2009-11-03]
* @changes 1.0.0b5 Updated code for the new fORMDatabase and fORMSchema APIs [wb, 2009-10-28]
* @changes 1.0.0b4 Fixed setting up the inspect callback in ::configureTimezoneColumn() [wb, 2009-10-11]
* @changes 1.0.0b3 Updated to use new fORM::registerInspectCallback() method [wb, 2009-07-13]
* @changes 1.0.0b2 Updated code to use new fValidationException::formatField() method [wb, 2009-06-04]
* @changes 1.0.0b The initial implementation [wb, 2008-09-05]
*/
class fORMDate
{
// The following constants allow for nice looking callbacks to static methods
const configureDateCreatedColumn = 'fORMDate::configureDateCreatedColumn';
const configureDateUpdatedColumn = 'fORMDate::configureDateUpdatedColumn';
const configureTimezoneColumn = 'fORMDate::configureTimezoneColumn';
const inspect = 'fORMDate::inspect';
const makeTimestampObjects = 'fORMDate::makeTimestampObjects';
const objectifyTimestampWithoutTimezone = 'fORMDate::objectifyTimestampWithoutTimezone';
const reset = 'fORMDate::reset';
const setDateCreated = 'fORMDate::setDateCreated';
const setDateUpdated = 'fORMDate::setDateUpdated';
const setTimestampColumn = 'fORMDate::setTimestampColumn';
const setTimezoneColumn = 'fORMDate::setTimezoneColumn';
const validateTimezoneColumns = 'fORMDate::validateTimezoneColumns';
/**
* Columns that should be filled with the date created for new objects
*
* @var array
*/
static private $date_created_columns = array();
/**
* Columns that should be filled with the date updated
*
* @var array
*/
static private $date_updated_columns = array();
/**
* Columns that store timezone information for timestamp columns
*
* @var array
*/
static private $timezone_columns = array();
/**
* Timestamp columns that have a corresponding timezone column
*
* @var array
*/
static private $timestamp_columns = array();
/**
* Composes text using fText if loaded
*
* @param string $message The message to compose
* @param mixed $component A string or number to insert into the message
* @param mixed ...
* @return string The composed and possible translated message
*/
static private function compose($message)
{
$args = array_slice(func_get_args(), 1);
if (class_exists('fText', FALSE)) {
return call_user_func_array(
array('fText', 'compose'),
array($message, $args)
);
} else {
return vsprintf($message, $args);
}
}
/**
* Sets a column to be a date created column
*
* When a new record is stored in the database, date created columns will
* be filled with the timestamp of the store operation.
*
* @param mixed $class The class name or instance of the class
* @param string $column The column to set as a date created column
* @return void
*/
static public function configureDateCreatedColumn($class, $column)
{
$class = fORM::getClass($class);
$table = fORM::tablize($class);
$schema = fORMSchema::retrieve($class);
$data_type = $schema->getColumnInfo($table, $column, 'type');
$valid_data_types = array('date', 'time', 'timestamp');
if (!in_array($data_type, $valid_data_types)) {
throw new fProgrammerException(
'The column specified, %1$s, is a %2$s column. Must be one of %3$s to be set as a date created column.',
$column,
$data_type,
join(', ', $valid_data_types)
);
}
fORM::registerHookCallback($class, 'pre::validate()', self::setDateCreated);
fORM::registerInspectCallback($class, $column, self::inspect);
if (empty(self::$date_created_columns[$class])) {
self::$date_created_columns[$class] = array();
}
self::$date_created_columns[$class][$column] = TRUE;
}
/**
* Sets a column to be a date updated column
*
* Whenever a record is stored in the database, a date updated column will
* be set to the timestamp of the operation.
*
* @param mixed $class The class name or instance of the class
* @param string $column The column to set as a date updated column
* @return void
*/
static public function configureDateUpdatedColumn($class, $column)
{
$class = fORM::getClass($class);
$table = fORM::tablize($class);
$schema = fORMSchema::retrieve($class);
$data_type = $schema->getColumnInfo($table, $column, 'type');
$valid_data_types = array('date', 'time', 'timestamp');
if (!in_array($data_type, $valid_data_types)) {
throw new fProgrammerException(
'The column specified, %1$s, is a %2$s column. Must be one of %3$s to be set as a date updated column.',
$column,
$data_type,
join(', ', $valid_data_types)
);
}
fORM::registerHookCallback($class, 'pre::validate()', self::setDateUpdated);
fORM::registerInspectCallback($class, $column, self::inspect);
if (empty(self::$date_updated_columns[$class])) {
self::$date_updated_columns[$class] = array();
}
self::$date_updated_columns[$class][$column] = TRUE;
}
/**
* Sets a timestamp column to store the timezone in another column
*
* Since not all databases support timezone information in timestamp
* columns, this method allows storing the timezone in another columns.
* When the timestamp and timezone are retrieved from the database, they
* will be automatically combined together into an fTimestamp object.
*
* @param mixed $class The class name or instance of the class to set the column format
* @param string $timestamp_column The timestamp column to store the timezone for
* @param string $timezone_column The column to store the timezone in
* @return void
*/
static public function configureTimezoneColumn($class, $timestamp_column, $timezone_column)
{
$class = fORM::getClass($class);
$table = fORM::tablize($class);
$schema = fORMSchema::retrieve($class);
$timestamp_data_type = $schema->getColumnInfo($table, $timestamp_column, 'type');
if ($timestamp_data_type != 'timestamp') {
throw new fProgrammerException(
'The timestamp column specified, %1$s, is a %2$s column. Must be a %3$s to have a related timezone column.',
$timestamp_column,
$data_type,
'timestamp'
);
}
$timezone_column_data_type = $schema->getColumnInfo($table, $timezone_column, 'type');
$valid_timezone_column_data_types = array('varchar', 'char', 'text');
if (!in_array($timezone_column_data_type, $valid_timezone_column_data_types)) {
throw new fProgrammerException(
'The timezone column specified, %1$s, is a %2$s column. Must be %3$s to be set as a timezone column.',
$timezone_column,
$timezone_column_data_type,
join(', ', $valid_timezone_column_data_types)
);
}
if (!fORM::checkHookCallback($class, 'post::validate()', self::validateTimezoneColumns)) {
fORM::registerHookCallback($class, 'post::validate()', self::validateTimezoneColumns);
}
if (!fORM::checkHookCallback($class, 'post::loadFromResult()', self::makeTimestampObjects)) {
fORM::registerHookCallback($class, 'post::loadFromResult()', self::makeTimestampObjects);
}
if (!fORM::checkHookCallback($class, 'pre::validate()', self::makeTimestampObjects)) {
fORM::registerHookCallback($class, 'pre::validate()', self::makeTimestampObjects);
}
fORM::registerInspectCallback($class, $timezone_column, self::inspect);
fORM::registerActiveRecordMethod(
$class,
'set' . fGrammar::camelize($timestamp_column, TRUE),
self::setTimestampColumn
);
fORM::registerActiveRecordMethod(
$class,
'set' . fGrammar::camelize($timezone_column, TRUE),
self::setTimezoneColumn
);
if (empty(self::$timestamp_columns[$class])) {
self::$timestamp_columns[$class] = array();
}
self::$timestamp_columns[$class][$timestamp_column] = $timezone_column;
if (empty(self::$timezone_columns[$class])) {
self::$timezone_columns[$class] = array();
}
self::$timezone_columns[$class][$timezone_column] = $timestamp_column;
}
/**
* Adds metadata about features added by this class
*
* @internal
*
* @param string $class The class being inspected
* @param string $column The column being inspected
* @param array &$metadata The array of metadata about a column
* @return void
*/
static public function inspect($class, $column, &$metadata)
{
if (!empty(self::$date_created_columns[$class][$column])) {
$metadata['feature'] = 'date created';
}
if (!empty(self::$date_updated_columns[$class][$column])) {
$metadata['feature'] = 'date updated';
}
if (!empty(self::$timezone_columns[$class][$column])) {
$metadata['feature'] = 'timezone';
}
}
/**
* Creates fTimestamp objects for every timestamp/timezone combination in the object
*
* @internal
*
* @param fActiveRecord $object The fActiveRecord instance
* @param array &$values The current values
* @param array &$old_values The old values
* @param array &$related_records Any records related to this record
* @param array &$cache The cache array for the record
* @return void
*/
static public function makeTimestampObjects($object, &$values, &$old_values, &$related_records, &$cache)
{
$class = get_class($object);
if (!isset(self::$timezone_columns[$class])) {
return;
}
foreach(self::$timezone_columns[$class] as $timezone_column => $timestamp_column) {
self::objectifyTimestampWithTimezone($values, $old_values, $timestamp_column, $timezone_column);
}
}
/**
* Turns a timestamp value into an fTimestamp object with a timezone specified by another column
*
* @internal
*
* @param array &$values The current values
* @param array &$old_values The old values
* @param string $timestamp_column The column holding the timestamp
* @param string $timezone_column The column holding the timezone
* @return void
*/
static public function objectifyTimestampWithTimezone(&$values, &$old_values, $timestamp_column, $timezone_column)
{
if ((!is_string($values[$timestamp_column]) &&
!is_object($values[$timestamp_column]) &&
!is_numeric($values[$timestamp_column])) ||
!strlen(trim($values[$timestamp_column]))) {
return;
}
try {
$value = $values[$timestamp_column];
if ($value instanceof fTimestamp) {
$value = $value->__toString();
}
$timezone = $values[$timezone_column];
if (!$timezone && $timezone !== '0' && $timezone !== 0) {
$timezone = NULL;
}
$value = new fTimestamp($value, $timezone);
if (fActiveRecord::hasOld($old_values, $timezone_column) && !fActiveRecord::hasOld($old_values, $timestamp_column)) {
fActiveRecord::assign($values, $old_values, $timestamp_column, $value);
} else {
$values[$timestamp_column] = $value;
}
if ($values[$timezone_column] === NULL) {
fActiveRecord::assign($values, $old_values, $timezone_column, $value->format('e'));
}
// If there was some error creating the timestamp object, we just leave all values alone
} catch (fExpectedException $e) { }
}
/**
* Resets the configuration of the class
*
* @internal
*
* @return void
*/
static public function reset()
{
self::$date_created_columns = array();
self::$date_updated_columns = array();
self::$timezone_columns = array();
self::$timestamp_columns = array();
}
/**
* Sets the appropriate column values to the date the object was created (for new records)
*
* @internal
*
* @param fActiveRecord $object The fActiveRecord instance
* @param array &$values The current values
* @param array &$old_values The old values
* @param array &$related_records Any records related to this record
* @param array &$cache The cache array for the record
* @return void
*/
static public function setDateCreated($object, &$values, &$old_values, &$related_records, &$cache)
{
if ($object->exists()) {
return;
}
$class = get_class($object);
foreach (self::$date_created_columns[$class] as $column => $enabled) {
fActiveRecord::assign(
$values,
$old_values,
$column,
fORM::objectify($class, $column, date('Y-m-d H:i:s'))
);
// If the column has a corresponding timezone column, set that too
if (isset(self::$timestamp_columns[$class][$column])) {
fActiveRecord::assign(
$values,
$old_values,
self::$timestamp_columns[$class][$column],
fTimestamp::getDefaultTimezone()
);
}
}
}
/**
* Sets the appropriate column values to the date the object was updated
*
* @internal
*
* @param fActiveRecord $object The fActiveRecord instance
* @param array &$values The current values
* @param array &$old_values The old values
* @param array &$related_records Any records related to this record
* @param array &$cache The cache array for the record
* @return void
*/
static public function setDateUpdated($object, &$values, &$old_values, &$related_records, &$cache)
{
$class = get_class($object);
foreach (self::$date_updated_columns[$class] as $column => $enabled) {
fActiveRecord::assign(
$values,
$old_values,
$column,
fORM::objectify($class, $column, date('Y-m-d H:i:s'))
);
// If the column has a corresponding timezone column, set that too
if (isset(self::$timestamp_columns[$class][$column])) {
fActiveRecord::assign(
$values,
$old_values,
self::$timestamp_columns[$class][$column],
fTimestamp::getDefaultTimezone()
);
}
}
}
/**
* Sets the timestamp column and then tries to objectify it with an related timezone column
*
* @internal
*
* @param fActiveRecord $object The fActiveRecord instance
* @param array &$values The current values
* @param array &$old_values The old values
* @param array &$related_records Any records related to this record
* @param array &$cache The cache array for the record
* @param string $method_name The method that was called
* @param array $parameters The parameters passed to the method
* @return fActiveRecord The record object, to allow for method chaining
*/
static public function setTimestampColumn($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters)
{
list ($action, $subject) = fORM::parseMethod($method_name);
$column = fGrammar::underscorize($subject);
$class = get_class($object);
if (!isset($parameters[0])) {
throw new fProgrammerException(
'The method, %s(), requires at least one parameter',
$method_name
);
}
$value = $parameters[0];
fActiveRecord::assign($values, $old_values, $column, $value);
$timezone_column = self::$timestamp_columns[$class][$column];
// See if we can make an fTimestamp object out of the values
self::objectifyTimestampWithTimezone($values, $old_values, $column, $timezone_column);
if ($value instanceof fTimestamp) {
fActiveRecord::assign($values, $old_values, $timezone_column, $value->format('e'));
}
return $object;
}
/**
* Sets the timezone column and then tries to objectify the related timestamp column
*
* @internal
*
* @param fActiveRecord $object The fActiveRecord instance
* @param array &$values The current values
* @param array &$old_values The old values
* @param array &$related_records Any records related to this record
* @param array &$cache The cache array for the record
* @param string $method_name The method that was called
* @param array $parameters The parameters passed to the method
* @return fActiveRecord The record object, to allow for method chaining
*/
static public function setTimezoneColumn($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters)
{
list ($action, $subject) = fORM::parseMethod($method_name);
$column = fGrammar::underscorize($subject);
$class = get_class($object);
if (!isset($parameters[0])) {
throw new fProgrammerException(
'The method, %s(), requires at least one parameter',
$method_name
);
}
fActiveRecord::assign($values, $old_values, $column, $parameters[0]);
// See if we can make an fTimestamp object out of the values
self::objectifyTimestampWithTimezone(
$values,
$old_values,
self::$timezone_columns[$class][$column],
$column
);
return $object;
}
/**
* Validates all timestamp/timezone columns
*
* @internal
*
* @param fActiveRecord $object The fActiveRecord instance
* @param array &$values The current values
* @param array &$old_values The old values
* @param array &$related_records Any records related to this record
* @param array &$cache The cache array for the record
* @param array &$validation_messages An array of ordered validation messages
* @return void
*/
static public function validateTimezoneColumns($object, &$values, &$old_values, &$related_records, &$cache, &$validation_messages)
{
$class = get_class($object);
if (empty(self::$timezone_columns[$class])) {
return;
}
foreach (self::$timezone_columns[$class] as $timezone_column => $timestamp_column) {
if ($values[$timestamp_column] instanceof fTimestamp || $values[$timestamp_column] === NULL) {
continue;
}
if (!fTimestamp::isValidTimezone($values[$timezone_column])) {
$validation_messages[$timezone_column] = self::compose(
'%sThe timezone specified is invalid',
fValidationException::formatField(fORM::getColumnName($class, $timezone_column))
);
} else {
$validation_messages[$timestamp_column] = self::compose(
'%sPlease enter a date/time',
fValidationException::formatField(fORM::getColumnName($class, $timestamp_column))
);
}
}
}
/**
* Forces use as a static class
*
* @return fORMDate
*/
private function __construct() { }
}
/**
* Copyright (c) 2008-2010 Will Bond <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/