From 112443ed208d75bb5b371e3964668853acc0f5ad Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Mon, 11 Sep 2006 07:50:56 +0000 Subject: [PATCH] fixed not working custom-fields in the alt. csv import --- addressbook/csv_import.php | 449 +++++++++++++++++++++++++++++++++++++ 1 file changed, 449 insertions(+) create mode 100644 addressbook/csv_import.php diff --git a/addressbook/csv_import.php b/addressbook/csv_import.php new file mode 100644 index 0000000000..0927557316 --- /dev/null +++ b/addressbook/csv_import.php @@ -0,0 +1,449 @@ + * + * -------------------------------------------- * + * 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->read(0,0,array('id'),'',"n_family=$n_family,n_given=$n_given,org_name=$org_name"); + if(!count($addrs)) + { + $addrs = $GLOBALS['egw']->contacts->read(0,0,array('id'),'',"n_family=$n_family,n_given=$n_given"); + } + if(!count($addrs)) + { + $addrs = $GLOBALS['egw']->contacts->read(0,0,array('id'),'',"n_family=$n_family,org_name=$org_name"); + } + + if(count($addrs)) + { + return $addrs[0]['id']; + } + + return False; + } + + function cat_id($cats) + { + if(!$cats) + { + return ''; + } + if(!is_object($GLOBALS['egw']->categories)) + { + $GLOBALS['egw']->categories =& CreateObject('phpgwapi.categories'); + } + $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->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(); + foreach($config->config_data['customfields'] as $name => $field) + { + if ($field['type'] == 'label' || !count($field['values']) && $field['rows'] <= 1 && $field['len'] <= 0) continue; + + $addr_names[$name] = lang('Custom fields').': '.$field['label']; + } + unset($config); + + foreach($addr_names as $field => $name) + { + if($dn = display_name($field)) + { + $addr_names[$field] = $dn; + } + } + $addr_name_options = "