1 | <?php |
||
17 | trait HasMediaTrait |
||
18 | { |
||
19 | /** @var array */ |
||
20 | public $mediaConversions = []; |
||
21 | |||
22 | /** @var bool */ |
||
23 | protected $deletePreservingMedia = false; |
||
24 | |||
25 | public static function bootHasMediaTrait() |
||
26 | { |
||
27 | static::deleted(function (HasMedia $entity) { |
||
28 | if (! $entity->shouldDeletePreservingMedia()) { |
||
29 | $entity->media()->get()->each(function (Media $media) { |
||
30 | $media->delete(); |
||
31 | }); |
||
32 | } |
||
33 | }); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Set the polymorphic relation. |
||
38 | * |
||
39 | * @return mixed |
||
40 | */ |
||
41 | public function media() |
||
45 | |||
46 | /** |
||
47 | * Add a file to the medialibrary. |
||
48 | * |
||
49 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
50 | * |
||
51 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
52 | */ |
||
53 | public function addMedia($file) |
||
57 | |||
58 | /** |
||
59 | * Add a file from a request. |
||
60 | * |
||
61 | * @param string $key |
||
62 | * |
||
63 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
64 | */ |
||
65 | public function addMediaFromRequest(string $key) |
||
69 | |||
70 | /** |
||
71 | * Add a remote file to the medialibrary. |
||
72 | * |
||
73 | * @param string $url |
||
74 | * |
||
75 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
76 | * |
||
77 | * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
||
78 | */ |
||
79 | public function addMediaFromUrl(string $url) |
||
80 | { |
||
81 | if (! $stream = @fopen($url, 'r')) { |
||
82 | throw FileCannotBeAdded::unreachableUrl($url); |
||
83 | } |
||
84 | |||
85 | $tmpFile = tempnam(sys_get_temp_dir(), 'media-library'); |
||
86 | file_put_contents($tmpFile, $stream); |
||
87 | |||
88 | $filename = basename(parse_url($url, PHP_URL_PATH)); |
||
89 | |||
90 | return app(FileAdderFactory::class) |
||
91 | ->create($this, $tmpFile) |
||
92 | ->usingName(pathinfo($filename, PATHINFO_FILENAME)) |
||
93 | ->usingFileName($filename); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Copy a file to the medialibrary. |
||
98 | * |
||
99 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
100 | * |
||
101 | * @return \Spatie\MediaLibrary\FileAdder\FileAdder |
||
102 | */ |
||
103 | public function copyMedia($file) |
||
107 | |||
108 | /* |
||
109 | * Determine if there is media in the given collection. |
||
110 | */ |
||
111 | public function hasMedia(string $collectionName = '') : bool |
||
115 | |||
116 | /* |
||
117 | * Get media collection by its collectionName. |
||
118 | * |
||
119 | * @param string $collectionName |
||
120 | * @param array|callable $filters |
||
121 | * |
||
122 | * @return \Illuminate\Support\Collection |
||
123 | */ |
||
124 | public function getMedia(string $collectionName = '', $filters = []) : Collection |
||
128 | |||
129 | /** |
||
130 | * Get the first media item of a media collection. |
||
131 | * |
||
132 | * @param string $collectionName |
||
133 | * @param array $filters |
||
134 | * |
||
135 | * @return Media|null |
||
136 | */ |
||
137 | public function getFirstMedia(string $collectionName = 'default', array $filters = []) |
||
143 | |||
144 | /* |
||
145 | * Get the url of the image for the given conversionName |
||
146 | * for first media for the given collectionName. |
||
147 | * If no profile is given, return the source's url. |
||
148 | */ |
||
149 | public function getFirstMediaUrl(string $collectionName = 'default', string $conversionName = '') : string |
||
159 | |||
160 | /* |
||
161 | * Get the url of the image for the given conversionName |
||
162 | * for first media for the given collectionName. |
||
163 | * If no profile is given, return the source's url. |
||
164 | */ |
||
165 | public function getFirstMediaPath(string $collectionName = 'default', string $conversionName = '') : string |
||
175 | |||
176 | /** |
||
177 | * Update a media collection by deleting and inserting again with new values. |
||
178 | * |
||
179 | * @param array $newMediaArray |
||
180 | * @param string $collectionName |
||
181 | * |
||
182 | * @return array |
||
183 | * |
||
184 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeUpdated |
||
185 | */ |
||
186 | public function updateMedia(array $newMediaArray, string $collectionName = 'default') : array |
||
187 | { |
||
188 | $this->removeMediaItemsNotPresentInArray($newMediaArray, $collectionName); |
||
189 | |||
190 | $orderColumn = 1; |
||
191 | |||
192 | $updatedMedia = []; |
||
193 | foreach ($newMediaArray as $newMediaItem) { |
||
194 | $mediaClass = config('laravel-medialibrary.media_model'); |
||
195 | $currentMedia = $mediaClass::findOrFail($newMediaItem['id']); |
||
196 | |||
197 | if ($currentMedia->collection_name != $collectionName) { |
||
198 | throw MediaCannotBeUpdated::doesNotBelongToCollection($collectionName, $currentMedia); |
||
199 | } |
||
200 | |||
201 | if (array_key_exists('name', $newMediaItem)) { |
||
202 | $currentMedia->name = $newMediaItem['name']; |
||
203 | } |
||
204 | |||
205 | if (array_key_exists('custom_properties', $newMediaItem)) { |
||
206 | $currentMedia->custom_properties = $newMediaItem['custom_properties']; |
||
207 | } |
||
208 | |||
209 | $currentMedia->order_column = $orderColumn++; |
||
210 | |||
211 | $currentMedia->save(); |
||
212 | |||
213 | $updatedMedia[] = $currentMedia; |
||
214 | } |
||
215 | |||
216 | return $updatedMedia; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @param array $newMediaArray |
||
221 | * @param string $collectionName |
||
222 | */ |
||
223 | protected function removeMediaItemsNotPresentInArray(array $newMediaArray, string $collectionName = 'default') |
||
233 | |||
234 | /** |
||
235 | * Remove all media in the given collection. |
||
236 | * |
||
237 | * @param string $collectionName |
||
238 | * |
||
239 | * @return $this |
||
240 | */ |
||
241 | public function clearMediaCollection(string $collectionName = 'default') |
||
256 | |||
257 | /** |
||
258 | * Delete the associated media with the given id. |
||
259 | * You may also pass a media object. |
||
260 | * |
||
261 | * @param int|\Spatie\MediaLibrary\Media $mediaId |
||
262 | * |
||
263 | * @throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeDeleted |
||
264 | */ |
||
265 | public function deleteMedia($mediaId) |
||
279 | |||
280 | /* |
||
281 | * Add a conversion. |
||
282 | */ |
||
283 | public function addMediaConversion(string $name) : Conversion |
||
291 | |||
292 | /** |
||
293 | * Delete the model, but preserve all the associated media. |
||
294 | * |
||
295 | * @return bool |
||
296 | */ |
||
297 | public function deletePreservingMedia() : bool |
||
303 | |||
304 | /** |
||
305 | * Determines if the media files should be preserved when the media object gets deleted. |
||
306 | * |
||
307 | * @return \Spatie\MediaLibrary\Media |
||
308 | */ |
||
309 | public function shouldDeletePreservingMedia() |
||
313 | |||
314 | protected function mediaIsPreloaded() : bool |
||
318 | |||
319 | /** |
||
320 | * Cache the media on the object. |
||
321 | * |
||
322 | * @param string $collectionName |
||
323 | * |
||
324 | * @return mixed |
||
325 | */ |
||
326 | public function loadMedia(string $collectionName) |
||
354 | } |
||
355 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.