QM-Texterkennung: Imagick nur mit Ghostscript als verfügbar melden
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Failing after 3m18s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Failing after 3m27s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Failing after 3m0s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Failing after 2m56s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 4m4s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 4m1s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 3m41s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 3m39s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m10s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-26 16:38:17 +00:00
parent 5a360d3cc7
commit b50d089946
117 changed files with 1747 additions and 2 deletions

View file

@ -0,0 +1,176 @@
<?php
namespace Inhaus\Plugins\Speiseplan\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Support\Audit;
use App\Support\Settings;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Inertia\Inertia;
use Inertia\Response;
use Inhaus\Plugins\Speiseplan\Models\Week;
use Inhaus\Plugins\Speiseplan\SpeiseplanPlugin;
use Symfony\Component\HttpFoundation\Response as HttpResponse;
class SpeiseplanController extends Controller
{
private const ALLERGENS = 'speiseplan.allergens';
public function __construct(private readonly Settings $settings) {}
public function index(Request $request): Response
{
$monday = Week::mondayOf(now());
$weeks = Week::query()->whereIn('week_start', [$monday->toDateString(), $monday->copy()->addWeek()->toDateString()])->get()->keyBy(fn (Week $w) => $w->week_start->toDateString());
$canManage = (bool) $request->user()->can('speiseplan.manage');
$slot = fn ($start, string $title) => [
'title' => $title,
'label' => 'KW '.$start->isoWeek().' · '.$start->format('d.m.').' – '.$start->copy()->addDays(4)->format('d.m.Y'),
'week_start' => $start->toDateString(),
'plan' => ($w = $weeks->get($start->toDateString())) ? $w->present() : null,
];
return Inertia::render('speiseplan::Index', [
'weeks' => [$slot($monday, 'Diese Woche'), $slot($monday->copy()->addWeek(), 'Nächste Woche')],
'allergens' => $this->allergens(),
'canManage' => $canManage,
'manage' => $canManage ? $this->manageData($monday) : null,
]);
}
/**
* Druckansicht: jeder Plan eine A4-Seite, randlos.
*/
public function print(Request $request): Response
{
$ids = array_filter(array_map('intval', explode(',', (string) $request->query('wochen', ''))));
$monday = Week::mondayOf(now());
$query = Week::query()->orderBy('week_start');
$ids ? $query->whereIn('id', $ids) : $query->whereIn('week_start', [$monday->toDateString(), $monday->copy()->addWeek()->toDateString()]);
return Inertia::render('speiseplan::Print', ['plans' => $query->get()->map(fn (Week $w) => $w->present())->values()]);
}
public function image(Week $week): HttpResponse
{
return Storage::disk(Week::DISK)->response($week->path, 'speiseplan-kw'.$week->week_start->isoWeek().'.'.pathinfo($week->path, PATHINFO_EXTENSION), [
'Cache-Control' => 'private, max-age=86400',
'X-Content-Type-Options' => 'nosniff',
]);
}
private function allergens(): ?array
{
$file = $this->settings->get(self::ALLERGENS);
return is_array($file) && ! empty($file['path']) ? ['name' => $file['name'], 'url' => route('speiseplan.allergens.show')] : null;
}
public function store(Request $request): RedirectResponse
{
$this->authorizeManage($request);
$data = $request->validate([
'week' => ['required', 'date'],
'image' => ['required', 'file', 'max:20480', 'mimes:jpg,jpeg,png,webp'],
], ['image.mimes' => 'Bitte ein Bild (JPG, PNG, WebP) oder ein PDF hochladen.'], ['week' => 'Woche', 'image' => 'Speiseplan']);
$monday = Week::mondayOf($data['week']);
$file = $request->file('image');
[$width, $height] = @getimagesize($file->getRealPath()) ?: [null, null];
$path = $file->storeAs('speiseplan', $monday->toDateString().'-'.Str::random(12).'.'.strtolower($file->extension() ?: 'jpg'), Week::DISK);
$existing = Week::query()->where('week_start', $monday->toDateString())->first();
$old = $existing?->path;
Week::query()->updateOrCreate(['week_start' => $monday->toDateString()], [
'path' => $path, 'original_name' => $file->getClientOriginalName(), 'width' => $width, 'height' => $height, 'uploaded_by' => $request->user()->id,
]);
if ($old) {
Storage::disk(Week::DISK)->delete($old);
}
Audit::log('speiseplan.uploaded', ['week' => $monday->toDateString()]);
return back()->with('status', 'Speiseplan für KW '.$monday->isoWeek().($existing ? ' ersetzt.' : ' gespeichert.'));
}
public function destroy(Request $request, Week $week): RedirectResponse
{
$this->authorizeManage($request);
$week->delete();
return back()->with('status', 'Speiseplan für KW '.$week->week_start->isoWeek().' entfernt.');
}
public function storeAllergens(Request $request): RedirectResponse
{
$this->authorizeManage($request);
$request->validate(['file' => ['required', 'file', 'max:20480', 'mimes:pdf']], [], ['file' => 'Allergie-Informationen']);
$old = $this->settings->get(self::ALLERGENS);
$path = $request->file('file')->storeAs('speiseplan', 'allergene-'.Str::random(12).'.pdf', Week::DISK);
$this->settings->set(self::ALLERGENS, ['path' => $path, 'name' => $request->file('file')->getClientOriginalName()]);
if (is_array($old) && ! empty($old['path'])) {
Storage::disk(Week::DISK)->delete($old['path']);
}
return back()->with('status', 'Allergie-Informationen gespeichert.');
}
public function destroyAllergens(Request $request): RedirectResponse
{
$this->authorizeManage($request);
$old = $this->settings->get(self::ALLERGENS);
if (is_array($old) && ! empty($old['path'])) {
Storage::disk(Week::DISK)->delete($old['path']);
}
$this->settings->set(self::ALLERGENS, null);
return back()->with('status', 'Allergie-Informationen entfernt.');
}
public function allergensFile(): HttpResponse
{
$file = $this->settings->get(self::ALLERGENS);
abort_unless(is_array($file) && ! empty($file['path']) && Storage::disk(Week::DISK)->exists($file['path']), 404);
return Storage::disk(Week::DISK)->response($file['path'], $file['name'] ?? 'allergene.pdf', ['Content-Type' => 'application/pdf', 'X-Content-Type-Options' => 'nosniff']);
}
/**
* @return array<string, mixed>
*/
private function manageData($monday): array
{
$uploaded = Week::query()->where('week_start', '>=', $monday->copy()->subWeeks(SpeiseplanPlugin::KEEP_WEEKS)->toDateString())->orderBy('week_start')->get()->keyBy(fn (Week $w) => $w->week_start->toDateString());
$options = [];
for ($i = 0; $i < 12; $i++) {
$start = $monday->copy()->addWeeks($i);
$options[] = [
'value' => $start->toDateString(),
'label' => 'KW '.$start->isoWeek().' · '.$start->format('d.m.').' – '.$start->copy()->addDays(4)->format('d.m.Y').($uploaded->has($start->toDateString()) ? ' (vorhanden)' : ''),
'uploaded' => $uploaded->has($start->toDateString()),
];
}
return [
'options' => $options,
'suggested' => collect($options)->firstWhere('uploaded', false)['value'] ?? $options[0]['value'],
'uploaded' => $uploaded->values()->map(fn (Week $w) => $w->present() + ['past' => $w->week_start->lt($monday)])->all(),
'keepWeeks' => SpeiseplanPlugin::KEEP_WEEKS,
];
}
private function authorizeManage(Request $request): void
{
abort_unless($request->user()->can('speiseplan.manage'), 403);
}
}