Issues (516)

app/Models/Content.php (1 issue)

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_INDEX = 3;
48
49
    // Status constants
50
    public const STATUS_ENABLED = 1;
51
    public const STATUS_DISABLED = 0;
52
53
    // Role constants (aligned with User roles)
54
    public const ROLE_EVERYONE = 0;
55
    public const ROLE_LOGGED_IN = 1;
56
    public const ROLE_ADMIN = 2;
57
58
    /**
59
     * @var string
60
     */
61
    protected $table = 'content';
62
63
    /**
64
     * @var bool
65
     */
66
    public $timestamps = false;
67
68
    /**
69
     * @var bool
70
     */
71
    protected $dateFormat = false;
72
73
    /**
74
     * @var array
75
     */
76
    protected $guarded = [];
77
78
    /**
79
     * @var array
80
     */
81
    protected $casts = [
82
        'contenttype' => 'integer',
83
        'status' => 'integer',
84
        'ordinal' => 'integer',
85
        'role' => 'integer',
86
    ];
87
88
    /**
89
     * Scope: Get only active content.
90
     */
91
    public function scopeActive(Builder $query): Builder
92
    {
93
        return $query->where('status', self::STATUS_ENABLED);
94
    }
95
96
    /**
97
     * Scope: Get content of a specific type.
98
     */
99
    public function scopeOfType(Builder $query, int $type): Builder
100
    {
101
        return $query->where('contenttype', $type);
102
    }
103
104
    /**
105
     * Scope: Get content accessible by a specific role.
106
     */
107
    public function scopeForRole(Builder $query, int $role): Builder
108
    {
109
        // Admins and moderators can see everything
110
        if (\in_array($role, [User::ROLE_ADMIN, User::ROLE_MODERATOR], true)) {
111
            return $query;
112
        }
113
114
        // Others can only see content for their role or everyone
115
        return $query->where(function ($q) use ($role) {
116
            $q->where('role', self::ROLE_EVERYONE)
117
                ->orWhere('role', $role);
118
        });
119
    }
120
121
    /**
122
     * Scope: Get front page content.
123
     */
124
    public function scopeFrontPage(Builder $query): Builder
125
    {
126
        return $query->active()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->active()-...ordinal, 1000000), id') could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
127
            ->ofType(self::TYPE_INDEX)
128
            ->orderByRaw('ordinal ASC, COALESCE(ordinal, 1000000), id');
129
    }
130
131
    /**
132
     * Get content type label.
133
     */
134
    public function getContentTypeLabel(): string
135
    {
136
        return match ($this->contenttype) {
137
            self::TYPE_USEFUL => 'Useful Link',
138
            self::TYPE_INDEX => 'Homepage',
139
            default => 'Unknown',
140
        };
141
    }
142
143
    /**
144
     * Get role label.
145
     */
146
    public function getRoleLabel(): string
147
    {
148
        return match ($this->role) {
149
            self::ROLE_EVERYONE => 'Everyone',
150
            self::ROLE_LOGGED_IN => 'Logged in Users',
151
            self::ROLE_ADMIN => 'Admins',
152
            default => 'Unknown',
153
        };
154
    }
155
156
    /**
157
     * Check if content is active.
158
     */
159
    public function isActive(): bool
160
    {
161
        return $this->status === self::STATUS_ENABLED;
162
    }
163
164
    /**
165
     * Check if content is a homepage type.
166
     */
167
    public function isHomepage(): bool
168
    {
169
        return $this->contenttype === self::TYPE_INDEX;
170
    }
171
}
172