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