zibo-dashboard/public/userarea/save_param.php
2025-12-03 14:07:11 +01:00

103 lines
3.2 KiB
PHP

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
header('Content-Type: application/json');
include('include/headscript.php');
try {
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode(['success' => false, 'message' => 'Invalid request method.']);
exit;
}
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
$paramId = isset($_POST['param_id']) ? (int)$_POST['param_id'] : 0;
$lineId = isset($_POST['line_id']) ? (int)$_POST['line_id'] : 0;
$position = isset($_POST['position']) ? (int)$_POST['position'] : 0;
$shortLabel = isset($_POST['short_label']) ? trim($_POST['short_label']) : '';
$label = isset($_POST['label']) ? trim($_POST['label']) : '';
$icon = isset($_POST['icon']) ? trim($_POST['icon']) : '';
if ($lineId <= 0) {
echo json_encode(['success' => false, 'message' => 'Invalid line ID.']);
exit;
}
if ($position <= 0) {
echo json_encode(['success' => false, 'message' => 'Position must be greater than zero.']);
exit;
}
if ($shortLabel === '' || $label === '') {
echo json_encode(['success' => false, 'message' => 'Short label and label are required.']);
exit;
}
// Check that line exists
$stmtLine = $pdo->prepare("SELECT id FROM production_lines WHERE id = :id");
$stmtLine->execute([':id' => $lineId]);
if (!$stmtLine->fetchColumn()) {
echo json_encode(['success' => false, 'message' => 'Production line not found.']);
exit;
}
if ($paramId > 0) {
// Update
$sql = "
UPDATE production_line_params
SET line_id = :line_id,
position = :position,
short_label = :short_label,
label = :label,
icon = :icon
WHERE id = :id
";
$stmt = $pdo->prepare($sql);
$stmt->execute([
':line_id' => $lineId,
':position' => $position,
':short_label' => $shortLabel,
':label' => $label,
':icon' => ($icon !== '' ? $icon : null),
':id' => $paramId,
]);
echo json_encode([
'success' => true,
'message' => 'Parameter updated successfully.',
'id' => $paramId
]);
} else {
// Insert
$sql = "
INSERT INTO production_line_params (line_id, position, short_label, label, icon)
VALUES (:line_id, :position, :short_label, :label, :icon)
";
$stmt = $pdo->prepare($sql);
$stmt->execute([
':line_id' => $lineId,
':position' => $position,
':short_label' => $shortLabel,
':label' => $label,
':icon' => ($icon !== '' ? $icon : null),
]);
$newId = (int)$pdo->lastInsertId();
echo json_encode([
'success' => true,
'message' => 'Parameter created successfully.',
'id' => $newId
]);
}
} catch (Exception $e) {
echo json_encode([
'success' => false,
'message' => 'Server error: ' . $e->getMessage()
]);
}