|
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\root\ExecuteResource; |
|
7
|
|
|
use rtens\domin\delivery\web\WebCommentParser; |
|
8
|
|
|
use watoki\collections\Map; |
|
9
|
|
|
use watoki\curir\protocol\Url; |
|
10
|
|
|
|
|
11
|
|
|
class LinkPrinter { |
|
12
|
|
|
|
|
13
|
|
|
/** @var \watoki\curir\protocol\Url */ |
|
14
|
|
|
private $baseUrl; |
|
15
|
|
|
|
|
16
|
|
|
/** @var LinkRegistry */ |
|
17
|
|
|
private $links; |
|
18
|
|
|
|
|
19
|
|
|
/** @var ActionRegistry */ |
|
20
|
|
|
private $actions; |
|
21
|
|
|
|
|
22
|
|
|
/** @var WebCommentParser */ |
|
23
|
|
|
private $parser; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(Url $baseUrl, LinkRegistry $links, ActionRegistry $actions, WebCommentParser $parser) { |
|
26
|
|
|
$this->baseUrl = $baseUrl; |
|
27
|
|
|
$this->links = $links; |
|
28
|
|
|
$this->actions = $actions; |
|
29
|
|
|
$this->parser = $parser; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param mixed $object |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
public function createLinkElements($object) { |
|
37
|
|
|
return $this->createLinks($object, 'btn btn-xs btn-primary'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param mixed $object |
|
42
|
|
|
* @param string|null $caption |
|
43
|
|
|
* @return array|\rtens\domin\delivery\web\Element[] |
|
44
|
|
|
*/ |
|
45
|
|
|
public function createDropDown($object, $caption = null) { |
|
46
|
|
|
$links = $this->createLinks($object); |
|
47
|
|
|
if (empty($links)) { |
|
48
|
|
|
return []; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (!$caption) { |
|
|
|
|
|
|
52
|
|
|
if (is_object($object)) { |
|
53
|
|
|
$caption = (new \ReflectionClass($object))->getShortName(); |
|
54
|
|
|
} else { |
|
55
|
|
|
$caption = 'Actions'; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return [ |
|
60
|
|
|
new Element('div', ['class' => 'dropdown'], [ |
|
61
|
|
|
new Element('button', [ |
|
62
|
|
|
'class' => 'btn btn-xs btn-primary dropdown-toggle', |
|
63
|
|
|
'type' => 'button', |
|
64
|
|
|
'data-toggle' => 'dropdown', |
|
65
|
|
|
'aria-haspopup' => 'true', |
|
66
|
|
|
'aria-expanded' => 'false' |
|
67
|
|
|
], [ |
|
68
|
|
|
$caption, |
|
69
|
|
|
new Element('span', ['class' => 'caret']) |
|
70
|
|
|
]), |
|
71
|
|
|
new Element('ul', ['class' => 'dropdown-menu'], array_map(function (Element $element) { |
|
72
|
|
|
return new Element('li', [], [$element]); |
|
73
|
|
|
}, $links)) |
|
74
|
|
|
]) |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function createLinks($object, $classes = '') { |
|
79
|
|
|
return array_map(function (Link $link) use ($object, $classes) { |
|
80
|
|
|
$action = $this->actions->getAction($link->actionId()); |
|
81
|
|
|
|
|
82
|
|
|
$url = $this->baseUrl |
|
83
|
|
|
->appended($link->actionId()) |
|
84
|
|
|
->withParameters(new Map($link->parameters($object))); |
|
85
|
|
|
|
|
86
|
|
|
if ($link->force()) { |
|
87
|
|
|
$url = $url->withParameter(ExecuteResource::FORCE_ARG, true); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$attributes = ['class' => $classes, 'href' => $url]; |
|
91
|
|
|
if ($link->confirm() !== null) { |
|
92
|
|
|
$attributes['onclick'] = "return confirm('{$link->confirm()}');"; |
|
93
|
|
|
} |
|
94
|
|
|
$description = $action->description(); |
|
95
|
|
|
if (!is_null($description)) { |
|
96
|
|
|
$attributes['title'] = str_replace('"', "'", strip_tags($this->parser->shorten($description))); |
|
97
|
|
|
} |
|
98
|
|
|
return new Element('a', $attributes, [ |
|
99
|
|
|
$action->caption() |
|
100
|
|
|
]); |
|
101
|
|
|
}, $this->links->getLinks($object)); |
|
102
|
|
|
} |
|
103
|
|
|
} |
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: