| Server IP : 193.86.120.172 / Your IP : 216.73.216.67 Web Server : Apache/2.4.63 (Unix) System : Linux JServices 3.10.108 #86003 SMP Wed Oct 22 13:20:46 CST 2025 x86_64 User : kubec ( 1026) PHP Version : 8.2.28 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /volume1/web/kadernictvi_jservices_cz/api/ |
Upload File : |
<?php
// ============================================
// api/sluzby.php
// ============================================
// Zachytit všechny chyby a vrátit jako JSON
set_error_handler(function($errno, $errstr, $errfile, $errline) {
header('Content-Type: application/json');
echo json_encode(['ok' => false, 'error' => "PHP Error $errno: $errstr in $errfile line $errline"]);
exit;
});
set_exception_handler(function($e) {
header('Content-Type: application/json');
echo json_encode(['ok' => false, 'error' => "Exception: " . $e->getMessage() . " in " . $e->getFile() . " line " . $e->getLine()]);
exit;
});
// Veřejný GET
if ($method === 'GET') {
$stmt = db()->query("SELECT id, nazev, popis, trvani_min, cena FROM sluzby WHERE aktivni = 1 ORDER BY poradi, id");
$rows = $stmt->fetchAll();
$out = array();
foreach ($rows as $row) {
$row['cena'] = ($row['cena'] !== null && $row['cena'] !== '') ? (float)$row['cena'] : null;
$out[] = $row;
}
json_response(array('ok' => true, 'data' => $out));
}
admin_required();
if ($method === 'PUT' && ($parts[1] ?? '') === 'poradi') {
$poradi = isset($input['poradi']) ? $input['poradi'] : array();
if (!is_array($poradi)) json_error('Neplatná data.');
$stmt = db()->prepare("UPDATE sluzby SET poradi = ? WHERE id = ?");
foreach ($poradi as $item) {
if (isset($item['id'], $item['poradi'])) {
$stmt->execute(array((int)$item['poradi'], (int)$item['id']));
}
}
json_response(array('ok' => true));
}
if ($method === 'POST') {
$nazev = sanitize(isset($input['nazev']) ? $input['nazev'] : '');
$popis = sanitize(isset($input['popis']) ? $input['popis'] : '');
$trvani_min = (int)(isset($input['trvani_min']) ? $input['trvani_min'] : 0);
$poradi = (int)(isset($input['poradi']) ? $input['poradi'] : 0);
$cena_raw = isset($input['cena']) ? $input['cena'] : null;
$cena = ($cena_raw !== null && $cena_raw !== '') ? (float)$cena_raw : null;
if (!$nazev || $trvani_min < 1) json_error('Vyplňte název a trvání.');
// Použij NULL string pro SQL pokud je cena null
if ($cena === null) {
$stmt = db()->prepare("INSERT INTO sluzby (nazev, popis, trvani_min, cena, poradi) VALUES (?, ?, ?, NULL, ?)");
$stmt->execute(array($nazev, $popis, $trvani_min, $poradi));
} else {
$stmt = db()->prepare("INSERT INTO sluzby (nazev, popis, trvani_min, cena, poradi) VALUES (?, ?, ?, ?, ?)");
$stmt->execute(array($nazev, $popis, $trvani_min, $cena, $poradi));
}
json_response(array('ok' => true, 'id' => db()->lastInsertId()), 201);
}
if ($method === 'PUT' && $id) {
$nazev = sanitize(isset($input['nazev']) ? $input['nazev'] : '');
$popis = sanitize(isset($input['popis']) ? $input['popis'] : '');
$trvani_min = (int)(isset($input['trvani_min']) ? $input['trvani_min'] : 0);
$aktivni = isset($input['aktivni']) ? (int)(bool)$input['aktivni'] : 1;
$poradi = (int)(isset($input['poradi']) ? $input['poradi'] : 0);
$cena_raw = isset($input['cena']) ? $input['cena'] : null;
$cena = ($cena_raw !== null && $cena_raw !== '') ? (float)$cena_raw : null;
if (!$nazev || $trvani_min < 1) json_error('Vyplňte název a trvání.');
// Použij NULL přímo v SQL pokud je cena null
if ($cena === null) {
$stmt = db()->prepare("UPDATE sluzby SET nazev=?, popis=?, trvani_min=?, cena=NULL, aktivni=?, poradi=? WHERE id=?");
$stmt->execute(array($nazev, $popis, $trvani_min, $aktivni, $poradi, $id));
} else {
$stmt = db()->prepare("UPDATE sluzby SET nazev=?, popis=?, trvani_min=?, cena=?, aktivni=?, poradi=? WHERE id=?");
$stmt->execute(array($nazev, $popis, $trvani_min, $cena, $aktivni, $poradi, $id));
}
json_response(array('ok' => true));
}
if ($method === 'DELETE' && $id) {
db()->prepare("UPDATE sluzby SET aktivni = 0 WHERE id = ?")->execute(array($id));
json_response(array('ok' => true));
}
json_error('Metoda není podporována.', 405);