Skip to content

Commit

Permalink
Translate detected column headers
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpapst committed Apr 6, 2024
1 parent 6f231e5 commit 8dfbf6c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Compatibility: requires minimum Kimai 2.11.0

- Added: Validate Hourly/Internal/Fixed rate
- Changed: Translate detected column headers

## Version 2.10.0

Expand Down
24 changes: 20 additions & 4 deletions Importer/TimesheetImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,27 +134,43 @@ public function checkHeader(array $header): array

protected function createImportData(ImportRow $row): ImportData
{
return new ImportData('time_tracking', array_keys($row->getData()));
$translated = $this->getTranslatedHeaders();
$converted = [];

foreach ($row->getData() as $key => $value) {
if (\array_key_exists($key, $translated)) {
$converted[] = $translated[$key];
} else {
$converted[] = $key;
}
}

return new ImportData('time_tracking', $converted);
}

public function importRow(Duration $durationParser, ImportData $data, ImportRow $row, bool $dryRun): void
{
$rawData = $row->getData();
$translated = $this->getTranslatedHeaders();
$converted = [];

foreach ($rawData as $key => $value) {
if (\array_key_exists($key, $translated)) {
if (($newKey = $translated[$key]) === $key) {
$newKey = $translated[$key];
$converted[$newKey] = trim($value);
if ($newKey === $key) {
continue;
}
if (\array_key_exists($newKey, $rawData)) {
// prevent that existing key will be overwritten (e.g. Date using the export CSV)
continue;
}
$rawData[$newKey] = $value;
$rawData[$newKey] = trim($value);
} else {
$converted[$key] = trim($value);
}
}

parent::importRow($durationParser, $data, new ImportRow($rawData), $dryRun);
parent::importRow($durationParser, $data, new ImportRow($converted), $dryRun);
}
}

0 comments on commit 8dfbf6c

Please sign in to comment.