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