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