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