Skip to content

Commit b4d9884

Browse files
committed
Implement value formatting for preview display and update import options handling
1 parent a4c507b commit b4d9884

4 files changed

Lines changed: 176 additions & 14 deletions

File tree

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

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static function generate_preview( $csv_data, $field_mappings, $post_type,
7575

7676
$formatted_value = implode( ', ', $connection_display );
7777
} else {
78-
$formatted_value = self::format_value_for_api( $processed_value, $field_key, $post_type );
78+
$formatted_value = self::format_value_for_preview( $processed_value, $field_key, $post_type );
7979
}
8080

8181
$processed_row[$field_key] = [
@@ -457,7 +457,7 @@ private static function process_location_value( $raw_value ) {
457457
// Validate grid ID exists
458458
global $wpdb;
459459
$grid_exists = $wpdb->get_var($wpdb->prepare(
460-
"SELECT grid_id FROM {$wpdb->prefix}dt_location_grid WHERE grid_id = %d",
460+
"SELECT grid_id FROM $wpdb->dt_location_grid WHERE grid_id = %d",
461461
intval( $raw_value )
462462
));
463463

@@ -595,6 +595,110 @@ public static function format_value_for_api( $processed_value, $field_key, $post
595595
return $processed_value;
596596
}
597597

598+
/**
599+
* Format processed value for preview display (human-readable)
600+
*/
601+
public static function format_value_for_preview( $processed_value, $field_key, $post_type ) {
602+
$field_settings = DT_Posts::get_post_field_settings( $post_type );
603+
604+
if ( !isset( $field_settings[$field_key] ) ) {
605+
return $processed_value;
606+
}
607+
608+
$field_config = $field_settings[$field_key];
609+
$field_type = $field_config['type'];
610+
611+
// Handle null/empty values
612+
if ( $processed_value === null || $processed_value === '' ) {
613+
return '';
614+
}
615+
616+
switch ( $field_type ) {
617+
case 'multi_select':
618+
case 'tags':
619+
// Convert array to comma-separated display
620+
if ( is_array( $processed_value ) ) {
621+
return implode( ', ', $processed_value );
622+
}
623+
break;
624+
625+
case 'connection':
626+
// Already handled in generate_preview for connection fields
627+
return $processed_value;
628+
629+
case 'communication_channel':
630+
// Extract values from communication channel array
631+
if ( is_array( $processed_value ) ) {
632+
$values = array_map( function( $channel ) {
633+
return $channel['value'] ?? $channel;
634+
}, $processed_value );
635+
return implode( ', ', $values );
636+
}
637+
break;
638+
639+
case 'location':
640+
case 'location_grid':
641+
case 'location_meta':
642+
// Handle location data for preview display
643+
if ( is_numeric( $processed_value ) ) {
644+
// Grid ID - try to get the location name
645+
global $wpdb;
646+
$location_name = $wpdb->get_var( $wpdb->prepare(
647+
"SELECT name FROM $wpdb->dt_location_grid WHERE grid_id = %d",
648+
intval( $processed_value )
649+
) );
650+
return $location_name ?: "Grid ID: {$processed_value}";
651+
} elseif ( is_array( $processed_value ) ) {
652+
// Handle coordinate or address arrays
653+
if ( isset( $processed_value['lat'] ) && isset( $processed_value['lng'] ) ) {
654+
return "Coordinates: {$processed_value['lat']}, {$processed_value['lng']}";
655+
} elseif ( isset( $processed_value['address'] ) ) {
656+
return $processed_value['address'];
657+
} elseif ( isset( $processed_value['label'] ) ) {
658+
return $processed_value['label'];
659+
} elseif ( isset( $processed_value['name'] ) ) {
660+
return $processed_value['name'];
661+
}
662+
// Fallback: return first non-empty value
663+
foreach ( $processed_value as $value ) {
664+
if ( !empty( $value ) ) {
665+
return $value;
666+
}
667+
}
668+
}
669+
break;
670+
671+
case 'key_select':
672+
// Return the label for the selected key
673+
if ( isset( $field_config['default'][$processed_value] ) ) {
674+
return $field_config['default'][$processed_value]['label'] ?? $processed_value;
675+
}
676+
break;
677+
678+
case 'user_select':
679+
// Get user display name
680+
if ( is_numeric( $processed_value ) ) {
681+
$user = get_user_by( 'id', intval( $processed_value ) );
682+
return $user ? $user->display_name : "User ID: {$processed_value}";
683+
}
684+
break;
685+
686+
case 'date':
687+
// Format date for display
688+
if ( is_numeric( $processed_value ) ) {
689+
return date( 'Y-m-d', intval( $processed_value ) );
690+
}
691+
break;
692+
693+
case 'boolean':
694+
// Convert boolean to Yes/No
695+
return $processed_value ? 'Yes' : 'No';
696+
}
697+
698+
// For all other field types, return as string
699+
return is_array( $processed_value ) ? implode( ', ', $processed_value ) : (string) $processed_value;
700+
}
701+
598702
/**
599703
* Execute the actual import
600704
*/

dt-import/assets/css/dt-import.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,15 +460,15 @@
460460
color: #0073aa;
461461
}
462462

463-
.stat-card.error h3 {
463+
.stat-card.error-card h3 {
464464
color: #dc3232;
465465
}
466466

467-
.stat-card.warning h3 {
467+
.stat-card.warning-card h3 {
468468
color: #f56e28;
469469
}
470470

471-
.stat-card.success h3 {
471+
.stat-card.success-card h3 {
472472
color: #46b450;
473473
}
474474

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

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,26 @@
385385
const assignedToVal = $('#import-assigned-to').val();
386386
const sourceVal = $('#import-source').val();
387387

388+
// Update import options, preserving existing values if form fields are empty
388389
this.importOptions = {
389390
assigned_to:
390-
assignedToVal && assignedToVal !== '' ? assignedToVal : null,
391-
source: sourceVal && sourceVal !== '' ? sourceVal : null,
392-
delimiter: $('#csv-delimiter').val() || ',',
393-
encoding: $('#csv-encoding').val() || 'UTF-8',
391+
assignedToVal && assignedToVal !== ''
392+
? assignedToVal
393+
: this.importOptions
394+
? this.importOptions.assigned_to
395+
: null,
396+
source:
397+
sourceVal && sourceVal !== ''
398+
? sourceVal
399+
: this.importOptions
400+
? this.importOptions.source
401+
: null,
402+
delimiter:
403+
$('#csv-delimiter').val() ||
404+
(this.importOptions ? this.importOptions.delimiter : ','),
405+
encoding:
406+
$('#csv-encoding').val() ||
407+
(this.importOptions ? this.importOptions.encoding : 'UTF-8'),
394408
};
395409

396410
this.showProcessing('Analyzing CSV columns...');
@@ -1031,14 +1045,14 @@
10311045
${
10321046
totalWarnings > 0
10331047
? `
1034-
<div class="stat-card warning">
1048+
<div class="stat-card warning-card">
10351049
<h3>${totalWarnings}</h3>
10361050
<p>Warnings</p>
10371051
</div>
10381052
`
10391053
: ''
10401054
}
1041-
<div class="stat-card error">
1055+
<div class="stat-card error-card">
10421056
<h3>${previewData.error_count}</h3>
10431057
<p>Errors</p>
10441058
</div>
@@ -1239,14 +1253,14 @@
12391253
<div class="import-results">
12401254
<h2>Import Complete!</h2>
12411255
<div class="results-stats">
1242-
<div class="stat-card success">
1256+
<div class="stat-card success-card">
12431257
<h3>${results.records_imported}</h3>
12441258
<p>Records Imported</p>
12451259
</div>
12461260
${
12471261
results.errors && results.errors.length > 0
12481262
? `
1249-
<div class="stat-card error">
1263+
<div class="stat-card error-card">
12501264
<h3>${results.errors.length}</h3>
12511265
<p>Errors</p>
12521266
</div>
@@ -1548,6 +1562,30 @@
15481562
return value.label;
15491563
}
15501564

1565+
// Handle coordinate objects
1566+
if (
1567+
typeof value === 'object' &&
1568+
value.lat !== undefined &&
1569+
value.lng !== undefined
1570+
) {
1571+
return `Coordinates: ${value.lat}, ${value.lng}`;
1572+
}
1573+
1574+
// Handle address objects
1575+
if (typeof value === 'object' && value.address !== undefined) {
1576+
return value.address;
1577+
}
1578+
1579+
// Handle grid ID objects
1580+
if (typeof value === 'object' && value.grid_id !== undefined) {
1581+
return `Grid ID: ${value.grid_id}`;
1582+
}
1583+
1584+
// Handle name objects
1585+
if (typeof value === 'object' && value.name !== undefined) {
1586+
return value.name;
1587+
}
1588+
15511589
if (typeof value === 'object') {
15521590
return JSON.stringify(value);
15531591
}
@@ -1988,6 +2026,11 @@
19882026
`<option value="${user.ID}">${this.escapeHtml(user.name)}</option>`,
19892027
);
19902028
});
2029+
2030+
// Restore previously selected value if it exists
2031+
if (this.importOptions && this.importOptions.assigned_to) {
2032+
$select.val(this.importOptions.assigned_to);
2033+
}
19912034
})
19922035
.catch((error) => {
19932036
console.error('Error loading users:', error);
@@ -2008,11 +2051,26 @@
20082051
`<option value="${this.escapeHtml(source.key)}">${this.escapeHtml(source.label)}</option>`,
20092052
);
20102053
});
2054+
2055+
// Restore previously selected value if it exists
2056+
if (this.importOptions && this.importOptions.source) {
2057+
$select.val(this.importOptions.source);
2058+
}
20112059
})
20122060
.catch((error) => {
20132061
console.error('Error loading sources:', error);
20142062
});
20152063
}
2064+
2065+
// Also restore the CSV options (delimiter and encoding)
2066+
if (this.importOptions) {
2067+
if (this.importOptions.delimiter) {
2068+
$('#csv-delimiter').val(this.importOptions.delimiter);
2069+
}
2070+
if (this.importOptions.encoding) {
2071+
$('#csv-encoding').val(this.importOptions.encoding);
2072+
}
2073+
}
20162074
}
20172075

20182076
loadUsers() {

dt-import/includes/dt-import-geocoding.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public static function validate_grid_id( $grid_id ) {
156156
global $wpdb;
157157

158158
$exists = $wpdb->get_var( $wpdb->prepare(
159-
"SELECT grid_id FROM {$wpdb->prefix}dt_location_grid WHERE grid_id = %d",
159+
"SELECT grid_id FROM $wpdb->dt_location_grid WHERE grid_id = %d",
160160
intval( $grid_id )
161161
) );
162162

0 commit comments

Comments
 (0)