* * -------------------------------------------- * * 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$ */ $phpgw_info['flags']['currentapp'] = 'addressbook'; $phpgw_info['flags']['enable_contacts_class'] = True; include('../header.inc.php'); $phpgw->contacts = createobject('phpgwapi.contacts'); $t = CreateObject('phpgwapi.Template',PHPGW_APP_TPL); // $t->unknows = 'keep'; $t->debug = 1; $t->set_file(array('import' => 'csv_import.tpl')); $t->set_block('import','filename','filenamehandle'); $t->set_block('import','fheader','fheaderhandle'); $t->set_block('import','fields','fieldshandle'); $t->set_block('import','ffooter','ffooterhandle'); $t->set_block('import','imported','importedhandle'); // $t->set_var("navbar_bg",$phpgw_info["theme"]["navbar_bg"]); // $t->set_var("navbar_text",$phpgw_info["theme"]["navbar_next"]); if ($action == 'download' && (!$fieldsep || !$csvfile || !($fp=fopen($csvfile,'r')))) { $action = ''; } $t->set_var('action_url',$phpgw->link('/addressbook/csv_import.php')); $t->set_var('lang_addr_action',lang('Import CSV-File into Addressbook')); $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 function dump_array( $arr ) { while (list($key,$val) = each($arr)) { $ret .= ($ret ? ',' : '(') . "'$key' => '$val'\n"; } return $ret.')'; } function index( $value,$arr ) { while (list ($key,$val) = each($arr)) { if ($value == $val) { return $key; } } return False; } // 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 = $phpgw->contacts->read( 0,0,array('id'),'',"n_family=$n_family,n_given=$n_given,org_name=$org_name" ); if (!count($addrs)) { $addrs = $phpgw->contacts->read( 0,0,array('id'),'',"n_family=$n_family,n_given=$n_given" ); } if (!count($addrs)) { $addrs = $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 ''; } $cats = explode(',',$cats); while (list($k,$cat) = each($cats)) { if (isset($cat2id[$cat])) { $ids[$cat] = $cat2id[$cat]; // cat is in cache } else { if (!is_object($phpgw->categories)) { $phpgw->categories = createobject('phpgwapi.categories'); } if ($id = $phpgw->categories->name2id( $cat )) { // cat exists $cat2id[$cat] = $ids[$cat] = $id; } else { // create new cat $phpgw->categories->add( $cat,0,$cat,'','public',0); $cat2id[$cat] = $ids[$cat] = $phpgw->categories->name2id( $cat ); } } } return implode( ',',$ids ); } switch ($action) { case '': // Start, ask Filename $t->set_var('lang_csvfile',lang('CSV-Filename')); $t->set_var('lang_fieldsep',lang('Fieldseparator')); $t->set_var('fieldsep',$fieldsep ? $fieldsep : ','); $t->set_var('submit',lang('Download')); $t->set_var('csvfile',$csvfile); $t->set_var('enctype','ENCTYPE="multipart/form-data"'); $hiddenvars .= ''."\n"; $t->parse('filenamehandle','filename'); break; case 'download': $pref_file = '/tmp/csv_import_addrbook.php'; if (is_readable($pref_file) && ($prefs = fopen($pref_file,'r'))) { eval("fread\(\$prefs,8000\);"); // echo "

defaults = array".dump_array($defaults)."

\n"; } $t->set_var('lang_csv_fieldname',lang('CSV-Fieldname')); $t->set_var('lang_addr_fieldname',lang('Addressbook-Fieldname')); $t->set_var('lang_translation',lang("Translation").' '.lang('help').''); $t->set_var('submit',lang('Import')); $t->set_var('lang_debug',lang('Test Import (show importable records only in browser)')); $t->parse('fheaderhandle','fheader'); $hiddenvars .= ''."\n" . '\n" . '\n"; $addr_names = $phpgw->contacts->stock_contact_fields + array( 'cat_id' => 'Categories: @cat_id(Cat1,Cat2)', 'access' => 'Access: public,private', 'owner' => 'Owner: defaults to user' ); while (list($field,$name) = each($addr_names)) { if ($dn = display_name($field)) { $addr_names[$field] = $dn; } } $addr_name_options = "