mirror of
https://github.com/EGroupware/egroupware.git
synced 2025-01-12 17:08:34 +01:00
Import / Export cleanup:
- Start reducing some duplication by making better use of common CSV import ancestor - Work towards a universal preview, regardless of file type
This commit is contained in:
parent
02e72dc15c
commit
2b96aff758
@ -28,6 +28,7 @@ class addressbook_egw_record implements importexport_iface_egw_record
|
||||
'date-time' => array('modified','created','last_event','next_event'),
|
||||
'date' => array('bday'),
|
||||
'select-cat' => array('cat_id'),
|
||||
'select' => array('tid')
|
||||
);
|
||||
|
||||
|
||||
|
@ -14,48 +14,14 @@
|
||||
/**
|
||||
* class import_csv for addressbook
|
||||
*/
|
||||
class addressbook_import_contacts_csv implements importexport_iface_import_plugin {
|
||||
|
||||
private static $plugin_options = array(
|
||||
'fieldsep', // char
|
||||
'charset', // string
|
||||
'contact_owner', // int
|
||||
'update_cats', // string {override|add} overides record
|
||||
// with cat(s) from csv OR add the cat from
|
||||
// csv file to exeisting cat(s) of record
|
||||
'num_header_lines', // int number of header lines
|
||||
'field_conversion', // array( $csv_col_num => conversion)
|
||||
'field_mapping', // array( $csv_col_num => adb_filed)
|
||||
'conditions', /* => array containing condition arrays:
|
||||
'type' => exists, // exists
|
||||
'string' => '#kundennummer',
|
||||
'true' => array(
|
||||
'action' => update,
|
||||
'last' => true,
|
||||
),
|
||||
'false' => array(
|
||||
'action' => insert,
|
||||
'last' => true,
|
||||
),*/
|
||||
|
||||
);
|
||||
|
||||
/**
|
||||
* actions wich could be done to data entries
|
||||
*/
|
||||
protected static $actions = array( 'none', 'update', 'insert', 'delete', );
|
||||
class addressbook_import_contacts_csv extends importexport_basic_import_csv {
|
||||
|
||||
/**
|
||||
* conditions for actions
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $conditions = array( 'exists', 'greater', 'greater or equal', );
|
||||
|
||||
/**
|
||||
* @var definition
|
||||
*/
|
||||
private $definition;
|
||||
protected static $conditions = array( 'exists' );
|
||||
|
||||
/**
|
||||
* @var bocontacts
|
||||
@ -67,35 +33,6 @@ class addressbook_import_contacts_csv implements importexport_iface_import_plugi
|
||||
*/
|
||||
protected $tracking;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $dry_run = false;
|
||||
|
||||
/**
|
||||
* @var bool is current user admin?
|
||||
*/
|
||||
private $is_admin = false;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $user = null;
|
||||
|
||||
/**
|
||||
* List of import warnings
|
||||
*/
|
||||
protected $warnings = array();
|
||||
|
||||
/**
|
||||
* List of import errors
|
||||
*/
|
||||
protected $errors = array();
|
||||
|
||||
/**
|
||||
* List of actions, and how many times that action was taken
|
||||
*/
|
||||
protected $results = array();
|
||||
|
||||
/**
|
||||
* imports entries according to given definition object.
|
||||
@ -103,23 +40,7 @@ class addressbook_import_contacts_csv implements importexport_iface_import_plugi
|
||||
* @param string $_charset
|
||||
* @param definition $_definition
|
||||
*/
|
||||
public function import( $_stream, importexport_definition $_definition ) {
|
||||
$import_csv = new importexport_import_csv( $_stream, array(
|
||||
'fieldsep' => $_definition->plugin_options['fieldsep'],
|
||||
'charset' => $_definition->plugin_options['charset'],
|
||||
));
|
||||
|
||||
$this->definition = $_definition;
|
||||
|
||||
// user, is admin ?
|
||||
$this->is_admin = isset( $GLOBALS['egw_info']['user']['apps']['admin'] ) && $GLOBALS['egw_info']['user']['apps']['admin'];
|
||||
$this->user = $GLOBALS['egw_info']['user']['account_id'];
|
||||
|
||||
// dry run?
|
||||
$this->dry_run = isset( $_definition->plugin_options['dry_run'] ) ? $_definition->plugin_options['dry_run'] : false;
|
||||
|
||||
// Needed for categories to work right
|
||||
$GLOBALS['egw_info']['flags']['currentapp'] = 'addressbook';
|
||||
public function init(importexport_definition &$_definition ) {
|
||||
|
||||
// fetch the addressbook bo
|
||||
$this->bocontacts = new addressbook_bo();
|
||||
@ -127,166 +48,157 @@ class addressbook_import_contacts_csv implements importexport_iface_import_plugi
|
||||
// Get the tracker for changes
|
||||
$this->tracking = new addressbook_tracking($this->bocontacts);
|
||||
|
||||
// set FieldMapping.
|
||||
$import_csv->mapping = $_definition->plugin_options['field_mapping'];
|
||||
|
||||
// set FieldConversion
|
||||
$import_csv->conversion = $_definition->plugin_options['field_conversion'];
|
||||
|
||||
//check if file has a header lines
|
||||
if ( isset( $_definition->plugin_options['num_header_lines'] ) && $_definition->plugin_options['num_header_lines'] > 0) {
|
||||
$import_csv->skip_records($_definition->plugin_options['num_header_lines']);
|
||||
} elseif(isset($_definition->plugin_options['has_header_line']) && $_definition->plugin_options['has_header_line']) {
|
||||
// First method is preferred
|
||||
$import_csv->skip_records(1);
|
||||
$this->lookups = array(
|
||||
'tid' => array('n'=>'contact')
|
||||
);
|
||||
foreach($this->bocontacts->content_types as $tid => $data)
|
||||
{
|
||||
$this->lookups['tid'][$tid] = $data['name'];
|
||||
}
|
||||
|
||||
$_lookups = array();
|
||||
|
||||
|
||||
// set contact owner
|
||||
|
||||
$contact_owner = isset( $_definition->plugin_options['contact_owner'] ) ?
|
||||
$_definition->plugin_options['contact_owner'] : $this->user;
|
||||
|
||||
// Import into importer's personal addressbook
|
||||
if($contact_owner == 'personal')
|
||||
{
|
||||
$contact_owner = $this->user;
|
||||
}
|
||||
$this->user = $contact_owner;
|
||||
}
|
||||
|
||||
// Start counting successes
|
||||
$count = 0;
|
||||
$this->results = array();
|
||||
/**
|
||||
* Import a single record
|
||||
*
|
||||
* You don't need to worry about mappings or translations, they've been done already.
|
||||
* You do need to handle the conditions and the actions taken.
|
||||
*
|
||||
* Updates the count of actions taken
|
||||
*
|
||||
* @return boolean success
|
||||
*/
|
||||
protected function import_record(importexport_iface_egw_record &$record, &$import_csv)
|
||||
{
|
||||
|
||||
// Failures
|
||||
$this->errors = array();
|
||||
|
||||
while ( $record = $import_csv->get_record() ) {
|
||||
$success = false;
|
||||
|
||||
// don't import empty contacts
|
||||
if( count( array_unique( $record ) ) < 2 ) continue;
|
||||
|
||||
$warning = importexport_import_csv::convert($record, addressbook_egw_record::$types, 'addressbook', $_lookups, $_definition->plugin_options['convert']);
|
||||
if($warning) $this->warnings[$import_csv->get_current_position()] = $warning;
|
||||
|
||||
// Set owner, unless it's supposed to come from CSV file
|
||||
if($_definition->plugin_options['owner_from_csv'] && $record['owner']) {
|
||||
if(!is_numeric($record['owner'])) {
|
||||
// Automatically handle text owner without explicit translation
|
||||
$new_owner = importexport_helper_functions::account_name2id($record['owner']);
|
||||
if($new_owner == '') {
|
||||
$this->errors[$import_csv->get_current_position()] = lang(
|
||||
'Unable to convert "%1" to account ID. Using plugin setting (%2) for owner.',
|
||||
$record['owner'],
|
||||
common::grab_owner_name($contact_owner)
|
||||
);
|
||||
$record['owner'] = $contact_owner;
|
||||
} else {
|
||||
$record['owner'] = $new_owner;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$record['owner'] = $contact_owner;
|
||||
}
|
||||
|
||||
// Check that owner (addressbook) is allowed
|
||||
if(!array_key_exists($record['owner'], $this->bocontacts->get_addressbooks()))
|
||||
{
|
||||
$this->errors[$import_csv->get_current_position()] = lang("Unable to import into %1, using %2",
|
||||
common::grab_owner_name($record['owner']),
|
||||
common::grab_owner_name($this->user)
|
||||
);
|
||||
$record['owner'] = $this->user;
|
||||
}
|
||||
|
||||
// Do not allow owner == 0 (accounts) without an account_id
|
||||
// It causes the contact to be filed as an account, and can't delete
|
||||
if(!$record['owner'] && !$record['account_id'])
|
||||
{
|
||||
$record['owner'] = $this->user;
|
||||
}
|
||||
|
||||
// Also handle categories in their own field
|
||||
$more_categories = array();
|
||||
foreach($_definition->plugin_options['field_mapping'] as $number => $field_name) {
|
||||
if(!array_key_exists($field_name, $record) ||
|
||||
substr($field_name,0,3) != 'cat' || !$record[$field_name] || $field_name == 'cat_id') continue;
|
||||
list($cat, $cat_id) = explode('-', $field_name);
|
||||
if(is_numeric($record[$field_name]) && $record[$field_name] != 1) {
|
||||
// Column has a single category ID
|
||||
$more_categories[] = $record[$field_name];
|
||||
} elseif($record[$field_name] == '1' ||
|
||||
(!is_numeric($record[$field_name]) && strtolower($record[$field_name]) == strtolower(lang('Yes')))) {
|
||||
// Each category got its own column. '1' is the database value, lang('yes') is the human value
|
||||
$more_categories[] = $cat_id;
|
||||
// Set owner, unless it's supposed to come from CSV file
|
||||
if($this->definition->plugin_options['owner_from_csv'] && $record->owner) {
|
||||
if(!is_numeric($record->owner)) {
|
||||
// Automatically handle text owner without explicit translation
|
||||
$new_owner = importexport_helper_functions::account_name2id($record->owner);
|
||||
if($new_owner == '') {
|
||||
$this->errors[$import_csv->get_current_position()] = lang(
|
||||
'Unable to convert "%1" to account ID. Using plugin setting (%2) for owner.',
|
||||
$record->owner,
|
||||
common::grab_owner_name($this->user)
|
||||
);
|
||||
$record->owner = $this->user;
|
||||
} else {
|
||||
// Text categories
|
||||
$more_categories = array_merge($more_categories, importexport_helper_functions::cat_name2id(is_array($record[$field_name]) ? $record[$field_name] : explode(',',$record[$field_name]), $cat_id));
|
||||
$record->owner = $new_owner;
|
||||
}
|
||||
}
|
||||
if(count($more_categories) > 0) $record['cat_id'] = array_merge(is_array($record['cat_id']) ? $record['cat_id'] : explode(',',$record['cat_id']), $more_categories);
|
||||
|
||||
// Private set but missing causes hidden entries
|
||||
if(array_key_exists('private', $record) && (!isset($record['private']) || $record['private'] == '')) unset($record['private']);
|
||||
|
||||
// Format birthday as backend requires - converter should give timestamp
|
||||
if($record['bday'] && is_numeric($record['bday']))
|
||||
{
|
||||
$time = new egw_time($record['bday']);
|
||||
$record['bday'] = $time->format('Y-m-d');
|
||||
}
|
||||
|
||||
if ( $_definition->plugin_options['conditions'] ) {
|
||||
foreach ( $_definition->plugin_options['conditions'] as $condition ) {
|
||||
$contacts = array();
|
||||
switch ( $condition['type'] ) {
|
||||
// exists
|
||||
case 'exists' :
|
||||
if($record[$condition['string']]) {
|
||||
$searchcondition = array( $condition['string'] => $record[$condition['string']]);
|
||||
// if we use account_id for the condition, we need to set the owner for filtering, as this
|
||||
// enables addressbook_so to decide what backend is to be used
|
||||
if ($condition['string']=='account_id') $searchcondition['owner']=0;
|
||||
$contacts = $this->bocontacts->search(
|
||||
//array( $condition['string'] => $record[$condition['string']],),
|
||||
'',
|
||||
$_definition->plugin_options['update_cats'] == 'add' ? false : true,
|
||||
'', '', '', false, 'AND', false,
|
||||
$searchcondition
|
||||
);
|
||||
}
|
||||
if ( is_array( $contacts ) && count( array_keys( $contacts ) ) >= 1 ) {
|
||||
// apply action to all contacts matching this exists condition
|
||||
$action = $condition['true'];
|
||||
foreach ( (array)$contacts as $contact ) {
|
||||
$record['id'] = $contact['id'];
|
||||
if ( $_definition->plugin_options['update_cats'] == 'add' ) {
|
||||
if ( !is_array( $contact['cat_id'] ) ) $contact['cat_id'] = explode( ',', $contact['cat_id'] );
|
||||
if ( !is_array( $record['cat_id'] ) ) $record['cat_id'] = explode( ',', $record['cat_id'] );
|
||||
$record['cat_id'] = implode( ',', array_unique( array_merge( $record['cat_id'], $contact['cat_id'] ) ) );
|
||||
}
|
||||
$success = $this->action( $action['action'], $record, $import_csv->get_current_position() );
|
||||
}
|
||||
} else {
|
||||
$action = $condition['false'];
|
||||
$success = ($this->action( $action['action'], $record, $import_csv->get_current_position() ));
|
||||
}
|
||||
break;
|
||||
|
||||
// not supported action
|
||||
default :
|
||||
die('condition / action not supported!!!');
|
||||
break;
|
||||
}
|
||||
if ($action['stop']) break;
|
||||
}
|
||||
} else {
|
||||
// unconditional insert
|
||||
$success = $this->action( 'insert', $record, $import_csv->get_current_position() );
|
||||
}
|
||||
$count++;
|
||||
} else {
|
||||
$record->owner = $this->user;
|
||||
}
|
||||
return $count;
|
||||
|
||||
// Check that owner (addressbook) is allowed
|
||||
if(!array_key_exists($record->owner, $this->bocontacts->get_addressbooks()))
|
||||
{
|
||||
$this->errors[$import_csv->get_current_position()] = lang("Unable to import into %1, using %2",
|
||||
common::grab_owner_name($record->owner),
|
||||
common::grab_owner_name($this->user)
|
||||
);
|
||||
$record->owner = $this->user;
|
||||
}
|
||||
|
||||
// Do not allow owner == 0 (accounts) without an account_id
|
||||
// It causes the contact to be filed as an account, and can't delete
|
||||
if(!$record->owner && !$record->account_id)
|
||||
{
|
||||
$record->owner = $GLOBALS['egw_info']['user']['account_id'];
|
||||
}
|
||||
|
||||
// Also handle categories in their own field
|
||||
$record_array = $record->get_record_array();
|
||||
$more_categories = array();
|
||||
foreach($this->definition->plugin_options['field_mapping'] as $number => $field_name) {
|
||||
if(!array_key_exists($field_name, $record_array) ||
|
||||
substr($field_name,0,3) != 'cat' || !$record->$field_name || $field_name == 'cat_id') continue;
|
||||
list($cat, $cat_id) = explode('-', $field_name);
|
||||
if(is_numeric($record->$field_name) && $record->$field_name != 1) {
|
||||
// Column has a single category ID
|
||||
$more_categories[] = $record->$field_name;
|
||||
} elseif($record->$field_name == '1' ||
|
||||
(!is_numeric($record->$field_name) && strtolower($record->$field_name) == strtolower(lang('Yes')))) {
|
||||
// Each category got its own column. '1' is the database value, lang('yes') is the human value
|
||||
$more_categories[] = $cat_id;
|
||||
} else {
|
||||
// Text categories
|
||||
$more_categories = array_merge($more_categories, importexport_helper_functions::cat_name2id(is_array($record->$field_name) ? $record->$field_name : explode(',',$record->$field_name), $cat_id));
|
||||
}
|
||||
}
|
||||
if(count($more_categories) > 0) $record->cat_id = array_merge(is_array($record->cat_id) ? $record->cat_id : explode(',',$record->cat_id), $more_categories);
|
||||
|
||||
// Private set but missing causes hidden entries
|
||||
if(array_key_exists('private', $record_array) && (!isset($record_array['private']) || $record_array['private'] == '')) unset($record->private);
|
||||
|
||||
// Format birthday as backend requires - converter should give timestamp
|
||||
if(isset($record->bday) && is_numeric($record->bday))
|
||||
{
|
||||
$time = new egw_time($record->bday);
|
||||
$record->bday = $time->format('Y-m-d');
|
||||
}
|
||||
|
||||
if ( $this->definition->plugin_options['conditions'] ) {
|
||||
foreach ( $this->definition->plugin_options['conditions'] as $condition ) {
|
||||
$contacts = array();
|
||||
switch ( $condition['type'] ) {
|
||||
// exists
|
||||
case 'exists' :
|
||||
if($record_array[$condition['string']]) {
|
||||
$searchcondition = array( $condition['string'] => $record_array[$condition['string']]);
|
||||
// if we use account_id for the condition, we need to set the owner for filtering, as this
|
||||
// enables addressbook_so to decide what backend is to be used
|
||||
if ($condition['string']=='account_id') $searchcondition['owner']=0;
|
||||
$contacts = $this->bocontacts->search(
|
||||
//array( $condition['string'] => $record[$condition['string']],),
|
||||
'',
|
||||
$this->definition->plugin_options['update_cats'] == 'add' ? false : true,
|
||||
'', '', '', false, 'AND', false,
|
||||
$searchcondition
|
||||
);
|
||||
}
|
||||
if ( is_array( $contacts ) && count( array_keys( $contacts ) ) >= 1 ) {
|
||||
// apply action to all contacts matching this exists condition
|
||||
$action = $condition['true'];
|
||||
foreach ( (array)$contacts as $contact ) {
|
||||
$record->id = $contact['id'];
|
||||
if ( $this->definition->plugin_options['update_cats'] == 'add' ) {
|
||||
if ( !is_array( $contact['cat_id'] ) ) $contact['cat_id'] = explode( ',', $contact['cat_id'] );
|
||||
if ( !is_array( $record_array['cat_id'] ) ) $record->cat_id = explode( ',', $record->cat_id );
|
||||
$record->cat_id = implode( ',', array_unique( array_merge( $record->cat_id, $contact['cat_id'] ) ) );
|
||||
}
|
||||
$success = $this->action( $action['action'], $record->get_record_array(), $import_csv->get_current_position() );
|
||||
}
|
||||
} else {
|
||||
$action = $condition['false'];
|
||||
$success = ($this->action( $action['action'], $record->get_record_array(), $import_csv->get_current_position() ));
|
||||
}
|
||||
break;
|
||||
|
||||
// not supported action
|
||||
default :
|
||||
die('condition / action not supported!!!');
|
||||
break;
|
||||
}
|
||||
if ($action['stop']) break;
|
||||
}
|
||||
} else {
|
||||
// unconditional insert
|
||||
$success = $this->action( 'insert', $record->get_record_array(), $import_csv->get_current_position() );
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -296,7 +208,7 @@ class addressbook_import_contacts_csv implements importexport_iface_import_plugi
|
||||
* @param array $_data contact data for the action
|
||||
* @return bool success or not
|
||||
*/
|
||||
private function action ( $_action, $_data, $record_num = 0 ) {
|
||||
protected function action ( $_action, Array $_data, $record_num = 0 ) {
|
||||
switch ($_action) {
|
||||
case 'none' :
|
||||
return true;
|
||||
|
@ -52,6 +52,7 @@ class addressbook_import_vcard implements importexport_iface_import_plugin {
|
||||
* @var bool
|
||||
*/
|
||||
private $dry_run = false;
|
||||
private $preview_records = array();
|
||||
|
||||
/**
|
||||
* @var bool is current user admin?
|
||||
@ -132,6 +133,16 @@ class addressbook_import_vcard implements importexport_iface_import_plugin {
|
||||
$contact = $contacts->current();
|
||||
|
||||
$this->action('insert', $contact, $this->current);
|
||||
|
||||
// Stop if we have enough records for a preview
|
||||
if($this->dry_run)
|
||||
{
|
||||
$egw_record = new addressbook_egw_record();
|
||||
$egw_record->set_record($contact);
|
||||
$this->preview_records[] = $egw_record;
|
||||
if($count >= $GLOBALS['egw_info']['user']['preferences']['common']['maxmatchs']) break;
|
||||
}
|
||||
|
||||
$count++;
|
||||
$contacts->next();
|
||||
}
|
||||
@ -222,7 +233,7 @@ class addressbook_import_vcard implements importexport_iface_import_plugin {
|
||||
}
|
||||
|
||||
if ( $this->dry_run ) {
|
||||
print_r($_data);
|
||||
//print_r($_data);
|
||||
$this->results[$_action]++;
|
||||
return true;
|
||||
} else {
|
||||
@ -240,6 +251,38 @@ class addressbook_import_vcard implements importexport_iface_import_plugin {
|
||||
}
|
||||
}
|
||||
|
||||
public function preview( $_stream, importexport_definition $_definition )
|
||||
{
|
||||
$rows = array('h1'=>array(),'f1'=>array(),'.h1'=>'class=th');
|
||||
|
||||
// Set this so plugin doesn't do any data changes
|
||||
$_definition->plugin_options = (array)$_definition->plugin_options + array('dry_run' => true);
|
||||
|
||||
$this->import($_stream, $_definition);
|
||||
rewind($_stream);
|
||||
|
||||
// Get field labels
|
||||
$rows['h1'] = $labels = $this->bocontacts->contact_fields;
|
||||
|
||||
$record_class = get_class($this->preview_records[0]);
|
||||
|
||||
foreach($this->preview_records as $i => $record)
|
||||
{
|
||||
// Convert to human-friendly
|
||||
importexport_export_csv::convert($record,$record_class::$types,$_definition->application);
|
||||
$record = $record->get_record_array();
|
||||
$row = array();
|
||||
foreach($labels as $field => $label)
|
||||
{
|
||||
$row[$field] = $record[$field];
|
||||
unset($record[$field]);
|
||||
}
|
||||
$row += $record;
|
||||
$rows[] = $row;
|
||||
}
|
||||
return html::table($rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* returns translated name of plugin
|
||||
*
|
||||
|
@ -11,17 +11,17 @@
|
||||
|
||||
/**
|
||||
* A basic CSV import plugin.
|
||||
*
|
||||
* You should extend this class to implement the various bits, but combined with the basic wizard
|
||||
*
|
||||
* You should extend this class to implement the various bits, but combined with the basic wizard
|
||||
* should get you started on building a CSV plugin for an application fairly quickly.
|
||||
*
|
||||
*
|
||||
*/
|
||||
abstract class importexport_basic_import_csv implements importexport_iface_import_plugin {
|
||||
|
||||
protected static $plugin_options = array(
|
||||
'fieldsep', // char
|
||||
'charset', // string
|
||||
'contact_owner', // int
|
||||
'record_owner', // int
|
||||
'update_cats', // string {override|add} overides record
|
||||
// with cat(s) from csv OR add the cat from
|
||||
// csv file to exeisting cat(s) of record
|
||||
@ -67,6 +67,11 @@ abstract class importexport_basic_import_csv implements importexport_iface_impor
|
||||
*/
|
||||
protected $dry_run = false;
|
||||
|
||||
/**
|
||||
* If doing a dry_run, instead of altering the DB, store the records here
|
||||
*/
|
||||
protected $preview_records = array();
|
||||
|
||||
/**
|
||||
* @var bool is current user admin?
|
||||
*/
|
||||
@ -122,10 +127,6 @@ abstract class importexport_basic_import_csv implements importexport_iface_impor
|
||||
$import_csv->skip_records(1);
|
||||
}
|
||||
|
||||
// set eventOwner
|
||||
$_definition->plugin_options['contact_owner'] = isset( $_definition->plugin_options['contact_owner'] ) ?
|
||||
$_definition->plugin_options['contact_owner'] : $this->user;
|
||||
|
||||
// Start counting successes
|
||||
$count = 0;
|
||||
$this->results = array();
|
||||
@ -134,28 +135,59 @@ abstract class importexport_basic_import_csv implements importexport_iface_impor
|
||||
$this->warnings = array();
|
||||
$this->errors = array();
|
||||
|
||||
// Record class name
|
||||
$app = $_definition->application;
|
||||
$record_class = isset(static::$record_class) ? static::$record_class : "{$app}_egw_record";
|
||||
|
||||
// Needed for categories to work right
|
||||
$GLOBALS['egw_info']['flags']['currentapp'] = $app;
|
||||
|
||||
$this->init($_definition);
|
||||
|
||||
while ( $record = $import_csv->get_record() ) {
|
||||
$success = false;
|
||||
|
||||
// don't import empty records
|
||||
if( count( array_unique( $record ) ) < 2 ) continue;
|
||||
|
||||
$record['owner'] = $this->_definition->plugin_options['contact_owner'];
|
||||
|
||||
$success = $this->import_record($record, $import_csv);
|
||||
$warning = importexport_import_csv::convert($record, $record_class::$types, $app, $this->lookups, $_definition->plugin_options['convert']);
|
||||
if($warning) $this->warnings[$import_csv->get_current_position()] = $warning;
|
||||
|
||||
$egw_record = new $record_class();
|
||||
$egw_record->set_record($record);
|
||||
$success = $this->import_record($egw_record, $import_csv);
|
||||
|
||||
if($success) $count++;
|
||||
|
||||
// Stop if we have enough records for a preview
|
||||
if($this->dry_run)
|
||||
{
|
||||
$this->preview_records[] = $egw_record;
|
||||
if($import_csv->get_current_position() >= $GLOBALS['egw_info']['user']['preferences']['common']['maxmatchs']) break;
|
||||
}
|
||||
}
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Import a single record
|
||||
*
|
||||
* You don't need to worry about mappings or translations, they've been done already.
|
||||
* You do need to handle the conditions and the actions taken.
|
||||
* Stub to hook into import initialization - set lookups, etc.
|
||||
*/
|
||||
protected function init(importexport_definition &$definition)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
*Import a single record
|
||||
*
|
||||
* You don't need to worry about mappings or translations, they've been done already.
|
||||
* You do need to handle the conditions and the actions taken.
|
||||
*
|
||||
* Updates the count of actions taken
|
||||
*
|
||||
* @return boolean success
|
||||
*/
|
||||
protected abstract function import_record(&$record, &$import_csv);
|
||||
/* Example stub:
|
||||
protected function import_record(importexport_iface_egw_record &$record, &$import_csv)
|
||||
{
|
||||
if ( $this->_definition->plugin_options['conditions'] ) {
|
||||
foreach ( $this->_definition->plugin_options['conditions'] as $condition ) {
|
||||
@ -163,12 +195,19 @@ abstract class importexport_basic_import_csv implements importexport_iface_impor
|
||||
// exists
|
||||
case 'exists' :
|
||||
// Check for that record
|
||||
$result = $this->exists($record, array($contition['string']), $matches);
|
||||
if($result)
|
||||
{
|
||||
// Apply true action to any matching records found
|
||||
$action = $condition['true'];
|
||||
$success = ($this->action( $action['action'], $record, $import_csv->get_current_position() ));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Apply false action if no matching records found
|
||||
$action = $condition['false'];
|
||||
$success = ($this->action( $action['action'], $record, $import_csv->get_current_position() ));
|
||||
}
|
||||
break;
|
||||
|
||||
// not supported action
|
||||
@ -176,7 +215,7 @@ abstract class importexport_basic_import_csv implements importexport_iface_impor
|
||||
die('condition / action not supported!!!');
|
||||
break;
|
||||
}
|
||||
if ($action['last']) break;
|
||||
if ($action['stop']) break;
|
||||
}
|
||||
} else {
|
||||
// unconditional insert
|
||||
@ -185,11 +224,23 @@ abstract class importexport_basic_import_csv implements importexport_iface_impor
|
||||
|
||||
return $success;
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Search for matching records, based on the the given condition
|
||||
*
|
||||
* @param record
|
||||
* @param condition array = array('string' => field name)
|
||||
* @param matches - On return, will be filled with matching records
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
protected function exists(iface_egw_record &$record, Array &$condition, &$matches = array())
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* perform the required action
|
||||
*
|
||||
*
|
||||
* Make sure you record any errors you encounter here:
|
||||
* $this->errors[$record_num] = error message;
|
||||
*
|
||||
@ -200,6 +251,59 @@ abstract class importexport_basic_import_csv implements importexport_iface_impor
|
||||
*/
|
||||
protected abstract function action ( $_action, Array $_data, $record_num = 0 );
|
||||
|
||||
/**
|
||||
* Reads entries, and presents them back as they will be understood
|
||||
* with no changes to the system.
|
||||
*
|
||||
* Uses information from the egw_record and the associated import wizard
|
||||
* to parse, normalize and export a human-friendly version of the data as
|
||||
* a HTML table.
|
||||
*
|
||||
* @param stream $stream
|
||||
* @param importexport_definition $definition
|
||||
* @return String HTML for preview
|
||||
*/
|
||||
public function preview( $stream, importexport_definition $definition )
|
||||
{
|
||||
$this->import($stream, $definition);
|
||||
rewind($stream);
|
||||
|
||||
// Set up result
|
||||
$rows = array('h1'=>array(),'f1'=>array(),'.h1'=>'class=th');
|
||||
|
||||
// Load labels for app
|
||||
$record_class = get_class($this->preview_records[0]);
|
||||
|
||||
// Get labels from wizard, if possible
|
||||
$labels = array_combine($definition->plugin_options['field_mapping'], $definition->plugin_options['field_mapping']);
|
||||
$plugin = get_called_class();
|
||||
$wizard_name = $definition->application . '_wizard_' . str_replace($definition->application . '_', '', $plugin);
|
||||
try {
|
||||
$wizard = new $wizard_name;
|
||||
$fields = $wizard->get_import_fields();
|
||||
foreach($labels as $field => &$label)
|
||||
{
|
||||
if($fields[$field]) $label = $fields[$field];
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
translation::add_app($definition->application);
|
||||
foreach($labels as $field => &$label) {
|
||||
$label = lang($label);
|
||||
}
|
||||
}
|
||||
|
||||
// Set up HTML
|
||||
$rows['h1'] = $labels;
|
||||
foreach($this->preview_records as $i => $row_data)
|
||||
{
|
||||
// Convert to human-friendly
|
||||
importexport_export_csv::convert($row_data,$record_class::$types,$definition->application,$this->lookups);
|
||||
$rows[] = $row_data->get_record_array();
|
||||
}
|
||||
|
||||
return html::table($rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* returns translated name of plugin
|
||||
*
|
||||
|
@ -13,13 +13,13 @@
|
||||
/**
|
||||
* class iface_import_plugin
|
||||
* This a the abstract interface for an import plugin of importexport
|
||||
*
|
||||
* You need to implement this class in
|
||||
* EGW_INCLUDE_ROOT/appname/inc/importexport/class.import_<type>.inc.php
|
||||
*
|
||||
* You need to implement this class in
|
||||
* EGW_INCLUDE_ROOT/appname/inc/class.<appname>_import_<type>.inc.php
|
||||
* to attend the importexport framwork with your export.
|
||||
*
|
||||
* NOTE: This is an easy interface, cause plugins live in theire own
|
||||
* space. Means that they are responsible for generating a defintion AND
|
||||
*
|
||||
* NOTE: This is an easy interface, cause plugins live in theire own
|
||||
* space. Means that they are responsible for generating a defintion AND
|
||||
* working on that definition.
|
||||
* So this interface just garanties the interaction with userinterfaces. It
|
||||
* has nothing to do with datatypes.
|
||||
@ -34,6 +34,16 @@ interface importexport_iface_import_plugin {
|
||||
* @return int number of successful imports
|
||||
*/
|
||||
public function import( $_stream, importexport_definition $_definition );
|
||||
|
||||
/**
|
||||
* Reads entries, and presents them back as they will be understood
|
||||
* with no changes to the system.
|
||||
*
|
||||
* @param stream $_stream
|
||||
* @param definition $_definition
|
||||
* @return String HTML preview
|
||||
*/
|
||||
// public function preview( $_stream, importexport_definition $_definition );
|
||||
|
||||
/**
|
||||
* returns translated name of plugin
|
||||
@ -60,7 +70,7 @@ interface importexport_iface_import_plugin {
|
||||
* return etemplate components for options.
|
||||
* @abstract We can't deal with etemplate objects here, as an uietemplate
|
||||
* objects itself are scipt orientated and not "dialog objects"
|
||||
*
|
||||
*
|
||||
* @return array (
|
||||
* name => string,
|
||||
* content => array,
|
||||
|
@ -103,7 +103,7 @@
|
||||
$this->message .= implode($check_message, "<br />\n") . "<br />\n";
|
||||
if($content['dry-run'])
|
||||
{
|
||||
echo $this->preview($file, $definition_obj);
|
||||
echo $this->preview($plugin, $file, $definition_obj);
|
||||
}
|
||||
$count = $plugin->import($file, $definition_obj);
|
||||
}
|
||||
@ -248,15 +248,14 @@
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the contents of the file for dry runs
|
||||
* Display the interpretation of the file for dry runs
|
||||
*
|
||||
* CSV files only
|
||||
*/
|
||||
protected function preview(&$_stream, &$definition_obj)
|
||||
protected function preview(importexport_iface_import_plugin &$plugin, &$stream, importexport_definition &$definition_obj)
|
||||
{
|
||||
if(!$definition_obj->plugin_options['csv_fields']) return;
|
||||
|
||||
$import_csv = new importexport_import_csv( $_stream, array(
|
||||
$preview = $plugin->preview($stream, $definition_obj);
|
||||
/*
|
||||
$import_csv = new importexport_import_csv( $stream, array(
|
||||
'fieldsep' => $definition_obj->plugin_options['fieldsep'],
|
||||
'charset' => $definition_obj->plugin_options['charset'],
|
||||
));
|
||||
@ -271,10 +270,9 @@
|
||||
$rows[$import_csv->get_current_position() <= $definition_obj->plugin_options['num_header_lines'] ? 'h1' : $row] = $row_data;
|
||||
if($import_csv->get_current_position() <= $definition_obj->plugin_options['num_header_lines']) $row--;
|
||||
}
|
||||
|
||||
// Rewind
|
||||
rewind($_stream);
|
||||
return '<h2>' . lang('Preview') . '</h2>' . html::table($rows);
|
||||
$preview = html::table($rows);
|
||||
*/
|
||||
return '<h2>' . lang('Preview') . ' - ' . $plugin->get_name() . '</h2>' . $preview;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -436,4 +436,12 @@ class importexport_wizard_basic_import_csv
|
||||
unset ($preserv['button']);
|
||||
return $this->step_templates[$content['step']];
|
||||
}
|
||||
|
||||
/**
|
||||
* Expose import fields for use elsewhere
|
||||
*/
|
||||
public function get_import_fields()
|
||||
{
|
||||
return $this->mapping_fields;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user