|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Spatie\Translatable\HasTranslations; |
|
8
|
|
|
use Spatie\Blender\Model\Traits\HasMedia; |
|
9
|
|
|
use Spatie\Blender\Model\Traits\Draftable; |
|
10
|
|
|
use Spatie\EloquentSortable\SortableTrait; |
|
11
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
|
12
|
|
|
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMediaConversions; |
|
13
|
|
|
|
|
14
|
|
|
class ContentBlock extends Model implements HasMediaConversions |
|
15
|
|
|
{ |
|
16
|
|
|
use Draftable, SortableTrait, HasTranslations, HasMedia; |
|
17
|
|
|
|
|
18
|
|
|
public $translatable = ['name', 'text']; |
|
19
|
|
|
protected $guarded = []; |
|
20
|
|
|
|
|
21
|
|
|
public function subject(): MorphTo |
|
22
|
|
|
{ |
|
23
|
|
|
return $this->morphTo('model'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function registerMediaConversions() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->addMediaConversion('admin') |
|
29
|
|
|
->setWidth(368) |
|
30
|
|
|
->setHeight(232) |
|
31
|
|
|
->nonQueued(); |
|
32
|
|
|
|
|
33
|
|
|
$this->addMediaConversion('thumb') |
|
34
|
|
|
->setWidth(368) |
|
35
|
|
|
->setHeight(232); |
|
36
|
|
|
|
|
37
|
|
|
$this->addMediaConversion('detail') |
|
38
|
|
|
->setWidth(740); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function mediaLibraryCollections(): array |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->subject->getContentBlockMediaLibraryCollections(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function mediaLibraryCollectionType(string $name): string |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->subject->getContentBlockMediaLibraryCollections()[$name]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function updateWithAttributes(array $values) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->draft = false; |
|
54
|
|
|
$this->type = $values['type']; |
|
55
|
|
|
|
|
56
|
|
|
foreach ($this->translatable as $attribute) { |
|
57
|
|
|
$this->setTranslations($attribute, $values[$attribute] ?? []); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
foreach ($this->mediaLibraryCollections() as $collection) { |
|
61
|
|
|
if (! isset($values[$collection])) { |
|
62
|
|
|
continue; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$media = array_filter($values[$collection], function ($media) { |
|
66
|
|
|
return ($media['markedForRemoval'] ?? false) !== true; |
|
67
|
|
|
}); |
|
68
|
|
|
|
|
69
|
|
|
$updatedMedia = $this->updateMedia($media, $collection); |
|
70
|
|
|
|
|
71
|
|
|
$this->media() |
|
72
|
|
|
->whereCollectionName($collection) |
|
73
|
|
|
->whereNotIn('id', Arr::pluck($updatedMedia, 'id')) |
|
74
|
|
|
->delete(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->save(); |
|
78
|
|
|
|
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function setOrder(int $i) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->order_column = $i; |
|
85
|
|
|
|
|
86
|
|
|
$this->save(); |
|
87
|
|
|
|
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|