|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Utilitte\Nette\UI\Presenter; |
| 4 | + |
| 5 | +use Nette\Application\UI\Link; |
| 6 | +use Nette\Application\UI\Presenter; |
| 7 | +use Nette\Http\Url; |
| 8 | + |
| 9 | +trait PresenterBackLinkTrait |
| 10 | +{ |
| 11 | + |
| 12 | + /** |
| 13 | + * @param mixed[] $params |
| 14 | + */ |
| 15 | + public function linkWithBacklink(Link $link, string $parameterName = 'backlink'): string |
| 16 | + { |
| 17 | + $link->setParameter($parameterName, $link->getComponent()->link('this', [$parameterName => null])); |
| 18 | + |
| 19 | + return (string) $link; |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * @return never |
| 24 | + */ |
| 25 | + public function redirectWithBacklink(Link $link, string $parameterName = 'backlink'): void |
| 26 | + { |
| 27 | + $link->setParameter($parameterName, $link->getComponent()->link('this', [$parameterName => null])); |
| 28 | + |
| 29 | + $link->getComponent()->redirect($link->getDestination(), $link->getParameters()); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @param mixed[] $params |
| 34 | + */ |
| 35 | + public function tryRedirectBack(string $parameterName = 'backlink'): void |
| 36 | + { |
| 37 | + $backlink = $this->getParameter($parameterName); |
| 38 | + |
| 39 | + if ($backlink) { |
| 40 | + $this->redirectUrlWithFlashKey($backlink); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @return never |
| 46 | + */ |
| 47 | + public function redirectUrlWithFlashKey(string $url): void |
| 48 | + { |
| 49 | + $url = new Url($url); |
| 50 | + |
| 51 | + if (!$url->getQueryParameter(Presenter::FLASH_KEY)) { |
| 52 | + $flashKey = $this->getParameter(Presenter::FLASH_KEY); |
| 53 | + if (is_string($flashKey) && $flashKey !== '') { |
| 54 | + $url->setQueryParameter(Presenter::FLASH_KEY, $flashKey); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + $this->redirectUrl((string) $url); |
| 59 | + } |
| 60 | + |
| 61 | +} |
0 commit comments