1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Modules; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Thinktomorrow\Chief\Concerns\Viewable\Viewable; |
7
|
|
|
use Thinktomorrow\Chief\Concerns\Viewable\ViewableContract; |
8
|
|
|
use Thinktomorrow\Chief\Management\ManagedModel; |
9
|
|
|
use Thinktomorrow\Chief\Pages\Page; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Spatie\MediaLibrary\HasMedia\HasMedia; |
12
|
|
|
use Thinktomorrow\Chief\Management\Managers; |
13
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
14
|
|
|
use Thinktomorrow\Chief\Relations\ActsAsChild; |
15
|
|
|
use Thinktomorrow\Chief\Snippets\WithSnippets; |
16
|
|
|
use Thinktomorrow\Chief\Fields\Types\HtmlField; |
17
|
|
|
use Thinktomorrow\Chief\Fields\Types\InputField; |
18
|
|
|
use Thinktomorrow\Chief\Relations\ActingAsChild; |
19
|
|
|
use Thinktomorrow\AssetLibrary\Traits\AssetTrait; |
20
|
|
|
use Thinktomorrow\Chief\Concerns\Morphable\Morphable; |
21
|
|
|
use Thinktomorrow\Chief\FlatReferences\FlatReference; |
22
|
|
|
use Dimsav\Translatable\Translatable as BaseTranslatable; |
23
|
|
|
use Thinktomorrow\Chief\Concerns\Translatable\Translatable; |
24
|
|
|
use Thinktomorrow\Chief\Concerns\Morphable\MorphableContract; |
25
|
|
|
use Thinktomorrow\Chief\Concerns\Translatable\TranslatableContract; |
26
|
|
|
|
27
|
|
|
class Module extends Model implements ManagedModel, TranslatableContract, HasMedia, ActsAsChild, MorphableContract, ViewableContract |
28
|
|
|
{ |
29
|
|
|
use Morphable, |
|
|
|
|
30
|
|
|
AssetTrait, |
31
|
|
|
Translatable, |
32
|
|
|
BaseTranslatable, |
33
|
|
|
SoftDeletes, |
34
|
|
|
ActingAsChild, |
35
|
|
|
WithSnippets, |
36
|
|
|
Viewable; |
37
|
|
|
|
38
|
|
|
// Explicitly mention the translation model so on inheritance the child class uses the proper default translation model |
39
|
|
|
protected $translationModel = ModuleTranslation::class; |
40
|
|
|
protected $translationForeignKey = 'module_id'; |
41
|
|
|
protected $translatedAttributes = [ |
42
|
|
|
'title', 'content' |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
public $useTranslationFallback = true; |
46
|
|
|
public $table = "modules"; |
47
|
|
|
protected $guarded = []; |
48
|
|
|
protected $dates = ['deleted_at']; |
49
|
|
|
protected $with = ['translations']; |
50
|
|
|
|
51
|
|
|
protected $baseViewPath; |
52
|
|
|
|
53
|
87 |
|
public function __construct(array $attributes = []) |
54
|
|
|
{ |
55
|
87 |
|
$this->constructWithSnippets(); |
56
|
|
|
|
57
|
87 |
|
if (!isset($this->baseViewPath)) { |
58
|
87 |
|
$this->baseViewPath = config('thinktomorrow.chief.base-view-paths.modules', 'modules'); |
59
|
|
|
} |
60
|
|
|
|
61
|
87 |
|
parent::__construct($attributes); |
62
|
87 |
|
} |
63
|
|
|
|
64
|
433 |
|
public static function managedModelKey(): string |
65
|
|
|
{ |
66
|
433 |
|
if(isset(static::$managedModelKey)){ |
67
|
433 |
|
return static::$managedModelKey; |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
throw new \Exception('Missing required static property \'managedModelKey\' on ' . static::class. '.'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Enlist all available managed modules. |
75
|
|
|
* @return Collection of ManagedModelDetails |
76
|
|
|
*/ |
77
|
1 |
|
public static function available(): Collection |
78
|
|
|
{ |
79
|
1 |
|
return app(Managers::class)->findDetailsByTag('module'); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public function page() |
83
|
|
|
{ |
84
|
1 |
|
return $this->belongsTo(Page::class, 'page_id'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* The page specific ones are the text modules |
89
|
|
|
* which are added via the page builder |
90
|
|
|
* |
91
|
|
|
* @param $query |
92
|
|
|
*/ |
93
|
1 |
|
public function scopeWithoutPageSpecific($query) |
94
|
|
|
{ |
95
|
1 |
|
$query->whereNull('page_id'); |
96
|
1 |
|
} |
97
|
|
|
|
98
|
26 |
|
public function isPageSpecific(): bool |
99
|
|
|
{ |
100
|
26 |
|
return !is_null($this->page_id); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Each page / Module model can expose some custom fields. Add here the list of fields defined as name => Field where Field |
105
|
|
|
* is an instance of \Thinktomorrow\Chief\Fields\Types\Field |
106
|
|
|
* |
107
|
|
|
* @param null $key |
|
|
|
|
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
public function customFields() |
111
|
|
|
{ |
112
|
|
|
return []; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Each module model can expose the managed translatable fields. These should be included as attributes just like the regular |
117
|
|
|
* translatable attributes. This method allows for easy installation of the form fields in chief. |
118
|
|
|
* |
119
|
|
|
* @param null $key |
|
|
|
|
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
8 |
|
final public static function translatableFields($key = null) |
123
|
|
|
{ |
124
|
8 |
|
$translatableFields = array_merge(static::defaultTranslatableFields(), static::customTranslatableFields()); |
125
|
|
|
|
126
|
8 |
|
return $key ? array_pluck($translatableFields, $key) : $translatableFields; |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* The custom addition of fields for a module model. |
131
|
|
|
* |
132
|
|
|
* To add a field, you should: |
133
|
|
|
* 1. override this method with your own and return the comprised list of fields. |
134
|
|
|
* 2. Setup the proper migrations and add the new field to the translatable values of the collection. |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
8 |
|
public static function customTranslatableFields(): array |
139
|
|
|
{ |
140
|
8 |
|
return []; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* The default set of fields for a module model. |
145
|
|
|
* |
146
|
|
|
* If you wish to remove any of these fields, you should: |
147
|
|
|
* 1. override this method with your own and return the comprised list of fields. |
148
|
|
|
* 2. Provide a migration to remove the column from database and remove the fields from the translatable values of the model. |
149
|
|
|
* |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
public static function defaultTranslatableFields(): array |
153
|
|
|
{ |
154
|
|
|
return [ |
155
|
|
|
InputField::make('title')->label('titel'), |
|
|
|
|
156
|
|
|
HtmlField::make('content')->label('Inhoud'), |
|
|
|
|
157
|
|
|
]; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function mediaUrls($type = null, $size = 'full'): Collection |
161
|
|
|
{ |
162
|
|
|
return $this->getAllFiles($type)->map->getFileUrl($size); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function mediaUrl($type = null, $size = 'full'): ?string |
166
|
|
|
{ |
167
|
|
|
return $this->mediaUrls($type, $size)->first(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public static function mediaFields($key = null) |
171
|
|
|
{ |
172
|
|
|
$types = [ |
173
|
|
|
// MediaType::BACKGROUND => [ |
174
|
|
|
// 'type' => MediaType::BACKGROUND, |
175
|
|
|
// 'label' => 'Achtergrond afbeelding', |
176
|
|
|
// 'description' => '', |
177
|
|
|
// ] |
178
|
|
|
]; |
179
|
|
|
|
180
|
|
|
return $key ? array_pluck($types, $key) : $types; |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
3 |
|
public static function findBySlug($slug) |
184
|
|
|
{ |
185
|
3 |
|
return static::where('slug', $slug)->first(); |
186
|
|
|
} |
187
|
|
|
|
188
|
16 |
|
public function flatReference(): FlatReference |
189
|
|
|
{ |
190
|
16 |
|
return new FlatReference(static::class, $this->id); |
191
|
|
|
} |
192
|
|
|
|
193
|
26 |
|
public function flatReferenceLabel(): string |
194
|
|
|
{ |
195
|
26 |
|
return $this->slug ?? ''; |
196
|
|
|
} |
197
|
|
|
|
198
|
1 |
|
public function flatReferenceGroup(): string |
199
|
|
|
{ |
200
|
1 |
|
$classKey = get_class($this); |
201
|
1 |
|
$labelSingular = property_exists($this, 'labelSingular') ? $this->labelSingular : str_singular($classKey); |
|
|
|
|
202
|
|
|
|
203
|
1 |
|
return $labelSingular; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|