Files
Backend/WEBROOT/controller/DatabaseController.php
T
2026-05-11 12:53:15 +02:00

270 lines
7.7 KiB
PHP

<?php
function DatabaseCreateTable($table, $columnsDefinition) {
include_once 'DatabaseConnect.php';
$conn = db_connect();
$sql = "CREATE TABLE IF NOT EXISTS `$table` ($columnsDefinition)";
$stmt = $conn->prepare($sql);
if (!$stmt) {
die("Fehler beim Prepare: " . $conn->error);
}
if (!$stmt->execute()) {
die("Fehler beim Execute: " . $stmt->error);
}
return true;
}
function DatabaseShowTables() {
include_once 'DatabaseConnect.php';
$conn = db_connect();
$tablesResult = $conn->query("SHOW TABLES");
if (!$tablesResult) {
die("Fehler beim Laden der Tabellen: " . $conn->error);
}
$tables = [];
while ($tableRow = $tablesResult->fetch_array()) {
$tables[] = $tableRow[0];
}
return $tables;
}
function DatabaseShowTableStructure($table) {
include_once 'DatabaseConnect.php';
$conn = db_connect();
$stmt = $conn->prepare("SHOW COLUMNS FROM `$table`");
if (!$stmt) {
die("Fehler beim Prepare: " . $conn->error);
}
if (!$stmt->execute()) {
die("Fehler beim Execute: " . $stmt->error);
}
$result = $stmt->get_result();
$columns = [];
while ($column = $result->fetch_assoc()) {
$columns[] = [
"Field" => $column["Field"],
"Type" => $column["Type"],
"Null" => $column["Null"],
"Key" => $column["Key"],
"Default" => $column["Default"],
"Extra" => $column["Extra"]
];
}
return $columns;
}
function DatabaseShow($table, $columns = "*", $where = "", ...$values) {
include_once 'DatabaseConnect.php';
$conn = db_connect();
$sql = "SELECT $columns FROM `$table`";
if (!empty($where)) {
$sql .= " WHERE $where";
}
$stmt = $conn->prepare($sql);
if (!$stmt) {
die("Fehler beim Prepare: " . $conn->error);
}
if (!$stmt->execute($values)) {
die("Fehler beim Execute: " . $stmt->error);
}
$result = $stmt->get_result();
return $result->fetch_all(MYSQLI_ASSOC);
}
function DatabaseInsert($table, $columns, ...$values) {
include_once 'DatabaseConnect.php';
$conn = db_connect();
$placeholders = rtrim(str_repeat("?,", count($values)), ",");
$sql = "INSERT INTO `$table` ($columns) VALUES ($placeholders)";
$stmt = $conn->prepare($sql);
if (!$stmt) {
die("Fehler beim Prepare: " . $conn->error);
}
if (!$stmt->execute($values)) {
die("Fehler beim Execute: " . $stmt->error);
}
return true;
}
function DatabaseDelete($table, $where, ...$values) {
include_once 'DatabaseConnect.php';
$conn = db_connect();
$sql = "DELETE FROM `$table` WHERE $where";
$stmt = $conn->prepare($sql);
if (!$stmt) {
die("Fehler beim Prepare: " . $conn->error);
}
if (!$stmt->execute($values)) {
die("Fehler beim Execute: " . $stmt->error);
}
return true;
}
function DatabaseUpdate($table, $set, $where, ...$values) {
include_once 'DatabaseConnect.php';
$conn = db_connect();
$sql = "UPDATE `$table` SET $set WHERE $where";
$stmt = $conn->prepare($sql);
if (!$stmt) {
die("Fehler beim Prepare: " . $conn->error);
}
if (!$stmt->execute($values)) {
die("Fehler beim Execute: " . $stmt->error);
}
return true;
}
function getFKValue($conn, $table, $key, $value, $id, &$cache) {
if ($id === null) return null;
$cacheKey = $table . "_" . $id;
if (isset($cache[$cacheKey])) {
return $cache[$cacheKey];
}
$stmt = $conn->prepare("SELECT `$value` FROM `$table` WHERE `$key` = ? LIMIT 1");
if (!$stmt) {
die("FK Prepare Fehler: " . $conn->error);
}
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result()->fetch_assoc();
$valueResult = $result[$value] ?? null;
$cache[$cacheKey] = $valueResult;
return $valueResult;
}
function DatabaseShowWithFK($table, $fkMap = [], $columns = "*", $where = "", ...$values) {
include_once 'DatabaseConnect.php';
$conn = db_connect();
$cache = [];
$select = is_array($columns) ? implode(", ", $columns) : $columns;
$sql = "SELECT $select FROM `$table`";
if (!empty($where)) {
$sql .= " WHERE $where";
}
$stmt = $conn->prepare($sql);
if (!$stmt) die("Fehler beim Prepare: " . $conn->error);
if (!empty($values)) {
$stmt->execute($values);
} else {
$stmt->execute();
}
$result = $stmt->get_result();
if (!$result) die("Fehler beim get_result: " . $conn->error);
$rows = $result->fetch_all(MYSQLI_ASSOC);
foreach ($rows as &$row) {
foreach ($fkMap as $fkColumn => $fkInfo) {
if (!isset($row[$fkColumn]) || $row[$fkColumn] === null) {
continue;
}
$row[$fkColumn] = getFKValue(
$conn,
$fkInfo['table'],
$fkInfo['key'],
$fkInfo['value'],
$row[$fkColumn],
$cache
);
}
}
return [
"rows" => $rows
];
}
$Berufsschulberichte = [
"FACH_S1" => ["table" => "Fach", "key" => "ID", "value" => "Fach"],
"LEHRER_S1" => ["table" => "Lehrer", "key" => "ID", "value" => "NAME"],
"RAUM_S1" => ["table" => "Raum", "key" => "ID", "value" => "Raum_NR"],
"FACH_S2" => ["table" => "Fach", "key" => "ID", "value" => "Fach"],
"LEHRER_S2" => ["table" => "Lehrer", "key" => "ID", "value" => "NAME"],
"RAUM_S2" => ["table" => "Raum", "key" => "ID", "value" => "Raum_NR"],
"FACH_S3" => ["table" => "Fach", "key" => "ID", "value" => "Fach"],
"LEHRER_S3" => ["table" => "Lehrer", "key" => "ID", "value" => "NAME"],
"RAUM_S3" => ["table" => "Raum", "key" => "ID", "value" => "Raum_NR"],
"FACH_S4" => ["table" => "Fach", "key" => "ID", "value" => "Fach"],
"LEHRER_S4" => ["table" => "Lehrer", "key" => "ID", "value" => "NAME"],
"RAUM_S4" => ["table" => "Raum", "key" => "ID", "value" => "Raum_NR"],
"FACH_S5" => ["table" => "Fach", "key" => "ID", "value" => "Fach"],
"LEHRER_S5" => ["table" => "Lehrer", "key" => "ID", "value" => "NAME"],
"RAUM_S5" => ["table" => "Raum", "key" => "ID", "value" => "Raum_NR"],
"FACH_S6" => ["table" => "Fach", "key" => "ID", "value" => "Fach"],
"LEHRER_S6" => ["table" => "Lehrer", "key" => "ID", "value" => "NAME"],
"RAUM_S6" => ["table" => "Raum", "key" => "ID", "value" => "Raum_NR"],
"FACH_S7" => ["table" => "Fach", "key" => "ID", "value" => "Fach"],
"LEHRER_S7" => ["table" => "Lehrer", "key" => "ID", "value" => "NAME"],
"RAUM_S7" => ["table" => "Raum", "key" => "ID", "value" => "Raum_NR"],
"FACH_S8" => ["table" => "Fach", "key" => "ID", "value" => "Fach"],
"LEHRER_S8" => ["table" => "Lehrer", "key" => "ID", "value" => "NAME"],
"RAUM_S8" => ["table" => "Raum", "key" => "ID", "value" => "Raum_NR"],
];
$Stundenplan = [
"MO" => ["table" => "Fach", "key" => "ID", "value" => "Fach"],
"DI" => ["table" => "Fach", "key" => "ID", "value" => "Fach"],
"MI" => ["table" => "Fach", "key" => "ID", "value" => "Fach"],
"DO" => ["table" => "Fach", "key" => "ID", "value" => "Fach"],
"FR" => ["table" => "Fach", "key" => "ID", "value" => "Fach"],
];
$Berechtigungen = [
"BENUTZER_ID" => ["table" => "Benutzer", "key" => "ID", "value" => "NAME"],
"SEITEN_ID" => ["table" => "Backend_Seiten", "key" => "ID", "value" => "DATEINAME"]
];