egroupware/calendar/importexport/class.import_events_csv.inc.php

332 lines
9.5 KiB
PHP
Raw Normal View History

2007-06-25 22:49:19 +02:00
<?php
/**
* eGroupWare
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package importexport
* @link http://www.egroupware.org
* @author Cornelius Weiss <nelius@cwtech.de>
* @copyright Cornelius Weiss <nelius@cwtech.de>
* @version $Id: $
*/
require_once(EGW_INCLUDE_ROOT. '/importexport/inc/class.iface_import_plugin.inc.php');
require_once(EGW_INCLUDE_ROOT.'/importexport/inc/class.import_csv.inc.php');
/**
* class import_csv for addressbook
*/
class import_events_csv implements iface_import_plugin {
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
private static $plugin_options = array(
'fieldsep', // char
'charset', // string
'event_owner', // int account_id or -1 for leave untuched
'owner_joins_event', // bool
2008-06-07 19:45:33 +02:00
'update_cats', // string {override|add} overides record
2007-06-25 22:49:19 +02:00
// 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
'trash_users_records', // trashes all events of events owner before import
2007-06-25 22:49:19 +02:00
'field_conversion', // array( $csv_col_num => conversion)
'field_mapping', // array( $csv_col_num => adb_filed)
2008-06-07 19:45:33 +02:00
'conditions', /* => array containing condition arrays:
2007-06-25 22:49:19 +02:00
'type' => exists, // record['uid'] exists
'true' => array(
'action' => update,
'last' => true,
),
'false' => array(
2008-06-07 19:45:33 +02:00
'action' => insert,
2007-06-25 22:49:19 +02:00
'last' => true,
),*/
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
);
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
/**
* actions wich could be done to data entries
*/
private static $actions = array( 'none', 'update', 'insert', 'delete', );
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
/**
* conditions for actions
*
* @var array
*/
private static $conditions = array( 'exists', 'empty', );
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
/**
* @var definition
*/
private $definition;
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
/**
* @var bocalupdate
*/
private $bocalupdate;
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
/**
* @var bool
*/
private $dry_run = false;
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
/**
* @var bool is current user admin?
*/
private $is_admin = false;
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
/**
* @var int
*/
private $user = null;
2008-06-07 19:45:33 +02:00
/**
* List of import errors
*/
protected $errors = array();
/**
* List of actions, and how many times that action was taken
*/
protected $results = array();
2007-06-25 22:49:19 +02:00
/**
* imports entries according to given definition object.
* @param resource $_stream
* @param string $_charset
* @param definition $_definition
*/
public function import( $_stream, definition $_definition ) {
$import_csv = new import_csv( $_stream, array(
'fieldsep' => $_definition->plugin_options['fieldsep'],
'charset' => $_definition->plugin_options['charset'],
));
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
$this->definition = $_definition;
2007-06-25 22:49:19 +02:00
// 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?
2008-06-07 19:45:33 +02:00
$this->dry_run = isset( $_definition->plugin_options['dry_run'] ) ? $_definition->plugin_options['dry_run'] : false;
2007-06-25 22:49:19 +02:00
// fetch the calendar bo
2008-06-07 19:45:33 +02:00
$this->bocalupdate = new calendar_boupdate();
2007-06-25 22:49:19 +02:00
// set FieldMapping.
$import_csv->mapping = $_definition->plugin_options['field_mapping'];
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
// set FieldConversion
$import_csv->conversion = $_definition->plugin_options['field_conversion'];
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
//check if file has a header lines
if ( isset( $_definition->plugin_options['num_header_lines'] ) ) {
$import_csv->skip_records( $_definition->plugin_options['num_header_lines'] );
}
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
// set eventOwner
2008-06-07 19:45:33 +02:00
$_definition->plugin_options['events_owner'] = isset( $_definition->plugin_options['events_owner'] ) ?
2007-06-25 22:49:19 +02:00
$_definition->plugin_options['events_owner'] : $this->user;
2008-06-07 19:45:33 +02:00
// trash_users_records ?
if ( $_definition->plugin_options['trash_users_records'] === true ) {
if ( !$_definition->plugin_options['dry_run'] ) {
2008-06-07 19:45:33 +02:00
$socal = new calendar_socal();
$this->bocalupdate->so->deleteaccount( $_definition->plugin_options['events_owner']);
unset( $socal );
} else {
$lid = $GLOBALS['egw']->accounts->id2name( $_definition->plugin_options['events_owner'] );
echo "Attension: All Events of '$lid' would be deleted!\n";
}
}
2008-06-07 19:45:33 +02:00
$this->errors = array();
$this->results = array();
2007-06-25 22:49:19 +02:00
while ( $record = $import_csv->get_record() ) {
// don't import empty events
if( count( array_unique( $record ) ) < 2 ) continue;
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
if ( $_definition->plugin_options['events_owner'] != -1 ) {
$record['owner'] = $_definition->plugin_options['events_owner'];
} else unset( $record['owner'] );
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
if ( $_definition->plugin_options['conditions'] ) {
foreach ( $_definition->plugin_options['conditions'] as $condition ) {
switch ( $condition['type'] ) {
// exists
case 'exists' :
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
if ( is_array( $event = $this->bocalupdate->read( $record['uid'], null, $this->is_admin ) ) ) {
// apply action to event matching this exists condition
$record['id'] = $event['id'];
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
if ( $_definition->plugin_options['update_cats'] == 'add' ) {
if ( !is_array( $event['cat_id'] ) ) $event['cat_id'] = explode( ',', $event['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'], $event['cat_id'] ) ) );
}
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
// check if entry is modiefied
$event = array_intersect_key( $event, $record );
$diff = array_diff( $event, $record );
if( !empty( $diff ) ) $record['modified'] = time();
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
$action = $condition['true'];
} else $action = $condition['false'];
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
$this->action( $action['action'], $record );
break;
case 'empty' :
$action = empty( $record[$condition['string']] ) ? $condition['true'] : $condition['false'];
$this->action( $action['action'], $record );
break;
2008-06-07 19:45:33 +02:00
// not supported action
2007-06-25 22:49:19 +02:00
default :
throw new Exception('condition not supported!!!');
break;
}
if ($action['last']) break;
}
} else {
// unconditional insert
$this->action( 'insert', $record );
}
}
}
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
/**
* perform the required action
*
* @param int $_action one of $this->actions
* @param array $_data event data for the action
* @return bool success or not
*/
private function action ( $_action, $_data ) {
switch ( $_action ) {
case 'none' :
return true;
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
case 'update' :
case 'insert' :
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
// paticipants handling
$participants = $_data['participants'] ? split( '[,;]', $_data['participants'] ) : array();
$_data['participants'] = array();
if ( $this->definition->plugin_options['owner_joins_event'] && $this->definition->plugin_options['events_owner'] > 0 ) {
$_data['participants'][$this->definition->plugin_options['events_owner']] = 'A';
}
foreach( $participants as $participant ) {
list( $participant, $status ) = explode( '=', $participant );
$valid_staties = array('U'=>'U','u'=>'U','A'=>'A','a'=>'A','R'=>'R','r'=>'R','T'=>'T','t'=>'T');
$status = isset( $valid_staties[$status] ) ? $valid_staties[$status] : 'U';
if ( $participant && is_numeric($participant ) ) {
$_data['participants'][$participant] = $status;
}
}
// no valid participants so far --> add the importing user/owner
if ( empty( $_data['participants'] ) ) {
$_data['participants'][$this->user] = 'A';
}
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
// are we serious?
if ( $this->dry_run ) {
print_r($_data);
$this->results[$_action]++;
2007-06-25 22:49:19 +02:00
} else {
$messages = array();
$result = $this->bocalupdate->update( $_data, true, !$_data['modified'], $this->is_admin, true, $messages);
if(!$result) {
$this->errors = implode(',', $messages);
} else {
$this->results[$_action]++;
}
return $result;
2007-06-25 22:49:19 +02:00
}
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
case 'delete' :
}
}
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
/**
* returns translated name of plugin
*
* @return string name
*/
public static function get_name() {
return lang('Calendar CSV export');
}
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
/**
* returns translated (user) description of plugin
*
* @return string descriprion
*/
public static function get_description() {
return lang("Imports events into your Calendar from a CSV File. CSV means 'Comma Seperated Values'. However in the options Tab you can also choose other seperators.");
}
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
/**
* retruns file suffix(s) plugin can handle (e.g. csv)
*
* @return string suffix (comma seperated)
*/
public static function get_filesuffix() {
return 'csv';
}
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
/**
* 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"
2008-06-07 19:45:33 +02:00
*
2007-06-25 22:49:19 +02:00
* @return array (
* name => string,
* content => array,
* sel_options => array,
* preserv => array,
* )
*/
public function get_options_etpl() {
// lets do it!
}
2008-06-07 19:45:33 +02:00
2007-06-25 22:49:19 +02:00
/**
* returns etemplate name for slectors of this plugin
*
* @return string etemplate name
*/
public function get_selectors_etpl() {
// lets do it!
}
/**
* 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;
}
2007-06-25 22:49:19 +02:00
} // end of iface_export_plugin
?>