|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) 2015 ublaboo <[email protected]> |
|
5
|
|
|
* @author Pavel Janda <[email protected]> |
|
6
|
|
|
* @package Ublaboo |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Ublaboo\DataGrid\Traits; |
|
10
|
|
|
|
|
11
|
|
|
use Nette; |
|
12
|
|
|
use Nette\Application\UI\InvalidLinkException; |
|
13
|
|
|
use Nette\Application\UI\Presenter; |
|
14
|
|
|
use Ublaboo\DataGrid\DataGrid; |
|
15
|
|
|
use Ublaboo\DataGrid\Exception\DataGridHasToBeAttachedToPresenterComponentException; |
|
16
|
|
|
use Ublaboo\DataGrid\Exception\DataGridLinkCreationException; |
|
17
|
|
|
|
|
18
|
1 |
|
trait TLink |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Create link to custom destination |
|
23
|
|
|
* @param DataGrid $grid |
|
24
|
|
|
* @param string $href |
|
25
|
|
|
* @param array $params |
|
26
|
|
|
* @return string |
|
27
|
|
|
* @throws DataGridHasToBeAttachedToPresenterComponentException |
|
28
|
|
|
* @throws \InvalidArgumentException |
|
29
|
|
|
* @throws DataGridLinkCreationException |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function createLink(DataGrid $grid, $href, $params) |
|
32
|
|
|
{ |
|
33
|
1 |
|
$targetComponent = $grid; |
|
34
|
|
|
|
|
35
|
1 |
|
if (strpos($href, ':') !== false) { |
|
36
|
|
|
return $grid->getPresenter()->link($href, $params); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
for ($iteration = 0; $iteration < 10; $iteration++) { |
|
40
|
1 |
|
$targetComponent = $targetComponent->getParent(); |
|
41
|
|
|
|
|
42
|
1 |
|
if ($targetComponent === null) { |
|
43
|
|
|
$this->throwHierarchyLookupException($grid, $href, $params); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
try { |
|
47
|
1 |
|
@$link = $targetComponent->link($href, $params); |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
} catch (InvalidLinkException $e) { |
|
|
|
|
|
|
50
|
|
|
$link = false; |
|
51
|
|
|
} catch (Nette\InvalidArgumentException $e) { |
|
|
|
|
|
|
52
|
|
|
$link = false; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
if ($link) { |
|
56
|
|
|
if ( |
|
57
|
1 |
|
strpos($link, '#error') === 0 || |
|
58
|
1 |
|
(strrpos($href, "!") !== false && strpos($link, '#') === 0) |
|
59
|
|
|
) { |
|
60
|
|
|
continue; // Did not find signal handler |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
return $link; // Found signal handler! |
|
64
|
|
|
} else { |
|
65
|
|
|
continue; // Did not find signal handler |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if ($targetComponent instanceof Presenter) { |
|
|
|
|
|
|
69
|
|
|
// Went the whole way up to the UI\Presenter and did not find any signal handler |
|
70
|
|
|
$this->throwHierarchyLookupException($grid, $href, $params); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// Went 10 steps up to the UI\Presenter and did not find any signal handler |
|
75
|
|
|
$this->throwHierarchyLookupException($grid, $href, $params); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @throws DataGridLinkCreationException |
|
81
|
|
|
*/ |
|
82
|
|
|
private function throwHierarchyLookupException(DataGrid $grid, $href, $params) |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
$desiredHandler = get_class($grid->getParent()) . '::handle' . ucfirst($href) . '()'; |
|
85
|
|
|
|
|
86
|
|
|
throw new DataGridLinkCreationException( |
|
87
|
|
|
'DataGrid could not create link "' |
|
88
|
|
|
. $href . '" - did not find any signal handler in componenet hierarchy from ' |
|
89
|
|
|
. get_class($grid->getParent()) . ' up to the ' |
|
90
|
|
|
. get_class($grid->getPresenter()) . '. ' |
|
91
|
|
|
. 'Try adding handler ' . $desiredHandler |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: