mirror of
https://github.com/EGroupware/egroupware.git
synced 2025-06-20 18:08:02 +02: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-time' => array('modified','created','last_event','next_event'),
|
||||||
'date' => array('bday'),
|
'date' => array('bday'),
|
||||||
'select-cat' => array('cat_id'),
|
'select-cat' => array('cat_id'),
|
||||||
|
'select' => array('tid')
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
@ -14,48 +14,14 @@
|
|||||||
/**
|
/**
|
||||||
* class import_csv for addressbook
|
* class import_csv for addressbook
|
||||||
*/
|
*/
|
||||||
class addressbook_import_contacts_csv implements importexport_iface_import_plugin {
|
class addressbook_import_contacts_csv extends importexport_basic_import_csv {
|
||||||
|
|
||||||
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', );
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* conditions for actions
|
* conditions for actions
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $conditions = array( 'exists', 'greater', 'greater or equal', );
|
protected static $conditions = array( 'exists' );
|
||||||
|
|
||||||
/**
|
|
||||||
* @var definition
|
|
||||||
*/
|
|
||||||
private $definition;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bocontacts
|
* @var bocontacts
|
||||||
@ -67,35 +33,6 @@ class addressbook_import_contacts_csv implements importexport_iface_import_plugi
|
|||||||
*/
|
*/
|
||||||
protected $tracking;
|
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.
|
* 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 string $_charset
|
||||||
* @param definition $_definition
|
* @param definition $_definition
|
||||||
*/
|
*/
|
||||||
public function import( $_stream, importexport_definition $_definition ) {
|
public function init(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';
|
|
||||||
|
|
||||||
// fetch the addressbook bo
|
// fetch the addressbook bo
|
||||||
$this->bocontacts = new addressbook_bo();
|
$this->bocontacts = new addressbook_bo();
|
||||||
@ -127,130 +48,123 @@ class addressbook_import_contacts_csv implements importexport_iface_import_plugi
|
|||||||
// Get the tracker for changes
|
// Get the tracker for changes
|
||||||
$this->tracking = new addressbook_tracking($this->bocontacts);
|
$this->tracking = new addressbook_tracking($this->bocontacts);
|
||||||
|
|
||||||
// set FieldMapping.
|
$this->lookups = array(
|
||||||
$import_csv->mapping = $_definition->plugin_options['field_mapping'];
|
'tid' => array('n'=>'contact')
|
||||||
|
);
|
||||||
// set FieldConversion
|
foreach($this->bocontacts->content_types as $tid => $data)
|
||||||
$import_csv->conversion = $_definition->plugin_options['field_conversion'];
|
{
|
||||||
|
$this->lookups['tid'][$tid] = $data['name'];
|
||||||
//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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$_lookups = array();
|
|
||||||
|
|
||||||
// set contact owner
|
// set contact owner
|
||||||
|
|
||||||
$contact_owner = isset( $_definition->plugin_options['contact_owner'] ) ?
|
$contact_owner = isset( $_definition->plugin_options['contact_owner'] ) ?
|
||||||
$_definition->plugin_options['contact_owner'] : $this->user;
|
$_definition->plugin_options['contact_owner'] : $this->user;
|
||||||
|
|
||||||
// Import into importer's personal addressbook
|
// Import into importer's personal addressbook
|
||||||
if($contact_owner == 'personal')
|
if($contact_owner == 'personal')
|
||||||
{
|
{
|
||||||
$contact_owner = $this->user;
|
$contact_owner = $this->user;
|
||||||
}
|
}
|
||||||
|
$this->user = $contact_owner;
|
||||||
|
}
|
||||||
|
|
||||||
// Start counting successes
|
/**
|
||||||
$count = 0;
|
* Import a single record
|
||||||
$this->results = array();
|
*
|
||||||
|
* You don't need to worry about mappings or translations, they've been done already.
|
||||||
// Failures
|
* You do need to handle the conditions and the actions taken.
|
||||||
$this->errors = array();
|
*
|
||||||
|
* Updates the count of actions taken
|
||||||
while ( $record = $import_csv->get_record() ) {
|
*
|
||||||
$success = false;
|
* @return boolean success
|
||||||
|
*/
|
||||||
// don't import empty contacts
|
protected function import_record(importexport_iface_egw_record &$record, &$import_csv)
|
||||||
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
|
// Set owner, unless it's supposed to come from CSV file
|
||||||
if($_definition->plugin_options['owner_from_csv'] && $record['owner']) {
|
if($this->definition->plugin_options['owner_from_csv'] && $record->owner) {
|
||||||
if(!is_numeric($record['owner'])) {
|
if(!is_numeric($record->owner)) {
|
||||||
// Automatically handle text owner without explicit translation
|
// Automatically handle text owner without explicit translation
|
||||||
$new_owner = importexport_helper_functions::account_name2id($record['owner']);
|
$new_owner = importexport_helper_functions::account_name2id($record->owner);
|
||||||
if($new_owner == '') {
|
if($new_owner == '') {
|
||||||
$this->errors[$import_csv->get_current_position()] = lang(
|
$this->errors[$import_csv->get_current_position()] = lang(
|
||||||
'Unable to convert "%1" to account ID. Using plugin setting (%2) for owner.',
|
'Unable to convert "%1" to account ID. Using plugin setting (%2) for owner.',
|
||||||
$record['owner'],
|
$record->owner,
|
||||||
common::grab_owner_name($contact_owner)
|
common::grab_owner_name($this->user)
|
||||||
);
|
);
|
||||||
$record['owner'] = $contact_owner;
|
$record->owner = $this->user;
|
||||||
} else {
|
} else {
|
||||||
$record['owner'] = $new_owner;
|
$record->owner = $new_owner;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$record['owner'] = $contact_owner;
|
$record->owner = $this->user;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that owner (addressbook) is allowed
|
// Check that owner (addressbook) is allowed
|
||||||
if(!array_key_exists($record['owner'], $this->bocontacts->get_addressbooks()))
|
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",
|
$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($record->owner),
|
||||||
common::grab_owner_name($this->user)
|
common::grab_owner_name($this->user)
|
||||||
);
|
);
|
||||||
$record['owner'] = $this->user;
|
$record->owner = $this->user;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do not allow owner == 0 (accounts) without an account_id
|
// Do not allow owner == 0 (accounts) without an account_id
|
||||||
// It causes the contact to be filed as an account, and can't delete
|
// It causes the contact to be filed as an account, and can't delete
|
||||||
if(!$record['owner'] && !$record['account_id'])
|
if(!$record->owner && !$record->account_id)
|
||||||
{
|
{
|
||||||
$record['owner'] = $this->user;
|
$record->owner = $GLOBALS['egw_info']['user']['account_id'];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also handle categories in their own field
|
// Also handle categories in their own field
|
||||||
|
$record_array = $record->get_record_array();
|
||||||
$more_categories = array();
|
$more_categories = array();
|
||||||
foreach($_definition->plugin_options['field_mapping'] as $number => $field_name) {
|
foreach($this->definition->plugin_options['field_mapping'] as $number => $field_name) {
|
||||||
if(!array_key_exists($field_name, $record) ||
|
if(!array_key_exists($field_name, $record_array) ||
|
||||||
substr($field_name,0,3) != 'cat' || !$record[$field_name] || $field_name == 'cat_id') continue;
|
substr($field_name,0,3) != 'cat' || !$record->$field_name || $field_name == 'cat_id') continue;
|
||||||
list($cat, $cat_id) = explode('-', $field_name);
|
list($cat, $cat_id) = explode('-', $field_name);
|
||||||
if(is_numeric($record[$field_name]) && $record[$field_name] != 1) {
|
if(is_numeric($record->$field_name) && $record->$field_name != 1) {
|
||||||
// Column has a single category ID
|
// Column has a single category ID
|
||||||
$more_categories[] = $record[$field_name];
|
$more_categories[] = $record->$field_name;
|
||||||
} elseif($record[$field_name] == '1' ||
|
} elseif($record->$field_name == '1' ||
|
||||||
(!is_numeric($record[$field_name]) && strtolower($record[$field_name]) == strtolower(lang('Yes')))) {
|
(!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
|
// Each category got its own column. '1' is the database value, lang('yes') is the human value
|
||||||
$more_categories[] = $cat_id;
|
$more_categories[] = $cat_id;
|
||||||
} else {
|
} else {
|
||||||
// Text categories
|
// 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));
|
$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);
|
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
|
// Private set but missing causes hidden entries
|
||||||
if(array_key_exists('private', $record) && (!isset($record['private']) || $record['private'] == '')) unset($record['private']);
|
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
|
// Format birthday as backend requires - converter should give timestamp
|
||||||
if($record['bday'] && is_numeric($record['bday']))
|
if(isset($record->bday) && is_numeric($record->bday))
|
||||||
{
|
{
|
||||||
$time = new egw_time($record['bday']);
|
$time = new egw_time($record->bday);
|
||||||
$record['bday'] = $time->format('Y-m-d');
|
$record->bday = $time->format('Y-m-d');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $_definition->plugin_options['conditions'] ) {
|
if ( $this->definition->plugin_options['conditions'] ) {
|
||||||
foreach ( $_definition->plugin_options['conditions'] as $condition ) {
|
foreach ( $this->definition->plugin_options['conditions'] as $condition ) {
|
||||||
$contacts = array();
|
$contacts = array();
|
||||||
switch ( $condition['type'] ) {
|
switch ( $condition['type'] ) {
|
||||||
// exists
|
// exists
|
||||||
case 'exists' :
|
case 'exists' :
|
||||||
if($record[$condition['string']]) {
|
if($record_array[$condition['string']]) {
|
||||||
$searchcondition = array( $condition['string'] => $record[$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
|
// 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
|
// enables addressbook_so to decide what backend is to be used
|
||||||
if ($condition['string']=='account_id') $searchcondition['owner']=0;
|
if ($condition['string']=='account_id') $searchcondition['owner']=0;
|
||||||
$contacts = $this->bocontacts->search(
|
$contacts = $this->bocontacts->search(
|
||||||
//array( $condition['string'] => $record[$condition['string']],),
|
//array( $condition['string'] => $record[$condition['string']],),
|
||||||
'',
|
'',
|
||||||
$_definition->plugin_options['update_cats'] == 'add' ? false : true,
|
$this->definition->plugin_options['update_cats'] == 'add' ? false : true,
|
||||||
'', '', '', false, 'AND', false,
|
'', '', '', false, 'AND', false,
|
||||||
$searchcondition
|
$searchcondition
|
||||||
);
|
);
|
||||||
@ -259,17 +173,17 @@ class addressbook_import_contacts_csv implements importexport_iface_import_plugi
|
|||||||
// apply action to all contacts matching this exists condition
|
// apply action to all contacts matching this exists condition
|
||||||
$action = $condition['true'];
|
$action = $condition['true'];
|
||||||
foreach ( (array)$contacts as $contact ) {
|
foreach ( (array)$contacts as $contact ) {
|
||||||
$record['id'] = $contact['id'];
|
$record->id = $contact['id'];
|
||||||
if ( $_definition->plugin_options['update_cats'] == 'add' ) {
|
if ( $this->definition->plugin_options['update_cats'] == 'add' ) {
|
||||||
if ( !is_array( $contact['cat_id'] ) ) $contact['cat_id'] = explode( ',', $contact['cat_id'] );
|
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'] );
|
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'] ) ) );
|
$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() );
|
$success = $this->action( $action['action'], $record->get_record_array(), $import_csv->get_current_position() );
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$action = $condition['false'];
|
$action = $condition['false'];
|
||||||
$success = ($this->action( $action['action'], $record, $import_csv->get_current_position() ));
|
$success = ($this->action( $action['action'], $record->get_record_array(), $import_csv->get_current_position() ));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -282,11 +196,9 @@ class addressbook_import_contacts_csv implements importexport_iface_import_plugi
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// unconditional insert
|
// unconditional insert
|
||||||
$success = $this->action( 'insert', $record, $import_csv->get_current_position() );
|
$success = $this->action( 'insert', $record->get_record_array(), $import_csv->get_current_position() );
|
||||||
}
|
}
|
||||||
$count++;
|
return $success;
|
||||||
}
|
|
||||||
return $count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -296,7 +208,7 @@ class addressbook_import_contacts_csv implements importexport_iface_import_plugi
|
|||||||
* @param array $_data contact data for the action
|
* @param array $_data contact data for the action
|
||||||
* @return bool success or not
|
* @return bool success or not
|
||||||
*/
|
*/
|
||||||
private function action ( $_action, $_data, $record_num = 0 ) {
|
protected function action ( $_action, Array $_data, $record_num = 0 ) {
|
||||||
switch ($_action) {
|
switch ($_action) {
|
||||||
case 'none' :
|
case 'none' :
|
||||||
return true;
|
return true;
|
||||||
|
@ -52,6 +52,7 @@ class addressbook_import_vcard implements importexport_iface_import_plugin {
|
|||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
private $dry_run = false;
|
private $dry_run = false;
|
||||||
|
private $preview_records = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool is current user admin?
|
* @var bool is current user admin?
|
||||||
@ -132,6 +133,16 @@ class addressbook_import_vcard implements importexport_iface_import_plugin {
|
|||||||
$contact = $contacts->current();
|
$contact = $contacts->current();
|
||||||
|
|
||||||
$this->action('insert', $contact, $this->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++;
|
$count++;
|
||||||
$contacts->next();
|
$contacts->next();
|
||||||
}
|
}
|
||||||
@ -222,7 +233,7 @@ class addressbook_import_vcard implements importexport_iface_import_plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ( $this->dry_run ) {
|
if ( $this->dry_run ) {
|
||||||
print_r($_data);
|
//print_r($_data);
|
||||||
$this->results[$_action]++;
|
$this->results[$_action]++;
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} 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
|
* returns translated name of plugin
|
||||||
*
|
*
|
||||||
|
@ -21,7 +21,7 @@ abstract class importexport_basic_import_csv implements importexport_iface_impor
|
|||||||
protected static $plugin_options = array(
|
protected static $plugin_options = array(
|
||||||
'fieldsep', // char
|
'fieldsep', // char
|
||||||
'charset', // string
|
'charset', // string
|
||||||
'contact_owner', // int
|
'record_owner', // int
|
||||||
'update_cats', // string {override|add} overides record
|
'update_cats', // string {override|add} overides record
|
||||||
// with cat(s) from csv OR add the cat from
|
// with cat(s) from csv OR add the cat from
|
||||||
// csv file to exeisting cat(s) of record
|
// 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;
|
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?
|
* @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);
|
$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
|
// Start counting successes
|
||||||
$count = 0;
|
$count = 0;
|
||||||
$this->results = array();
|
$this->results = array();
|
||||||
@ -134,28 +135,59 @@ abstract class importexport_basic_import_csv implements importexport_iface_impor
|
|||||||
$this->warnings = array();
|
$this->warnings = array();
|
||||||
$this->errors = 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() ) {
|
while ( $record = $import_csv->get_record() ) {
|
||||||
$success = false;
|
$success = false;
|
||||||
|
|
||||||
// don't import empty records
|
// don't import empty records
|
||||||
if( count( array_unique( $record ) ) < 2 ) continue;
|
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++;
|
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;
|
return $count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stub to hook into import initialization - set lookups, etc.
|
||||||
|
*/
|
||||||
|
protected function init(importexport_definition &$definition)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*Import a single record
|
*Import a single record
|
||||||
*
|
*
|
||||||
* You don't need to worry about mappings or translations, they've been done already.
|
* 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.
|
* 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);
|
protected function import_record(importexport_iface_egw_record &$record, &$import_csv)
|
||||||
/* Example stub:
|
|
||||||
{
|
{
|
||||||
if ( $this->_definition->plugin_options['conditions'] ) {
|
if ( $this->_definition->plugin_options['conditions'] ) {
|
||||||
foreach ( $this->_definition->plugin_options['conditions'] as $condition ) {
|
foreach ( $this->_definition->plugin_options['conditions'] as $condition ) {
|
||||||
@ -163,12 +195,19 @@ abstract class importexport_basic_import_csv implements importexport_iface_impor
|
|||||||
// exists
|
// exists
|
||||||
case 'exists' :
|
case 'exists' :
|
||||||
// Check for that record
|
// Check for that record
|
||||||
|
$result = $this->exists($record, array($contition['string']), $matches);
|
||||||
|
if($result)
|
||||||
|
{
|
||||||
// Apply true action to any matching records found
|
// Apply true action to any matching records found
|
||||||
$action = $condition['true'];
|
$action = $condition['true'];
|
||||||
$success = ($this->action( $action['action'], $record, $import_csv->get_current_position() ));
|
$success = ($this->action( $action['action'], $record, $import_csv->get_current_position() ));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
// Apply false action if no matching records found
|
// Apply false action if no matching records found
|
||||||
$action = $condition['false'];
|
$action = $condition['false'];
|
||||||
$success = ($this->action( $action['action'], $record, $import_csv->get_current_position() ));
|
$success = ($this->action( $action['action'], $record, $import_csv->get_current_position() ));
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// not supported action
|
// not supported action
|
||||||
@ -176,7 +215,7 @@ abstract class importexport_basic_import_csv implements importexport_iface_impor
|
|||||||
die('condition / action not supported!!!');
|
die('condition / action not supported!!!');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if ($action['last']) break;
|
if ($action['stop']) break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// unconditional insert
|
// unconditional insert
|
||||||
@ -185,7 +224,19 @@ abstract class importexport_basic_import_csv implements importexport_iface_impor
|
|||||||
|
|
||||||
return $success;
|
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
|
* perform the required action
|
||||||
@ -200,6 +251,59 @@ abstract class importexport_basic_import_csv implements importexport_iface_impor
|
|||||||
*/
|
*/
|
||||||
protected abstract function action ( $_action, Array $_data, $record_num = 0 );
|
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
|
* returns translated name of plugin
|
||||||
*
|
*
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
* This a the abstract interface for an import plugin of importexport
|
* This a the abstract interface for an import plugin of importexport
|
||||||
*
|
*
|
||||||
* You need to implement this class in
|
* You need to implement this class in
|
||||||
* EGW_INCLUDE_ROOT/appname/inc/importexport/class.import_<type>.inc.php
|
* EGW_INCLUDE_ROOT/appname/inc/class.<appname>_import_<type>.inc.php
|
||||||
* to attend the importexport framwork with your export.
|
* to attend the importexport framwork with your export.
|
||||||
*
|
*
|
||||||
* NOTE: This is an easy interface, cause plugins live in theire own
|
* NOTE: This is an easy interface, cause plugins live in theire own
|
||||||
@ -35,6 +35,16 @@ interface importexport_iface_import_plugin {
|
|||||||
*/
|
*/
|
||||||
public function import( $_stream, importexport_definition $_definition );
|
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
|
* returns translated name of plugin
|
||||||
*
|
*
|
||||||
|
@ -103,7 +103,7 @@
|
|||||||
$this->message .= implode($check_message, "<br />\n") . "<br />\n";
|
$this->message .= implode($check_message, "<br />\n") . "<br />\n";
|
||||||
if($content['dry-run'])
|
if($content['dry-run'])
|
||||||
{
|
{
|
||||||
echo $this->preview($file, $definition_obj);
|
echo $this->preview($plugin, $file, $definition_obj);
|
||||||
}
|
}
|
||||||
$count = $plugin->import($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;
|
$preview = $plugin->preview($stream, $definition_obj);
|
||||||
|
/*
|
||||||
$import_csv = new importexport_import_csv( $_stream, array(
|
$import_csv = new importexport_import_csv( $stream, array(
|
||||||
'fieldsep' => $definition_obj->plugin_options['fieldsep'],
|
'fieldsep' => $definition_obj->plugin_options['fieldsep'],
|
||||||
'charset' => $definition_obj->plugin_options['charset'],
|
'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;
|
$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--;
|
if($import_csv->get_current_position() <= $definition_obj->plugin_options['num_header_lines']) $row--;
|
||||||
}
|
}
|
||||||
|
$preview = html::table($rows);
|
||||||
// Rewind
|
*/
|
||||||
rewind($_stream);
|
return '<h2>' . lang('Preview') . ' - ' . $plugin->get_name() . '</h2>' . $preview;
|
||||||
return '<h2>' . lang('Preview') . '</h2>' . html::table($rows);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -436,4 +436,12 @@ class importexport_wizard_basic_import_csv
|
|||||||
unset ($preserv['button']);
|
unset ($preserv['button']);
|
||||||
return $this->step_templates[$content['step']];
|
return $this->step_templates[$content['step']];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expose import fields for use elsewhere
|
||||||
|
*/
|
||||||
|
public function get_import_fields()
|
||||||
|
{
|
||||||
|
return $this->mapping_fields;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user