|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Media\Application; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Thinktomorrow\Chief\Fields\Types\MediaField; |
|
8
|
|
|
use Thinktomorrow\AssetLibrary\Application\AddAsset; |
|
9
|
|
|
use Thinktomorrow\AssetLibrary\Application\DetachAsset; |
|
10
|
|
|
use Thinktomorrow\AssetLibrary\Application\ReplaceAsset; |
|
11
|
|
|
|
|
12
|
|
|
abstract class AbstractMediaFieldHandler |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var ReplaceAsset */ |
|
15
|
|
|
protected $replaceAsset; |
|
16
|
|
|
|
|
17
|
|
|
/** @var AddAsset */ |
|
18
|
|
|
protected $addAsset; |
|
19
|
|
|
|
|
20
|
|
|
/** @var DetachAsset */ |
|
21
|
|
|
protected $detachAsset; |
|
22
|
|
|
|
|
23
|
|
|
final public function __construct(AddAsset $addAsset, ReplaceAsset $replaceAsset, DetachAsset $detachAsset) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->replaceAsset = $replaceAsset; |
|
26
|
|
|
$this->addAsset = $addAsset; |
|
27
|
|
|
$this->detachAsset = $detachAsset; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
protected function mediaRequest(array $requests, MediaField $field, Request $request): MediaRequest |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
$mediaRequest = new MediaRequest(); |
|
33
|
|
|
|
|
34
|
|
|
foreach($requests as $requestData){ |
|
35
|
|
|
foreach($requestData as $locale => $filesPerLocale) { |
|
36
|
|
|
foreach($filesPerLocale as $action => $files) { |
|
37
|
|
|
foreach($files as $k => $file) { |
|
38
|
|
|
$mediaRequest->add($action, new MediaRequestInput( |
|
39
|
|
|
$file, $locale, $field->getKey(), [ |
|
40
|
|
|
'index' => $k, // index key is used for replace method to indicate the current asset id |
|
41
|
|
|
'existing_asset' => $this->refersToExistingAsset($file), |
|
42
|
|
|
] |
|
43
|
|
|
)); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $mediaRequest; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function refersToExistingAsset($value): bool |
|
53
|
|
|
{ |
|
54
|
|
|
return is_int($value); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $filename |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function sluggifyFilename(string $filename): string |
|
62
|
|
|
{ |
|
63
|
|
|
if(false === strpos($filename, '.')) return $filename; |
|
64
|
|
|
|
|
65
|
|
|
$extension = substr($filename, strrpos($filename, '.') + 1); |
|
66
|
|
|
$filename = substr($filename, 0, strrpos($filename, '.')); |
|
67
|
|
|
|
|
68
|
|
|
return Str::slug($filename) . '.' . $extension; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.