Calendar CSV import improvements:

- Better recognizing participants
- Use robust username parsing, with exact matches
- Participants preview fiexed
- Error checking if end date is before start date
This commit is contained in:
Nathan Gray 2015-09-29 14:34:47 +00:00
parent 0e50f9083f
commit bebfc80259
4 changed files with 278 additions and 290 deletions

View File

@ -124,7 +124,8 @@ class calendar_egw_record implements importexport_iface_egw_record
* @return string identifier * @return string identifier
*/ */
public function save ( $_dst_identifier ) { public function save ( $_dst_identifier ) {
// Not yet implemeted
$this->identifier = $_dst_identifier;
} }
/** /**

View File

@ -2,8 +2,7 @@
/** /**
* eGroupWare * eGroupWare
* *
* This plugin is blacklisted in importexport_helper_functions. You should use iCal, * Plugin to import events from a CSV file
* but if you need to import CSV, you can remove the blacklist.
* *
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package calendar * @package calendar
@ -18,146 +17,46 @@
/** /**
* class import_csv for calendar * class import_csv for calendar
*/ */
class calendar_import_csv implements importexport_iface_import_plugin { class calendar_import_csv extends importexport_basic_import_csv {
private static $plugin_options = array(
'fieldsep', // char
'charset', // string
'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 * actions wich could be done to data entries
*/ */
protected static $actions = array( 'none', 'insert' ); protected static $actions = array( 'none', 'update', 'insert' );
/** /**
* conditions for actions * conditions for actions
* *
* @var array * @var array
*/ */
protected static $conditions = array(); protected static $conditions = array('exists');
/**
* @var definition
*/
private $definition;
/**
* @var bo
*/
private $bo;
/** /**
* For figuring out if an entry has changed * For figuring out if an entry has changed
*/ */
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 * List of import warnings
*/ */
protected $warnings = array(); protected $warnings = array();
/** /**
* List of import errors * Set up tracker
*/ */
protected $errors = array(); protected function init(importexport_definition &$definition)
{
/**
* List of actions, and how many times that action was taken
*/
protected $results = array();
/**
* imports entries according to given definition object.
* @param resource $_stream
* @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;
// fetch the addressbook bo // fetch the addressbook bo
$this->bo= new calendar_boupdate(); $this->bo= new calendar_boupdate();
// Get the tracker for changes // Get the tracker for changes
$this->tracking = new calendar_tracking(); $this->tracking = new calendar_tracking();
// 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);
}
// set eventOwner
$options =& $_definition->plugin_options;
$options['owner'] = $options['owner'] ? $options['owner'] : $this->user;
// Start counting successes
$count = 0;
$this->results = array();
// Failures
$this->errors = array();
// Used for participants // Used for participants
$status_map = array_flip(array_map('lang',$this->bo->verbose_status)); $this->status_map = array_flip(array_map('lang',$this->bo->verbose_status));
$role_map = array_flip($this->bo->roles); $this->role_map = array_flip($this->bo->roles);
$lookups = array( $this->lookups = array(
'priority' => Array( 'priority' => Array(
0 => lang('None'), 0 => lang('None'),
1 => lang('Low'), 1 => lang('Low'),
@ -166,82 +65,145 @@ class calendar_import_csv implements importexport_iface_import_plugin {
), ),
'recurrence' => $this->bo->recur_types 'recurrence' => $this->bo->recur_types
); );
}
while ( $record = $import_csv->get_record() ) { /**
$success = false; * imports a single entry according to given definition object.
* Handles the conditions and the actions taken.
// don't import empty records *
if( count( array_unique( $record ) ) < 2 ) continue; * @param importepport_iface_egw_record record The egw_record object being imported
* @param importexport_iface_import_record import_csv Import object contains current state
// Automatic conversions *
importexport_import_csv::convert($record, calendar_egw_record::$types, 'calendar', $lookups, * @return boolean success
$_definition->plugin_options['convert'] */
); public function import_record(\importexport_iface_egw_record &$record, &$import_csv)
{
// set eventOwner
$options =& $this->definition->plugin_options;
$options['owner'] = $options['owner'] ? $options['owner'] : $this->user;
// 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']) { if($options['owner_from_csv']) {
if(!is_numeric($record['owner'])) { if(!is_numeric($record['owner'])) {
$this->errors[$import_csv->get_current_position()] = lang( $this->errors[$import_csv->get_current_position()] = lang(
'Invalid owner ID: %1. Might be a bad field translation. Used %2 instead.', 'Invalid owner ID: %1. Might be a bad field translation. Used %2 instead.',
$record['owner'], $record->owner,
$options['owner'] $options['owner']
); );
$record['owner'] = $options['owner']; $record->owner = $options['owner'];
} }
} else { }
$record['owner'] = $options['owner']; else
{
$record->owner = $options['owner'];
} }
if ($record['participants'] && !is_array($record['participants'])) { // Handle errors in length or start/end date
// Importing participants in human friendly format if($record->start > $record->end)
preg_match_all('/(([^(]+?)( \(([0-9]+)\))? \((.+?)\) ([^,]+)),?/',$record['participants'],$participants); {
$record['participants'] = array(); $record->end = $record->start + $GLOBALS['egw_info']['user']['preferences']['calendar']['defaultlength'] * 60;
list($lines, $p, $names, $q, $quantity, $status, $role) = $participants; $this->warnings[$import_csv->get_current_position()] = lang('error: starttime has to be before the endtime !!!');
}
// Parse particpants
if ($record->participants && !is_array($record->participants)) {
// Importing participants in human friendly format:
// Name (quantity)? (status) Role[, Name (quantity)? (status) Role]+
preg_match_all('/(([^(]+?)(?: \(([\d]+)\))? \(([^,)]+)\)(?: ([^ ,]+))?)(?:, )?/',$record->participants,$participants);
$p_participants = array();
$missing = array();
list($lines, $p, $names, $quantity, $status, $role) = $participants;
foreach($names as $key => $name) { foreach($names as $key => $name) {
$id = $GLOBALS['egw']->accounts->name2id($name, 'account_fullname'); //error_log("Name: $name Quantity: {$quantity[$key]} Status: {$status[$key]} Role: {$role[$key]}");
if(!$id) {
$contacts = ExecMethod2('addressbook.addressbook_bo.search', $name,array('contact_id','account_id'),'org_name,n_family,n_given,cat_id,contact_email','','%',false,'OR',array(0,1)); // Search for direct account name, then user in accounts first
if($contacts) $id = $contacts[0]['account_id'] ? $contacts[0]['account_id'] : 'c'.$contacts[0]['id']; $search = "\"$name\"";
$id = importexport_helper_functions::account_name2id($name);
// If not found, or not an exact match to a user (account_name2id is pretty generous)
if(!$id || $names[$key] !== $this->bo->participant_name($id)) {
$contacts = ExecMethod2('addressbook.addressbook_bo.search', $search,array('contact_id','account_id'),'org_name,n_family,n_given,cat_id,contact_email','','%',false,'OR',array(0,1));
if($contacts) $id = $contacts[0]['account_id'] ? $contacts[0]['account_id'] : 'c'.$contacts[0]['contact_id'];
}
if(!$id)
{
// Use calendar's registered resources to find participant
foreach($this->bo->resources as $resource)
{
// Can't search for email
if($resource['app'] == 'email') continue;
// Special resource search, since it does special stuff in link_query
if($resource['app'] == 'resources')
{
if(!$this->resource_so)
{
$this->resource_so = new resources_so();
}
$result = $this->resource_so->search($search,'res_id');
if(count($result) >= 1) {
$id = $resource['type'].$result[0]['res_id'];
break;
}
}
else
{
// Search app via link query
$result = egw_link::query($resource['app'], $search, $options);
if($result)
{
$id = $resource['type'] . key($result);
break;
}
}
}
} }
if($id) { if($id) {
$record['participants'][$id] = calendar_so::combine_status( $p_participants[$id] = calendar_so::combine_status(
$status_map[lang($status[$key])] ? $status_map[lang($status[$key])] : $status[$key][0], $this->status_map[lang($status[$key])] ? $this->status_map[lang($status[$key])] : $status[$key][0],
$quantity[$key] ? $quantity[$key] : 1, $quantity[$key] ? $quantity[$key] : 1,
$role_map[lang($role[$key])] ? $role_map[lang($role[$key])] : $role[$key] $this->role_map[lang($role[$key])] ? $this->role_map[lang($role[$key])] : $role[$key]
); );
} }
} else
}
if($record['recurrence'])
{ {
list($record['recur_type'], $record['recur_interval']) = explode('/',$record['recurrence'],2); $missing[] = $name;
$record['recur_interval'] = trim($record['recur_interval']); }
$record['recur_type'] = array_search(strtolower(trim($record['recur_type'])), array_map('strtolower',$lookups['recurrence'])); if(count($missing) > 0)
unset($record['recurrence']); {
$this->warnings[$import_csv->get_current_position()] = $record->title . ' ' . lang('participants') . ': ' .
lang('Contact not found!') . '<br />'.implode(", ",$missing);
}
}
$record->participants = $p_participants;
} }
$record['tzid'] = calendar_timezones::id2tz($record['tz_id']);
// Calendar doesn't actually support conditional importing if($record->recurrence)
if ( $_definition->plugin_options['conditions'] ) { {
foreach ( $_definition->plugin_options['conditions'] as $condition ) { list($record->recur_type, $record->recur_interval) = explode('/',$record->recurrence,2);
$record->recur_interval = trim($record->recur_interval);
$record->recur_type = array_search(strtolower(trim($record->recur_type)), array_map('strtolower',$lookups['recurrence']));
unset($record->recurrence);
}
$record->tzid = calendar_timezones::id2tz($record->tz_id);
if ( $options['conditions'] ) {
foreach ( $options['conditions'] as $condition ) {
$records = array(); $records = array();
switch ( $condition['type'] ) { switch ( $condition['type'] ) {
// exists // exists
case 'exists' : case 'exists' :
if($record[$condition['string']] && $condition['string'] == 'id') { // Check for that record
$event = $this->bo->read($record[$condition['string']]); $result = $this->exists($record, $condition, $records);
$records = array($event);
}
if ( is_array( $records ) && count( $records ) >= 1) { if ( is_array( $records ) && count( $records ) >= 1) {
// apply action to all records matching this exists condition // apply action to all records matching this exists condition
$action = $condition['true']; $action = $condition['true'];
foreach ( (array)$records as $event ) { foreach ( (array)$records as $event ) {
$record['id'] = $event['id']; $record->id = $event['id'];
if ( $_definition->plugin_options['update_cats'] == 'add' ) { if ( $this->definition->plugin_options['update_cats'] == 'add' ) {
if ( !is_array( $record['category'] ) ) $record['category'] = explode( ',', $record['category'] ); if ( !is_array( $record->category ) ) $record->category = explode( ',', $record->category );
$record['category'] = implode( ',', array_unique( array_merge( $record['category'], $event['category'] ) ) ); $record->category = implode( ',', array_unique( array_merge( $record->category, $event['category'] ) ) );
} }
$success = $this->action( $action['action'], $record, $import_csv->get_current_position() ); $success = $this->action( $action['action'], $record, $import_csv->get_current_position() );
} }
@ -262,9 +224,30 @@ class calendar_import_csv implements importexport_iface_import_plugin {
// unconditional insert // unconditional insert
$success = $this->action( 'insert', $record, $import_csv->get_current_position() ); $success = $this->action( 'insert', $record, $import_csv->get_current_position() );
} }
if($success) $count++;
return $success;
} }
return $count;
/**
* 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(importexport_iface_egw_record &$record, Array &$condition, &$records = array())
{
if($record->__get($condition['string']) && $condition['string'] == 'id') {
$event = $this->bo->read($record->__get($condition['string']));
$records = array($event);
}
if ( is_array( $records ) && count( $records ) >= 1) {
return true;
}
return false;
} }
/** /**
@ -274,7 +257,9 @@ class calendar_import_csv implements importexport_iface_import_plugin {
* @param array $_data record data for the action * @param array $_data record 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, importexport_iface_egw_record &$record, $record_num = 0 )
{
$_data = $record->get_record_array();
switch ($_action) { switch ($_action) {
case 'none' : case 'none' :
return true; return true;
@ -317,6 +302,8 @@ class calendar_import_csv implements importexport_iface_import_plugin {
$this->errors[$record_num] = lang('Unable to save'); $this->errors[$record_num] = lang('Unable to save');
} else { } else {
$this->results[$_action]++; $this->results[$_action]++;
// This does nothing (yet?) but update the identifier
$record->save($result);
} }
return $result; return $result;
} }
@ -354,64 +341,14 @@ class calendar_import_csv implements importexport_iface_import_plugin {
} }
/** /**
* return etemplate components for options. * Alter a row for preview to show multiple participants instead of Array
* @abstract We can't deal with etemplate objects here, as an uietemplate
* objects itself are scipt orientated and not "dialog objects"
* *
* @return array ( * @param egw_record $row_entry
* name => string,
* content => array,
* sel_options => array,
* preserv => array,
* )
*/ */
public function get_options_etpl() { protected function row_preview(importexport_iface_egw_record &$row_entry)
// lets do it! {
$row_entry->participants = implode('<br />', $this->bo->participants(array('participants' => $row_entry->participants),true));
} }
/**
* returns etemplate name for slectors of this plugin
*
* @return string etemplate name
*/
public function get_selectors_etpl() {
// lets do it!
}
/**
* Returns warnings that were encountered during importing
* Maximum of one warning message per record, but you can append if you need to
*
* @return Array (
* record_# => warning message
* )
*/
public function get_warnings() {
return $this->warnings;
}
/**
* Returns errors that were encountered during importing
* Maximum of one error message per record, but you can append if you need to
*
* @return Array (
* record_# => error message
* )
*/
public function get_errors() {
return $this->errors;
}
/**
* Returns a list of actions taken, and the number of records for that action.
* Actions are things like 'insert', 'update', 'delete', and may be different for each plugin.
*
* @return Array (
* action => record count
* )
*/
public function get_results() {
return $this->results;
}
} // end of iface_export_plugin } // end of iface_export_plugin
?> ?>

View File

@ -136,6 +136,68 @@ class calendar_tracking extends bo_tracking
{ {
$participants = $data['participants']; $participants = $data['participants'];
$data['participants'] = array(); $data['participants'] = array();
$data = array_merge($data, $this->alter_participants($participants));
}
// if clients eg. CalDAV do NOT set participants, they are left untouched
// therefore we should not track them, as all updates then show up as all participants removed
elseif(!isset($data['participants']))
{
unset($old['participants']);
}
if(is_array($old['participants']))
{
$participants = $old['participants'];
$old['participants'] = array();
$old = array_merge($old, $this->alter_participants($participants));
}
parent::track($data,$old,$user,$deleted, $changed_fields);
}
/**
* Overrides parent because calendar_boupdates handles the notifications
*/
public function do_notifications($data,$old,$deleted=null)
{
unset($data, $old, $deleted); // unused, but required by function signature
return true;
}
/**
* Compute changes between new and old data
*
* Can be used to check if saving the data is really necessary or user just pressed save
* Overridden to handle various participants options
*
* @param array $data
* @param array $old = null
* @return array of keys with different values in $data and $old
*/
public function changed_fields(array $data,array $old=null)
{
if(is_array($data['participants']))
{
$participants = $data['participants'];
$data['participants'] = array();
$data = array_merge($data, $this->alter_participants($participants));
}
if(is_array($old['participants']))
{
$participants = $old['participants'];
$old['participants'] = array();
$old = array_merge($old, $this->alter_participants($participants));
}
return parent::changed_fields($data,$old);
}
/**
* Do some magic with the participants and recurrance.
* If this is one of a recurring event, append the recur_date to the participant field so we can
* filter by it later.
*/
protected function alter_participants($participants)
{
$data = array();
foreach($participants as $uid => $status) foreach($participants as $uid => $status)
{ {
$quantity = $role = $user_type = $user_id = null; $quantity = $role = $user_type = $user_id = null;
@ -150,40 +212,6 @@ class calendar_tracking extends bo_tracking
'recur' => $data['recur_date'] ? $data['recur_date'] : 0, 'recur' => $data['recur_date'] ? $data['recur_date'] : 0,
); );
} }
} return $data;
// if clients eg. CalDAV do NOT set participants, they are left untouched
// therefore we should not track them, as all updates then show up as all participants removed
elseif(!isset($data['participants']))
{
unset($old['participants']);
}
if(is_array($old['participants']))
{
$participants = $old['participants'];
$old['participants'] = array();
foreach($participants as $uid => $status)
{
calendar_so::split_status($status, $quantity, $role);
calendar_so::split_user($uid, $user_type, $user_id);
$field = is_numeric($uid) ? 'participants' : 'participants-'.$user_type;
$old[$field][] = array(
'user_id' => $user_id,
'status' => $status,
'quantity' => $quantity,
'role' => $role,
'recur' => $data['recur_date'] ? $data['recur_date'] : 0,
);
}
}
parent::track($data,$old,$user,$deleted, $changed_fields);
}
/**
* Overrides parent because calendar_boupdates handles the notifications
*/
public function do_notifications($data,$old,$deleted=null)
{
unset($data, $old, $deleted); // unused, but required by function signature
return true;
} }
} }

View File

@ -438,6 +438,7 @@ abstract class importexport_basic_import_csv implements importexport_iface_impor
{ {
// Convert to human-friendly // Convert to human-friendly
importexport_export_csv::convert($row_data,$record_class::$types,$definition->application,$this->lookups); importexport_export_csv::convert($row_data,$record_class::$types,$definition->application,$this->lookups);
$this->row_preview($row_data);
$rows[] = $row_data->get_record_array(); $rows[] = $row_data->get_record_array();
} }
$this->preview_records = array(); $this->preview_records = array();
@ -445,6 +446,15 @@ abstract class importexport_basic_import_csv implements importexport_iface_impor
return html::table($rows); return html::table($rows);
} }
/**
* Allows an extending class to alter a row for preview
*
* @param egw_record $row_entry
*/
protected function row_preview(importexport_iface_egw_record &$row_entry)
{
}
/** /**
* Search for contact, but only using family, given & org name fields. * Search for contact, but only using family, given & org name fields.
* *
@ -626,6 +636,18 @@ error_log("Searching for $custom_field = $value");
// lets do it! // lets do it!
} }
/**
* Returns warnings that were encountered during importing
* Maximum of one warning message per record, but you can append if you need to
*
* @return Array (
* record_# => warning message
* )
*/
public function get_warnings() {
return $this->warnings;
}
/** /**
* Returns errors that were encountered during importing * Returns errors that were encountered during importing
* Maximum of one error message per record, but you can append if you need to * Maximum of one error message per record, but you can append if you need to