1
|
|
|
<?php |
2
|
|
|
namespace rtens\domin\delivery\web\fields; |
3
|
|
|
|
4
|
|
|
use rtens\domin\delivery\web\Element; |
5
|
|
|
use rtens\domin\delivery\web\renderers\FileRenderer; |
6
|
|
|
use rtens\domin\delivery\web\WebField; |
7
|
|
|
use rtens\domin\Parameter; |
8
|
|
|
use rtens\domin\parameters\File; |
9
|
|
|
use rtens\domin\parameters\file\MemoryFile; |
10
|
|
|
use rtens\domin\parameters\file\SavedFile; |
11
|
|
|
use watoki\reflect\type\ClassType; |
12
|
|
|
|
13
|
|
|
class FileField implements WebField { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param Parameter $parameter |
17
|
|
|
* @return bool |
18
|
|
|
*/ |
19
|
|
|
public function handles(Parameter $parameter) { |
20
|
|
|
return $parameter->getType() == new ClassType(File::class); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Parameter $parameter |
25
|
|
|
* @param array[]|string[] $serialized |
26
|
|
|
* @return null|File |
27
|
|
|
*/ |
28
|
|
|
public function inflate(Parameter $parameter, $serialized) { |
29
|
|
|
$file = $serialized['file']; |
30
|
|
|
|
31
|
|
|
if ($file && !$file['error']) { |
32
|
|
|
return new SavedFile($file['tmp_name'], $file['name'], $file['type']); |
33
|
|
View Code Duplication |
} else if (isset($serialized['name']) && $serialized['name']) { |
|
|
|
|
34
|
|
|
return $this->createPreservedFile($serialized); |
|
|
|
|
35
|
|
|
} else { |
36
|
|
|
return null; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string[] $serialized |
42
|
|
|
* @return File |
43
|
|
|
*/ |
44
|
|
|
protected function createPreservedFile($serialized) { |
45
|
|
|
return new MemoryFile($serialized['name'], $serialized['type'], base64_decode($serialized['data'])); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Parameter $parameter |
50
|
|
|
* @param File|null $value |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function render(Parameter $parameter, $value) { |
54
|
|
|
return (string)new Element('div', [], [ |
55
|
|
|
$this->renderImagePreservation($parameter, $value), |
56
|
|
|
new ELement('label', [], [ |
57
|
|
|
new Element('div', ['class' => 'input-group file-field'], [ |
58
|
|
|
new Element('span', ['class' => 'input-group-btn'], [ |
59
|
|
|
new Element('span', ['class' => 'btn btn-success'], ['Choose File']), |
60
|
|
|
new Element("input", array_merge([ |
61
|
|
|
'class' => 'sr-only file-input', |
62
|
|
|
'type' => 'file', |
63
|
|
|
'name' => $parameter->getName() . '[file]' |
64
|
|
|
], $parameter->isRequired() && is_null($value) ? [ |
65
|
|
|
'required' => 'required' |
66
|
|
|
] : [])) |
67
|
|
|
]), |
68
|
|
|
new Element('span', ['class' => 'form-control file-name']) |
69
|
|
|
]) |
70
|
|
|
]) |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Parameter $parameter |
76
|
|
|
* @param File|null $file |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
protected function renderImagePreservation(Parameter $parameter, File $file = null) { |
80
|
|
|
if ($file === null) { |
81
|
|
|
return ''; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return (string)new Element('p', [], [ |
85
|
|
|
new Element('input', [ |
86
|
|
|
'type' => 'hidden', |
87
|
|
|
'name' => $parameter->getName() . '[name]', |
88
|
|
|
'value' => $file->getName() |
89
|
|
|
]), |
90
|
|
|
new Element('input', [ |
91
|
|
|
'type' => 'hidden', |
92
|
|
|
'name' => $parameter->getName() . '[type]', |
93
|
|
|
'value' => $file->getType() |
94
|
|
|
]), |
95
|
|
|
new Element('input', [ |
96
|
|
|
'type' => 'hidden', |
97
|
|
|
'name' => $parameter->getName() . '[data]', |
98
|
|
|
'value' => base64_encode($file->getContent()) |
99
|
|
|
]), |
100
|
|
|
(new FileRenderer())->render($file) |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Parameter $parameter |
106
|
|
|
* @return array|Element[] |
107
|
|
|
*/ |
108
|
|
|
public function headElements(Parameter $parameter) { |
109
|
|
|
return [ |
110
|
|
|
new Element('script', [], [" |
111
|
|
|
$(function () { |
112
|
|
|
$('.file-input').change(function (e) { |
113
|
|
|
$(this).closest('.file-field').find('.file-name').html($(this)[0].files[0].name); |
114
|
|
|
}); |
115
|
|
|
}); |
116
|
|
|
"]) |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.