@@ -34,11 +34,13 @@ function CSVImportExportPlugin() {
3434 </button>\
3535 </div>\
3636 <div class="modal-body">\
37- <div class="form-group ">\
38- <label for="uploadFileCSV">CSV-file to load data from</label>\
39- <input type="file" class="form-control- file" id="uploadFileCSV">\
37+ <div class="custom-file mb-2 ">\
38+ <label for="uploadFileCSV" class="custom-file-label" id="uploadFileCSVLabel" >CSV-file to load data from</label>\
39+ <input type="file" class="custom- file-input " id="uploadFileCSV">\
4040 </div>\
4141 <form id="import-csvdata-form"></form>\
42+ </div>\
43+ <div class="modal-footer">\
4244 <button type="button" class="btn btn-primary disabled" id="btn-import-from-csv">Import <span class="badge badge-light found-csv-objects">0</span> objects</button>\
4345 <button type="button" class="btn btn-primary disabled" id="btn-add-from-csv">Add <span class="badge badge-light found-csv-objects">0</span> objects</button>\
4446 </div>\
@@ -86,6 +88,7 @@ function CSVImportExportPlugin() {
8688 }
8789 } ) ;
8890 $ ( '#modals' ) . on ( 'change' , '#uploadFileCSV' , function ( e ) {
91+ $ ( '#uploadFileCSVLabel' ) . html ( this . files [ 0 ] . name ) ;
8992 plugin . getObjectsFromCSV ( ) ;
9093 } ) ;
9194 $ ( '#modals' ) . on ( 'change' , '#mergeFileCSV' , function ( e ) {
@@ -173,14 +176,36 @@ CSVImportExportPlugin.prototype.getObjectsFromCSV = function() {
173176 var id = letter [ column + 'ID' ] ;
174177 var entity = { } ;
175178 // Name and ID are given. Check for duplicate ID
176- // TODO: if ID already imported and name differs add name as alias
177179 if ( name && id && ! found_ids . includes ( id ) ) {
178180 entity [ config . v . titleElement ] = name ;
179181 entity [ config . v . identifierElement ] = id ;
180182 importable_entities . push ( entity ) ;
181183 unique_names . push ( name ) ;
182184 found_ids . push ( id ) ;
183185 }
186+ // If ID already imported and name differs add name as alias
187+ else if ( name && id && found_ids . includes ( id ) && ! unique_names . includes ( name ) ) {
188+ if ( config . v . aliasElement != undefined ) {
189+ // Update importable entity with id and add an alias, if not already done
190+ var already_imported_entity = importable_entities . find ( function ( e ) {
191+ return e [ config . v . identifierElement ] == id ;
192+ } ) ;
193+ if ( already_imported_entity [ config . v . aliasElement ] == undefined ) {
194+ // there is no alias yet
195+ already_imported_entity [ config . v . aliasElement ] = name ;
196+ } else {
197+ // Alias(es) aready set.
198+ var old_aliases = asArray ( already_imported_entity [ config . v . aliasElement ] ) ;
199+ // Add if not already done.
200+ if ( ! old_aliases . includes ( name ) ) {
201+ old_aliases . push ( name ) ;
202+ already_imported_entity [ config . v . aliasElement ] = old_aliases ;
203+ }
204+ }
205+ } else {
206+ // If aliases aren't configured, we can't handle this case
207+ }
208+ }
184209 // Only the name is given. Check for duplicate name.
185210 else if ( name && ! id && ! unique_names . includes ( name ) ) {
186211 entity [ config . v . titleElement ] = name ;
@@ -195,12 +220,12 @@ CSVImportExportPlugin.prototype.getObjectsFromCSV = function() {
195220 count_span . html ( importable_entities . length ) ;
196221 if ( importable_entities . length > 0 ) {
197222 // Add importable entities to import form with filter buttons
198- $ ( '#csv-file-upload-modal .form-group button[type="button"]' ) . remove ( ) ;
199- var chk_button_filter = '<div class="btn-group mt-2" id="check-buttons" role="group">\
223+ var chk_button_filter = '<div class="btn-group form-group" id="check-buttons" role="group">\
200224 <button class="btn btn-sm btn-secondary" id="import-csv-btn-chk-all" type="button">Select All</button>\
201225 <button class="btn btn-sm btn-secondary" id="import-csv-btn-chk-none" type="button">Deselect All</button>\
202226 </div>' ;
203- $ ( chk_button_filter ) . appendTo ( '#csv-file-upload-modal .form-group' ) ;
227+ $ ( chk_button_filter ) . appendTo ( entities_form ) ;
228+ var entities_btn_group = $ ( '<div class="form-group"></div>' ) . appendTo ( entities_form ) ;
204229 importable_entities . forEach ( function ( e , i ) {
205230 var ref_html = '' ;
206231 if ( e [ config . v . identifierElement ] && e [ config . v . identifierElement ] . startsWith ( config . v . identifierBaseURL ) ) {
@@ -212,7 +237,7 @@ CSVImportExportPlugin.prototype.getObjectsFromCSV = function() {
212237 ' + e [ config . v . titleElement ] + ref_html + '\
213238 </label>\
214239 </div>' ;
215- $ ( chk_html ) . appendTo ( entities_form ) ;
240+ $ ( chk_html ) . appendTo ( entities_btn_group ) ;
216241 } )
217242 $ ( btn_import ) . removeClass ( 'disabled' ) ;
218243 $ ( btn_add ) . removeClass ( 'disabled' ) ;
@@ -265,6 +290,9 @@ CSVImportExportPlugin.prototype.addEntities = function(event) {
265290 'preferred' : 'YES'
266291 } ;
267292 }
293+ if ( e [ config . v . aliasElement ] != undefined ) {
294+ params [ config . v . aliasElement ] = e [ config . v . aliasElement ] ;
295+ }
268296 addObject ( event . target , params )
269297 }
270298 } ) ;
@@ -285,6 +313,7 @@ CSVImportExportPlugin.prototype.mergeObjectsWithCSV = function() {
285313 var context2columnnames = plugin . context2columnnames ;
286314 var preselected_statuus = [ 'safe' ] ;
287315
316+ // TODO: Check if file type is text/csv and handle error case
288317 if ( file ) {
289318 Papa . parse ( file , {
290319 header : true ,
@@ -360,7 +389,12 @@ CSVImportExportPlugin.prototype.mergeCSV = function() {
360389 // Don't replace/add anything in 'soft' mode if an ID is already given
361390 if ( name != undefined && name . trim ( ) != '' && ! ( method == 'soft' && id . trim ( ) != '' ) ) {
362391 // Get matching object
392+ // Default case is matching with preferred name (titleElement)
363393 var obj = getLocalObjectByTitle ( name ) ;
394+ // If there is no match on titleElement, try to find one with matching alias, if alias is configured
395+ if ( obj == undefined && config . v . aliasElement != undefined ) {
396+ obj = getLocalObjectByAlias ( name ) ;
397+ }
364398 // Check if object is in correct state
365399 if ( obj !== undefined && statuus . includes ( obj [ config . v . statusElement ] ) ) {
366400 // Get preferred ID from local object
0 commit comments