82 lines
2.4 KiB
PHP
82 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Presentation\Zvestovatel;
|
|
|
|
use App\Model\ZvestovatelFacade;
|
|
use App\Presentation\BasePresenter;
|
|
use Nette;
|
|
use Nette\Application\UI\Form;
|
|
use Nette\Utils\ArrayHash;
|
|
|
|
|
|
final class ZvestovatelPresenter extends BasePresenter
|
|
{
|
|
public function __construct(
|
|
private ZvestovatelFacade $zvestovatele,
|
|
) {
|
|
}
|
|
|
|
public function renderDefault(): void
|
|
{
|
|
$this->template->zvestovatele = $this->zvestovatele->getVsechny();
|
|
}
|
|
|
|
public function actionEdit(string $jmeno = ''): void
|
|
{
|
|
$jmeno = trim($jmeno);
|
|
if ($this->zvestovatele->get($jmeno) === null) {
|
|
$this->error('Zvěstovatel nenalezen.', Nette\Http\IResponse::S404_NotFound);
|
|
}
|
|
}
|
|
|
|
public function renderEdit(string $jmeno = ''): void
|
|
{
|
|
$jmeno = trim($jmeno);
|
|
$this['zvestovatelForm']->setDefaults([
|
|
'puvodni' => $jmeno,
|
|
'jmeno' => $jmeno,
|
|
]);
|
|
}
|
|
|
|
protected function createComponentZvestovatelForm(): Form
|
|
{
|
|
$form = new Form;
|
|
$form->addHidden('puvodni');
|
|
$form->addText('jmeno', 'Jméno')
|
|
->setRequired('Zadejte jméno zvěstovatele.')
|
|
->setMaxLength(100);
|
|
$form->addSubmit('send', 'Uložit');
|
|
$form->addProtection('Platnost formuláře vypršela, odešlete ho znovu.');
|
|
$form->onSuccess[] = $this->zvestovatelFormSucceeded(...);
|
|
return $form;
|
|
}
|
|
|
|
private function zvestovatelFormSucceeded(Form $form, ArrayHash $values): void
|
|
{
|
|
$jmeno = trim((string) $values->jmeno);
|
|
$puvodni = trim((string) $values->puvodni);
|
|
if ($jmeno === '') {
|
|
$form->addError('Zadejte jméno zvěstovatele.');
|
|
return;
|
|
}
|
|
if (mb_strlen($jmeno) > 100) {
|
|
$form->addError('Jméno zvěstovatele je příliš dlouhé.');
|
|
return;
|
|
}
|
|
if ($this->zvestovatele->get($puvodni) === null) {
|
|
$form->addError('Zvěstovatel nenalezen.');
|
|
return;
|
|
}
|
|
if ($jmeno !== $puvodni && $this->zvestovatele->existujeJmeno($jmeno, $puvodni)) {
|
|
$form->addError('Zvěstovatel s tímto jménem už existuje.');
|
|
return;
|
|
}
|
|
|
|
$this->zvestovatele->uprav($puvodni, $jmeno);
|
|
$this->flashMessage('Zvěstovatel byl upraven.');
|
|
$this->redirect('default');
|
|
}
|
|
}
|