diff --git a/addressbook/csv_import.php b/addressbook/csv_import.php new file mode 100644 index 0000000000..accdb1772e --- /dev/null +++ b/addressbook/csv_import.php @@ -0,0 +1,462 @@ + * + * -------------------------------------------- * + * 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['phpgw_info']['flags'] = array( + 'currentapp' => 'addressbook', + 'noheader' => True, + 'enable_contacts_class' => True, + ); + include('../header.inc.php'); + + if (isset($_FILES['csvfile']['tmp_name'])) + { + $csvfile = tempnam($GLOBALS['phpgw_info']['server']['temp_dir'],$GLOBALS['phpgw_info']['flags']['currentapp']."_"); + $GLOBALS['phpgw']->session->appsession('csvfile','',$csvfile); + $_POST['action'] = move_uploaded_file($_FILES['csvfile']['tmp_name'],$csvfile) ? + 'download' : ''; + } + else + { + $csvfile = $GLOBALS['phpgw']->session->appsession('csvfile'); + } + if ($_POST['cancel']) + { + @unlink($csvfile); + $GLOBALS['phpgw']->redirect_link('/addressbook/index.php'); + } + $GLOBALS['phpgw_info']['flags']['app_header'] = lang('Import CSV-File into Addressbook'); + $GLOBALS['phpgw']->common->phpgw_header(); + + $GLOBALS['phpgw']->contacts = createobject('phpgwapi.contacts'); + + //$GLOBALS['phpgw']->template->set_unknowns('keep'); + $GLOBALS['phpgw']->template->set_file(array('import' => 'csv_import.tpl')); + $GLOBALS['phpgw']->template->set_block('import','filename','filenamehandle'); + $GLOBALS['phpgw']->template->set_block('import','fheader','fheaderhandle'); + $GLOBALS['phpgw']->template->set_block('import','fields','fieldshandle'); + $GLOBALS['phpgw']->template->set_block('import','ffooter','ffooterhandle'); + $GLOBALS['phpgw']->template->set_block('import','imported','importedhandle'); + + if(($_POST['action'] == 'download' || $_POST['action'] == 'continue') && (!$_POST['fieldsep'] || !$csvfile || !($fp=fopen($csvfile,'rb')))) + { + $_POST['action'] = ''; + } + $GLOBALS['phpgw']->template->set_var('action_url',$GLOBALS['phpgw']->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['phpgw']->contacts->read(0,0,array('id'),'',"n_family=$n_family,n_given=$n_given,org_name=$org_name"); + if(!count($addrs)) + { + $addrs = $GLOBALS['phpgw']->contacts->read(0,0,array('id'),'',"n_family=$n_family,n_given=$n_given"); + } + if(!count($addrs)) + { + $addrs = $GLOBALS['phpgw']->contacts->read(0,0,array('id'),'',"n_family=$n_family,org_name=$org_name"); + } + + if(count($addrs)) + { + return $addrs[0]['id']; + } + + return False; + } + + $cat2id = array(); + + function cat_id($cats) + { + if(!$cats) + { + return ''; + } + + foreach(split('[,;]',$cats) as $cat) + { + if(isset($cat2id[$cat])) + { + $ids[$cat] = $cat2id[$cat]; // cat is in cache + } + else + { + if(!is_object($GLOBALS['phpgw']->categories)) + { + $GLOBALS['phpgw']->categories = createobject('phpgwapi.categories'); + } + if (is_numeric($cat) && $GLOBALS['phpgw']->categories->id2name($cat) != '--') + { + $cat2id[$cat] = $ids[$cat] = $cat; + } + elseif ($id = $GLOBALS['phpgw']->categories->name2id(addslashes($cat))) + { // cat exists + $cat2id[$cat] = $ids[$cat] = $id; + } + else + { // create new cat + $GLOBALS['phpgw']->categories->add(array('name' => $cat,'descr' => $cat)); + $cat2id[$cat] = $ids[$cat] = $GLOBALS['phpgw']->categories->name2id(addslashes($cat)); + } + } + } + $id_str = implode(',',$ids); + + if(count($ids) > 1) // multiple cats need to be in ',' + { + $id_str = ",$id_str,"; + } + return $id_str; + } + if (!is_object($GLOBALS['phpgw']->html)) + { + $GLOBALS['phpgw']->html = CreateObject('phpgwapi.html'); + } + + if ($_POST['next']) $_POST['action'] = 'next'; + + switch($_POST['action']) + { + case '': // Start, ask Filename + $GLOBALS['phpgw']->template->set_var('lang_csvfile',lang('CSV-Filename')); + $GLOBALS['phpgw']->template->set_var('lang_fieldsep',lang('Fieldseparator')); + $GLOBALS['phpgw']->template->set_var('lang_charset',lang('Charset of file')); + $GLOBALS['phpgw']->template->set_var('select_charset', + $GLOBALS['phpgw']->html->select('charset','', + $GLOBALS['phpgw']->translation->get_installed_charsets()+ + array('utf-8' => 'utf-8 (Unicode)'),True)); + $GLOBALS['phpgw']->template->set_var('fieldsep',$_POST['fieldsep'] ? $_POST['fieldsep'] : ','); + $GLOBALS['phpgw']->template->set_var('submit',lang('Import')); + $GLOBALS['phpgw']->template->set_var('csvfile',$csvfile); + $GLOBALS['phpgw']->template->set_var('enctype','ENCTYPE="multipart/form-data"'); + + $GLOBALS['phpgw']->template->parse('filenamehandle','filename'); + break; + + case 'continue': + case 'download': + $defaults = $GLOBALS['phpgw_info']['user']['preferences']['addressbook']['cvs_import']; + if(!is_array($defaults)) + { + $defaults = array(); + } + $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").' '.lang('help').''); + $GLOBALS['phpgw']->template->set_var('submit', + $GLOBALS['phpgw']->html->submit_button('convert','Import') . ' '. + $GLOBALS['phpgw']->html->submit_button('cancel','Cancel')); + $GLOBALS['phpgw']->template->set_var('lang_debug',lang('Test Import (show importable records only in browser)')); + $GLOBALS['phpgw']->template->parse('fheaderhandle','fheader'); + + $addr_names = $GLOBALS['phpgw']->contacts->stock_contact_fields + array( + 'cat_id' => 'Categories: id\'s or names, comma separated list', + 'access' => 'Access: public, private', + 'owner' => 'Owner: user-id/-name, defaults to user', + 'address2' => 'address line 2', + 'address3' => 'address line 3', + 'ophone' => 'Other Phone' + ); + $config = CreateObject('phpgwapi.config','addressbook'); + $config->read_repository(); + while(list($name,$descr) = @each($config->config_data['custom_fields'])) + { + $addr_names[$name] = $descr; + } + unset($config); + + foreach($addr_names as $field => $name) + { + if($dn = display_name($field)) + { + $addr_names[$field] = $dn; + } + } + $addr_name_options = "