From e9b056f66c90a25c02156fb9ec4985abeb595aba Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Wed, 3 May 2017 14:14:35 -0500 Subject: [PATCH] Importer fixes v2 (#3524) * Pave the imports table * Format error response if file is the wrong type. * If a custom field doesn't exist, don't insert a blank string into the custom fields table * CustomField::db_column_name can return the stored name in the db. It's slugified when that value is set initially. This fixes a weird issue where _1 was replaced with _xx --- app/Console/Commands/PaveIt.php | 2 ++ app/Http/Controllers/Api/ImportController.php | 5 ++--- app/Importer/AssetImporter.php | 7 ++++--- app/Importer/Importer.php | 2 +- app/Models/CustomField.php | 3 ++- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app/Console/Commands/PaveIt.php b/app/Console/Commands/PaveIt.php index c7a3954dbc..2b9cf0fcf2 100644 --- a/app/Console/Commands/PaveIt.php +++ b/app/Console/Commands/PaveIt.php @@ -72,6 +72,7 @@ class PaveIt extends Command Statuslabel::getQuery()->delete(); Supplier::getQuery()->delete(); Group::getQuery()->delete(); + Import::getQuery()->delete(); DB::statement('delete from accessories_users'); DB::statement('delete from asset_logs'); @@ -131,6 +132,7 @@ class PaveIt extends Command \DB::statement('drop table IF EXISTS throttle'); \DB::statement('drop table IF EXISTS users_groups'); \DB::statement('drop table IF EXISTS users'); + \DB::statement('drop table IF EXISTS imports'); } } } diff --git a/app/Http/Controllers/Api/ImportController.php b/app/Http/Controllers/Api/ImportController.php index 6fdb087274..2ef18b784f 100644 --- a/app/Http/Controllers/Api/ImportController.php +++ b/app/Http/Controllers/Api/ImportController.php @@ -45,7 +45,6 @@ class ImportController extends Controller $results = []; $import = new Import; foreach ($files as $file) { - if (!in_array($file->getMimeType(), array( 'application/vnd.ms-excel', 'text/csv', @@ -53,7 +52,7 @@ class ImportController extends Controller 'text/comma-separated-values', 'text/tsv'))) { $results['error']='File type must be CSV'; - return $results; + return response()->json(Helper::formatStandardApiResponse('error', null, $results['error']), 500); } $date = date('Y-m-d-his'); @@ -132,7 +131,7 @@ class ImportController extends Controller try { unlink(config('app.private_uploads').'/imports/'.$import->file_path); $import->delete(); - return response()->json(Helper::formatStandardApiResponse('success', null, trans('message.import.file_delete_success'))); + return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/hardware/message.import.file_delete_success'))); } catch (\Exception $e) { return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.import.file_delete_error')), 500); } diff --git a/app/Importer/AssetImporter.php b/app/Importer/AssetImporter.php index 187d7ead54..1604586e0d 100644 --- a/app/Importer/AssetImporter.php +++ b/app/Importer/AssetImporter.php @@ -24,8 +24,10 @@ class AssetImporter extends ItemImporter parent::handle($row); foreach ($this->customFields as $customField) { - if ($this->item['custom_fields'][$customField->db_column_name()] = $this->array_smart_custom_field_fetch($row, $customField)) { - $this->log('Custom Field '. $customField->name.': '.$this->array_smart_custom_field_fetch($row, $customField)); + $customFieldValue = $this->array_smart_custom_field_fetch($row, $customField); + if ($customFieldValue) { + $this->item['custom_fields'][$customField->db_column_name()] = $customFieldValue; + $this->log('Custom Field '. $customField->name.': '.$customFieldValue); } } @@ -87,7 +89,6 @@ class AssetImporter extends ItemImporter $asset->{$custom_field} = $val; } } - if (!$this->testRun) { if ($asset->save()) { $asset->logCreate('Imported using csv importer'); diff --git a/app/Importer/Importer.php b/app/Importer/Importer.php index 8bc0eacf05..f4a1df2d46 100644 --- a/app/Importer/Importer.php +++ b/app/Importer/Importer.php @@ -133,7 +133,7 @@ abstract class Importer public function array_smart_custom_field_fetch(array $array, $key) { $index_name = strtolower($key->name); - return array_key_exists($index_name, $array) ? e(trim($array[$index_name])) : ''; + return array_key_exists($index_name, $array) ? e(trim($array[$index_name])) : false; } protected function log($string) diff --git a/app/Models/CustomField.php b/app/Models/CustomField.php index fd0c38a301..0f3b1bbbcc 100644 --- a/app/Models/CustomField.php +++ b/app/Models/CustomField.php @@ -101,7 +101,8 @@ class CustomField extends Model public function db_column_name() { - return self::convertUnicodeDbSlug(); + return $this->db_column; + // return self::convertUnicodeDbSlug(); } //mutators for 'format' attribute