|
1
|
|
|
<?php |
|
2
|
|
|
namespace rtens\domin\delivery\web\renderers\link; |
|
3
|
|
|
|
|
4
|
|
|
use rtens\domin\ActionRegistry; |
|
5
|
|
|
use rtens\domin\delivery\web\Element; |
|
6
|
|
|
use rtens\domin\delivery\web\resources\ExecutionResource; |
|
7
|
|
|
use rtens\domin\delivery\web\WebCommentParser; |
|
8
|
|
|
|
|
9
|
|
|
class LinkPrinter { |
|
10
|
|
|
|
|
11
|
|
|
/** @var LinkRegistry */ |
|
12
|
|
|
private $links; |
|
13
|
|
|
|
|
14
|
|
|
/** @var ActionRegistry */ |
|
15
|
|
|
private $actions; |
|
16
|
|
|
|
|
17
|
|
|
/** @var WebCommentParser */ |
|
18
|
|
|
private $parser; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(LinkRegistry $links, ActionRegistry $actions, WebCommentParser $parser) { |
|
21
|
|
|
$this->links = $links; |
|
22
|
|
|
$this->actions = $actions; |
|
23
|
|
|
$this->parser = $parser; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param mixed $object |
|
28
|
|
|
* @return array |
|
29
|
|
|
*/ |
|
30
|
|
|
public function createLinkElements($object) { |
|
31
|
|
|
return $this->createLinks($object, 'btn btn-xs btn-primary'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param mixed $object |
|
36
|
|
|
* @param string|null $caption |
|
37
|
|
|
* @return array|\rtens\domin\delivery\web\Element[] |
|
38
|
|
|
*/ |
|
39
|
|
|
public function createDropDown($object, $caption = null) { |
|
40
|
|
|
$links = $this->createLinks($object); |
|
41
|
|
|
if (empty($links)) { |
|
42
|
|
|
return []; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (!$caption) { |
|
|
|
|
|
|
46
|
|
|
if (is_object($object)) { |
|
47
|
|
|
$caption = (new \ReflectionClass($object))->getShortName(); |
|
48
|
|
|
} else { |
|
49
|
|
|
$caption = 'Actions'; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return [ |
|
54
|
|
|
new Element('div', ['class' => 'dropdown'], [ |
|
55
|
|
|
new Element('button', [ |
|
56
|
|
|
'class' => 'btn btn-xs btn-primary dropdown-toggle', |
|
57
|
|
|
'type' => 'button', |
|
58
|
|
|
'data-toggle' => 'dropdown', |
|
59
|
|
|
'aria-haspopup' => 'true', |
|
60
|
|
|
'aria-expanded' => 'false' |
|
61
|
|
|
], [ |
|
62
|
|
|
$caption, |
|
63
|
|
|
new Element('span', ['class' => 'caret']) |
|
64
|
|
|
]), |
|
65
|
|
|
new Element('ul', ['class' => 'dropdown-menu'], array_map(function (Element $element) { |
|
66
|
|
|
return new Element('li', [], [$element]); |
|
67
|
|
|
}, $links)) |
|
68
|
|
|
]) |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function createLinks($object, $classes = '') { |
|
73
|
|
|
return array_map(function (Link $link) use ($object, $classes) { |
|
74
|
|
|
$action = $this->actions->getAction($link->actionId()); |
|
75
|
|
|
|
|
76
|
|
|
$parameters = $link->parameters($object); |
|
77
|
|
|
if ($link->force()) { |
|
78
|
|
|
$parameters[ExecutionResource::FORCE_ARG] = true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$attributes = ['class' => $classes, 'href' => $this->makeUrl($link->actionId(), $parameters)]; |
|
82
|
|
|
if ($link->confirm() !== null) { |
|
83
|
|
|
$attributes['onclick'] = "return confirm('{$link->confirm()}');"; |
|
84
|
|
|
} |
|
85
|
|
|
$description = $action->description(); |
|
86
|
|
|
if (!is_null($description)) { |
|
87
|
|
|
$attributes['title'] = str_replace('"', "'", strip_tags($this->parser->shorten($description))); |
|
88
|
|
|
} |
|
89
|
|
|
return new Element('a', $attributes, [ |
|
90
|
|
|
$action->caption() |
|
91
|
|
|
]); |
|
92
|
|
|
}, $this->links->getLinks($object)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
private function makeUrl($actionId, array $parameters) { |
|
|
|
|
|
|
96
|
|
|
$url = $actionId; |
|
97
|
|
|
|
|
98
|
|
|
if ($parameters) { |
|
|
|
|
|
|
99
|
|
|
$keyValues = []; |
|
100
|
|
|
foreach ($parameters as $key => $value) { |
|
101
|
|
|
$keyValues[] = urlencode($key) . '=' . urlencode($value); |
|
102
|
|
|
} |
|
103
|
|
|
$url .= '?' . implode('&', $keyValues); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $url; |
|
107
|
|
|
} |
|
108
|
|
|
} |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: