Get infolog working with import/export

This commit is contained in:
Nathan Gray 2010-10-06 22:18:09 +00:00
parent e1505758b7
commit 76d2792308
3 changed files with 567 additions and 1 deletions

View File

@ -0,0 +1,444 @@
<?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 infolog
*/
class infolog_import_infologs_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,
),*/
);
public static $special_fields = array(
'projectmanager' => 'Link to Projectmanager, use Project-ID, Title or @project_id(id_or_title)',
'addressbook' => 'Link to Addressbook, use nlast,nfirst[,org] or contact_id from addressbook',
'link_1' => '1. link: appname:appid the entry should be linked to, eg.: addressbook:123',
'link_2' => '2. link: appname:appid the entry should be linked to, eg.: addressbook:123',
'link_3' => '3. link: appname:appid the entry should be linked to, eg.: addressbook:123',
);
/**
* actions wich could be done to data entries
*/
protected static $actions = array( 'none', 'update', 'insert', 'delete', );
/**
* conditions for actions
*
* @var array
*/
protected static $conditions = array( 'exists' );
/**
* @var definition
*/
private $definition;
/**
* @var business object
*/
private $boinfolog;
/**
* For figuring out if a record has changed
*/
protected $tracking;
/**
* @var bool
*/
private $dry_run = false;
/**
* @var int
*/
private $user = null;
/**
* 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.
* @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;
$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 infolog bo
$this->boinfolog = new infolog_bo();
// Get the tracker for changes
$this->tracking = new infolog_tracking($this->boinfolog);
// set FieldMapping.
$import_csv->mapping = $_definition->plugin_options['field_mapping'];
// set FieldConversion
$import_csv->conversion = $_definition->plugin_options['field_conversion'];
// Add extra conversions
$import_csv->conversion_class = $this;
//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 Owner
$_definition->plugin_options['record_owner'] = isset( $_definition->plugin_options['record_owner'] ) ?
$_definition->plugin_options['record_owner'] : $this->user;
// Start counting successes
$count = 0;
$this->results = array();
// Failures
$this->errors = array();
while ( $record = $import_csv->get_record() ) {
$success = false;
// don't import empty records
if( count( array_unique( $record ) ) < 2 ) continue;
// Set owner, unless it's supposed to come from CSV file
if($_definition->plugin_options['owner_from_csv']) {
if(!is_numeric($record['info_owner'])) {
$this->errors[$import_csv->get_current_position()] = lang(
'Invalid owner ID: %1. Might be a bad field translation. Used %2 instead.',
$record['info_owner'],
$_definition->plugin_options['record_owner']
);
$record['info_owner'] = $_definition->plugin_options['record_owner'];
}
} else {
$record['info_owner'] = $_definition->plugin_options['record_owner'];
}
// Special values
if ($record['addressbook'] && !is_numeric($record['addressbook']))
{
list($lastname,$firstname,$org_name) = explode(',',$record['addressbook']);
$record['addressbook'] = self::addr_id($lastname,$firstname,$org_name);
}
if ($record['projectmanager'] && !is_numeric($record['projectmanager']))
{
$record['projectmanager'] = self::project_id($record['projectmanager']);
}
if ( $_definition->plugin_options['conditions'] ) {
foreach ( $_definition->plugin_options['conditions'] as $condition ) {
switch ( $condition['type'] ) {
// exists
case 'exists' :
$results = $this->boinfolog->search(
//array( $condition['string'] => $record[$condition['string']],),
'',
$_definition->plugin_options['update_cats'] == 'add' ? false : true,
'', '', '', false, 'AND', false,
array( $condition['string'] => $record[$condition['string']],)
);
if ( is_array( $results ) && count( array_keys( $results ) >= 1 ) ) {
// apply action to all records matching this exists condition
$action = $condition['true'];
foreach ( (array)$results 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['last']) break;
}
} else {
// unconditional insert
$success = $this->action( 'insert', $record, $import_csv->get_current_position() );
}
if($success) $count++;
}
return $count;
}
/**
* perform the required action
*
* @param int $_action one of $this->actions
* @param array $_data contact data for the action
* @return bool success or not
*/
private function action ( $_action, $_data, $record_num = 0 ) {
$result = true;
switch ($_action) {
case 'none' :
return true;
case 'update' :
// Only update if there are changes
$old = $this->boinfolog->read($_data['id']);
if(!$this->definition->plugin_options['change_owner']) {
// Don't change addressbook of an existing contact
unset($_data['owner']);
}
// 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 && !$this->definition->plugin_options['update_timestamp']) {
break;
}
// Fall through
case 'insert' :
if ( $this->dry_run ) {
//print_r($_data);
$this->results[$_action]++;
break;
} else {
$result = $this->boinfolog->write( $_data, true, true);
if(!$result) {
$this->errors[$record_num] = lang('Permissions error - %1 could not %2',
$GLOBALS['egw']->accounts->id2name($_data['info_owner']),
lang($_action)
);
} else {
$this->results[$_action]++;
}
break;
}
default:
throw new egw_exception('Unsupported action');
}
// Process some additional fields
if(!is_numeric($result)) {
return $result;
}
$info_link_id = false;
foreach(self::$special_fields as $field => $desc) {
if(!$_data[$field]) continue;
if(strpos('link', $field) === 0) {
list($app, $id) = explode(':', $_data[$field]);
} else {
$app = $field;
$id = $_data[$field];
}
if ($app && $app_id) {
//echo "<p>linking infolog:$id with $app:$app_id</p>\n";
$link_id = egw_link::link('infolog',$id,$app,$app_id);
if ($link_id && !$info_link_id)
{
$to_write = array(
'info_id' => $id,
'info_link_id' => $link_id,
);
$infolog_bo->write($to_write);
$info_link_id = true;
}
}
}
return $result;
}
/**
* returns translated name of plugin
*
* @return string name
*/
public static function get_name() {
return lang('Infolog CSV import');
}
/**
* returns translated (user) description of plugin
*
* @return string descriprion
*/
public static function get_description() {
return lang("Imports entries into the infolog from a CSV File. CSV means 'Comma Seperated Values'. However in the options Tab you can also choose other seperators.");
}
/**
* retruns file suffix(s) plugin can handle (e.g. csv)
*
* @return string suffix (comma seperated)
*/
public static function get_filesuffix() {
return 'csv';
}
/**
* 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,
* sel_options => array,
* preserv => array,
* )
*/
public function get_options_etpl() {
// lets do it!
}
/**
* 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;
}
// end of iface_export_plugin
// Extra conversion functions - must be static
public static function addr_id( $n_family,$n_given=null,$org_name=null ) {
// find in Addressbook, at least n_family AND (n_given OR org_name) have to match
static $contacts;
if (is_null($n_given) && is_null($org_name))
{
// Maybe all in one
list($n_family, $n_given, $org_name) = explode(',', $n_family);
}
$n_family = trim($n_family);
if(!is_null($n_given)) $n_given = trim($n_given);
if (!is_object($contacts))
{
$contacts =& CreateObject('phpgwapi.contacts');
}
if (!is_null($org_name)) // org_name given?
{
$org_name = trim($org_name);
$addrs = $contacts->read( 0,0,array('id'),'',"n_family=$n_family,n_given=$n_given,org_name=$org_name" );
if (!count($addrs))
{
$addrs = $contacts->read( 0,0,array('id'),'',"n_family=$n_family,org_name=$org_name",'','n_family,org_name');
}
}
if (!is_null($n_given) && (is_null($org_name) || !count($addrs))) // first name given and no result so far
{
$addrs = $contacts->search(array('n_family' => $n_family, 'n_given' => $n_given));
}
if (is_null($n_given) && is_null($org_name)) // just one name given, check against fn (= full name)
{
$addrs = $contacts->read( 0,0,array('id'),'',"n_fn=$n_family",'','n_fn' );
}
if (count($addrs))
{
return $addrs[0]['id'];
}
return False;
}
public static function project_id($num_or_title)
{
static $boprojects;
if (!$num_or_title) return false;
if (!is_object($boprojects))
{
$boprojects =& CreateObject('projectmanager.boprojectmanager');
}
if (($projects = $boprojects->search(array('pm_number' => $num_or_title))) ||
($projects = $boprojects->search(array('pm_title' => $num_or_title))))
{
return $projects[0]['pm_id'];
}
return false;
}
}
?>

View File

@ -0,0 +1,120 @@
<?php
/**
* eGroupWare - Wizard for Infolog CSV import
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package addressbook
* @link http://www.egroupware.org
* @author Nathan Gray
* @version $Id: $
*/
class infolog_wizard_import_infologs_csv extends importexport_wizard_basic_import_csv
{
/**
* constructor
*/
function __construct()
{
parent::__construct();
$this->steps += array(
'wizard_step50' => lang('Manage mapping'),
# This doesn't work with infolog very well
#'wizard_step60' => lang('Choose owner of imported data'),
);
// Field mapping
$tracking = new infolog_tracking();
$this->mapping_fields = $tracking->field2label + infolog_import_infologs_csv::$special_fields;
// List each custom field
unset($this->mapping_fields['custom']);
$custom = config::get_customfields('infolog');
foreach($custom as $name => $data) {
$this->mapping_fields['#'.$name] = $data['label'];
}
// Actions
$this->actions = array(
'none' => lang('none'),
'update' => lang('update'),
'insert' => lang('insert'),
'delete' => lang('delete'),
);
// Conditions
$this->conditions = array(
'exists' => lang('exists'),
);
}
function wizard_step50(&$content, &$sel_options, &$readonlys, &$preserv)
{
$result = parent::wizard_step50($content, $sel_options, $readonlys, $preserv);
return $result;
}
# Skipped for now (or forever)
function wizard_step60(&$content, &$sel_options, &$readonlys, &$preserv)
{
if($this->debug) error_log(__METHOD__.'->$content '.print_r($content,true));
unset($content['no_owner_map']);
// Check that record owner has access
$access = true;
if($content['record_owner'])
{
$bo = new infolog_bo();
$access = $bo->check_access(0,EGW_ACL_EDIT, $content['record_owner']);
}
// return from step60
if ($content['step'] == 'wizard_step60')
{
if(!$access) {
$step = $content['step'];
unset($content['step']);
return $step;
}
switch (array_search('pressed', $content['button']))
{
case 'next':
return $GLOBALS['egw']->importexport_definitions_ui->get_step($content['step'],1);
case 'previous' :
return $GLOBALS['egw']->importexport_definitions_ui->get_step($content['step'],-1);
case 'finish':
return 'wizard_finish';
default :
return $this->wizard_step60($content,$sel_options,$readonlys,$preserv);
}
}
// init step60
else
{
$content['msg'] = $this->steps['wizard_step60'];
if(!$access) {
$content['msg'] .= "\n* " . lang('Owner does not have edit rights');
}
$content['step'] = 'wizard_step60';
if(!array_key_exists($content['record_owner']) && $content['plugin_options']) {
$content['record_owner'] = $content['plugin_options']['record_owner'];
}
if(!array_key_exists($content['owner_from_csv']) && $content['plugin_options']) {
$content['owner_from_csv'] = $content['plugin_options']['owner_from_csv'];
}
if(!array_key_exists($content['change_owner']) && $content['plugin_options']) {
$content['change_owner'] = $content['plugin_options']['change_owner'];
}
if(!in_array('info_owner', $content['field_mapping'])) {
$content['no_owner_map'] = true;
}
$preserv = $content;
unset ($preserv['button']);
return 'infolog.importexport_wizard_chooseowner';
}
}
}

View File

@ -2,7 +2,7 @@
/**
* eGroupWare - eTemplates for Application infolog
* http://www.egroupware.org
* generated by soetemplate::dump4setup() 2010-03-16 14:28
* generated by soetemplate::dump4setup() 2010-10-06 12:32
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package infolog
@ -51,6 +51,8 @@ $templ_data[] = array('name' => 'infolog.edit.print.project','template' => '','l
$templ_data[] = array('name' => 'infolog.edit.project','template' => '','lang' => '','group' => '0','version' => '1.5.004','data' => 'a:1:{i:0;a:6:{s:4:"type";s:4:"grid";s:4:"data";a:7:{i:0;a:8:{s:1:"A";s:3:"100";s:2:"c1";s:2:"th";s:2:"c2";s:3:"row";s:2:"c3";s:3:"row";s:2:"c4";s:3:"row";s:2:"c6";s:7:"row,top";s:2:"h6";s:3:"60%";s:2:"c5";s:3:"row";}i:1;a:2:{s:1:"A";a:3:{s:4:"type";s:5:"label";s:4:"span";s:3:"all";s:5:"label";s:14:"Projectmanager";}s:1:"B";a:1:{s:4:"type";s:5:"label";}}i:2;a:2:{s:1:"A";a:2:{s:4:"type";s:5:"label";s:5:"label";s:7:"Project";}s:1:"B";a:4:{s:4:"type";s:21:"projectmanager-select";s:4:"name";s:5:"pm_id";s:8:"onchange";s:1:"1";s:4:"size";s:4:"None";}}i:3;a:2:{s:1:"A";a:2:{s:4:"type";s:5:"label";s:5:"label";s:5:"Price";}s:1:"B";a:5:{s:4:"type";s:4:"hbox";s:4:"span";s:3:"all";s:4:"size";s:1:"2";i:1;a:4:{s:4:"type";s:24:"projectmanager-pricelist";s:4:"name";s:5:"pl_id";s:4:"size";s:4:"None";s:8:"onchange";s:207:"this.form[\'exec[info_price]\'].value=this.options[this.selectedIndex].text.lastIndexOf(\'(\') < 0 ? \'\' : this.options[this.selectedIndex].text.slice(this.options[this.selectedIndex].text.lastIndexOf(\'(\')+1,-1);";}i:2;a:3:{s:4:"type";s:5:"float";s:4:"name";s:10:"info_price";s:4:"span";s:3:"all";}}}i:4;a:2:{s:1:"A";a:3:{s:4:"type";s:5:"label";s:4:"size";s:20:",,,info_planned_time";s:5:"label";s:12:"planned time";}s:1:"B";a:3:{s:4:"type";s:13:"date-duration";s:4:"name";s:17:"info_planned_time";s:4:"size";s:23:",$cont[duration_format]";}}i:5;a:2:{s:1:"A";a:3:{s:4:"type";s:5:"label";s:4:"size";s:22:",,,info_replanned_time";s:5:"label";s:15:"Re-planned time";}s:1:"B";a:3:{s:4:"type";s:13:"date-duration";s:4:"name";s:19:"info_replanned_time";s:4:"size";s:23:",$cont[duration_format]";}}i:6;a:2:{s:1:"A";a:4:{s:4:"type";s:5:"label";s:4:"size";s:17:",,,info_used_time";s:5:"label";s:9:"used time";s:4:"help";s:64:"Leave blank to get the used time calculated by timesheet entries";}s:1:"B";a:2:{s:4:"type";s:13:"date-duration";s:4:"name";s:14:"info_used_time";}}}s:4:"rows";i:6;s:4:"cols";i:2;s:4:"size";s:8:"100%,245";s:7:"options";a:2:{i:0;s:4:"100%";i:1;s:3:"245";}}}','size' => '100%,245','style' => '','modified' => '1223093893',);
$templ_data[] = array('name' => 'infolog.importexport_wizard_chooseowner','template' => '','lang' => '','group' => '0','version' => '1.9.001','data' => 'a:1:{i:0;a:4:{s:4:"type";s:4:"grid";s:4:"data";a:6:{i:0;a:1:{s:2:"h2";s:14:",@no_owner_map";}i:1;a:1:{s:1:"A";a:3:{s:4:"type";s:5:"label";s:4:"name";s:3:"msg";s:7:"no_lang";s:1:"1";}}i:2;a:1:{s:1:"A";a:3:{s:4:"type";s:8:"checkbox";s:5:"label";s:30:"Use field from CSV if possible";s:4:"name";s:14:"owner_from_csv";}}i:3;a:1:{s:1:"A";a:2:{s:4:"type";s:14:"select-account";s:4:"name";s:5:"owner";}}i:4;a:1:{s:1:"A";a:1:{s:4:"type";s:5:"label";}}i:5;a:1:{s:1:"A";a:3:{s:4:"type";s:11:"select-bool";s:5:"label";s:26:"Change owner when updating";s:4:"name";s:12:"change_owner";}}}s:4:"rows";i:5;s:4:"cols";i:1;}}','size' => '','style' => '','modified' => '1286389863',);
$templ_data[] = array('name' => 'infolog.index','template' => '','lang' => '','group' => '0','version' => '1.7.003','data' => 'a:1:{i:0;a:5:{s:4:"type";s:4:"grid";s:4:"data";a:7:{i:0;a:6:{s:1:"A";s:3:"90%";s:2:"h3";s:2:",1";s:2:"h2";s:6:",!@msg";s:2:"c6";s:7:"noPrint";s:2:"h4";s:7:",!@main";s:2:"h1";s:6:",!@css";}i:1;a:2:{s:1:"A";a:3:{s:4:"type";s:4:"html";s:4:"span";s:3:"all";s:4:"name";s:3:"css";}s:1:"B";a:1:{s:4:"type";s:5:"label";}}i:2;a:2:{s:1:"A";a:5:{s:4:"type";s:5:"label";s:4:"span";s:13:"all,redItalic";s:5:"align";s:6:"center";s:4:"name";s:3:"msg";s:7:"no_lang";s:1:"1";}s:1:"B";a:1:{s:4:"type";s:5:"label";}}i:3;a:2:{s:1:"A";a:2:{s:4:"type";s:8:"template";s:4:"name";s:11:"header_left";}s:1:"B";a:2:{s:4:"type";s:8:"template";s:4:"name";s:12:"header_right";}}i:4;a:2:{s:1:"A";a:4:{s:4:"type";s:8:"template";s:4:"size";s:4:"main";s:4:"span";s:3:"all";s:4:"name";s:27:"infolog.index.rows-noheader";}s:1:"B";a:1:{s:4:"type";s:5:"label";}}i:5;a:2:{s:1:"A";a:4:{s:4:"type";s:9:"nextmatch";s:4:"size";s:20:"infolog.index.rows,1";s:4:"span";s:3:"all";s:4:"name";s:2:"nm";}s:1:"B";a:1:{s:4:"type";s:5:"label";}}i:6;a:2:{s:1:"A";a:5:{s:4:"type";s:4:"hbox";s:4:"size";s:1:"2";s:4:"span";s:3:"all";i:1;a:5:{s:4:"type";s:6:"button";s:5:"label";s:3:"Add";s:4:"name";s:9:"add[note]";s:4:"help";s:15:"Add a new Entry";s:7:"onclick";s:245:"window.open(egw::link(\'/index.php\',\'menuaction=infolog.infolog_ui.edit&type=note&action=$cont[action]&action_id=$cont[action_id]&cat_id={$cont[nm][cat_id]}\'),\'_blank\',\'dependent=yes,width=750,height=600,scrollbars=yes,status=yes\'); return false;";}i:2;a:4:{s:4:"type";s:6:"button";s:5:"label";s:6:"Cancel";s:4:"name";s:6:"cancel";s:4:"help";s:17:"Back to main list";}}s:1:"B";a:1:{s:4:"type";s:5:"label";}}}s:4:"rows";i:6;s:4:"cols";i:2;s:4:"size";s:12:"100%,,0,,0,0";}}','size' => '100%,,0,,0,0','style' => '','modified' => '1242996117',);
$templ_data[] = array('name' => 'infolog.index.dates','template' => '','lang' => '','group' => '0','version' => '1.7.001','data' => 'a:1:{i:0;a:10:{s:4:"type";s:4:"hbox";s:4:"data";a:2:{i:0;a:0:{}i:1;a:1:{s:1:"A";a:1:{s:4:"type";s:5:"label";}}}s:4:"rows";i:1;s:4:"cols";i:1;s:4:"size";s:1:"4";i:1;a:2:{s:4:"type";s:5:"label";s:5:"label";s:5:"Start";}i:2;a:2:{s:4:"type";s:4:"date";s:4:"name";s:9:"startdate";}i:3;a:2:{s:4:"type";s:5:"label";s:5:"label";s:3:"End";}i:4;a:3:{s:4:"type";s:4:"date";s:4:"name";s:7:"enddate";s:4:"help";s:30:"Leave it empty for a full week";}s:4:"span";s:12:",custom_hide";}}','size' => '','style' => '.custom_hide { visibility: hidden; }','modified' => '1257761831',);