70 lines
2.1 KiB
PHP
70 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\UI\Survey;
|
|
|
|
use Nette;
|
|
use App\UI\Form\SurveyFormFactory;
|
|
use Nette\Application\UI\Form;
|
|
use Nubium\Exception\ThisShouldNeverHappenException;
|
|
|
|
class SurveyPresenter extends Nette\Application\UI\Presenter
|
|
{
|
|
public function __construct(
|
|
protected readonly SurveyFormFactory $surveyFormFactor,
|
|
protected readonly Nette\Database\Explorer $database,
|
|
protected readonly Nette\Localization\Translator $translator
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
public function renderDefault(): void
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $values
|
|
*/
|
|
public function processSurveyForm(Nette\Forms\Form $form, array $values): void
|
|
{
|
|
if ($form->isValid()) {
|
|
$this->database->beginTransaction();
|
|
|
|
try {
|
|
$recordId = $this->database->table('survey')->insert(
|
|
[
|
|
'name' => $values['name'],
|
|
'comments' => $values['comments'],
|
|
'agreement' => $values['agreement'],
|
|
]
|
|
);
|
|
|
|
foreach ($values['interests'] as $value) {
|
|
$this->database->table('interests')->insert(
|
|
[
|
|
'survey_id' => $recordId,
|
|
'interest' => $value,
|
|
]
|
|
);
|
|
}
|
|
|
|
$this->database->commit();
|
|
|
|
$this->flashMessage($this->translator->translate('survey.successfullyFilled'), 'success');
|
|
$this->redirect('this');
|
|
} catch (\PDOException $e) {
|
|
$this->database->rollback(); // TODO: Tady bych dal log do sentry a rekl uziteli at to zkusi pozdeji ;-)
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function createComponentSurveyForm(): Nette\Application\UI\Form
|
|
{
|
|
$form = $this->surveyFormFactor->create();
|
|
$form->onSuccess[] = [$this, 'processSurveyForm']; // @phpstan-ignore assign.propertyType
|
|
return $form;
|
|
}
|
|
}
|