Plugin-System; Neuigkeiten und Termine als mitgelieferte Plugins
- Plugins nach WordPress-Prinzip: plugin.json-Manifest mit Versions- und Abhängigkeitsprüfung, eigenes PSR-4-Autoloading, register()/boot(), activate()/deactivate()/uninstall(), Migrationen, Routen und Views je Plugin - plugins-core/ (mitgeliefert, aktiv bis zur Deaktivierung) und plugins/ (Kunden-Plugins, geschützt vor Updates, inaktiv bis zur Aktivierung) - Zustand in storage/app/plugins.json – Laden ohne Datenbankabfrage - Wiederherstellungsmodus: Ausnahmen und Fatal Errors beim Laden deaktivieren das Plugin, das Portal bleibt erreichbar - Hooks (Actions/Filter mit Prioritäten) und Registries für Navigation, Startseiten-Kacheln, Rechte und Admin-Seiten (Filament-Discovery) - Admin-Seite „Plugins“: aktivieren, deaktivieren, per ZIP installieren (ZIP-Slip-Schutz), löschen mit oder ohne Daten; abschaltbar per INHAUS_DISALLOW_FILE_MODS - Startseite komplett aus Kacheln; Plugin-Seiten als „slug::Seite“ - Version 0.3.0 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
cc0020458f
commit
28ef1e4a20
155 changed files with 2762 additions and 573 deletions
86
plugins-core/events/src/Calendar.php
Normal file
86
plugins-core/events/src/Calendar.php
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\Events;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Inhaus\Plugins\Events\Models\Event;
|
||||
|
||||
/**
|
||||
* iCalendar-Export (RFC 5545) für einzelne Termine – zum Übernehmen in Outlook & Co.
|
||||
*/
|
||||
class Calendar
|
||||
{
|
||||
public function toIcs(Event $event, string $organizer): string
|
||||
{
|
||||
$end = $event->ends_at ?? ($event->all_day ? $event->starts_at : $event->starts_at->copy()->addHour());
|
||||
|
||||
$lines = [
|
||||
'BEGIN:VCALENDAR',
|
||||
'VERSION:2.0',
|
||||
'PRODID:-//'.$this->escape($organizer).'//Inhaus//DE',
|
||||
'CALSCALE:GREGORIAN',
|
||||
'METHOD:PUBLISH',
|
||||
'BEGIN:VEVENT',
|
||||
'UID:event-'.$event->id.'@'.(parse_url(config('app.url'), PHP_URL_HOST) ?: 'inhaus'),
|
||||
'DTSTAMP:'.now()->utc()->format('Ymd\THis\Z'),
|
||||
];
|
||||
|
||||
if ($event->all_day) {
|
||||
$lines[] = 'DTSTART;VALUE=DATE:'.$event->starts_at->format('Ymd');
|
||||
// DTEND ist bei ganztägigen Terminen exklusiv: der Tag nach dem letzten Tag.
|
||||
$lines[] = 'DTEND;VALUE=DATE:'.$end->copy()->startOfDay()->addDay()->format('Ymd');
|
||||
} else {
|
||||
$lines[] = 'DTSTART:'.$event->starts_at->copy()->utc()->format('Ymd\THis\Z');
|
||||
$lines[] = 'DTEND:'.$end->copy()->utc()->format('Ymd\THis\Z');
|
||||
}
|
||||
|
||||
$lines[] = 'SUMMARY:'.$this->escape($event->title);
|
||||
|
||||
if ($event->location) {
|
||||
$lines[] = 'LOCATION:'.$this->escape($event->location);
|
||||
}
|
||||
|
||||
$description = trim(html_entity_decode(strip_tags(str_replace(['</p>', '<br>'], "\n", (string) $event->description_html))));
|
||||
|
||||
if ($description !== '') {
|
||||
$lines[] = 'DESCRIPTION:'.$this->escape(Str::limit($description, 2000));
|
||||
}
|
||||
|
||||
if ($event->online_url) {
|
||||
$lines[] = 'URL:'.$this->escape($event->online_url);
|
||||
}
|
||||
|
||||
$lines[] = 'END:VEVENT';
|
||||
$lines[] = 'END:VCALENDAR';
|
||||
|
||||
return implode("\r\n", array_map($this->fold(...), $lines))."\r\n";
|
||||
}
|
||||
|
||||
private function escape(string $value): string
|
||||
{
|
||||
return str_replace(['\\', ';', ',', "\r\n", "\n"], ['\\\\', '\;', '\\,', '\\n', '\\n'], $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Zeilen nach 75 Oktetts umbrechen (Folgezeilen beginnen mit einem Leerzeichen,
|
||||
* das mitzählt), ohne UTF-8-Zeichen zu zerschneiden.
|
||||
*/
|
||||
private function fold(string $line): string
|
||||
{
|
||||
$folded = '';
|
||||
$current = '';
|
||||
$limit = 75;
|
||||
|
||||
foreach (mb_str_split($line) as $char) {
|
||||
if (strlen($current.$char) > $limit) {
|
||||
$folded .= $current."\r\n ";
|
||||
$current = '';
|
||||
$limit = 74;
|
||||
}
|
||||
|
||||
$current .= $char;
|
||||
}
|
||||
|
||||
return $folded.$current;
|
||||
}
|
||||
}
|
||||
29
plugins-core/events/src/Database/Factories/EventFactory.php
Normal file
29
plugins-core/events/src/Database/Factories/EventFactory.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\Events\Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Inhaus\Plugins\Events\Models\Event;
|
||||
|
||||
/**
|
||||
* @extends Factory<Event>
|
||||
*/
|
||||
class EventFactory extends Factory
|
||||
{
|
||||
protected $model = Event::class;
|
||||
|
||||
public function definition(): array
|
||||
{
|
||||
$start = now()->addDays(fake()->numberBetween(1, 30))->setTime(10, 0);
|
||||
|
||||
return [
|
||||
'title' => fake()->sentence(4),
|
||||
'description' => '<p>'.fake()->paragraph().'</p>',
|
||||
'location' => fake()->city(),
|
||||
'starts_at' => $start,
|
||||
'ends_at' => $start->copy()->addHours(2),
|
||||
'all_day' => false,
|
||||
'is_published' => true,
|
||||
];
|
||||
}
|
||||
}
|
||||
24
plugins-core/events/src/EventsPlugin.php
Normal file
24
plugins-core/events/src/EventsPlugin.php
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\Events;
|
||||
|
||||
use App\Dashboard\Widgets;
|
||||
use App\Models\User;
|
||||
use App\Plugins\Plugin;
|
||||
use App\Support\Abilities;
|
||||
use App\Support\Navigation;
|
||||
use Inhaus\Plugins\Events\Http\Resources\EventResource;
|
||||
use Inhaus\Plugins\Events\Models\Event;
|
||||
|
||||
class EventsPlugin extends Plugin
|
||||
{
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->make(Navigation::class)->add('events', 'Termine', '/termine', active: 'events.*', sort: 30);
|
||||
$this->app->make(Abilities::class)->define('events.manage', 'Termine verwalten', 'Termine');
|
||||
|
||||
$this->app->make(Widgets::class)->add('events.upcoming', 'events::Upcoming', 'aside', sort: 20, props: fn (User $user) => [
|
||||
'events' => EventResource::collection(Event::query()->upcoming()->visibleTo($user)->limit(4)->get())->resolve(),
|
||||
], mobileSort: 30);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,140 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\Events\Filament\Resources\Events;
|
||||
|
||||
use BackedEnum;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\FileUpload;
|
||||
use Filament\Forms\Components\RichEditor;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\TernaryFilter;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Inhaus\Plugins\Events\Filament\Resources\Events\Pages\CreateEvent;
|
||||
use Inhaus\Plugins\Events\Filament\Resources\Events\Pages\EditEvent;
|
||||
use Inhaus\Plugins\Events\Filament\Resources\Events\Pages\ListEvents;
|
||||
use Inhaus\Plugins\Events\Models\Event;
|
||||
use UnitEnum;
|
||||
|
||||
class EventResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Event::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedCalendarDays;
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Inhalte';
|
||||
|
||||
protected static ?int $navigationSort = 3;
|
||||
|
||||
protected static ?string $modelLabel = 'Termin';
|
||||
|
||||
protected static ?string $pluralModelLabel = 'Termine';
|
||||
|
||||
protected static ?string $slug = 'termine';
|
||||
|
||||
protected static ?string $recordTitleAttribute = 'title';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->columns(3)
|
||||
->components([
|
||||
Section::make('Termin')
|
||||
->columnSpan(2)
|
||||
->columns(2)
|
||||
->schema([
|
||||
TextInput::make('title')->label('Titel')->required()->maxLength(160)->columnSpanFull(),
|
||||
Toggle::make('all_day')->label('Ganztägig')->live()->columnSpanFull(),
|
||||
DateTimePicker::make('starts_at')
|
||||
->label('Beginn')
|
||||
->required()
|
||||
->seconds(false)
|
||||
->native(false)
|
||||
->time(fn (Get $get) => ! $get('all_day'))
|
||||
->displayFormat(fn (Get $get) => $get('all_day') ? 'd.m.Y' : 'd.m.Y H:i'),
|
||||
DateTimePicker::make('ends_at')
|
||||
->label('Ende')
|
||||
->seconds(false)
|
||||
->native(false)
|
||||
->time(fn (Get $get) => ! $get('all_day'))
|
||||
->displayFormat(fn (Get $get) => $get('all_day') ? 'd.m.Y' : 'd.m.Y H:i')
|
||||
->afterOrEqual('starts_at'),
|
||||
TextInput::make('location')->label('Ort')->maxLength(255),
|
||||
TextInput::make('online_url')->label('Online-Link')->url()->maxLength(500)->placeholder('https://…'),
|
||||
RichEditor::make('description')
|
||||
->label('Beschreibung')
|
||||
->toolbarButtons([
|
||||
['bold', 'italic', 'link'],
|
||||
['bulletList', 'orderedList'],
|
||||
['undo', 'redo'],
|
||||
])
|
||||
->columnSpanFull(),
|
||||
]),
|
||||
Section::make('Anzeige')
|
||||
->columnSpan(1)
|
||||
->schema([
|
||||
Toggle::make('is_published')->label('Veröffentlicht')->default(true),
|
||||
FileUpload::make('image_path')
|
||||
->label('Bild')
|
||||
->helperText('Optional, Querformat.')
|
||||
->image()
|
||||
->disk('uploads')
|
||||
->directory('events')
|
||||
->visibility('public')
|
||||
->imageAspectRatio('16:9')
|
||||
->automaticallyCropImagesToAspectRatio()
|
||||
->maxSize(10 * 1024),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->defaultSort('starts_at')
|
||||
->columns([
|
||||
TextColumn::make('starts_at')
|
||||
->label('Datum')
|
||||
->formatStateUsing(fn (Event $record) => $record->starts_at->format($record->all_day ? 'd.m.Y' : 'd.m.Y H:i'))
|
||||
->sortable(),
|
||||
TextColumn::make('title')->label('Titel')->searchable()->limit(60)->weight('medium'),
|
||||
TextColumn::make('location')->label('Ort')->limit(40)->toggleable(),
|
||||
IconColumn::make('is_published')->label('Veröffentlicht')->boolean(),
|
||||
])
|
||||
->filters([
|
||||
TernaryFilter::make('upcoming')
|
||||
->label('Zeitraum')
|
||||
->placeholder('Alle')
|
||||
->trueLabel('Anstehende')
|
||||
->falseLabel('Vergangene')
|
||||
->default(true)
|
||||
->queries(
|
||||
true: fn (Builder $query) => $query->where(fn ($q) => $q->where('ends_at', '>=', now())->orWhere(fn ($q) => $q->whereNull('ends_at')->where('starts_at', '>=', now()->startOfDay()))),
|
||||
false: fn (Builder $query) => $query->where(fn ($q) => $q->where('ends_at', '<', now())->orWhere(fn ($q) => $q->whereNull('ends_at')->where('starts_at', '<', now()->startOfDay()))),
|
||||
),
|
||||
])
|
||||
->recordActions([
|
||||
EditAction::make(),
|
||||
DeleteAction::make(),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListEvents::route('/'),
|
||||
'create' => CreateEvent::route('/neu'),
|
||||
'edit' => EditEvent::route('/{record}/bearbeiten'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\Events\Filament\Resources\Events\Pages;
|
||||
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Inhaus\Plugins\Events\Filament\Resources\Events\EventResource;
|
||||
|
||||
class CreateEvent extends CreateRecord
|
||||
{
|
||||
protected static string $resource = EventResource::class;
|
||||
|
||||
protected function mutateFormDataBeforeCreate(array $data): array
|
||||
{
|
||||
$data['author_id'] = auth()->id();
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\Events\Filament\Resources\Events\Pages;
|
||||
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Inhaus\Plugins\Events\Filament\Resources\Events\EventResource;
|
||||
|
||||
class EditEvent extends EditRecord
|
||||
{
|
||||
protected static string $resource = EventResource::class;
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\Events\Filament\Resources\Events\Pages;
|
||||
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
use Inhaus\Plugins\Events\Filament\Resources\Events\EventResource;
|
||||
|
||||
class ListEvents extends ListRecords
|
||||
{
|
||||
protected static string $resource = EventResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make()->label('Neuer Termin'),
|
||||
];
|
||||
}
|
||||
}
|
||||
46
plugins-core/events/src/Http/Controllers/EventController.php
Normal file
46
plugins-core/events/src/Http/Controllers/EventController.php
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\Events\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Support\Branding;
|
||||
use Illuminate\Http\Response as HttpResponse;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
use Inhaus\Plugins\Events\Calendar;
|
||||
use Inhaus\Plugins\Events\Http\Resources\EventResource;
|
||||
use Inhaus\Plugins\Events\Models\Event;
|
||||
|
||||
class EventController extends Controller
|
||||
{
|
||||
public function index(): Response
|
||||
{
|
||||
return Inertia::render('events::Index', [
|
||||
'events' => EventResource::collection(
|
||||
Event::query()->upcoming()->limit(200)->get()
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(Event $event): Response
|
||||
{
|
||||
abort_unless($event->is_published, 404);
|
||||
|
||||
return Inertia::render('events::Show', [
|
||||
'event' => [
|
||||
...(new EventResource($event))->resolve(),
|
||||
'description_html' => $event->description_html,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function ics(Event $event, Calendar $calendar, Branding $branding): HttpResponse
|
||||
{
|
||||
abort_unless($event->is_published, 404);
|
||||
|
||||
return response($calendar->toIcs($event, $branding->portalName()), 200, [
|
||||
'Content-Type' => 'text/calendar; charset=utf-8',
|
||||
'Content-Disposition' => 'attachment; filename="termin-'.$event->id.'.ics"',
|
||||
]);
|
||||
}
|
||||
}
|
||||
31
plugins-core/events/src/Http/Resources/EventResource.php
Normal file
31
plugins-core/events/src/Http/Resources/EventResource.php
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\Events\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Inhaus\Plugins\Events\Models\Event;
|
||||
|
||||
/**
|
||||
* @mixin Event
|
||||
*/
|
||||
class EventResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'location' => $this->location,
|
||||
'online_url' => $this->online_url,
|
||||
'starts_at' => $this->starts_at->toIso8601String(),
|
||||
'ends_at' => $this->ends_at?->toIso8601String(),
|
||||
'all_day' => $this->all_day,
|
||||
'image_url' => $this->imageUrl('large'),
|
||||
'thumb_url' => $this->imageUrl('thumb'),
|
||||
];
|
||||
}
|
||||
}
|
||||
99
plugins-core/events/src/Models/Event.php
Normal file
99
plugins-core/events/src/Models/Event.php
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\Events\Models;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Support\Images;
|
||||
use App\Support\RichText;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Inhaus\Plugins\Events\Database\Factories\EventFactory;
|
||||
|
||||
#[Fillable([
|
||||
'author_id', 'title', 'description', 'location', 'online_url',
|
||||
'starts_at', 'ends_at', 'all_day', 'image_path', 'is_published',
|
||||
])]
|
||||
class Event extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected static function newFactory(): EventFactory
|
||||
{
|
||||
return EventFactory::new();
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saving(function (Event $event): void {
|
||||
if ($event->isDirty('description') || $event->description_html === null) {
|
||||
$event->description_html = app(RichText::class)->toSafeHtml($event->description);
|
||||
}
|
||||
});
|
||||
|
||||
// Bildvarianten erzeugen bzw. beim Austausch die alten Dateien entfernen.
|
||||
static::saved(function (Event $event): void {
|
||||
if (! $event->wasRecentlyCreated && ! $event->wasChanged('image_path')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$previous = $event->wasRecentlyCreated ? null : $event->getOriginal('image_path');
|
||||
|
||||
if ($previous && $previous !== $event->image_path) {
|
||||
app(Images::class)->deleteWithVariants($previous);
|
||||
}
|
||||
|
||||
if ($event->image_path) {
|
||||
app(Images::class)->createVariants($event->image_path);
|
||||
}
|
||||
});
|
||||
|
||||
static::deleted(fn (Event $event) => app(Images::class)->deleteWithVariants($event->image_path));
|
||||
}
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'starts_at' => 'datetime',
|
||||
'ends_at' => 'datetime',
|
||||
'all_day' => 'boolean',
|
||||
'is_published' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<User, $this>
|
||||
*/
|
||||
public function author(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Veröffentlichte Termine, die noch nicht vorbei sind (laufende eingeschlossen).
|
||||
*
|
||||
* @param Builder<Event> $query
|
||||
*/
|
||||
public function scopeUpcoming(Builder $query): void
|
||||
{
|
||||
$query->where('is_published', true)
|
||||
->where(fn (Builder $q) => $q
|
||||
->where('ends_at', '>=', now())
|
||||
->orWhere(fn (Builder $q) => $q->whereNull('ends_at')->where('starts_at', '>=', now()->startOfDay())))
|
||||
->orderBy('starts_at');
|
||||
}
|
||||
|
||||
public function imageUrl(string $variant = 'large'): ?string
|
||||
{
|
||||
return app(Images::class)->url($this->image_path, $variant);
|
||||
}
|
||||
|
||||
/**
|
||||
* Nur Einträge, die diese Person sehen darf (Zielgruppen). Platzhalter bis Phase 2c.
|
||||
*
|
||||
* @param Builder<Event> $query
|
||||
*/
|
||||
public function scopeVisibleTo(Builder $query, ?User $user): void {}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue