Skip to content

Commit 83ae95e

Browse files
committed
Implement a CSV import directly in the theme
1 parent 86abc0a commit 83ae95e

16 files changed

Lines changed: 7903 additions & 0 deletions
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Value Mapping Implementation for Key Select and Multi Select Fields
2+
3+
## Overview
4+
5+
This implementation adds comprehensive value mapping functionality for `key_select` and `multi_select` fields in the DT Import system. Users can now map CSV values to the available field options for these field types during the field mapping step.
6+
7+
## Key Features Implemented
8+
9+
### 1. Enhanced Value Mapping Modal
10+
- **Real data fetching**: Fetches actual CSV column data and field options from the server
11+
- **Unique value detection**: Shows all unique values found in the CSV column
12+
- **Flexible mapping**: Users can map, skip, or ignore specific CSV values
13+
- **Auto-mapping**: Intelligent auto-mapping with fuzzy matching for similar values
14+
- **Batch operations**: Clear all mappings or auto-map similar values with one click
15+
16+
### 2. New API Endpoints
17+
18+
#### Get Field Options (`/dt-import/v2/{post_type}/field-options`)
19+
- **Method**: GET
20+
- **Parameters**: `field_key` (required)
21+
- **Purpose**: Fetches available options for key_select and multi_select fields
22+
- **Returns**: Formatted key-value pairs of field options
23+
24+
#### Get Column Data (`/dt-import/v2/{session_id}/column-data`)
25+
- **Method**: GET
26+
- **Parameters**: `column_index` (required)
27+
- **Purpose**: Fetches unique values and sample data from a specific CSV column
28+
- **Returns**: Unique values, sample data, and total count
29+
30+
### 3. Enhanced JavaScript Functionality
31+
32+
#### DTImportModals Class Extensions
33+
- `getColumnCSVData()`: Fetches CSV column data from session
34+
- `getFieldOptions()`: Fetches field options from server API
35+
- `autoMapValues()`: Intelligent auto-mapping with fuzzy matching
36+
- `clearAllMappings()`: Clears all value mappings
37+
- `updateMappingCount()`: Live count of mapped vs unmapped values
38+
39+
### 4. User Interface Improvements
40+
41+
#### Value Mapping Modal
42+
- **Enhanced layout**: Wider modal (800px) with better spacing
43+
- **Control buttons**: Auto-map and clear all functionality
44+
- **Live feedback**: Real-time mapping count and progress indicator
45+
- **Better data display**: Shows total unique values found
46+
- **Sticky headers**: Table headers remain visible while scrolling
47+
48+
#### Field Mapping Integration
49+
- **Seamless integration**: "Configure Values" button appears for key_select/multi_select fields
50+
- **Mapping indicator**: Button shows count of mapped values
51+
- **Field-specific options**: Only shows for applicable field types
52+
53+
### 5. Data Processing Enhancements
54+
55+
#### Value Mapping Storage
56+
```php
57+
// Field mapping structure now supports value mappings
58+
$field_mappings[column_index] = [
59+
'field_key' => 'field_name',
60+
'column_index' => 0,
61+
'value_mapping' => [
62+
'csv_value_1' => 'dt_option_key_1',
63+
'csv_value_2' => 'dt_option_key_2',
64+
// ...
65+
]
66+
];
67+
```
68+
69+
#### Processing Logic
70+
- **key_select fields**: Maps single CSV values to single DT options
71+
- **multi_select fields**: Splits semicolon-separated values and maps each
72+
- **Fallback handling**: Direct option matching if no mapping defined
73+
- **Error handling**: Clear error messages for invalid mappings
74+
75+
### 6. CSS Styling
76+
77+
#### New CSS Classes
78+
- `.value-mapping-modal`: Enhanced modal styling
79+
- `.value-mapping-controls`: Action button container
80+
- `.value-mapping-container`: Scrollable table container
81+
- `.value-mapping-select`: Styled dropdown selects
82+
- `.mapping-summary`: Live mapping progress display
83+
84+
## User Workflow
85+
86+
1. **Field Selection**: User selects a key_select or multi_select field for a CSV column
87+
2. **Configure Values**: "Configure Values" button appears in field-specific options
88+
3. **Modal Display**: Click opens modal showing all unique CSV values
89+
4. **Value Mapping**: User maps CSV values to available field options
90+
5. **Auto-mapping**: Optional auto-mapping for similar values
91+
6. **Save Mapping**: Mappings are saved and stored with field configuration
92+
7. **Import Processing**: Values are transformed according to mappings during import
93+
94+
## Technical Implementation Details
95+
96+
### Backend Processing
97+
- **DT_Import_Mapping::get_unique_column_values()**: Extracts unique values from CSV
98+
- **Field validation**: Ensures mapped values are valid field options
99+
- **Import processing**: Applies value mappings during record creation
100+
101+
### Frontend Integration
102+
- **Modal system**: Reusable modal framework for field configuration
103+
- **Event handling**: Proper event delegation for dynamic content
104+
- **Error handling**: User-friendly error messages and validation
105+
- **State management**: Maintains mapping state across modal interactions
106+
107+
### API Integration
108+
- **REST API**: Follows WordPress REST API standards
109+
- **Authentication**: Uses WordPress nonce verification
110+
- **Error responses**: Standardized error response format
111+
- **Data validation**: Server-side validation of all inputs
112+
113+
## Benefits
114+
115+
1. **Data Integrity**: Ensures CSV values map to valid DT field options
116+
2. **User Experience**: Intuitive interface with helpful auto-mapping
117+
3. **Flexibility**: Supports skipping unwanted values or mapping multiple values
118+
4. **Efficiency**: Batch operations for common mapping tasks
119+
5. **Validation**: Real-time feedback and error prevention
120+
121+
## Future Enhancements
122+
123+
1. **Value Suggestions**: AI-powered mapping suggestions based on content analysis
124+
2. **Template Mapping**: Save and reuse value mappings for similar imports
125+
3. **Bulk Import**: Handle very large CSV files with chunked processing
126+
4. **Advanced Matching**: Regex or pattern-based value matching
127+
128+
This implementation provides a robust, user-friendly solution for mapping CSV values to DT field options, significantly improving the import experience for key_select and multi_select fields.

0 commit comments

Comments
 (0)