3
0
mirror of https://github.com/snipe/snipe-it.git synced 2026-08-18 11:15:42 +00:00
Files
snipe-it/app/Importer/CategoryImporter.php
TowyTowy c5bded7238 Fixed category importer silently ignoring the alert_on_response column
The importer UI offers "Alert on Response" as a mappable column for
category imports (app/Livewire/Importer.php, categories_fields), but
CategoryImporter only ever read use_default_eula, require_acceptance
and checkin_email. Anything the user mapped to alert_on_response was
dropped: new categories were always created with the column default,
and update-mode imports could never change it.

alert_on_response was added to the Category model in #17116, four days
after the category importer landed in #17062, and the importer's flag
list was never extended to match.

Adds it to the existing boolean-flag loop so it gets the same
fetchHumanBoolean handling and the same present/absent CSV semantics
as its three siblings.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-04 20:02:21 +02:00

138 lines
4.5 KiB
PHP

<?php
namespace App\Importer;
use App\Models\Category;
use Illuminate\Support\Facades\Log;
/**
* When we are importing users via an Asset/etc import, we use createOrFetchUser() in
* Importer\Importer.php. [ALG]
*
* Class CategoryImporter
*/
class CategoryImporter extends ItemImporter
{
protected $categories;
public function __construct($filename)
{
parent::__construct($filename);
}
protected function handle($row)
{
// CategoryImporter deliberately does NOT call parent::handle(). See
// the other subclass migrations for the same 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.
$this->item = [];
$this->setItemFromCsvIfPresent($row, 'name');
$this->setItemFromCsvIfPresent($row, 'notes');
$this->setItemFromCsvIfPresent($row, 'eula_text');
$this->setItemFromCsvIfPresent($row, 'tag_color');
// category_type is required-in-list-of-5 validation. Lowercase it so
// "Asset" from a human-authored CSV lands as "asset". Present-empty
// stays as null (which will fail validation with a clear message).
if ($this->csvRowHas($row, 'category_type')) {
$raw = $this->findCsvMatch($row, 'category_type');
$this->item['category_type'] = ($raw !== '') ? strtolower($raw) : null;
}
// Boolean flags. Present-empty maps to 0; present-with-value uses
// fetchHumanBoolean; absent leaves the DB value alone on update.
foreach (['use_default_eula', 'require_acceptance', 'checkin_email', 'alert_on_response'] as $flag) {
if ($this->csvRowHas($row, $flag)) {
$raw = $this->findCsvMatch($row, $flag);
$this->item[$flag] = ($this->fetchHumanBoolean($raw) == 1) ? 1 : 0;
}
}
$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.
*
* @todo Investigate how this should interact with Importer::createCategoryIfNotExists
*
* @author A. Gianotto
*
* @since 6.1.0
*/
public function createCategoryIfNotExists(array $row)
{
$editingCategory = false;
$name = trim($this->item['name'] ?? '');
$category = Category::where('name', '=', $name)->first();
if ($this->findCsvMatch($row, 'id') != '') {
// Override category if an ID was given
\Log::debug('Finding category by ID: '.$this->findCsvMatch($row, 'id'));
$category = Category::find($this->findCsvMatch($row, 'id'));
}
if ($category) {
if (! $this->updating) {
$this->log('A matching Category '.$name.' already exists');
$this->recordSkipped();
return;
}
$this->log('Updating Category');
$editingCategory = true;
} else {
$this->log('No Matching Category, Create a new one');
$category = new Category;
$category->created_by = $this->created_by;
}
Log::debug('Item array is: ');
Log::debug(print_r($this->item, true));
if ($editingCategory) {
Log::debug('Updating existing category');
$category->update($this->sanitizeItemForUpdating($category));
} else {
Log::debug('Creating category');
$category->fill($this->sanitizeItemForStoring($category));
}
if ($category->save()) {
$this->log('Category '.$category->name.' created or updated from CSV import');
if ($editingCategory) {
$this->recordUpdated();
} else {
$this->recordCreated();
}
return $category;
} else {
Log::debug($category->getErrors());
$this->recordErrored();
$this->logError($category, 'Category "'.$name.'"');
return $category->errors;
}
}
}