* ImportExport: Date custom fields with custom formatting now correctly handled

This commit is contained in:
nathangray 2020-09-03 11:29:55 -06:00 committed by Ralf Becker
parent f4af341919
commit 3cc204e9ed
2 changed files with 63 additions and 19 deletions

View File

@ -229,14 +229,14 @@ class importexport_export_csv implements importexport_iface_export_record
/** /**
* Parse custom fields for an app, so a more human friendly value can be exported * Parse custom fields for an app, so a more human friendly value can be exported
* *
* @param importexport_iface_egw_record|Array $record
* @param appname Name of the app to fetch the custom fields for * @param appname Name of the app to fetch the custom fields for
* @param selects Lookup values for select boxes * @param array $selects
* @param links Appnames for links to fetch the title * @param array $links
* @param methods Method will be called with the record's value * @param array $methods
*
* @return Array of fields to be added to list of fields needing conversion * @return Array of fields to be added to list of fields needing conversion
*/ */
public static function convert_parse_custom_fields($appname, &$selects = array(), &$links = array(), &$methods = array()) { public static function convert_parse_custom_fields(&$record, $appname, &$selects = array(), &$links = array(), &$methods = array()) {
if(!$appname) return; if(!$appname) return;
$fields = array(); $fields = array();
@ -246,6 +246,24 @@ class importexport_export_csv implements importexport_iface_export_record
switch($c_field['type']) { switch($c_field['type']) {
case 'date': case 'date':
$fields['date'][] = $name; $fields['date'][] = $name;
if ($c_field['values']['format'] && (is_array($record) ? $record[$name] : $record->$name))
{
// Date has custom format. Convert so it's standard.
$val = is_array($record) ? $record[$name] : $record->$name;
$date = Api\DateTime::createFromFormat($c_field['values']['format'], $val, Api\DateTime::$user_timezone);
if($date)
{
$val = $date->format(APi\DateTime::DATABASE);
if (is_array($record))
{
$record[$name] = $val;
}
else
{
$record->$name = $val;
}
}
}
break; break;
case 'date-time': case 'date-time':
$fields['date-time'][] = $name; $fields['date-time'][] = $name;
@ -288,10 +306,13 @@ class importexport_export_csv implements importexport_iface_export_record
* @parem fields List of field types => field names to be converted * @parem fields List of field types => field names to be converted
* @param appname Current appname if you want to do custom fields too * @param appname Current appname if you want to do custom fields too
*/ */
public static function convert(importexport_iface_egw_record &$record, Array $fields = array(), $appname = null, $selects = array()) { public static function convert(importexport_iface_egw_record &$record, Array $fields = array(), $appname = null, $selects = array())
if($appname) { {
if(!self::$cf_parse_cache[$appname]) { if($appname)
$c_fields = self::convert_parse_custom_fields($appname, $selects, $links, $methods); {
if(!self::$cf_parse_cache[$appname])
{
$c_fields = self::convert_parse_custom_fields($record, $appname, $selects, $links, $methods);
self::$cf_parse_cache[$appname] = array($c_fields, $selects, $links, $methods); self::$cf_parse_cache[$appname] = array($c_fields, $selects, $links, $methods);
} }
list($c_fields, $c_selects, $links, $methods) = self::$cf_parse_cache[$appname]; list($c_fields, $c_selects, $links, $methods) = self::$cf_parse_cache[$appname];
@ -307,8 +328,10 @@ class importexport_export_csv implements importexport_iface_export_record
} }
// Not quite a recursive merge, since only one level // Not quite a recursive merge, since only one level
foreach($fields as $type => &$list) { foreach($fields as $type => &$list)
if($c_fields[$type]) { {
if($c_fields[$type])
{
$list = array_merge($c_fields[$type], $list); $list = array_merge($c_fields[$type], $list);
unset($c_fields[$type]); unset($c_fields[$type]);
} }
@ -316,17 +339,23 @@ class importexport_export_csv implements importexport_iface_export_record
$fields += $c_fields; $fields += $c_fields;
$selects += $c_selects; $selects += $c_selects;
} }
foreach((array)$fields['select'] as $name) { foreach((array)$fields['select'] as $name)
if($record->$name != null && is_array($selects) && $selects[$name]) { {
if($record->$name != null && is_array($selects) && $selects[$name])
{
$record->$name = is_string($record->$name) ? explode(',', $record->$name) : $record->$name; $record->$name = is_string($record->$name) ? explode(',', $record->$name) : $record->$name;
if(is_array($record->$name)) { if(is_array($record->$name))
{
$names = array(); $names = array();
foreach($record->$name as $_name) { foreach($record->$name as $_name)
{
$option = $selects[$name][$_name]; $option = $selects[$name][$_name];
$names[] = lang(is_array($option) && $option['label'] ? $option['label'] : $option); $names[] = lang(is_array($option) && $option['label'] ? $option['label'] : $option);
} }
$record->$name = implode(', ', $names); $record->$name = implode(', ', $names);
} else { }
else
{
$record->$name = lang($selects[$name][$record->$name]); $record->$name = lang($selects[$name][$record->$name]);
} }
} }

View File

@ -274,7 +274,7 @@ class importexport_import_csv implements importexport_iface_import_record { //,
Api\Translation::add_app($appname); Api\Translation::add_app($appname);
if(!self::$cf_parse_cache[$appname]) { if(!self::$cf_parse_cache[$appname]) {
$c_fields = importexport_export_csv::convert_parse_custom_fields($appname, $selects, $links, $methods); $c_fields = importexport_export_csv::convert_parse_custom_fields($record, $appname, $selects, $links, $methods);
self::$cf_parse_cache[$appname] = array($c_fields, $selects, $links, $methods); self::$cf_parse_cache[$appname] = array($c_fields, $selects, $links, $methods);
} }
list($c_fields, $c_selects, $links, $methods) = self::$cf_parse_cache[$appname]; list($c_fields, $c_selects, $links, $methods) = self::$cf_parse_cache[$appname];
@ -426,7 +426,15 @@ class importexport_import_csv implements importexport_iface_import_record { //,
if(is_array(self::$cf_parse_cache[$appname][0]['date-time']) && if(is_array(self::$cf_parse_cache[$appname][0]['date-time']) &&
in_array($name, self::$cf_parse_cache[$appname][0]['date-time'])) { in_array($name, self::$cf_parse_cache[$appname][0]['date-time'])) {
// Custom fields stored in a particular format (from customfields_widget) // Custom fields stored in a particular format (from customfields_widget)
$record[$name] = date('Y-m-d H:i:s', $record[$name]); $date_format = 'Y-m-d H:i:s';
// Check for custom format
$cfs = Api\Storage\Customfields::get($appname);
if($cfs && $cfs[substr($name,1)] && $cfs[substr($name,1)]['values']['format'])
{
$date_format = $cfs[substr($name,1)]['values']['format'];
}
$record[$name] = date($date_format, $record[$name]);
} }
} }
if(array_key_exists($name, $record) && strlen(trim($record[$name])) == 0) if(array_key_exists($name, $record) && strlen(trim($record[$name])) == 0)
@ -450,7 +458,14 @@ class importexport_import_csv implements importexport_iface_import_record { //,
if(is_array(self::$cf_parse_cache[$appname][0]['date']) && if(is_array(self::$cf_parse_cache[$appname][0]['date']) &&
in_array($name, self::$cf_parse_cache[$appname][0]['date'])) { in_array($name, self::$cf_parse_cache[$appname][0]['date'])) {
// Custom fields stored in a particular format (from customfields_widget) // Custom fields stored in a particular format (from customfields_widget)
$record[$name] = date('Y-m-d', $record[$name]); $date_format = 'Y-m-d';
// Check for custom format
$cfs = Api\Storage\Customfields::get($appname);
if($cfs && $cfs[substr($name,1)] && $cfs[substr($name,1)]['values']['format'])
{
$date_format = $cfs[substr($name,1)]['values']['format'];
}
$record[$name] = date($date_format, $record[$name]);
} }
} }
if(array_key_exists($name, $record) && strlen(trim($record[$name])) == 0) if(array_key_exists($name, $record) && strlen(trim($record[$name])) == 0)