Add improved field-guessing logic; make sure 'do not map' fields work

This commit is contained in:
Brady Wetherington 2023-03-07 18:26:59 -08:00
parent 7edec26e73
commit fe699737eb
3 changed files with 119 additions and 61 deletions

View file

@ -31,7 +31,7 @@ class ImporterFile extends Component
public function generate_field_map() public function generate_field_map()
{ {
$tmp = array_combine($this->activeFile->header_row, $this->field_map); $tmp = array_combine($this->activeFile->header_row, $this->field_map);
return json_encode($tmp); return json_encode(array_filter($tmp));
} }
static $general = [ static $general = [
@ -102,16 +102,17 @@ class ImporterFile extends Component
'country' => 'Country', 'country' => 'Country',
]; ];
//array of "real fieldnames" to a list of aliases for that field
static $aliases = [
'model_number' => ['model', 'model no','model no.','model number', 'model num', 'model num.'],
'warranty_months' => ['Warranty', 'Warranty Months']
];
private function getColumns($type) private function getColumns($type)
{ {
$customFields = [];
foreach($this->customFields AS $field) {
$customFields[$field->db_column_name()] = $field->name;
}
switch($type) { switch($type) {
case 'asset': case 'asset':
$results = self::$general + self::$assets + $customFields; $results = self::$general + self::$assets;
break; break;
case 'accessory': case 'accessory':
$results = self::$general + self::$accessories; $results = self::$general + self::$accessories;
@ -129,13 +130,59 @@ class ImporterFile extends Component
$results = self::$general; $results = self::$general;
} }
asort($results, SORT_FLAG_CASE|SORT_STRING); asort($results, SORT_FLAG_CASE|SORT_STRING);
if($type == "asset") {
// add Custom Fields after a horizontal line
$results['-'] = "———".trans('admin/custom_fields/general.custom_fields')."———’";
foreach(CustomField::orderBy('name')->get() AS $field) {
$results[$field->db_column_name()] = $field->name;
}
}
return $results; return $results;
} }
public function updating($name, $new_import_type)
{
if ($name == "activeFile.import_type") {
\Log::info("WE ARE CHANGING THE import_type!!!!! TO: ".$new_import_type);
// go through each header, find a matching field to try and map it to.
foreach($this->activeFile->header_row as $i => $header) {
// do we have something mapped already?
if (array_key_exists($i, $this->field_map)) {
// yes, we do. Is it valid for this type of import?
// (e.g. the import type might have been changed...?)
if (array_key_exists($this->field_map[$i], $this->columnOptions[$value])) {
//yes, this key *is* valid. Continue on to the next field.
continue;
} else {
//no, this key is *INVALID* for this import type. Better set it to null
// and we'll hope that the aliases or something else picks it up.
$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[$new_import_type] AS $value => $text) {
if (strcasecmp($text, $header) === 0) { // case-INSENSITIVe on purpose!
$this->field_map[$i] = $value;
continue 2; //don't bother with the alias check, go to the next header
}
}
// if you got here, we didn't find a match. Try the aliases
foreach(self::$aliases as $key => $alias_values) {
foreach($alias_values as $alias_value) {
if (strcasecmp($alias_value,$header) === 0) { // aLsO CaSe-INSENSitiVE!
$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 :(
}
}
}
public function mount() public function mount()
{ {
$this->customFields = CustomField::all();
$this->importTypes = [ $this->importTypes = [
'asset' => 'Assets', // TODO - translate! 'asset' => 'Assets', // TODO - translate!
'accessory' => 'Accessories', 'accessory' => 'Accessories',
@ -149,7 +196,6 @@ class ImporterFile extends Component
foreach($this->importTypes AS $type => $name) { foreach($this->importTypes AS $type => $name) {
$this->columnOptions[$type] = $this->getColumns($type); $this->columnOptions[$type] = $this->getColumns($type);
} }
$this->increment = 0;
$this->field_map = $this->activeFile->field_map ? array_values($this->activeFile->field_map): []; $this->field_map = $this->activeFile->field_map ? array_values($this->activeFile->field_map): [];
} }

View file

@ -1,4 +1,4 @@
<tr> <tr id="importer-file">
<td colspan="5"> <td colspan="5">
<div class="col-md-12"> <div class="col-md-12">
@ -9,7 +9,13 @@
</div> </div>
<div class="col-md-7 col-xs-12"> <div class="col-md-7 col-xs-12">
{{ Form::select('activeFile.import_type', $importTypes, $activeFile->import_type, ['id' => 'import_type', 'class' => 'livewire-select2', 'style' => 'min-width: 350px', 'data-placeholder' => 'Select an import type...', /* TODO: translate me */ 'placeholder' => '', 'data-livewire-component' => $_instance->id]) }} {{ Form::select('activeFile.import_type', $importTypes, $activeFile->import_type, [
'id' => 'import_type',
'class' => 'livewire-select2',
'style' => 'min-width: 350px',
'data-placeholder' => 'Select an import type...', /* TODO: translate me */
'data-livewire-component' => $_instance->id
]) }}
</div> </div>
</div><!-- /dynamic-form-row --> </div><!-- /dynamic-form-row -->
@ -70,8 +76,9 @@
'class' => 'mappings livewire-select2', 'class' => 'mappings livewire-select2',
'placeholder' => 'Do Not Import', 'placeholder' => 'Do Not Import',
'data-livewire-component' => $_instance->id 'data-livewire-component' => $_instance->id
],[
'-' => ['disabled' => true] // this makes the "-----" line unclickable
]) ])
}} }}
</div> </div>
</div> </div>
@ -113,52 +120,57 @@ $(function () {
$('.minimal.livewire-icheck').iCheck({ $('.minimal.livewire-icheck').iCheck({
checkboxClass: 'icheckbox_minimal-blue', checkboxClass: 'icheckbox_minimal-blue',
}) })
})
$('#import').on('click', function () { // we have to hook up to the `<tr id='importer-file'>` at the root of this display,
if(!@this.activeFile.import_type) { // because the #import button isn't visible until you click an import_type
@this.statusType='error'; $('#importer-file').on('click', '#import', function () {
@this.statusText= "An import type is required... "; //TODO: translate? console.warn("You clicked it!!!!")
return; if(!@this.activeFile.import_type) {
} @this.statusType='error';
@this.statusType='pending'; @this.statusText= "An import type is required... "; //TODO: translate?
@this.statusText = "Processing..."; return;
@this.generate_field_map().then(function (mappings_raw) { }
var mappings = JSON.parse(mappings_raw) @this.statusType='pending';
console.warn("Here is the mappings:") @this.statusText = "Processing...";
console.dir(mappings) @this.generate_field_map().then(function (mappings_raw) {
$.post({ var mappings = JSON.parse(mappings_raw)
url: "{{ route('api.imports.importFile', $activeFile->id) }}", // console.warn("Here is the mappings:")
data: { // console.dir(mappings)
'import-update': !!@this.update, $.post({
'send-welcome': !!@this.send_welcome, url: "{{ route('api.imports.importFile', $activeFile->id) }}",
'import-type': @this.activeFile.import_type, contentType: 'application/json',
'run-backup': !!@this.run_backup, data: JSON.stringify({
'column-mappings': mappings 'import-update': !!@this.update,
}, 'send-welcome': !!@this.send_welcome,
headers: { 'import-type': @this.activeFile.import_type,
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content') 'run-backup': !!@this.run_backup,
} 'column-mappings': mappings
}).done( function (body) { }),
// Success headers: {
@this.statusType="success"; "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content')
@this.statusText = "Success... Redirecting."; }
console.dir(body) }).done( function (body) {
window.location.href = body.messages.redirect_url; // Success
}).fail( function (jqXHR, textStatus, error) { @this.statusType="success";
// Failure @this.statusText = "Success... Redirecting.";
var body = jqXHR.responseJSON
if(body.status == 'import-errors') {
@this.emit('importError', body.messages);
@this.statusType='error';
@this.statusText = "Error";
} else {
console.warn("Not import-errors, just regular errors")
console.dir(body) console.dir(body)
@this.emit('alert', body.error) window.location.href = body.messages.redirect_url;
} }).fail( function (jqXHR, textStatus, error) {
@this.emit('hideDetails') // Failure
}); var body = jqXHR.responseJSON
}) if(body.status == 'import-errors') {
return false; @this.emit('importError', body.messages);
}); @this.statusType='error';
@this.statusText = "Error";
} else {
console.warn("Not import-errors, just regular errors")
console.dir(body)
@this.emit('alert', body.error)
}
@this.emit('hideDetails')
});
})
return false;
});})
</script> </script>

View file

@ -36,7 +36,7 @@
<tbody> <tbody>
@foreach($import_errors as $field => $error_list) @foreach($import_errors as $field => $error_list)
<tr> <tr>
<td>{{ $processDetails->file_path }}</td> <td>{{ $processDetails->file_path ?? "Unknown File" }}</td>
<td> <td>
<b>{{ $field }}:</b> <b>{{ $field }}:</b>
<span>{{ implode(", ",$error_list) }}</span> <span>{{ implode(", ",$error_list) }}</span>