diff --git a/public/api/market-occurrence.php b/public/api/market-occurrence.php index eda42b2..0a29c63 100644 --- a/public/api/market-occurrence.php +++ b/public/api/market-occurrence.php @@ -33,12 +33,13 @@ if ($requestMethod === 'POST') { echo json_encode(['ok' => false, 'error' => 'Ungültige Anfrage']); exit; } - $scope = (string) ($payload['scope'] ?? ''); - $id = market_management_require_positive_id($payload['id'] ?? null, 'id'); - $fields = is_array($payload['fields'] ?? null) ? $payload['fields'] : []; - if ($scope === 'day') { - market_management_update_day($pdo, $id, $fields); - } elseif (in_array($scope, ['occurrence', 'status', 'overview', 'participation', 'infrastructure', 'travel', 'notes'], true)) { + try { + $scope = (string) ($payload['scope'] ?? ''); + $id = market_management_require_positive_id($payload['id'] ?? null, 'id'); + $fields = is_array($payload['fields'] ?? null) ? $payload['fields'] : []; + if ($scope === 'day') { + market_management_update_day($pdo, $id, $fields); + } elseif (in_array($scope, ['occurrence', 'status', 'overview', 'participation', 'infrastructure', 'travel', 'notes'], true)) { $marketFields = []; foreach (['market_name', 'category_id', 'website_url'] as $marketField) { if (array_key_exists($marketField, $fields)) { @@ -61,13 +62,17 @@ if ($requestMethod === 'POST') { market_management_update_market($pdo, (int) ($existingOccurrence['market_definition_id'] ?? 0), $marketFields); } if ($contactFields !== []) { - $contactId = (int) ($fields['contact_person_id'] ?? $existingOccurrence['contact_person_id'] ?? 0); + $submittedContactId = trim((string) ($fields['contact_person_id'] ?? '')); + $contactId = (int) ($submittedContactId !== '' ? $submittedContactId : ($existingOccurrence['contact_person_id'] ?? 0)); update_contact_details($pdo, $contactId, $contactFields); } $organizerFields = []; foreach (['organization_contact_id', 'contact_person_id'] as $organizerField) { if (array_key_exists($organizerField, $fields)) { - $organizerFields[$organizerField] = $fields[$organizerField]; + $value = trim((string) $fields[$organizerField]); + if ($value !== '') { + $organizerFields[$organizerField] = $value; + } unset($fields[$organizerField]); } } @@ -82,11 +87,16 @@ if ($requestMethod === 'POST') { if ($fields !== []) { market_management_update_occurrence($pdo, $id, $fields); } - } else { - throw new InvalidArgumentException('Ungültiger Bearbeitungsbereich.'); + } else { + throw new InvalidArgumentException('Ungültiger Bearbeitungsbereich.'); + } + echo json_encode(['ok' => true]); + exit; + } catch (Throwable $error) { + http_response_code(422); + echo json_encode(['ok' => false, 'error' => $error->getMessage()], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + exit; } - echo json_encode(['ok' => true]); - exit; } $occurrenceId = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);