Skip to content

A free open source IT asset/license management system

Removed duplicate code

snipe27113663b473
diff --git a/app/Http/Requests/ItemImportRequest.php b/app/Http/Requests/ItemImportRequest.php
index 364313b961..bd0e9c7417 100644
--- a/app/Http/Requests/ItemImportRequest.php
+++ b/app/Http/Requests/ItemImportRequest.php
@@ -66,6 +66,15 @@ class ItemImportRequest extends FormRequest
->setUsernameFormat('firstname.lastname')
->setFieldMappings($fieldMappings);
+ // "Skip updating fields with blank cells" opts out of the default
+ // clear-DB-on-blank behavior so an empty cell in the CSV keeps
+ // whatever value is already in the DB column. Only the update path
+ // is affected. New-row inserts ignore the flag entirely. See
+ // ItemImporter::$rejectEmptyOnUpdate.
+ if ($importer instanceof \App\Importer\ItemImporter) {
+ $importer->setRejectEmptyOnUpdate((bool) $this->input('import-preserve-blanks'));
+ }
+
// Matcher options only apply to the asset history importer, which
// resolves rows to existing users by name (not by creating new
// users). Any other importer ignores these switches.
diff --git a/app/Importer/AccessoryImporter.php b/app/Importer/AccessoryImporter.php
index 12f6773f1b..4ac058fc6e 100644
--- a/app/Importer/AccessoryImporter.php
+++ b/app/Importer/AccessoryImporter.php
@@ -20,7 +20,7 @@ class AccessoryImporter extends ItemImporter
// columns are left out of $this->item so update mode preserves the
// DB value, and present-but-empty cells land as null so update mode
// clears the DB value. The base sanitize's reject-empty pass is
- // suppressed via the sanitizeItemForStoring override below.
+ // disabled by $rejectEmptyOnUpdate on ItemImporter.
$this->item = [];
// Shared lookup fields. Present-and-empty clears the FK; absent
@@ -91,17 +91,6 @@ class AccessoryImporter extends ItemImporter
$this->createAccessoryIfNotExists($row);
}
- /**
- * Override the base sanitize to skip the reject-empty pass. See handle()
- * above for the matching item-population.
- *
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- protected function sanitizeItemForStoring($model, $updating = false)
- {
- return collect($this->item)->only($model->getFillable())->toArray();
- }
-
/**
* Create an accessory if a duplicate does not exist
*
diff --git a/app/Importer/AssetImporter.php b/app/Importer/AssetImporter.php
index 7031d9b84b..2349fffe5f 100644
--- a/app/Importer/AssetImporter.php
+++ b/app/Importer/AssetImporter.php
@@ -45,8 +45,8 @@ class AssetImporter extends ItemImporter
// $this->item exclusively via setItemFromCsvIfPresent so absent
// columns never enter the update payload (preserving DB values)
// and present-but-empty columns land as null (clearing DB values).
- // See sanitizeItemForStoring override below for the matching
- // pass-through sanitize.
+ // The base sanitize's reject-empty pass is disabled by
+ // $rejectEmptyOnUpdate on ItemImporter.
$this->item = [];
// Shared lookup fields. Present-and-empty clears the FK; absent
@@ -181,19 +181,6 @@ class AssetImporter extends ItemImporter
$this->createAssetIfNotExists($row);
}
- /**
- * Override the base sanitize to skip the reject-empty pass. AssetImporter
- * populates $this->item exclusively from CSV columns that were present in
- * the row, so an empty value here is an explicit intent to clear the DB
- * field on update. See handle() above for the matching item-population.
- *
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- protected function sanitizeItemForStoring($model, $updating = false)
- {
- return collect($this->item)->only($model->getFillable())->toArray();
- }
-
/**
* Create the asset if it does not exist.
*
diff --git a/app/Importer/AssetModelImporter.php b/app/Importer/AssetModelImporter.php
index cd752fa20f..f2800a1509 100644
--- a/app/Importer/AssetModelImporter.php
+++ b/app/Importer/AssetModelImporter.php
@@ -29,7 +29,7 @@ class AssetModelImporter extends ItemImporter
// columns stay out of $this->item so update mode preserves the DB
// value, and present-but-empty cells land as null so update mode
// clears the DB value. The base sanitize's reject-empty pass is
- // suppressed via the sanitizeItemForStoring override below.
+ // disabled by $rejectEmptyOnUpdate on ItemImporter.
$this->item = [];
$this->setItemFromCsvIfPresent($row, 'name');
@@ -65,17 +65,6 @@ class AssetModelImporter extends ItemImporter
$this->createAssetModelIfNotExists($row);
}
- /**
- * Override the base sanitize to skip the reject-empty pass. See handle()
- * above for the matching item-population.
- *
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- protected function sanitizeItemForStoring($model, $updating = false)
- {
- return collect($this->item)->only($model->getFillable())->toArray();
- }
-
/**
* Create a model if a duplicate does not exist.
*
diff --git a/app/Importer/CategoryImporter.php b/app/Importer/CategoryImporter.php
index 10983fa3bb..7e707166b8 100644
--- a/app/Importer/CategoryImporter.php
+++ b/app/Importer/CategoryImporter.php
@@ -27,7 +27,7 @@ class CategoryImporter extends ItemImporter
// columns stay out of $this->item so update mode preserves the DB
// value, and present-but-empty cells land as null so update mode
// clears the DB value. The base sanitize's reject-empty pass is
- // suppressed via the sanitizeItemForStoring override below.
+ // disabled by $rejectEmptyOnUpdate on ItemImporter.
$this->item = [];
$this->setItemFromCsvIfPresent($row, 'name');
@@ -55,17 +55,6 @@ class CategoryImporter extends ItemImporter
$this->createCategoryIfNotExists($row);
}
- /**
- * Override the base sanitize to skip the reject-empty pass. See handle()
- * above for the matching item-population.
- *
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- protected function sanitizeItemForStoring($model, $updating = false)
- {
- return collect($this->item)->only($model->getFillable())->toArray();
- }
-
/**
* Create a category if a duplicate does not exist.
*
diff --git a/app/Importer/ComponentImporter.php b/app/Importer/ComponentImporter.php
index 57e0d0a125..fc496712e4 100644
--- a/app/Importer/ComponentImporter.php
+++ b/app/Importer/ComponentImporter.php
@@ -92,17 +92,6 @@ class ComponentImporter extends ItemImporter
$this->createComponentIfNotExists($row);
}
- /**
- * Override the base sanitize to skip the reject-empty pass. See handle()
- * above for the matching item-population.
- *
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- protected function sanitizeItemForStoring($model, $updating = false)
- {
- return collect($this->item)->only($model->getFillable())->toArray();
- }
-
/**
* Create a component if a duplicate does not exist
*
diff --git a/app/Importer/ConsumableImporter.php b/app/Importer/ConsumableImporter.php
index 81ad5d4ad1..8908766b19 100644
--- a/app/Importer/ConsumableImporter.php
+++ b/app/Importer/ConsumableImporter.php
@@ -20,7 +20,7 @@ class ConsumableImporter extends ItemImporter
// pattern: absent CSV columns stay out of $this->item so update mode
// preserves the DB value, and present-but-empty cells land as null
// so update mode clears the DB value. The base sanitize's reject-empty
- // pass is suppressed via the sanitizeItemForStoring override below.
+ // pass is disabled by $rejectEmptyOnUpdate on ItemImporter.
$this->item = [];
// Shared lookup fields. Present-and-empty clears the FK; absent
@@ -92,17 +92,6 @@ class ConsumableImporter extends ItemImporter
$this->createConsumableIfNotExists($row);
}
- /**
- * Override the base sanitize to skip the reject-empty pass. See handle()
- * above for the matching item-population.
- *
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- protected function sanitizeItemForStoring($model, $updating = false)
- {
- return collect($this->item)->only($model->getFillable())->toArray();
- }
-
/**
* Create a consumable if a duplicate does not exist
*
diff --git a/app/Importer/ItemImporter.php b/app/Importer/ItemImporter.php
index f684db8b8e..896813224a 100644
--- a/app/Importer/ItemImporter.php
+++ b/app/Importer/ItemImporter.php
@@ -20,6 +20,22 @@ class ItemImporter extends Importer
protected Setting $settings;
+ // Every concrete subclass currently disables the reject-empty pass on
+ // update so that a present-but-empty CSV cell clears the corresponding
+ // DB column. Absent columns still preserve the DB value because the
+ // subclasses' handle() methods only populate $this->item for cells that
+ // are actually in the row. Flip to true on a subclass, or via
+ // setRejectEmptyOnUpdate() at runtime, to fall back to "keep DB value
+ // on empty CSV cell" behavior.
+ protected bool $rejectEmptyOnUpdate = false;
+
+ public function setRejectEmptyOnUpdate(bool $reject): self
+ {
+ $this->rejectEmptyOnUpdate = $reject;
+
+ return $this;
+ }
+
public function __construct($filename)
{
parent::__construct($filename);
@@ -230,7 +246,7 @@ class ItemImporter extends Importer
$item = $item->only($model->getFillable());
// Then iterate through the item and, if we are updating, remove any blank values.
- if ($updating) {
+ if ($updating && $this->rejectEmptyOnUpdate) {
$item = $item->reject(function ($value) {
return empty($value);
});
diff --git a/app/Importer/LicenseImporter.php b/app/Importer/LicenseImporter.php
index baf199f320..f5f0792aac 100644
--- a/app/Importer/LicenseImporter.php
+++ b/app/Importer/LicenseImporter.php
@@ -22,8 +22,8 @@ class LicenseImporter extends ItemImporter
// $this->item exclusively via setItemFromCsvIfPresent so absent
// columns never enter the update payload (preserving DB values)
// and present-but-empty columns land as empty strings (clearing
- // DB values). See sanitizeItemForStoring override below for the
- // matching pass-through sanitize.
+ // DB values). The base sanitize's reject-empty pass is disabled
+ // by $rejectEmptyOnUpdate on ItemImporter.
$this->item = [];
// Shared lookup fields. Present-and-empty clears the FK; absent
@@ -71,19 +71,6 @@ class LicenseImporter extends ItemImporter
$this->createLicenseIfNotExists($row);
}
- /**
- * Override the base sanitize to skip the reject-empty pass. LicenseImporter
- * populates $this->item exclusively from CSV columns that were present in
- * the row, so an empty value here is an explicit intent to clear the DB
- * field on update. See handle() above for the matching item-population.
- *
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- protected function sanitizeItemForStoring($model, $updating = false)
- {
- return collect($this->item)->only($model->getFillable())->toArray();
- }
-
/**
* Create the license if it does not exist.
*
diff --git a/app/Importer/LocationImporter.php b/app/Importer/LocationImporter.php
index c69883ec91..21446c5d9a 100644
--- a/app/Importer/LocationImporter.php
+++ b/app/Importer/LocationImporter.php
@@ -27,7 +27,7 @@ class LocationImporter extends ItemImporter
// columns stay out of $this->item so update mode preserves the DB
// value, and present-but-empty cells land as null so update mode
// clears the DB value. The base sanitize's reject-empty pass is
- // suppressed via the sanitizeItemForStoring override below.
+ // disabled by $rejectEmptyOnUpdate on ItemImporter.
$this->item = [];
// Straight CSV-to-item assignments for Location fillable fields.
@@ -83,17 +83,6 @@ class LocationImporter extends ItemImporter
$this->createLocationIfNotExists($row);
}
- /**
- * Override the base sanitize to skip the reject-empty pass. See handle()
- * above for the matching item-population.
- *
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- protected function sanitizeItemForStoring($model, $updating = false)
- {
- return collect($this->item)->only($model->getFillable())->toArray();
- }
-
/**
* Create a location if a duplicate does not exist.
*
diff --git a/app/Importer/ManufacturerImporter.php b/app/Importer/ManufacturerImporter.php
index d7934c7cbf..04642489f8 100644
--- a/app/Importer/ManufacturerImporter.php
+++ b/app/Importer/ManufacturerImporter.php
@@ -27,7 +27,7 @@ class ManufacturerImporter extends ItemImporter
// columns stay out of $this->item so update mode preserves the DB
// value, and present-but-empty cells land as null so update mode
// clears the DB value. The base sanitize's reject-empty pass is
- // suppressed via the sanitizeItemForStoring override below.
+ // disabled by $rejectEmptyOnUpdate on ItemImporter.
$this->item = [];
foreach ([
@@ -46,17 +46,6 @@ class ManufacturerImporter extends ItemImporter
$this->createManufacturerIfNotExists($row);
}
- /**
- * Override the base sanitize to skip the reject-empty pass. See handle()
- * above for the matching item-population.
- *
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- protected function sanitizeItemForStoring($model, $updating = false)
- {
- return collect($this->item)->only($model->getFillable())->toArray();
- }
-
/**
* Create a manufacturer if a duplicate does not exist.
*
diff --git a/app/Importer/SupplierImporter.php b/app/Importer/SupplierImporter.php
index 81cd2fe07c..dd7b6e9f66 100644
--- a/app/Importer/SupplierImporter.php
+++ b/app/Importer/SupplierImporter.php
@@ -27,7 +27,7 @@ class SupplierImporter extends ItemImporter
// columns stay out of $this->item so update mode preserves the DB
// value, and present-but-empty cells land as null so update mode
// clears the DB value. The base sanitize's reject-empty pass is
- // suppressed via the sanitizeItemForStoring override below.
+ // disabled by $rejectEmptyOnUpdate on ItemImporter.
$this->item = [];
foreach ([
@@ -52,17 +52,6 @@ class SupplierImporter extends ItemImporter
$this->createSupplierIfNotExists($row);
}
- /**
- * Override the base sanitize to skip the reject-empty pass. See handle()
- * above for the matching item-population.
- *
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- protected function sanitizeItemForStoring($model, $updating = false)
- {
- return collect($this->item)->only($model->getFillable())->toArray();
- }
-
/**
* Create a supplier if a duplicate does not exist.
*
diff --git a/app/Importer/UserImporter.php b/app/Importer/UserImporter.php
index 5612fe16e4..2ef7ef3734 100644
--- a/app/Importer/UserImporter.php
+++ b/app/Importer/UserImporter.php
@@ -141,17 +141,6 @@ class UserImporter extends ItemImporter
$this->createUserIfNotExists($row, $csvId);
}
- /**
- * Override the base sanitize to skip the reject-empty pass. See handle()
- * above for the matching item-population.
- *
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- protected function sanitizeItemForStoring($model, $updating = false)
- {
- return collect($this->item)->only($model->getFillable())->toArray();
- }
-
/**
* Parse a pipe-separated company column value into an array of company IDs,
* creating companies that do not yet exist. Returns an empty array when the
diff --git a/app/Livewire/Importer.php b/app/Livewire/Importer.php
index a0031aadac..af4b49394c 100644
--- a/app/Livewire/Importer.php
+++ b/app/Livewire/Importer.php
@@ -104,6 +104,12 @@ class Importer extends Component
public $update;
+ // When true, blank CSV cells during an update pass are ignored (the DB
+ // value stays put). Default false matches the legacy behavior: a
+ // present-but-empty cell clears the corresponding DB column. Only the
+ // update flow reads it. New-row inserts ignore it entirely.
+ public $preserve_blanks = false;
+
public $send_welcome;
public $run_backup;