Skip to content

Commit 8253294

Browse files
committed
add presenter traits
1 parent 03dd01f commit 8253294

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Utilitte\Nette\UI\Presenter;
4+
5+
trait PresenterCheckParametersTrait
6+
{
7+
8+
/**
9+
* @param array<string, mixed> $values name => value
10+
*/
11+
protected function checkParameters(array $values): void {
12+
$parameters = $this->getParameters();
13+
$redirect = false;
14+
15+
foreach ($values as $name => $value) {
16+
if (!isset($parameters[$name]) || $parameters[$name] !== Strings::webalize((string) $value)) {
17+
$parameters[$name] = Strings::webalize((string) $value);
18+
$redirect = true;
19+
}
20+
}
21+
22+
if ($redirect) {
23+
$this->redirectPermanent('this', $parameters);
24+
}
25+
}
26+
27+
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Utilitte\Nette\UI\Presenter;
4+
5+
trait RedrawControlTrait
6+
{
7+
8+
/**
9+
* @param string|string[] $snippets
10+
* @param mixed[] $args
11+
*/
12+
public function redraw($snippets = null, string $link = 'this', array $args = []): void
13+
{
14+
if ($this->isAjax()) {
15+
foreach ((array) $snippets as $snippet) {
16+
$this->redrawControl($snippet);
17+
}
18+
} else {
19+
$this->redirect($link, $args);
20+
}
21+
}
22+
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Utilitte\Nette\UI\Presenter;
4+
5+
use Nette\HtmlStringable;
6+
use stdClass;
7+
8+
trait SuccessFlashMessageTrait
9+
{
10+
11+
/**
12+
* @param string|stdClass|HtmlStringable $message
13+
*/
14+
public function flashMessage($message, string $type = 'success'): stdClass
15+
{
16+
return parent::flashMessage($message, $type);
17+
}
18+
19+
}

0 commit comments

Comments
 (0)