Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 5m33s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 5m25s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 6m7s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m54s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 9m9s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 10m55s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 10m12s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 1m26s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 9m48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m52s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
57 lines
2.8 KiB
PHP
57 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace Inhaus\Plugins\Umfragen;
|
|
|
|
use App\Dashboard\Widgets;
|
|
use App\Editorial\Editorial;
|
|
use App\Models\User;
|
|
use App\Pagebuilder\Blocks;
|
|
use App\Pagebuilder\BlockType;
|
|
use App\Pagebuilder\Field;
|
|
use App\Plugins\Plugin;
|
|
use App\Support\Abilities;
|
|
use App\Support\Navigation;
|
|
use Illuminate\Support\Facades\Schedule;
|
|
use Inhaus\Plugins\Umfragen\Models\Poll;
|
|
|
|
class UmfragenPlugin extends Plugin
|
|
{
|
|
public function register(): void
|
|
{
|
|
$this->app->make(Navigation::class)->add('umfragen', 'Umfragen', '/umfragen', active: 'polls.*', sort: 74);
|
|
$this->app->make(Abilities::class)->define('polls.manage', 'Umfragen anlegen und auswerten', 'Umfragen', portal: true);
|
|
|
|
$this->app->make(Editorial::class)->add('umfragen', 'Umfragen', 'umfragen::EditorialPolls', 32,
|
|
fn (User $user) => $user->can('polls.manage'),
|
|
fn (User $user) => [
|
|
'polls' => Poll::query()->withCount('responses')->orderByRaw('published_at IS NULL DESC')->latest('published_at')->latest('id')->limit(50)->get()
|
|
->map(fn (Poll $poll) => [...Presenter::card($poll), 'responses' => $poll->responses_count, 'edit_url' => route('polls.edit', $poll)])->all(),
|
|
]);
|
|
|
|
// Startseite: offene Umfragen, an denen die Person noch nicht teilgenommen hat
|
|
$this->app->make(Widgets::class)->add('umfragen.open', 'umfragen::Open', 'aside', sort: 25, props: function (User $user) {
|
|
$polls = Poll::query()->open()->visibleTo($user)->latest('published_at')->limit(10)->get()
|
|
->reject(fn (Poll $poll) => $poll->hasVoted($user))->take(3);
|
|
|
|
return $polls->isEmpty() ? null : ['polls' => $polls->map(fn (Poll $poll) => Presenter::card($poll))->values()->all()];
|
|
}, label: 'Offene Umfragen', mobileSort: 35);
|
|
|
|
// Baustein: Umfrage direkt in Seite oder Beitrag
|
|
$this->app->make(Blocks::class)->add(BlockType::make('umfragen.poll', 'Umfrage', 'vote', 'dynamic', 'umfragen::PollBlock')
|
|
->description('Eine Umfrage direkt auf der Seite beantworten lassen.')
|
|
->fields([
|
|
Field::select('poll', 'Umfrage', fn () => Poll::query()->latest('id')->limit(200)->pluck('title', 'id')->mapWithKeys(fn ($t, $id) => [(string) $id => $t])->all()),
|
|
])
|
|
->resolveUsing(function (array $data, ?User $user) {
|
|
$poll = $data['poll'] ? Poll::query()->find($data['poll']) : null;
|
|
|
|
return $poll && $user && ($poll->isPublished() || $user->can('polls.manage')) && $poll->isVisibleTo($user) ? Presenter::present($poll, $user) : null;
|
|
}));
|
|
}
|
|
|
|
public function boot(): void
|
|
{
|
|
// Geplante Umfragen ankündigen, sobald sie beginnen
|
|
Schedule::call(fn () => $this->app->make(Announcer::class)->announceDue())->everyTenMinutes()->name('umfragen-announce')->withoutOverlapping(10);
|
|
}
|
|
}
|