Skip to content

Commit eced48e

Browse files
committed
Implement connection field logic
1 parent 9c6bdfb commit eced48e

2 files changed

Lines changed: 113 additions & 19 deletions

File tree

dt-import/admin/dt-import-processor.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,25 @@ public static function format_value_for_api( $processed_value, $field_key, $post
495495
}
496496
break;
497497

498+
case 'location':
499+
// Format location data for DT_Posts API
500+
if ( is_numeric( $processed_value ) ) {
501+
// Grid ID
502+
return [
503+
'values' => [
504+
[ 'value' => $processed_value ]
505+
]
506+
];
507+
} elseif ( is_array( $processed_value ) ) {
508+
// Lat/lng or address data
509+
return [
510+
'values' => [
511+
$processed_value
512+
]
513+
];
514+
}
515+
break;
516+
498517
case 'user_select':
499518
// Convert single user ID to proper format
500519
if ( is_numeric( $processed_value ) ) {

dt-import/assets/js/dt-import.js

Lines changed: 94 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -426,30 +426,64 @@
426426

427427
$('.dt-import-step-content').html(step3Html);
428428

429-
// Initialize field mappings from suggested mappings
430-
Object.entries(mappingSuggestions).forEach(([index, mapping]) => {
431-
if (mapping.suggested_field) {
432-
this.fieldMappings[index] = {
433-
field_key: mapping.suggested_field,
434-
column_index: parseInt(index),
435-
};
436-
}
437-
});
438-
439-
// Show field-specific options for auto-suggested fields after DOM is ready
440-
setTimeout(() => {
429+
// Initialize field mappings from suggested mappings ONLY if no existing mappings
430+
if (Object.keys(this.fieldMappings).length === 0) {
431+
// Initialize from suggestions for first-time display
441432
Object.entries(mappingSuggestions).forEach(([index, mapping]) => {
442433
if (mapping.suggested_field) {
434+
this.fieldMappings[index] = {
435+
field_key: mapping.suggested_field,
436+
column_index: parseInt(index),
437+
};
438+
}
439+
});
440+
}
441+
442+
// Restore existing field mappings to the dropdowns and read actual dropdown values
443+
setTimeout(() => {
444+
// First, restore any existing user mappings to the dropdowns
445+
Object.entries(this.fieldMappings).forEach(([columnIndex, mapping]) => {
446+
const $select = $(
447+
`.field-mapping-select[data-column-index="${columnIndex}"]`,
448+
);
449+
if ($select.length && mapping.field_key) {
450+
$select.val(mapping.field_key);
443451
this.showFieldSpecificOptions(
444-
parseInt(index),
445-
mapping.suggested_field,
452+
parseInt(columnIndex),
453+
mapping.field_key,
446454
);
447455
}
448456
});
457+
458+
// Read the actual dropdown values to ensure mappings match what's displayed
459+
// This handles cases where suggestions were auto-selected but user wants different behavior
460+
const actualMappings = {};
461+
$('.field-mapping-select').each((index, select) => {
462+
const $select = $(select);
463+
const columnIndex = $select.data('column-index');
464+
const fieldKey = $select.val();
465+
466+
// Only create mapping if a field is actually selected (not empty)
467+
if (fieldKey && fieldKey !== '' && fieldKey !== 'create_new') {
468+
actualMappings[columnIndex] = {
469+
field_key: fieldKey,
470+
column_index: parseInt(columnIndex),
471+
};
472+
}
473+
});
474+
475+
// Update field mappings to match actual dropdown state
476+
this.fieldMappings = actualMappings;
477+
console.log(
478+
'DT Import: Field mappings synchronized with dropdown state:',
479+
this.fieldMappings,
480+
);
481+
482+
// Update the summary after mappings are initialized
483+
this.updateMappingSummary();
449484
}, 100);
450485

451486
this.updateNavigation();
452-
this.updateMappingSummary();
453487
}
454488

455489
createColumnMappingCard(columnIndex, mapping) {
@@ -465,12 +499,18 @@
465499
.map((sample) => `<li>${this.escapeHtml(sample)}</li>`)
466500
.join('');
467501

502+
// Check if there's an existing user mapping for this column
503+
const existingMapping = this.fieldMappings[columnIndex];
504+
const selectedField = existingMapping
505+
? existingMapping.field_key
506+
: mapping.suggested_field; // Restore auto-mapping
507+
468508
return `
469509
<div class="column-mapping-card" data-column-index="${columnIndex}">
470510
<div class="column-header">
471511
<h4>${this.escapeHtml(mapping.column_name)}</h4>
472512
${
473-
mapping.confidence > 0
513+
mapping.confidence > 0 && !existingMapping
474514
? `
475515
<div class="confidence-indicator confidence-${confidenceClass}">
476516
${mapping.confidence}% confidence
@@ -489,7 +529,7 @@
489529
<label>Map to field:</label>
490530
<select class="field-mapping-select" data-column-index="${columnIndex}">
491531
<option value="">-- Do not import --</option>
492-
${this.getFieldOptions(mapping.suggested_field)}
532+
${this.getFieldOptions(selectedField)}
493533
<option value="create_new">+ Create New Field</option>
494534
</select>
495535
@@ -537,16 +577,29 @@
537577
return;
538578
}
539579

540-
// Store mapping
541-
if (fieldKey) {
580+
console.log(
581+
`DT Import: Column ${columnIndex} field mapping changed to: "${fieldKey}"`,
582+
);
583+
584+
// Store mapping - properly handle empty values for "do not import"
585+
if (fieldKey && fieldKey !== '') {
542586
this.fieldMappings[columnIndex] = {
543587
field_key: fieldKey,
544588
column_index: columnIndex,
545589
};
590+
console.log(
591+
`DT Import: Added mapping for column ${columnIndex} -> ${fieldKey}`,
592+
);
546593
} else {
594+
// When "do not import" is selected (empty value), remove the mapping entirely
547595
delete this.fieldMappings[columnIndex];
596+
console.log(
597+
`DT Import: Removed mapping for column ${columnIndex} (do not import)`,
598+
);
548599
}
549600

601+
console.log('DT Import: Current field mappings:', this.fieldMappings);
602+
550603
// Show field-specific options if needed
551604
this.showFieldSpecificOptions(columnIndex, fieldKey);
552605
this.updateMappingSummary();
@@ -821,6 +874,10 @@
821874
return;
822875
}
823876

877+
console.log(
878+
'DT Import: Saving field mappings to server:',
879+
this.fieldMappings,
880+
);
824881
this.showProcessing('Saving field mappings...');
825882

826883
fetch(`${dtImport.restUrl}${this.sessionId}/mapping`, {
@@ -838,6 +895,7 @@
838895
this.hideProcessing();
839896

840897
if (data.success) {
898+
console.log('DT Import: Field mappings saved successfully');
841899
this.showStep4();
842900
} else {
843901
this.showError(data.message || 'Failed to save mappings');
@@ -955,7 +1013,14 @@
9551013
return '<p>No data to preview.</p>';
9561014
}
9571015

1016+
// Get only the headers for fields that are actually being imported
1017+
// The preview data from the server only contains fields that have mappings
9581018
const headers = Object.keys(rows[0].data);
1019+
1020+
if (headers.length === 0) {
1021+
return '<p>No fields selected for import. Please go back and configure field mappings.</p>';
1022+
}
1023+
9591024
const headerHtml = headers
9601025
.map((header) => `<th>${this.escapeHtml(header)}</th>`)
9611026
.join('');
@@ -1010,6 +1075,16 @@
10101075
`;
10111076
}
10121077

1078+
getColumnIndexForField(fieldKey) {
1079+
// Find the column index that maps to this field
1080+
for (const [columnIndex, mapping] of Object.entries(this.fieldMappings)) {
1081+
if (mapping.field_key === fieldKey) {
1082+
return parseInt(columnIndex);
1083+
}
1084+
}
1085+
return null;
1086+
}
1087+
10131088
executeImport() {
10141089
if (this.isProcessing) return;
10151090

0 commit comments

Comments
 (0)