Files
WKVS/www/intern/scripts/riegeneinteilung/ajax_delete_specific_abt.php
2026-04-12 21:25:44 +02:00

75 lines
1.8 KiB
PHP

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
// Show all errors except deprecation notices (these come from vendor libraries
// that aren't yet typed for newer PHP versions). Long-term fix: update
// dependencies to versions compatible with your PHP runtime.
error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);
if (session_status() !== PHP_SESSION_ACTIVE) session_start();
if (empty($_SESSION['access_granted_wk_leitung']) || $_SESSION['access_granted_wk_leitung'] !== true || empty($_SESSION['passcodewk_leitung_id']) || intval($_SESSION['passcodewk_leitung_id']) < 0 ) {
http_response_code(403);
exit;
}
// Validate input
if (!isset($_POST['abt']) || !ctype_digit($_POST['abt'])) {
http_response_code(406);
exit;
}
$abtInput = (int) $_POST['abt'];
if (!isset($baseDir)) {
$baseDir = $_SERVER['DOCUMENT_ROOT'];
}
$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';
// Fetch existing Abteilungen ordered by name
$abts = db_select($mysqli, $tableAbt, '*', '', [], 'name ASC');
$deleteId = null;
$deleteIndex = null;
foreach ($abts as $ind => $abt) {
if ((int)$abt['name'] === (int)$abtInput) {
$deleteId = (int)$abt['id'];
$deleteIndex = $ind;
break;
}
}
if ($deleteId === null) {
http_response_code(406);
exit;
}
// Delete selected row
db_delete($mysqli, $tableAbt, ['id' => $deleteId]);
// Reindex subsequent rows
for ($i = $deleteIndex + 1; $i < count($abts); $i++) {
db_update(
$mysqli,
$tableAbt,
['name' => (int)$abts[$i]['name'] - 1],
['id' => (int)$abts[$i]['id']]
);
}
http_response_code(201);