Implement importing fields that are primary keys/links to a record in another app

This commit is contained in:
Nathan Gray 2012-12-17 18:50:03 +00:00
parent ca3bb185ad
commit 0791bbb9f1
5 changed files with 86 additions and 4 deletions

View File

@ -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
*/

View File

@ -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]) {

View File

@ -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) {

View File

@ -296,6 +296,21 @@
$preview = html::table($rows);
rewind($stream);
}
if(count($plugin->get_warnings())) {
$this->message .= "<br />\n".lang('Warnings').':';
foreach($plugin->get_warnings() as $record => $message) {
$this->message .= "\n$record: $message";
}
$this->message .= "<br />\n";
};
if(count($plugin->get_errors())) {
$this->message .= "<br />\n".lang('Problems during import:');
foreach($plugin->get_errors() as $record => $message) {
$this->message .= "<br />\n$record: $message";
}
if($count != $total_processed) $this->message .= "<br />\n".lang('Some records may not have been imported');
$this->message .= "<br />\n";
}
return '<h2>' . lang('Preview') . ' - ' . $plugin->get_name() . '</h2>' . $preview;
}

View File

@ -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