From 21c6a0f44786266d2a5e05c5394427074e9e1aab Mon Sep 17 00:00:00 2001 From: Nathan Gray Date: Thu, 1 Dec 2011 18:49:42 +0000 Subject: [PATCH] Add ability to create an export definition based on an existing import definition --- .../inc/class.importexport_definition.inc.php | 2 +- .../class.importexport_definitions_bo.inc.php | 68 +++++++++++++++++++ .../class.importexport_definitions_ui.inc.php | 46 ++++++++++++- 3 files changed, 114 insertions(+), 2 deletions(-) diff --git a/importexport/inc/class.importexport_definition.inc.php b/importexport/inc/class.importexport_definition.inc.php index 5b55eb3005..9f0ef58e65 100644 --- a/importexport/inc/class.importexport_definition.inc.php +++ b/importexport/inc/class.importexport_definition.inc.php @@ -67,7 +67,7 @@ class importexport_definition implements importexport_iface_egw_record { $this->user = $GLOBALS['egw_info']['user']['user_id']; $this->is_admin = $GLOBALS['egw_info']['user']['apps']['admin'] || $GLOBALS['egw_setup'] ? true : false; // compability to string identifiers - if (is_string($_identifier) && strlen($_identifier) > 3) $_identifier = $this->name2identifier($_identifier); + if (!is_numeric($_identifier) && strlen($_identifier) > 3) $_identifier = $this->name2identifier($_identifier); if ((int)$_identifier != 0) { $this->definition = $this->so_sql->read(array('definition_id' => $_identifier)); diff --git a/importexport/inc/class.importexport_definitions_bo.inc.php b/importexport/inc/class.importexport_definitions_bo.inc.php index 122600976b..3e66c99c79 100644 --- a/importexport/inc/class.importexport_definitions_bo.inc.php +++ b/importexport/inc/class.importexport_definitions_bo.inc.php @@ -73,6 +73,7 @@ class importexport_definitions_bo { $row['class'] .= 'rowNoEdit'; $ro_count++; } + $row['class'] .= ' ' . $row['type']; } $readonlys['delete_selected'] = $ro_count == count($rows); return $total; @@ -234,5 +235,72 @@ class importexport_definitions_bo { } } + /** + * Create a matching export definition from the given import definition. + * + * Sets up the field mapping as closely as possible, and sets charset, + * header, conversion, etc. using the associated export plugin. Plugin + * is determined by replacing 'import' with 'export'. + * + * It is not possible to handle some plugin options automatically, because they + * just don't have equivalents. (eg: What to do with unknown categories) + * + * @param importexport_definition $definition Import definition + * + * @return importexport_definition Export definition + */ + public static function export_from_import(importexport_definition $import) + { + // Only operates on import definitions + if($import->type != 'import') throw new egw_exception_wrong_parameter('Only import definitions'); + + // Find export plugin + $plugin = str_replace('import', 'export',$import->plugin); + $plugin_list = importexport_helper_functions::get_plugins($import->application, 'export'); + foreach($plugin_list as $appname => $type) + { + $plugins = $type['export']; + foreach($plugins as $name => $label) + { + if($plugin == $name) break; + } + if($plugin !== $name) $plugin = $name; + } + + $export = new importexport_definition(); + + // Common settings + $export->name = str_replace('import', 'export',$import->name); + if($export->name == $import->name) $export->name = $export->name . '-export'; + $test = new importexport_definition($export->name); + if($test->name) $export->name = $export->name .'-'.$GLOBALS['egw_info']['user']['account_lid']; + + $export->application = $import->application; + $export->plugin = $plugin; + $export->type = 'export'; + + // Options + $i_options = $import->plugin_options; + $e_options = array( + 'delimiter' => $i_options['fieldsep'], + 'charset' => $i_options['charset'], + 'begin_with_fieldnames' => $i_options['num_header_lines'] ? ($i_options['convert'] ? 'label' : 1) : 0, + 'convert' => $i_options['convert'] + ); + + // Mapping + foreach($i_options['field_mapping'] as $col_num => $field) + { + // Try to use heading from import file, if possible + $e_options['mapping'][$field] = $i_options['csv_fields'][$col_num] ? $i_options['csv_fields'][$col_num] : $field; + } + $export->plugin_options = $e_options; + + // Permissions + $export->owner = $import->owner; + $export->allowed_users = $import->allowed_users; + + return $export; + } } diff --git a/importexport/inc/class.importexport_definitions_ui.inc.php b/importexport/inc/class.importexport_definitions_ui.inc.php index c2514d743a..d75080f0c6 100644 --- a/importexport/inc/class.importexport_definitions_ui.inc.php +++ b/importexport/inc/class.importexport_definitions_ui.inc.php @@ -245,6 +245,15 @@ class importexport_definitions_ui 'caption' => 'Copy', 'group' => ++$group, ), + 'createexport' => array( + 'caption' => 'Create export', + 'hint' => 'Create a matching export definition based on this import definition', + 'icon' => 'export', + 'group' => $group, + 'allowOnMultiple' => false, + 'disableClass' => 'export' + ), + 'export' => array( 'caption' => 'Export', 'group' => $group, @@ -376,6 +385,34 @@ class importexport_definitions_ui } } break; + case 'createexport': + $action_msg = lang('created'); + // Should only be one selected + foreach($selected as $id) { + $definition = new importexport_definition($id); + try { + $export = $bodefinitions::export_from_import($definition); + $export->save(); + } catch (Exception $e) { + if($export) + { + try { + $export->name = $export->name.' ' . $GLOBALS['egw_info']['user']['account_lid']; + $export->save(); + } catch (Exception $ex) { + $failed++; + $msg .= lang('Duplicate name, please choose another.'); + continue; + } + } + else + { + $failed++; + } + } + $success++; + } + break; } return !$failed; } @@ -393,7 +430,14 @@ class importexport_definitions_ui { //close window } - $definition = array('name' => $_definition); + if(is_numeric($_GET['definition'])) + { + $definition = (int)$_GET['definition']; + } + else + { + $definition = array('name' => $_definition); + } $bodefinitions = new importexport_definitions_bo(); $definition = $bodefinitions->read($definition); $definition['edit'] = true;