1 | <?php |
||
14 | class ActionPanelRenderer implements WebRenderer { |
||
15 | |||
16 | /** @var RendererRegistry */ |
||
17 | private $renderers; |
||
18 | |||
19 | /** @var ActionRegistry */ |
||
20 | private $actions; |
||
21 | |||
22 | private $results = []; |
||
23 | |||
24 | /** @var FieldRegistry */ |
||
25 | private $fields; |
||
26 | |||
27 | /** |
||
28 | * @param RendererRegistry $renderers |
||
29 | * @param ActionRegistry $actions |
||
30 | * @param FieldRegistry $fields |
||
31 | */ |
||
32 | public function __construct(RendererRegistry $renderers, ActionRegistry $actions, FieldRegistry $fields) { |
||
33 | $this->renderers = $renderers; |
||
34 | $this->actions = $actions; |
||
35 | $this->fields = $fields; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @param mixed $value |
||
40 | * @return bool |
||
41 | */ |
||
42 | public function handles($value) { |
||
43 | return $value instanceof ActionPanel; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param ActionPanel $value |
||
48 | * @return mixed |
||
49 | */ |
||
50 | public function render($value) { |
||
51 | $heading = $this->actions->getAction($value->getActionId())->caption(); |
||
52 | return (string)(new Panel($heading, $this->getContent($value))) |
||
53 | ->setMaxHeight($value->getMaxHeight()) |
||
54 | ->setRightHeading([new Element('a', [ |
||
55 | 'href' => (string)Url::relative($value->getActionId(), $value->getParameters()) |
||
56 | ], [new Element('span', ['class' => 'glyphicon glyphicon-circle-arrow-right'])])]) |
||
57 | ->render($this->renderers); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param mixed $value |
||
62 | * @return array|Element[] |
||
63 | */ |
||
64 | public function headElements($value) { |
||
65 | $content = $this->getContent($value); |
||
66 | $renderer = $this->renderers->getRenderer($content); |
||
67 | if ($renderer instanceof WebRenderer) { |
||
68 | return $renderer->headElements($content); |
||
69 | } |
||
70 | return []; |
||
71 | } |
||
72 | |||
73 | private function getContent(ActionPanel $value) { |
||
74 | $key = spl_object_hash($value); |
||
75 | |||
76 | if (!isset($this->results[$key])) { |
||
77 | $action = $this->actions->getAction($value->getActionId()); |
||
78 | $this->results[$key] = $action->execute($this->inflate($action->parameters(), $value->getParameters())); |
||
79 | } |
||
80 | return $this->results[$key]; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param Parameter[] $parameters |
||
85 | * @param mixed[] $values |
||
86 | * @return mixed[] |
||
87 | */ |
||
88 | private function inflate($parameters, $values) { |
||
98 | } |