76 lines
2.4 KiB
PHP
76 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Livewire\MainPage;
|
|
|
|
use App\Models\Poll;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Livewire\Attributes\Locked;
|
|
use Livewire\Component;
|
|
|
|
/**
|
|
* Main-page card with the first open poll the active profile may answer and has not
|
|
* answered yet (soonest closing first). Disappears once voted, on the next page load.
|
|
*/
|
|
class PollCardFull extends Component
|
|
{
|
|
#[Locked]
|
|
public ?int $pollId = null;
|
|
#[Locked]
|
|
public ?string $title = null;
|
|
#[Locked]
|
|
public ?string $excerpt = null;
|
|
#[Locked]
|
|
public ?string $slug = null;
|
|
/** More than one question: the card links to the poll page instead of showing the form */
|
|
#[Locked]
|
|
public int $questionCount = 0;
|
|
/** The post has a longer text: "Read more" above the single question */
|
|
#[Locked]
|
|
public bool $hasContent = false;
|
|
|
|
public function mount(): void
|
|
{
|
|
$profile = getActiveProfile();
|
|
$user = Auth::guard('web')->user();
|
|
if (!$profile || !$user instanceof User) {
|
|
return;
|
|
}
|
|
|
|
$locale = App::getLocale();
|
|
$published = function ($query) use ($locale) {
|
|
$query->where('locale', $locale)
|
|
->whereDate('from', '<=', now())
|
|
->where(fn ($q) => $q->whereDate('till', '>', now())->orWhereNull('till'));
|
|
};
|
|
|
|
$polls = Poll::openNow()
|
|
->whereHas('post.translations', $published)
|
|
->with(['post.translations' => $published])
|
|
->orderByRaw('closes_at IS NULL')
|
|
->orderBy('closes_at')
|
|
->orderByDesc('id')
|
|
->limit(20)
|
|
->get();
|
|
|
|
foreach ($polls as $poll) {
|
|
if ($poll->isTargetedAt($profile, $user) && $poll->responseFor($profile, $user->id) === null) {
|
|
$translation = $poll->post->translations->first();
|
|
$this->pollId = $poll->id;
|
|
$this->title = $translation->title;
|
|
$this->excerpt = $translation->excerpt;
|
|
$this->slug = $translation->slug;
|
|
$this->questionCount = $poll->questions()->count();
|
|
$this->hasContent = trim(strip_tags((string) $translation->content)) !== '';
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.main-page.poll-card-full');
|
|
}
|
|
}
|