1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Pages; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Thinktomorrow\Chief\Audit\Audit; |
7
|
|
|
use Thinktomorrow\Chief\Fields\Fields; |
8
|
|
|
use Thinktomorrow\Chief\Filters\Filters; |
9
|
|
|
use Thinktomorrow\Chief\Fields\FieldsTab; |
10
|
|
|
use Thinktomorrow\Chief\Management\Manager; |
11
|
|
|
use Thinktomorrow\Chief\Fields\Types\TextField; |
12
|
|
|
use Thinktomorrow\Chief\Fields\FieldArrangement; |
13
|
|
|
use Thinktomorrow\Chief\Fields\Types\InputField; |
14
|
|
|
use Thinktomorrow\Chief\Fields\Types\MediaField; |
15
|
|
|
use Thinktomorrow\Chief\Management\Registration; |
16
|
|
|
use Thinktomorrow\Chief\Fields\RemainingFieldsTab; |
17
|
|
|
use Thinktomorrow\Chief\Management\AbstractManager; |
18
|
|
|
use Thinktomorrow\Chief\Management\Details\Details; |
19
|
|
|
use Thinktomorrow\Chief\Pages\Application\DeletePage; |
20
|
|
|
use Thinktomorrow\Chief\Concerns\Sluggable\UniqueSlug; |
21
|
|
|
use Thinktomorrow\Chief\Management\Exceptions\DeleteAborted; |
22
|
|
|
use Thinktomorrow\Chief\Management\Assistants\ArchiveAssistant; |
23
|
|
|
use Thinktomorrow\Chief\Management\Assistants\PublishAssistant; |
24
|
|
|
use Thinktomorrow\Chief\Management\Exceptions\NotAllowedManagerRoute; |
25
|
|
|
|
26
|
|
|
class PageManager extends AbstractManager implements Manager |
27
|
|
|
{ |
28
|
|
|
/** @var \Thinktomorrow\Chief\Concerns\Sluggable\UniqueSlug */ |
29
|
|
|
private $uniqueSlug; |
30
|
|
|
|
31
|
|
|
/** @var PageBuilderField */ |
|
|
|
|
32
|
|
|
private $pageBuilderField; |
33
|
|
|
|
34
|
|
|
protected $assistants = [ |
35
|
|
|
'archive' => ArchiveAssistant::class, |
36
|
|
|
'publish' => PublishAssistant::class, |
37
|
|
|
]; |
38
|
|
|
|
39
|
73 |
|
public function __construct(Registration $registration) |
40
|
|
|
{ |
41
|
73 |
|
parent::__construct($registration); |
42
|
|
|
|
43
|
|
|
$this->uniqueSlug = UniqueSlug::make(new PageTranslation)->slugResolver(function ($value) { |
44
|
43 |
|
return str_slug_slashed($value); |
45
|
73 |
|
}); |
46
|
73 |
|
} |
47
|
|
|
|
48
|
63 |
|
public function can($verb): bool |
49
|
|
|
{ |
50
|
|
|
try { |
51
|
63 |
|
$this->authorize($verb); |
52
|
3 |
|
} catch (NotAllowedManagerRoute $e) { |
53
|
3 |
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
60 |
|
return parent::can($verb); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $verb |
61
|
|
|
* @throws NotAllowedManagerRoute |
62
|
|
|
*/ |
63
|
63 |
|
private function authorize($verb) |
64
|
|
|
{ |
65
|
63 |
|
$permission = 'update-page'; |
66
|
|
|
|
67
|
63 |
|
if (in_array($verb, ['index','show'])) { |
68
|
17 |
|
$permission = 'view-page'; |
69
|
50 |
|
} elseif (in_array($verb, ['create','store'])) { |
70
|
14 |
|
$permission = 'create-page'; |
71
|
40 |
|
} elseif (in_array($verb, ['delete'])) { |
72
|
4 |
|
$permission = 'delete-page'; |
73
|
|
|
} |
74
|
|
|
|
75
|
63 |
|
if (! auth()->guard('chief')->user()->hasPermissionTo($permission)) { |
|
|
|
|
76
|
3 |
|
throw NotAllowedManagerRoute::notAllowedPermission($permission, $this); |
77
|
|
|
} |
78
|
60 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The set of fields that should be manageable for a certain model. |
82
|
|
|
* |
83
|
|
|
* Additionally, you should: |
84
|
|
|
* 1. Make sure to setup the proper migrations and |
85
|
|
|
* 2. For a translatable field you should add this field to the $translatedAttributes property of the model as well. |
86
|
|
|
* |
87
|
|
|
* @return Fields |
88
|
|
|
*/ |
89
|
40 |
|
public function fields(): Fields |
90
|
|
|
{ |
91
|
40 |
|
return new Fields([ |
92
|
40 |
|
$this->pageBuilderField(), |
93
|
40 |
|
InputField::make('title')->translatable($this->model->availableLocales()) |
|
|
|
|
94
|
40 |
|
->validation('required-fallback-locale|max:200', [], [ |
95
|
40 |
|
'trans.'.config('app.fallback_locale', 'nl').'.title' => 'title', |
96
|
|
|
]) |
97
|
40 |
|
->label('De titel van je '.$this->model->labelSingular ?? 'pagina') |
|
|
|
|
98
|
40 |
|
->description('Dit is de titel die zal worden getoond in de overzichten en modules.<br> Deze zal gebruikt worden als interne titel en slug van de nieuwe pagina.'), |
|
|
|
|
99
|
40 |
|
InputField::make('slug') |
100
|
40 |
|
->translatable($this->model->availableLocales()) |
101
|
40 |
|
->validation($this->model->id |
102
|
29 |
|
? 'required-fallback-locale|unique:page_translations,slug,' . $this->model->id . ',page_id' |
103
|
40 |
|
: 'required-fallback-locale|unique:page_translations,slug', [], [ |
104
|
40 |
|
'trans.'.config('app.fallback_locale', 'nl').'.slug' => 'slug' |
105
|
|
|
]) |
106
|
40 |
|
->label('Link') |
107
|
40 |
|
->description('De unieke url verwijzing naar deze pagina.') |
108
|
|
|
->prepend(collect($this->model->availableLocales())->mapWithKeys(function ($locale) { |
|
|
|
|
109
|
40 |
|
return [$locale => url($this->model->baseUrlSegment($locale)).'/']; |
|
|
|
|
110
|
40 |
|
})->all()), |
111
|
40 |
|
InputField::make('seo_title') |
112
|
40 |
|
->translatable($this->model->availableLocales()) |
113
|
40 |
|
->label('Zoekmachine titel'), |
114
|
40 |
|
TextField::make('seo_description') |
115
|
40 |
|
->translatable($this->model->availableLocales()) |
116
|
40 |
|
->label('Zoekmachine omschrijving') |
|
|
|
|
117
|
40 |
|
->description('omschrijving van de pagina zoals in search engines (o.a. google) wordt weergegeven.'), |
|
|
|
|
118
|
40 |
|
InputField::make('seo_keywords') |
119
|
40 |
|
->translatable($this->model->availableLocales()) |
120
|
40 |
|
->label('Zoekmachine sleutelwoorden') |
121
|
40 |
|
->description('sleutelwoorden van de pagina waarop in search engines (o.a google) gezocht kan worden.'), |
122
|
40 |
|
MediaField::make('seo_image') |
123
|
40 |
|
->label('Zoekmachine foto') |
|
|
|
|
124
|
40 |
|
->description('foto die bij het delen van deze pagina getoont word. (afmeting: 1200x627px)') |
|
|
|
|
125
|
|
|
]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public static function filters(): Filters |
129
|
|
|
{ |
130
|
|
|
return new Filters([ |
131
|
|
|
PublishedFilter::class |
132
|
|
|
]); |
133
|
|
|
} |
134
|
|
|
|
135
|
40 |
|
private function pageBuilderField() |
136
|
|
|
{ |
137
|
40 |
|
if ($this->pageBuilderField) { |
138
|
36 |
|
return $this->pageBuilderField; |
139
|
|
|
} |
140
|
|
|
|
141
|
40 |
|
return $this->pageBuilderField = $this->createPagebuilderField(); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
public function fieldArrangement($key = null): FieldArrangement |
145
|
|
|
{ |
146
|
2 |
|
if ($key == 'create') { |
147
|
|
|
return new FieldArrangement($this->fields()->filterBy(function ($field) { |
148
|
1 |
|
return $field->key == 'title'; |
149
|
1 |
|
})); |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
return new FieldArrangement($this->fields(), [ |
153
|
1 |
|
new FieldsTab('pagina', ['sections']), |
154
|
1 |
|
new RemainingFieldsTab('inhoud'), |
155
|
1 |
|
new FieldsTab('eigen modules', [], 'chief::back.pages._partials.modules'), |
156
|
1 |
|
new FieldsTab('seo', ['seo_title', 'seo_description', 'seo_keywords', 'seo_image']), |
157
|
|
|
]); |
158
|
|
|
} |
159
|
|
|
|
160
|
64 |
|
public function details(): Details |
161
|
|
|
{ |
162
|
|
|
// For existing model |
163
|
64 |
|
if ($this->model->id) { |
164
|
50 |
|
return parent::details() |
165
|
50 |
|
->set('title', $this->model->title) |
166
|
50 |
|
->set('intro', 'laatst aangepast op ' . $this->model->updated_at->format('d/m/Y H:i')) |
167
|
50 |
|
->set('context', '<span class="inline-s">' . $this->assistant('publish')->publicationStatusAsLabel() . '</span>'); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
17 |
|
return parent::details(); |
171
|
|
|
} |
172
|
|
|
|
173
|
42 |
|
public function saveFields(): Manager |
174
|
|
|
{ |
175
|
|
|
// Store the morph_key upon creation |
176
|
42 |
|
if (! $this->model->morph_key) { |
177
|
11 |
|
$this->model->morph_key = $this->model->morphKey(); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
42 |
|
return parent::saveFields(); |
181
|
|
|
} |
182
|
|
|
|
183
|
4 |
|
public function delete() |
184
|
|
|
{ |
185
|
4 |
|
if (request()->get('deleteconfirmation') !== 'DELETE') { |
186
|
1 |
|
throw new DeleteAborted(); |
187
|
|
|
} |
188
|
|
|
|
189
|
3 |
|
app(DeletePage::class)->handle($this->model->id); |
190
|
3 |
|
} |
191
|
|
|
|
192
|
13 |
|
public function storeRequest(Request $request): Request |
193
|
|
|
{ |
194
|
13 |
|
$trans = []; |
195
|
13 |
|
foreach ($request->get('trans', []) as $locale => $translation) { |
196
|
12 |
|
if (is_array_empty($translation)) { |
197
|
|
|
continue; |
198
|
|
|
} |
199
|
|
|
|
200
|
12 |
|
$translation = $this->enforceUniqueSlug($request->get('trans'), $locale, $this->model); |
|
|
|
|
201
|
12 |
|
$trans[$locale] = $this->addDefaultShortDescription($translation); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// Merge with request... |
205
|
13 |
|
return $request->merge(['trans' => $trans]); |
206
|
|
|
} |
207
|
|
|
|
208
|
35 |
|
public function updateRequest(Request $request): Request |
209
|
|
|
{ |
210
|
35 |
|
$trans = []; |
211
|
35 |
|
foreach ($request->get('trans', []) as $locale => $translation) { |
212
|
34 |
|
if (is_array_empty($translation)) { |
213
|
|
|
|
214
|
|
|
// Nullify all values |
215
|
|
|
$trans[$locale] = array_map(function ($value) { |
|
|
|
|
216
|
1 |
|
return null; |
217
|
1 |
|
}, $translation); |
218
|
1 |
|
continue; |
219
|
|
|
} |
220
|
|
|
|
221
|
34 |
|
$translation = $this->enforceUniqueSlug($request->get('trans'), $locale, $this->model); |
|
|
|
|
222
|
34 |
|
$trans[$locale] = $this->addDefaultShortDescription($translation); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
// Merge with request... |
226
|
35 |
|
return $request->merge(['trans' => $trans]); |
227
|
|
|
} |
228
|
|
|
|
229
|
11 |
|
public function afterStore($request) |
|
|
|
|
230
|
|
|
{ |
231
|
11 |
|
Audit::activity() |
232
|
11 |
|
->performedOn($this->model) |
|
|
|
|
233
|
11 |
|
->log('created'); |
234
|
11 |
|
} |
235
|
|
|
|
236
|
34 |
|
public function afterUpdate($request) |
|
|
|
|
237
|
|
|
{ |
238
|
34 |
|
Audit::activity() |
239
|
34 |
|
->performedOn($this->model) |
|
|
|
|
240
|
34 |
|
->log('edited'); |
241
|
34 |
|
} |
242
|
|
|
|
243
|
43 |
|
private function enforceUniqueSlug(array $translations, string $locale, Page $page): array |
244
|
|
|
{ |
245
|
43 |
|
$translation = $translations[$locale]; |
246
|
|
|
|
247
|
43 |
|
$translation['slug'] = $translation['slug'] ?? $translation['title']; |
248
|
43 |
|
$translation['slug'] = $this->uniqueSlug->get($translation['slug'], $page->getTranslation($locale)); |
|
|
|
|
249
|
|
|
|
250
|
43 |
|
return $translation; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param array $translation |
255
|
|
|
* @return array |
256
|
|
|
*/ |
257
|
43 |
|
private function addDefaultShortDescription(array $translation): array |
258
|
|
|
{ |
259
|
43 |
|
if (isset($translation['content'])) { |
260
|
1 |
|
$translation['short'] = $translation['short'] ?? teaser($translation['content'], 100); |
261
|
|
|
} |
262
|
|
|
|
263
|
43 |
|
return $translation; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths