1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_tree_controls\Controls; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_forms\Controls; |
7
|
|
|
use kalanis\kw_paths\Interfaces\IPaths; |
8
|
|
|
use kalanis\kw_tree\Essentials\FileNode; |
9
|
|
|
use kalanis\kw_tree_controls\ControlNode; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ATreeControl |
14
|
|
|
* @package kalanis\kw_tree_controls\Controls |
15
|
|
|
*/ |
16
|
|
|
abstract class ATreeControl extends Controls\AControl |
17
|
|
|
{ |
18
|
|
|
protected string $templateInput = '%1$s'; // by our own! |
19
|
|
|
protected ?ControlNode $tree = null; |
20
|
|
|
/** @var Controls\AControl[]|Controls\Checkbox[]|Controls\Radio[]|Controls\SelectOption[] */ |
21
|
|
|
protected array $inputs = []; |
22
|
|
|
protected bool $wantEmptySub = true; |
23
|
|
|
|
24
|
7 |
|
public function set(string $key, string $value = '', string $label = '', ?FileNode $tree = null, bool $wantRootControl = true): self |
25
|
|
|
{ |
26
|
7 |
|
$this->setEntry($key, $value, $label); |
27
|
7 |
|
$this->tree = $this->fillTreeControl($tree, $wantRootControl); |
28
|
7 |
|
$this->setValue($value); |
29
|
7 |
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
6 |
|
public function renderInput($attributes = null): string |
33
|
|
|
{ |
34
|
6 |
|
$this->addAttributes($attributes); |
35
|
6 |
|
return $this->wrapIt(sprintf($this->templateInput, $this->renderTree($this->tree)), $this->wrappersInput); |
36
|
|
|
} |
37
|
|
|
|
38
|
7 |
|
protected function fillTreeControl(?FileNode $baseNode, bool $wantRootControl): ?ControlNode |
39
|
|
|
{ |
40
|
7 |
|
if (!$baseNode) { |
41
|
7 |
|
return null; |
42
|
|
|
} |
43
|
6 |
|
$node = $this->getControlNode(); |
44
|
6 |
|
if ($wantRootControl) { |
45
|
6 |
|
$node->setControl($this->getInput($baseNode)); |
46
|
6 |
|
} elseif ($this->wantEmptySub) { |
47
|
4 |
|
$node->setControl($this->getEmpty($baseNode)); |
48
|
|
|
} // no else -> no control |
49
|
6 |
|
$node->setNode($baseNode); |
50
|
6 |
|
foreach ($baseNode->getSubNodes() as $subNode) { |
51
|
6 |
|
if ($subControl = $this->fillTreeControl($subNode, true)) { |
52
|
6 |
|
$node->addSubNode($subControl); |
53
|
|
|
} |
54
|
|
|
} |
55
|
6 |
|
return $node; |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
protected function getControlNode(): ControlNode |
59
|
|
|
{ |
60
|
6 |
|
return new ControlNode(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
abstract protected function getInput(FileNode $node): Controls\AControl; |
64
|
|
|
|
65
|
4 |
|
protected function getEmpty(FileNode $node): Controls\AControl |
66
|
|
|
{ |
67
|
4 |
|
$input = new EmptyControl(); |
68
|
4 |
|
$input->set($this->getKey(), null, $this->stringName($node)); |
69
|
4 |
|
return $input; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
abstract protected function renderTree(?ControlNode $baseNode): string; |
73
|
|
|
|
74
|
6 |
|
protected function stringName(?FileNode $node): string |
75
|
|
|
{ |
76
|
6 |
|
if (is_null($node)) { |
77
|
1 |
|
return ''; |
78
|
|
|
} |
79
|
6 |
|
$path = $node->getPath(); |
80
|
6 |
|
$last = end($path); |
81
|
6 |
|
return (false !== $last) ? strval($last) : IPaths::SPLITTER_SLASH; |
82
|
|
|
} |
83
|
|
|
|
84
|
7 |
|
protected function stringPath(?FileNode $node): string |
85
|
|
|
{ |
86
|
7 |
|
return is_null($node) |
87
|
1 |
|
? IPaths::SPLITTER_SLASH |
88
|
7 |
|
: implode(IPaths::SPLITTER_SLASH, $node->getPath()); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|