improved condition json to have UI integration
This commit is contained in:
105
common.php
105
common.php
@ -512,7 +512,7 @@ function qdb_set_category_german_label(PDO $pdo, string $categoryKey, string $te
|
||||
json_error('INVALID_FIELD', 'German label is required', 400);
|
||||
}
|
||||
$stringKey = qdb_questionnaire_group_string_key($categoryKey);
|
||||
qdb_put_translation($pdo, 'app_string', $stringKey, QDB_SOURCE_LANGUAGE, $text);
|
||||
qdb_set_app_string_german_label($pdo, $stringKey, $text);
|
||||
qdb_update_questionnaire_group_german_default($stringKey, $text);
|
||||
return $stringKey;
|
||||
}
|
||||
@ -569,12 +569,111 @@ function qdb_app_ui_string_entries(): array {
|
||||
* Static catalog plus questionnaire_group_* keys for categoryKey values in use.
|
||||
* @return list<array{key: string, type: string, entityId: string, sortOrder: int}>
|
||||
*/
|
||||
/** messageKey field inside questionnaire conditionJson (app locked hint). */
|
||||
function qdb_message_key_from_condition_json(?string $json): string {
|
||||
if ($json === null || trim($json) === '' || trim($json) === '{}') {
|
||||
return '';
|
||||
}
|
||||
$obj = json_decode($json, true);
|
||||
if (!is_array($obj)) {
|
||||
return '';
|
||||
}
|
||||
$key = trim((string)($obj['messageKey'] ?? ''));
|
||||
return $key !== '' && qdb_is_stable_key($key) ? $key : '';
|
||||
}
|
||||
|
||||
/** @return array<string, true> */
|
||||
function qdb_collect_condition_message_keys(PDO $pdo): array {
|
||||
$keys = [];
|
||||
foreach ($pdo->query('SELECT conditionJson FROM questionnaire')->fetchAll(PDO::FETCH_COLUMN) as $json) {
|
||||
$mk = qdb_message_key_from_condition_json($json);
|
||||
if ($mk !== '') {
|
||||
$keys[$mk] = true;
|
||||
}
|
||||
}
|
||||
return $keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* App string entries for condition messageKey values referenced in questionnaire conditions.
|
||||
* @return list<array{key: string, displayKey: string, type: string, entityId: string, sortOrder: int, germanDefault: string}>
|
||||
*/
|
||||
function qdb_condition_message_string_entries(PDO $pdo): array {
|
||||
$staticKeys = qdb_app_string_key_set();
|
||||
$defaults = qdb_app_string_catalog()['germanDefaults'] ?? [];
|
||||
$entries = [];
|
||||
$order = 0;
|
||||
foreach (array_keys(qdb_collect_condition_message_keys($pdo)) as $key) {
|
||||
if (isset($staticKeys[$key])) {
|
||||
continue;
|
||||
}
|
||||
$entries[] = [
|
||||
'key' => $key,
|
||||
'displayKey' => $key,
|
||||
'type' => 'app_string',
|
||||
'entityId' => $key,
|
||||
'sortOrder' => $order++,
|
||||
'germanDefault' => $defaults[$key] ?? $key,
|
||||
];
|
||||
}
|
||||
usort($entries, fn($a, $b) => strcmp($a['key'], $b['key']));
|
||||
return $entries;
|
||||
}
|
||||
|
||||
function qdb_update_app_string_german_default(string $stringKey, string $text): void {
|
||||
$path = qdb_app_string_catalog_path();
|
||||
if (!is_readable($path) || !is_writable($path)) {
|
||||
return;
|
||||
}
|
||||
$data = json_decode(file_get_contents($path), true);
|
||||
if (!is_array($data)) {
|
||||
return;
|
||||
}
|
||||
if (!isset($data['germanDefaults']) || !is_array($data['germanDefaults'])) {
|
||||
$data['germanDefaults'] = [];
|
||||
}
|
||||
if (($data['germanDefaults'][$stringKey] ?? '') === $text) {
|
||||
return;
|
||||
}
|
||||
$data['germanDefaults'][$stringKey] = $text;
|
||||
file_put_contents(
|
||||
$path,
|
||||
json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n",
|
||||
LOCK_EX
|
||||
);
|
||||
qdb_app_string_catalog(true);
|
||||
}
|
||||
|
||||
function qdb_set_app_string_german_label(PDO $pdo, string $stringKey, string $text): void {
|
||||
$stringKey = qdb_validate_stable_key($stringKey, 'messageKey');
|
||||
$text = trim($text);
|
||||
if ($text === '') {
|
||||
json_error('INVALID_FIELD', 'German label is required', 400);
|
||||
}
|
||||
qdb_put_translation($pdo, 'app_string', $stringKey, QDB_SOURCE_LANGUAGE, $text);
|
||||
qdb_update_app_string_german_default($stringKey, $text);
|
||||
}
|
||||
|
||||
function qdb_app_ui_string_entries_merged(PDO $pdo): array {
|
||||
$merged = qdb_app_ui_string_entries();
|
||||
$existing = [];
|
||||
foreach ($merged as $e) {
|
||||
$existing[$e['key']] = true;
|
||||
}
|
||||
$order = count($merged);
|
||||
foreach (qdb_category_group_string_entries($pdo) as $e) {
|
||||
$e['sortOrder'] = $order++;
|
||||
$merged[] = $e;
|
||||
if (!isset($existing[$e['key']])) {
|
||||
$e['sortOrder'] = $order++;
|
||||
$merged[] = $e;
|
||||
$existing[$e['key']] = true;
|
||||
}
|
||||
}
|
||||
foreach (qdb_condition_message_string_entries($pdo) as $e) {
|
||||
if (!isset($existing[$e['key']])) {
|
||||
$e['sortOrder'] = $order++;
|
||||
$merged[] = $e;
|
||||
$existing[$e['key']] = true;
|
||||
}
|
||||
}
|
||||
return $merged;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user