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