85 lines
3.2 KiB
PHP
85 lines
3.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/../../modules/shared/db.php';
|
|
require_once __DIR__ . '/../../modules/shared/auth/service.php';
|
|
require_once __DIR__ . '/../../modules/market_management/service.php';
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
header('Cache-Control: no-store');
|
|
|
|
$env = expand_env_values(parse_env_file(__DIR__ . '/../../.env'));
|
|
$pdo = connect_database($env);
|
|
auth_bootstrap_session();
|
|
|
|
if (auth_current_user($pdo) === null) {
|
|
http_response_code(401);
|
|
echo json_encode(['ok' => false, 'error' => 'Nicht autorisiert.']);
|
|
exit;
|
|
}
|
|
|
|
if (strtoupper((string) ($_SERVER['REQUEST_METHOD'] ?? 'GET')) !== 'POST') {
|
|
http_response_code(405);
|
|
echo json_encode(['ok' => false, 'error' => 'Methode nicht erlaubt.']);
|
|
exit;
|
|
}
|
|
|
|
$csrf = (string) ($_SERVER['HTTP_X_CSRF_TOKEN'] ?? '');
|
|
if (!auth_validate_csrf_token($csrf)) {
|
|
http_response_code(419);
|
|
echo json_encode(['ok' => false, 'error' => 'Ungültiges Sicherheits-Token.']);
|
|
exit;
|
|
}
|
|
|
|
$payload = json_decode((string) file_get_contents('php://input'), true);
|
|
if (!is_array($payload)) {
|
|
http_response_code(400);
|
|
echo json_encode(['ok' => false, 'error' => 'Ungültige Anfrage.']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$fields = is_array($payload['fields'] ?? null) ? $payload['fields'] : [];
|
|
if (($fields['organizer_id'] ?? '') === '__new__') {
|
|
$organizer = market_management_create_organizer(
|
|
$pdo,
|
|
(string) ($fields['organizer_name'] ?? ''),
|
|
(string) ($fields['organizer_email'] ?? ''),
|
|
(string) ($fields['organizer_phone'] ?? '')
|
|
);
|
|
$fields['organizer_id'] = $organizer['id'];
|
|
}
|
|
if (($payload['action'] ?? 'create') === 'delete') {
|
|
$marketId = (int) ($payload['id'] ?? 0);
|
|
if ($marketId < 1) {
|
|
throw new InvalidArgumentException('id ist erforderlich.');
|
|
}
|
|
if (($payload['confirmation'] ?? '') !== 'DELETE') {
|
|
throw new InvalidArgumentException('Die Löschbestätigung muss exakt DELETE lauten.');
|
|
}
|
|
$market = market_management_set_market_archive($pdo, $marketId, true);
|
|
} elseif (($payload['action'] ?? 'create') === 'update') {
|
|
$marketId = (int) ($payload['id'] ?? 0);
|
|
if ($marketId < 1) {
|
|
throw new InvalidArgumentException('id ist erforderlich.');
|
|
}
|
|
$market = market_management_update_market($pdo, $marketId, [
|
|
'name' => $fields['name'] ?? null,
|
|
'category_id' => $fields['category_id'] ?? null,
|
|
'organizer_id' => $fields['organizer_id'] ?? null,
|
|
'website_url' => $fields['website_url'] ?? null,
|
|
]);
|
|
} else {
|
|
$market = market_management_create_market($pdo, [
|
|
'name' => $fields['name'] ?? null,
|
|
'category_id' => $fields['category_id'] ?? null,
|
|
'organizer_id' => $fields['organizer_id'] ?? null,
|
|
'website_url' => $fields['website_url'] ?? null,
|
|
]);
|
|
}
|
|
echo json_encode(['ok' => true, 'market' => $market], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
} catch (Throwable $error) {
|
|
http_response_code(422);
|
|
echo json_encode(['ok' => false, 'error' => $error->getMessage()], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
}
|