|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Del\Form\Renderer\Field; |
|
4
|
|
|
|
|
5
|
|
|
use Del\Form\Field\FieldInterface; |
|
6
|
|
|
use Del\Form\Field\FileUpload; |
|
7
|
|
|
use DOMElement; |
|
8
|
|
|
use DOMNode; |
|
9
|
|
|
use DOMText; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
|
|
12
|
|
|
class FileUploadRender extends AbstractFieldRender |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param FieldInterface $field |
|
17
|
|
|
* @param DOMElement $element |
|
18
|
|
|
* @return DOMNode |
|
19
|
|
|
*/ |
|
20
|
2 |
|
public function renderBlock(FieldInterface $field, DOMElement $element): DOMElement |
|
21
|
|
|
{ |
|
22
|
|
|
// Make sure the FieldInterface is actually a Radio |
|
23
|
2 |
|
if (!$field instanceof FileUpload) { |
|
24
|
1 |
|
throw new InvalidArgumentException('Must be a Del\Form\Field\FileUpload'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
1 |
|
$div = $this->getDom()->createElement('div'); |
|
28
|
1 |
|
$script = $this->getDom()->createElement('script'); |
|
29
|
1 |
|
$style = $this->getDom()->createElement('style'); |
|
30
|
1 |
|
$style->setAttribute('type', 'text/css'); |
|
31
|
1 |
|
$style->appendChild(new DOMText($this->getStyle())); |
|
32
|
1 |
|
$input = $this->getDom()->createElement('input'); |
|
33
|
1 |
|
$span = $this->getDom()->createElement('span'); |
|
34
|
1 |
|
$innerSpan = $this->getDom()->createElement('span'); |
|
35
|
1 |
|
$text = new DOMText($field->getLabel()); |
|
36
|
|
|
|
|
37
|
1 |
|
$innerSpan->setAttribute('class', 'btn btn-primary btn-file'); |
|
38
|
1 |
|
$innerSpan->appendChild($text); |
|
39
|
1 |
|
$innerSpan->appendChild($element); |
|
40
|
|
|
|
|
41
|
1 |
|
$span->setAttribute('class', 'input-group-btn'); |
|
42
|
1 |
|
$span->appendChild($innerSpan); |
|
43
|
|
|
|
|
44
|
1 |
|
$input->setAttribute('type', 'text'); |
|
45
|
1 |
|
$input->setAttribute('class', 'form-control'); |
|
46
|
1 |
|
$input->setAttribute('readonly', 'readonly'); |
|
47
|
|
|
|
|
48
|
1 |
|
$script->setAttribute('type', 'text/javascript'); |
|
49
|
1 |
|
$code = new DOMText($this->getJavascript()); |
|
50
|
1 |
|
$script->appendChild($code); |
|
51
|
|
|
|
|
52
|
1 |
|
$div->setAttribute('class', 'input-group'); |
|
53
|
1 |
|
$div->appendChild($span); |
|
54
|
1 |
|
$div->appendChild($input); |
|
55
|
1 |
|
$div->appendChild($style); |
|
56
|
1 |
|
$div->appendChild($script); |
|
57
|
|
|
|
|
58
|
1 |
|
return $div; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
1 |
|
private function getJavascript() |
|
65
|
|
|
{ |
|
66
|
|
|
return <<<END |
|
67
|
1 |
|
$(document).on('change', '.btn-file :file', function() { |
|
68
|
|
|
var input = $(this), |
|
69
|
|
|
numFiles = input.get(0).files ? input.get(0).files.length : 1, |
|
70
|
|
|
label = input.val().replace(/\\\\/g, '/').replace(/.*\//, ''); |
|
71
|
|
|
input.trigger('fileselect', [numFiles, label]); |
|
72
|
|
|
}); |
|
73
|
|
|
|
|
74
|
|
|
$(document).ready( function() { |
|
75
|
|
|
$('.btn-file :file').on('fileselect', function(event, numFiles, label) { |
|
76
|
|
|
|
|
77
|
|
|
var input = $(this).parents('.input-group').find(':text'), |
|
78
|
|
|
log = numFiles > 1 ? numFiles + ' files selected' : label; |
|
79
|
|
|
|
|
80
|
|
|
if( input.length ) { |
|
81
|
|
|
input.val(log); |
|
82
|
|
|
} else { |
|
83
|
|
|
if( log ) alert(log); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
}); |
|
87
|
|
|
}); |
|
88
|
|
|
END; |
|
89
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
private function getStyle() |
|
93
|
|
|
{ |
|
94
|
|
|
return <<<END |
|
95
|
1 |
|
.btn-file { |
|
96
|
|
|
position: relative; |
|
97
|
|
|
overflow: hidden; |
|
98
|
|
|
} |
|
99
|
|
|
.btn-file input[type=file] { |
|
100
|
|
|
position: absolute; |
|
101
|
|
|
top: 0; |
|
102
|
|
|
right: 0; |
|
103
|
|
|
min-width: 100%; |
|
104
|
|
|
min-height: 100%; |
|
105
|
|
|
font-size: 100px; |
|
106
|
|
|
text-align: right; |
|
107
|
|
|
filter: alpha(opacity=0); |
|
108
|
|
|
opacity: 0; |
|
109
|
|
|
outline: none; |
|
110
|
|
|
background: white; |
|
111
|
|
|
cursor: inherit; |
|
112
|
|
|
display: block; |
|
113
|
|
|
} |
|
114
|
|
|
input[readonly] { |
|
115
|
|
|
background-color: white !important; |
|
116
|
|
|
cursor: text !important; |
|
117
|
|
|
} |
|
118
|
|
|
END; |
|
119
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|