$definition * @return list}> */ public function affected(InquiryTemplate $template, ?array $definition = null): array { $result = []; foreach ($template->entries()->whereNull('archived_at')->orderBy('name')->get() as $entry) { $problems = $definition !== null && $entry->published_config !== null ? $this->factory->problems($definition, (array) $entry->published_config) : []; $result[] = ['entry' => $entry, 'problems' => $problems]; } return $result; } /** * Veröffentlicht den Entwurf der Vorlage als neue Fassung. * * @throws ValidationException */ public function publishTemplate(InquiryTemplate $template, ?User $by = null, ?string $note = null): InquiryTemplateVersion { $compiled = $this->compiler->compile((array) $template->draft); if ($compiled['definition'] === null) { throw ValidationException::withMessages(['draft' => $compiled['errors']]); } $blocking = []; foreach ($this->affected($template, $compiled['definition']) as $row) { if ($row['entry']->published_config !== null && $row['problems'] !== []) { $blocking[] = 'Der veröffentlichte Einstieg „'.$row['entry']->name.'“ passt nicht zur neuen Fassung: '.implode(' ', $row['problems']); } } if ($blocking !== []) { throw ValidationException::withMessages(['draft' => $blocking]); } return DB::transaction(function () use ($template, $compiled, $by, $note): InquiryTemplateVersion { $template = InquiryTemplate::query()->lockForUpdate()->findOrFail($template->id); $next = (int) InquiryTemplateVersion::query()->where('template_id', $template->id)->max('version') + 1; $version = InquiryTemplateVersion::query()->create([ 'template_id' => $template->id, 'version' => $next, 'definition' => $compiled['definition'], 'source' => (array) $template->draft, 'note' => $note !== null && trim($note) !== '' ? mb_substr(trim($note), 0, 300) : null, 'published_by' => $by?->id, 'published_at' => now(), ]); $template->forceFill(['published_version_id' => $version->id])->save(); return $version; }); } /** * Setzt eine frühere Fassung wieder in Kraft (Rückkehr zu einer gültigen Konfiguration). Betroffene Einstiege * müssen zur Fassung passen, sonst wird abgelehnt. * * @throws ValidationException */ public function restoreVersion(InquiryTemplate $template, InquiryTemplateVersion $version): void { abort_unless($version->template_id === $template->id, 404); $blocking = []; foreach ($this->affected($template, (array) $version->definition) as $row) { if ($row['entry']->published_config !== null && $row['problems'] !== []) { $blocking[] = 'Der veröffentlichte Einstieg „'.$row['entry']->name.'“ passt nicht zu dieser Fassung: '.implode(' ', $row['problems']); } } if ($blocking !== []) { throw ValidationException::withMessages(['draft' => $blocking]); } $template->forceFill(['published_version_id' => $version->id])->save(); } /** * Veröffentlicht die Konfiguration eines Einstiegs. * * @throws ValidationException */ public function publishEntry(InquiryEntry $entry): void { $template = $entry->template()->with('publishedVersion')->firstOrFail(); if ($template->publishedVersion === null) { throw ValidationException::withMessages(['config' => ['Die Anfragevorlage „'.$template->name.'“ ist noch nicht veröffentlicht. Ein Einstieg kann nur eine veröffentlichte Vorlage verwenden.']]); } if ($template->archived_at !== null) { throw ValidationException::withMessages(['config' => ['Die Anfragevorlage „'.$template->name.'“ ist archiviert.']]); } $problems = $this->factory->problems((array) $template->publishedVersion->definition, (array) $entry->config); if ($problems !== []) { throw ValidationException::withMessages(['config' => $problems]); } $entry->forceFill([ 'published_config' => $entry->config, 'published_at' => $entry->published_at ?? now(), 'archived_at' => null, ])->save(); } /** Nimmt einen Einstieg aus dem öffentlichen Angebot; die Konfiguration bleibt erhalten. */ public function withdrawEntry(InquiryEntry $entry): void { $entry->forceFill(['published_config' => null, 'published_at' => null])->save(); } }