[FEATURE] Add support for native date/time fields
authorXavier Perseguers <xavier@typo3.org>
Sun, 15 Jul 2012 15:01:23 +0000 (17:01 +0200)
committerSusanne Moog <typo3@susannemoog.de>
Fri, 17 Aug 2012 16:33:23 +0000 (18:33 +0200)
TYPO3 stores date and date/time values as a Unix timestamp.
This feature allows native database types to be used instead.

Native fields must be marked in the TCA using the key "dbType":

'my_native_date' => array(
    'exclude' => 0,
    'label' => 'My native date',
    'config' => array(
        'dbType'   => 'date',
        'type'     => 'input',
        'size'     => '8',
        'max'      => '20',
        'eval'     => 'date',
        'checkbox' => '0',
        'default'  => '0'
    )
),

Supported types for "dbType" are: date, datetime

Change-Id: I078047abd7a93e16cfca7f1fec3fe52109c6d347
Resolves: #38965
Releases: 6.0
Reviewed-on: http://review.typo3.org/12808
Reviewed-by: Marcus Schwemer
Tested-by: Marcus Schwemer
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Susanne Moog
Tested-by: Susanne Moog
t3lib/class.t3lib_befunc.php
t3lib/class.t3lib_db.php
t3lib/class.t3lib_tcemain.php
t3lib/class.t3lib_transferdata.php

index e66c700..1b91499 100644 (file)
@@ -2164,6 +2164,12 @@ class t3lib_BEfunc {
                                                // Hide value 0 for dates, but show it for everything else
                                        if (isset($value)) {
                                                if (t3lib_div::inList($theColConf['eval'], 'date')) {
+                                                               // Handle native date field
+                                                       if (isset($theColConf['dbType']) && $theColConf['dbType'] === 'date') {
+                                                               $dateTimeFormats = $GLOBALS['TYPO3_DB']->getDateTimeFormats($table);
+                                                               $emptyValue = $dateTimeFormats['date']['empty'];
+                                                               $value = $value !== $emptyValue ? strtotime($value) : 0;
+                                                       }
                                                        if (!empty($value)) {
                                                                $l = self::date($value) .
                                                                        ' (' .
@@ -2180,6 +2186,12 @@ class t3lib_BEfunc {
                                                                $l = self::time($value);
                                                        }
                                                } elseif (t3lib_div::inList($theColConf['eval'], 'datetime')) {
+                                                               // Handle native date/time field
+                                                       if (isset($theColConf['dbType']) && $theColConf['dbType'] === 'datetime') {
+                                                               $dateTimeFormats = $GLOBALS['TYPO3_DB']->getDateTimeFormats($table);
+                                                               $emptyValue = $dateTimeFormats['datetime']['empty'];
+                                                               $value = $value !== $emptyValue ? strtotime($value) : 0;
+                                                       }
                                                        if (!empty($value)) {
                                                                $l = self::datetime($value);
                                                        }
index 10f0ac1..0f84d14 100644 (file)
@@ -878,6 +878,25 @@ class t3lib_DB {
                return $wgolParts;
        }
 
+       /**
+        * Returns the date and time formats compatible with the given database table.
+        *
+        * @param string $table Table name for which to return an empty date. Just enter the table that the field-value is selected from (and any DBAL will look up which handler to use and then how date and time should be formatted).
+        * @return array
+        */
+       public function getDateTimeFormats($table) {
+               return array(
+                       'date' => array(
+                               'empty' => '0000-00-00',
+                               'format' => 'Y-m-d',
+                       ),
+                       'datetime' => array(
+                               'empty' => '0000-00-00 00:00:00',
+                               'format' => 'Y-m-d H:i:s',
+                       ),
+               );
+       }
+
        /**************************************
         *
         * MySQL wrapper functions
index 4572c8c..5345210 100644 (file)
@@ -519,6 +519,20 @@ class t3lib_TCEmain {
                                        // $incomingFieldArray is the array of fields
                                foreach ($this->datamap[$table] as $id => $incomingFieldArray) {
                                        if (is_array($incomingFieldArray)) {
+                                                       // Handle native date/time fields
+                                               $dateTimeFormats = $GLOBALS['TYPO3_DB']->getDateTimeFormats($table);
+                                               if (!isset($GLOBALS['TCA'][$table]['columns'])) {
+                                                       t3lib_div::loadTCA($table);
+                                               }
+                                               foreach ($GLOBALS['TCA'][$table]['columns'] as $column => $config) {
+                                                       if (isset($incomingFieldArray[$column])) {
+                                                               if (isset($config['config']['dbType']) && t3lib_div::inList('date,datetime', $config['config']['dbType'])) {
+                                                                       $emptyValue = $dateTimeFormats[$config['config']['dbType']]['empty'];
+                                                                       $format = $dateTimeFormats[$config['config']['dbType']]['format'];
+                                                                       $incomingFieldArray[$column] = $incomingFieldArray[$column] ? date($format, $incomingFieldArray[$column]) : $emptyValue;
+                                                               }
+                                                       }
+                                               }
 
                                                        // Hook: processDatamap_preProcessFieldArray
                                                foreach ($hookObjectsArr as $hookObj) {
@@ -1257,6 +1271,15 @@ class t3lib_TCEmain {
        function checkValue_input($res, $value, $tcaFieldConf, $PP, $field = '') {
                list($table, $id, $curValue, $status, $realPid, $recFID) = $PP;
 
+                       // Handle native date/time fields
+               $dateTimeFormats = $GLOBALS['TYPO3_DB']->getDateTimeFormats($table);
+               if (isset($tcaFieldConf['dbType']) && t3lib_div::inList('date,datetime', $tcaFieldConf['dbType'])) {
+                               // Convert the date/time into a timestamp for the sake of the checks
+                       $emptyValue = $dateTimeFormats[$tcaFieldConf['dbType']]['empty'];
+                       $timeOffset = $tcaFieldConf['dbType'] === 'date' ? 3600 * $GLOBALS['TYPO3_CONF_VARS']['SYS']['serverTimeZone'] : 0;
+                       $value = $value === $emptyValue ? 0 : strtotime($value) + $timeOffset;
+               }
+
                        // Secures the string-length to be less than max.
                if (intval($tcaFieldConf['max']) > 0) {
                        $value = $GLOBALS['LANG']->csConvObj->substr($GLOBALS['LANG']->charSet, $value, 0, intval($tcaFieldConf['max']));
@@ -1287,6 +1310,14 @@ class t3lib_TCEmain {
                        }
                }
 
+                       // Handle native date/time fields
+               if (isset($tcaFieldConf['dbType']) && t3lib_div::inList('date,datetime', $tcaFieldConf['dbType'])) {
+                               // Convert the timestamp back to a date/time
+                       $emptyValue = $dateTimeFormats[$tcaFieldConf['dbType']]['empty'];
+                       $format = $dateTimeFormats[$tcaFieldConf['dbType']]['format'];
+                       $res['value'] = $res['value'] ? date($format, $res['value']) : $emptyValue;
+               }
+
                return $res;
        }
 
index 75e8fde..fa0f6a2 100644 (file)
@@ -188,6 +188,13 @@ class t3lib_transferData {
         * @see fetchRecord()
         */
        function renderRecord($table, $id, $pid, $row) {
+               $dateTimeFormats = $GLOBALS['TYPO3_DB']->getDateTimeFormats($table);
+               foreach ($GLOBALS['TCA'][$table]['columns'] as $column => $config) {
+                       if (isset($config['config']['dbType']) && t3lib_div::inList('date,datetime', $config['config']['dbType'])) {
+                               $emptyValue = $dateTimeFormats[$config['config']['dbType']]['empty'];
+                               $row[$column] = (!empty($row[$column]) && $row[$column] !== $emptyValue) ? strtotime($row[$column]) : 0;
+                       }
+               }
 
                        // Init:
                $uniqueItemRef = $table . '_' . $id;
@@ -975,4 +982,4 @@ class t3lib_transferData {
        }
 }
 
-?>
\ No newline at end of file
+?>