session management
Some checks failed
PHPUnit / test (push) Has been cancelled

This commit is contained in:
tom.hempel
2026-06-30 08:35:18 +02:00
parent 7fefc264f4
commit 9db886a1ae
6 changed files with 7026 additions and 5 deletions

View File

@ -1669,17 +1669,86 @@ function qdb_build_app_translations_map(PDO $pdo): array {
function qdb_build_questionnaire_translations_map(PDO $pdo, string $qnID): array {
$lists = qdb_translation_entry_lists($pdo, $qnID);
$entries = array_merge($lists['stringEntries'], $lists['contentEntries']);
if (!$entries) {
return [];
}
$translations = qdb_load_translations_for_entries($pdo, $entries);
$translations = $entries ? qdb_load_translations_for_entries($pdo, $entries) : [];
$result = [];
$stringLabelCache = [];
$appDefaults = qdb_app_string_catalog()['germanDefaults'] ?? [];
foreach ($entries as $e) {
$key = $e['key'];
if ($key === '') {
continue;
}
foreach ($translations[$e['entityId']] ?? [] as $lang => $text) {
$result[$lang][$key] = $text;
$text = trim((string)$text);
if ($text !== '') {
$result[$lang][$key] = $text;
}
}
$german = trim($e['germanText'] ?? '');
if ($german === '' && ($e['type'] ?? '') === 'string') {
$trDe = trim($translations[$e['entityId']][QDB_SOURCE_LANGUAGE] ?? '');
if ($trDe !== '' && !qdb_is_stable_key($trDe)) {
$german = $trDe;
} elseif (!empty($appDefaults[$key])) {
$german = trim((string)$appDefaults[$key]);
} else {
$fetched = trim(qdb_string_german_label($pdo, $key, $stringLabelCache));
if ($fetched !== '' && !qdb_is_stable_key($fetched)) {
$german = $fetched;
}
}
}
if ($german !== '' && !isset($result[QDB_SOURCE_LANGUAGE][$key])) {
$result[QDB_SOURCE_LANGUAGE][$key] = $german;
}
}
$qStmt = $pdo->prepare(
'SELECT questionID, defaultText, configJson FROM question WHERE questionnaireID = :id ORDER BY orderIndex'
);
$qStmt->execute([':id' => $qnID]);
foreach ($qStmt->fetchAll(PDO::FETCH_ASSOC) as $dbQ) {
$config = json_decode($dbQ['configJson'] ?: '{}', true) ?: [];
$qKey = qdb_question_key($config, $dbQ['defaultText']);
if ($qKey === '') {
continue;
}
$qGerman = qdb_question_german_label($pdo, $dbQ);
if ($qGerman !== '' && !qdb_is_stable_key($qGerman)) {
$result[QDB_SOURCE_LANGUAGE][$qKey] = $qGerman;
}
$noteBefore = trim((string)($config['noteBefore'] ?? ''));
if ($noteBefore !== '') {
$result[QDB_SOURCE_LANGUAGE][qdb_note_before_key($qKey)] = $noteBefore;
$textKey = trim((string)($config['textKey'] ?? ''));
if ($textKey !== '') {
$result[QDB_SOURCE_LANGUAGE][$textKey] = $noteBefore;
}
}
$noteAfter = trim((string)($config['noteAfter'] ?? ''));
if ($noteAfter !== '') {
$result[QDB_SOURCE_LANGUAGE][qdb_note_after_key($qKey)] = $noteAfter;
}
}
$aoStmt = $pdo->prepare(
'SELECT answerOptionID, defaultText FROM answer_option WHERE questionID IN (
SELECT questionID FROM question WHERE questionnaireID = :id
) ORDER BY questionID, orderIndex'
);
$aoStmt->execute([':id' => $qnID]);
foreach ($aoStmt->fetchAll(PDO::FETCH_ASSOC) as $ao) {
$optKey = qdb_option_key($ao);
if ($optKey === '') {
continue;
}
$optGerman = qdb_option_german_label($pdo, $ao['answerOptionID'], $ao['defaultText']);
if ($optGerman !== '' && !qdb_is_stable_key($optGerman)) {
$result[QDB_SOURCE_LANGUAGE][$optKey] = $optGerman;
}
}
return $result;
}
@ -2253,6 +2322,52 @@ function qdb_import_translations_bundle(PDO $pdo, array $bundle): array {
];
}
/**
* Remove translation rows from the database.
* By default keeps German source language rows and the de language entry.
*/
function qdb_delete_all_translations(PDO $pdo, bool $keepSourceLanguage = true): array {
if ($keepSourceLanguage) {
$de = QDB_SOURCE_LANGUAGE;
$count = static function (PDO $pdo, string $table) use ($de): int {
$stmt = $pdo->prepare("SELECT COUNT(*) FROM {$table} WHERE languageCode != :de");
$stmt->execute([':de' => $de]);
return (int)$stmt->fetchColumn();
};
$questionCount = $count($pdo, 'question_translation');
$answerCount = $count($pdo, 'answer_option_translation');
$stringCount = $count($pdo, 'string_translation');
$langStmt = $pdo->prepare('SELECT COUNT(*) FROM language WHERE languageCode != :de');
$langStmt->execute([':de' => $de]);
$languageCount = (int)$langStmt->fetchColumn();
$pdo->prepare('DELETE FROM question_translation WHERE languageCode != :de')->execute([':de' => $de]);
$pdo->prepare('DELETE FROM answer_option_translation WHERE languageCode != :de')->execute([':de' => $de]);
$pdo->prepare('DELETE FROM string_translation WHERE languageCode != :de')->execute([':de' => $de]);
$pdo->prepare('DELETE FROM language WHERE languageCode != :de')->execute([':de' => $de]);
} else {
$questionCount = (int)$pdo->query('SELECT COUNT(*) FROM question_translation')->fetchColumn();
$answerCount = (int)$pdo->query('SELECT COUNT(*) FROM answer_option_translation')->fetchColumn();
$stringCount = (int)$pdo->query('SELECT COUNT(*) FROM string_translation')->fetchColumn();
$languageCount = (int)$pdo->query('SELECT COUNT(*) FROM language')->fetchColumn();
$pdo->exec('DELETE FROM question_translation');
$pdo->exec('DELETE FROM answer_option_translation');
$pdo->exec('DELETE FROM string_translation');
$pdo->exec('DELETE FROM language');
}
qdb_ensure_source_language($pdo);
return [
'question' => $questionCount,
'answer_option' => $answerCount,
'string' => $stringCount,
'languages' => $languageCount,
'keptSource' => $keepSourceLanguage,
];
}
/** Import a full export bundle. */
function qdb_import_questionnaires_bundle(PDO $pdo, array $bundle, bool $replaceIfExists = false): array {
$items = $bundle['questionnaires'] ?? [];