1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_tree_controls\Controls; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_forms\Controls\AControl; |
7
|
|
|
use kalanis\kw_forms\Controls\SelectOption; |
8
|
|
|
use kalanis\kw_forms\Exceptions\RenderException; |
9
|
|
|
use kalanis\kw_templates\HtmlElement; |
10
|
|
|
use kalanis\kw_tree\Essentials\FileNode; |
11
|
|
|
use kalanis\kw_tree_controls\ControlNode; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class FileSelect |
16
|
|
|
* @package kalanis\kw_tree_controls\Controls |
17
|
|
|
*/ |
18
|
|
|
class FileSelect extends ATreeControl |
19
|
|
|
{ |
20
|
|
|
use TSimpleValue; |
21
|
|
|
|
22
|
|
|
protected bool $wantEmptySub = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param ControlNode|null $baseNode |
26
|
|
|
* @throws RenderException |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
1 |
|
protected function renderTree(?ControlNode $baseNode): string |
30
|
|
|
{ |
31
|
1 |
|
if (empty($baseNode)) { |
32
|
1 |
|
return ''; |
33
|
|
|
} |
34
|
1 |
|
$select = HtmlElement::init('select'); |
35
|
1 |
|
$select->setAttribute('name', $this->key); |
36
|
1 |
|
$select->addChild($this->getOptionGroup($baseNode)); |
37
|
1 |
|
return $select->render(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param ControlNode $node |
42
|
|
|
* @throws RenderException |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
1 |
|
protected function getOptionGroup(ControlNode $node): string |
46
|
|
|
{ |
47
|
1 |
|
if ($node->getNode() && $node->getNode()->isDir()) { |
48
|
1 |
|
$group = HtmlElement::init('optgroup', ['label' => $this->stringPath($node->getNode())]); |
49
|
1 |
|
foreach ($node->getSubNodes() as $subNode) { |
50
|
1 |
|
$group->addChild($this->getOptionGroup($subNode)); |
51
|
|
|
} |
52
|
1 |
|
return strval($group); |
53
|
|
|
} else { |
54
|
1 |
|
return $this->getOption($node); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param ControlNode $node |
60
|
|
|
* @throws RenderException |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
1 |
|
protected function getOption(ControlNode $node): string |
64
|
|
|
{ |
65
|
1 |
|
return $node->getControl() ? $node->getControl()->render() : ''; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
protected function getInput(FileNode $node): AControl |
69
|
|
|
{ |
70
|
1 |
|
$input = new SelectOption(); |
71
|
1 |
|
$input->setEntry($this->getKey(), $this->stringPath($node), $this->stringName($node)); |
72
|
1 |
|
$this->inputs[] = $input; |
73
|
1 |
|
return $input; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|