2007-06-08 00:31:08 +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 : $
*/
/**
* class import_csv for addressbook
*/
2010-03-22 16:11:12 +01:00
class addressbook_import_contacts_csv implements importexport_iface_import_plugin {
2008-05-10 14:02:49 +02:00
2007-06-08 00:31:08 +02:00
private static $plugin_options = array (
2007-06-09 17:38:30 +02:00
'fieldsep' , // char
'charset' , // string
'contact_owner' , // int
2008-05-10 14:02:49 +02:00
'update_cats' , // string {override|add} overides record
2007-06-23 14:06:08 +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
2007-06-09 17:38:30 +02:00
'field_conversion' , // array( $csv_col_num => conversion)
2007-06-08 00:31:08 +02:00
'field_mapping' , // array( $csv_col_num => adb_filed)
2008-05-10 14:02:49 +02:00
'conditions' , /* => array containing condition arrays :
2007-06-21 16:02:28 +02:00
'type' => exists , // exists
2007-06-08 00:31:08 +02:00
'string' => '#kundennummer' ,
'true' => array (
'action' => update ,
'last' => true ,
),
'false' => array (
2008-05-10 14:02:49 +02:00
'action' => insert ,
2007-06-08 00:31:08 +02:00
'last' => true ,
), */
2008-05-10 14:02:49 +02:00
2007-06-08 00:31:08 +02:00
);
2008-05-10 14:02:49 +02:00
2007-06-08 00:31:08 +02:00
/**
* actions wich could be done to data entries
*/
2010-02-26 00:18:45 +01:00
protected static $actions = array ( 'none' , 'update' , 'insert' , 'delete' , );
2008-05-10 14:02:49 +02:00
2007-06-08 00:31:08 +02:00
/**
* conditions for actions
*
* @ var array
*/
2010-02-26 00:18:45 +01:00
protected static $conditions = array ( 'exists' , 'greater' , 'greater or equal' , );
2008-05-10 14:02:49 +02:00
2007-06-23 14:06:08 +02:00
/**
* @ var definition
*/
private $definition ;
2008-05-10 14:02:49 +02:00
2007-06-08 00:31:08 +02:00
/**
* @ var bocontacts
*/
private $bocontacts ;
2008-05-10 14:02:49 +02:00
2010-02-26 00:18:45 +01:00
/**
* For figuring out if a contact has changed
*/
protected $tracking ;
2007-06-08 00:31:08 +02:00
/**
2007-06-21 20:08:17 +02:00
* @ var bool
*/
private $dry_run = false ;
2008-05-10 14:02:49 +02:00
2007-06-23 14:06:08 +02:00
/**
* @ var bool is current user admin ?
*/
private $is_admin = false ;
2008-05-10 14:02:49 +02:00
2007-06-23 14:06:08 +02:00
/**
* @ var int
*/
private $user = null ;
2008-05-10 14:02:49 +02:00
2010-02-26 00:18:45 +01:00
/**
* List of import errors
*/
protected $errors = array ();
2010-03-03 17:29:25 +01:00
/**
* List of actions , and how many times that action was taken
*/
protected $results = array ();
2007-06-08 00:31:08 +02:00
/**
* imports entries according to given definition object .
* @ param resource $_stream
* @ param string $_charset
* @ param definition $_definition
*/
2010-03-22 16:11:12 +01:00
public function import ( $_stream , importexport_definition $_definition ) {
$import_csv = new importexport_import_csv ( $_stream , array (
2007-06-08 00:31:08 +02:00
'fieldsep' => $_definition -> plugin_options [ 'fieldsep' ],
'charset' => $_definition -> plugin_options [ 'charset' ],
));
2008-05-10 14:02:49 +02:00
2007-06-23 14:06:08 +02:00
$this -> definition = $_definition ;
2008-05-10 14:02:49 +02:00
2007-06-23 14:06:08 +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' ];
2008-05-10 14:02:49 +02:00
2007-06-21 20:08:17 +02:00
// dry run?
2008-05-10 14:02:49 +02:00
$this -> dry_run = isset ( $_definition -> plugin_options [ 'dry_run' ] ) ? $_definition -> plugin_options [ 'dry_run' ] : false ;
2007-06-08 00:31:08 +02:00
// fetch the addressbook bo
2008-05-10 14:02:49 +02:00
$this -> bocontacts = new addressbook_bo ();
2010-02-26 00:18:45 +01:00
// Get the tracker for changes
$this -> tracking = new addressbook_tracking ( $this -> bocontacts );
2007-06-09 17:38:30 +02:00
// set FieldMapping.
$import_csv -> mapping = $_definition -> plugin_options [ 'field_mapping' ];
2008-05-10 14:02:49 +02:00
2007-06-09 17:38:30 +02:00
// set FieldConversion
$import_csv -> conversion = $_definition -> plugin_options [ 'field_conversion' ];
2008-05-10 14:02:49 +02:00
2007-06-23 14:06:08 +02:00
//check if file has a header lines
2010-02-26 00:18:45 +01:00
if ( isset ( $_definition -> plugin_options [ 'num_header_lines' ] ) && $_definition -> plugin_options [ 'num_header_lines' ] > 0 ) {
2007-06-23 14:06:08 +02:00
$import_csv -> skip_records ( $_definition -> plugin_options [ 'num_header_lines' ]);
2010-02-26 00:18:45 +01:00
} elseif ( isset ( $_definition -> plugin_options [ 'has_header_line' ]) && $_definition -> plugin_options [ 'has_header_line' ]) {
// First method is preferred
$import_csv -> skip_records ( 1 );
2007-06-08 00:31:08 +02:00
}
2008-05-10 14:02:49 +02:00
2007-06-23 14:06:08 +02:00
// set eventOwner
2008-05-10 14:02:49 +02:00
$_definition -> plugin_options [ 'contact_owner' ] = isset ( $_definition -> plugin_options [ 'contact_owner' ] ) ?
2007-06-23 14:06:08 +02:00
$_definition -> plugin_options [ 'contact_owner' ] : $this -> user ;
2008-05-10 14:02:49 +02:00
2010-02-26 00:18:45 +01:00
// Start counting successes
$count = 0 ;
2010-03-03 17:29:25 +01:00
$this -> results = array ();
2010-02-26 00:18:45 +01:00
// Failures
$this -> errors = array ();
2007-06-08 00:31:08 +02:00
while ( $record = $import_csv -> get_record () ) {
2010-02-26 00:18:45 +01:00
$success = false ;
2007-06-08 00:31:08 +02:00
// don't import empty contacts
if ( count ( array_unique ( $record ) ) < 2 ) continue ;
2008-05-10 14:02:49 +02:00
2010-03-02 00:08:50 +01:00
$record [ 'owner' ] = $_definition -> plugin_options [ 'contact_owner' ];
2007-06-08 00:31:08 +02:00
if ( $_definition -> plugin_options [ 'conditions' ] ) {
foreach ( $_definition -> plugin_options [ 'conditions' ] as $condition ) {
switch ( $condition [ 'type' ] ) {
// exists
case 'exists' :
2007-06-21 16:02:28 +02:00
$contacts = $this -> bocontacts -> search (
2010-04-21 21:09:11 +02:00
//array( $condition['string'] => $record[$condition['string']],),
'' ,
$_definition -> plugin_options [ 'update_cats' ] == 'add' ? false : true ,
'' , '' , '' , false , 'AND' , false ,
array ( $condition [ 'string' ] => $record [ $condition [ 'string' ]],)
2007-06-21 16:02:28 +02:00
);
2008-05-10 14:02:49 +02:00
2007-06-08 00:31:08 +02:00
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' ];
2007-06-23 14:06:08 +02:00
if ( $_definition -> plugin_options [ 'update_cats' ] == 'add' ) {
2007-06-21 16:02:28 +02:00
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' ] ) ) );
}
2010-02-26 00:18:45 +01:00
$success = $this -> action ( $action [ 'action' ], $record , $import_csv -> get_current_position () );
2007-06-08 00:31:08 +02:00
}
} else {
2007-06-21 20:08:17 +02:00
$action = $condition [ 'false' ];
2010-02-26 00:18:45 +01:00
$success = ( $this -> action ( $action [ 'action' ], $record , $import_csv -> get_current_position () ));
2007-06-08 00:31:08 +02:00
}
break ;
2008-05-10 14:02:49 +02:00
// not supported action
2007-06-08 00:31:08 +02:00
default :
die ( 'condition / action not supported!!!' );
break ;
}
if ( $action [ 'last' ]) break ;
}
} else {
// unconditional insert
2010-02-26 00:18:45 +01:00
$success = $this -> action ( 'insert' , $record , $import_csv -> get_current_position () );
2007-06-08 00:31:08 +02:00
}
2010-02-26 00:18:45 +01:00
if ( $success ) $count ++ ;
2007-06-08 00:31:08 +02:00
}
2010-02-26 00:18:45 +01:00
return $count ;
2007-06-08 00:31:08 +02:00
}
2008-05-10 14:02:49 +02:00
2007-06-08 00:31:08 +02:00
/**
* perform the required action
*
* @ param int $_action one of $this -> actions
* @ param array $_data contact data for the action
* @ return bool success or not
*/
2010-02-26 00:18:45 +01:00
private function action ( $_action , $_data , $record_num = 0 ) {
2007-06-08 00:31:08 +02:00
switch ( $_action ) {
case 'none' :
return true ;
case 'update' :
2010-02-26 00:18:45 +01:00
// Only update if there are changes
$old = $this -> bocontacts -> read ( $_data [ 'id' ]);
2010-03-24 16:43:52 +01:00
// Don't change a user account into a contact
if ( $old [ 'owner' ] == 0 ) {
unset ( $_data [ 'owner' ]);
}
2010-02-26 00:18:45 +01:00
// Merge to deal with fields not in import record
$_data = array_merge ( $old , $_data );
$changed = $this -> tracking -> changed_fields ( $_data , $old );
if ( count ( $changed ) == 0 ) {
return true ;
2010-09-23 17:38:28 +02:00
}
// Make sure n_fn gets updated
unset ( $_data [ 'n_fn' ]);
2010-02-26 00:18:45 +01:00
// Fall through
2007-06-08 00:31:08 +02:00
case 'insert' :
2010-03-02 00:08:50 +01:00
if ( $_action == 'insert' ) {
// Addressbook backend doesn't like inserting with ID specified, it screws up the owner & etag
unset ( $_data [ 'id' ]);
}
2010-09-23 17:38:28 +02:00
if ( ! isset ( $_data [ 'org_name' ])) {
// org_name is a trigger to update n_fileas
$_data [ 'org_name' ] = '' ;
}
2007-06-21 20:08:17 +02:00
if ( $this -> dry_run ) {
2010-08-16 16:56:14 +02:00
//print_r($_data);
2010-03-03 17:29:25 +01:00
$this -> results [ $_action ] ++ ;
2010-02-26 00:18:45 +01:00
return true ;
2007-06-21 20:08:17 +02:00
} else {
2010-02-26 00:18:45 +01:00
$result = $this -> bocontacts -> save ( $_data , $this -> is_admin );
if ( ! $result ) {
$this -> errors [ $record_num ] = $this -> bocontacts -> error ;
2010-03-03 17:29:25 +01:00
} else {
$this -> results [ $_action ] ++ ;
2010-02-26 00:18:45 +01:00
}
return $result ;
2007-06-21 20:08:17 +02:00
}
2010-03-22 16:11:12 +01:00
default :
throw new egw_exception ( 'Unsupported action' );
2007-06-08 00:31:08 +02:00
}
}
2008-05-10 14:02:49 +02:00
2007-06-08 00:31:08 +02:00
/**
* returns translated name of plugin
*
* @ return string name
*/
public static function get_name () {
2010-02-26 00:18:45 +01:00
return lang ( 'Addressbook CSV import' );
2007-06-08 00:31:08 +02:00
}
2008-05-10 14:02:49 +02:00
2007-06-08 00:31:08 +02:00
/**
* returns translated ( user ) description of plugin
*
* @ return string descriprion
*/
public static function get_description () {
return lang ( " Imports contacts into your Addressbook from a CSV File. CSV means 'Comma Seperated Values'. However in the options Tab you can also choose other seperators. " );
}
2008-05-10 14:02:49 +02:00
2007-06-08 00:31:08 +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-05-10 14:02:49 +02:00
2007-06-08 00:31:08 +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-05-10 14:02:49 +02:00
*
2007-06-08 00:31:08 +02:00
* @ return array (
* name => string ,
* content => array ,
* sel_options => array ,
* preserv => array ,
* )
*/
public function get_options_etpl () {
// lets do it!
}
2008-05-10 14:02:49 +02:00
2007-06-08 00:31:08 +02:00
/**
* returns etemplate name for slectors of this plugin
*
* @ return string etemplate name
*/
public function get_selectors_etpl () {
// lets do it!
}
2010-02-26 00:18:45 +01:00
/**
* 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 ;
}
2010-03-03 17:29:25 +01:00
/**
* 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-08 00:31:08 +02:00
} // end of iface_export_plugin
?>