mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-04-04 13:39:36 +00:00
Automatically detect the delimiter of generic BOM imports
The detectFields does this anyway, so use that guessed value further on
This commit is contained in:
parent
de371877b9
commit
74e5102943
2 changed files with 27 additions and 16 deletions
|
|
@ -240,7 +240,8 @@ class ProjectController extends AbstractController
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detect fields and get suggestions
|
// Detect fields and get suggestions
|
||||||
$detected_fields = $BOMImporter->detectFields($file_content);
|
$detected_delimiter = $BOMImporter->detectDelimiter($file_content);
|
||||||
|
$detected_fields = $BOMImporter->detectFields($file_content, $detected_delimiter);
|
||||||
$suggested_mapping = $BOMImporter->getSuggestedFieldMapping($detected_fields);
|
$suggested_mapping = $BOMImporter->getSuggestedFieldMapping($detected_fields);
|
||||||
|
|
||||||
// Create mapping of original field names to sanitized field names for template
|
// Create mapping of original field names to sanitized field names for template
|
||||||
|
|
@ -257,7 +258,7 @@ class ProjectController extends AbstractController
|
||||||
$builder->add('delimiter', ChoiceType::class, [
|
$builder->add('delimiter', ChoiceType::class, [
|
||||||
'label' => 'project.bom_import.delimiter',
|
'label' => 'project.bom_import.delimiter',
|
||||||
'required' => true,
|
'required' => true,
|
||||||
'data' => ',',
|
'data' => $detected_delimiter,
|
||||||
'choices' => [
|
'choices' => [
|
||||||
'project.bom_import.delimiter.comma' => ',',
|
'project.bom_import.delimiter.comma' => ',',
|
||||||
'project.bom_import.delimiter.semicolon' => ';',
|
'project.bom_import.delimiter.semicolon' => ';',
|
||||||
|
|
|
||||||
|
|
@ -721,26 +721,36 @@ class BOMImporter
|
||||||
return $mapped;
|
return $mapped;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Try to detect the separator used in the CSV data by analyzing the first line and counting occurrences of common delimiters.
|
||||||
|
* @param string $data
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function detectDelimiter(string $data): string
|
||||||
|
{
|
||||||
|
$delimiters = [',', ';', "\t"];
|
||||||
|
$lines = explode("\n", $data, 2);
|
||||||
|
$header_line = $lines[0] ?? '';
|
||||||
|
$delimiter_counts = [];
|
||||||
|
foreach ($delimiters as $delim) {
|
||||||
|
$delimiter_counts[$delim] = substr_count($header_line, $delim);
|
||||||
|
}
|
||||||
|
// Choose the delimiter with the highest count, default to comma if all are zero
|
||||||
|
$max_count = max($delimiter_counts);
|
||||||
|
$delimiter = array_search($max_count, $delimiter_counts, true);
|
||||||
|
if ($max_count === 0 || $delimiter === false) {
|
||||||
|
$delimiter = ',';
|
||||||
|
}
|
||||||
|
return $delimiter;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Detect available fields in CSV data for field mapping UI
|
* Detect available fields in CSV data for field mapping UI
|
||||||
*/
|
*/
|
||||||
public function detectFields(string $data, ?string $delimiter = null): array
|
public function detectFields(string $data, ?string $delimiter = null): array
|
||||||
{
|
{
|
||||||
if ($delimiter === null) {
|
if ($delimiter === null) {
|
||||||
// Detect delimiter by counting occurrences in the first row (header)
|
$delimiter = $this->detectDelimiter($data);
|
||||||
$delimiters = [',', ';', "\t"];
|
|
||||||
$lines = explode("\n", $data, 2);
|
|
||||||
$header_line = $lines[0] ?? '';
|
|
||||||
$delimiter_counts = [];
|
|
||||||
foreach ($delimiters as $delim) {
|
|
||||||
$delimiter_counts[$delim] = substr_count($header_line, $delim);
|
|
||||||
}
|
|
||||||
// Choose the delimiter with the highest count, default to comma if all are zero
|
|
||||||
$max_count = max($delimiter_counts);
|
|
||||||
$delimiter = array_search($max_count, $delimiter_counts, true);
|
|
||||||
if ($max_count === 0 || $delimiter === false) {
|
|
||||||
$delimiter = ',';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Handle potential BOM (Byte Order Mark) at the beginning
|
// Handle potential BOM (Byte Order Mark) at the beginning
|
||||||
$data = preg_replace('/^\xEF\xBB\xBF/', '', $data);
|
$data = preg_replace('/^\xEF\xBB\xBF/', '', $data);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue