Migrate import type to component

This commit is contained in:
Marcus Moore 2024-07-16 14:20:41 -07:00
parent 69263f0e5b
commit b7744105a0
No known key found for this signature in database
2 changed files with 47 additions and 47 deletions

View file

@ -22,6 +22,7 @@ class Importer extends Component
public $import_errors; // public $import_errors; //
public ?Import $activeFile = null; public ?Import $activeFile = null;
public $headerRow = []; public $headerRow = [];
public $typeOfImport;
public $importTypes; public $importTypes;
public $columnOptions; public $columnOptions;
public $statusType; public $statusType;
@ -51,6 +52,7 @@ class Importer extends Component
'activeFile.field_map' => 'array', 'activeFile.field_map' => 'array',
'activeFile.header_row' => 'array', 'activeFile.header_row' => 'array',
'headerRow' => 'array', 'headerRow' => 'array',
'typeOfImport' => 'string',
'field_map' => 'array' 'field_map' => 'array'
]; ];
@ -109,49 +111,46 @@ class Importer extends Component
return $results; return $results;
} }
public function updatingActiveFile($value, $propertyKey) public function updatingTypeOfImport($type)
{ {
if ($propertyKey == "import_type") { // go through each header, find a matching field to try and map it to.
foreach ($this->headerRow as $i => $header) {
// go through each header, find a matching field to try and map it to. // do we have something mapped already?
foreach ($this->headerRow as $i => $header) { if (array_key_exists($i, $this->field_map)) {
// do we have something mapped already? // yes, we do. Is it valid for this type of import?
if (array_key_exists($i, $this->field_map)) { // (e.g. the import type might have been changed...?)
// yes, we do. Is it valid for this type of import? if (array_key_exists($this->field_map[$i], $this->columnOptions[$type])) {
// (e.g. the import type might have been changed...?) //yes, this key *is* valid. Continue on to the next field.
if (array_key_exists($this->field_map[$i], $this->columnOptions[$value])) { continue;
//yes, this key *is* valid. Continue on to the next field. } else {
continue; //no, this key is *INVALID* for this import type. Better set it to null
} else { // and we'll hope that the $aliases_fields or something else picks it up.
//no, this key is *INVALID* for this import type. Better set it to null $this->field_map[$i] = null; // fingers crossed! But it's not likely, tbh.
// and we'll hope that the $aliases_fields or something else picks it up. } // TODO - strictly speaking, this isn't necessary here I don't think.
$this->field_map[$i] = null; // fingers crossed! But it's not likely, tbh. }
} // TODO - strictly speaking, this isn't necessary here I don't think. // first, check for exact matches
foreach ($this->columnOptions[$type] as $v => $text) {
if (strcasecmp($text, $header) === 0) { // case-INSENSITIVe on purpose!
$this->field_map[$i] = $v;
continue 2; //don't bother with the alias check, go to the next header
} }
// first, check for exact matches }
foreach ($this->columnOptions[$value] as $v => $text) { // if you got here, we didn't find a match. Try the $aliases_fields
if (strcasecmp($text, $header) === 0) { // case-INSENSITIVe on purpose! foreach ($this->aliases_fields as $key => $alias_values) {
$this->field_map[$i] = $v; foreach ($alias_values as $alias_value) {
continue 2; //don't bother with the alias check, go to the next header if (strcasecmp($alias_value, $header) === 0) { // aLsO CaSe-INSENSitiVE!
} // Make *absolutely* sure that this key actually _exists_ in this import type -
} // you can trigger this by importing accessories with a 'Warranty' column (which don't exist
// if you got here, we didn't find a match. Try the $aliases_fields // in "Accessories"!)
foreach ($this->aliases_fields as $key => $alias_values) { if (array_key_exists($key, $this->columnOptions[$type])) {
foreach ($alias_values as $alias_value) { $this->field_map[$i] = $key;
if (strcasecmp($alias_value, $header) === 0) { // aLsO CaSe-INSENSitiVE! continue 3; // bust out of both of these loops; as well as the surrounding one - e.g. move on to the next header
// Make *absolutely* sure that this key actually _exists_ in this import type -
// you can trigger this by importing accessories with a 'Warranty' column (which don't exist
// in "Accessories"!)
if (array_key_exists($key, $this->columnOptions[$value])) {
$this->field_map[$i] = $key;
continue 3; // bust out of both of these loops; as well as the surrounding one - e.g. move on to the next header
}
} }
} }
} }
// and if you got here, we got nothing. Let's recommend 'null'
$this->field_map[$i] = null; // Booooo :(
} }
// and if you got here, we got nothing. Let's recommend 'null'
$this->field_map[$i] = null; // Booooo :(
} }
} }
@ -513,6 +512,7 @@ class Importer extends Component
} }
$this->headerRow = $this->activeFile->header_row; $this->headerRow = $this->activeFile->header_row;
$this->typeOfImport = $this->activeFile->import_type;
$this->field_map = null; $this->field_map = null;
foreach ($this->headerRow as $element) { foreach ($this->headerRow as $element) {

View file

@ -142,12 +142,12 @@
<div class="form-group"> <div class="form-group">
<label for="activeFile.import_type" class="col-md-3 col-xs-12"> <label for="typeOfImport" class="col-md-3 col-xs-12">
{{ trans('general.import_type') }} {{ trans('general.import_type') }}
</label> </label>
<div class="col-md-9 col-xs-12" wire:ignore> <div class="col-md-9 col-xs-12" wire:ignore>
{{ Form::select('activeFile.import_type', $importTypes, $activeFile->import_type, [ {{ Form::select('typeOfImport', $importTypes, $typeOfImport, [
'id' => 'import_type', 'id' => 'import_type',
'class' => 'livewire-select2', 'class' => 'livewire-select2',
'style' => 'min-width: 350px', 'style' => 'min-width: 350px',
@ -156,7 +156,7 @@
'data-minimum-results-for-search' => '-1', // Remove this if the list gets long enough that we need to search 'data-minimum-results-for-search' => '-1', // Remove this if the list gets long enough that we need to search
'data-livewire-component' => $this->getId() 'data-livewire-component' => $this->getId()
]) }} ]) }}
@if ($activeFile->import_type === 'asset' && $snipeSettings->auto_increment_assets == 0) @if ($typeOfImport === 'asset' && $snipeSettings->auto_increment_assets == 0)
<p class="help-block"> <p class="help-block">
{{ trans('general.auto_incrementing_asset_tags_disabled_so_tags_required') }} {{ trans('general.auto_incrementing_asset_tags_disabled_so_tags_required') }}
</p> </p>
@ -169,7 +169,7 @@
<input type="checkbox" name="update" data-livewire-component="{{ $this->getId() }}" wire:model.live="update"> <input type="checkbox" name="update" data-livewire-component="{{ $this->getId() }}" wire:model.live="update">
{{ trans('general.update_existing_values') }} {{ trans('general.update_existing_values') }}
</label> </label>
@if ($activeFile->import_type === 'asset' && $snipeSettings->auto_increment_assets == 1 && $update) @if ($typeOfImport === 'asset' && $snipeSettings->auto_increment_assets == 1 && $update)
<p class="help-block"> <p class="help-block">
{{ trans('general.auto_incrementing_asset_tags_enabled_so_now_assets_will_be_created') }} {{ trans('general.auto_incrementing_asset_tags_enabled_so_now_assets_will_be_created') }}
</p> </p>
@ -195,10 +195,10 @@
@endif @endif
@if ($activeFile->import_type) @if ($typeOfImport)
<div class="form-group col-md-12"> <div class="form-group col-md-12">
<hr style="border-top: 1px solid lightgray"> <hr style="border-top: 1px solid lightgray">
<h3><i class="{{ Helper::iconTypeByItem($activeFile->import_type) }}"></i> Map {{ ucwords($activeFile->import_type) }} Import Fields</h3> <h3><i class="{{ Helper::iconTypeByItem($typeOfImport) }}"></i> Map {{ ucwords($typeOfImport) }} Import Fields</h3>
<hr style="border-top: 1px solid lightgray"> <hr style="border-top: 1px solid lightgray">
</div> </div>
<div class="form-group col-md-12"> <div class="form-group col-md-12">
@ -222,7 +222,7 @@
<label for="field_map.{{ $index }}" class="col-md-3 control-label text-right">{{ $header }}</label> <label for="field_map.{{ $index }}" class="col-md-3 control-label text-right">{{ $header }}</label>
<div class="col-md-4" wire:ignore> <div class="col-md-4" wire:ignore>
{{ Form::select('field_map.'.$index, $columnOptions[$activeFile->import_type], @$field_map[$index], {{ Form::select('field_map.'.$index, $columnOptions[$typeOfImport], @$field_map[$index],
[ [
'class' => 'mappings livewire-select2', 'class' => 'mappings livewire-select2',
'placeholder' => trans('general.importer.do_not_import'), 'placeholder' => trans('general.importer.do_not_import'),
@ -270,7 +270,7 @@
<a href="#" wire:click.prevent="$set('activeFile',null)">{{ trans('general.cancel') }}</a> <a href="#" wire:click.prevent="$set('activeFile',null)">{{ trans('general.cancel') }}</a>
</div> </div>
</div> </div>
@endif {{-- end of if ... activeFile->import_type --}} @endif {{-- end of if ... $typeOfImport --}}
</div><!-- /div v-show --> </td> </div><!-- /div v-show --> </td>
</tr> </tr>
@ -328,7 +328,7 @@
// we have to hook up to the `<tr id='importer-file'>` at the root of this display, // we have to hook up to the `<tr id='importer-file'>` at the root of this display,
// because the #import button isn't visible until you click an import_type // because the #import button isn't visible until you click an import_type
$('#upload-table').on('click', '#import', function () { $('#upload-table').on('click', '#import', function () {
if (!$wire.$get('activeFile.import_type')) { if (!$wire.$get('typeOfImport')) {
$wire.$set('statusType', 'error'); $wire.$set('statusType', 'error');
$wire.$set('statusText', "An import type is required... "); //TODO: translate? $wire.$set('statusText', "An import type is required... "); //TODO: translate?
return; return;
@ -348,7 +348,7 @@
data: JSON.stringify({ data: JSON.stringify({
'import-update': !!$wire.$get('update'), 'import-update': !!$wire.$get('update'),
'send-welcome': !!$wire.$get('send_welcome'), 'send-welcome': !!$wire.$get('send_welcome'),
'import-type': $wire.$get('activeFile.import_type'), 'import-type': $wire.$get('typeOfImport'),
'run-backup': !!$wire.$get('run_backup'), 'run-backup': !!$wire.$get('run_backup'),
'column-mappings': mappings 'column-mappings': mappings
}), }),