1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NovaSeoEntity\Nova\Resources; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Laravel\Nova\Fields\ID; |
9
|
|
|
use Laravel\Nova\Fields\Image; |
10
|
|
|
use Laravel\Nova\Fields\MorphTo; |
11
|
|
|
use Laravel\Nova\Fields\Text; |
12
|
|
|
use Laravel\Nova\Fields\Textarea; |
13
|
|
|
use Laravel\Nova\Http\Requests\NovaRequest; |
14
|
|
|
use Laravel\Nova\Panel; |
15
|
|
|
use Laravel\Nova\Resource as NovaResource; |
16
|
|
|
use NovaSeoEntity\Models\SEOInfo as SEOInfoModel; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @extends NovaResource<SEOInfoModel> |
20
|
|
|
*/ |
21
|
|
|
class SEOInfo extends NovaResource |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The model the resource corresponds to. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public static $model = SEOInfoModel::class; |
29
|
|
|
|
30
|
|
|
public static $displayInNavigation = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Model|null |
34
|
|
|
*/ |
35
|
|
|
protected ?Model $cachedViaModel = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inerhitDoc |
39
|
|
|
*/ |
40
|
4 |
|
public static function label() |
41
|
|
|
{ |
42
|
4 |
|
return trans('nova-seo-entity::resource.label'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inerhitDoc |
47
|
|
|
*/ |
48
|
4 |
|
public static function uriKey() |
49
|
|
|
{ |
50
|
4 |
|
return 'seo-entities'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The single value that should be used to represent the resource when being displayed. |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
public static $title = 'title'; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The single value that should be used to represent the resource when being displayed. |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
public static array $morphToTypes = []; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The columns that should be searched. |
69
|
|
|
* |
70
|
|
|
* @var array |
71
|
|
|
*/ |
72
|
|
|
public static $search = [ |
73
|
|
|
'id', |
74
|
|
|
'title', |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set morph types. |
79
|
|
|
* |
80
|
|
|
* @param array $morphToTypes |
81
|
|
|
*/ |
82
|
9 |
|
public static function morphToTypes(array $morphToTypes = []) |
83
|
|
|
{ |
84
|
9 |
|
static::$morphToTypes = $morphToTypes; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get morph types. |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
4 |
|
public function getMorphToTypes(): array |
93
|
|
|
{ |
94
|
4 |
|
return array_unique(array_merge(static::$morphToTypes, config('nova-seo-entity.morph.types'))); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the fields displayed by the resource. |
99
|
|
|
* |
100
|
|
|
* @param \Illuminate\Http\Request $request |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
* @throws \Exception |
104
|
|
|
*/ |
105
|
4 |
|
public function fields(Request $request): array |
106
|
|
|
{ |
107
|
4 |
|
return [ |
108
|
4 |
|
ID::make()->sortable(), |
109
|
|
|
|
110
|
4 |
|
MorphTo::make(trans('nova-seo-entity::resource.fields.seoptimisable'), 'seoptimisable') |
111
|
4 |
|
->types($this->getMorphToTypes()) |
112
|
4 |
|
->searchable() |
113
|
4 |
|
->help(trans('nova-seo-entity::resource.help.seoptimisable')), |
114
|
|
|
|
115
|
4 |
|
new Panel(trans('nova-seo-entity::resource.panels.general'), $this->generalInformationFields($request)), |
116
|
4 |
|
]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param Request $request |
121
|
|
|
* @param bool $fresh |
122
|
|
|
* |
123
|
|
|
* @return Model|null |
124
|
|
|
*/ |
125
|
4 |
|
protected function getViaModel(Request $request, bool $fresh = false): ?Model |
126
|
|
|
{ |
127
|
4 |
|
if (!$fresh && $this->cachedViaModel) { |
128
|
1 |
|
return $this->cachedViaModel; |
129
|
|
|
} |
130
|
4 |
|
if ($request instanceof NovaRequest |
131
|
4 |
|
&& $request->viaRelationship() |
132
|
4 |
|
&& $request->isCreateOrAttachRequest()) { |
133
|
|
|
/** @var class-string<Model> $classString */ |
134
|
1 |
|
$classString = $request->newViaResource()->model(); |
135
|
|
|
/** @var Model $viaModel */ |
136
|
1 |
|
$viaModel = $classString::find($request->viaResourceId); |
137
|
|
|
|
138
|
1 |
|
return $this->cachedViaModel = $viaModel; |
139
|
|
|
} |
140
|
|
|
|
141
|
3 |
|
return null; |
142
|
|
|
} |
143
|
|
|
|
144
|
4 |
|
protected function getDefaultSeoValueFor(Request $request, string $key): mixed |
145
|
|
|
{ |
146
|
4 |
|
$viaModel = $this->getViaModel($request); |
147
|
4 |
|
if ($viaModel && method_exists($viaModel, 'getNewInstanceSeoValueFor')) { |
148
|
1 |
|
return $viaModel->getNewInstanceSeoValueFor($key, $request); |
149
|
|
|
} |
150
|
|
|
|
151
|
3 |
|
return null; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get the general information fields. |
156
|
|
|
* |
157
|
|
|
* @param Request $request |
158
|
|
|
* |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
4 |
|
protected function generalInformationFields(Request $request): array |
162
|
|
|
{ |
163
|
4 |
|
return [ |
164
|
4 |
|
Text::make(trans('nova-seo-entity::resource.fields.title'), 'title') |
165
|
4 |
|
->rules('nullable', 'string', 'max:255') |
166
|
4 |
|
->default($this->getDefaultSeoValueFor($request, 'title')) |
167
|
4 |
|
->help(trans('nova-seo-entity::resource.help.title')) |
168
|
4 |
|
->showOnPreview() |
169
|
4 |
|
->hideFromIndex(), |
170
|
|
|
|
171
|
4 |
|
Textarea::make(trans('nova-seo-entity::resource.fields.description'), 'description') |
172
|
4 |
|
->rules('nullable', 'string', 'max:255') |
173
|
4 |
|
->default($this->getDefaultSeoValueFor($request, 'description')) |
174
|
4 |
|
->help(trans('nova-seo-entity::resource.help.description')) |
175
|
4 |
|
->showOnPreview() |
176
|
4 |
|
->hideFromIndex(), |
177
|
|
|
|
178
|
4 |
|
Text::make(trans('nova-seo-entity::resource.fields.canonical'), 'canonical') |
179
|
4 |
|
->rules('nullable', 'string', 'max:255') |
180
|
4 |
|
->default($this->getDefaultSeoValueFor($request, 'canonical')) |
181
|
4 |
|
->help(trans('nova-seo-entity::resource.help.canonical')) |
182
|
4 |
|
->showOnPreview() |
183
|
4 |
|
->hideFromIndex(), |
184
|
|
|
|
185
|
4 |
|
Image::make(trans('nova-seo-entity::resource.fields.image'), 'image') |
186
|
4 |
|
->store(function ($request, SEOInfoModel $model, $attribute, $requestAttribute, $storageDisk, $storageDir) { |
187
|
1 |
|
return function () use ($request, $model, $attribute, $requestAttribute, $storageDisk, $storageDir) { |
|
|
|
|
188
|
1 |
|
$model->$attribute = $model->seoImage() |
189
|
1 |
|
->upload( |
190
|
1 |
|
$request->file($requestAttribute), |
191
|
1 |
|
"{$model->getKey()}/" . Str::uuid(), |
192
|
1 |
|
$model->$attribute |
193
|
1 |
|
); |
194
|
1 |
|
$model->save(); |
195
|
1 |
|
}; |
196
|
4 |
|
}) |
197
|
4 |
|
->maxWidth(300) |
198
|
4 |
|
->preview(fn ($value, $storageDisk, SEOInfoModel $model) => $model->seoImage()->url('thumbnail')) |
199
|
4 |
|
->thumbnail(fn ($value, $storageDisk, SEOInfoModel $model) => $model->seoImage()->url('thumbnail')) |
200
|
4 |
|
->delete(fn ($request, SEOInfoModel $model, $storageDisk, $storagePath) => $model->seoImage()->delete()) |
|
|
|
|
201
|
4 |
|
->help(trans('nova-seo-entity::resource.help.image')), |
202
|
|
|
|
203
|
4 |
|
Text::make(trans('nova-seo-entity::resource.fields.robots'), 'robots') |
204
|
4 |
|
->rules('nullable', 'string', 'max:255') |
205
|
4 |
|
->default($this->getDefaultSeoValueFor($request, 'robots')) |
206
|
4 |
|
->placeholder(trans('nova-seo-entity::resource.help.robots')) |
207
|
4 |
|
->help(trans('nova-seo-entity::resource.help.robots')) |
208
|
4 |
|
->showOnPreview() |
209
|
4 |
|
->hideFromIndex(), |
210
|
|
|
|
211
|
4 |
|
// image |
212
|
4 |
|
// meta |
213
|
4 |
|
// open_graph |
214
|
4 |
|
// twitter_card |
215
|
4 |
|
// json_ld |
216
|
4 |
|
// json_ld_multi |
217
|
4 |
|
]; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..