Skip to content

Commit

Permalink
Improving CSV import of dates. Should now correctly handle any valid …
Browse files Browse the repository at this point in the history
…format, and can specify the form or the list format as the one to use for import, as well as GMT. At some point need to split out the TZ option into it's own setting, instead of being "GMT with TZ / GMT without tZ".
  • Loading branch information
cheesegrits committed Jan 11, 2017
1 parent a280970 commit e9285dd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 47 deletions.
90 changes: 45 additions & 45 deletions plugins/fabrik_element/date/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function renderListData($data, stdClass &$thisRow, $opts = array())

if (strstr($f, '%'))
{
FabDate::strftimeFormatToDateFormat($f);
$f = FabDate::strftimeFormatToDateFormat($f);
}

foreach ($data as $d)
Expand Down Expand Up @@ -253,7 +253,7 @@ public function render($data, $repeatCounter = 0)

if (strstr($format, '%'))
{
FabDate::strftimeFormatToDateFormat($format);
$format = FabDate::strftimeFormatToDateFormat($format);
}

$timeFormat = $params->get('date_time_format', 'H:i');
Expand Down Expand Up @@ -818,7 +818,7 @@ protected function _CalendarJSOpts($id)
$opts->firstDay = intval($params->get('date_firstday'));
$opts->ifFormat = $params->get('date_form_format', $params->get('date_table_format', '%Y-%m-%d'));
$opts->timeFormat = 24;
FabDate::dateFormatToStrftimeFormat($opts->ifFormat);
$opts->ifFormat = FabDate::dateFormatToStrftimeFormat($opts->ifFormat);
$opts->dateAllowFunc = $params->get('date_allow_func');

return $opts;
Expand Down Expand Up @@ -1106,6 +1106,8 @@ public function getValue($data, $repeatCounter = 0, $opts = array())
*/
protected function formatContainsTime($format)
{
$format = FabDate::dateFormatToStrftimeFormat($format);

$times = array('%H', '%I', '%l', '%M', '%p', '%P', '%r', '%R', '%S', '%T', '%X', '%z', '%Z');

foreach ($times as $t)
Expand Down Expand Up @@ -1829,33 +1831,45 @@ public function prepareCSVData(&$data, $key, $isRaw = false)
}

$params = $this->getParams();
$format = $params->get('date_form_format', '%Y-%m-%d %H:%S:%I');
$csvFormat = $params->get('date_csv_offset_tz', '0');

// Go through data and turn any dates into unix timestamps
for ($j = 0; $j < count($data); $j++)
// 0 is "format format", 1 is "list format"
if ($csvFormat === '0' || $csvFormat === '3')
{
$orig_data = $data[$j][$key];
$paramName = $csvFormat === '0' ? 'date_form_format' : 'date_table_format';
$format = $params->get($paramName, 'Y-m-d H:i:s');

try
// Go through data and convert specified format to MySQL
for ($j = 0; $j < count($data); $j++)
{
$date = JFactory::getDate($data[$j][$key]);
$data[$j][$key] = $date->format($format, true);
/* $$$ hugh - bit of a hack specific to a customer who needs to import dates with year as 1899,
* which we then change to 1999 using a tablecsv import script (don't ask!). But of course FabDate doesn't
* like dates outside of UNIX timestamp range, so the previous line was zapping them. So I'm just restoring
* the date as found in the CSV file. This could have side effects if someone else tries to import invalid dates,
* but ... ah well.
* */
if (empty($data[$j][$key]) && !empty($orig_data))
$orig_data = $data[$j][$key];

if (empty($orig_data))
{
$data[$j][$key] = $orig_data;
$data[$j][$key] = '0000-00-00 00:00:00';
continue;
}
}
catch (Exception $e)
{
// Suppress date time format error
}

try
{
$date = DateTime::createFromFormat($format, $orig_data);
$exactTime = $this->formatContainsTime($format);

if ($date !== false)
{
if (!$params->get('date_showtime', 0) || $exactTime == false)
{
$date->setTime(0, 0, 0);
}

$data[$j][$key] = $date->format('Y-m-d H:i:s');
}
}
catch (Exception $e)
{
// Suppress date time format error
}
}
}
}

Expand Down Expand Up @@ -2666,13 +2680,13 @@ protected function monthToInt($str)
/**
* Converts strftime format into PHP date() format
*
* @param string &$format Strftime format
* @param string $format Strftime format
*
* @since 3.0.7
*
* @return void
* @return string converted format
*/
static public function strftimeFormatToDateFormat(&$format)
static public function strftimeFormatToDateFormat($format)
{
$app = JFactory::getApplication();

Expand All @@ -2689,32 +2703,18 @@ static public function strftimeFormatToDateFormat(&$format)
$replace = array('j', 'z', 'w', 'W', 'W', 'M', 'F', 'Y', 'y', 'Y', 'i', 'a', '"g:i:s a', 'H:i', 'H:i:s', 'H:i:s', 'O', 'O', 'm/d/y"', 'Y-m-d', 'U',
'Y-m-d', 'l', 'Y', 'm', 'd', 'H', 's');

$format = str_replace($search, $replace, $format);
return str_replace($search, $replace, $format);
}

/**
* Convert strftime to PHP time format
*
* @param string &$format Format
* @param string $format Format
*
* @return void
* @return string converted format
*/
static public function dateFormatToStrftimeFormat(&$format)
static public function dateFormatToStrftimeFormat($format)
{
// changed to use single array and strtr() to avoid left to right translation dupes, leaving this here for now
/*
$search = array('d', 'D', 'j', 'l', 'N', 'S', 'w', 'z', 'W', 'F', 'm', 'M', 'n', 't', 'L', 'o', 'Y',
'y', 'a', 'A', 'B', 'g', 'G', 'h', 'H', 'i', 's', 'u',
'I', 'O', 'P', 'T', 'Z', 'c', 'r', 'U');
$replace = array('%d', '%a', '%e', '%A', '%u', '', '%w', '%j', '%V', '%B', '%m', '%b', '%m', '', '', '%g', '%Y',
'%y', '%P', '%p', '', '%l', '%H', '%I', '%H', '%M', '%S', '',
'', '', '', '%z', '', '%c', '%a, %d %b %Y %H:%M:%S %z', '%s');
// Removed e => %z as that meant, j => %e => %%z (prob could re-implement with a regex if really needed)
$format = str_replace($search, $replace, $format);
*/

$trs = array(
'd' => '%d',
'D' => '%a',
Expand Down Expand Up @@ -2755,7 +2755,7 @@ static public function dateFormatToStrftimeFormat(&$format)
'U' => '%s'
);

$format = strtr($format, $trs);
return strtr($format, $trs);
}

/**
Expand Down
1 change: 1 addition & 0 deletions plugins/fabrik_element/date/forms/fields.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
description="PLG_ELEMENT_DATE_CSV_OFFSET_TZ_DESC"
label="PLG_ELEMENT_DATE_CSV_OFFSET_TZ_LABEL">
<option value="0">PLG_ELEMENT_DATE_CSV_FORMAT_NORMAL</option>
<option value="3">PLG_ELEMENT_DATE_CSV_FORMAT_NORMAL_LIST</option>
<option value="1">PLG_ELEMENT_DATE_CSV_FORMAT_MYSQL_GMT</option>
<option value="2">PLG_ELEMENT_DATE_CSV_FORMAT_MYSQL_LOCAL</option>
</field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ PLG_ELEMENT_DATE_BOOTSTRAP_TIME_CLASS_DESC="The bootstrap class to assign to the
PLG_ELEMENT_DATE_BOOTSTRAP_TIME_CLASS_LABEL="Time bootstrap class"
PLG_ELEMENT_DATE_DATE_FORMATS_HEADING="Date Formats"
PLG_ELEMENT_DATE_CSV_OFFSET_TZ_LABEL="CSV Import Format"
PLG_ELEMENT_DATE_CSV_OFFSET_TZ_DESC="Date format in CSV import files. If set to Normal, Fabrik assumes your CSV date is in your specified 'form format', and in J!'s specified time zone. If set to 'MySQL, Local' your CSV dates must be in MySQL format (YYYY/MM/DD HH:MM:SS) with J!'s selected local time zone already applied. If set to 'MySQL, GMT', dates must be in MySQL format, in the GMT (UTC) time zone."
PLG_ELEMENT_DATE_CSV_FORMAT_NORMAL="Normal"
PLG_ELEMENT_DATE_CSV_OFFSET_TZ_DESC="Date format in CSV import files. If set to Normal, Fabrik assumes your CSV date is in your specified 'form format' or 'list format', and in J!'s specified time zone. If set to 'MySQL, Local' your CSV dates must be in MySQL format (YYYY/MM/DD HH:MM:SS) with J!'s selected local time zone already applied. If set to 'MySQL, GMT', dates must be in MySQL format, in the GMT (UTC) time zone."
PLG_ELEMENT_DATE_CSV_FORMAT_NORMAL="Normal (form format)"
PLG_ELEMENT_DATE_CSV_FORMAT_NORMAL_LIST="Normal (list format)"
PLG_ELEMENT_DATE_CSV_FORMAT_MYSQL_LOCAL="MySQL, Local TZ"
PLG_ELEMENT_DATE_CSV_FORMAT_MYSQL_GMT="MySQL, GMT TZ"
PLG_ELEMENT_DATE_STORE_AS_LABEL="Store Date As"
Expand Down

0 comments on commit e9285dd

Please sign in to comment.