taktik-nette
This commit is contained in:
69
app/UI/Survey/SurveyPresenter.php
Normal file
69
app/UI/Survey/SurveyPresenter.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
4
app/UI/Survey/default.latte
Normal file
4
app/UI/Survey/default.latte
Normal file
@@ -0,0 +1,4 @@
|
||||
{block content}
|
||||
<h1>{_('survey.header')}</h1>
|
||||
{control surveyForm}
|
||||
{/block}
|
||||
Reference in New Issue
Block a user