initial upload
This commit is contained in:
132
db_view_ordered.php
Normal file
132
db_view_ordered.php
Normal file
@ -0,0 +1,132 @@
|
||||
<?php
|
||||
// /var/www/html/db_view_ordered.php
|
||||
require_once __DIR__ . '/common.php';
|
||||
header('Content-Type: text/html; charset=UTF-8');
|
||||
|
||||
// ---- Auth ----
|
||||
$auth = $_SERVER['HTTP_AUTHORIZATION'] ?? ($_SERVER['Authorization'] ?? '');
|
||||
if (stripos($auth, 'Bearer ') !== 0) { http_response_code(401); echo "Missing Bearer token"; exit; }
|
||||
$token = substr($auth, 7);
|
||||
if (!token_is_valid($token)) { http_response_code(403); echo "Invalid or expired token"; exit; }
|
||||
|
||||
// ---- Paths for order, header labels and value translations ----
|
||||
$ORDER_JSON = __DIR__ . '/header_order.json';
|
||||
$LABELS_JSON = __DIR__ . '/questions_en.json'; // Spaltenüberschriften (2. Kopfzeile)
|
||||
$VALS_JSON = __DIR__ . '/translations_en.json'; // NEU: Werte-Übersetzungen
|
||||
|
||||
$order = @json_decode(@file_get_contents($ORDER_JSON), true);
|
||||
$labels = @json_decode(@file_get_contents($LABELS_JSON), true);
|
||||
$vals = @json_decode(@file_get_contents($VALS_JSON), true);
|
||||
if (!is_array($order) || empty($order)) { http_response_code(500); echo "header_order.json missing or empty."; exit; }
|
||||
if (!is_array($labels)) $labels = [];
|
||||
if (!is_array($vals)) $vals = [];
|
||||
|
||||
function t_val(string $id, string $raw, array $vals): string {
|
||||
// feste Wörter direkt durchlassen
|
||||
if ($raw === '' || $raw === 'None' || $raw === 'Done' || $raw === 'Not Done') return $raw;
|
||||
|
||||
// Kandidaten: exakt, normalisiert, id+value
|
||||
$norm = strtolower(preg_replace('/[^a-z0-9]+/i', '_', $raw));
|
||||
$cands = [$raw];
|
||||
if ($norm && $norm !== $raw) $cands[] = $norm;
|
||||
$cands[] = "{$id}_{$raw}";
|
||||
$cands[] = "{$id}-{$raw}";
|
||||
$cands[] = "{$id}_{$norm}";
|
||||
$cands[] = "{$id}-{$norm}";
|
||||
|
||||
foreach ($cands as $k) {
|
||||
if (isset($vals[$k]) && is_string($vals[$k]) && $vals[$k] !== '') return $vals[$k];
|
||||
}
|
||||
// Fallback: Versuch mit einfachen globalen Keys (z. B. "yes","no","consent_signed")
|
||||
if (isset($vals[$norm])) return (string)$vals[$norm];
|
||||
if (isset($vals[$raw])) return (string)$vals[$raw];
|
||||
|
||||
return $raw; // nichts gefunden → Original anzeigen
|
||||
}
|
||||
|
||||
// ---- load & decrypt DB ----
|
||||
$dbPath = __DIR__ . '/uploads/questionnaire_database';
|
||||
if (!file_exists($dbPath)) { http_response_code(404); echo "Database not found"; exit; }
|
||||
$enc = file_get_contents($dbPath);
|
||||
if ($enc === false) { http_response_code(500); echo "Read error"; exit; }
|
||||
|
||||
try { $plain = aes256_cbc_decrypt_bytes($enc, get_master_key_bytes()); }
|
||||
catch (Throwable $e) { http_response_code(500); echo "Decryption failed"; exit; }
|
||||
|
||||
// ---- temp SQLite file ----
|
||||
$tmp = tempnam(sys_get_temp_dir(), 'qdb_');
|
||||
file_put_contents($tmp, $plain);
|
||||
|
||||
try {
|
||||
$pdo = new PDO("sqlite:$tmp");
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
$clients = $pdo->query("SELECT clientCode FROM clients ORDER BY clientCode")->fetchAll(PDO::FETCH_COLUMN) ?: [];
|
||||
$questionnaireIds = $pdo->query("SELECT id FROM questionnaires")->fetchAll(PDO::FETCH_COLUMN) ?: [];
|
||||
$qidSet = array_fill_keys($questionnaireIds, true);
|
||||
|
||||
$completed = [];
|
||||
$stmt = $pdo->query("SELECT clientCode, questionnaireId, isDone FROM completed_questionnaires");
|
||||
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
|
||||
$c = $row['clientCode'] ?? 'undefined';
|
||||
$q = $row['questionnaireId'] ?? 'undefined';
|
||||
$completed[$c][$q] = ((int)($row['isDone'] ?? 0)) ? true : false;
|
||||
}
|
||||
|
||||
$answers = [];
|
||||
$stmt = $pdo->query("SELECT clientCode, questionId, answerValue FROM answers");
|
||||
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
|
||||
$c = $row['clientCode'] ?? 'undefined';
|
||||
$q = $row['questionId'] ?? 'undefined';
|
||||
$answers[$c][$q] = (string)($row['answerValue'] ?? '');
|
||||
}
|
||||
|
||||
// ---- HTML ----
|
||||
echo '<style>
|
||||
table{border-collapse:collapse;width:100%;}
|
||||
th,td{border:1px solid #ddd;padding:6px 8px;text-align:left;vertical-align:top;}
|
||||
thead th{background:#f7f7f7;position:sticky;top:0;z-index:1;}
|
||||
thead tr:nth-child(2) th{background:#fafafa;}
|
||||
.nowrap{white-space:nowrap;}
|
||||
.chkcol{width:36px;text-align:center;}
|
||||
</style>';
|
||||
|
||||
echo '<table id="qdb-table">';
|
||||
echo '<thead>';
|
||||
// Row 1: checkbox + IDs (bleiben als technische Schlüssel im Kopf – Werte darunter sind übersetzt)
|
||||
echo '<tr><th class="chkcol"><input type="checkbox" id="chkAll" title="select all"></th>';
|
||||
foreach ($order as $id) echo '<th data-id="'.htmlspecialchars($id).'">'.htmlspecialchars($id).'</th>';
|
||||
echo '</tr>';
|
||||
|
||||
// Row 2: blank + English labels
|
||||
echo '<tr><th class="chkcol"></th>';
|
||||
foreach ($order as $id) {
|
||||
$lbl = $labels[$id] ?? '';
|
||||
echo '<th>'.htmlspecialchars($lbl).'</th>';
|
||||
}
|
||||
echo '</tr></thead><tbody>';
|
||||
|
||||
foreach ($clients as $client) {
|
||||
echo '<tr data-client="'.htmlspecialchars($client).'">';
|
||||
echo '<td class="chkcol"><input type="checkbox" class="rowchk"></td>';
|
||||
foreach ($order as $id) {
|
||||
if ($id === 'client_code') {
|
||||
$val = $client;
|
||||
} elseif (isset($qidSet[$id]) && strpos($id, '-') === false) {
|
||||
$val = ($completed[$client][$id] ?? false) ? 'Done' : 'Not Done';
|
||||
} else {
|
||||
$raw = $answers[$client][$id] ?? '';
|
||||
$val = (strlen(trim($raw)) > 0) ? t_val($id, $raw, $vals) : 'None';
|
||||
}
|
||||
echo '<td>'.htmlspecialchars($val).'</td>';
|
||||
}
|
||||
echo '</tr>';
|
||||
}
|
||||
echo '</tbody></table>';
|
||||
|
||||
} catch (Throwable $e) {
|
||||
http_response_code(500);
|
||||
echo "Error: ".htmlspecialchars($e->getMessage());
|
||||
} finally {
|
||||
@unlink($tmp);
|
||||
}
|
||||
Reference in New Issue
Block a user