|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Thinktomorrow\Chief\Media\Application; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
use Thinktomorrow\AssetLibrary\Asset; |
|
10
|
|
|
use Thinktomorrow\AssetLibrary\HasAsset; |
|
11
|
|
|
use Thinktomorrow\Chief\Fields\Types\MediaField; |
|
12
|
|
|
use Thinktomorrow\AssetLibrary\Application\AddAsset; |
|
13
|
|
|
use Thinktomorrow\Chief\Media\DuplicateAssetException; |
|
14
|
|
|
use Thinktomorrow\AssetLibrary\Application\DetachAsset; |
|
15
|
|
|
use Thinktomorrow\AssetLibrary\Application\ReplaceAsset; |
|
16
|
|
|
use Thinktomorrow\AssetLibrary\Application\AssetUploader; |
|
17
|
|
|
use Thinktomorrow\AssetLibrary\Application\SortAssets; |
|
18
|
|
|
|
|
19
|
|
|
abstract class AbstractMediaFieldHandler |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var ReplaceAsset */ |
|
22
|
|
|
protected $replaceAsset; |
|
23
|
|
|
|
|
24
|
|
|
/** @var AddAsset */ |
|
25
|
|
|
protected $addAsset; |
|
26
|
|
|
|
|
27
|
|
|
/** @var DetachAsset */ |
|
28
|
|
|
protected $detachAsset; |
|
29
|
|
|
|
|
30
|
|
|
/** @var AssetUploader */ |
|
31
|
|
|
protected $assetUploader; |
|
32
|
|
|
|
|
33
|
|
|
/** @var SortAssets */ |
|
34
|
|
|
protected $sortAssets; |
|
35
|
|
|
|
|
36
|
|
|
final public function __construct(AddAsset $addAsset, ReplaceAsset $replaceAsset, DetachAsset $detachAsset, SortAssets $sortAssets, AssetUploader $assetUploader) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->replaceAsset = $replaceAsset; |
|
39
|
|
|
$this->addAsset = $addAsset; |
|
40
|
|
|
$this->detachAsset = $detachAsset; |
|
41
|
|
|
$this->sortAssets = $sortAssets; |
|
42
|
|
|
$this->assetUploader = $assetUploader; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function mediaRequest(array $requests, MediaField $field, Request $request): MediaRequest |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
$mediaRequest = new MediaRequest(); |
|
48
|
|
|
|
|
49
|
|
|
foreach ($requests as $requestData) { |
|
50
|
|
|
foreach ($requestData as $locale => $values) { |
|
51
|
|
|
foreach ($values as $key => $value) { |
|
52
|
|
|
|
|
53
|
|
|
// The null entries in the 'replace' request are passed explicitly - the replace array contains all existing assets (ids as keys, null as value) |
|
54
|
|
|
if (is_null($value)) { |
|
55
|
|
|
$mediaRequest->add(MediaRequest::DETACH, new MediaRequestInput( |
|
56
|
|
|
'', $locale, $field->getKey(), [ |
|
57
|
|
|
'existing_id' => $key, |
|
58
|
|
|
'value_as_assetid' => true, |
|
59
|
|
|
] |
|
60
|
|
|
)); |
|
61
|
|
|
|
|
62
|
|
|
continue; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$action = $this->looksLikeAnAssetID($key) |
|
66
|
|
|
? MediaRequest::REPLACE |
|
67
|
|
|
: MediaRequest::NEW; |
|
68
|
|
|
|
|
69
|
|
|
// // If the value is the same as the original key and the asset already exists, we'll ignore this request. |
|
70
|
|
|
// if($key == $value && $keyRefersToExistingAsset && $valueRefersToExistingAsset) { |
|
71
|
|
|
// continue; |
|
72
|
|
|
// } |
|
73
|
|
|
|
|
74
|
|
|
$mediaRequest->add($action, new MediaRequestInput( |
|
75
|
|
|
$value, $locale, $field->getKey(), [ |
|
76
|
|
|
'existing_id' => $key, |
|
77
|
|
|
'value_as_assetid' => $this->looksLikeAnAssetID($value), // index key is used for replace method to indicate the current asset id |
|
78
|
|
|
] |
|
79
|
|
|
)); |
|
80
|
|
|
|
|
81
|
|
|
// // Slim can push the ajax response object as value so we'll need to extract the id from this object |
|
82
|
|
|
// if(is_string($file) && ($slimPayload = json_decode($file)) && isset($slimPayload->id)) { |
|
83
|
|
|
// $file = $slimPayload->id; |
|
84
|
|
|
// } |
|
85
|
|
|
|
|
86
|
|
|
// $mediaRequest->add($action, new MediaRequestInput( |
|
87
|
|
|
// $file, $locale, $field->getKey(), [ |
|
88
|
|
|
// 'index' => $k, |
|
89
|
|
|
// // index key is used for replace method to indicate the current asset id |
|
90
|
|
|
// 'value_as_assetid' => $this->refersToExistingAsset($file), |
|
91
|
|
|
// ] |
|
92
|
|
|
// )); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $mediaRequest; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function looksLikeAnAssetID($value): bool |
|
101
|
|
|
{ |
|
102
|
|
|
if (!is_string($value) && !is_int($value)) { |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// check if passed value is an ID |
|
107
|
|
|
return (bool)preg_match('/^[1-9][0-9]*$/', (string)$value); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param HasAsset $model |
|
112
|
|
|
* @param MediaRequestInput $mediaRequestInput |
|
113
|
|
|
* @return Asset |
|
114
|
|
|
* @throws DuplicateAssetException |
|
115
|
|
|
* @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function newExistingAsset(HasAsset $model, string $locale, string $type, $value): Asset |
|
118
|
|
|
{ |
|
119
|
|
|
$existingAsset = Asset::find($value); |
|
120
|
|
|
|
|
121
|
|
|
if ($model->assetRelation()->where('asset_pivots.type', $type)->where('asset_pivots.locale', $locale)->get()->contains($existingAsset)) { |
|
122
|
|
|
throw new DuplicateAssetException(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $this->addAsset->add($model, $existingAsset, $type, $locale); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param string $filename |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function sluggifyFilename(string $filename): string |
|
133
|
|
|
{ |
|
134
|
|
|
if (false === strpos($filename, '.')) { |
|
135
|
|
|
return $filename; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$extension = substr($filename, strrpos($filename, '.') + 1); |
|
139
|
|
|
$filename = substr($filename, 0, strrpos($filename, '.')); |
|
140
|
|
|
|
|
141
|
|
|
return Str::slug($filename) . '.' . $extension; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
protected function sort(HasAsset $model, MediaField $field, Request $request) |
|
145
|
|
|
{ |
|
146
|
|
|
if ($request->has('filesOrder')) { |
|
147
|
|
|
foreach ($request->input('filesOrder') as $locale => $fileIdInput) { |
|
148
|
|
|
$fileIds = $this->getFileIdsFromInput($field->getKey(), $fileIdInput); |
|
149
|
|
|
|
|
150
|
|
|
if (!empty($fileIds)) { |
|
151
|
|
|
$this->sortAssets->handle($model, $fileIds, $field->getKey(), $locale); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param string $key |
|
159
|
|
|
* @param array $fileIdInput |
|
160
|
|
|
* @return array |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function getFileIdsFromInput(string $key, array $fileIdInput): array |
|
163
|
|
|
{ |
|
164
|
|
|
$values = isset($fileIdInput[$key]) |
|
165
|
|
|
? $fileIdInput[$key] |
|
166
|
|
|
: (isset($fileIdInput['files-' . $key]) |
|
167
|
|
|
? $fileIdInput['files-' . $key] |
|
168
|
|
|
: '' |
|
169
|
|
|
); |
|
170
|
|
|
|
|
171
|
|
|
if (!$values) { |
|
172
|
|
|
return []; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return explode(',', $values); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.