|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Javaabu\Mediapicker\Concerns; |
| 4 | + |
| 5 | +use Javaabu\Helpers\Media\AllowedMimeTypes; |
| 6 | +use Javaabu\Mediapicker\Mediapicker; |
| 7 | +use Spatie\Image\Enums\Fit; |
| 8 | +use Spatie\MediaLibrary\MediaCollections\File; |
| 9 | +use Spatie\MediaLibrary\MediaCollections\Models\Media; |
| 10 | + |
| 11 | +trait OwnsMedia |
| 12 | +{ |
| 13 | + public function getMediapickerCollectionName(): string |
| 14 | + { |
| 15 | + return Mediapicker::collectionName(); |
| 16 | + } |
| 17 | + |
| 18 | + /* |
| 19 | + * Register the media picker collections. |
| 20 | + */ |
| 21 | + public function registerMediapickerCollections() |
| 22 | + { |
| 23 | + $this->addMediaCollection($this->getMediapickerCollectionName()) |
| 24 | + ->acceptsFile(function (File $file) { |
| 25 | + return AllowedMimeTypes::isAllowedMimeType($file->mimeType); |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + /* |
| 30 | + * Register the media picker conversions. |
| 31 | + */ |
| 32 | + public function registerMediapickerConversions(?Media $media = null) |
| 33 | + { |
| 34 | + $this->addMediaConversion('mediapicker-large') |
| 35 | + ->fit(Fit::Max, 1200, 1200) |
| 36 | + ->width(1200) |
| 37 | + ->height(1200) |
| 38 | + ->keepOriginalImageFormat() |
| 39 | + ->shouldBePerformedOn($this->getMediapickerCollectionName()); |
| 40 | + |
| 41 | + $this->addMediaConversion('mediapicker-thumb') |
| 42 | + ->width(250) |
| 43 | + ->height(250) |
| 44 | + ->fit(Fit::Crop, 250, 250) |
| 45 | + ->keepOriginalImageFormat() |
| 46 | + ->shouldBePerformedOn($this->getMediapickerCollectionName()); |
| 47 | + } |
| 48 | + |
| 49 | + /* |
| 50 | + * Check if can owns the given media |
| 51 | + */ |
| 52 | + public function ownsMedia(Media $media): bool |
| 53 | + { |
| 54 | + return $media->model_type == $this->getMorphClass() && $media->model_id == $this->getKey(); |
| 55 | + } |
| 56 | + |
| 57 | + public function canViewAnyMedia(): bool |
| 58 | + { |
| 59 | + return $this->can('view_media'); |
| 60 | + } |
| 61 | + |
| 62 | + public function canCreateMedia(): bool |
| 63 | + { |
| 64 | + return $this->can('edit_media'); |
| 65 | + } |
| 66 | + |
| 67 | + public function canViewOthersMedia(): bool |
| 68 | + { |
| 69 | + return $this->can('view_others_media'); |
| 70 | + } |
| 71 | + |
| 72 | + public function canEditOthersMedia(): bool |
| 73 | + { |
| 74 | + return $this->can('edit_others_media'); |
| 75 | + } |
| 76 | + |
| 77 | + public function canDeleteOthersMedia(): bool |
| 78 | + { |
| 79 | + return $this->can('delete_others_media'); |
| 80 | + } |
| 81 | + |
| 82 | + public function canViewMedia(Media $media): bool |
| 83 | + { |
| 84 | + return $this->canViewAnyMedia() && |
| 85 | + ($this->canViewOthersMedia() || $this->ownsMedia($media)); |
| 86 | + } |
| 87 | + |
| 88 | + public function canEditMedia(Media $media): bool |
| 89 | + { |
| 90 | + return $this->canCreateMedia() && |
| 91 | + ($this->canEditOthersMedia() || $this->ownsMedia($media)); |
| 92 | + } |
| 93 | + |
| 94 | + public function canDeleteMedia(Media $media): bool |
| 95 | + { |
| 96 | + return $this->can('delete_media') && |
| 97 | + ($this->canDeleteOthersMedia() || $this->ownsMedia($media)); |
| 98 | + } |
| 99 | +} |
0 commit comments