Completed
Push — 16.1 ( 4b5176...efe11c )
by Nathan
28:12 queued 11:24
created
importexport/inc/class.importexport_import_csv.inc.php 1 patch
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(trim($subs[$sub_index]), $selects);
526
-					if(!$sub_key)
526
+					if (!$sub_key)
527 527
 					{
528
-						$sub_key = static::find_select_key(trim($subs[$sub_index]).','.trim($subs[$sub_index+1]), $selects);
529
-						if($sub_key) $sub_index++;
528
+						$sub_key = static::find_select_key(trim($subs[$sub_index]).','.trim($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.