Kufrik/app/UI/ZakaznikEdit/ZakaznikEditPresenter.php

101 lines
2.6 KiB
PHP

<?php
namespace App\UI\ZakaznikEdit;
use App\UI\BasePresenter;
use App\Model\Facade\ZakaznikFacade;
use Nette\Application\UI\Form;
class ZakaznikEditPresenter extends BasePresenter
{
/** @persistent */
public ?int $id = null;
public function __construct(
private ZakaznikFacade $zakaznikFacade
) {
parent::__construct();
}
public function actionNew(): void
{
$this->id = null;
$this->setView('edit'); // <--- Zde přikážeme Nette použít edit.latte
}
public function actionEdit(int $id): void
{
$this->id = $id;
$zakaznik = $this->zakaznikFacade->getById($id);
if (!$zakaznik) {
$this->flashMessage('Zákazník nebyl nalezen.', 'danger');
$this->redirect('Zakaznik:default');
}
$this['zakaznikForm']->setDefaults($zakaznik->toArray());
}
public function renderEdit(?int $id = null): void
{
$this->template->isEdit = (bool) $this->id;
}
public function renderNew(): void
{
$this->template->isEdit = false;
}
protected function createComponentZakaznikForm(): Form
{
$form = new Form;
// Základní údaje
$form->addText('PRIJMENI', 'Příjmení / Název firmy:')
->setRequired('Zadejte příjmení nebo název firmy.');
$form->addText('JMENO', 'Jméno:');
// IČO / DIČ
$form->addText('IC', 'IČ:')
->setMaxLength(8);
$form->addText('DIC', 'DIČ:')
->setMaxLength(10);
// Adresa
$form->addText('ULICE', 'Ulice a ČP:');
$form->addText('MESTO', 'Město:');
$form->addText('PSC', 'PSČ:')
->setMaxLength(5);
// Kontaktní údaje
$form->addText('TELEFON', 'Telefon:');
$form->addEmail('EMAIL', 'E-mail:');
// Příznak aktivity
$form->addCheckbox('AKTIVNI', 'Aktivní zákazník')
->setDefaultValue(true);
$form->addSubmit('save', 'Uložit zákazníka')
->setHtmlAttribute('class', 'btn btn-primary');
$form->onSuccess[] = [$this, 'zakaznikFormSucceeded'];
return $form;
}
public function zakaznikFormSucceeded(Form $form, \stdClass $data): void
{
$firmaId = $this->getUser()->getIdentity()->firma_id;
$this->zakaznikFacade->save((array) $data, $this->id, $firmaId);
$this->flashMessage(
$this->id ? 'Údaje zákazníka byly upraveny.' : 'Nový zákazník byl úspěšně vytvořen.',
'success'
);
$this->redirect('Zakaznik:default');
}
}