Validate chronological market occurrence days

This commit is contained in:
gitea_admin committed 2026-09-19 16:38:32 +02:00
1 parent 361635332c
commit 4eef9d9785
2 files changed
+61 -6

No files matched your search

+59 -4
View File
@@ -612,16 +612,62 @@ function market_management_update_occurrence(PDO $pdo, int $occurrenceId, array
return $occurrence;
}
function market_management_validate_day_date(PDO $pdo, int $occurrenceId, string $date, ?int $dayId = null): string
{
$value = trim($date);
$parsed = DateTimeImmutable::createFromFormat('!Y-m-d', $value);
if ($value === '' || $parsed === false || $parsed->format('Y-m-d') !== $value) {
throw new InvalidArgumentException('date muss ein gültiges Datum im Format YYYY-MM-DD sein.');
}
$previousDate = null;
$nextDate = null;
if ($dayId !== null) {
$currentStmt = $pdo->prepare(
'SELECT occurrence_id, date FROM market_occurrence_day WHERE id = :id'
);
$currentStmt->execute([':id' => $dayId]);
$currentDay = $currentStmt->fetch();
if (!is_array($currentDay) || (int) $currentDay['occurrence_id'] !== $occurrenceId) {
throw new RuntimeException('Durchführungstag nicht gefunden.');
}
$neighborParams = [':occurrence_id' => $occurrenceId, ':day_id' => $dayId, ':current_date' => $currentDay['date']];
$previousStmt = $pdo->prepare(
'SELECT MAX(date) FROM market_occurrence_day
WHERE occurrence_id = :occurrence_id AND id <> :day_id AND date < :current_date'
);
$previousStmt->execute($neighborParams);
$previousDate = $previousStmt->fetchColumn() ?: null;
$nextStmt = $pdo->prepare(
'SELECT MIN(date) FROM market_occurrence_day
WHERE occurrence_id = :occurrence_id AND id <> :day_id AND date > :current_date'
);
$nextStmt->execute($neighborParams);
$nextDate = $nextStmt->fetchColumn() ?: null;
} else {
$latestStmt = $pdo->prepare(
'SELECT MAX(date) FROM market_occurrence_day WHERE occurrence_id = :occurrence_id'
);
$latestStmt->execute([':occurrence_id' => $occurrenceId]);
$previousDate = $latestStmt->fetchColumn() ?: null;
}
if ($previousDate !== null && $value <= (string) $previousDate) {
throw new InvalidArgumentException('Das Datum muss nach dem vorherigen Durchführungstag liegen.');
}
if ($nextDate !== null && $value >= (string) $nextDate) {
throw new InvalidArgumentException('Das Datum muss vor dem nachfolgenden Durchführungstag liegen.');
}
return $value;
}
function market_management_create_day(PDO $pdo, array $input): array
{
$occurrenceId = market_management_require_positive_id($input['occurrence_id'] ?? null, 'occurrence_id');
if (market_management_get_occurrence($pdo, $occurrenceId, true) === null) {
throw new InvalidArgumentException('occurrence_id verweist nicht auf eine Durchführung.');
}
$date = trim((string) ($input['date'] ?? ''));
if ($date === '') {
throw new InvalidArgumentException('date ist erforderlich.');
}
$date = market_management_validate_day_date($pdo, $occurrenceId, (string) ($input['date'] ?? ''));
$definitions = [
'stand_start_time' => 'string', 'stand_end_time' => 'string', 'setup_start_time' => 'string',
'setup_end_time' => 'string', 'delivery_start_time' => 'string', 'delivery_end_time' => 'string',
@@ -652,6 +698,15 @@ function market_management_update_day(PDO $pdo, int $dayId, array $input): array
if ($fields === []) {
throw new InvalidArgumentException('Mindestens ein änderbares Feld ist erforderlich.');
}
if (array_key_exists('date', $input)) {
$currentStmt = $pdo->prepare('SELECT occurrence_id FROM market_occurrence_day WHERE id = :id');
$currentStmt->execute([':id' => $dayId]);
$occurrenceId = $currentStmt->fetchColumn();
if ($occurrenceId === false) {
throw new RuntimeException('Durchführungstag nicht gefunden.');
}
$params[':date'] = market_management_validate_day_date($pdo, (int) $occurrenceId, (string) $input['date'], $dayId);
}
$fields[] = 'updated_at = NOW()';
$params[':id'] = $dayId;
$stmt = $pdo->prepare(