|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_forms\Controls; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_forms\Exceptions\EntryException; |
|
7
|
|
|
use kalanis\kw_forms\Exceptions\RenderException; |
|
8
|
|
|
use kalanis\kw_forms\Interfaces\IMultiValue; |
|
9
|
|
|
use kalanis\kw_input\Interfaces\IFileEntry; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Definition of form controls Group of Files |
|
14
|
|
|
* |
|
15
|
|
|
* <b>Examples</b> |
|
16
|
|
|
* <code> |
|
17
|
|
|
* // render form for upload 5 files |
|
18
|
|
|
* $form = new Form(); |
|
19
|
|
|
* $form->addFiles('fotos', 'Select files', 5); |
|
20
|
|
|
* echo $form; |
|
21
|
|
|
* |
|
22
|
|
|
* // render form for upload 5 files with defined labels |
|
23
|
|
|
* $labels = array('file 1','file 2','file 3','file 4','file 5'); |
|
24
|
|
|
* $form = new Form(); |
|
25
|
|
|
* $form->addFiles('fotos', $labels, 5)->setLabel('Select files'); |
|
26
|
|
|
* echo $form; |
|
27
|
|
|
* |
|
28
|
|
|
* // render form for upload 5 files with defined labels |
|
29
|
|
|
* $form = new Form(); |
|
30
|
|
|
* for($i=1;$i<6;$i++) { |
|
31
|
|
|
* $files[] = new Controls\File(null, null, 'File '.$i); |
|
32
|
|
|
* } |
|
33
|
|
|
* $form->addFiles('fotos', 'Select files', $files); |
|
34
|
|
|
* echo $form; |
|
35
|
|
|
* </code> |
|
36
|
|
|
*/ |
|
37
|
|
|
class Files extends AControl implements IMultiValue |
|
38
|
|
|
{ |
|
39
|
|
|
use TShorterKey; |
|
40
|
|
|
|
|
41
|
|
|
protected string $templateLabel = '<label>%2$s</label>'; |
|
42
|
|
|
protected string $templateInput = '%3$s'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string $alias |
|
46
|
|
|
* @param iterable<string|int, string> $items |
|
47
|
|
|
* @param string $label |
|
48
|
|
|
* @param array<string, string|array<string>>|string $attributes |
|
49
|
|
|
* @return $this |
|
50
|
|
|
*/ |
|
51
|
6 |
|
public function set(string $alias, iterable $items = [], string $label = '', $attributes = []): self |
|
52
|
|
|
{ |
|
53
|
6 |
|
$this->alias = $alias; |
|
54
|
6 |
|
$this->setLabel($label); |
|
55
|
6 |
|
foreach ($items as $key => $item) { |
|
56
|
4 |
|
$al = is_numeric($key) ? sprintf('%s[]', $alias) : sprintf('%s[%s]', $alias, strval($key)); |
|
57
|
4 |
|
$this->addFile($al, $item, $attributes); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
6 |
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Add File input |
|
64
|
|
|
* @param string $label |
|
65
|
|
|
* @param string $key |
|
66
|
|
|
* @param array<string, string|array<string>>|string $attributes |
|
67
|
|
|
*/ |
|
68
|
4 |
|
public function addFile(string $key, string $label = '', $attributes = []): void |
|
69
|
|
|
{ |
|
70
|
4 |
|
$formFile = new File(); |
|
71
|
4 |
|
$formFile->set($key, $label)->setAttributes($attributes); |
|
72
|
4 |
|
$this->addChild($formFile); |
|
73
|
4 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param array<string, string|int|float|IFileEntry|null> $data |
|
77
|
|
|
*/ |
|
78
|
3 |
|
public function setValues(array $data = []): void |
|
79
|
|
|
{ |
|
80
|
3 |
|
foreach ($this->children as $child) { |
|
81
|
2 |
|
if ($child instanceof File) { |
|
82
|
2 |
|
$shortKey = $this->shorterKey($child->getKey()); |
|
83
|
2 |
|
$child->setValue( |
|
84
|
2 |
|
$data[$shortKey] ?? ( |
|
85
|
2 |
|
$data[$child->getKey()] ?? '' |
|
86
|
|
|
) |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
3 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @throws EntryException |
|
94
|
|
|
* @return array<string, IFileEntry> |
|
95
|
|
|
*/ |
|
96
|
2 |
|
public function getValues(): array |
|
97
|
|
|
{ |
|
98
|
2 |
|
$result = []; |
|
99
|
2 |
|
foreach ($this->children as $child) { |
|
100
|
1 |
|
if ($child instanceof File) { |
|
101
|
1 |
|
$result[$child->getKey()] = $child->getFile(); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
2 |
|
return $result; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Render all sub-controls and wrap it all |
|
109
|
|
|
* @throws RenderException |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
4 |
|
public function renderChildren(): string |
|
113
|
|
|
{ |
|
114
|
4 |
|
$return = ''; |
|
115
|
4 |
|
foreach ($this->children as $alias => $child) { |
|
116
|
4 |
|
if ($child instanceof AControl) { |
|
117
|
4 |
|
$child->setAttribute('id', $this->getAlias() . '_' . $alias); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
4 |
|
$return .= $this->wrapIt($child->render(), $this->wrappersChild) . PHP_EOL; |
|
121
|
|
|
} |
|
122
|
4 |
|
return $this->wrapIt($return, $this->wrappersChildren); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|