|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* App\Models\Content. |
|
10
|
|
|
* |
|
11
|
|
|
* @property int $id |
|
12
|
|
|
* @property string $title |
|
13
|
|
|
* @property string|null $url |
|
14
|
|
|
* @property string|null $body |
|
15
|
|
|
* @property string $metadescription |
|
16
|
|
|
* @property string $metakeywords |
|
17
|
|
|
* @property int $contenttype |
|
18
|
|
|
* @property int $status |
|
19
|
|
|
* @property int|null $ordinal |
|
20
|
|
|
* @property int $role |
|
21
|
|
|
* |
|
22
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Content whereBody($value) |
|
23
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Content whereContenttype($value) |
|
24
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Content whereId($value) |
|
25
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Content whereMetadescription($value) |
|
26
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Content whereMetakeywords($value) |
|
27
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Content whereOrdinal($value) |
|
28
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Content whereRole($value) |
|
29
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Content whereStatus($value) |
|
30
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Content whereTitle($value) |
|
31
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Content whereUrl($value) |
|
32
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Content active() |
|
33
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Content ofType($type) |
|
34
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Content forRole($role) |
|
35
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Content frontPage() |
|
36
|
|
|
* |
|
37
|
|
|
* @mixin \Eloquent |
|
38
|
|
|
* |
|
39
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Content newModelQuery() |
|
40
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Content newQuery() |
|
41
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Content query() |
|
42
|
|
|
*/ |
|
43
|
|
|
class Content extends Model |
|
44
|
|
|
{ |
|
45
|
|
|
// Content type constants |
|
46
|
|
|
public const TYPE_USEFUL = 1; |
|
47
|
|
|
public const TYPE_ARTICLE = 2; |
|
48
|
|
|
public const TYPE_INDEX = 3; |
|
49
|
|
|
|
|
50
|
|
|
// Status constants |
|
51
|
|
|
public const STATUS_ENABLED = 1; |
|
52
|
|
|
public const STATUS_DISABLED = 0; |
|
53
|
|
|
|
|
54
|
|
|
// Role constants (aligned with User roles) |
|
55
|
|
|
public const ROLE_EVERYONE = 0; |
|
56
|
|
|
public const ROLE_LOGGED_IN = 1; |
|
57
|
|
|
public const ROLE_ADMIN = 2; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var string |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $table = 'content'; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var bool |
|
66
|
|
|
*/ |
|
67
|
|
|
public $timestamps = false; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var bool |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $dateFormat = false; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var array |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $guarded = []; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var array |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $casts = [ |
|
83
|
|
|
'contenttype' => 'integer', |
|
84
|
|
|
'status' => 'integer', |
|
85
|
|
|
'ordinal' => 'integer', |
|
86
|
|
|
'role' => 'integer', |
|
87
|
|
|
]; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Scope: Get only active content. |
|
91
|
|
|
*/ |
|
92
|
|
|
public function scopeActive(Builder $query): Builder |
|
93
|
|
|
{ |
|
94
|
|
|
return $query->where('status', self::STATUS_ENABLED); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Scope: Get content of a specific type. |
|
99
|
|
|
*/ |
|
100
|
|
|
public function scopeOfType(Builder $query, int $type): Builder |
|
101
|
|
|
{ |
|
102
|
|
|
return $query->where('contenttype', $type); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Scope: Get content accessible by a specific role. |
|
107
|
|
|
*/ |
|
108
|
|
|
public function scopeForRole(Builder $query, int $role): Builder |
|
109
|
|
|
{ |
|
110
|
|
|
// Admins and moderators can see everything |
|
111
|
|
|
if (\in_array($role, [User::ROLE_ADMIN, User::ROLE_MODERATOR], true)) { |
|
112
|
|
|
return $query; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// Others can only see content for their role or everyone |
|
116
|
|
|
return $query->where(function ($q) use ($role) { |
|
117
|
|
|
$q->where('role', self::ROLE_EVERYONE) |
|
118
|
|
|
->orWhere('role', $role); |
|
119
|
|
|
}); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Scope: Get front page content. |
|
124
|
|
|
*/ |
|
125
|
|
|
public function scopeFrontPage(Builder $query): Builder |
|
126
|
|
|
{ |
|
127
|
|
|
return $query->active() |
|
|
|
|
|
|
128
|
|
|
->ofType(self::TYPE_INDEX) |
|
129
|
|
|
->orderByRaw('ordinal ASC, COALESCE(ordinal, 1000000), id'); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Get content type label. |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getContentTypeLabel(): string |
|
136
|
|
|
{ |
|
137
|
|
|
return match ($this->contenttype) { |
|
138
|
|
|
self::TYPE_USEFUL => 'Useful Link', |
|
139
|
|
|
self::TYPE_ARTICLE => 'Article', |
|
140
|
|
|
self::TYPE_INDEX => 'Homepage', |
|
141
|
|
|
default => 'Unknown', |
|
142
|
|
|
}; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Get role label. |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getRoleLabel(): string |
|
149
|
|
|
{ |
|
150
|
|
|
return match ($this->role) { |
|
151
|
|
|
self::ROLE_EVERYONE => 'Everyone', |
|
152
|
|
|
self::ROLE_LOGGED_IN => 'Logged in Users', |
|
153
|
|
|
self::ROLE_ADMIN => 'Admins', |
|
154
|
|
|
default => 'Unknown', |
|
155
|
|
|
}; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Check if content is active. |
|
160
|
|
|
*/ |
|
161
|
|
|
public function isActive(): bool |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->status === self::STATUS_ENABLED; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Check if content is a homepage type. |
|
168
|
|
|
*/ |
|
169
|
|
|
public function isHomepage(): bool |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->contenttype === self::TYPE_INDEX; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|