2001-05-23 22:15:07 +02:00
|
|
|
<?php
|
|
|
|
/**************************************************************************\
|
|
|
|
* phpGroupWare - Addressbook: CSV - Import *
|
|
|
|
* http://www.phpgroupware.org *
|
|
|
|
* Written by Ralf Becker <RalfBecker@outdoor-training.de> *
|
|
|
|
* -------------------------------------------- *
|
|
|
|
* This program is free software; you can redistribute it and/or modify it *
|
|
|
|
* under the terms of the GNU General Public License as published by the *
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your *
|
|
|
|
* option) any later version. *
|
|
|
|
\**************************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2001-12-05 17:19:12 +01:00
|
|
|
$GLOBALS['phpgw_info'] = array();
|
2001-12-04 02:04:21 +01:00
|
|
|
$GLOBALS['phpgw_info']['flags']['currentapp'] = 'addressbook';
|
|
|
|
$GLOBALS['phpgw_info']['flags']['enable_contacts_class'] = True;
|
2001-05-23 22:15:07 +02:00
|
|
|
include('../header.inc.php');
|
|
|
|
|
2001-12-04 02:04:21 +01:00
|
|
|
$GLOBALS['phpgw']->contacts = createobject('phpgwapi.contacts');
|
|
|
|
|
2002-06-01 13:02:10 +02:00
|
|
|
$GLOBALS['phpgw']->template->set_file(array('import_t' => 'csv_import.tpl'));
|
|
|
|
$GLOBALS['phpgw']->template->set_block('import_t','filename');
|
|
|
|
$GLOBALS['phpgw']->template->set_block('import_t','fheader');
|
|
|
|
$GLOBALS['phpgw']->template->set_block('import_t','fields');
|
|
|
|
$GLOBALS['phpgw']->template->set_block('import_t','ffooter');
|
|
|
|
$GLOBALS['phpgw']->template->set_block('import_t','imported');
|
|
|
|
$GLOBALS['phpgw']->template->set_block('import_t','import');
|
2001-12-04 02:04:21 +01:00
|
|
|
|
2002-03-19 03:58:50 +01:00
|
|
|
if($action == 'download' && (!$fieldsep || !$csvfile || !($fp=fopen($csvfile,'rb'))))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
|
|
|
$action = '';
|
2002-03-19 03:58:50 +01:00
|
|
|
}
|
2001-12-04 02:04:21 +01:00
|
|
|
$GLOBALS['phpgw']->template->set_var('action_url',$GLOBALS['phpgw']->link('/addressbook/csv_import.php'));
|
|
|
|
$GLOBALS['phpgw']->template->set_var('lang_addr_action',lang('Import CSV-File into Addressbook'));
|
2001-05-23 22:15:07 +02:00
|
|
|
|
|
|
|
$PSep = '||'; // Pattern-Separator, separats the pattern-replacement-pairs in trans
|
|
|
|
$ASep = '|>'; // Assignment-Separator, separats pattern and replacesment
|
|
|
|
$VPre = '|#'; // Value-Prefix, is expanded to \ for ereg_replace
|
|
|
|
$CPre = '|['; $CPreReg = '\|\['; // |{csv-fieldname} is expanded to the value of the csv-field
|
|
|
|
$CPos = ']'; $CPosReg = '\]'; // if used together with @ (replacement is eval-ed) value gets autom. quoted
|
|
|
|
|
2002-03-19 03:58:50 +01:00
|
|
|
function dump_array($arr)
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2002-03-19 03:58:50 +01:00
|
|
|
while(is_array($arr) && (list($key,$val) = each($arr)))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
|
|
|
$ret .= ($ret ? ',' : '(') . "'$key' => '$val'\n";
|
|
|
|
}
|
|
|
|
return $ret.')';
|
|
|
|
}
|
|
|
|
|
2002-03-19 03:58:50 +01:00
|
|
|
function index($value,$arr)
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2002-03-19 03:58:50 +01:00
|
|
|
while(is_array($arr) && (list($key,$val) = each($arr)))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2002-03-19 03:58:50 +01:00
|
|
|
if($value == $val)
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find in Addressbook, at least n_family AND (n_given OR org_name) have to match
|
2002-03-19 03:58:50 +01:00
|
|
|
function addr_id($n_family,$n_given,$org_name)
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2002-03-19 03:58:50 +01:00
|
|
|
$addrs = $GLOBALS['phpgw']->contacts->read(0,0,array('id'),'',"n_family=$n_family,n_given=$n_given,org_name=$org_name");
|
|
|
|
if(!count($addrs))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2002-03-19 03:58:50 +01:00
|
|
|
$addrs = $GLOBALS['phpgw']->contacts->read(0,0,array('id'),'',"n_family=$n_family,n_given=$n_given");
|
2001-05-23 22:15:07 +02:00
|
|
|
}
|
2002-03-19 03:58:50 +01:00
|
|
|
if(!count($addrs))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2002-03-19 03:58:50 +01:00
|
|
|
$addrs = $GLOBALS['phpgw']->contacts->read(0,0,array('id'),'',"n_family=$n_family,org_name=$org_name");
|
2001-05-23 22:15:07 +02:00
|
|
|
}
|
|
|
|
|
2002-03-19 03:58:50 +01:00
|
|
|
if(count($addrs))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
|
|
|
return $addrs[0]['id'];
|
|
|
|
}
|
|
|
|
|
2002-03-19 03:58:50 +01:00
|
|
|
return False;
|
2001-05-23 22:15:07 +02:00
|
|
|
}
|
|
|
|
|
2002-03-19 03:58:50 +01:00
|
|
|
$cat2id = array();
|
2001-05-23 22:15:07 +02:00
|
|
|
|
|
|
|
function cat_id($cats)
|
|
|
|
{
|
2002-03-19 03:58:50 +01:00
|
|
|
if(!$cats)
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2002-02-14 19:42:19 +01:00
|
|
|
$cats = split('[,;]',$cats);
|
2001-05-23 22:15:07 +02:00
|
|
|
|
2002-03-19 03:58:50 +01:00
|
|
|
while(list($k,$cat) = each($cats))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2002-03-19 03:58:50 +01:00
|
|
|
if(isset($cat2id[$cat]))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2001-12-04 02:04:21 +01:00
|
|
|
$ids[$cat] = $cat2id[$cat]; // cat is in cache
|
2001-05-23 22:15:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-03-19 03:58:50 +01:00
|
|
|
if(!is_object($GLOBALS['phpgw']->categories))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2001-12-04 02:04:21 +01:00
|
|
|
$GLOBALS['phpgw']->categories = createobject('phpgwapi.categories');
|
2002-03-19 03:58:50 +01:00
|
|
|
}
|
|
|
|
if($id = $GLOBALS['phpgw']->categories->name2id(addslashes($cat)))
|
2001-05-23 22:15:07 +02:00
|
|
|
{ // cat exists
|
|
|
|
$cat2id[$cat] = $ids[$cat] = $id;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // create new cat
|
2002-03-19 03:58:50 +01:00
|
|
|
$GLOBALS['phpgw']->categories->add(array('name' => $cat,'descr' => $cat));
|
|
|
|
$cat2id[$cat] = $ids[$cat] = $GLOBALS['phpgw']->categories->name2id(addslashes($cat));
|
2001-05-23 22:15:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-03-19 03:58:50 +01:00
|
|
|
$id_str = implode(',',$ids);
|
2002-02-14 19:42:19 +01:00
|
|
|
|
2002-03-19 03:58:50 +01:00
|
|
|
if(count($ids) > 1) // multiple cats need to be in ','
|
2002-02-14 19:42:19 +01:00
|
|
|
{
|
|
|
|
$id_str = ",$id_str,";
|
|
|
|
}
|
|
|
|
return $id_str;
|
2001-05-23 22:15:07 +02:00
|
|
|
}
|
|
|
|
|
2002-03-19 03:58:50 +01:00
|
|
|
switch($action)
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
|
|
|
case '': // Start, ask Filename
|
2001-12-04 02:04:21 +01:00
|
|
|
$GLOBALS['phpgw']->template->set_var('lang_csvfile',lang('CSV-Filename'));
|
|
|
|
$GLOBALS['phpgw']->template->set_var('lang_fieldsep',lang('Fieldseparator'));
|
|
|
|
$GLOBALS['phpgw']->template->set_var('fieldsep',$fieldsep ? $fieldsep : ',');
|
|
|
|
$GLOBALS['phpgw']->template->set_var('submit',lang('Download'));
|
|
|
|
$GLOBALS['phpgw']->template->set_var('csvfile',$csvfile);
|
|
|
|
$GLOBALS['phpgw']->template->set_var('enctype','ENCTYPE="multipart/form-data"');
|
2001-05-23 22:15:07 +02:00
|
|
|
$hiddenvars .= '<input type="hidden" name="action" value="download">'."\n";
|
|
|
|
|
2002-06-01 13:02:10 +02:00
|
|
|
$GLOBALS['phpgw']->template->parse('rows','filename');
|
2001-05-23 22:15:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'download':
|
2002-03-05 11:26:33 +01:00
|
|
|
$GLOBALS['phpgw']->preferences->read_repository();
|
|
|
|
$defaults = $GLOBALS['phpgw_info']['user']['preferences']['addressbook']['cvs_import'];
|
2002-03-19 03:58:50 +01:00
|
|
|
if(!is_array($defaults))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2002-03-05 11:26:33 +01:00
|
|
|
$defaults = array();
|
|
|
|
}
|
2001-12-04 02:04:21 +01:00
|
|
|
$GLOBALS['phpgw']->template->set_var('lang_csv_fieldname',lang('CSV-Fieldname'));
|
|
|
|
$GLOBALS['phpgw']->template->set_var('lang_addr_fieldname',lang('Addressbook-Fieldname'));
|
|
|
|
$GLOBALS['phpgw']->template->set_var('lang_translation',lang("Translation").' <a href="#help">'.lang('help').'</a>');
|
|
|
|
$GLOBALS['phpgw']->template->set_var('submit',lang('Import'));
|
|
|
|
$GLOBALS['phpgw']->template->set_var('lang_debug',lang('Test Import (show importable records <u>only</u> in browser)'));
|
2002-06-01 13:02:10 +02:00
|
|
|
$GLOBALS['phpgw']->template->parse('rows','fheader');
|
2001-05-23 22:15:07 +02:00
|
|
|
$hiddenvars .= '<input type="hidden" name="action" value="import">'."\n"
|
2002-03-05 11:26:33 +01:00
|
|
|
. '<input type="hidden" name="fieldsep" value="'.$fieldsep."\">\n";
|
2001-05-23 22:15:07 +02:00
|
|
|
|
2001-12-04 02:04:21 +01:00
|
|
|
$addr_names = $GLOBALS['phpgw']->contacts->stock_contact_fields + array(
|
2001-05-23 22:15:07 +02:00
|
|
|
'cat_id' => 'Categories: @cat_id(Cat1,Cat2)',
|
|
|
|
'access' => 'Access: public,private',
|
2002-09-02 08:07:09 +02:00
|
|
|
'owner' => 'Owner: defaults to user',
|
|
|
|
'address2' => 'address line 2',
|
|
|
|
'address3' => 'address line 3',
|
2003-04-06 14:23:58 +02:00
|
|
|
'ophone' => 'Other Phone',
|
|
|
|
'ab_id' => 'Address Id: @addr_id(n_family,n_given,org_name)'
|
2001-05-23 22:15:07 +02:00
|
|
|
);
|
|
|
|
|
2002-03-19 03:58:50 +01:00
|
|
|
while(list($field,$name) = each($addr_names))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2002-03-19 03:58:50 +01:00
|
|
|
if($dn = display_name($field))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
|
|
|
$addr_names[$field] = $dn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$addr_name_options = "<option value=\"\">none\n";
|
|
|
|
reset($addr_names);
|
2002-03-19 03:58:50 +01:00
|
|
|
while(list($field,$name) = each($addr_names))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2001-12-04 02:04:21 +01:00
|
|
|
$addr_name_options .= "<option value=\"$field\">".$GLOBALS['phpgw']->strip_html($name)."\n";
|
2001-05-23 22:15:07 +02:00
|
|
|
}
|
|
|
|
$csv_fields = fgetcsv($fp,8000,$fieldsep);
|
2002-03-19 03:58:50 +01:00
|
|
|
$csv_fields[] = 'no CSV 1'; // eg. for static assignments
|
2002-03-05 11:26:33 +01:00
|
|
|
$csv_fields[] = 'no CSV 2';
|
|
|
|
$csv_fields[] = 'no CSV 3';
|
2002-03-19 03:58:50 +01:00
|
|
|
while(list($csv_idx,$csv_field) = each($csv_fields))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2001-12-04 02:04:21 +01:00
|
|
|
$GLOBALS['phpgw']->template->set_var('csv_field',$csv_field);
|
|
|
|
$GLOBALS['phpgw']->template->set_var('csv_idx',$csv_idx);
|
2002-03-19 03:58:50 +01:00
|
|
|
if($def = $defaults[$csv_field])
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2002-03-19 03:58:50 +01:00
|
|
|
list($addr,$trans) = explode($PSep,$def,2);
|
2001-12-04 02:04:21 +01:00
|
|
|
$GLOBALS['phpgw']->template->set_var('trans',$trans);
|
|
|
|
$GLOBALS['phpgw']->template->set_var('addr_fields',str_replace('="'.$addr.'">','="'.$addr.'" selected>',$addr_name_options));
|
2001-05-23 22:15:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-12-04 02:04:21 +01:00
|
|
|
$GLOBALS['phpgw']->template->set_var('trans','');
|
|
|
|
$GLOBALS['phpgw']->template->set_var('addr_fields',$addr_name_options);
|
2002-03-19 03:58:50 +01:00
|
|
|
}
|
2002-06-01 13:02:10 +02:00
|
|
|
$GLOBALS['phpgw']->template->parse('rows','fields',True);
|
2002-03-19 03:58:50 +01:00
|
|
|
}
|
2001-12-04 02:04:21 +01:00
|
|
|
$GLOBALS['phpgw']->template->set_var('lang_start',lang('Startrecord'));
|
|
|
|
$GLOBALS['phpgw']->template->set_var('start',$start);
|
|
|
|
$GLOBALS['phpgw']->template->set_var('lang_max',lang('Number of records to read (<=200)'));
|
|
|
|
$GLOBALS['phpgw']->template->set_var('max',200);
|
2002-06-01 13:02:10 +02:00
|
|
|
$GLOBALS['phpgw']->template->parse('rows','ffooter',True);
|
2001-05-23 22:15:07 +02:00
|
|
|
fclose($fp);
|
2001-12-04 02:04:21 +01:00
|
|
|
$old = $csvfile; $csvfile = $GLOBALS['phpgw_info']['server']['temp_dir'].'/addrbook_import_'.basename($csvfile);
|
2001-05-23 22:15:07 +02:00
|
|
|
rename($old,$csvfile);
|
|
|
|
$hiddenvars .= '<input type="hidden" name="csvfile" value="'.$csvfile.'">';
|
2002-06-01 13:02:10 +02:00
|
|
|
$mktime_lotus = "${PSep}0?([0-9]+)[ .:-]+0?([0-9]*)[ .:-]+0?([0-9]*)[ .:-]+0?([0-9]*)[ .:-]+0?([0-9]*)[ .:-]+0?([0-9]*).*$ASep@mktime(${VPre}4,${VPre}5,${VPre}6,${VPre}2,${VPre}3,${VPre}1)";
|
2001-12-04 02:04:21 +01:00
|
|
|
$help_on_trans = "<a name='help'><b>How to use Translation's</b><p>".
|
2002-03-19 03:58:50 +01:00
|
|
|
"Translations enable you to change / adapt the content of each CSV field for your needs. <br>".
|
|
|
|
"General syntax is: <b>pattern1 ${ASep} replacement1 ${PSep} ... ${PSep} patternN ${ASep} replacementN</b><br>".
|
|
|
|
"If the pattern-part of a pair is ommited it will match everything ('^.*$'), which is only ".
|
|
|
|
"usefull for the last pair, as they are worked from left to right.<p>".
|
|
|
|
"First example: <b>1${ASep}private${PSep}public</b><br>".
|
|
|
|
"This will translate a '1' in the CSV field to 'privat' and everything else to 'public'.<p>".
|
|
|
|
"Patterns as well as the replacement can be regular expressions (the replacement is done via ereg_replace). ".
|
|
|
|
"If, after all replacements, the value starts with an '@' the whole value is eval()'ed, so you ".
|
|
|
|
"may use all php, phpgw plus your own functions. This is quiet powerfull, but <u>circumvents all ACL</u>.<p>".
|
|
|
|
"Example using regular expressions and '@'-eval(): <br><b>$mktime_lotus</b><br>".
|
|
|
|
"It will read a date of the form '2001-05-20 08:00:00.00000000000000000' (and many more, see the regular expr.). ".
|
|
|
|
"The [ .:-]-separated fields are read and assigned in different order to @mktime(). Please note to use ".
|
|
|
|
"${VPre} insted of a backslash (I couldn't get backslash through all the involved templates and forms.) ".
|
|
|
|
"plus the field-number of the pattern.<p>".
|
|
|
|
"In addintion to the fields assign by the pattern of the reg.exp. you can use all other CSV-fields, with the ".
|
|
|
|
"syntax <b>${CPre}CSV-FIELDNAME$CPos</b>. Here is an example: <br>".
|
|
|
|
"<b>.+$ASep${CPre}Company$CPos: ${CPre}NFamily$CPos, ${CPre}NGiven$CPos$PSep${CPre}NFamily$CPos, ${CPre}NGiven$CPos</b><br>".
|
|
|
|
"It is used on the CSV-field 'Company' and constructs a something like <i>Company: FamilyName, GivenName</i> or ".
|
|
|
|
"<i>FamilyName, GivenName</i> if 'Company' is empty.<p>".
|
|
|
|
"You can use the 'No CSV #'-fields to assign csv-values to more than on field, the following example uses the ".
|
|
|
|
"csv-field 'Note' (which gots already assingned to the description) and construct a short subject: ".
|
|
|
|
"<b>@substr(${CPre}Note$CPos,0,60).' ...'</b><p>".
|
|
|
|
"Their is one important user-function for the Info Log:<br>".
|
|
|
|
"<b>@addr_id(${CPre}NFamily$CPos,${CPre}NGiven$CPos,${CPre}Company$CPos)</b> ".
|
|
|
|
"searches the addressbook for an address and returns the id if it founds an exact match of at least ".
|
|
|
|
"<i>NFamily</i> AND (<i>NGiven</i> OR <i>Company</i>). This is necessary to link your imported InfoLog-entrys ".
|
|
|
|
"with the addressbook.<br>".
|
|
|
|
"<b>@cat_id(Cat1,...,CatN)</b> returns a (','-separated) list with the cat_id's. If a category isn't found, it ".
|
|
|
|
"will be automaticaly added.<p>".
|
|
|
|
"I hope that helped to understand the features, if not <a href='mailto:RalfBecker@outdoor-training.de'>ask</a>.";
|
2001-12-04 02:04:21 +01:00
|
|
|
|
|
|
|
$GLOBALS['phpgw']->template->set_var('help_on_trans',lang($help_on_trans)); // I don't think anyone will translate this
|
2001-05-23 22:15:07 +02:00
|
|
|
break;
|
|
|
|
case 'import':
|
2002-03-19 03:58:50 +01:00
|
|
|
$fp=fopen($csvfile,'rb');
|
2001-05-23 22:15:07 +02:00
|
|
|
$csv_fields = fgetcsv($fp,8000,$fieldsep);
|
2002-03-19 03:58:50 +01:00
|
|
|
$csv_fields[] = 'no CSV 1'; // eg. for static assignments
|
2002-03-05 11:26:33 +01:00
|
|
|
$csv_fields[] = 'no CSV 2';
|
|
|
|
$csv_fields[] = 'no CSV 3';
|
2001-05-23 22:15:07 +02:00
|
|
|
|
2002-03-19 03:58:50 +01:00
|
|
|
$addr_fields = array_diff($addr_fields,array('')); // throw away empty / not assigned entrys
|
2001-05-23 22:15:07 +02:00
|
|
|
|
2002-03-05 11:26:33 +01:00
|
|
|
$defaults = array();
|
2002-03-19 03:58:50 +01:00
|
|
|
while(list($csv_idx,$addr) = each($addr_fields))
|
2002-03-05 11:26:33 +01:00
|
|
|
{ // convert $trans[$csv_idx] into array of pattern => value
|
|
|
|
$defaults[$csv_fields[$csv_idx]] = $addr;
|
2002-03-19 03:58:50 +01:00
|
|
|
if($trans[$csv_idx])
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2002-03-05 11:26:33 +01:00
|
|
|
$defaults[$csv_fields[$csv_idx]] .= $PSep.$trans[$csv_idx];
|
2001-05-23 22:15:07 +02:00
|
|
|
}
|
|
|
|
}
|
2002-03-05 11:26:33 +01:00
|
|
|
|
|
|
|
$GLOBALS['phpgw']->preferences->read_repository();
|
|
|
|
$GLOBALS['phpgw']->preferences->add('addressbook','cvs_import',$defaults);
|
|
|
|
$GLOBALS['phpgw']->preferences->save_repository(True);
|
|
|
|
|
2001-05-23 22:15:07 +02:00
|
|
|
$log = "<table border=1>\n\t<tr><td>#</td>\n";
|
|
|
|
|
|
|
|
reset($addr_fields);
|
2002-03-19 03:58:50 +01:00
|
|
|
while(list($csv_idx,$addr) = each($addr_fields))
|
2001-05-23 22:15:07 +02:00
|
|
|
{ // convert $trans[$csv_idx] into array of pattern => value
|
|
|
|
// if (!$debug) echo "<p>$csv_idx: ".$csv_fields[$csv_idx].": $addr".($trans[$csv_idx] ? ': '.$trans[$csv_idx] : '')."</p>";
|
|
|
|
$pat_reps = explode($PSep,stripslashes($trans[$csv_idx]));
|
|
|
|
$replaces = ''; $values = '';
|
2002-03-19 03:58:50 +01:00
|
|
|
if($pat_reps[0] != '')
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2002-03-19 03:58:50 +01:00
|
|
|
while(list($k,$pat_rep) = each($pat_reps))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
|
|
|
list($pattern,$replace) = explode($ASep,$pat_rep,2);
|
2002-03-19 03:58:50 +01:00
|
|
|
if($replace == '')
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
|
|
|
$replace = $pattern; $pattern = '^.*$';
|
|
|
|
}
|
|
|
|
$values[$pattern] = $replace; // replace two with only one, added by the form
|
|
|
|
$replaces .= ($replaces != '' ? $PSep : '') . $pattern . $ASep . $replace;
|
|
|
|
}
|
|
|
|
$trans[$csv_idx] = $values;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unset( $trans[$csv_idx] );
|
|
|
|
}
|
2002-03-19 03:58:50 +01:00
|
|
|
$log .= "\t\t<td><b>$addr</b></td>\n";
|
|
|
|
}
|
2002-05-01 14:46:20 +02:00
|
|
|
if (!in_array('fn',$addr_fields)) // autocreate full name, if not set by user
|
|
|
|
{
|
|
|
|
$log .= "\t\t<td><b>fn</b></td>\n";
|
|
|
|
|
|
|
|
$auto_fn = array('n_prefix','n_given','n_middle','n_family','n_suffix');
|
|
|
|
}
|
2002-03-19 03:58:50 +01:00
|
|
|
if($start < 1)
|
|
|
|
{
|
|
|
|
$start = 1;
|
2001-05-23 22:15:07 +02:00
|
|
|
}
|
2002-03-19 03:58:50 +01:00
|
|
|
for($i = 1; $i < $start && fgetcsv($fp,8000,$fieldsep); ++$i); // overread lines before our start-record
|
2001-05-23 22:15:07 +02:00
|
|
|
|
2002-03-19 03:58:50 +01:00
|
|
|
for($anz = 0; $anz < $max && ($fields = fgetcsv($fp,8000,$fieldsep)); ++$anz)
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
|
|
|
$log .= "\t</tr><tr><td>".($start+$anz)."</td>\n";
|
|
|
|
|
|
|
|
reset($addr_fields); $values = array();
|
2002-03-19 03:58:50 +01:00
|
|
|
while(list($csv_idx,$addr) = each($addr_fields))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
|
|
|
//echo "<p>$csv: $addr".($trans[$csv] ? ': '.$trans[$csv] : '')."</p>";
|
|
|
|
$val = $fields[$csv_idx];
|
2002-03-19 03:58:50 +01:00
|
|
|
if(isset($trans[$csv_idx]))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
|
|
|
$trans_csv = $trans[$csv_idx];
|
2002-03-19 03:58:50 +01:00
|
|
|
while(list($pattern,$replace) = each($trans_csv))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2002-03-19 03:58:50 +01:00
|
|
|
if(ereg((string) $pattern,$val))
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
|
|
|
// echo "<p>csv_idx='$csv_idx',info='$addr',trans_csv=".dump_array($trans_csv).",ereg_replace('$pattern','$replace','$val') = ";
|
|
|
|
$val = ereg_replace((string) $pattern,str_replace($VPre,'\\',$replace),(string) $val);
|
|
|
|
// echo "'$val'</p>";
|
|
|
|
|
|
|
|
$reg = $CPreReg.'([a-zA-Z_0-9]+)'.$CPosReg;
|
2002-03-19 03:58:50 +01:00
|
|
|
while(ereg($reg,$val,$vars))
|
2001-05-23 22:15:07 +02:00
|
|
|
{ // expand all CSV fields
|
2002-03-19 03:58:50 +01:00
|
|
|
$val = str_replace($CPre . $vars[1] . $CPos, $val[0] == '@' ? "'"
|
|
|
|
. addslashes($fields[index($vars[1], $csv_fields)])
|
|
|
|
. "'" : $fields[index($vars[1], $csv_fields)], $val);
|
2001-05-23 22:15:07 +02:00
|
|
|
}
|
2002-03-19 03:58:50 +01:00
|
|
|
if($val[0] == '@')
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2002-05-13 16:46:47 +02:00
|
|
|
// removing the $ to close security hole of showing vars, which contain eg. passwords
|
|
|
|
$val = 'return '.substr(str_replace('$','',$val),1).';';
|
2001-05-23 22:15:07 +02:00
|
|
|
// echo "<p>eval('$val')=";
|
|
|
|
$val = eval($val);
|
|
|
|
// echo "'$val'</p>";
|
|
|
|
}
|
2002-03-19 03:58:50 +01:00
|
|
|
if($pattern[0] != '@' || $val)
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$values[$addr] = $val;
|
|
|
|
|
|
|
|
$log .= "\t\t<td>$val</td>\n";
|
|
|
|
}
|
|
|
|
// if (!isset($values['datecreated'])) $values['datecreated'] = $values['startdate'];
|
|
|
|
|
2002-05-01 14:46:20 +02:00
|
|
|
if (is_array($auto_fn) && !isset($values['fn'])) // autocreate full name
|
|
|
|
{
|
|
|
|
reset($auto_fn);
|
|
|
|
while (list($idx,$name) = each($auto_fn))
|
|
|
|
{
|
|
|
|
$values['fn'] .= ($values['fn'] != '' && $values[$name] != '' ? ' ' : '') . $values[$name];
|
|
|
|
}
|
|
|
|
$log .= "\t\t<td>".$values['fn']."</td>\n";
|
|
|
|
}
|
2002-03-19 03:58:50 +01:00
|
|
|
if(!$debug)
|
2001-05-23 22:15:07 +02:00
|
|
|
{
|
2003-04-06 14:23:58 +02:00
|
|
|
if (!$values['ab_id'])
|
|
|
|
{
|
|
|
|
$GLOBALS['phpgw']->contacts->add( $values['owner'] ? $values['owner'] : $GLOBALS['phpgw_info']['user']['account_id'],
|
|
|
|
$values,$values['access'],$values['cat_id']);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$GLOBALS['phpgw']->contacts->update( $values['ab_id'],
|
|
|
|
$values['owner'] ? $values['owner'] : $GLOBALS['phpgw_info']['user']['account_id'],
|
|
|
|
$values,$values['access'],$values['cat_id']);
|
|
|
|
}
|
2001-05-23 22:15:07 +02:00
|
|
|
// echo "<p>adding: ".dump_array($values)."</p>\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$log .= "\t</tr>\n</table>\n";
|
|
|
|
|
2003-04-06 14:32:53 +02:00
|
|
|
$GLOBALS['phpgw']->template->set_var('anz_imported',$debug ? lang('%1 records read (not yet imported, you may go %2back%3 and uncheck Test Import)',
|
2002-03-19 03:58:50 +01:00
|
|
|
$anz,'<a href="javascript:history.back()">','</a>') :
|
|
|
|
lang('%1 records imported',$anz));
|
2001-12-04 02:04:21 +01:00
|
|
|
$GLOBALS['phpgw']->template->set_var('log',$log);
|
2002-06-01 13:02:10 +02:00
|
|
|
$GLOBALS['phpgw']->template->parse('rows','imported');
|
2001-05-23 22:15:07 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-12-04 02:04:21 +01:00
|
|
|
$GLOBALS['phpgw']->template->set_var('hiddenvars',$hiddenvars);
|
2002-06-01 15:19:48 +02:00
|
|
|
$GLOBALS['phpgw']->template->fp('phpgw_body','import');
|
2001-05-23 22:15:07 +02:00
|
|
|
?>
|