Ideenbox (0.33.0)
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 9m16s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 9m12s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 9m52s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 9m47s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 10m4s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 10m6s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 8m6s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 56s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 8m56s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m53s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-27 13:09:05 +00:00
parent bd7536ab82
commit 858e248d26
160 changed files with 1543 additions and 348 deletions

View file

@ -0,0 +1,222 @@
<?php
namespace Inhaus\Plugins\Ideen\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Notifications\PortalNotification;
use App\Support\Audit;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Inertia\Inertia;
use Inertia\Response;
use Inhaus\Plugins\Ideen\Models\Idea;
use Inhaus\Plugins\Ideen\Models\IdeaComment;
use Inhaus\Plugins\Ideen\Presenter;
/**
* Ideenbox: einreichen, unterstützen, kommentieren – und die Bewertung durch das Gremium.
*/
class IdeaController extends Controller
{
public function index(Request $request): Response|JsonResponse
{
$data = $request->validate([
'status' => ['nullable', Rule::in(array_keys(Idea::STATUSES))],
'sortierung' => ['nullable', Rule::in(['neu', 'beliebt', 'diskutiert'])],
'q' => ['nullable', 'string', 'max:100'],
]);
$user = $request->user();
$ideas = Idea::query()->with('author')
->when($data['status'] ?? null, fn ($q, $status) => $q->where('status', $status))
->when($data['q'] ?? null, fn ($q, $term) => $q->where(fn ($q) => $q->where('title', 'like', '%'.addcslashes($term, '%_\\').'%')->orWhere('body', 'like', '%'.addcslashes($term, '%_\\').'%')))
->when(($data['sortierung'] ?? 'neu') === 'beliebt', fn ($q) => $q->orderByDesc('supports_count'))
->when(($data['sortierung'] ?? 'neu') === 'diskutiert', fn ($q) => $q->orderByDesc('comments_count'))
->latest()->latest('id')
->paginate(20)->withQueryString();
$supported = $user->belongsToMany(Idea::class, 'idea_supports')->whereIn('ideas.id', $ideas->modelKeys())->pluck('ideas.id')->all();
$items = collect($ideas->items())->map(fn (Idea $idea) => Presenter::card($idea, in_array($idea->id, $supported, true)))->all();
if ($this->wantsJson($request)) {
return response()->json(['data' => $items, 'meta' => ['current_page' => $ideas->currentPage(), 'last_page' => $ideas->lastPage(), 'total' => $ideas->total()]]);
}
return Inertia::render('ideen::Index', [
'ideas' => $items,
'pagination' => ['current' => $ideas->currentPage(), 'last' => $ideas->lastPage(), 'prev' => $ideas->previousPageUrl(), 'next' => $ideas->nextPageUrl()],
'filters' => ['status' => $data['status'] ?? null, 'sortierung' => $data['sortierung'] ?? 'neu', 'q' => $data['q'] ?? ''],
'statuses' => Idea::STATUSES,
'counts' => Idea::query()->selectRaw('status, count(*) as c')->groupBy('status')->pluck('c', 'status'),
]);
}
public function show(Request $request, Idea $idea): Response|JsonResponse
{
$detail = Presenter::detail($idea->load('author'), $request->user());
return $this->wantsJson($request)
? response()->json(['data' => $detail])
: Inertia::render('ideen::Show', ['idea' => $detail, 'statuses' => Idea::STATUSES]);
}
public function store(Request $request): RedirectResponse|JsonResponse
{
$data = $this->validated($request);
$anonymous = $request->boolean('anonymous');
$idea = Idea::query()->create([
'title' => $data['title'],
'body' => $data['body'],
'author_id' => $anonymous ? null : $request->user()->id,
'anonymous' => $anonymous,
])->refresh();
if ($this->wantsJson($request)) {
return response()->json(['data' => Presenter::detail($idea, $request->user())], 201);
}
return to_route('ideas.show', $idea)->with('status', 'Danke! Ihre Idee ist eingereicht.');
}
public function update(Request $request, Idea $idea): RedirectResponse
{
abort_unless(! $idea->anonymous && $idea->author_id === $request->user()->id && $idea->status === 'new', 403);
$idea->update($this->validated($request));
return back()->with('status', 'Idee gespeichert.');
}
public function destroy(Request $request, Idea $idea): RedirectResponse
{
$user = $request->user();
$own = ! $idea->anonymous && $idea->author_id === $user->id && $idea->status === 'new';
abort_unless($own || $user->can('ideas.manage'), 403);
$idea->delete();
if (! $own) {
Audit::log('idea.deleted', ['idea' => $idea->id, 'title' => $idea->title]);
}
return to_route('ideas.index')->with('status', 'Idee gelöscht.');
}
public function support(Request $request, Idea $idea): RedirectResponse|JsonResponse
{
abort_if(! $idea->anonymous && $idea->author_id === $request->user()->id, 422, 'Die eigene Idee lässt sich nicht unterstützen.');
$idea->supporters()->syncWithoutDetaching([$request->user()->id => ['created_at' => now()]]);
return $this->supportResponse($request, $idea, true);
}
public function unsupport(Request $request, Idea $idea): RedirectResponse|JsonResponse
{
$idea->supporters()->detach($request->user()->id);
return $this->supportResponse($request, $idea, false);
}
public function comment(Request $request, Idea $idea): RedirectResponse|JsonResponse
{
$data = $request->validate(['body' => ['required', 'string', 'max:'.IdeaComment::MAX_LENGTH], 'official' => ['boolean']], [], ['body' => 'Kommentar']);
$user = $request->user();
$official = ($data['official'] ?? false) && $user->can('ideas.manage');
$comment = $idea->comments()->create(['user_id' => $user->id, 'body' => trim($data['body']), 'official' => $official]);
$idea->refreshCounts();
// Die einreichende Person erfährt von neuen Kommentaren
if (! $idea->anonymous && $idea->author && $idea->author_id !== $user->id && $idea->author->is_active) {
$idea->author->notify(new PortalNotification(
($official ? 'Antwort zu Ihrer Idee: ' : 'Neuer Kommentar zu Ihrer Idee: ').$idea->title,
Str::limit($comment->body, 120),
route('ideas.show', $idea, false).'#kommentare',
'lightbulb',
));
}
return $this->wantsJson($request)
? response()->json(['data' => Presenter::detail($idea->fresh(), $user)], 201)
: back()->with('status', 'Kommentar veröffentlicht.');
}
public function deleteComment(Request $request, Idea $idea, IdeaComment $comment): RedirectResponse
{
abort_unless($comment->idea_id === $idea->id, 404);
$user = $request->user();
abort_unless($comment->user_id === $user->id || $user->can('ideas.manage'), 403);
$comment->delete();
$idea->refreshCounts();
return back()->with('status', 'Kommentar gelöscht.');
}
/** Gremium: Status und Rückmeldung */
public function status(Request $request, Idea $idea): RedirectResponse
{
Gate::authorize('ideas.manage');
$data = $request->validate([
'status' => ['required', Rule::in(array_keys(Idea::STATUSES))],
'status_note' => ['nullable', 'string', 'max:3000'],
]);
$changed = $data['status'] !== $idea->status;
$idea->update([
'status' => $data['status'],
'status_note' => filled($data['status_note'] ?? null) ? trim($data['status_note']) : null,
'status_changed_at' => $changed ? now() : $idea->status_changed_at,
]);
Audit::log('idea.status', ['idea' => $idea->id, 'status' => $idea->status]);
if ($changed) {
// Einreichende Person und alle Unterstützenden informieren
$recipients = $idea->supporters()->where('is_active', true)->get()
->when(! $idea->anonymous && $idea->author?->is_active, fn ($c) => $c->push($idea->author))
->unique('id')->reject(fn ($u) => $u->id === $request->user()->id);
Notification::send($recipients, new PortalNotification(
'Idee „'.Str::limit($idea->title, 60).'“: '.$idea->statusLabel(),
Str::limit((string) $idea->status_note, 120),
route('ideas.show', $idea, false),
'lightbulb',
));
}
return back()->with('status', 'Status gespeichert.');
}
/**
* @return array{title: string, body: string}
*/
private function validated(Request $request): array
{
$data = $request->validate([
'title' => ['required', 'string', 'max:160'],
'body' => ['required', 'string', 'max:5000'],
'anonymous' => ['boolean'],
], [], ['title' => 'Titel', 'body' => 'Beschreibung']);
return ['title' => trim($data['title']), 'body' => trim($data['body'])];
}
private function supportResponse(Request $request, Idea $idea, bool $supported): RedirectResponse|JsonResponse
{
$idea->refreshCounts();
return $this->wantsJson($request)
? response()->json(['data' => ['supported' => $supported, 'supports' => $idea->supports_count]])
: back();
}
private function wantsJson(Request $request): bool
{
return $request->expectsJson() && ! $request->header('X-Inertia');
}
}

View file

@ -0,0 +1,37 @@
<?php
namespace Inhaus\Plugins\Ideen;
use App\Dashboard\Widgets;
use App\Editorial\Editorial;
use App\Models\User;
use App\Plugins\Plugin;
use App\Support\Abilities;
use App\Support\Navigation;
use Inhaus\Plugins\Ideen\Models\Idea;
class IdeenPlugin extends Plugin
{
public function register(): void
{
$this->app->make(Navigation::class)->add('ideen', 'Ideenbox', '/ideen', active: 'ideas.*', sort: 76);
$this->app->make(Abilities::class)->define('ideas.manage', 'Ideen bewerten, beantworten und moderieren (Gremium)', 'Ideenbox', portal: true);
// Redaktion: was das Gremium noch sichten muss
$this->app->make(Editorial::class)->add('ideen', 'Ideenbox', 'ideen::EditorialIdeas', 34,
fn (User $user) => $user->can('ideas.manage'),
fn (User $user) => [
'ideas' => Idea::query()->with('author')->whereIn('status', ['new', 'review'])
->orderByRaw("status = 'new' DESC")->orderByDesc('supports_count')->latest()->limit(30)->get()
->map(fn (Idea $idea) => Presenter::card($idea))->all(),
]);
// Startseite: beliebteste offene Ideen
$this->app->make(Widgets::class)->add('ideen.popular', 'ideen::Popular', 'aside', sort: 28, props: function () {
$ideas = Idea::query()->with('author')->whereIn('status', ['new', 'review'])->where('created_at', '>=', now()->subDays(60))
->orderByDesc('supports_count')->latest()->limit(3)->get();
return $ideas->isEmpty() ? null : ['ideas' => $ideas->map(fn (Idea $idea) => Presenter::card($idea))->all()];
}, label: 'Ideenbox', mobileSort: 38);
}
}

View file

@ -0,0 +1,63 @@
<?php
namespace Inhaus\Plugins\Ideen\Models;
use App\Models\User;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
#[Fillable(['title', 'body', 'author_id', 'anonymous', 'status', 'status_note', 'status_changed_at'])]
class Idea extends Model
{
public const STATUSES = [
'new' => 'Neu',
'review' => 'In Prüfung',
'done' => 'Umgesetzt',
'rejected' => 'Abgelehnt',
];
protected function casts(): array
{
return ['anonymous' => 'boolean', 'status_changed_at' => 'datetime'];
}
/**
* @return BelongsTo<User, $this>
*/
public function author(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* @return BelongsToMany<User, $this>
*/
public function supporters(): BelongsToMany
{
return $this->belongsToMany(User::class, 'idea_supports')->withPivot('created_at');
}
/**
* @return HasMany<IdeaComment, $this>
*/
public function comments(): HasMany
{
return $this->hasMany(IdeaComment::class);
}
public function statusLabel(): string
{
return self::STATUSES[$this->status] ?? $this->status;
}
public function refreshCounts(): void
{
$this->forceFill([
'supports_count' => $this->supporters()->count(),
'comments_count' => $this->comments()->count(),
])->saveQuietly();
}
}

View file

@ -0,0 +1,35 @@
<?php
namespace Inhaus\Plugins\Ideen\Models;
use App\Models\User;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
#[Fillable(['idea_id', 'user_id', 'body', 'official'])]
class IdeaComment extends Model
{
public const MAX_LENGTH = 3000;
protected function casts(): array
{
return ['official' => 'boolean'];
}
/**
* @return BelongsTo<User, $this>
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* @return BelongsTo<Idea, $this>
*/
public function idea(): BelongsTo
{
return $this->belongsTo(Idea::class);
}
}

View file

@ -0,0 +1,69 @@
<?php
namespace Inhaus\Plugins\Ideen;
use App\Models\User;
use Illuminate\Support\Str;
use Inhaus\Plugins\Ideen\Models\Idea;
use Inhaus\Plugins\Ideen\Models\IdeaComment;
class Presenter
{
/**
* @return array<string, mixed>
*/
public static function card(Idea $idea, ?bool $supported = null): array
{
return [
'id' => $idea->id,
'title' => $idea->title,
'excerpt' => Str::limit(preg_replace('/\s+/u', ' ', $idea->body), 180),
'status' => $idea->status,
'status_label' => $idea->statusLabel(),
'supports' => $idea->supports_count,
'comments' => $idea->comments_count,
'supported' => $supported,
'author' => self::author($idea),
'created_at' => $idea->created_at?->toIso8601String(),
'url' => route('ideas.show', $idea),
];
}
/**
* @return array<string, mixed>
*/
public static function detail(Idea $idea, User $user): array
{
$manager = $user->can('ideas.manage');
return [
...self::card($idea, $idea->supporters()->whereKey($user->id)->exists()),
'body' => $idea->body,
'anonymous' => $idea->anonymous,
'status_note' => $idea->status_note,
'status_changed_at' => $idea->status_changed_at?->toIso8601String(),
'mine' => ! $idea->anonymous && $idea->author_id === $user->id,
'can_edit' => ! $idea->anonymous && $idea->author_id === $user->id && $idea->status === 'new',
'can_manage' => $manager,
'comment_list' => $idea->comments()->with('user')->oldest()->oldest('id')->limit(500)->get()
->map(fn (IdeaComment $c) => [
'id' => $c->id,
'body' => $c->body,
'official' => $c->official,
'created_at' => $c->created_at?->toIso8601String(),
'author' => $c->user ? ['id' => $c->user->id, 'name' => $c->user->name, 'initials' => $c->user->initials, 'avatar_url' => $c->user->avatarUrl(64)] : null,
'can_delete' => $manager || $c->user_id === $user->id,
])->all(),
];
}
/**
* @return array{id: int, name: string, initials: string, avatar_url: ?string}|null
*/
private static function author(Idea $idea): ?array
{
$author = $idea->anonymous ? null : $idea->author;
return $author ? ['id' => $author->id, 'name' => $author->name, 'initials' => $author->initials, 'avatar_url' => $author->avatarUrl(64)] : null;
}
}