From 0791bbb9f12ffcef429aab9ca2d26869e2c9091a Mon Sep 17 00:00:00 2001 From: Nathan Gray Date: Mon, 17 Dec 2012 18:50:03 +0000 Subject: [PATCH] Implement importing fields that are primary keys/links to a record in another app --- ...lass.importexport_basic_import_csv.inc.php | 5 ++ .../inc/class.importexport_export_csv.inc.php | 11 ++++ .../inc/class.importexport_import_csv.inc.php | 56 +++++++++++++++++-- .../inc/class.importexport_import_ui.inc.php | 15 +++++ importexport/lang/egw_en.lang | 3 + 5 files changed, 86 insertions(+), 4 deletions(-) diff --git a/importexport/inc/class.importexport_basic_import_csv.inc.php b/importexport/inc/class.importexport_basic_import_csv.inc.php index 78abab4ff4..05f83cdc55 100644 --- a/importexport/inc/class.importexport_basic_import_csv.inc.php +++ b/importexport/inc/class.importexport_basic_import_csv.inc.php @@ -77,6 +77,11 @@ abstract class importexport_basic_import_csv implements importexport_iface_impor */ protected $is_admin = false; + /** + * Select box values for human conversion + */ + protected $lookups = array(); + /** * @var int */ diff --git a/importexport/inc/class.importexport_export_csv.inc.php b/importexport/inc/class.importexport_export_csv.inc.php index f2d32c4ca1..922ed1680b 100644 --- a/importexport/inc/class.importexport_export_csv.inc.php +++ b/importexport/inc/class.importexport_export_csv.inc.php @@ -282,6 +282,16 @@ class importexport_export_csv implements importexport_iface_export_record if($appname) { if(!self::$cf_parse_cache[$appname]) { $c_fields = self::convert_parse_custom_fields($appname, $selects, $links, $methods); + + // Add in any fields that are keys to another app + foreach((array)$fields['links'] as $link_field => $app) + { + if(is_numeric($link_field)) continue; + $links[$link_field] = $app; + // Set it as a normal link field + $fields['links'][] = $link_field; + unset($fields['links'][$link_field]); + } self::$cf_parse_cache[$appname] = array($c_fields, $selects, $links, $methods); } list($c_fields, $c_selects, $links, $methods) = self::$cf_parse_cache[$appname]; @@ -294,6 +304,7 @@ class importexport_export_csv implements importexport_iface_export_record } $fields += $c_fields; $selects += $c_selects; + } foreach((array)$fields['select'] as $name) { if($record->$name != null && is_array($selects) && $selects[$name]) { diff --git a/importexport/inc/class.importexport_import_csv.inc.php b/importexport/inc/class.importexport_import_csv.inc.php index 85589aadf5..7c7851bc73 100755 --- a/importexport/inc/class.importexport_import_csv.inc.php +++ b/importexport/inc/class.importexport_import_csv.inc.php @@ -272,13 +272,23 @@ class importexport_import_csv implements importexport_iface_import_record { //, if(!self::$cf_parse_cache[$appname]) { $c_fields = importexport_export_csv::convert_parse_custom_fields($appname, $selects, $links, $methods); + // Add in any fields that are keys to another app + foreach((array)$fields['links'] as $link_field => $app) + { + if(is_numeric($link_field)) continue; + $links[$link_field] = $app; + // Set it as a normal link field + $fields['links'][] = $link_field; + unset($fields['links'][$link_field]); + } self::$cf_parse_cache[$appname] = array($c_fields, $selects, $links, $methods); } list($c_fields, $c_selects, $links, $methods) = self::$cf_parse_cache[$appname]; // Not quite a recursive merge, since only one level - foreach($fields as $type => &$list) { + foreach($fields as $type => &$list) + { if($c_fields[$type]) { - $list = array_merge($c_fields[$type], $list);; + $list = array_merge($c_fields[$type], $list); unset($c_fields[$type]); } } @@ -301,8 +311,46 @@ class importexport_import_csv implements importexport_iface_import_record { //, } } foreach((array)$fields['links'] as $name) { - if($record[$name]) { - // TODO + if($record[$name] && $links[$name]) + { + // Primary key to another app, not a link + // Text - search for a matching record + if(!is_numeric($record[$name])) + { + $results = egw_link::query($links[$name], $record[$name]); + if(count($results) > 1) + { + // More than 1 result. Check for exact match + $exact_count = 0; + foreach($results as $id => $title) + { + if($title == $record[$name]) + { + $exact_count++; + $app_id = $id; + } + } + if($exact_count != 1) + { + $warnings[] = lang('Unable to link to %1 "%2"', + lang($links[$name]), $record[$name]). + ' - ' .lang('too many matches'); + $record[$name] = null; + continue; + } + $record[$name] = $app_id; + } + else if (count($results) == 0) + { + $warnings[] = lang('Unable to link to %1 "%2"', + lang($links[$name]), $record[$name]). + ' - ' . lang('no matches'); + $record[$name] = null; + continue; + } else { + $record[$name] = key($results); + } + } } } foreach((array)$fields['select-account'] as $name) { diff --git a/importexport/inc/class.importexport_import_ui.inc.php b/importexport/inc/class.importexport_import_ui.inc.php index 4c8fed1414..04c45aba92 100644 --- a/importexport/inc/class.importexport_import_ui.inc.php +++ b/importexport/inc/class.importexport_import_ui.inc.php @@ -296,6 +296,21 @@ $preview = html::table($rows); rewind($stream); } + if(count($plugin->get_warnings())) { + $this->message .= "
\n".lang('Warnings').':'; + foreach($plugin->get_warnings() as $record => $message) { + $this->message .= "\n$record: $message"; + } + $this->message .= "
\n"; + }; + if(count($plugin->get_errors())) { + $this->message .= "
\n".lang('Problems during import:'); + foreach($plugin->get_errors() as $record => $message) { + $this->message .= "
\n$record: $message"; + } + if($count != $total_processed) $this->message .= "
\n".lang('Some records may not have been imported'); + $this->message .= "
\n"; + } return '

' . lang('Preview') . ' - ' . $plugin->get_name() . '

' . $preview; } diff --git a/importexport/lang/egw_en.lang b/importexport/lang/egw_en.lang index 0b960c3f5e..c71304460d 100644 --- a/importexport/lang/egw_en.lang +++ b/importexport/lang/egw_en.lang @@ -69,6 +69,7 @@ load sample file importexport en Load sample file manage mapping importexport en Manage mapping next importexport en Next next run importexport en Next run +no matches importexport en no matches no records selected importexport en No records selected! old fixed definition preferences en Old fixed definition please select file to import importexport en Please select file to import @@ -92,9 +93,11 @@ target importexport en Target target examples: vfs://default/home/user/export.csv or http://server.net/prices.csv importexport en Target examples: vfs://default/home/user/export.csv or http://server.net/prices.csv target field importexport en Target field test only importexport en Test only +too many matches importexport en too many matches translation importexport en Translation true importexport en True try importexport en Try +unable to link to %1 "%2" importexport en Unable to link to %1 "%2" unable to schedule importexport en Unable to schedule. update categories importexport en Update categories user preference importexport en User preference