*
* -------------------------------------------- *
* 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$ */
$GLOBALS['egw_info']['flags'] = array(
'currentapp' => 'addressbook',
'noheader' => True,
'enable_contacts_class' => True,
);
include('../header.inc.php');
if (isset($_FILES['csvfile']['tmp_name']))
{
$csvfile = tempnam($GLOBALS['egw_info']['server']['temp_dir'],$GLOBALS['egw_info']['flags']['currentapp']."_");
$GLOBALS['egw']->session->appsession('csvfile','',$csvfile);
$_POST['action'] = move_uploaded_file($_FILES['csvfile']['tmp_name'],$csvfile) ?
'download' : '';
}
else
{
$csvfile = $GLOBALS['egw']->session->appsession('csvfile');
}
if ($_POST['cancel'])
{
@unlink($csvfile);
$GLOBALS['egw']->redirect_link('/addressbook/index.php');
}
$GLOBALS['egw_info']['flags']['app_header'] = lang('Import CSV-File into Addressbook');
$GLOBALS['egw']->common->egw_header();
$GLOBALS['egw']->contacts = createobject('phpgwapi.contacts');
//$GLOBALS['egw']->template->set_unknowns('keep');
$GLOBALS['egw']->template->set_file(array('import' => 'csv_import.tpl'));
$GLOBALS['egw']->template->set_block('import','filename','filenamehandle');
$GLOBALS['egw']->template->set_block('import','fheader','fheaderhandle');
$GLOBALS['egw']->template->set_block('import','fields','fieldshandle');
$GLOBALS['egw']->template->set_block('import','ffooter','ffooterhandle');
$GLOBALS['egw']->template->set_block('import','imported','importedhandle');
if(($_POST['action'] == 'download' || $_POST['action'] == 'continue') && (!$_POST['fieldsep'] || !$csvfile || !($fp=fopen($csvfile,'rb'))))
{
$_POST['action'] = '';
}
$GLOBALS['egw']->template->set_var('action_url',$GLOBALS['egw']->link('/addressbook/csv_import.php'));
$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
// find in Addressbook, at least n_family AND (n_given OR org_name) have to match
function addr_id($n_family,$n_given,$org_name)
{
$addrs = $GLOBALS['egw']->contacts->search(array('n_family'=>$n_family,'n_given'=>$n_given,'org_name'=>$org_name));
if(!count($addrs))
{
$addrs = $GLOBALS['egw']->contacts->search(array('n_family'=>$n_family,'n_given'=>$n_given));
}
if(!count($addrs))
{
$addrs = $GLOBALS['egw']->contacts->search(array('n_family'=>$n_family,'org_name'=>$org_name));
}
if(count($addrs))
{
return $addrs[0]['id'];
}
return False;
}
function cat_id($cats)
{
if(!$cats)
{
return '';
}
$ids = array();
foreach(split(' *[,;] *',$cats) as $cat)
{
if (is_numeric($cat) && $GLOBALS['egw']->categories->id2name($cat) != '--')
{
$id = (int) $cat;
}
elseif ($id = $GLOBALS['egw']->categories->name2id(addslashes($cat)))
{
// cat exists
}
else
{ // create new cat
$id = $GLOBALS['egw']->categories->add(array('name' => $cat,'descr' => $cat));
}
$ids[$id] = $id; // we use the $id as index to not ass a cat twice
}
return implode(',',$ids);
}
if (!is_object($GLOBALS['egw']->html))
{
$GLOBALS['egw']->html = CreateObject('phpgwapi.html');
}
if ($_POST['next']) $_POST['action'] = 'next';
switch($_POST['action'])
{
case '': // Start, ask Filename
$GLOBALS['egw']->template->set_var('lang_csvfile',lang('CSV-Filename'));
$GLOBALS['egw']->template->set_var('lang_fieldsep',lang('Fieldseparator'));
$GLOBALS['egw']->template->set_var('lang_charset',lang('Charset of file'));
$GLOBALS['egw']->template->set_var('select_charset',
$GLOBALS['egw']->html->select('charset','',
$GLOBALS['egw']->translation->get_installed_charsets()+
array('utf-8' => 'utf-8 (Unicode)'),True));
$GLOBALS['egw']->template->set_var('fieldsep',$_POST['fieldsep'] ? $_POST['fieldsep'] : ';');
$GLOBALS['egw']->template->set_var('submit',lang('Import'));
$GLOBALS['egw']->template->set_var('csvfile',$csvfile);
$GLOBALS['egw']->template->set_var('enctype','ENCTYPE="multipart/form-data"');
$GLOBALS['egw']->template->parse('filenamehandle','filename');
break;
case 'continue':
case 'download':
$defaults = $GLOBALS['egw_info']['user']['preferences']['addressbook']['cvs_import'];
if(!is_array($defaults))
{
$defaults = array();
}
$GLOBALS['egw']->template->set_var('lang_csv_fieldname',lang('CSV-Fieldname'));
$GLOBALS['egw']->template->set_var('lang_addr_fieldname',lang('Addressbook-Fieldname'));
$GLOBALS['egw']->template->set_var('lang_translation',lang("Translation").' '.lang('help').'');
$GLOBALS['egw']->template->set_var('submit',
$GLOBALS['egw']->html->submit_button('convert','Import') . ' '.
$GLOBALS['egw']->html->submit_button('cancel','Cancel'));
$GLOBALS['egw']->template->set_var('lang_debug',lang('Test Import (show importable records only in browser)'));
$GLOBALS['egw']->template->parse('fheaderhandle','fheader');
$addr_names = $GLOBALS['egw']->contacts->contact_fields;
$addr_names['cat_id'] .= ': id or name, comma separated list';
$addr_names['private'] .= ': 0 = public, 1 = private';
$addr_names['owner'] .= ': id or account name of user or group, defaults to importing user';
$addr_names['bday'] .= ': YYYY-mm-dd';
unset($addr_names['jpegphoto']); // cant cvs import that
foreach($GLOBALS['egw']->contacts->customfields as $name => $data)
{
$addr_names['#'.$name] = $data['label'];
}
$addr_name_options = "