Completed
Push — 16.1 ( c78edd...14e632 )
by Nathan
93:08 queued 75:41
created
importexport/inc/class.importexport_import_csv.inc.php 2 patches
Spacing   +82 added lines, -82 removed lines patch added patch discarded remove patch
@@ -83,10 +83,10 @@  discard block
 block discarded – undo
83 83
 	 * @param string $_resource resource containing data. May be each valid php-stream
84 84
 	 * @param array $_options options for the resource array with keys: charset and fieldsep
85 85
 	 */
86
-	public function __construct( $_resource,  array $_options ) {
86
+	public function __construct($_resource, array $_options) {
87 87
 		$this->resource = $_resource;
88 88
 		$this->csv_fieldsep = $_options['fieldsep'];
89
-		if($_options['charset'] == 'user') $_options['charset'] = $GLOBALS['egw_info']['user']['preferences']['common']['csv_charset'];
89
+		if ($_options['charset'] == 'user') $_options['charset'] = $GLOBALS['egw_info']['user']['preferences']['common']['csv_charset'];
90 90
 		$this->csv_charset = $_options['charset'];
91 91
 		return;
92 92
 	} // end of member function __construct
@@ -103,20 +103,20 @@  discard block
 block discarded – undo
103 103
 	 * @param mixed _position may be: {current|first|last|next|previous|somenumber}
104 104
 	 * @return mixed array with data / false if no furtor records
105 105
 	 */
106
-	public function get_record( $_position = 'next' ) {
106
+	public function get_record($_position = 'next') {
107 107
 		
108
-		if ($this->get_raw_record( $_position ) === false) {
108
+		if ($this->get_raw_record($_position) === false) {
109 109
 			return false;
110 110
 		}
111 111
 		
112 112
 		// skip empty records
113
-		if( count( array_unique( $this->record ) ) < 2 ) return $this->get_record( $_position );
113
+		if (count(array_unique($this->record)) < 2) return $this->get_record($_position);
114 114
 		
115
-		if ( !empty( $this->conversion ) ) {
115
+		if (!empty($this->conversion)) {
116 116
 			$this->do_conversions();
117 117
 		}
118 118
 		
119
-		if ( !empty( $this->mapping ) ) {
119
+		if (!empty($this->mapping)) {
120 120
 			$this->do_fieldmapping();
121 121
 		}
122 122
 		
@@ -128,9 +128,9 @@  discard block
 block discarded – undo
128 128
 	 *
129 129
 	 * @param int $_numToSkip
130 130
 	 */
131
-	public function skip_records( $_numToSkip ) {
132
-		while ( (int)$_numToSkip-- !== 0 ) {
133
-			fgetcsv( $this->resource, self::csv_max_linelength, $this->csv_fieldsep);
131
+	public function skip_records($_numToSkip) {
132
+		while ((int)$_numToSkip-- !== 0) {
133
+			fgetcsv($this->resource, self::csv_max_linelength, $this->csv_fieldsep);
134 134
 		}
135 135
 	}
136 136
 	
@@ -140,7 +140,7 @@  discard block
 block discarded – undo
140 140
 	 * @param mixed $_position
141 141
 	 * @return bool
142 142
 	 */
143
-	private function get_raw_record( $_position = 'next' ) {
143
+	private function get_raw_record($_position = 'next') {
144 144
 		switch ($_position) {
145 145
 			case 'current' :
146 146
 				if ($this->current_position == 0) {
@@ -154,7 +154,7 @@  discard block
 block discarded – undo
154 154
 				}
155 155
 				
156 156
 			case 'next' :
157
-				$csv_data = fgetcsv( $this->resource, self::csv_max_linelength, $this->csv_fieldsep);
157
+				$csv_data = fgetcsv($this->resource, self::csv_max_linelength, $this->csv_fieldsep);
158 158
 				if (!is_array($csv_data)) {
159 159
 					return false;
160 160
 				}
@@ -235,7 +235,7 @@  discard block
 block discarded – undo
235 235
 		$this->record = array();
236 236
 		foreach ($this->mapping as $cvs_idx => $new_idx)
237 237
 		{
238
-			if( $new_idx == '' ) continue;
238
+			if ($new_idx == '') continue;
239 239
 			$this->record[$new_idx] = $record[$cvs_idx];
240 240
 		}
241 241
 		return true;
@@ -247,7 +247,7 @@  discard block
 block discarded – undo
247 247
 	 * @return bool
248 248
 	 */
249 249
 	protected function do_conversions() {
250
-		if ( $record = importexport_helper_functions::conversion( $this->record, $this->conversion, $this->conversion_class )) {
250
+		if ($record = importexport_helper_functions::conversion($this->record, $this->conversion, $this->conversion_class)) {
251 251
 			$this->record = $record;
252 252
 			return;
253 253
 		}
@@ -264,25 +264,25 @@  discard block
 block discarded – undo
264 264
 	 *
265 265
 	 * @return string warnings, if any
266 266
 	 */
267
-	public static function convert(Array &$record, Array $fields = array(), $appname = null, Array $selects = array(), $format=0) {
267
+	public static function convert(Array &$record, Array $fields = array(), $appname = null, Array $selects = array(), $format = 0) {
268 268
 		$warnings = array();
269 269
 
270 270
 		// Automatic conversions
271
-		if($appname) {
271
+		if ($appname) {
272 272
 
273 273
 			// Load translations
274 274
 			Api\Translation::add_app($appname);
275 275
 
276
-			if(!self::$cf_parse_cache[$appname]) {
276
+			if (!self::$cf_parse_cache[$appname]) {
277 277
 				$c_fields = importexport_export_csv::convert_parse_custom_fields($appname, $selects, $links, $methods);
278 278
 				self::$cf_parse_cache[$appname] = array($c_fields, $selects, $links, $methods);
279 279
 			}
280 280
 			list($c_fields, $c_selects, $links, $methods) = self::$cf_parse_cache[$appname];
281 281
 
282 282
 			// Add in any fields that are keys to another app
283
-			foreach((array)$fields['links'] as $link_field => $app)
283
+			foreach ((array)$fields['links'] as $link_field => $app)
284 284
 			{
285
-				if(is_numeric($link_field)) continue;
285
+				if (is_numeric($link_field)) continue;
286 286
 				$links[$link_field] = $app;
287 287
 				// Set it as a normal link field
288 288
 				$fields['links'][] = $link_field;
@@ -290,9 +290,9 @@  discard block
 block discarded – undo
290 290
 			}
291 291
 
292 292
 			// Not quite a recursive merge, since only one level
293
-			foreach($fields as $type => &$list)
293
+			foreach ($fields as $type => &$list)
294 294
 			{
295
-				if($c_fields[$type]) {
295
+				if ($c_fields[$type]) {
296 296
 					$list = array_merge($c_fields[$type], $list);
297 297
 					unset($c_fields[$type]);
298 298
 				}
@@ -300,26 +300,26 @@  discard block
 block discarded – undo
300 300
 			$fields += $c_fields;
301 301
 			$selects += $c_selects;
302 302
 		}
303
-		if($fields) {
304
-			foreach((array)$fields['select'] as $name) {
303
+		if ($fields) {
304
+			foreach ((array)$fields['select'] as $name) {
305 305
 				$record[$name] = static::find_select_key($record[$name], $selects[$name]);
306 306
 			}
307 307
 			
308
-			foreach((array)$fields['links'] as $name) {
309
-				if($record[$name] && $links[$name])
308
+			foreach ((array)$fields['links'] as $name) {
309
+				if ($record[$name] && $links[$name])
310 310
 				{
311 311
 					// Primary key to another app, not a link
312 312
 					// Text - search for a matching record
313
-					if(!is_numeric($record[$name]))
313
+					if (!is_numeric($record[$name]))
314 314
 					{
315 315
 						$results = Link::query($links[$name], $record[$name]);
316
-						if(count($results) >= 1)
316
+						if (count($results) >= 1)
317 317
 						{
318 318
 							// More than 1 result.  Check for exact match
319 319
 							$exact_count = 0;
320
-							foreach($results as $id => $title)
320
+							foreach ($results as $id => $title)
321 321
 							{
322
-								if($title == $record[$name])
322
+								if ($title == $record[$name])
323 323
 								{
324 324
 									$exact_count++;
325 325
 									$app_id = $id;
@@ -328,11 +328,11 @@  discard block
 block discarded – undo
328 328
 								unset($results[$id]);
329 329
 							}
330 330
 							// Too many exact matches, or none good enough
331
-							if($exact_count > 1 || count($results) == 0)
331
+							if ($exact_count > 1 || count($results) == 0)
332 332
 							{
333 333
 								$warnings[] = lang('Unable to link to %1 "%2"',
334 334
 									lang($links[$name]), $record[$name]).
335
- 									' - ' .lang('too many matches');
335
+ 									' - '.lang('too many matches');
336 336
 								continue;
337 337
 							}
338 338
 							elseif ($exact_count == 1)
@@ -345,7 +345,7 @@  discard block
 block discarded – undo
345 345
 						{
346 346
 							$warnings[] = lang('Unable to link to %1 "%2"',
347 347
 								lang($links[$name]), $record[$name]).
348
- 								' - ' . lang('no matches');
348
+ 								' - '.lang('no matches');
349 349
 							continue;
350 350
 						} else {
351 351
 							$record[$name] = key($results);
@@ -353,17 +353,17 @@  discard block
 block discarded – undo
353 353
 					}
354 354
 				}
355 355
 			}
356
-			foreach((array)$fields['select-account'] as $name) {
356
+			foreach ((array)$fields['select-account'] as $name) {
357 357
 				// Compare against null to deal with empty arrays
358 358
 				if ($record[$name]) {
359 359
 					// Automatically handle text owner without explicit translation
360 360
 					$new_owner = importexport_helper_functions::account_name2id($record[$name]);
361
-					if(count($new_owner) != count(explode(',',$record[$name])))
361
+					if (count($new_owner) != count(explode(',', $record[$name])))
362 362
 					{
363 363
 						// Unable to parse value into account
364
-						$warnings[] = $name . ': ' .lang('%1 is not a known user or group', $record[$name]);
364
+						$warnings[] = $name.': '.lang('%1 is not a known user or group', $record[$name]);
365 365
 					}
366
-					if($new_owner != '') {
366
+					if ($new_owner != '') {
367 367
 						$record[$name] = $new_owner;
368 368
 					} else {
369 369
 						// Clear it to prevent trouble later on
@@ -371,24 +371,24 @@  discard block
 block discarded – undo
371 371
 					}
372 372
 				}
373 373
 			}
374
-			foreach((array)$fields['select-bool'] as $name) {
375
-				if($record[$name] != null && $record[$name] != '') {
374
+			foreach ((array)$fields['select-bool'] as $name) {
375
+				if ($record[$name] != null && $record[$name] != '') {
376 376
 					$record[$name] = ($record[$name] == lang('Yes') || $record[$name] == '1' ? 1 : 0);
377 377
 				}
378 378
 			}
379
-			foreach((array)$fields['date-time'] as $name) {
379
+			foreach ((array)$fields['date-time'] as $name) {
380 380
 				if (isset($record[$name]) && !is_numeric($record[$name]) && strlen(trim($record[$name])) > 0)
381 381
 				{
382 382
 					// Need to handle format first
383
-					if($format == 1)
383
+					if ($format == 1)
384 384
 					{
385 385
 						$formatted = Api\DateTime::createFromFormat(
386
-							'!'.Api\DateTime::$user_dateformat . ' ' .Api\DateTime::$user_timeformat,
386
+							'!'.Api\DateTime::$user_dateformat.' '.Api\DateTime::$user_timeformat,
387 387
 							$record[$name],
388 388
 							Api\DateTime::$user_timezone
389 389
 						);
390 390
 
391
-						if(!$formatted && $errors = Api\DateTime::getLastErrors())
391
+						if (!$formatted && $errors = Api\DateTime::getLastErrors())
392 392
 						{
393 393
 							// Try again, without time
394 394
 							$formatted = Api\DateTime::createFromFormat(
@@ -397,70 +397,70 @@  discard block
 block discarded – undo
397 397
 								Api\DateTime::$user_timezone
398 398
 							);
399 399
 							
400
-							if(!$formatted && $errors = Api\DateTime::getLastErrors())
400
+							if (!$formatted && $errors = Api\DateTime::getLastErrors())
401 401
 							{
402 402
 								// Try again, anything goes
403 403
 								try {
404 404
 									$formatted = new Api\DateTime($record[$name]);
405 405
 								} catch (Exception $e) {
406
-									$warnings[] = $name.': ' . $e->getMessage() . "\n" .
407
-										'Format: '.'!'.Api\DateTime::$user_dateformat . ' ' .Api\DateTime::$user_timeformat;
406
+									$warnings[] = $name.': '.$e->getMessage()."\n".
407
+										'Format: '.'!'.Api\DateTime::$user_dateformat.' '.Api\DateTime::$user_timeformat;
408 408
 									continue;
409 409
 								}
410 410
 								$errors = Api\DateTime::getLastErrors();
411
-								foreach($errors['errors'] as $char => $msg)
411
+								foreach ($errors['errors'] as $char => $msg)
412 412
 								{
413 413
 									$warnings[] = "$name: [$char] $msg\n".
414
-										'Format: '.'!'.Api\DateTime::$user_dateformat . ' ' .Api\DateTime::$user_timeformat;
414
+										'Format: '.'!'.Api\DateTime::$user_dateformat.' '.Api\DateTime::$user_timeformat;
415 415
 								}
416 416
 							}
417 417
 						}
418
-						if($formatted)
418
+						if ($formatted)
419 419
 						{
420 420
 							$record[$name] = $formatted->getTimestamp();
421 421
 							// Timestamp is apparently in server time, but apps will do the same conversion
422
-							$record[$name] = Api\DateTime::server2user($record[$name],'ts');
422
+							$record[$name] = Api\DateTime::server2user($record[$name], 'ts');
423 423
 						}
424 424
 					}
425 425
 					
426
-					if(is_array(self::$cf_parse_cache[$appname][0]['date-time']) &&
426
+					if (is_array(self::$cf_parse_cache[$appname][0]['date-time']) &&
427 427
 							in_array($name, self::$cf_parse_cache[$appname][0]['date-time'])) {
428 428
 						// Custom fields stored in a particular format (from customfields_widget)
429 429
 						$record[$name] = date('Y-m-d H:i:s', $record[$name]);
430 430
 					}
431 431
 				}
432
-				if(array_key_exists($name, $record) && strlen(trim($record[$name])) == 0)
432
+				if (array_key_exists($name, $record) && strlen(trim($record[$name])) == 0)
433 433
 				{
434 434
 					$record[$name] = null;
435 435
 				}
436 436
 			}
437
-			foreach((array)$fields['date'] as $name) {
437
+			foreach ((array)$fields['date'] as $name) {
438 438
 				if (isset($record[$name]) && !is_numeric($record[$name]) && strlen(trim($record[$name])) > 0)
439 439
 				{
440 440
 					// Need to handle format first
441
-					if($format == 1)
441
+					if ($format == 1)
442 442
 					{
443 443
 						$formatted = Api\DateTime::createFromFormat('!'.Api\DateTime::$user_dateformat, $record[$name]);
444
-						if($formatted && $errors = Api\DateTime::getLastErrors() && $errors['error_count'] == 0)
444
+						if ($formatted && $errors = Api\DateTime::getLastErrors() && $errors['error_count'] == 0)
445 445
 						{
446 446
 							$record[$name] = $formatted->getTimestamp();
447 447
 						}
448 448
 					}
449
-					$record[$name] = Api\DateTime::server2user($record[$name],'ts');
450
-					if(is_array(self::$cf_parse_cache[$appname][0]['date']) &&
449
+					$record[$name] = Api\DateTime::server2user($record[$name], 'ts');
450
+					if (is_array(self::$cf_parse_cache[$appname][0]['date']) &&
451 451
 							in_array($name, self::$cf_parse_cache[$appname][0]['date'])) {
452 452
 						// Custom fields stored in a particular format (from customfields_widget)
453 453
 						$record[$name] = date('Y-m-d', $record[$name]);
454 454
 					}
455 455
 				}
456
-				if(array_key_exists($name, $record) && strlen(trim($record[$name])) == 0)
456
+				if (array_key_exists($name, $record) && strlen(trim($record[$name])) == 0)
457 457
 				{
458 458
 					$record[$name] = null;
459 459
 				}
460 460
 			}
461
-			foreach((array)$fields['float'] as $name)
461
+			foreach ((array)$fields['float'] as $name)
462 462
 			{
463
-				if($record[$name] != null && $record[$name] != '') {
463
+				if ($record[$name] != null && $record[$name] != '') {
464 464
 					$dec_point = $GLOBALS['egw_info']['user']['preferences']['common']['number_format'][0];
465 465
 					if (empty($dec_point)) $dec_point = '.';
466 466
 					$record[$name] = floatval(str_replace($dec_point, '.', preg_replace('/[^\d'.preg_quote($dec_point).']/', '', $record[$name])));
@@ -468,23 +468,23 @@  discard block
 block discarded – undo
468 468
 			}
469 469
 
470 470
 			// Some custom methods for conversion
471
-			foreach((array)$methods as $name => $method) {
472
-				if($record[$name]) $record[$name] = ExecMethod($method, $record[$name]);
471
+			foreach ((array)$methods as $name => $method) {
472
+				if ($record[$name]) $record[$name] = ExecMethod($method, $record[$name]);
473 473
 			}
474 474
 
475 475
 			// cat_name2id will use currentapp to create new categories
476 476
 			$current_app = $GLOBALS['egw_info']['flags']['currentapp'];
477
-			if($appname) {
477
+			if ($appname) {
478 478
 				$GLOBALS['egw_info']['flags']['currentapp'] = $appname;
479 479
 			}
480
-			$categories = new Api\Categories('',$appname);
481
-			foreach((array)$fields['select-cat'] as $name) {
482
-				if($record[$name]) {
480
+			$categories = new Api\Categories('', $appname);
481
+			foreach ((array)$fields['select-cat'] as $name) {
482
+				if ($record[$name]) {
483 483
 					// Only parse name if it needs it
484
-					if($format == 1)
484
+					if ($format == 1)
485 485
 					{
486
-						$existing_cat = $categories->exists('all',$record[$name]);
487
-						if($existing_cat)
486
+						$existing_cat = $categories->exists('all', $record[$name]);
487
+						if ($existing_cat)
488 488
 						{
489 489
 							$cat_id = $existing_cat;
490 490
 						}
@@ -493,14 +493,14 @@  discard block
 block discarded – undo
493 493
 							$cat_id = importexport_helper_functions::cat_name2id($record[$name]);
494 494
 						}
495 495
 						// Don't clear it if it wasn't found
496
-						if($cat_id) $record[$name] = $cat_id;
496
+						if ($cat_id) $record[$name] = $cat_id;
497 497
 					}
498 498
 				}
499 499
 			}
500 500
 			$GLOBALS['egw_info']['flags']['currentapp'] = $current_app;
501 501
 		}
502 502
 
503
-		return implode("\n",$warnings);
503
+		return implode("\n", $warnings);
504 504
 	}
505 505
 
506 506
 	/**
@@ -514,36 +514,36 @@  discard block
 block discarded – undo
514 514
 	 */
515 515
 	protected static function find_select_key($record_value, $selects)
516 516
 	{
517
-		if($record_value != null && is_array($selects)) {
518
-			if(is_array($record_value) || is_string($record_value) && strpos($record_value, ',') !== FALSE)
517
+		if ($record_value != null && is_array($selects)) {
518
+			if (is_array($record_value) || is_string($record_value) && strpos($record_value, ',') !== FALSE)
519 519
 			{
520 520
 				// Array, or CSV
521 521
 				$key = array();
522
-				$subs = explode(',',$record_value);
523
-				for($sub_index = 0; $sub_index < count($subs); $sub_index++)
522
+				$subs = explode(',', $record_value);
523
+				for ($sub_index = 0; $sub_index < count($subs); $sub_index++)
524 524
 				{
525 525
 					$sub_key = static::find_select_key($subs[$sub_index], $selects);
526
-					if(!$sub_key)
526
+					if (!$sub_key)
527 527
 					{
528
-						$sub_key = static::find_select_key($subs[$sub_index].','.$subs[$sub_index+1], $selects);
529
-						if($sub_key) $sub_index++;
528
+						$sub_key = static::find_select_key($subs[$sub_index].','.$subs[$sub_index + 1], $selects);
529
+						if ($sub_key) $sub_index++;
530 530
 					}
531
-					if($sub_key)
531
+					if ($sub_key)
532 532
 					{
533 533
 						$key[] = $sub_key;
534 534
 					}
535 535
 				}
536 536
 				return $key;
537 537
 			}
538
-			$key = array_search(strtolower($record_value), array_map('strtolower',$selects));
539
-			if($key !== false)
538
+			$key = array_search(strtolower($record_value), array_map('strtolower', $selects));
539
+			if ($key !== false)
540 540
 			{
541 541
 				$record_value = $key;
542 542
 			}
543 543
 			else
544 544
 			{
545
-				$key = array_search(strtolower($record_value), array_map('strtolower',array_map('lang',$selects)));
546
-				if($key !== false) $record_value = $key;
545
+				$key = array_search(strtolower($record_value), array_map('strtolower', array_map('lang', $selects)));
546
+				if ($key !== false) $record_value = $key;
547 547
 			}
548 548
 		}
549 549
 
Please login to merge, or discard this patch.
Braces   +147 added lines, -62 removed lines patch added patch discarded remove patch
@@ -22,7 +22,9 @@  discard block
 block discarded – undo
22 22
  * @todo Throw away spechial chars and trim() entries ?
23 23
  * @todo Check for XSS like userinput! (see common_functions)
24 24
  */
25
-class importexport_import_csv implements importexport_iface_import_record { //, Iterator {
25
+class importexport_import_csv implements importexport_iface_import_record
26
+{
27
+//, Iterator {
26 28
 
27 29
 	const csv_max_linelength = 8000;
28 30
 	
@@ -83,10 +85,14 @@  discard block
 block discarded – undo
83 85
 	 * @param string $_resource resource containing data. May be each valid php-stream
84 86
 	 * @param array $_options options for the resource array with keys: charset and fieldsep
85 87
 	 */
86
-	public function __construct( $_resource,  array $_options ) {
88
+	public function __construct( $_resource,  array $_options )
89
+	{
87 90
 		$this->resource = $_resource;
88 91
 		$this->csv_fieldsep = $_options['fieldsep'];
89
-		if($_options['charset'] == 'user') $_options['charset'] = $GLOBALS['egw_info']['user']['preferences']['common']['csv_charset'];
92
+		if($_options['charset'] == 'user')
93
+		{
94
+			$_options['charset'] = $GLOBALS['egw_info']['user']['preferences']['common']['csv_charset'];
95
+		}
90 96
 		$this->csv_charset = $_options['charset'];
91 97
 		return;
92 98
 	} // end of member function __construct
@@ -94,7 +100,8 @@  discard block
 block discarded – undo
94 100
 	/**
95 101
 	 * cleanup
96 102
 	 */
97
-	public function __destruct( ) {
103
+	public function __destruct( )
104
+	{
98 105
 	} // end of member function __destruct
99 106
 
100 107
 	/**
@@ -103,20 +110,27 @@  discard block
 block discarded – undo
103 110
 	 * @param mixed _position may be: {current|first|last|next|previous|somenumber}
104 111
 	 * @return mixed array with data / false if no furtor records
105 112
 	 */
106
-	public function get_record( $_position = 'next' ) {
113
+	public function get_record( $_position = 'next' )
114
+	{
107 115
 		
108
-		if ($this->get_raw_record( $_position ) === false) {
116
+		if ($this->get_raw_record( $_position ) === false)
117
+		{
109 118
 			return false;
110 119
 		}
111 120
 		
112 121
 		// skip empty records
113
-		if( count( array_unique( $this->record ) ) < 2 ) return $this->get_record( $_position );
122
+		if( count( array_unique( $this->record ) ) < 2 )
123
+		{
124
+			return $this->get_record( $_position );
125
+		}
114 126
 		
115
-		if ( !empty( $this->conversion ) ) {
127
+		if ( !empty( $this->conversion ) )
128
+		{
116 129
 			$this->do_conversions();
117 130
 		}
118 131
 		
119
-		if ( !empty( $this->mapping ) ) {
132
+		if ( !empty( $this->mapping ) )
133
+		{
120 134
 			$this->do_fieldmapping();
121 135
 		}
122 136
 		
@@ -128,8 +142,10 @@  discard block
 block discarded – undo
128 142
 	 *
129 143
 	 * @param int $_numToSkip
130 144
 	 */
131
-	public function skip_records( $_numToSkip ) {
132
-		while ( (int)$_numToSkip-- !== 0 ) {
145
+	public function skip_records( $_numToSkip )
146
+	{
147
+		while ( (int)$_numToSkip-- !== 0 )
148
+		{
133 149
 			fgetcsv( $this->resource, self::csv_max_linelength, $this->csv_fieldsep);
134 150
 		}
135 151
 	}
@@ -140,22 +156,27 @@  discard block
 block discarded – undo
140 156
 	 * @param mixed $_position
141 157
 	 * @return bool
142 158
 	 */
143
-	private function get_raw_record( $_position = 'next' ) {
144
-		switch ($_position) {
159
+	private function get_raw_record( $_position = 'next' )
160
+	{
161
+		switch ($_position)
162
+		{
145 163
 			case 'current' :
146
-				if ($this->current_position == 0) {
164
+				if ($this->current_position == 0)
165
+				{
147 166
 					return;
148 167
 				}
149 168
 				break;
150 169
 			case 'first' :
151
-				if (!$this->current_position == 0) {
170
+				if (!$this->current_position == 0)
171
+				{
152 172
 					$this->current_position = 0;
153 173
 					rewind($this->resource);
154 174
 				}
155 175
 				
156 176
 			case 'next' :
157 177
 				$csv_data = fgetcsv( $this->resource, self::csv_max_linelength, $this->csv_fieldsep);
158
-				if (!is_array($csv_data)) {
178
+				if (!is_array($csv_data))
179
+				{
159 180
 					return false;
160 181
 				}
161 182
 				$this->current_position++;
@@ -163,33 +184,41 @@  discard block
 block discarded – undo
163 184
 				break;
164 185
 				
165 186
 			case 'previous' :
166
-				if ($this->current_position < 2) {
187
+				if ($this->current_position < 2)
188
+				{
167 189
 					throw new Exception('Error: There is no previous record!');
168 190
 				}
169 191
 				$final_position = --$this->current_position;
170 192
 				$this->current_position = 0;
171 193
 				rewind($this->resource);
172
-				while ($this->current_position !== $final_position) {
194
+				while ($this->current_position !== $final_position)
195
+				{
173 196
 					$this->get_raw_record();
174 197
 				}
175 198
 				break;
176 199
 				
177 200
 			case 'last' :
178
-				while ($this->get_raw_record()) {}
201
+				while ($this->get_raw_record())
202
+				{
203
+}
179 204
 				break;
180 205
 				
181 206
 			default: //somenumber
182
-				if (!is_int($_position)) {
207
+				if (!is_int($_position))
208
+				{
183 209
 					throw new Exception('Error: $position must be one of {current|first|last|next|previous} or an integer value');
184 210
 				}
185
-				if ($_position == $this->current_position) {
211
+				if ($_position == $this->current_position)
212
+				{
186 213
 					break;
187 214
 				}
188
-				elseif ($_position < $this->current_position) {
215
+				elseif ($_position < $this->current_position)
216
+				{
189 217
 					$this->current_position = 0;
190 218
 					rewind($this->resource);
191 219
 				}
192
-				while ($this->current_position !== $_position) {
220
+				while ($this->current_position !== $_position)
221
+				{
193 222
 					$this->get_raw_record();
194 223
 				}
195 224
 				break;				
@@ -202,12 +231,16 @@  discard block
 block discarded – undo
202 231
 	 *
203 232
 	 * @return int
204 233
 	 */
205
-	public function get_num_of_records( ) {
206
-		if ($this->num_of_records > 0) {
234
+	public function get_num_of_records( )
235
+	{
236
+		if ($this->num_of_records > 0)
237
+		{
207 238
 			return $this->num_of_records;
208 239
 		}
209 240
 		$current_position = $this->current_position;
210
-		while ($this->get_raw_record()) {}
241
+		while ($this->get_raw_record())
242
+		{
243
+}
211 244
 		$this->num_of_records = $this->current_position;
212 245
 		$this->get_record($current_position);
213 246
 		return $this->num_of_records;
@@ -218,7 +251,8 @@  discard block
 block discarded – undo
218 251
 	 *
219 252
 	 * @return int
220 253
 	 */
221
-	public function get_current_position( ) {
254
+	public function get_current_position( )
255
+	{
222 256
 		
223 257
 		return $this->current_position;
224 258
 		
@@ -230,12 +264,16 @@  discard block
 block discarded – undo
230 264
 	 *
231 265
 	 * @return
232 266
 	 */
233
-	protected function do_fieldmapping( ) {
267
+	protected function do_fieldmapping( )
268
+	{
234 269
 		$record = $this->record;
235 270
 		$this->record = array();
236 271
 		foreach ($this->mapping as $cvs_idx => $new_idx)
237 272
 		{
238
-			if( $new_idx == '' ) continue;
273
+			if( $new_idx == '' )
274
+			{
275
+				continue;
276
+			}
239 277
 			$this->record[$new_idx] = $record[$cvs_idx];
240 278
 		}
241 279
 		return true;
@@ -246,8 +284,10 @@  discard block
 block discarded – undo
246 284
 	 *
247 285
 	 * @return bool
248 286
 	 */
249
-	protected function do_conversions() {
250
-		if ( $record = importexport_helper_functions::conversion( $this->record, $this->conversion, $this->conversion_class )) {
287
+	protected function do_conversions()
288
+	{
289
+		if ( $record = importexport_helper_functions::conversion( $this->record, $this->conversion, $this->conversion_class ))
290
+		{
251 291
 			$this->record = $record;
252 292
 			return;
253 293
 		}
@@ -264,16 +304,19 @@  discard block
 block discarded – undo
264 304
 	 *
265 305
 	 * @return string warnings, if any
266 306
 	 */
267
-	public static function convert(Array &$record, Array $fields = array(), $appname = null, Array $selects = array(), $format=0) {
307
+	public static function convert(Array &$record, Array $fields = array(), $appname = null, Array $selects = array(), $format=0)
308
+	{
268 309
 		$warnings = array();
269 310
 
270 311
 		// Automatic conversions
271
-		if($appname) {
312
+		if($appname)
313
+		{
272 314
 
273 315
 			// Load translations
274 316
 			Api\Translation::add_app($appname);
275 317
 
276
-			if(!self::$cf_parse_cache[$appname]) {
318
+			if(!self::$cf_parse_cache[$appname])
319
+			{
277 320
 				$c_fields = importexport_export_csv::convert_parse_custom_fields($appname, $selects, $links, $methods);
278 321
 				self::$cf_parse_cache[$appname] = array($c_fields, $selects, $links, $methods);
279 322
 			}
@@ -282,7 +325,10 @@  discard block
 block discarded – undo
282 325
 			// Add in any fields that are keys to another app
283 326
 			foreach((array)$fields['links'] as $link_field => $app)
284 327
 			{
285
-				if(is_numeric($link_field)) continue;
328
+				if(is_numeric($link_field))
329
+				{
330
+					continue;
331
+				}
286 332
 				$links[$link_field] = $app;
287 333
 				// Set it as a normal link field
288 334
 				$fields['links'][] = $link_field;
@@ -292,7 +338,8 @@  discard block
 block discarded – undo
292 338
 			// Not quite a recursive merge, since only one level
293 339
 			foreach($fields as $type => &$list)
294 340
 			{
295
-				if($c_fields[$type]) {
341
+				if($c_fields[$type])
342
+				{
296 343
 					$list = array_merge($c_fields[$type], $list);
297 344
 					unset($c_fields[$type]);
298 345
 				}
@@ -300,12 +347,15 @@  discard block
 block discarded – undo
300 347
 			$fields += $c_fields;
301 348
 			$selects += $c_selects;
302 349
 		}
303
-		if($fields) {
304
-			foreach((array)$fields['select'] as $name) {
350
+		if($fields)
351
+		{
352
+			foreach((array)$fields['select'] as $name)
353
+			{
305 354
 				$record[$name] = static::find_select_key($record[$name], $selects[$name]);
306 355
 			}
307 356
 			
308
-			foreach((array)$fields['links'] as $name) {
357
+			foreach((array)$fields['links'] as $name)
358
+			{
309 359
 				if($record[$name] && $links[$name])
310 360
 				{
311 361
 					// Primary key to another app, not a link
@@ -347,15 +397,19 @@  discard block
 block discarded – undo
347 397
 								lang($links[$name]), $record[$name]).
348 398
  								' - ' . lang('no matches');
349 399
 							continue;
350
-						} else {
400
+						}
401
+						else
402
+						{
351 403
 							$record[$name] = key($results);
352 404
 						}
353 405
 					}
354 406
 				}
355 407
 			}
356
-			foreach((array)$fields['select-account'] as $name) {
408
+			foreach((array)$fields['select-account'] as $name)
409
+			{
357 410
 				// Compare against null to deal with empty arrays
358
-				if ($record[$name]) {
411
+				if ($record[$name])
412
+				{
359 413
 					// Automatically handle text owner without explicit translation
360 414
 					$new_owner = importexport_helper_functions::account_name2id($record[$name]);
361 415
 					if(count($new_owner) != count(explode(',',$record[$name])))
@@ -363,20 +417,26 @@  discard block
 block discarded – undo
363 417
 						// Unable to parse value into account
364 418
 						$warnings[] = $name . ': ' .lang('%1 is not a known user or group', $record[$name]);
365 419
 					}
366
-					if($new_owner != '') {
420
+					if($new_owner != '')
421
+					{
367 422
 						$record[$name] = $new_owner;
368
-					} else {
423
+					}
424
+					else
425
+					{
369 426
 						// Clear it to prevent trouble later on
370 427
 						$record[$name] = '';
371 428
 					}
372 429
 				}
373 430
 			}
374
-			foreach((array)$fields['select-bool'] as $name) {
375
-				if($record[$name] != null && $record[$name] != '') {
431
+			foreach((array)$fields['select-bool'] as $name)
432
+			{
433
+				if($record[$name] != null && $record[$name] != '')
434
+				{
376 435
 					$record[$name] = ($record[$name] == lang('Yes') || $record[$name] == '1' ? 1 : 0);
377 436
 				}
378 437
 			}
379
-			foreach((array)$fields['date-time'] as $name) {
438
+			foreach((array)$fields['date-time'] as $name)
439
+			{
380 440
 				if (isset($record[$name]) && !is_numeric($record[$name]) && strlen(trim($record[$name])) > 0)
381 441
 				{
382 442
 					// Need to handle format first
@@ -402,7 +462,8 @@  discard block
 block discarded – undo
402 462
 								// Try again, anything goes
403 463
 								try {
404 464
 									$formatted = new Api\DateTime($record[$name]);
405
-								} catch (Exception $e) {
465
+								}
466
+								catch (Exception $e) {
406 467
 									$warnings[] = $name.': ' . $e->getMessage() . "\n" .
407 468
 										'Format: '.'!'.Api\DateTime::$user_dateformat . ' ' .Api\DateTime::$user_timeformat;
408 469
 									continue;
@@ -424,7 +485,8 @@  discard block
 block discarded – undo
424 485
 					}
425 486
 					
426 487
 					if(is_array(self::$cf_parse_cache[$appname][0]['date-time']) &&
427
-							in_array($name, self::$cf_parse_cache[$appname][0]['date-time'])) {
488
+							in_array($name, self::$cf_parse_cache[$appname][0]['date-time']))
489
+					{
428 490
 						// Custom fields stored in a particular format (from customfields_widget)
429 491
 						$record[$name] = date('Y-m-d H:i:s', $record[$name]);
430 492
 					}
@@ -434,7 +496,8 @@  discard block
 block discarded – undo
434 496
 					$record[$name] = null;
435 497
 				}
436 498
 			}
437
-			foreach((array)$fields['date'] as $name) {
499
+			foreach((array)$fields['date'] as $name)
500
+			{
438 501
 				if (isset($record[$name]) && !is_numeric($record[$name]) && strlen(trim($record[$name])) > 0)
439 502
 				{
440 503
 					// Need to handle format first
@@ -448,7 +511,8 @@  discard block
 block discarded – undo
448 511
 					}
449 512
 					$record[$name] = Api\DateTime::server2user($record[$name],'ts');
450 513
 					if(is_array(self::$cf_parse_cache[$appname][0]['date']) &&
451
-							in_array($name, self::$cf_parse_cache[$appname][0]['date'])) {
514
+							in_array($name, self::$cf_parse_cache[$appname][0]['date']))
515
+					{
452 516
 						// Custom fields stored in a particular format (from customfields_widget)
453 517
 						$record[$name] = date('Y-m-d', $record[$name]);
454 518
 					}
@@ -460,26 +524,37 @@  discard block
 block discarded – undo
460 524
 			}
461 525
 			foreach((array)$fields['float'] as $name)
462 526
 			{
463
-				if($record[$name] != null && $record[$name] != '') {
527
+				if($record[$name] != null && $record[$name] != '')
528
+				{
464 529
 					$dec_point = $GLOBALS['egw_info']['user']['preferences']['common']['number_format'][0];
465
-					if (empty($dec_point)) $dec_point = '.';
530
+					if (empty($dec_point))
531
+					{
532
+						$dec_point = '.';
533
+					}
466 534
 					$record[$name] = floatval(str_replace($dec_point, '.', preg_replace('/[^\d'.preg_quote($dec_point).']/', '', $record[$name])));
467 535
 				}
468 536
 			}
469 537
 
470 538
 			// Some custom methods for conversion
471
-			foreach((array)$methods as $name => $method) {
472
-				if($record[$name]) $record[$name] = ExecMethod($method, $record[$name]);
539
+			foreach((array)$methods as $name => $method)
540
+			{
541
+				if($record[$name])
542
+				{
543
+					$record[$name] = ExecMethod($method, $record[$name]);
544
+				}
473 545
 			}
474 546
 
475 547
 			// cat_name2id will use currentapp to create new categories
476 548
 			$current_app = $GLOBALS['egw_info']['flags']['currentapp'];
477
-			if($appname) {
549
+			if($appname)
550
+			{
478 551
 				$GLOBALS['egw_info']['flags']['currentapp'] = $appname;
479 552
 			}
480 553
 			$categories = new Api\Categories('',$appname);
481
-			foreach((array)$fields['select-cat'] as $name) {
482
-				if($record[$name]) {
554
+			foreach((array)$fields['select-cat'] as $name)
555
+			{
556
+				if($record[$name])
557
+				{
483 558
 					// Only parse name if it needs it
484 559
 					if($format == 1)
485 560
 					{
@@ -493,7 +568,10 @@  discard block
 block discarded – undo
493 568
 							$cat_id = importexport_helper_functions::cat_name2id($record[$name]);
494 569
 						}
495 570
 						// Don't clear it if it wasn't found
496
-						if($cat_id) $record[$name] = $cat_id;
571
+						if($cat_id)
572
+						{
573
+							$record[$name] = $cat_id;
574
+						}
497 575
 					}
498 576
 				}
499 577
 			}
@@ -514,7 +592,8 @@  discard block
 block discarded – undo
514 592
 	 */
515 593
 	protected static function find_select_key($record_value, $selects)
516 594
 	{
517
-		if($record_value != null && is_array($selects)) {
595
+		if($record_value != null && is_array($selects))
596
+		{
518 597
 			if(is_array($record_value) || is_string($record_value) && strpos($record_value, ',') !== FALSE)
519 598
 			{
520 599
 				// Array, or CSV
@@ -526,7 +605,10 @@  discard block
 block discarded – undo
526 605
 					if(!$sub_key)
527 606
 					{
528 607
 						$sub_key = static::find_select_key($subs[$sub_index].','.$subs[$sub_index+1], $selects);
529
-						if($sub_key) $sub_index++;
608
+						if($sub_key)
609
+						{
610
+							$sub_index++;
611
+						}
530 612
 					}
531 613
 					if($sub_key)
532 614
 					{
@@ -543,7 +625,10 @@  discard block
 block discarded – undo
543 625
 			else
544 626
 			{
545 627
 				$key = array_search(strtolower($record_value), array_map('strtolower',array_map('lang',$selects)));
546
-				if($key !== false) $record_value = $key;
628
+				if($key !== false)
629
+				{
630
+					$record_value = $key;
631
+				}
547 632
 			}
548 633
 		}
549 634
 
Please login to merge, or discard this patch.