1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Form\Field; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Form\Field; |
6
|
|
|
use Illuminate\Support\Facades\Validator; |
7
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
8
|
|
|
|
9
|
|
|
class File extends Field |
10
|
|
|
{ |
11
|
|
|
use UploadField; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Css. |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected static $css = [ |
19
|
|
|
'/vendor/laravel-admin/bootstrap-fileinput/css/fileinput.min.css?v=4.3.7', |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Js. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected static $js = [ |
28
|
|
|
'/vendor/laravel-admin/bootstrap-fileinput/js/plugins/canvas-to-blob.min.js?v=4.3.7', |
29
|
|
|
'/vendor/laravel-admin/bootstrap-fileinput/js/fileinput.min.js?v=4.3.7', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create a new File instance. |
34
|
|
|
* |
35
|
|
|
* @param string $column |
36
|
|
|
* @param array $arguments |
37
|
|
|
*/ |
38
|
|
|
public function __construct($column, $arguments = []) |
39
|
|
|
{ |
40
|
|
|
$this->initStorage(); |
41
|
|
|
|
42
|
|
|
parent::__construct($column, $arguments); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Default directory for file to upload. |
47
|
|
|
* |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
public function defaultDirectory() |
51
|
|
|
{ |
52
|
|
|
return config('admin.upload.directory.file'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function getValidator(array $input) |
59
|
|
|
{ |
60
|
|
|
if (request()->has(static::FILE_DELETE_FLAG)) { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($this->validator) { |
65
|
|
|
return $this->validator->call($this, $input); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/* |
69
|
|
|
* If has original value, means the form is in edit mode, |
70
|
|
|
* then remove required rule from rules. |
71
|
|
|
*/ |
72
|
|
|
if ($this->original()) { |
73
|
|
|
$this->removeRule('required'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/* |
77
|
|
|
* Make input data validatable if the column data is `null`. |
78
|
|
|
*/ |
79
|
|
|
if (array_has($input, $this->column) && is_null(array_get($input, $this->column))) { |
|
|
|
|
80
|
|
|
$input[$this->column] = ''; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$rules = $attributes = []; |
84
|
|
|
|
85
|
|
|
if (!$fieldRules = $this->getRules()) { |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$rules[$this->column] = $fieldRules; |
90
|
|
|
$attributes[$this->column] = $this->label; |
91
|
|
|
|
92
|
|
|
return Validator::make($input, $rules, $this->validationMessages, $attributes); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Prepare for saving. |
97
|
|
|
* |
98
|
|
|
* @param UploadedFile|array $file |
99
|
|
|
* |
100
|
|
|
* @return mixed|string |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function prepare($file) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
if (request()->has(static::FILE_DELETE_FLAG)) { |
105
|
|
|
return $this->destroy(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->name = $this->getStoreName($file); |
|
|
|
|
109
|
|
|
|
110
|
|
|
return $this->uploadAndDeleteOriginal($file); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Upload file and delete original file. |
115
|
|
|
* |
116
|
|
|
* @param UploadedFile $file |
117
|
|
|
* |
118
|
|
|
* @return mixed |
119
|
|
|
*/ |
120
|
|
|
protected function uploadAndDeleteOriginal(UploadedFile $file) |
121
|
|
|
{ |
122
|
|
|
$this->renameIfExists($file); |
123
|
|
|
|
124
|
|
|
$path = $this->storage->putFileAs($this->getDirectory(), $file, $this->name); |
125
|
|
|
|
126
|
|
|
$this->destroy(); |
127
|
|
|
|
128
|
|
|
return $path; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Preview html for file-upload plugin. |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
protected function preview() |
137
|
|
|
{ |
138
|
|
|
return $this->objectUrl($this->value); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Initialize the caption. |
143
|
|
|
* |
144
|
|
|
* @param string $caption |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
protected function initialCaption($caption) |
149
|
|
|
{ |
150
|
|
|
return basename($caption); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
protected function initialPreviewConfig() |
157
|
|
|
{ |
158
|
|
|
return [ |
159
|
|
|
['caption' => basename($this->value), 'key' => 0], |
160
|
|
|
]; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Render file upload field. |
165
|
|
|
* |
166
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
167
|
|
|
*/ |
168
|
|
View Code Duplication |
public function render() |
|
|
|
|
169
|
|
|
{ |
170
|
|
|
$this->setupDefaultOptions(); |
171
|
|
|
|
172
|
|
|
if (!empty($this->value)) { |
173
|
|
|
$this->attribute('data-initial-preview', $this->preview()); |
174
|
|
|
|
175
|
|
|
$this->setupPreviewOptions(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$this->options(['overwriteInitial' => true]); |
179
|
|
|
|
180
|
|
|
$options = json_encode($this->options); |
181
|
|
|
|
182
|
|
|
$this->script = <<<EOT |
183
|
|
|
|
184
|
|
|
$("input{$this->getElementClassSelector()}").fileinput({$options}); |
185
|
|
|
|
186
|
|
|
EOT; |
187
|
|
|
|
188
|
|
|
return parent::render(); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.