1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Form\Element; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Http\UploadedFile; |
9
|
|
|
use KodiComponents\Support\Upload; |
|
|
|
|
10
|
|
|
use Illuminate\Support\Facades\File; |
|
|
|
|
11
|
|
|
|
12
|
|
|
class Files extends Images |
13
|
|
|
{ |
14
|
|
|
protected $uploadValidationRules = ['required']; |
15
|
|
|
|
16
|
|
|
protected $view = 'form.element.files'; |
17
|
|
|
|
18
|
|
|
protected $files_group_class = null; |
19
|
|
|
|
20
|
|
|
protected $show_title = true; |
21
|
|
|
|
22
|
|
|
protected $show_description = true; |
23
|
|
|
|
24
|
|
|
protected $title_required = false; |
25
|
|
|
|
26
|
|
|
protected $description_required = false; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param bool $bool |
30
|
|
|
* |
31
|
|
|
* @return $this |
32
|
|
|
*/ |
33
|
|
|
public function showTitle($bool) |
34
|
|
|
{ |
35
|
|
|
$this->show_title = $bool; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param bool $bool |
42
|
|
|
* |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
|
|
public function showDescription($bool) |
46
|
|
|
{ |
47
|
|
|
$this->show_description = $bool; |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param bool $bool |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function setTitleRequired($bool) |
58
|
|
|
{ |
59
|
|
|
if ($this->show_title) { |
60
|
|
|
$this->title_required = $bool; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param bool $bool |
68
|
|
|
* |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function setDescriptionRequired($bool) |
72
|
|
|
{ |
73
|
|
|
if ($this->show_description) { |
74
|
|
|
$this->description_required = $bool; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function getUploadValidationMessages() |
84
|
|
|
{ |
85
|
|
|
$messages = []; |
86
|
|
|
foreach ($this->validationMessages as $rule => $message) { |
87
|
|
|
$messages["file.{$rule}"] = $message; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $messages; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function getUploadValidationLabels() |
97
|
|
|
{ |
98
|
|
|
return ['file' => $this->getLabel()]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $driver |
103
|
|
|
* @param array $driverOptions |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function setDriver($driver, $driverOptions = []) |
107
|
|
|
{ |
108
|
|
|
$this->driver = $driver; |
109
|
|
|
$this->driverOptions = $driverOptions; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function getDriver() |
118
|
|
|
{ |
119
|
|
|
return ['driver' => $this->driver, 'driverOptions' => $this->driverOptions]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function getUploadValidationRules() |
126
|
|
|
{ |
127
|
|
|
return ['file' => array_unique($this->uploadValidationRules)]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param UploadedFile $file |
132
|
|
|
* |
133
|
|
|
* @return mixed |
134
|
|
|
*/ |
135
|
|
|
public function getUploadPath(UploadedFile $file) |
136
|
|
|
{ |
137
|
|
|
if (! is_callable($this->uploadPath)) { |
138
|
|
|
return $this->defaultUploadPath($file); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return call_user_func($this->uploadPath, $file); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param Closure $uploadPath |
146
|
|
|
* |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
|
|
public function setUploadPath(Closure $uploadPath) |
150
|
|
|
{ |
151
|
|
|
$this->uploadPath = $uploadPath; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param UploadedFile $file |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public function getUploadFileName(UploadedFile $file) |
162
|
|
|
{ |
163
|
|
|
if (! is_callable($this->uploadFileName)) { |
164
|
|
|
return $this->defaultUploadFilename($file); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return call_user_func($this->uploadFileName, $file); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param Closure $uploadFileName |
172
|
|
|
* |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
|
|
public function setUploadFileName(Closure $uploadFileName) |
176
|
|
|
{ |
177
|
|
|
$this->uploadFileName = $uploadFileName; |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
|
|
public function getUploadSettings() |
186
|
|
|
{ |
187
|
|
|
if (empty($this->uploadSettings) && in_array(Upload::class, class_uses($this->getModel()))) { |
188
|
|
|
return (array) array_get($this->getModel()->getUploadSettings(), $this->getPath()); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $this->uploadSettings; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param array $imageSettings |
196
|
|
|
* |
197
|
|
|
* @return $this |
198
|
|
|
*/ |
199
|
|
|
public function setUploadSettings(array $imageSettings) |
200
|
|
|
{ |
201
|
|
|
$this->uploadSettings = $imageSettings; |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param string $rule |
208
|
|
|
* @param null $message |
|
|
|
|
209
|
|
|
* @return $this|\SleepingOwl\Admin\Form\Element\File|\SleepingOwl\Admin\Form\Element\NamedFormElement |
210
|
|
|
*/ |
211
|
|
|
public function addValidationRule($rule, $message = null) |
212
|
|
|
{ |
213
|
|
|
$uploadRules = ['file', 'image', 'mime', 'size', 'dimensions', 'max', 'min', 'between']; |
214
|
|
|
|
215
|
|
|
foreach ($uploadRules as $uploadRule) { |
216
|
|
|
if (strpos($rule, $uploadRule) !== false) { |
217
|
|
|
$this->uploadValidationRules[] = $rule; |
218
|
|
|
|
219
|
|
|
if (is_null($message)) { |
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $this->addValidationMessage($rule, $message); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return parent::addValidationRule($rule, $message); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param int $size Max size in kilobytes |
232
|
|
|
* |
233
|
|
|
* @return $this |
234
|
|
|
*/ |
235
|
|
|
public function maxSize($size) |
236
|
|
|
{ |
237
|
|
|
$this->addValidationRule('max:'.(int) $size); |
238
|
|
|
|
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param int $size Max size in kilobytes |
244
|
|
|
* |
245
|
|
|
* @return $this |
246
|
|
|
*/ |
247
|
|
|
public function minSize($size) |
248
|
|
|
{ |
249
|
|
|
$this->addValidationRule('min:'.(int) $size); |
250
|
|
|
|
251
|
|
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param \Closure $callable |
256
|
|
|
* @return $this |
257
|
|
|
*/ |
258
|
|
|
public function setSaveCallback(\Closure $callable) |
259
|
|
|
{ |
260
|
|
|
$this->saveCallback = $callable; |
261
|
|
|
|
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Return save callback. |
267
|
|
|
* @return \Closure |
268
|
|
|
*/ |
269
|
|
|
public function getSaveCallback() |
270
|
|
|
{ |
271
|
|
|
return $this->saveCallback; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param UploadedFile $file |
276
|
|
|
* @param string $path |
277
|
|
|
* @param string $filename |
278
|
|
|
* @param array $settings |
279
|
|
|
* @return \Closure|array |
280
|
|
|
*/ |
281
|
|
|
public function saveFile(UploadedFile $file, $path, $filename, array $settings) |
282
|
|
|
{ |
283
|
|
|
if (is_callable($callback = $this->getSaveCallback())) { |
284
|
|
|
return $callback($file, $path, $filename, $settings); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
$file->move($path, $filename); |
288
|
|
|
|
289
|
|
|
//S3 Implement |
290
|
|
|
$value = $path.'/'.$filename; |
291
|
|
|
|
292
|
|
|
return ['path' => asset($value), 'value' => $value]; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param \Illuminate\Validation\Validator $validator |
297
|
|
|
*/ |
298
|
|
|
public function customValidation(\Illuminate\Validation\Validator $validator) |
299
|
|
|
{ |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @param UploadedFile $file |
304
|
|
|
* |
305
|
|
|
* @return string |
306
|
|
|
*/ |
307
|
|
|
public function defaultUploadFilename(UploadedFile $file) |
308
|
|
|
{ |
309
|
|
|
return md5(time().$file->getClientOriginalName()).'.'.$file->getClientOriginalExtension(); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @param UploadedFile $file |
314
|
|
|
* |
315
|
|
|
* @return string |
316
|
|
|
*/ |
317
|
|
|
public function defaultUploadPath(UploadedFile $file) |
318
|
|
|
{ |
319
|
|
|
return config('sleeping_owl.filesUploadDirectory', 'files/uploads'); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @return array|mixed|string |
324
|
|
|
*/ |
325
|
|
|
public function getValueFromModel() |
326
|
|
|
{ |
327
|
|
|
$value = $this->model->{$this->name}; |
328
|
|
|
$return = isset($value) && mb_strlen($value) >= 5 ? json_decode($value, true) : []; |
329
|
|
|
|
330
|
|
|
return $return; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @param string $mode |
335
|
|
|
* |
336
|
|
|
* @return $this |
337
|
|
|
*/ |
338
|
|
|
public function setListMode($mode) |
339
|
|
|
{ |
340
|
|
|
if ($mode == 'vertical') { |
341
|
|
|
$this->files_group_class = 'files-group-vertical'; |
342
|
|
|
} elseif ($mode == 'horizontal') { |
343
|
|
|
$this->files_group_class = null; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
return $this; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @return $this |
351
|
|
|
*/ |
352
|
|
|
public function setVertical() |
353
|
|
|
{ |
354
|
|
|
$this->setListMode('vertical'); |
355
|
|
|
|
356
|
|
|
return $this; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @return $this |
361
|
|
|
*/ |
362
|
|
|
public function setHorizontal() |
363
|
|
|
{ |
364
|
|
|
$this->setListMode('horizontal'); |
365
|
|
|
|
366
|
|
|
return $this; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* @param Request $request |
371
|
|
|
*/ |
372
|
|
|
public function save(Request $request) |
373
|
|
|
{ |
374
|
|
|
$name = $this->getName(); |
375
|
|
|
$value = Arr::get($request->all(), $this->getNameKey()); |
376
|
|
|
|
377
|
|
|
if (is_array($value_array = json_decode($value, true)) && count($value_array)) { |
378
|
|
|
foreach ($value_array as $v => $array) { |
379
|
|
|
$file = $array['url']; |
380
|
|
|
if ($file && File::exists($file)) { |
381
|
|
|
if (! isset($array['filesize'])) { |
382
|
|
|
$array['filesize'] = File::size($file); |
383
|
|
|
} |
384
|
|
|
if (! isset($array['ext'])) { |
385
|
|
|
$array['ext'] = File::extension($file); |
386
|
|
|
} |
387
|
|
|
$mime = File::mimeType($file); |
388
|
|
|
if (! isset($array['mime'])) { |
389
|
|
|
$array['mime'] = $mime; |
390
|
|
|
} |
391
|
|
|
if (mb_strpos($mime, '/')) { |
|
|
|
|
392
|
|
|
[$mime1, $mime2] = explode('/', $mime); |
|
|
|
|
393
|
|
|
if (! isset($array['mime_base'])) { |
394
|
|
|
$array['mime_base'] = $mime1; |
395
|
|
|
} |
396
|
|
|
if (! isset($array['mime_detail'])) { |
397
|
|
|
$array['mime_detail'] = $mime2; |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
$value_array[$v] = $array; |
402
|
|
|
} |
403
|
|
|
$value = json_encode($value_array); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
$request->merge([$name => $value]); |
407
|
|
|
|
408
|
|
|
$this->setModelAttribute( |
409
|
|
|
$this->getValueFromRequest($request) |
410
|
|
|
); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* @return array |
415
|
|
|
*/ |
416
|
|
|
public function toArray() |
417
|
|
|
{ |
418
|
|
|
$return = parent::toArray(); |
419
|
|
|
|
420
|
|
|
$return = array_merge($return, [ |
421
|
|
|
'files_group_class' => $this->files_group_class, |
422
|
|
|
'show_title' => $this->show_title, |
423
|
|
|
'show_description' => $this->show_description, |
424
|
|
|
'title_required' => $this->title_required, |
425
|
|
|
'description_required' => $this->description_required, |
426
|
|
|
]); |
427
|
|
|
|
428
|
|
|
return $return; |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: