|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sco\Admin\Form\Elements; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\UploadedFile; |
|
6
|
|
|
use Storage; |
|
7
|
|
|
|
|
8
|
|
|
class File extends NamedElement |
|
9
|
|
|
{ |
|
10
|
|
|
protected $type = 'file'; |
|
11
|
|
|
|
|
12
|
|
|
protected $actionUrl; |
|
13
|
|
|
|
|
14
|
|
|
protected $multiSelect = false; |
|
15
|
|
|
|
|
16
|
|
|
protected $multiFile = true; |
|
17
|
|
|
|
|
18
|
|
|
protected $showFileList = true; |
|
19
|
|
|
|
|
20
|
|
|
protected $withCredentials = false; |
|
21
|
|
|
|
|
22
|
|
|
protected $fileSizeLimit = 0; |
|
23
|
|
|
|
|
24
|
|
|
protected $fileUploadsLimit = 0; |
|
25
|
|
|
|
|
26
|
|
|
protected $fileExtensions; |
|
27
|
|
|
|
|
28
|
|
|
protected $listType = 'text'; |
|
29
|
|
|
|
|
30
|
|
|
protected $disk; |
|
31
|
|
|
|
|
32
|
|
|
protected $uploadPath; |
|
33
|
|
|
|
|
34
|
|
|
public function getValue() |
|
35
|
|
|
{ |
|
36
|
|
|
$value = parent::getValue(); |
|
37
|
|
|
if (empty($value)) { |
|
38
|
|
|
return []; |
|
39
|
|
|
} |
|
40
|
|
|
return collect(explode(',', $value))->map(function ($item) { |
|
41
|
|
|
return $this->getFileUrl($item); |
|
42
|
|
|
}); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getActionUrl() |
|
46
|
|
|
{ |
|
47
|
|
|
if ($this->actionUrl) { |
|
48
|
|
|
return $this->actionUrl; |
|
49
|
|
|
} |
|
50
|
|
|
$model = app('admin.components')->get(get_class($this->getModel())); |
|
51
|
|
|
|
|
52
|
|
|
$params = [ |
|
53
|
|
|
'model' => $model->getName(), |
|
54
|
|
|
'field' => $this->getName(), |
|
55
|
|
|
]; |
|
56
|
|
|
$params['id'] = null; |
|
57
|
|
|
if ($this->getModel()->exists) { |
|
58
|
|
|
$params['id'] = $this->getModel()->getKey(); |
|
59
|
|
|
} |
|
60
|
|
|
$params['_token'] = csrf_token(); |
|
61
|
|
|
|
|
62
|
|
|
return route('admin.model.upload.file', $params); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function setActionUrl($value) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->actionUrl = $value; |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function isMultiSelect() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->multiSelect; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function enableMultiSelect() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->multiSelect = true; |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Show file list |
|
86
|
|
|
* |
|
87
|
|
|
* @return $this |
|
88
|
|
|
*/ |
|
89
|
|
|
public function disableFileList() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->showFileList = false; |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Indicates whether or not cross-site Access-Control requests |
|
98
|
|
|
* should be made using credentials |
|
99
|
|
|
* |
|
100
|
|
|
* @return $this |
|
101
|
|
|
*/ |
|
102
|
|
|
public function withCredentials() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->withCredentials = true; |
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* The maximum size allowed for a file upload. (KB) |
|
111
|
|
|
* |
|
112
|
|
|
* @param int $value |
|
113
|
|
|
* |
|
114
|
|
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
|
|
public function setFileSizeLimit($value) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->fileSizeLimit = intval($value); |
|
119
|
|
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function getFileExtensions() |
|
123
|
|
|
{ |
|
124
|
|
|
if ($this->fileExtensions) { |
|
125
|
|
|
return $this->fileExtensions; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return config('admin.upload.extensions'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* A list of allowable extensions that can be uploaded. |
|
133
|
|
|
* |
|
134
|
|
|
* @param array|string $value |
|
135
|
|
|
* |
|
136
|
|
|
* @return $this |
|
137
|
|
|
*/ |
|
138
|
|
|
public function setFileExtensions($value) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->fileExtensions = is_array($value) ? $value : explode(',', |
|
141
|
|
|
$value); |
|
142
|
|
|
|
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function getFileUploadsLimit() |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->fileUploadsLimit; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* The maximum number of files that can be uploaded. |
|
153
|
|
|
* |
|
154
|
|
|
* @param int $value |
|
155
|
|
|
* |
|
156
|
|
|
* @return $this |
|
157
|
|
|
*/ |
|
158
|
|
|
public function setFileUploadsLimit($value) |
|
159
|
|
|
{ |
|
160
|
|
|
$this->fileUploadsLimit = intval($value); |
|
161
|
|
|
|
|
162
|
|
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function getListType() |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->listType; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function toArray() |
|
171
|
|
|
{ |
|
172
|
|
|
return parent::toArray() + [ |
|
173
|
|
|
'action' => $this->getActionUrl(), |
|
174
|
|
|
'showFileList' => $this->showFileList, |
|
175
|
|
|
'multiSelect' => $this->isMultiSelect(), |
|
176
|
|
|
'fileSizeLimit' => $this->fileSizeLimit, |
|
177
|
|
|
'fileUploadsLimit' => $this->getFileUploadsLimit(), |
|
178
|
|
|
'fileExtensions' => $this->getFileExtensions(), |
|
179
|
|
|
'listType' => $this->getListType(), |
|
180
|
|
|
]; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public function getDisk() |
|
184
|
|
|
{ |
|
185
|
|
|
if ($this->disk) { |
|
186
|
|
|
return $this->disk; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return config('admin.upload.disk', 'public'); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function setDisk($value) |
|
193
|
|
|
{ |
|
194
|
|
|
$this->disk = $value; |
|
195
|
|
|
|
|
196
|
|
|
return $this; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function getUploadPath() |
|
200
|
|
|
{ |
|
201
|
|
|
if ($this->uploadPath) { |
|
202
|
|
|
return $this->uploadPath; |
|
203
|
|
|
} |
|
204
|
|
|
return config('admin.upload.directory', 'admin/uploads'); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function setUploadPath($value) |
|
208
|
|
|
{ |
|
209
|
|
|
$this->uploadPath = $value; |
|
210
|
|
|
|
|
211
|
|
|
return $this; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function saveFile(UploadedFile $file) |
|
215
|
|
|
{ |
|
216
|
|
|
$path = $file->store($this->getUploadPath(), $this->getDisk()); |
|
217
|
|
|
|
|
218
|
|
|
return [ |
|
219
|
|
|
'path' => $path, |
|
220
|
|
|
'url' => $this->getFileUrl($path), |
|
221
|
|
|
]; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
protected function prepareValue($value) |
|
225
|
|
|
{ |
|
226
|
|
|
if (empty($value) && !is_array($value)) { |
|
227
|
|
|
return $value; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return implode(',', $value); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
protected function getFileUrl($path) |
|
234
|
|
|
{ |
|
235
|
|
|
return Storage::disk($this->getDisk())->url($path); |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|