From 801328ec42d5844bbf924bb80408cc89e98c801b Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 20 Jan 2025 16:23:13 +0000 Subject: [PATCH 1/5] Fixed #16097 - allow location update by ID in importer Signed-off-by: snipe --- app/Importer/Importer.php | 1 + app/Importer/ItemImporter.php | 3 +-- app/Importer/LocationImporter.php | 9 ++++++++- app/Livewire/Importer.php | 2 +- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/Importer/Importer.php b/app/Importer/Importer.php index 907c8b72c5..0d4b8d4932 100644 --- a/app/Importer/Importer.php +++ b/app/Importer/Importer.php @@ -39,6 +39,7 @@ abstract class Importer * @var array */ private $defaultFieldMap = [ + 'id' => 'id', 'asset_tag' => 'asset tag', 'activated' => 'activated', 'category' => 'category', diff --git a/app/Importer/ItemImporter.php b/app/Importer/ItemImporter.php index 6b1d647136..435f081aba 100644 --- a/app/Importer/ItemImporter.php +++ b/app/Importer/ItemImporter.php @@ -456,14 +456,13 @@ class ItemImporter extends Importer { if (empty($asset_location)) { $this->log('No location given, so none created.'); - return null; } + $location = Location::where(['name' => $asset_location])->first(); if ($location) { $this->log('Location '.$asset_location.' already exists'); - return $location->id; } // No matching locations in the collection, create a new one. diff --git a/app/Importer/LocationImporter.php b/app/Importer/LocationImporter.php index a9a5152234..b875d66c6a 100644 --- a/app/Importer/LocationImporter.php +++ b/app/Importer/LocationImporter.php @@ -38,7 +38,14 @@ class LocationImporter extends ItemImporter { $editingLocation = false; - $location = Location::where('name', '=', $this->findCsvMatch($row, 'name'))->first(); + + if ($this->findCsvMatch($row, 'id')) { + $location = Location::find($this->findCsvMatch($row, 'id')); + } else { + $location = Location::where('name', '=', $this->findCsvMatch($row, 'name'))->first(); + } + + if ($location) { if (! $this->updating) { diff --git a/app/Livewire/Importer.php b/app/Livewire/Importer.php index 75b707b8ad..aee6b852d4 100644 --- a/app/Livewire/Importer.php +++ b/app/Livewire/Importer.php @@ -329,6 +329,7 @@ class Importer extends Component ]; $this->locations_fields = [ + 'id' => trans('general.id'), 'name' => trans('general.item_name_var', ['item' => trans('general.location')]), 'address' => trans('general.address'), 'address2' => trans('general.importer.address2'), @@ -400,7 +401,6 @@ class Importer extends Component 'requestable', 'Requestable', ], - 'gravatar' => [ 'gravatar', From 48a916ab775f910c8ad02367d0ef4cf0db6b18f3 Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 20 Jan 2025 16:38:31 +0000 Subject: [PATCH 2/5] Check that the ID has a value Signed-off-by: snipe --- app/Importer/LocationImporter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Importer/LocationImporter.php b/app/Importer/LocationImporter.php index b875d66c6a..58db51affa 100644 --- a/app/Importer/LocationImporter.php +++ b/app/Importer/LocationImporter.php @@ -39,7 +39,7 @@ class LocationImporter extends ItemImporter $editingLocation = false; - if ($this->findCsvMatch($row, 'id')) { + if ($this->findCsvMatch($row, 'id')!='') { $location = Location::find($this->findCsvMatch($row, 'id')); } else { $location = Location::where('name', '=', $this->findCsvMatch($row, 'name'))->first(); From 0a5c41ce23e4d6658ed1dae9a884b35b84d80623 Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 20 Jan 2025 16:42:53 +0000 Subject: [PATCH 3/5] Log error Signed-off-by: snipe --- app/Importer/LocationImporter.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Importer/LocationImporter.php b/app/Importer/LocationImporter.php index 58db51affa..36c909ba8b 100644 --- a/app/Importer/LocationImporter.php +++ b/app/Importer/LocationImporter.php @@ -103,6 +103,7 @@ class LocationImporter extends ItemImporter } else { Log::debug($location->getErrors()); return $location->errors; + $this->logError($location, 'Location "'.$this->item['name'].'"'); } From 713c9fdd6f6d39aaa97c3518fb032361ee899e94 Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 20 Jan 2025 16:46:22 +0000 Subject: [PATCH 4/5] Derp Signed-off-by: snipe --- app/Importer/LocationImporter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Importer/LocationImporter.php b/app/Importer/LocationImporter.php index 36c909ba8b..7005834767 100644 --- a/app/Importer/LocationImporter.php +++ b/app/Importer/LocationImporter.php @@ -102,8 +102,8 @@ class LocationImporter extends ItemImporter } else { Log::debug($location->getErrors()); - return $location->errors; $this->logError($location, 'Location "'.$this->item['name'].'"'); + return $location->errors; } From 52734086314f78347cb4e639eb610b2ad9589643 Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 20 Jan 2025 16:53:48 +0000 Subject: [PATCH 5/5] Check for matching ID Signed-off-by: snipe --- app/Importer/LocationImporter.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/Importer/LocationImporter.php b/app/Importer/LocationImporter.php index 7005834767..5a87d069e5 100644 --- a/app/Importer/LocationImporter.php +++ b/app/Importer/LocationImporter.php @@ -39,12 +39,13 @@ class LocationImporter extends ItemImporter $editingLocation = false; - if ($this->findCsvMatch($row, 'id')!='') { - $location = Location::find($this->findCsvMatch($row, 'id')); - } else { - $location = Location::where('name', '=', $this->findCsvMatch($row, 'name'))->first(); - } + $location = Location::where('name', '=', $this->findCsvMatch($row, 'name'))->first(); + if ($this->findCsvMatch($row, 'id')!='') { + // Override location if an ID was given + \Log::debug('Finding location by ID: '.$this->findCsvMatch($row, 'id')); + $location = Location::find($this->findCsvMatch($row, 'id')); + } if ($location) {