180 lines
6.0 KiB
PHP
180 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\UI\Nastaveni;
|
|
|
|
use App\Model\Faktura\FakturaCislovani;
|
|
use App\Model\Facade\DatabazeFacade;
|
|
use App\Model\Facade\FakturaFacade;
|
|
use App\UI\BasePresenter;
|
|
use Nette\Application\UI\Form;
|
|
use Nette\Security\SimpleIdentity;
|
|
|
|
class NastaveniPresenter extends BasePresenter
|
|
{
|
|
public function __construct(
|
|
private FakturaFacade $fakturaFacade,
|
|
private DatabazeFacade $databazeFacade,
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
public function actionDefault(): void
|
|
{
|
|
$firmaId = $this->getUser()->getIdentity()->firma_id ?? null;
|
|
if (!$firmaId) {
|
|
$this->flashMessage('K účtu není přiřazena žádná aktivní firma.', 'danger');
|
|
$this->redirect('Home:');
|
|
}
|
|
|
|
$nastaveni = $this->fakturaFacade->getNastaveni((int) $firmaId);
|
|
$this['nastaveniForm']->setDefaults([
|
|
'maska' => $nastaveni['maska'],
|
|
'obdobi' => $nastaveni['obdobi'],
|
|
'splatnost' => $nastaveni['splatnostDny'],
|
|
]);
|
|
|
|
$login = $this->databazeFacade->getById((int) $this->getUser()->getId());
|
|
$email = $login !== null
|
|
? (string) $login->EMAIL
|
|
: (string) ($this->getUser()->getIdentity()->email ?? '');
|
|
$this['jmenoForm']->setDefaults([
|
|
'email' => $email,
|
|
]);
|
|
}
|
|
|
|
public function renderDefault(): void
|
|
{
|
|
$firmaId = (int) $this->getUser()->getIdentity()->firma_id;
|
|
$dalsi = $this->fakturaFacade->getNextCislo($firmaId);
|
|
$this->template->prikladCisla = $dalsi['cislo'];
|
|
|
|
$hesloForm = $this['hesloForm'];
|
|
$this->template->otevritHesloModal = $hesloForm->isSubmitted() && !$hesloForm->isSuccess();
|
|
}
|
|
|
|
protected function createComponentNastaveniForm(): Form
|
|
{
|
|
$form = new Form;
|
|
|
|
$form->addText('maska', 'Číslování faktur (maska):')
|
|
->setRequired('Zadejte masku čísla faktury.')
|
|
->setMaxLength(40);
|
|
|
|
$form->addSelect('obdobi', 'Číslovat po:', FakturaCislovani::OBDOBI)
|
|
->setRequired('Vyberte období číslování.');
|
|
|
|
$form->addInteger('splatnost', 'Splatnost faktury (dny):')
|
|
->setRequired('Zadejte počet dní splatnosti.')
|
|
->setDefaultValue(FakturaCislovani::SPLATNOST_VYCHOZI)
|
|
->addRule(Form::Range, 'Splatnost musí být 0 až 365 dní.', [0, 365]);
|
|
|
|
$form->addSubmit('save', 'Uložit nastavení');
|
|
$form->onSuccess[] = [$this, 'nastaveniFormSucceeded'];
|
|
|
|
return $form;
|
|
}
|
|
|
|
public function nastaveniFormSucceeded(Form $form, \stdClass $data): void
|
|
{
|
|
$chyba = FakturaCislovani::validateMaska((string) $data->maska);
|
|
if ($chyba !== null) {
|
|
$form['maska']->addError($chyba);
|
|
return;
|
|
}
|
|
|
|
$firmaId = (int) $this->getUser()->getIdentity()->firma_id;
|
|
$this->fakturaFacade->saveNastaveni(
|
|
$firmaId,
|
|
(string) $data->maska,
|
|
(string) $data->obdobi,
|
|
(int) $data->splatnost
|
|
);
|
|
|
|
$this->flashMessage('Nastavení faktur bylo uloženo.', 'success');
|
|
$this->redirect('this');
|
|
}
|
|
|
|
protected function createComponentJmenoForm(): Form
|
|
{
|
|
$form = new Form;
|
|
|
|
$form->addText('email', 'Uživatelské jméno:')
|
|
->setHtmlType('email')
|
|
->setHtmlAttribute('autocomplete', 'username')
|
|
->setRequired('Zadejte uživatelské jméno.')
|
|
->addRule(Form::Email, 'Zadejte platný e-mail.')
|
|
->addRule(Form::MaxLength, 'Uživatelské jméno může mít nejvýše 100 znaků.', 100);
|
|
|
|
$form->addSubmit('save', 'Uložit jméno');
|
|
$form->onSuccess[] = [$this, 'jmenoFormSucceeded'];
|
|
|
|
return $form;
|
|
}
|
|
|
|
public function jmenoFormSucceeded(Form $form, \stdClass $data): void
|
|
{
|
|
try {
|
|
$email = $this->databazeFacade->changeOwnEmail(
|
|
(int) $this->getUser()->getId(),
|
|
(string) $data->email
|
|
);
|
|
} catch (\InvalidArgumentException $e) {
|
|
$form->addError($e->getMessage());
|
|
return;
|
|
}
|
|
|
|
$this->replaceIdentityEmail($email);
|
|
$this->flashMessage('Uživatelské jméno bylo uloženo.', 'success');
|
|
$this->redirect('this');
|
|
}
|
|
|
|
protected function createComponentHesloForm(): Form
|
|
{
|
|
$form = new Form;
|
|
|
|
$form->addPassword('current', 'Původní heslo:')
|
|
->setHtmlAttribute('autocomplete', 'current-password')
|
|
->setRequired('Zadejte původní heslo.');
|
|
|
|
$form->addPassword('password', 'Nové heslo:')
|
|
->setHtmlAttribute('autocomplete', 'new-password')
|
|
->setRequired('Zadejte nové heslo.')
|
|
->addRule(Form::MinLength, 'Heslo musí mít alespoň 6 znaků.', 6);
|
|
|
|
$form->addPassword('passwordVerify', 'Nové heslo znovu:')
|
|
->setHtmlAttribute('autocomplete', 'new-password')
|
|
->setRequired('Zadejte nové heslo znovu.')
|
|
->addRule(Form::Equal, 'Hesla se neshodují.', $form['password']);
|
|
|
|
$form->addSubmit('save', 'Uložit heslo');
|
|
$form->onSuccess[] = [$this, 'hesloFormSucceeded'];
|
|
|
|
return $form;
|
|
}
|
|
|
|
public function hesloFormSucceeded(Form $form, \stdClass $data): void
|
|
{
|
|
try {
|
|
$this->databazeFacade->changeOwnPassword(
|
|
(int) $this->getUser()->getId(),
|
|
(string) $data->current,
|
|
(string) $data->password
|
|
);
|
|
} catch (\InvalidArgumentException $e) {
|
|
$form['current']->addError($e->getMessage());
|
|
return;
|
|
}
|
|
|
|
$this->flashMessage('Heslo bylo změněno.', 'success');
|
|
$this->redirect('this');
|
|
}
|
|
|
|
private function replaceIdentityEmail(string $email): void
|
|
{
|
|
$identity = $this->getUser()->getIdentity();
|
|
$data = $identity->getData();
|
|
$data['email'] = $email;
|
|
$this->getUser()->login(new SimpleIdentity($identity->getId(), $identity->getRoles(), $data));
|
|
}
|
|
}
|