diff --git a/addressbook/csv_import.php b/addressbook/csv_import.php index 8060b68350..9700ee4d94 100644 --- a/addressbook/csv_import.php +++ b/addressbook/csv_import.php @@ -1,434 +1,427 @@ * - * -------------------------------------------- * - * 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. * - \**************************************************************************/ +/** + * Addressbook - CSV Import + * + * @link http://www.egroupware.org + * @author Ralf Becker + * @package addressbook + * @copyright (c) 2003-8 by Ralf Becker + * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License + * @version $Id$ + */ - /* $Id$ */ - - $GLOBALS['egw_info']['flags'] = array( +$GLOBALS['egw_info'] = array( + 'flags' => array( 'currentapp' => 'addressbook', 'noheader' => True, - 'enable_contacts_class' => True, - ); - include('../header.inc.php'); + ), +); +include('../header.inc.php'); - if (isset($_FILES['csvfile']['tmp_name'])) +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 = new 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)) { - $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' : ''; + $addrs = $GLOBALS['egw']->contacts->search(array('n_family'=>$n_family,'n_given'=>$n_given)); } - else + if(!count($addrs)) { - $csvfile = $GLOBALS['egw']->session->appsession('csvfile'); + $addrs = $GLOBALS['egw']->contacts->search(array('n_family'=>$n_family,'org_name'=>$org_name)); } - if ($_POST['cancel']) + + if(count($addrs)) { - @unlink($csvfile); - $GLOBALS['egw']->redirect_link('/addressbook/index.php'); + return $addrs[0]['id']; } - $GLOBALS['egw_info']['flags']['app_header'] = lang('Import CSV-File into Addressbook'); - $GLOBALS['egw']->common->egw_header(); - $GLOBALS['egw']->contacts = createobject('phpgwapi.contacts'); + return False; +} - //$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')))) +function cat_id($cats) +{ + if(!$cats) { - $_POST['action'] = ''; + return ''; } - $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) + $ids = array(); + foreach(split(' *[,;] *',$cats) as $cat) { - $addrs = $GLOBALS['egw']->contacts->search(array('n_family'=>$n_family,'n_given'=>$n_given,'org_name'=>$org_name)); - if(!count($addrs)) + if (is_numeric($cat) && $GLOBALS['egw']->categories->id2name($cat) != '--') { - $addrs = $GLOBALS['egw']->contacts->search(array('n_family'=>$n_family,'n_given'=>$n_given)); + $id = (int) $cat; } - if(!count($addrs)) + elseif ($id = $GLOBALS['egw']->categories->name2id(addslashes($cat))) { - $addrs = $GLOBALS['egw']->contacts->search(array('n_family'=>$n_family,'org_name'=>$org_name)); + // cat exists } - - if(count($addrs)) - { - return $addrs[0]['id']; + else + { // create new cat + $id = $GLOBALS['egw']->categories->add(array('name' => $cat,'descr' => $cat)); } - - return False; + $ids[$id] = $id; // we use the $id as index to not ass a cat twice } + return implode(',',$ids); +} - function cat_id($cats) - { - if(!$cats) +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', + 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)) { - return ''; + $defaults = array(); } - $ids = array(); - foreach(split(' *[,;] *',$cats) as $cat) + $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', + html::submit_button('convert','Import') . ' '. + 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) { - if (is_numeric($cat) && $GLOBALS['egw']->categories->id2name($cat) != '--') + $addr_names['#'.$name] = $data['label']; + } + $addr_name_options = "