Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 6m49s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m19s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m29s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 7m39s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 9m38s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 9m25s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 7m57s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 8m4s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 46s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m9s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
82 lines
3.6 KiB
PHP
82 lines
3.6 KiB
PHP
<?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;
|
||
use Inhaus\Plugins\Ideen\Models\IdeaReport;
|
||
|
||
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');
|
||
$reports = IdeaReport::query()->where('idea_id', $idea->id)->get();
|
||
$mineReported = $reports->where('user_id', $user->id)->map(fn ($r) => $r->comment_id ?? 0)->all();
|
||
// Für das Gremium: offene Meldungen je Inhalt – Gründe und Anmerkungen, ohne Namen der Meldenden
|
||
$open = fn (?int $commentId) => $manager ? $reports->whereNull('resolved_at')->where('comment_id', $commentId)->values()
|
||
->map(fn (IdeaReport $r) => ['reason' => IdeaReport::REASONS[$r->reason] ?? $r->reason, 'note' => $r->note])->all() : [];
|
||
|
||
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,
|
||
'hidden' => $idea->hidden_at !== null,
|
||
'reported' => in_array(0, $mineReported, true),
|
||
'reports' => $open(null),
|
||
'comment_list' => $idea->comments()->with('user')->oldest()->oldest('id')->limit(500)->get()
|
||
->map(fn (IdeaComment $c) => [
|
||
'id' => $c->id,
|
||
// Ausgeblendet: nur Gremium und die schreibende Person sehen den Text
|
||
'body' => $c->hidden_at && ! $manager && $c->user_id !== $user->id ? null : $c->body,
|
||
'hidden' => $c->hidden_at !== null,
|
||
'reported' => in_array($c->id, $mineReported, true),
|
||
'reports' => $open($c->id),
|
||
'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;
|
||
}
|
||
}
|