false, 'message' => "Operation nicht gestattet."]); exit; } if ($anfrageType !== "start" && ($id < 1 || intval($jahr) < 1)) { echo json_encode(['success' => false, 'message' => 'Personen ID ist nicht valide.']); exit; } if ($geraetId < 1) { echo json_encode(['success' => false, 'message' => 'Invalid discipline']); exit; } $type = 'kr'; $data = require $baseDir . '/../scripts/db/db-verbindung-script.php'; if ($data['success'] === false){ echo json_encode(['success' => false, 'message' => $data['message']]); exit; } require $baseDir . '/../scripts/db/db-functions.php'; require $baseDir . '/../scripts/db/db-tables.php'; $stmt = $mysqli->prepare("SELECT `name` FROM $tableGeraete WHERE `id` = ? LIMIT 1"); $stmt->bind_param("s", $geraetId); if (!$stmt->execute()) { http_response_code(500); exit; } $result = $stmt->get_result(); if ($result->num_rows === 0) { echo json_encode(['success' => false, 'message' => 'Invalid discipline']); exit; } $geraetData = $result->fetch_assoc(); $geraetName = $geraetData['name']; $stmt->close(); $folder = realpath($baseDir . '/displays/json'); if ($folder === false) { echo json_encode([ 'success' => false, 'message' => 'Could not find displays folder.' ]); exit; } $filename = 'display_' . strtolower($geraetName) . '.json'; $filepath = $folder . '/' . $filename; if (!is_writable($folder)) { echo json_encode(['success' => false, 'message' => 'Folder not writable']); exit; } $jsonString = file_get_contents($filepath); // decode JSON, fallback to empty array if invalid $oldjson = json_decode($jsonString, true) ?? []; switch ($anfrageType) { case "neu": $stmt = $mysqli->prepare("SELECT * FROM `$tableTurnerinnen` WHERE id = ? LIMIT 1"); $stmt->bind_param("i", $id); $stmt->execute(); $result = $stmt->get_result(); $rows = $result->fetch_all(MYSQLI_ASSOC); if (!$rows || !is_array($rows) || count($rows) !== 1) { echo json_encode(['success' => false, 'message' => 'Row fetch failed']); exit; } $row = $rows[0]; // safely get value, default 0 if missing $olduniqueid = $oldjson['uniqueid'] ?? 0; $uniqueid = $olduniqueid + 1; $data = ["noteLinks" => '', "noteRechts" => '', "id" => $id, "name" => $row['name'], "vorname" => $row['vorname'], "programm" => $row['programm'], "verein" => $row['verein'], "start" => false, "musik" => 'nan', "uniqueid" => $uniqueid]; $jsonData = json_encode($data); break; case "start": if (!array_key_exists("id", $oldjson) || intval($oldjson["id"]) !== $id || !array_key_exists("start", $oldjson)) { echo json_encode(['success' => false, 'message' => 'Person nicht auf Display!']); exit; } $oldjson["start"] = (bool) $dataType; $jsonData = json_encode($oldjson); break; case "result": // 1. Get IDs and filter out empty values $noteLinksId = db_get_var($mysqli, "SELECT `value` FROM $tableVar WHERE `name` = ?", ['displayIdNoteL']); $noteRechtsId = db_get_var($mysqli, "SELECT `value` FROM $tableVar WHERE `name` = ?", ['displayIdNoteR']); $stmt = $mysqli->prepare("UPDATE $tableNoten SET `is_public` = 1, `public_value` = `value` WHERE `person_id` = ? AND `jahr` = ? AND `geraet_id` = ? AND `run_number` = ?"); $stmt->bind_param("ssss", $id, $jahr, $geraetId, $run); $stmt->execute(); $stmt->close(); // Create an array of IDs that actually exist $validIds = array_filter([$noteLinksId, $noteRechtsId]); $noten = []; $notenConfig = []; if (!empty($validIds)) { // 2. Fetch Noten (Only if we have IDs to look for) $placeholders = implode(',', array_fill(0, count($validIds), '?')); $sqlNoten = "SELECT `value`, `note_bezeichnung_id` FROM $tableNoten WHERE person_id = ? AND `jahr` = ? AND `geraet_id` = ? AND run_number = ? AND `note_bezeichnung_id` IN ($placeholders)"; $stmt = $mysqli->prepare($sqlNoten); // Combine standard params with our dynamic ID list $params = array_merge([$id, $jahr, $geraetId, $run], $validIds); $types = str_repeat('s', count($params)); $stmt->bind_param($types, ...$params); $stmt->execute(); $notenDB = $stmt->get_result()->fetch_all(MYSQLI_ASSOC); $noten = array_column($notenDB, 'value', 'note_bezeichnung_id'); $stmt->close(); // 3. Fetch Config $sqlConfig = "SELECT `id`, `default_value`, `nullstellen`, `prefix_display` FROM $tableNotenBezeichnungen WHERE `id` IN ($placeholders)"; $stmt = $mysqli->prepare($sqlConfig); $typesConfig = str_repeat('s', count($validIds)); $stmt->bind_param($typesConfig, ...$validIds); $stmt->execute(); $notenConfigDB = $stmt->get_result()->fetch_all(MYSQLI_ASSOC); $notenConfig = array_column($notenConfigDB, null, 'id'); $stmt->close(); } // 4. Helper function to safely format the output without crashing $formatNote = function($id) use ($noten, $notenConfig) { if (!$id || !isset($notenConfig[$id])) { return ""; // Return empty string if ID is not set or not found in DB } $conf = $notenConfig[$id]; $val = $noten[$id] ?? $conf['default_value'] ?? 0; $prec = $conf['nullstellen'] ?? 2; $pre = $conf['prefix_display'] ?? ''; return $pre . number_format((float)$val, (int)$prec, '.', ''); }; // 5. Assign to JSON $oldjson["noteLinks"] = $formatNote($noteLinksId); $oldjson["noteRechts"] = $formatNote($noteRechtsId); $jsonData = json_encode($oldjson); break; } // Write file if (file_put_contents($filepath, $jsonData) === false) { echo json_encode([ 'success' => false, 'message' => 'Failed to write JSON file' ]); exit; } // ---------- Return JSON ---------- echo json_encode([ 'success' => true, 'message' => 'JSON updated successfully for ' . $geraetName, 'data' => json_decode($jsonData, true), 'nameGeraet' => strtolower($geraetName) ]); exit;