Add first-day inheritance for occurrence days
This commit is contained in:
1 parent
4eef9d9785
commit
20f130f614
3 files changed
+58
-7
No files matched your search
@@ -15,6 +15,11 @@ const MARKET_MANAGEMENT_STATUSES = [
|
||||
'completed',
|
||||
];
|
||||
|
||||
const MARKET_OCCURRENCE_DAY_INHERITED_FIELDS = [
|
||||
'stand_start_time', 'stand_end_time', 'setup_start_time', 'setup_end_time',
|
||||
'delivery_start_time', 'delivery_end_time', 'observations',
|
||||
];
|
||||
|
||||
function market_management_require_positive_id(mixed $value, string $field): int
|
||||
{
|
||||
if (!is_int($value) && !ctype_digit((string) $value)) {
|
||||
@@ -533,6 +538,16 @@ function market_management_get_occurrence(PDO $pdo, int $occurrenceId, bool $inc
|
||||
);
|
||||
$days->execute([':occurrence_id' => $occurrenceId]);
|
||||
$occurrence['days'] = $days->fetchAll();
|
||||
if (count($occurrence['days']) > 1) {
|
||||
$firstDay = $occurrence['days'][0];
|
||||
foreach ($occurrence['days'] as $index => $day) {
|
||||
if ($index > 0 && (bool) ($day['inherit_from_first_day'] ?? false)) {
|
||||
foreach (MARKET_OCCURRENCE_DAY_INHERITED_FIELDS as $field) {
|
||||
$occurrence['days'][$index][$field] = $firstDay[$field] ?? null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$organizer = $pdo->prepare('SELECT * FROM market_occurrence_organizer WHERE occurrence_id = :occurrence_id');
|
||||
$organizer->execute([':occurrence_id' => $occurrenceId]);
|
||||
$organizer = $organizer->fetch();
|
||||
@@ -690,6 +705,7 @@ function market_management_create_day(PDO $pdo, array $input): array
|
||||
function market_management_update_day(PDO $pdo, int $dayId, array $input): array
|
||||
{
|
||||
$definitions = [
|
||||
'inherit_from_first_day' => 'bool',
|
||||
'date' => 'string', 'stand_start_time' => 'string', 'stand_end_time' => 'string',
|
||||
'setup_start_time' => 'string', 'setup_end_time' => 'string', 'delivery_start_time' => 'string',
|
||||
'delivery_end_time' => 'string', 'observations' => 'string',
|
||||
@@ -698,14 +714,42 @@ function market_management_update_day(PDO $pdo, int $dayId, array $input): array
|
||||
if ($fields === []) {
|
||||
throw new InvalidArgumentException('Mindestens ein änderbares Feld ist erforderlich.');
|
||||
}
|
||||
$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)) {
|
||||
throw new RuntimeException('Durchführungstag nicht gefunden.');
|
||||
}
|
||||
$occurrenceId = (int) $currentDay['occurrence_id'];
|
||||
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, $occurrenceId, (string) $input['date'], $dayId);
|
||||
}
|
||||
if (array_key_exists('inherit_from_first_day', $input) && $params[':inherit_from_first_day']) {
|
||||
$firstStmt = $pdo->prepare(
|
||||
'SELECT id FROM market_occurrence_day
|
||||
WHERE occurrence_id = :occurrence_id ORDER BY date ASC, id ASC LIMIT 1'
|
||||
);
|
||||
$firstStmt->execute([':occurrence_id' => $occurrenceId]);
|
||||
if ((int) $firstStmt->fetchColumn() === $dayId) {
|
||||
throw new InvalidArgumentException('Tag 1 kann nicht von sich selbst erben.');
|
||||
}
|
||||
}
|
||||
if (array_key_exists('inherit_from_first_day', $input) && !$params[':inherit_from_first_day']) {
|
||||
$sourceStmt = $pdo->prepare(
|
||||
'SELECT ' . implode(', ', MARKET_OCCURRENCE_DAY_INHERITED_FIELDS) . '
|
||||
FROM market_occurrence_day
|
||||
WHERE occurrence_id = :occurrence_id AND id <> :day_id
|
||||
ORDER BY date ASC, id ASC LIMIT 1'
|
||||
);
|
||||
$sourceStmt->execute([':occurrence_id' => (int) $occurrenceId, ':day_id' => $dayId]);
|
||||
$sourceDay = $sourceStmt->fetch();
|
||||
if (!is_array($sourceDay)) {
|
||||
throw new InvalidArgumentException('Für die Übernahme muss Tag 1 vorhanden sein.');
|
||||
}
|
||||
foreach (MARKET_OCCURRENCE_DAY_INHERITED_FIELDS as $field) {
|
||||
$fields[] = $field . ' = :' . $field;
|
||||
$params[':' . $field] = $sourceDay[$field] ?? null;
|
||||
}
|
||||
$params[':date'] = market_management_validate_day_date($pdo, (int) $occurrenceId, (string) $input['date'], $dayId);
|
||||
}
|
||||
$fields[] = 'updated_at = NOW()';
|
||||
$params[':id'] = $dayId;
|
||||
|
||||
Reference in new issue
Block a user