| Current Path : /home/forge/stage.sksb.smartcon-survey.com/app/Livewire/ |
| Current File : /home/forge/stage.sksb.smartcon-survey.com/app/Livewire/Comments.php |
<?php
namespace App\Livewire;
use App\Livewire\Filter\MonthFilter;
use App\Models\Comment;
use App\Models\Topic;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Redirector;
use Illuminate\View\View;
use Livewire\Component;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class Comments extends Component
{
public $comments;
public $topics;
public $pickedTopic;
public array $pickedMonths = [];
public int $limit = 10;
public array $months = [];
public function updatedPickedTopic(): void
{
$this->comments = $this->filterComments($this->limit)->get();
}
public function updatedPickedMonths(): void
{
$this->comments = $this->filterComments($this->limit)->get();
}
private function filterComments(int $limit = 10): Builder
{
$comments = Comment::where('show', 1)
->orderBy('comments.created_at', 'desc');
if ($this->pickedTopic > 0) {
$comments = $comments->whereHas('assignedTopics', fn ($query) => $query->where('topic_id', $this->pickedTopic));
}
if (! empty($this->pickedMonths)) {
$comments = $comments->whereInDateRange($this->pickedMonths);
}
if ($limit > 0) {
$comments = $comments->limit($limit);
}
return $comments;
}
public function render(): View
{
$this->months = (new MonthFilter)->months();
$this->topics = Topic::select(['id', 'name'])->get()->all();
$this->comments = $this->filterComments($this->limit)->get();
return view('livewire.comments');
}
public function showMore(): void
{
$this->limit += 10;
}
public function downloadExcel(): BinaryFileResponse
{
$collection = $this->comments->map(function ($item) {
return $item->only(['text', 'topic', 'created_at']);
});
$collection = $collection->map(function ($item) {
$item['topic'] = $item['topic']->name;
return $item;
});
return $collection->downloadExcel('Offene_Antworten.xlsx');
}
public function restoreComment(int $id): RedirectResponse|Redirector
{
Comment::restoreComment($id);
session()->flash('comment_success', 'Das Antwort wurde erfolgreich zurückgeholt.');
return redirect(route('dashboard.comments'));
}
}