1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Pages\Models; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Spatie\Sluggable\SlugOptions; |
9
|
|
|
use Rinvex\Support\Traits\HasSlug; |
10
|
|
|
use Rinvex\Support\Traits\Macroable; |
11
|
|
|
use Spatie\EloquentSortable\Sortable; |
12
|
|
|
use Illuminate\Database\Eloquent\Model; |
13
|
|
|
use Rinvex\Support\Traits\HasTranslations; |
14
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
15
|
|
|
use Spatie\EloquentSortable\SortableTrait; |
16
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
17
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Rinvex\Pages\Models\Page. |
21
|
|
|
* |
22
|
|
|
* @property int $id |
23
|
|
|
* @property string $uri |
24
|
|
|
* @property string $slug |
25
|
|
|
* @property string $route |
26
|
|
|
* @property string $domain |
27
|
|
|
* @property string $middleware |
28
|
|
|
* @property array $title |
29
|
|
|
* @property array $subtitle |
30
|
|
|
* @property array $excerpt |
31
|
|
|
* @property array $content |
32
|
|
|
* @property string $view |
33
|
|
|
* @property bool $is_active |
34
|
|
|
* @property int $sort_order |
35
|
|
|
* @property \Carbon\Carbon|null $created_at |
36
|
|
|
* @property \Carbon\Carbon|null $updated_at |
37
|
|
|
* @property \Carbon\Carbon|null $deleted_at |
38
|
|
|
* |
39
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page ordered($direction = 'asc') |
40
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereContent($value) |
41
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereCreatedAt($value) |
42
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereDeletedAt($value) |
43
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereDomain($value) |
44
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereExcerpt($value) |
45
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereId($value) |
46
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereIsActive($value) |
47
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereMiddleware($value) |
48
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereRoute($value) |
49
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereSlug($value) |
50
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereSortOrder($value) |
51
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereSubtitle($value) |
52
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereTitle($value) |
53
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereUpdatedAt($value) |
54
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereUri($value) |
55
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Pages\Models\Page whereView($value) |
56
|
|
|
* @mixin \Eloquent |
57
|
|
|
*/ |
58
|
|
|
class Page extends Model implements Sortable |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
use HasSlug; |
61
|
|
|
use Macroable; |
62
|
|
|
use SoftDeletes; |
63
|
|
|
use SortableTrait; |
64
|
|
|
use HasTranslations; |
65
|
|
|
use ValidatingTrait; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
protected $fillable = [ |
71
|
|
|
'uri', |
72
|
|
|
'slug', |
73
|
|
|
'title', |
74
|
|
|
'route', |
75
|
|
|
'subtitle', |
76
|
|
|
'domain', |
77
|
|
|
'middleware', |
78
|
|
|
'excerpt', |
79
|
|
|
'content', |
80
|
|
|
'view', |
81
|
|
|
'is_active', |
82
|
|
|
'sort_order', |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
protected $casts = [ |
89
|
|
|
'uri' => 'string', |
90
|
|
|
'slug' => 'string', |
91
|
|
|
'route' => 'string', |
92
|
|
|
'domain' => 'string', |
93
|
|
|
'middleware' => 'string', |
94
|
|
|
'view' => 'string', |
95
|
|
|
'is_active' => 'boolean', |
96
|
|
|
'sort_order' => 'integer', |
97
|
|
|
'deleted_at' => 'datetime', |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
protected $observables = [ |
104
|
|
|
'validating', |
105
|
|
|
'validated', |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public $translatable = [ |
112
|
|
|
'title', |
113
|
|
|
'subtitle', |
114
|
|
|
'excerpt', |
115
|
|
|
'content', |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public $sortable = [ |
122
|
|
|
'order_column_name' => 'sort_order', |
123
|
|
|
]; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* The default rules that the model will validate against. |
127
|
|
|
* |
128
|
|
|
* @var array |
129
|
|
|
*/ |
130
|
|
|
protected $rules = []; |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Whether the model should throw a |
134
|
|
|
* ValidationException if it fails validation. |
135
|
|
|
* |
136
|
|
|
* @var bool |
137
|
|
|
*/ |
138
|
|
|
protected $throwValidationExceptions = true; |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Create a new Eloquent model instance. |
142
|
|
|
* |
143
|
|
|
* @param array $attributes |
144
|
|
|
*/ |
145
|
|
|
public function __construct(array $attributes = []) |
146
|
|
|
{ |
147
|
|
|
$this->setTable(config('rinvex.pages.tables.pages')); |
148
|
|
|
$this->mergeRules([ |
149
|
|
|
'uri' => 'required|regex:/^([0-9a-z\/_-]+)$/|max:150|unique:'.config('rinvex.pages.tables.pages').',uri,NULL,id,domain,'.($this->domain ?? 'null'), |
150
|
|
|
'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.pages.tables.pages').',slug,NULL,id,domain,'.($this->domain ?? 'null'), |
151
|
|
|
'route' => 'required|regex:/^([0-9a-z\._-]+)$/|max:150|unique:'.config('rinvex.pages.tables.pages').',route,NULL,id,domain,'.($this->domain ?? 'null'), |
152
|
|
|
'domain' => 'nullable|string|strip_tags|max:150', |
153
|
|
|
'middleware' => 'nullable|string|strip_tags|max:150', |
154
|
|
|
'title' => 'required|string|strip_tags|max:150', |
155
|
|
|
'subtitle' => 'nullable|string|strip_tags|max:150', |
156
|
|
|
'excerpt' => 'nullable|string|max:32768', |
157
|
|
|
'content' => 'nullable|string|max:262144', |
158
|
|
|
'view' => 'required|string|strip_tags|max:150', |
159
|
|
|
'is_active' => 'sometimes|boolean', |
160
|
|
|
'sort_order' => 'nullable|integer|max:100000', |
161
|
|
|
]); |
162
|
|
|
|
163
|
|
|
app('rinvex.pages.pageables')->each(function ($pageable, $key) { |
164
|
|
|
$field = Str::plural($key); |
165
|
|
|
$this->mergeFillable([$field]); |
166
|
|
|
$this->mergeRules([$field => 'nullable|array']); |
167
|
|
|
}); |
168
|
|
|
|
169
|
|
|
parent::__construct($attributes); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Determine if a set mutator exists for an attribute. |
174
|
|
|
* |
175
|
|
|
* @param string $key |
176
|
|
|
* |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
|
|
public function hasSetMutator($key) |
180
|
|
|
{ |
181
|
|
|
$method = 'set'.Str::studly($key).'Attribute'; |
182
|
|
|
|
183
|
|
|
return method_exists($this, $method) ?: self::hasMacro($method); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Determine if a get mutator exists for an attribute. |
188
|
|
|
* |
189
|
|
|
* @param string $key |
190
|
|
|
* |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
|
public function hasGetMutator($key) |
194
|
|
|
{ |
195
|
|
|
$method = 'get'.Str::studly($key).'Attribute'; |
196
|
|
|
|
197
|
|
|
return method_exists($this, $method) ?: self::hasMacro($method); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get all attached models of the given class to the page. |
202
|
|
|
* |
203
|
|
|
* @param string $class |
204
|
|
|
* |
205
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
206
|
|
|
*/ |
207
|
|
|
public function entries(string $class): MorphToMany |
208
|
|
|
{ |
209
|
|
|
return $this->morphedByMany($class, 'pageable', config('rinvex.pages.tables.pageables'), 'page_id', 'pageable_id', 'id', 'id'); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get the options for generating the slug. |
214
|
|
|
* |
215
|
|
|
* @return \Spatie\Sluggable\SlugOptions |
216
|
|
|
*/ |
217
|
|
|
public function getSlugOptions(): SlugOptions |
218
|
|
|
{ |
219
|
|
|
return SlugOptions::create() |
220
|
|
|
->doNotGenerateSlugsOnUpdate() |
221
|
|
|
->generateSlugsFrom('title') |
222
|
|
|
->saveSlugsTo('slug'); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Activate the page. |
227
|
|
|
* |
228
|
|
|
* @return $this |
229
|
|
|
*/ |
230
|
|
|
public function activate() |
231
|
|
|
{ |
232
|
|
|
$this->update(['is_active' => true]); |
233
|
|
|
|
234
|
|
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Deactivate the page. |
239
|
|
|
* |
240
|
|
|
* @return $this |
241
|
|
|
*/ |
242
|
|
|
public function deactivate() |
243
|
|
|
{ |
244
|
|
|
$this->update(['is_active' => false]); |
245
|
|
|
|
246
|
|
|
return $this; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|