diff --git a/infolog/csv_import.php b/infolog/csv_import.php new file mode 100644 index 0000000000..0a33db7121 --- /dev/null +++ b/infolog/csv_import.php @@ -0,0 +1,542 @@ + * + * -------------------------------------------- * + * 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' => 'infolog', + 'noheader' => True, + 'enable_contacts_class' => True, + ); + include('../header.inc.php'); + + if (!isset($GLOBALS['egw_info']['user']['apps']['admin']) || + !$GLOBALS['egw_info']['user']['apps']['admin']) // no admin + { + $GLOBALS['egw']->redirect_link('/home.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('/admin/index.php'); + } + $GLOBALS['egw_info']['flags']['app_header'] = lang('InfoLog - Import CSV-File'); + $GLOBALS['egw']->common->egw_header(); + + $boinfolog = createobject('infolog.boinfolog'); + + $GLOBALS['egw']->template->set_file(array('import_t' => 'csv_import.tpl')); + $GLOBALS['egw']->template->set_block('import_t','filename','filenamehandle'); + $GLOBALS['egw']->template->set_block('import_t','fheader','fheaderhandle'); + $GLOBALS['egw']->template->set_block('import_t','fields','fieldshandle'); + $GLOBALS['egw']->template->set_block('import_t','ffooter','ffooterhandle'); + $GLOBALS['egw']->template->set_block('import_t','imported','importedhandle'); + $GLOBALS['egw']->template->set_block('import_t','import','importhandle'); + + + 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("/infolog/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 + +function addr_id( $n_family,$n_given=null,$org_name=null ) +{ // find in Addressbook, at least n_family AND (n_given OR org_name) have to match + static $contacts; + if (!is_object($contacts)) + { + $contacts =& CreateObject('phpgwapi.contacts'); + } + if (!is_null($org_name)) // org_name given? + { + $addrs = $contacts->read( 0,0,array('id'),'',"n_family=$n_family,n_given=$n_given,org_name=$org_name" ); + if (!count($addrs)) + { + $addrs = $contacts->read( 0,0,array('id'),'',"n_family=$n_family,org_name=$org_name",'','n_family,org_name'); + } + } + if (!is_null($n_given) && (is_null($org_name) || !count($addrs))) // first name given and no result so far + { + $addrs = $contacts->read( 0,0,array('id'),'',"n_family=$n_family,n_given=$n_given",'','n_family,n_given' ); + } + if (is_null($n_given) && is_null($org_name)) // just one name given, check against fn (= full name) + { + $addrs = $contacts->read( 0,0,array('id'),'',"fn=$n_family",'','fn' ); + } + if (count($addrs)) + { + return $addrs[0]['id']; + } + return False; +} + +function project_id($num_or_title) +{ + static $boprojects; + + if (!$num_or_title) return false; + + if (!is_object($boprojects)) + { + $boprojects =& CreateObject('projectmanager.boprojectmanager'); + } + if (($projects = $boprojects->search(array('pm_number' => $num_or_title))) || + ($projects = $boprojects->search(array('pm_title' => $num_or_title)))) + { + return $projects[0]['pm_id']; + } + return false; +} + +$cat2id = array( ); + +function cat_id($cats) +{ + if (!$cats) + { + return ''; + } + + // no multiple cat's in InfoLog atm. + foreach(array($cats) /*split('[,;]',$cats)*/ as $cat) + { + if (isset($cat2id[$cat])) + { + $ids[$cat] = $cat2id[$cat]; // cat is in cache + } + else + { + if (!is_object($GLOBALS['egw']->categories)) + { + $GLOBALS['egw']->categories = createobject('phpgwapi.categories'); + } + if (is_numeric($cat) && $GLOBALS['egw']->categories->id2name($cat) != '--') + { + $cat2id[$cat] = $ids[$cat] = $cat; + } + elseif (($id = $GLOBALS['egw']->categories->name2id( addslashes($cat) ))) + { // cat exists + $cat2id[$cat] = $ids[$cat] = $id; + } + else + { // create new cat + $GLOBALS['egw']->categories->add( array('name' => $cat,'descr' => $cat )); + $cat2id[$cat] = $ids[$cat] = $GLOBALS['egw']->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['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('enctype','ENCTYPE="multipart/form-data"'); + + $GLOBALS['egw']->template->parse('rows','filename'); + break; + + case 'continue': + case 'download': + $GLOBALS['egw']->preferences->read_repository(); + $defaults = $GLOBALS['egw_info']['user']['preferences']['infolog']['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_info_fieldname',lang('InfoLog-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('rows','fheader'); + + $info_names = array( + 'type' => 'Type: char(10) task,phone,note,confirm,reject,email,fax', + 'from' => 'From: text(255) free text if no Addressbook-entry assigned', + 'addr' => 'Addr: text(255) phone-nr/email-address', + 'subject' => 'Subject: text(255)', + 'des' => 'Description: text long free text', + 'location' => 'Location: text(255)', + 'responsible' => 'Responsible: int(11) user-id or user-name', + 'owner' => 'Owner: int(11) user-id/-name of owner, if empty current user', + 'access' => 'Access: public,private', + 'cat' => 'Category: int(11) category-id or -name (new ones got created)', + 'startdate' => 'Start Date: DateTime: Timestamp or eg. YYYY-MM-DD hh:mm', + 'enddate' => 'End Date: DateTime', + 'datecompleted'=> 'Date completed: DateTime', + 'datemodified'=> 'Date Last Modified: DateTime, if empty = Date Created', + 'modifier' => 'Modifier: int(11) user-id, if empty current user', + 'priority' => 'Priority: 3=urgent, 2=high, 1=normal, 0=low', + 'planned_time'=> 'planned Time: int(11) time used in min', + 'used_time' => 'used Time: int(11) time used in min', + 'status' => 'Status: char(10) offer,not-started,ongoing,call,will-call,done,billed,cancelled', + 'percent' => 'Percent completed: int', +// 'confirm' => 'Confirmation: char(10) not,accept,finish,both when to confirm', + 'project_id' => 'Link to Projectmanager, use Project-ID, Title or @project_id(id_or_title)', + 'addr_id' => 'Link to Addressbook, use nlast,nfirst[,org] or @addr_id(nlast,nfirst,org)', + 'link_1' => '1. link: appname:appid the entry should be linked to, eg.: addressbook:123', + 'link_2' => '2. link: appname:appid the entry should be linked to, eg.: addressbook:123', + 'link_3' => '3. link: appname:appid the entry should be linked to, eg.: addressbook:123', + ); + + // the next line is used in the help-text too + $mktime_lotus = "${PSep}0?([0-9]+)[ .:-]+0?([0-9]*)[ .:-]+0?([0-9]*)[ .:-]+0?([0-9]*)[ .:-]+0?([0-9]*)[ .:-]+0?([0-9]*).*$ASep@mktime(${VPre}4,${VPre}5,${VPre}6,${VPre}2,${VPre}3,${VPre}1)"; + + /* this are settings to import from Lotus Organizer + $defaults += array( 'Land' => "addr$PSep.*[(]+([0-9]+)[)]+$ASep+${VPre}1 (${CPre}Ortsvorwahl$CPos) ${CPre}Telefon$CPos$PSep${CPre}Telefon$CPos", + 'Notiz' => 'des', + 'Privat' => "access${PSep}1${ASep}private${PSep}public", + 'Startdatum' => 'startdate'.$mktime_lotus, + 'Enddatum' => 'enddate'.$mktime_lotus, + 'Erledigt' => "status${PSep}1${ASep}done${PSep}call", + 'Nachname' => "addr_id${PSep}@addr_id(${CPre}Nachname$CPos,${CPre}Vorname$CPos,${CPre}Firma$CPos)", + 'Firma' => "from${PSep}.+$ASep${CPre}Firma$CPos: ${CPre}Nachname$CPos, ${CPre}Vorname$CPos". + "${PSep}${CPre}Nachname$CPos, ${CPre}Vorname$CPos", + 'no CSV 1' => "type${PSep}phone", + 'no CSV 2' => "subject${PSep}@substr(${CPre}Notiz$CPos,0,60).' ...'" ); + */ + $info_name_options = "