From 7711d0a456e7a91c484b09acf6f02a8b9cabc612 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Sun, 8 Jan 2017 16:13:23 +0200 Subject: [PATCH] Skip blank rows in CSV import --- app/Services/ImportService.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/app/Services/ImportService.php b/app/Services/ImportService.php index 09305a91b45e..61257913ead8 100644 --- a/app/Services/ImportService.php +++ b/app/Services/ImportService.php @@ -252,6 +252,10 @@ class ImportService $this->checkData($entityType, count($reader->all())); $reader->each(function ($row) use ($source, $entityType, &$row_list, &$results) { + if ($this->isRowEmpty($row)) { + return; + } + $data_index = $this->transformRow($source, $entityType, $row); if ($data_index !== false) { @@ -286,8 +290,6 @@ class ImportService */ private function transformRow($source, $entityType, $row) { - // TODO skip import if row is blank - $transformer = $this->getTransformer($source, $entityType, $this->maps); // Create expesnse category @@ -620,6 +622,9 @@ class ImportService } $row = $this->convertToObject($entityType, $row, $map); + if ($this->isRowEmpty($row)) { + continue; + } $data_index = $this->transformRow($source, $entityType, $row); if ($data_index !== false) { @@ -812,4 +817,17 @@ class ImportService $this->maps['expense_category'][$name] = $category->id; } } + + private function isRowEmpty($row) + { + $isEmpty = true; + + foreach ($row as $key => $val) { + if (trim($val)) { + $isEmpty = false; + } + } + + return $isEmpty; + } }