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 $templateLabel = '<label>%2$s</label>'; |
42
|
|
|
protected $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> $array |
77
|
|
|
*/ |
78
|
3 |
|
public function setValues(array $array = []): 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 |
|
isset($array[$shortKey]) |
85
|
|
|
// @codeCoverageIgnoreStart |
86
|
|
|
? $array[$shortKey] // should not happen - set prevents this |
87
|
|
|
// @codeCoverageIgnoreEnd |
88
|
|
|
: ( |
89
|
2 |
|
isset($array[$child->getKey()]) |
90
|
1 |
|
? $array[$child->getKey()] |
91
|
2 |
|
: '' |
92
|
|
|
) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
3 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @throws EntryException |
100
|
|
|
* @return array<string, IFileEntry> |
101
|
|
|
*/ |
102
|
2 |
|
public function getValues(): array |
103
|
|
|
{ |
104
|
2 |
|
$result = []; |
105
|
2 |
|
foreach ($this->children as $child) { |
106
|
1 |
|
if ($child instanceof File) { |
107
|
1 |
|
$result[$child->getKey()] = $child->getFile(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
2 |
|
return $result; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Render all sub-controls and wrap it all |
115
|
|
|
* @throws RenderException |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
4 |
|
public function renderChildren(): string |
119
|
|
|
{ |
120
|
4 |
|
$return = ''; |
121
|
4 |
|
foreach ($this->children as $alias => $child) { |
122
|
4 |
|
if ($child instanceof AControl) { |
123
|
4 |
|
$child->setAttribute('id', $this->getAlias() . '_' . $alias); |
124
|
|
|
} |
125
|
|
|
|
126
|
4 |
|
$return .= $this->wrapIt($child->render(), $this->wrappersChild) . PHP_EOL; |
127
|
|
|
} |
128
|
4 |
|
return $this->wrapIt($return, $this->wrappersChildren); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|