|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Utilitte\Nette\Utility; |
| 4 | + |
| 5 | +use Nette\Application\UI\Control; |
| 6 | +use Nette\Application\UI\Form; |
| 7 | +use Nette\Application\UI\Link; |
| 8 | +use Nette\Forms\Controls\BaseControl; |
| 9 | + |
| 10 | +final class FormUtility |
| 11 | +{ |
| 12 | + |
| 13 | + private Control $control; |
| 14 | + |
| 15 | + private Form $form; |
| 16 | + |
| 17 | + /** @var callable[] */ |
| 18 | + private array $onSuccess = []; |
| 19 | + |
| 20 | + /** @var callable[] */ |
| 21 | + private array $onError = []; |
| 22 | + |
| 23 | + /** @var bool[] */ |
| 24 | + private array $useChecker = []; |
| 25 | + |
| 26 | + public function __construct(Control $control, Form $form) |
| 27 | + { |
| 28 | + $this->control = $control; |
| 29 | + $this->form = $form; |
| 30 | + |
| 31 | + $form->onAnchor[] = function () { |
| 32 | + $this->form->onSuccess[] = fn () => $this->onSuccess(); |
| 33 | + $this->form->onError[] = fn () => $this->onError(); |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + public function successFlashMessage(string $message): self |
| 38 | + { |
| 39 | + $this->onSuccess[] = fn () => $this->control->flashMessage($message); |
| 40 | + |
| 41 | + return $this; |
| 42 | + } |
| 43 | + |
| 44 | + public function errorsToFlashMessages(): self |
| 45 | + { |
| 46 | + $this->checkUse(__METHOD__); |
| 47 | + |
| 48 | + $this->onError[] = function (): void { |
| 49 | + foreach ($this->form->getOwnErrors() as $error) { |
| 50 | + $this->control->flashMessage($error, 'error'); |
| 51 | + } |
| 52 | + |
| 53 | + /** @var BaseControl $control */ |
| 54 | + foreach ($this->form->getControls() as $control) { |
| 55 | + foreach ($control->getErrors() as $error) { |
| 56 | + $this->control->flashMessage($control->caption . ': ' . $error, 'error'); |
| 57 | + } |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + return $this; |
| 62 | + } |
| 63 | + |
| 64 | + public function redirectWithBacklink(string $parameterName = 'backlink'): self |
| 65 | + { |
| 66 | + $this->checkUse('redirect'); |
| 67 | + |
| 68 | + $backlink = $this->control->getParameter($parameterName); |
| 69 | + |
| 70 | + if (!$backlink) { |
| 71 | + return $this; |
| 72 | + } |
| 73 | + |
| 74 | + $this->form->onAnchor[] = function () use ($backlink): void { |
| 75 | + $link = $this->form->getAction(); |
| 76 | + |
| 77 | + if ($link instanceof Link) { |
| 78 | + $link->setParameter('backlink', $backlink); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + $this->onSuccess[] = fn () => $this->control->getPresenter()->redirectUrl($backlink); |
| 83 | + |
| 84 | + return $this; |
| 85 | + } |
| 86 | + |
| 87 | + public function redirectOnSuccess(string $destination, array $args = [], ?Control $control = null) { |
| 88 | + $this->checkUse('redirect'); |
| 89 | + |
| 90 | + $this->onSuccess[] = fn () => ($control ?? $this->control)->redirect($destination, $args); |
| 91 | + |
| 92 | + return $this; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @param string[] $snippets |
| 97 | + */ |
| 98 | + public function refreshOnSuccess(array $snippets = []): self |
| 99 | + { |
| 100 | + $this->checkUse('redirect'); |
| 101 | + |
| 102 | + $presenter = $this->control->getPresenterIfExists(); |
| 103 | + if ($presenter && $presenter->isAjax()) { |
| 104 | + $this->onSuccess[] = function () use ($snippets): void { |
| 105 | + foreach ($snippets as $snippet) { |
| 106 | + $this->control->redrawControl($snippet); |
| 107 | + } |
| 108 | + }; |
| 109 | + } else { |
| 110 | + $this->onSuccess[] = fn () => $this->control->redirect('this'); |
| 111 | + } |
| 112 | + |
| 113 | + return $this; |
| 114 | + } |
| 115 | + |
| 116 | + private function checkUse(string $name): void { |
| 117 | + if (isset($this->useChecker[$name])) { |
| 118 | + throw new \LogicException('Cannot call ' . $name . ' twice.'); |
| 119 | + } |
| 120 | + |
| 121 | + $this->useChecker[$name] = true; |
| 122 | + } |
| 123 | + |
| 124 | + private function onSuccess(): void |
| 125 | + { |
| 126 | + foreach ($this->onSuccess as $success) { |
| 127 | + $success(); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private function onError(): void |
| 132 | + { |
| 133 | + foreach ($this->onError as $onError) { |
| 134 | + $onError(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments