taktik-nette-pohovor/app/UI/Results/ResultsPresenter.php

138 lines
4.3 KiB
PHP
Raw Normal View History

2025-01-22 22:52:26 +01:00
<?php
declare(strict_types=1);
namespace App\UI\Results;
use App\Repository\SurveyRepository;
use App\UI\DTO\FilterDTO;
use App\UI\DTO\OrderDTO;
use App\UI\Form\ResultFilteringFactory;
use Nette\Application\Attributes\Persistent;
use Nette\Application\UI\Form;
use Nette\Application\UI\Presenter;
use Nette\Http\Session;
use Nette\Utils\Paginator;
use Nubium\Exception\ThisShouldNeverHappenException;
class ResultsPresenter extends Presenter
{
#[Persistent]
public ?string $order = null;
#[Persistent]
public ?string $direction = null;
/**
* @var array<string, mixed>|null
*/
#[Persistent]
public ?array $filter = null;
protected const SESSION_STORE_KEY = 'survey_results_v1';
public function __construct(
protected readonly SurveyRepository $surveyRepository,
protected readonly ResultFilteringFactory $resultFilteringFactory,
protected readonly Session $session,
) {
parent::__construct();
$this->restoreStateFromSession();
if ($this->order === null) {
$this->order = 'survey_id';
}
if ($this->direction === null) {
$this->direction = 'desc';
}
if ($this->filter === null) {
$this->filter = null;
}
}
public function renderDefault(int $page = 1): void
{
$order = OrderDTO::createOrReturnNullFromData($this->order, $this->direction, ['survey_name', 'comments', 'survey_id']);
$filter = FilterDTO::createFromData($this->filter, ['name', 'comments', 'agreement', 'interest']);
$itemsCount = $this->surveyRepository->getSuveysCount($filter);
$paginator = new Paginator();
$paginator->setItemCount($itemsCount);
$paginator->setItemsPerPage(10);
$paginator->setPage($page);
$articles = $this->surveyRepository->findSurveys($paginator->getLength(), $paginator->getOffset(), $order, $filter);
$this->template->surveys = $articles; // @phpstan-ignore property.notFound
$this->template->paginator = $paginator; // @phpstan-ignore property.notFound
$this->template->order = $this->order; // @phpstan-ignore property.notFound
$this->template->direction = $this->direction; // @phpstan-ignore property.notFound
}
protected function createComponentFilteringForm(): Form
{
$form = $this->resultFilteringFactory->create();
$form->setDefaults($this->filter ?? []);
// @phpstan-ignore-next-line assign.propertyType
$form->onSuccess[] = function (\Nette\Forms\Form $form, array $values): void {
if ($form->isValid()) {
$filter = [];
if (!empty($values['name'])) {
$filter['name'] = $values['name'];
}
if (!empty($values['agreement'])) {
$filter['agreement'] = $values['agreement'];
}
if (!empty($values['comments'])) {
$filter['comments'] = $values['comments'];
}
if (!empty($values['interest'])) {
$filter['interest'] = $values['interest'];
}
$this->filter = $filter;
$this->storeStateIntoSession();
$this->redirect('default', [
'page' => 1,
'filter' => $filter,
]);
}
};
return $form;
}
protected function restoreStateFromSession(): void
{
$session = $this->session->getSection(self::SESSION_STORE_KEY);
if ($session->get('order') !== null) {
$this->order = $session->get('order');
}
if ($session->get('direction') !== null) {
$this->direction = $session->get('direction');
}
if ($session->get('filter') !== null) {
$this->filter = $session->get('filter');
}
}
protected function storeStateIntoSession(): void
{
$session = $this->session->getSection(self::SESSION_STORE_KEY);
$session->set('order', $this->order);
$session->set('direction', $this->direction);
$session->set('filter', $this->filter);
}
public function terminate(): void
{
$this->storeStateIntoSession();
parent::terminate();
}
}