Semi-stable version with old variable names
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
header('Content-Type: application/json');
|
||||
|
||||
|
||||
ini_set("display_errors", 1);
|
||||
|
||||
if (!isset($baseDir)) {
|
||||
$baseDir = $_SERVER['DOCUMENT_ROOT'];
|
||||
}
|
||||
|
||||
require_once $baseDir . '/../scripts/session_functions.php';
|
||||
|
||||
ini_wkvs_session();
|
||||
|
||||
check_user_permission('wk_leitung');
|
||||
|
||||
verify_csrf();
|
||||
|
||||
require $baseDir . '/../scripts/db/db-functions.php';
|
||||
require $baseDir . '/../scripts/db/db-tables.php';
|
||||
|
||||
|
||||
$type = 'wkl';
|
||||
$dbconnection = require $baseDir . '/../scripts/db/db-verbindung-script.php';
|
||||
|
||||
if ($dbconnection['success'] !== true){
|
||||
echo json_encode(['success' => false, 'message' => 'Critical DB Error.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
$action = $_POST['action'] ?? '';
|
||||
|
||||
if ($action === 'update') {
|
||||
$abt_id = intval($_POST['abt'] ?? 0);
|
||||
$field = $_POST['type'] ?? '';
|
||||
$type_id = intval($_POST['type_id'] ?? 0);
|
||||
|
||||
$value = $_POST['value'] ?? '';
|
||||
|
||||
if (!preg_match('/^\d{2}:\d{2}$/', $value)) {
|
||||
echo json_encode(['success' => false, 'message' => 'Invalid parameters.']);
|
||||
http_response_code(400);
|
||||
exit;
|
||||
}
|
||||
|
||||
$allowedFields = ['start', 'end'];
|
||||
if (
|
||||
$abt_id > 0 &&
|
||||
$type_id > 0 &&
|
||||
db_check_var($mysqli, $tableAbt, 'id = ?', [$abt_id]) &&
|
||||
db_check_var($mysqli, $tableZeitplanTypes, 'id = ?', [$type_id]) &&
|
||||
($field === 'start' || ($field === 'end' && db_check_var($mysqli, $tableZeitplanTypes, 'id = ? AND has_endtime = ?', [$type_id, 1])))
|
||||
) {
|
||||
|
||||
$stmt = $mysqli->prepare("INSERT INTO $tableAbtZeiten (`abt_id`, `zeitplan_id`, `" . $field . "_time`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE `" . $field . "_time` = VALUES(`" . $field . "_time`)");
|
||||
|
||||
$stmt->bind_param("iis", $abt_id, $type_id, $value);
|
||||
|
||||
$stmt->execute();
|
||||
|
||||
$stmt->close();
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Invalid parameters.']);
|
||||
http_response_code(400);
|
||||
exit;
|
||||
}
|
||||
|
||||
} elseif ($action === 'delete') {
|
||||
$abt_id = intval($_POST['abt'] ?? 0);
|
||||
$type_id = intval($_POST['type_id'] ?? 0);
|
||||
|
||||
if ($type_id > 0 && $abt_id > 0) {
|
||||
db_delete($mysqli, $tableAbtZeiten, ['abt_id' => $abt_id, 'zeitplan_id' => $type_id]);
|
||||
echo json_encode(['success' => true]);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Invalid ID.']);
|
||||
http_response_code(400);
|
||||
exit;
|
||||
}
|
||||
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Action not found.']);
|
||||
}
|
||||
@@ -19,6 +19,16 @@ check_user_permission('wk_leitung');
|
||||
|
||||
verify_csrf();
|
||||
|
||||
$allowed_operations = ['overrite', 'add'];
|
||||
|
||||
if (!isset($_POST['operation']) || !in_array($_POST['operation'], $allowed_operations, true)) {
|
||||
http_response_code(400);
|
||||
echo 'Invalid operation.';
|
||||
exit;
|
||||
}
|
||||
|
||||
$operation = $_POST['operation'];
|
||||
|
||||
$type = 'wkl';
|
||||
|
||||
$dbconnection = require $baseDir . '/../scripts/db/db-verbindung-script.php';
|
||||
@@ -32,43 +42,18 @@ require $baseDir . '/../scripts/db/db-functions.php';
|
||||
require $baseDir . '/../scripts/db/db-tables.php';
|
||||
|
||||
$allGeraete = db_select($mysqli, $tableGeraete, "id, name, start_index", "", [], "start_index ASC");
|
||||
$allAbt = db_select($mysqli, $tableAbt, "name, id", "", [], "name ASC");
|
||||
$allAbt = db_select($mysqli, $tableAbt, "name, id, order_index", "", [], "order_index ASC");
|
||||
|
||||
$indexded_order_index_abt = array_column($allAbt, null, 'order_index');
|
||||
|
||||
$maxTurnerinnenGruppe = 8;
|
||||
|
||||
// $allGeraete[] = ['name' => 'null', 'id' => 'null'];
|
||||
// $allAbt[] = ['name' => 'null', 'id' => 'null'];
|
||||
|
||||
/*$stmt = $mysqli->prepare("
|
||||
SELECT
|
||||
t.id,
|
||||
t.programm,
|
||||
t.verein,
|
||||
GROUP_CONCAT(ta.id SEPARATOR ', ') AS abt_table_id
|
||||
FROM $tableTurnerinnen t
|
||||
LEFT JOIN $tableTurnerinnenAbt ta ON ta.turnerin_id = t.id
|
||||
GROUP BY t.id, t.programm, t.verein
|
||||
ORDER BY t.id DESC
|
||||
");
|
||||
|
||||
$stmt->execute();
|
||||
|
||||
|
||||
$result = $stmt->get_result();
|
||||
$allTurnerinnen = $result->fetch_all(MYSQLI_ASSOC);
|
||||
|
||||
$stmt->close();
|
||||
|
||||
print_r($allTurnerinnen);*/
|
||||
|
||||
|
||||
$allTurnerinnen = db_select($mysqli, $tableTurnerinnen, "id, programm, verein, name" , "", [], "programm ASC");
|
||||
$allTurnerinnen = db_select($mysqli, $tableTeilnehmende, "id, programm, verein, name" , "", [], "programm ASC");
|
||||
|
||||
$grouped = [];
|
||||
|
||||
foreach ($allTurnerinnen as $t) {
|
||||
$sanitasedprogramm = str_replace(['-kader'], '', strtolower($t['programm']));
|
||||
$grouped[$sanitasedprogramm][$t['verein']][] = $t;
|
||||
$grouped[strtolower($t['programm'])][$t['verein']][] = $t;
|
||||
}
|
||||
|
||||
function getMinKey(array $array): string|int {
|
||||
@@ -159,28 +144,35 @@ function arrayRiegeneiteilung($allturnerinnen, $maxTurnerinnenGruppe, $allGeraet
|
||||
|
||||
$arrayAutoRiegeneinteilung = arrayRiegeneiteilung($grouped, $maxTurnerinnenGruppe, $allGeraete);
|
||||
|
||||
if ($operation === 'overrite') {
|
||||
$stmt = $mysqli->prepare("DELETE FROM $tableAbt");
|
||||
$stmt->execute();
|
||||
$stmt->close();
|
||||
}
|
||||
|
||||
|
||||
$stmt = $mysqli->prepare("DELETE FROM $tableTurnerinnenAbt");
|
||||
$stmt = $mysqli->prepare("DELETE FROM $tableTeilnehmendeAbt");
|
||||
$stmt->execute();
|
||||
$stmt->close();
|
||||
|
||||
$stmt = $mysqli->prepare("DELETE FROM $tableAbt");
|
||||
$stmt->execute();
|
||||
$stmt->close();
|
||||
|
||||
|
||||
foreach ($arrayAutoRiegeneinteilung as $indAbt => $abt) {
|
||||
|
||||
$stmt = $mysqli->prepare("INSERT INTO $tableAbt (name) VALUES (?)");
|
||||
$stmt->bind_param("s", $indAbt);
|
||||
$stmt->execute();
|
||||
$idAbt = $stmt->insert_id;
|
||||
$stmt->close();
|
||||
if ($operation === 'overrite' || !isset($indexded_order_index_abt[$indAbt])) {
|
||||
$var_name = "Abteilung $indAbt";
|
||||
|
||||
$stmt = $mysqli->prepare("INSERT INTO $tableAbt (`name`, `order_index`) VALUES (?, ?)");
|
||||
$stmt->bind_param("si", $var_name, $indAbt);
|
||||
$stmt->execute();
|
||||
$idAbt = $stmt->insert_id;
|
||||
$stmt->close();
|
||||
} else {
|
||||
$idAbt = $indexded_order_index_abt[$indAbt]['id'];
|
||||
}
|
||||
|
||||
foreach ($abt as $indGeraet => $geraet) {
|
||||
foreach ($geraet as $indTurnerin => $turnerin) {
|
||||
$stmt = $mysqli->prepare("INSERT INTO $tableTurnerinnenAbt (turnerin_id, abteilung_id, geraet_id, turnerin_index) VALUES (?, ?, ?, ?)");
|
||||
$stmt = $mysqli->prepare("INSERT INTO $tableTeilnehmendeAbt (turnerin_id, abteilung_id, geraet_id, turnerin_index) VALUES (?, ?, ?, ?)");
|
||||
$stmt->bind_param("ssss", $turnerin['id'], $idAbt, $indGeraet, $turnerin['turnerin_index']);
|
||||
$stmt->execute();
|
||||
$stmt->close();
|
||||
@@ -188,47 +180,4 @@ foreach ($arrayAutoRiegeneinteilung as $indAbt => $abt) {
|
||||
}
|
||||
}
|
||||
|
||||
return http_response_code(201);
|
||||
|
||||
|
||||
|
||||
|
||||
/*foreach ($grouped as $ind => $g) {
|
||||
echo $ind;
|
||||
foreach ($g as $verein) {
|
||||
foreach ($verein as $turnerin) {
|
||||
echo '<div class="riegeneinteilung_turnerin_row" data-turnerin_id="' . $turnerin['id'] . '">';
|
||||
echo '<div class="riegeneinteilung_turnerin_info">';
|
||||
echo '<span class="riegeneinteilung_turnerin_name">' . htmlspecialchars($turnerin['name']) . '</span>';
|
||||
echo '<span class="riegeneinteilung_turnerin_verein">(' . htmlspecialchars($turnerin['verein']) . ')</span>';
|
||||
echo '<span class="riegeneinteilung_turnerin_verein">(' . htmlspecialchars($turnerin['abt']) . ')</span>';
|
||||
echo '<span class="riegeneinteilung_turnerin_verein">(' . htmlspecialchars($turnerin['geraet_id']) . ')</span>';
|
||||
echo '</div>';
|
||||
|
||||
echo '</div></div>';
|
||||
}
|
||||
}
|
||||
echo '<br><br>';
|
||||
}
|
||||
|
||||
foreach ($arrayAutoRiegeneinteilung as $ind => $abt) {
|
||||
|
||||
echo '<br>ABT:' . $ind;
|
||||
foreach ($abt as $g) {
|
||||
echo '<br>GERÄT';
|
||||
foreach ($g as $turnerin) {
|
||||
echo '<div class="riegeneinteilung_turnerin_row" data-turnerin_id="' . $turnerin['id'] . '">';
|
||||
echo '<div class="riegeneinteilung_turnerin_info">';
|
||||
echo '<span class="riegeneinteilung_turnerin_name">' . htmlspecialchars($turnerin['name']) . '</span>';
|
||||
echo '<span class="riegeneinteilung_turnerin_verein">(' . htmlspecialchars($turnerin['verein']) . ')</span>';
|
||||
echo '<span class="riegeneinteilung_turnerin_verein">(' . htmlspecialchars($turnerin['turnerin_index']) . ')</span>';
|
||||
echo '<span class="riegeneinteilung_turnerin_verein">(' . htmlspecialchars($turnerin['programm']) . ')</span>';
|
||||
echo '</div>';
|
||||
|
||||
echo '</div></div>';
|
||||
}
|
||||
}
|
||||
echo '<br><br>';
|
||||
}
|
||||
|
||||
print_r($arrayAutoRiegeneinteilung);*/
|
||||
return http_response_code(200);
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
$baseDir = $_SERVER['DOCUMENT_ROOT'];
|
||||
|
||||
require_once $baseDir . '/../scripts/session_functions.php';
|
||||
|
||||
ini_wkvs_session();
|
||||
|
||||
check_user_permission('wk_leitung');
|
||||
|
||||
verify_csrf();
|
||||
|
||||
// Validate input
|
||||
if (!isset($_POST['abt']) || !ctype_digit($_POST['abt']) || !isset($_POST['newName'])) {
|
||||
http_response_code(406);
|
||||
exit;
|
||||
}
|
||||
|
||||
$abtInput = (int) $_POST['abt'];
|
||||
$new_name = trim($_POST['newName']);
|
||||
|
||||
$type = 'wkl';
|
||||
|
||||
$dbconnection = require $baseDir . '/../scripts/db/db-verbindung-script.php';
|
||||
|
||||
if ($dbconnection['success'] !== true){
|
||||
echo 'Critical DB Error.';
|
||||
exit;
|
||||
}
|
||||
|
||||
require $baseDir . '/../scripts/db/db-functions.php';
|
||||
require $baseDir . '/../scripts/db/db-tables.php';
|
||||
|
||||
|
||||
db_update(
|
||||
$mysqli,
|
||||
$tableAbt,
|
||||
['name' => $new_name],
|
||||
['id' => $abtInput]
|
||||
);
|
||||
|
||||
http_response_code(200);
|
||||
echo json_encode(['success' => true]);
|
||||
exit;
|
||||
@@ -44,20 +44,21 @@ $abts = db_select($mysqli, $tableAbt, '*');
|
||||
|
||||
// Delete excess Abteilungen
|
||||
foreach ($abts as $dbabt) {
|
||||
if ((int)$dbabt['name'] > $anz_abt) {
|
||||
db_delete($mysqli, $tableAbt, ['name' => $dbabt['name']]);
|
||||
if ((int) $dbabt['order_index'] > $anz_abt) {
|
||||
db_delete($mysqli, $tableAbt, ['id' => $dbabt['id']]);
|
||||
}
|
||||
}
|
||||
|
||||
// Build lookup of existing names
|
||||
$existing = array_map('intval', array_column($abts, 'name'));
|
||||
$existing = array_map('intval', array_column($abts, 'order_index'));
|
||||
|
||||
// Insert missing Abteilungen
|
||||
$stmt = $mysqli->prepare("INSERT INTO $tableAbt (name) VALUES (?)");
|
||||
$stmt = $mysqli->prepare("INSERT INTO $tableAbt (`name`, `order_index`) VALUES (?, ?)");
|
||||
|
||||
for ($i = 1; $i <= $anz_abt; $i++) {
|
||||
if (!in_array($i, $existing, true)) {
|
||||
$stmt->bind_param('i', $i);
|
||||
$var_name = "Abteilung $i";
|
||||
$stmt->bind_param('si', $var_name, $i);
|
||||
$stmt->execute();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
$baseDir = $_SERVER['DOCUMENT_ROOT'];
|
||||
|
||||
require_once $baseDir . '/../scripts/session_functions.php';
|
||||
|
||||
ini_wkvs_session();
|
||||
|
||||
check_user_permission('wk_leitung');
|
||||
|
||||
verify_csrf();
|
||||
|
||||
// Validate input
|
||||
if (!isset($_POST['abt']) || !ctype_digit($_POST['abt']) || !isset($_POST['newOrderIndex'])) {
|
||||
http_response_code(406);
|
||||
exit;
|
||||
}
|
||||
|
||||
$abtInput = (int) $_POST['abt'];
|
||||
$newOrderIndex_input = (int) $_POST['newOrderIndex'];
|
||||
|
||||
$type = 'wkl';
|
||||
|
||||
$dbconnection = require $baseDir . '/../scripts/db/db-verbindung-script.php';
|
||||
|
||||
if ($dbconnection['success'] !== true){
|
||||
echo 'Critical DB Error.';
|
||||
exit;
|
||||
}
|
||||
|
||||
require $baseDir . '/../scripts/db/db-functions.php';
|
||||
require $baseDir . '/../scripts/db/db-tables.php';
|
||||
|
||||
$allAbts = db_select($mysqli, $tableAbt, 'id', 'id != ? ORDER BY order_index ASC', [$abtInput]);
|
||||
$ids = array_column($allAbts, 'id');
|
||||
|
||||
$targetIndex = max(0, $newOrderIndex_input - 1);
|
||||
|
||||
array_splice($ids, $targetIndex, 0, $abtInput);
|
||||
|
||||
$mysqli->begin_transaction();
|
||||
|
||||
$one_based_indexes = [];
|
||||
|
||||
try {
|
||||
foreach ($ids as $zeroBasedIndex => $abtId) {
|
||||
$newIndex = $zeroBasedIndex + 1;
|
||||
db_update(
|
||||
$mysqli,
|
||||
$tableAbt,
|
||||
['order_index' => $newIndex],
|
||||
['id' => $abtId]
|
||||
);
|
||||
$one_based_indexes[$abtId] = $newIndex;
|
||||
}
|
||||
|
||||
$mysqli->commit();
|
||||
} catch (Exception $e) {
|
||||
$mysqli->rollback();
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
||||
exit;
|
||||
}
|
||||
|
||||
http_response_code(200);
|
||||
echo json_encode(['success' => true, 'newIndexes' => $one_based_indexes]);
|
||||
exit;
|
||||
@@ -21,12 +21,12 @@ check_user_permission('wk_leitung');
|
||||
verify_csrf();
|
||||
|
||||
// Validate input
|
||||
if (!isset($_POST['abt']) || !ctype_digit($_POST['abt'])) {
|
||||
if (!isset($_POST['abtId']) || !ctype_digit($_POST['abtId'])) {
|
||||
http_response_code(406);
|
||||
exit;
|
||||
}
|
||||
|
||||
$abtInput = (int) $_POST['abt'];
|
||||
$abtInputId = (int) $_POST['abtId'];
|
||||
|
||||
$type = 'wkl';
|
||||
|
||||
@@ -40,34 +40,27 @@ if ($dbconnection['success'] !== true){
|
||||
require $baseDir . '/../scripts/db/db-functions.php';
|
||||
require $baseDir . '/../scripts/db/db-tables.php';
|
||||
|
||||
// Fetch existing Abteilungen ordered by name
|
||||
$abts = db_select($mysqli, $tableAbt, '*', '', [], 'name ASC');
|
||||
// Fetch existing Abteilungen ordered by order_index
|
||||
$abts = db_select($mysqli, $tableAbt, '*', '', [], 'order_index ASC');
|
||||
|
||||
$deleteId = null;
|
||||
$deleteIndex = null;
|
||||
$indexed_abts = array_column($abts, null, 'id');
|
||||
|
||||
foreach ($abts as $ind => $abt) {
|
||||
if ((int)$abt['name'] === (int)$abtInput) {
|
||||
$deleteId = (int)$abt['id'];
|
||||
$deleteIndex = $ind;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$deleteIndex = $indexed_abts[$abtInputId]['order_index'] ?? null;
|
||||
|
||||
if ($deleteId === null) {
|
||||
if ($deleteIndex === null) {
|
||||
http_response_code(406);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Delete selected row
|
||||
db_delete($mysqli, $tableAbt, ['id' => $deleteId]);
|
||||
db_delete($mysqli, $tableAbt, ['id' => $abtInputId]);
|
||||
|
||||
// Reindex subsequent rows
|
||||
for ($i = $deleteIndex + 1; $i < count($abts); $i++) {
|
||||
db_update(
|
||||
$mysqli,
|
||||
$tableAbt,
|
||||
['name' => (int)$abts[$i]['name'] - 1],
|
||||
['order_index' => (int)$abts[$i]['order_index'] - 1],
|
||||
['id' => (int)$abts[$i]['id']]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
<?php
|
||||
|
||||
if (!isset($baseDir)) {
|
||||
$baseDir = $_SERVER['DOCUMENT_ROOT'];
|
||||
}
|
||||
|
||||
require_once $baseDir . '/../scripts/session_functions.php';
|
||||
|
||||
ini_wkvs_session();
|
||||
|
||||
check_user_permission('wk_leitung');
|
||||
|
||||
verify_csrf();
|
||||
|
||||
|
||||
$type = 'wkl';
|
||||
|
||||
$dbconnection = require $baseDir . '/../scripts/db/db-verbindung-script.php';
|
||||
|
||||
if ($dbconnection['success'] !== true){
|
||||
echo 'Critical DB Error.';
|
||||
exit;
|
||||
}
|
||||
|
||||
require $baseDir . '/../scripts/db/db-tables.php';
|
||||
|
||||
$input = $_POST;
|
||||
|
||||
if (!$input || !isset($input['new'])) {
|
||||
http_response_code(400);
|
||||
exit;
|
||||
}
|
||||
|
||||
$mysqli->begin_transaction();
|
||||
|
||||
try {
|
||||
$stmt = $mysqli->prepare("
|
||||
UPDATE $tableTurnerinnenAbt
|
||||
SET turnerin_index = ?
|
||||
WHERE turnerin_id = ?
|
||||
");
|
||||
|
||||
foreach (['new', 'old'] as $bucket) {
|
||||
if (empty($input[$bucket])) continue;
|
||||
|
||||
foreach ($input[$bucket] as $row) {
|
||||
$stmt->bind_param("ii", $row['order_index'], $row['id']);
|
||||
$stmt->execute();
|
||||
}
|
||||
}
|
||||
|
||||
$mysqli->commit();
|
||||
http_response_code(200);
|
||||
|
||||
} catch (Throwable $e) {
|
||||
$mysqli->rollback();
|
||||
http_response_code(500);
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
<?php
|
||||
|
||||
ini_set("display_errors", 1);
|
||||
ini_set("display_startup_errors", 1);
|
||||
|
||||
if (!isset($baseDir)) {
|
||||
$baseDir = $_SERVER['DOCUMENT_ROOT'];
|
||||
$baseDir = $_SERVER['DOCUMENT_ROOT'];
|
||||
}
|
||||
|
||||
require_once $baseDir . '/../scripts/session_functions.php';
|
||||
@@ -16,60 +19,143 @@ $type = 'wkl';
|
||||
|
||||
$dbconnection = require $baseDir . '/../scripts/db/db-verbindung-script.php';
|
||||
|
||||
if ($dbconnection['success'] !== true){
|
||||
if ($dbconnection['success'] !== true) {
|
||||
echo 'Critical DB Error.';
|
||||
exit;
|
||||
}
|
||||
|
||||
require $baseDir . '/../scripts/db/db-tables.php';
|
||||
require $baseDir . '/../scripts/db/db-functions.php';
|
||||
|
||||
$turnerinId = (int)$_POST['turnerin_id'];
|
||||
$abteilung = $_POST['abteilung'];
|
||||
$geraet = $_POST['geraet'];
|
||||
|
||||
// Default to NULL if frontend sends "null"
|
||||
$abtId = null;
|
||||
$geraetId = null;
|
||||
$turnerin_ids = $_POST['turnerin_ids'] ?? [];
|
||||
$neu_abteilung_id = intval($_POST['neuAbteilungId'] ?? 0);
|
||||
$neu_geraet_id = intval($_POST['neuGeraetId'] ?? 0);
|
||||
$alt_abteilung_id = intval($_POST['altAbteilungId'] ?? 0);
|
||||
$alt_geraet_id = intval($_POST['altGeraetId'] ?? 0);
|
||||
$neu_first_index = intval($_POST['neuFirstIndex'] ?? 0);
|
||||
|
||||
// Resolve Abteilung ID
|
||||
if ($abteilung !== 'null') {
|
||||
$stmt = $mysqli->prepare("SELECT id FROM $tableAbt WHERE name = ?");
|
||||
$stmt->bind_param("s", $abteilung);
|
||||
if ($neu_abteilung_id < 1) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'Keine Abteilungs ID angegeben.']);
|
||||
exit;
|
||||
} else {
|
||||
$stmt = $mysqli->prepare("SELECT 1 FROM $tableAbt WHERE id = ?");
|
||||
$stmt->bind_param("i", $neu_abteilung_id);
|
||||
$stmt->execute();
|
||||
if ($row = $stmt->get_result()->fetch_assoc()) {
|
||||
$abtId = (int)$row['id'];
|
||||
if (!$stmt->get_result()->fetch_assoc()) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'Invalide Abteilungs ID angegeben.']);
|
||||
exit;
|
||||
}
|
||||
$stmt->close();
|
||||
}
|
||||
|
||||
// Resolve Gerät ID
|
||||
if ($geraet !== 'null') {
|
||||
$stmt = $mysqli->prepare("SELECT id FROM $tableGeraete WHERE name = ?");
|
||||
$stmt->bind_param("s", $geraet);
|
||||
if ($neu_geraet_id < 1) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'Keine neue Geräte ID angegeben.']);
|
||||
exit;
|
||||
} else {
|
||||
$stmt = $mysqli->prepare("SELECT 1 FROM $tableGeraete WHERE id = ?");
|
||||
$stmt->bind_param("i", $neu_geraet_id);
|
||||
$stmt->execute();
|
||||
if ($row = $stmt->get_result()->fetch_assoc()) {
|
||||
$geraetId = (int)$row['id'];
|
||||
if (!$stmt->get_result()->fetch_assoc()) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'Invalide Geräte ID angegeben.']);
|
||||
exit;
|
||||
}
|
||||
$stmt->close();
|
||||
}
|
||||
|
||||
// Upsert into turnerinnen_abt (turnerin_id is UNIQUE)
|
||||
$stmt = $mysqli->prepare("
|
||||
INSERT INTO $tableTurnerinnenAbt (turnerin_id, abteilung_id, geraet_id)
|
||||
VALUES (?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
abteilung_id = VALUES(abteilung_id),
|
||||
geraet_id = VALUES(geraet_id)
|
||||
");
|
||||
$stmt->bind_param("iii", $turnerinId, $abtId, $geraetId);
|
||||
$stmt->execute();
|
||||
$stmt->close();
|
||||
function arrayshift($v) {
|
||||
return intval($v) + 1;
|
||||
}
|
||||
|
||||
// Return JSON
|
||||
http_response_code(200);
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'turnerin_id' => $turnerinId,
|
||||
'abteilung_id' => $abtId,
|
||||
'geraet_id' => $geraetId
|
||||
]);
|
||||
$array_ids = array_map('intval', $turnerin_ids);
|
||||
|
||||
$mysqli->begin_transaction();
|
||||
|
||||
try {
|
||||
// 1. Move/Upsert the specific gymnasts into the new group
|
||||
$stmt = $mysqli->prepare("
|
||||
INSERT INTO $tableTeilnehmendeAbt (turnerin_id, abteilung_id, geraet_id)
|
||||
VALUES (?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE abteilung_id = VALUES(abteilung_id), geraet_id = VALUES(geraet_id)
|
||||
");
|
||||
|
||||
foreach ($array_ids as $id) {
|
||||
$stmt->bind_param("iii", $id, $neu_abteilung_id, $neu_geraet_id);
|
||||
$stmt->execute();
|
||||
}
|
||||
$stmt->close();
|
||||
|
||||
// 2. Reorder the old group to fill any gaps left by the moved gymnasts
|
||||
|
||||
if ($alt_abteilung_id !== 0 && $alt_geraet_id !== 0 && ($alt_abteilung_id !== $neu_abteilung_id || $alt_geraet_id !== $neu_geraet_id)) {
|
||||
$old_group_rows_zero_based = db_select($mysqli, $tableTeilnehmendeAbt, "turnerin_id",
|
||||
"abteilung_id = ? AND geraet_id = ?", [$alt_abteilung_id, $alt_geraet_id], "turnerin_index ASC");
|
||||
|
||||
$updOld = $mysqli->prepare("UPDATE $tableTeilnehmendeAbt SET `turnerin_index` = ? WHERE `turnerin_id` = ?");
|
||||
|
||||
$old_group_rows = [];
|
||||
|
||||
foreach ($old_group_rows_zero_based as $i => $row) {
|
||||
$new_idx = $i + 1; // Start indexing at 1
|
||||
$updOld->bind_param("ii", $new_idx, $row['turnerin_id']);
|
||||
$updOld->execute();
|
||||
$old_group_rows[$row['turnerin_id']] = $new_idx;
|
||||
}
|
||||
$updOld->close();
|
||||
}
|
||||
|
||||
// 3. Fetch all gymnasts now in this group to determine final order
|
||||
// Order by start_index so we preserve the existing relative order of gymnasts already there
|
||||
$rows = db_select(
|
||||
$mysqli,
|
||||
$tableTeilnehmendeAbt,
|
||||
"turnerin_id",
|
||||
"abteilung_id = ? AND geraet_id = ?",
|
||||
[$neu_abteilung_id, $neu_geraet_id],
|
||||
"turnerin_index ASC"
|
||||
);
|
||||
|
||||
$current_ids = array_column($rows, 'turnerin_id');
|
||||
|
||||
// 4. Reconstruct the order: [Remaining ones before index] -> [Prioritized] -> [Remaining ones after index]
|
||||
$remaining_ids = array_values(array_diff($current_ids, $array_ids));
|
||||
|
||||
$final_order_zero_based = [];
|
||||
// Adjust index to 0-based for array manipulation
|
||||
$target_pos = max(0, $neu_first_index - 1);
|
||||
|
||||
// Splice the prioritized IDs into the remaining list at the target position
|
||||
array_splice($remaining_ids, $target_pos, 0, $array_ids);
|
||||
$final_order_zero_based = $remaining_ids;
|
||||
|
||||
$final_order = [];
|
||||
|
||||
// 5. Update the database with the new continuous indices
|
||||
$updStmt = $mysqli->prepare("UPDATE $tableTeilnehmendeAbt SET `turnerin_index` = ? WHERE `turnerin_id` = ?");
|
||||
|
||||
foreach ($final_order_zero_based as $array_index => $t_id) {
|
||||
$actual_index = $array_index + 1;
|
||||
$updStmt->bind_param("ii", $actual_index, $t_id);
|
||||
$updStmt->execute();
|
||||
$final_order[$t_id] = $actual_index;
|
||||
}
|
||||
$updStmt->close();
|
||||
|
||||
$mysqli->commit();
|
||||
http_response_code(200);
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'old_new_start_indexes' => $old_group_rows ?? null,
|
||||
'new_new_start_indexes' => $final_order
|
||||
]);
|
||||
|
||||
} catch (Throwable $e) {
|
||||
$mysqli->rollback();
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false]);
|
||||
}
|
||||
Reference in New Issue
Block a user