[2] | 1 | <?php |
---|
| 2 | /**************************************************************************\ |
---|
| 3 | * eGroupWare - Calendar: CSV - Import * |
---|
| 4 | * http://www.egroupware.org * |
---|
| 5 | * Written by Ralf Becker <RalfBecker@outdoor-training.de> * |
---|
| 6 | * -------------------------------------------- * |
---|
| 7 | * This program is free software; you can redistribute it and/or modify it * |
---|
| 8 | * under the terms of the GNU General Public License as published by the * |
---|
| 9 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
| 10 | * option) any later version. * |
---|
| 11 | \**************************************************************************/ |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | $GLOBALS['phpgw_info']['flags'] = array( |
---|
| 15 | 'currentapp' => 'calendar', |
---|
| 16 | 'noheader' => True |
---|
| 17 | ); |
---|
| 18 | include('../header.inc.php'); |
---|
| 19 | |
---|
| 20 | if (!isset($GLOBALS['phpgw_info']['user']['apps']['admin']) || |
---|
| 21 | !$GLOBALS['phpgw_info']['user']['apps']['admin']) // no admin |
---|
| 22 | { |
---|
| 23 | $GLOBALS['phpgw']->redirect_link('/home.php'); |
---|
| 24 | } |
---|
| 25 | if (isset($_FILES['csvfile']['tmp_name'])) |
---|
| 26 | { |
---|
| 27 | $csvfile = $GLOBALS['phpgw_info']['server']['temp_dir'].'/calendar_import_'.basename($csvfile); |
---|
| 28 | $GLOBALS['phpgw']->session->appsession('csvfile','',$csvfile); |
---|
| 29 | $_POST['action'] = move_uploaded_file($_FILES['csvfile']['tmp_name'],$csvfile) ? |
---|
| 30 | 'download' : ''; |
---|
| 31 | } |
---|
| 32 | else |
---|
| 33 | { |
---|
| 34 | $csvfile = $GLOBALS['phpgw']->session->appsession('csvfile'); |
---|
| 35 | } |
---|
| 36 | if ($_POST['cancel']) |
---|
| 37 | { |
---|
| 38 | @unlink($csvfile); |
---|
| 39 | $GLOBALS['phpgw']->redirect_link('/admin/index.php'); |
---|
| 40 | } |
---|
| 41 | $GLOBALS['phpgw_info']['flags']['app_header'] = $GLOBALS['phpgw_info']['apps']['calendar']['title'].' - '.lang('Import CSV-File'); |
---|
| 42 | $GLOBALS['phpgw']->common->phpgw_header(); |
---|
| 43 | |
---|
| 44 | $GLOBALS['phpgw']->template->set_file(array('import_t' => 'csv_import.tpl')); |
---|
| 45 | $GLOBALS['phpgw']->template->set_block('import_t','filename','filenamehandle'); |
---|
| 46 | $GLOBALS['phpgw']->template->set_block('import_t','fheader','fheaderhandle'); |
---|
| 47 | $GLOBALS['phpgw']->template->set_block('import_t','fields','fieldshandle'); |
---|
| 48 | $GLOBALS['phpgw']->template->set_block('import_t','ffooter','ffooterhandle'); |
---|
| 49 | $GLOBALS['phpgw']->template->set_block('import_t','imported','importedhandle'); |
---|
| 50 | $GLOBALS['phpgw']->template->set_block('import_t','import','importhandle'); |
---|
| 51 | |
---|
| 52 | if(($_POST['action'] == 'download' || $_POST['action'] == 'continue') && (!$_POST['fieldsep'] || !$csvfile || !($fp=fopen($csvfile,'rb')))) |
---|
| 53 | { |
---|
| 54 | $_POST['action'] = ''; |
---|
| 55 | } |
---|
| 56 | $GLOBALS['phpgw']->template->set_var("action_url",$GLOBALS['phpgw']->link("/calendar/csv_import.php")); |
---|
| 57 | |
---|
| 58 | $PSep = '||'; // Pattern-Separator, separats the pattern-replacement-pairs in trans |
---|
| 59 | $ASep = '|>'; // Assignment-Separator, separats pattern and replacesment |
---|
[5934] | 60 | $VPre = '|#'; // Value-Prefix, is expanded to \ for preg_replace |
---|
[2] | 61 | $CPre = '|['; $CPreReg = '\|\['; // |{csv-fieldname} is expanded to the value of the csv-field |
---|
| 62 | $CPos = ']'; $CPosReg = '\]'; // if used together with @ (replacement is eval-ed) value gets autom. quoted |
---|
| 63 | |
---|
| 64 | function addr_id( $n_family,$n_given,$org_name ) |
---|
| 65 | { // find in Addressbook, at least n_family AND (n_given OR org_name) have to match |
---|
| 66 | $contacts = createobject('phpgwapi.contacts'); |
---|
| 67 | |
---|
| 68 | $addrs = $contacts->read(0,0,array('id'),'',"n_family=$n_family,n_given=$n_given,org_name=$org_name"); |
---|
| 69 | if(!count($addrs)) |
---|
| 70 | { |
---|
| 71 | $addrs = $contacts->read(0,0,array('id'),'',"n_family=$n_family,n_given=$n_given"); |
---|
| 72 | } |
---|
| 73 | if(!count($addrs)) |
---|
| 74 | { |
---|
| 75 | $addrs = $contacts->read(0,0,array('id'),'',"n_family=$n_family,org_name=$org_name"); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | if (count($addrs)) |
---|
| 79 | { |
---|
| 80 | return $addrs[0]['id']; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | return False; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | $cat2id = array( ); |
---|
| 87 | |
---|
| 88 | function cat_id($cats) |
---|
| 89 | { |
---|
| 90 | if (!$cats) |
---|
| 91 | { |
---|
| 92 | return ''; |
---|
| 93 | } |
---|
| 94 | |
---|
[6057] | 95 | $cats = preg_split('/[,;]/',$cats); |
---|
[2] | 96 | |
---|
| 97 | foreach($cats as $k => $cat) |
---|
| 98 | { |
---|
| 99 | if (isset($cat2id[$cat])) |
---|
| 100 | { |
---|
| 101 | $ids[$cat] = $cat2id[$cat]; // cat is in cache |
---|
| 102 | } |
---|
| 103 | else |
---|
| 104 | { |
---|
| 105 | if (!is_object($GLOBALS['phpgw']->categories)) |
---|
| 106 | { |
---|
| 107 | $GLOBALS['phpgw']->categories = createobject('phpgwapi.categories'); |
---|
| 108 | } |
---|
| 109 | if ($id = $GLOBALS['phpgw']->categories->name2id( addslashes($cat) )) |
---|
| 110 | { // cat exists |
---|
| 111 | $cat2id[$cat] = $ids[$cat] = $id; |
---|
| 112 | } |
---|
| 113 | else |
---|
| 114 | { // create new cat |
---|
| 115 | $GLOBALS['phpgw']->categories->add( array('name' => $cat,'descr' => $cat )); |
---|
| 116 | $cat2id[$cat] = $ids[$cat] = $GLOBALS['phpgw']->categories->name2id( addslashes($cat) ); |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | $id_str = implode( ',',$ids ); |
---|
| 121 | |
---|
| 122 | if (count($ids) > 1) // multiple cats need to be in ',' |
---|
| 123 | { |
---|
| 124 | $id_str = ",$id_str,"; |
---|
| 125 | } |
---|
| 126 | return $id_str; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | if (!is_object($GLOBALS['phpgw']->html)) |
---|
| 130 | { |
---|
| 131 | $GLOBALS['phpgw']->html = CreateObject('phpgwapi.html'); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | if ($_POST['next']) $_POST['action'] = 'next'; |
---|
| 135 | switch ($_POST['action']) |
---|
| 136 | { |
---|
| 137 | case '': // Start, ask Filename |
---|
| 138 | $GLOBALS['phpgw']->template->set_var('lang_csvfile',lang('CSV-Filename')); |
---|
| 139 | $GLOBALS['phpgw']->template->set_var('lang_fieldsep',lang('Fieldseparator')); |
---|
| 140 | $GLOBALS['phpgw']->template->set_var('lang_charset',lang('Charset of file')); |
---|
| 141 | $GLOBALS['phpgw']->template->set_var('select_charset', |
---|
| 142 | $GLOBALS['phpgw']->html->select('charset','', |
---|
| 143 | $GLOBALS['phpgw']->translation->get_installed_charsets()+ |
---|
| 144 | array('utf-8' => 'utf-8 (Unicode)'),True)); |
---|
| 145 | $GLOBALS['phpgw']->template->set_var('fieldsep',$_POST['fieldsep'] ? $_POST['fieldsep'] : ','); |
---|
| 146 | $GLOBALS['phpgw']->template->set_var('submit',lang('Import')); |
---|
| 147 | $GLOBALS['phpgw']->template->set_var('enctype','ENCTYPE="multipart/form-data"'); |
---|
| 148 | |
---|
| 149 | $GLOBALS['phpgw']->template->parse('rows','filename'); |
---|
| 150 | break; |
---|
| 151 | |
---|
| 152 | case 'continue': |
---|
| 153 | case 'download': |
---|
| 154 | $GLOBALS['phpgw']->preferences->read_repository(); |
---|
| 155 | $defaults = $GLOBALS['phpgw_info']['user']['preferences']['calendar']['cvs_import']; |
---|
| 156 | if (!is_array($defaults)) |
---|
| 157 | { |
---|
| 158 | $defaults = array(); |
---|
| 159 | } |
---|
| 160 | $GLOBALS['phpgw']->template->set_var('lang_csv_fieldname',lang('CSV-Fieldname')); |
---|
| 161 | $GLOBALS['phpgw']->template->set_var('lang_info_fieldname',lang('calendar-Fieldname')); |
---|
| 162 | $GLOBALS['phpgw']->template->set_var('lang_translation',lang("Translation").' <a href="#help">'.lang('help').'</a>'); |
---|
| 163 | $GLOBALS['phpgw']->template->set_var('submit', |
---|
| 164 | $GLOBALS['phpgw']->html->submit_button('convert','Import') . ' '. |
---|
| 165 | $GLOBALS['phpgw']->html->submit_button('cancel','Cancel')); |
---|
| 166 | $GLOBALS['phpgw']->template->set_var('lang_debug',lang('Test Import (show importable records <u>only</u> in browser)')); |
---|
| 167 | $GLOBALS['phpgw']->template->parse('rows','fheader'); |
---|
| 168 | |
---|
| 169 | $cal_names = array( |
---|
| 170 | 'title' => 'Title varchar(80)', |
---|
| 171 | 'description' => 'Description text', |
---|
| 172 | 'location' => 'Location varchar(255)', |
---|
| 173 | 'start' => 'Start Date: Timestamp or eg. YYYY-MM-DD hh:mm', |
---|
| 174 | 'end' => 'End Date: Timestamp or eg. YYYY-MM-DD hh:mm', |
---|
| 175 | 'participants' => 'Participants: comma separated user-id\'s or -names', |
---|
| 176 | 'category' => 'Categories: id\'s or names, comma separated (new ones got created)', |
---|
| 177 | 'priority' => 'Priority: 1=Low, 2=Normal, 3=High', |
---|
| 178 | 'public' => 'Access: 1=public, 0=private', |
---|
| 179 | 'owner' => 'Owner: int(11) user-id/-name', |
---|
| 180 | 'modtime' => 'Modification date', |
---|
| 181 | // 'groups' => 'Groups (dont know what this field is for)', |
---|
| 182 | // 'cal_type' => 'cal_type: single_event=E (default) else M', |
---|
| 183 | ); |
---|
| 184 | $custom_fields = CreateObject('calendar.bocustom_fields'); |
---|
| 185 | //echo "custom-fields=<pre>".print_r($custom_fields,True)."</pre>"; |
---|
| 186 | foreach ($custom_fields->fields as $name => $data) |
---|
| 187 | { |
---|
| 188 | if ($name[0] == '#') // a custom field ? |
---|
| 189 | { |
---|
| 190 | $cal_names[$name] = $data['name'].': Custom field, varchar('.$data['length'].')'; |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | // the next line is used in the help-text too |
---|
| 195 | $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)"; |
---|
| 196 | |
---|
| 197 | $cal_name_options = "<option value=\"\">none\n"; |
---|
| 198 | foreach($cal_names as $field => $name) |
---|
| 199 | { |
---|
| 200 | $cal_name_options .= "<option value=\"$field\">".$GLOBALS['phpgw']->strip_html($name)."\n"; |
---|
| 201 | } |
---|
| 202 | $csv_fields = fgetcsv($fp,8000,$_POST['fieldsep']); |
---|
| 203 | $csv_fields = $GLOBALS['phpgw']->translation->convert($csv_fields,$_POST['charset']); |
---|
| 204 | $csv_fields[] = 'no CSV 1'; // eg. for static assignments |
---|
| 205 | $csv_fields[] = 'no CSV 2'; |
---|
| 206 | $csv_fields[] = 'no CSV 3'; |
---|
| 207 | foreach($csv_fields as $csv_idx => $csv_field) |
---|
| 208 | { |
---|
| 209 | $GLOBALS['phpgw']->template->set_var('csv_field',$csv_field); |
---|
| 210 | $GLOBALS['phpgw']->template->set_var('csv_idx',$csv_idx); |
---|
| 211 | if ($def = $defaults[$csv_field]) |
---|
| 212 | { |
---|
| 213 | list( $info,$trans ) = explode($PSep,$def,2); |
---|
| 214 | $GLOBALS['phpgw']->template->set_var('trans',$trans); |
---|
| 215 | $GLOBALS['phpgw']->template->set_var('cal_fields',str_replace('="'.$info.'">','="'.$info.'" selected>',$cal_name_options)); |
---|
| 216 | } |
---|
| 217 | else |
---|
| 218 | { |
---|
| 219 | $GLOBALS['phpgw']->template->set_var('trans',''); |
---|
| 220 | $GLOBALS['phpgw']->template->set_var('cal_fields',$cal_name_options); |
---|
| 221 | } |
---|
| 222 | $GLOBALS['phpgw']->template->parse('rows','fields',True); |
---|
| 223 | } |
---|
| 224 | $GLOBALS['phpgw']->template->set_var('lang_start',lang('Startrecord')); |
---|
| 225 | $GLOBALS['phpgw']->template->set_var('start',get_var('start',array('POST'),1)); |
---|
| 226 | $msg = ($safe_mode = ini_get('safe_mode') == 'On') ? lang('to many might exceed your execution-time-limit'): |
---|
| 227 | lang('empty for all'); |
---|
| 228 | $GLOBALS['phpgw']->template->set_var('lang_max',lang('Number of records to read (%1)',$msg)); |
---|
| 229 | $GLOBALS['phpgw']->template->set_var('max',get_var('max',array('POST'),$safe_mode ? 200 : '')); |
---|
| 230 | $GLOBALS['phpgw']->template->set_var('debug',get_var('debug',array('POST'),True)?' checked':''); |
---|
| 231 | $GLOBALS['phpgw']->template->parse('rows','ffooter',True); |
---|
| 232 | fclose($fp); |
---|
| 233 | |
---|
| 234 | $hiddenvars = $GLOBALS['phpgw']->html->input_hidden(array( |
---|
| 235 | 'action' => 'import', |
---|
| 236 | 'fieldsep'=> $_POST['fieldsep'], |
---|
| 237 | 'charset' => $_POST['charset'] |
---|
| 238 | )); |
---|
| 239 | $help_on_trans = "<a name=\"help\"></a><b>How to use Translation's</b><p>". |
---|
| 240 | "Translations enable you to change / adapt the content of each CSV field for your needs. <br>". |
---|
| 241 | "General syntax is: <b>pattern1 ${ASep} replacement1 ${PSep} ... ${PSep} patternN ${ASep} replacementN</b><br>". |
---|
| 242 | "If the pattern-part of a pair is ommited it will match everything ('^.*$'), which is only ". |
---|
| 243 | "usefull for the last pair, as they are worked from left to right.<p>". |
---|
| 244 | "First example: <b>1${ASep}private${PSep}public</b><br>". |
---|
[204] | 245 | "This will translate a '1' in the CVS field to 'private' and everything else to 'public'.<p>". |
---|
[5934] | 246 | "Patterns as well as the replacement can be regular expressions (the replacement is done via preg_replace). ". |
---|
[2] | 247 | "If, after all replacements, the value starts with an '@' the whole value is eval()'ed, so you ". |
---|
| 248 | "may use all php, phpgw plus your own functions. This is quiet powerfull, but <u>circumvents all ACL</u>.<p>". |
---|
| 249 | "Example using regular expressions and '@'-eval(): <br><b>$mktime_lotus</b><br>". |
---|
| 250 | "It will read a date of the form '2001-05-20 08:00:00.00000000000000000' (and many more, see the regular expr.). ". |
---|
| 251 | "The [ .:-]-separated fields are read and assigned in different order to @mktime(). Please note to use ". |
---|
| 252 | "${VPre} insted of a backslash (I couldn't get backslash through all the involved templates and forms.) ". |
---|
| 253 | "plus the field-number of the pattern.<p>". |
---|
| 254 | "In addintion to the fields assign by the pattern of the reg.exp. you can use all other CSV-fields, with the ". |
---|
| 255 | "syntax <b>${CPre}CVS-FIELDNAME$CPos</b>. Here is an example: <br>". |
---|
| 256 | "<b>.+$ASep${CPre}Company$CPos: ${CPre}NFamily$CPos, ${CPre}NGiven$CPos$PSep${CPre}NFamily$CPos, ${CPre}NGiven$CPos</b><br>". |
---|
| 257 | "It is used on the CVS-field 'Company' and constructs a something like <i>Company: FamilyName, GivenName</i> or ". |
---|
| 258 | "<i>FamilyName, GivenName</i> if 'Company' is empty.<p>". |
---|
| 259 | "You can use the 'No CVS #'-fields to assign cvs-values to more than on field, the following example uses the ". |
---|
| 260 | "cvs-field 'Note' (which gots already assingned to the description) and construct a short subject: ". |
---|
| 261 | "<b>@substr(${CPre}Note$CPos,0,60).' ...'</b><p>". |
---|
| 262 | "Their is two important user-function for the calendar:<br>". |
---|
| 263 | "<b>@addr_id(${CPre}NFamily$CPos,${CPre}NGiven$CPos,${CPre}Company$CPos)</b> ". |
---|
| 264 | "searches the addressbook for an address and returns the id if it founds an exact match of at least ". |
---|
| 265 | "<i>NFamily</i> AND (<i>NGiven</i> OR <i>Company</i>). This is necessary to link your imported calendar-entrys ". |
---|
| 266 | "with the addressbook.<br>". |
---|
| 267 | "<b>@cat_id(Cat1,...,CatN)</b> returns a (','-separated) list with the cat_id's. If a category isn't found, it ". |
---|
| 268 | "will be automaticaly added. This function is automaticaly called if the category is not numerical!<p>". |
---|
| 269 | "I hope that helped to understand the features, if not <a href='mailto:RalfBecker@outdoor-training.de'>ask</a>."; |
---|
| 270 | |
---|
| 271 | $GLOBALS['phpgw']->template->set_var('help_on_trans',lang($help_on_trans)); // I don't think anyone will translate this |
---|
| 272 | break; |
---|
| 273 | |
---|
| 274 | case 'next': |
---|
| 275 | $_POST['cal_fields'] = unserialize(stripslashes($_POST['cal_fields'])); |
---|
| 276 | $_POST['trans'] = unserialize(stripslashes($_POST['trans'])); |
---|
| 277 | // fall-through |
---|
| 278 | case 'import': |
---|
| 279 | $hiddenvars = $GLOBALS['phpgw']->html->input_hidden(array( |
---|
| 280 | 'action' => 'continue', |
---|
| 281 | 'fieldsep'=> $_POST['fieldsep'], |
---|
| 282 | 'charset' => $_POST['charset'], |
---|
| 283 | 'start' => $_POST['start']+(!$_POST['debug'] ? $_POST['max'] : 0), |
---|
| 284 | 'max' => $_POST['max'], |
---|
| 285 | 'debug' => $_POST['debug'], |
---|
| 286 | 'cal_fields' => $_POST['cal_fields'], |
---|
| 287 | 'trans' => $_POST['trans'] |
---|
| 288 | )); |
---|
| 289 | @set_time_limit(0); |
---|
| 290 | $fp=fopen($csvfile,'r'); |
---|
| 291 | $csv_fields = fgetcsv($fp,8000,$_POST['fieldsep']); |
---|
| 292 | $csv_fields = $GLOBALS['phpgw']->translation->convert($csv_fields,$_POST['charset']); |
---|
| 293 | $csv_fields[] = 'no CSV 1'; // eg. for static assignments |
---|
| 294 | $csv_fields[] = 'no CSV 2'; |
---|
| 295 | $csv_fields[] = 'no CSV 3'; |
---|
| 296 | |
---|
| 297 | $cal_fields = array_diff($_POST['cal_fields'],array( '' )); // throw away empty / not assigned entrys |
---|
| 298 | |
---|
| 299 | $defaults = array(); |
---|
| 300 | foreach($cal_fields as $csv_idx => $info) |
---|
| 301 | { // convert $trans[$csv_idx] into array of pattern => value |
---|
| 302 | $defaults[$csv_fields[$csv_idx]] = $info; |
---|
| 303 | if ($_POST['trans'][$csv_idx]) |
---|
| 304 | { |
---|
| 305 | $defaults[$csv_fields[$csv_idx]] .= $PSep.addslashes($_POST['trans'][$csv_idx]); |
---|
| 306 | } |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | $GLOBALS['phpgw']->preferences->read_repository(); |
---|
| 310 | $GLOBALS['phpgw']->preferences->add('calendar','cvs_import',$defaults); |
---|
| 311 | $GLOBALS['phpgw']->preferences->save_repository(True); |
---|
| 312 | |
---|
| 313 | $log = "<table border=1>\n\t<tr><td>#</td>\n"; |
---|
| 314 | |
---|
| 315 | foreach($cal_fields as $csv_idx => $info) |
---|
| 316 | { // convert $trans[$csv_idx] into array of pattern => value |
---|
| 317 | // if (!$debug) echo "<p>$csv_idx: ".$csv_fields[$csv_idx].": $info".($trans[$csv_idx] ? ': '.$trans[$csv_idx] : '')."</p>"; |
---|
| 318 | $pat_reps = explode($PSep,stripslashes($_POST['trans'][$csv_idx])); |
---|
| 319 | $replaces = ''; $values = ''; |
---|
| 320 | if ($pat_reps[0] != '') |
---|
| 321 | { |
---|
| 322 | foreach($pat_reps as $k => $pat_rep) |
---|
| 323 | { |
---|
| 324 | list($pattern,$replace) = explode($ASep,$pat_rep,2); |
---|
| 325 | if ($replace == '') |
---|
| 326 | { |
---|
| 327 | $replace = $pattern; $pattern = '^.*$'; |
---|
| 328 | } |
---|
| 329 | $values[$pattern] = $replace; // replace two with only one, added by the form |
---|
| 330 | $replaces .= ($replaces != '' ? $PSep : '') . $pattern . $ASep . $replace; |
---|
| 331 | } |
---|
| 332 | $trans[$csv_idx] = $values; |
---|
| 333 | } /*else |
---|
| 334 | unset( $trans[$csv_idx] );*/ |
---|
| 335 | |
---|
| 336 | $log .= "\t\t<td><b>$info</b></td>\n"; |
---|
| 337 | } |
---|
| 338 | if (!in_array('public',$cal_fields)) // autocreate public access if not set by user |
---|
| 339 | { |
---|
| 340 | $log .= "\t\t<td><b>isPublic</b></td>\n"; |
---|
| 341 | } |
---|
| 342 | /* maybe later ;-) |
---|
| 343 | if (!in_array('cal_type',$cal_fields)) // autocreate single event if not set by user |
---|
| 344 | { |
---|
| 345 | $log .= "\t\t<td><b>recureing</b></td>\n"; |
---|
| 346 | } |
---|
| 347 | */ |
---|
| 348 | $start = $_POST['start'] < 1 ? 1 : $_POST['start']; |
---|
| 349 | |
---|
| 350 | // ignore empty lines, is_null($fields[0]) is returned on empty lines !!! |
---|
| 351 | for($i = 1; $i < $start; ++$i) // overread lines before our start-record |
---|
| 352 | { |
---|
| 353 | while(($fields = fgetcsv($fp,8000,$_POST['fieldsep'])) && is_null($fields[0])) ; |
---|
| 354 | } |
---|
| 355 | for($anz = 0; !$_POST['max'] || $anz < $_POST['max']; ++$anz) |
---|
| 356 | { |
---|
| 357 | while(($fields = fgetcsv($fp,8000,$_POST['fieldsep'])) && is_null($fields[0])) ; |
---|
| 358 | if (!$fields) |
---|
| 359 | { |
---|
| 360 | break; // EOF |
---|
| 361 | } |
---|
| 362 | $fields = $GLOBALS['phpgw']->translation->convert($fields,$_POST['charset']); |
---|
| 363 | |
---|
| 364 | $log .= "\t</tr><tr><td>".($start+$anz)."</td>\n"; |
---|
| 365 | |
---|
| 366 | $values = array(); |
---|
| 367 | foreach($cal_fields as $csv_idx => $info) |
---|
| 368 | { |
---|
| 369 | //echo "<p>$csv: $info".($trans[$csv] ? ': '.$trans[$csv] : '')."</p>"; |
---|
| 370 | $val = $fields[$csv_idx]; |
---|
| 371 | if (isset($trans[$csv_idx]) && is_array($trans[$csv_idx])) |
---|
| 372 | { |
---|
| 373 | foreach($trans[$csv_idx] as $pattern => $replace) |
---|
| 374 | { |
---|
[5915] | 375 | if (preg_match("/$pattern/",$val)) |
---|
[2] | 376 | { |
---|
[5934] | 377 | // echo "<p>csv_idx='$csv_idx',info='$info',trans_csv=".print_r($trans_csv).",preg_replace("/$pattern/",'$replace','$val') = "; |
---|
| 378 | $val = preg_replace((string) "/$pattern/",str_replace($VPre,'\\',$replace),(string) $val); |
---|
[2] | 379 | // echo "'$val'</p>"; |
---|
| 380 | |
---|
| 381 | $reg = $CPreReg.'([a-zA-Z_0-9]+)'.$CPosReg; |
---|
[5915] | 382 | while (preg_match("/$reg/",$val,$vars)) |
---|
[2] | 383 | { // expand all CSV fields |
---|
| 384 | $val = str_replace($CPre.$vars[1].$CPos,$val[0] == '@' ? "'".addslashes($fields[array_search($vars[1],$csv_fields)])."'" : $fields[array_search($vars[1],$csv_fields)],$val); |
---|
| 385 | } |
---|
| 386 | if ($val[0] == '@') |
---|
| 387 | { |
---|
| 388 | // removing the $ to close security hole of showing vars, which contain eg. passwords |
---|
| 389 | $val = 'return '.substr(str_replace('$','',$val),1).';'; |
---|
| 390 | // echo "<p>eval('$val')="; |
---|
| 391 | $val = eval($val); |
---|
| 392 | // echo "'$val'</p>"; |
---|
| 393 | } |
---|
| 394 | if ($pattern[0] != '@' || $val) |
---|
| 395 | break; |
---|
| 396 | } |
---|
| 397 | } |
---|
| 398 | } |
---|
| 399 | $values[$info] = $val; |
---|
| 400 | |
---|
| 401 | $log .= "\t\t<td>$val</td>\n"; |
---|
| 402 | } |
---|
| 403 | if (!in_array('public',$cal_fields)) |
---|
| 404 | { |
---|
| 405 | $values['public'] = 1; // public access if not set by user |
---|
| 406 | $log .= "\t\t<td>".$values['public']."</td>\n"; |
---|
| 407 | } |
---|
| 408 | /* maybe later ;-) |
---|
| 409 | if (!in_array('cal_type',$cal_fields)) |
---|
| 410 | { |
---|
| 411 | $values['cal_type'] = 'E'; // single event if not set by user |
---|
| 412 | $log .= "\t\t<td>".$values['cal_type']."</td>\n"; |
---|
| 413 | } |
---|
| 414 | */ |
---|
| 415 | if(!$_POST['debug'] && count($values)) // dont import empty contacts |
---|
| 416 | { |
---|
| 417 | //echo "values=<pre>".print_r($values,True)."</pre>\n"; |
---|
| 418 | if (!is_numeric($values['owner'])) |
---|
| 419 | { |
---|
| 420 | $values['owner'] = $GLOBALS['phpgw']->accounts->name2id($values['owner']); |
---|
| 421 | } |
---|
| 422 | if (!$values['owner'] || !$GLOBALS['phpgw']->accounts->exists($values['owner'])) |
---|
| 423 | { |
---|
| 424 | $values['owner'] = $GLOBALS['phpgw_info']['user']['account_id']; |
---|
| 425 | } |
---|
| 426 | if (!is_object($calendar)) |
---|
| 427 | { |
---|
| 428 | $calendar = createobject('calendar.socalendar',array()); |
---|
| 429 | $cal_owner = False; |
---|
| 430 | } |
---|
| 431 | if ($cal_owner != $values['owner']) |
---|
| 432 | { |
---|
| 433 | $calendar->open_box($cal_owner = $values['owner']); |
---|
| 434 | } |
---|
| 435 | // echo "<p>adding: ".dump_array($values)."</p>\n"; |
---|
| 436 | $calendar->event_init(); // set some reasonable defaults |
---|
| 437 | $calendar->set_recur_none(); |
---|
| 438 | |
---|
| 439 | if ($values['priority'] < 1 || $values['priority'] > 3) |
---|
| 440 | { |
---|
| 441 | $values['priority'] = 2; |
---|
| 442 | } |
---|
| 443 | // convert the category name to an id |
---|
| 444 | if ($values['category'] && !is_numeric($values['category'])) |
---|
| 445 | { |
---|
| 446 | $values['category'] = cat_id($values['category']); |
---|
| 447 | } |
---|
| 448 | if (empty($values['title'])) |
---|
| 449 | { |
---|
| 450 | $values['title'] = '('.lang('none').')'; |
---|
| 451 | } |
---|
| 452 | foreach(array('owner','title','description','location','category','priority','public') as $name) |
---|
| 453 | { |
---|
| 454 | $calendar->add_attribute($name,$values[$name]); |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | if (!isset($values['modtime'])) $values['modtime'] = $values['start']; |
---|
| 458 | // convert dates to timestamps |
---|
| 459 | foreach(array('start','end','modtime') as $date) |
---|
| 460 | { |
---|
| 461 | if (isset($values[$date]) && !is_numeric($date)) |
---|
| 462 | { |
---|
| 463 | // convert german DD.MM.YYYY format into ISO YYYY-MM-DD format |
---|
[5934] | 464 | $values[$date] = preg_replace('/([0-9]{2}).([0-9]{2}).([0-9]{4})/','\3-\2-\1',$values[$date]); |
---|
[2] | 465 | // remove fractures of seconds if present at the end of the string |
---|
[5912] | 466 | if (preg_match('/(.*)\.[0-9]+/',$values[$date],$parts)) $values[$date] = $parts[1]; |
---|
[2] | 467 | $values[$date] = strtotime($values[$date]); |
---|
| 468 | } |
---|
| 469 | $datearr = $GLOBALS['phpgw']->datetime->localdates($values[$date]); |
---|
| 470 | $calendar->set_date($date,$datearr['year'],$datearr['month'],$datearr['day'], |
---|
| 471 | $datearr['hour'],$datearr['minute'],$datearr['second']); |
---|
| 472 | } |
---|
| 473 | // convert participants-names to user-id's |
---|
| 474 | $participants = 0; |
---|
[6057] | 475 | foreach(preg_split('/[,;]/',$values['participants']) as $part_status) |
---|
[2] | 476 | { |
---|
| 477 | list($part,$status) = explode('=',$part_status); |
---|
| 478 | $valid_status = array('U'=>'U','u'=>'U','A'=>'A','a'=>'A','R'=>'R','r'=>'R','T'=>'T','t'=>'T'); |
---|
| 479 | $status = isset($valid_status[$status]) ? $valid_status[$status] : 'U'; |
---|
| 480 | if ($GLOBALS['phpgw']->accounts->exists($part)) |
---|
| 481 | { |
---|
| 482 | $part = $GLOBALS['phpgw']->accounts->name2id($part); |
---|
| 483 | } |
---|
| 484 | if ($part && is_numeric($part)) |
---|
| 485 | { |
---|
| 486 | $calendar->add_attribute('participants',$status,$part); |
---|
| 487 | $participants++; |
---|
| 488 | } |
---|
| 489 | } |
---|
| 490 | if (!$participants) // no valid participants so far --> add the importing user/owner |
---|
| 491 | { |
---|
| 492 | $calendar->add_attribute('participants','A',$values['owner']); |
---|
| 493 | } |
---|
| 494 | // add custom-fields |
---|
| 495 | foreach($values as $name => $val) |
---|
| 496 | { |
---|
| 497 | if ($name[0] == '#') |
---|
| 498 | { |
---|
| 499 | $calendar->add_attribute($name,$values[$name]); |
---|
| 500 | } |
---|
| 501 | } |
---|
| 502 | //echo "add_entry(<pre>".print_r($calendar->cal->event,True).")</pre>\n"; |
---|
| 503 | $calendar->add_entry( $calendar->cal->event ); |
---|
| 504 | } |
---|
| 505 | } |
---|
| 506 | $log .= "\t</tr>\n</table>\n"; |
---|
| 507 | |
---|
| 508 | $GLOBALS['phpgw']->template->set_var('anz_imported',($_POST['debug'] ? |
---|
| 509 | lang('%1 records read (not yet imported, you may go back and uncheck Test Import)', |
---|
| 510 | $anz,'','') : |
---|
| 511 | lang('%1 records imported',$anz)). ' '. |
---|
| 512 | (!$_POST['debug'] && $fields ? $GLOBALS['phpgw']->html->submit_button('next','Import next set') . ' ':''). |
---|
| 513 | $GLOBALS['phpgw']->html->submit_button('continue','Back') . ' '. |
---|
| 514 | $GLOBALS['phpgw']->html->submit_button('cancel','Cancel')); |
---|
| 515 | $GLOBALS['phpgw']->template->set_var('log',$log); |
---|
| 516 | $GLOBALS['phpgw']->template->parse('rows','imported'); |
---|
| 517 | break; |
---|
| 518 | } |
---|
| 519 | $GLOBALS['phpgw']->template->set_var('hiddenvars',str_replace('{','{',$hiddenvars)); |
---|
| 520 | $GLOBALS['phpgw']->template->pfp('phpgw_body','import'); |
---|
| 521 | $GLOBALS['phpgw']->common->phpgw_footer(); |
---|
| 522 | |
---|
| 523 | ?> |
---|