*
* -------------------------------------------- *
* 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' => 'calendar',
'noheader' => True
);
include('../header.inc.php');
$is_admin = isset($GLOBALS['egw_info']['user']['apps']['admin']) && $GLOBALS['egw_info']['user']['apps']['admin'];
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'] = $GLOBALS['egw_info']['apps']['calendar']['title'].' - '.lang('Import CSV-File');
$cal =& CreateObject('calendar.uical',true);
$GLOBALS['egw']->common->egw_header();
$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("/calendar/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,$org_name )
{ // find in Addressbook, at least n_family AND (n_given OR org_name) have to match
$contacts =& CreateObject('phpgwapi.contacts');
$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,n_given=$n_given");
}
if(!count($addrs))
{
$addrs = $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['egw']->categories))
{
$GLOBALS['egw']->categories =& CreateObject('phpgwapi.categories','','calendar');
}
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) );
}
}
}
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('lang_help',lang('Please note: You can configure the field assignments AFTER you uploaded the 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']['calendar']['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('calendar-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');
$cal_names = array(
'title' => 'Title varchar(80)',
'description' => 'Description text',
'location' => 'Location varchar(255)',
'start' => 'Start Date: Timestamp or eg. YYYY-MM-DD hh:mm',
'end' => 'End Date: Timestamp or eg. YYYY-MM-DD hh:mm',
'participants' => 'Participants: comma separated user-id\'s or -names',
'category' => 'Categories: id\'s or names, comma separated (new ones got created)',
'priority' => 'Priority: 1=Low, 2=Normal, 3=High',
'public' => 'Access: 1=public, 0=private',
'owner' => 'Owner: int(11) user-id/-name',
'modified' => 'Modification date',
'non_blocking' => '0=Event blocks time, 1=Event creates no conflicts',
'uid' => 'Unique Id, allows multiple import to update identical entries',
// 'recur_type'=> 'Type of recuring event',
);
$config =& CreateObject('phpgwapi.config','calendar');
$custom_fields = $config->read_repository();
unset($config);
//echo "custom-fields=
".print_r($custom_fields,True)."
";
if (is_array($custom_fields['customfields']))
foreach ($custom_fields['customfields'] as $name => $data)
{
$cal_names['#'.$name] = $data['label'].': Custom field ('.$data['type'].')';
}
// 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)";
$cal_name_options = "