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
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:
parent
bd7536ab82
commit
858e248d26
160 changed files with 1543 additions and 348 deletions
222
plugins-core/ideen/src/Http/Controllers/IdeaController.php
Normal file
222
plugins-core/ideen/src/Http/Controllers/IdeaController.php
Normal 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');
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue