1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* App\Models\Forumpost. |
10
|
|
|
* |
11
|
|
|
* @property int $id |
12
|
|
|
* @property int $forumid |
13
|
|
|
* @property int $parentid |
14
|
|
|
* @property int $users_id |
15
|
|
|
* @property string $subject |
16
|
|
|
* @property string $message |
17
|
|
|
* @property bool $locked |
18
|
|
|
* @property bool $sticky |
19
|
|
|
* @property int $replies |
20
|
|
|
* @property \Carbon\Carbon|null $created_at |
21
|
|
|
* @property \Carbon\Carbon|null $updated_at |
22
|
|
|
* |
23
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Forumpost whereCreatedAt($value) |
24
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Forumpost whereForumid($value) |
25
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Forumpost whereId($value) |
26
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Forumpost whereLocked($value) |
27
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Forumpost whereMessage($value) |
28
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Forumpost whereParentid($value) |
29
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Forumpost whereReplies($value) |
30
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Forumpost whereSticky($value) |
31
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Forumpost whereSubject($value) |
32
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Forumpost whereUpdatedAt($value) |
33
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Forumpost whereUsersId($value) |
34
|
|
|
* |
35
|
|
|
* @mixin \Eloquent |
36
|
|
|
* |
37
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Forumpost newModelQuery() |
38
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Forumpost newQuery() |
39
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Forumpost query() |
40
|
|
|
*/ |
41
|
|
|
class Forumpost extends Model |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $table = 'forumpost'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
protected $dateFormat = false; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $guarded = []; |
57
|
|
|
|
58
|
|
|
public static function add($parentId, $userid, $subject, $message, int $locked = 0, int $sticky = 0, int $replies = 0): int |
59
|
|
|
{ |
60
|
|
|
if ($message === '') { |
61
|
|
|
return -1; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($parentId !== 0) { |
65
|
|
|
$par = self::getParent($parentId); |
66
|
|
|
if ($par === false) { |
|
|
|
|
67
|
|
|
return -1; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
self::query()->where('id', $parentId)->increment('replies', 1, ['updated_at' => now()]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return self::create( |
74
|
|
|
[ |
75
|
|
|
'forumid' => 1, |
76
|
|
|
'parentid' => $parentId, |
77
|
|
|
'users_id' => $userid, |
78
|
|
|
'subject' => $subject, |
79
|
|
|
'message' => $message, |
80
|
|
|
'locked' => $locked, |
81
|
|
|
'sticky' => $sticky, |
82
|
|
|
'replies' => $replies, |
83
|
|
|
] |
84
|
|
|
)->id; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get parent of the forum post. |
89
|
|
|
* |
90
|
|
|
* |
91
|
|
|
* @return Model|null|static |
92
|
|
|
*/ |
93
|
|
|
public static function getParent($parent) |
94
|
|
|
{ |
95
|
|
|
return self::query() |
96
|
|
|
->where('forumpost.id', $parent) |
97
|
|
|
->select(['forumpost.*', 'users.username']) |
98
|
|
|
->leftJoin('users', 'users.id', '=', 'forumpost.users_id') |
99
|
|
|
->first(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get forum posts for a parent category. |
104
|
|
|
* |
105
|
|
|
* |
106
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] |
107
|
|
|
*/ |
108
|
|
|
public static function getPosts($parent) |
109
|
|
|
{ |
110
|
|
|
return self::query() |
111
|
|
|
->where('forumpost.id', $parent) |
112
|
|
|
->orWhere('forumpost.parentid', $parent) |
113
|
|
|
->leftJoin('users', 'users.id', '=', 'forumpost.users_id') |
114
|
|
|
->leftJoin('roles', 'roles.id', '=', 'users.roles_id') |
115
|
|
|
->orderBy('forumpost.created_at') |
|
|
|
|
116
|
|
|
->limit(250) |
117
|
|
|
->select(['forumpost.*', 'users.username', 'roles.name as rolename']) |
118
|
|
|
->get(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get post from forum. |
123
|
|
|
* |
124
|
|
|
* |
125
|
|
|
* @return Model|null|static |
126
|
|
|
*/ |
127
|
|
|
public static function getPost($id) |
128
|
|
|
{ |
129
|
|
|
return self::query()->where('id', $id)->first(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get browse range for forum. |
134
|
|
|
* |
135
|
|
|
* |
136
|
|
|
* @param $start |
137
|
|
|
*/ |
138
|
|
|
public static function getBrowseRange(): LengthAwarePaginator |
139
|
|
|
{ |
140
|
|
|
return self::query() |
141
|
|
|
->where('forumpost.parentid', '=', 0) |
142
|
|
|
->leftJoin('users', 'users.id', '=', 'forumpost.users_id') |
143
|
|
|
->leftJoin('roles', 'roles.id', '=', 'users.roles_id') |
144
|
|
|
->select(['forumpost.*', 'users.username', 'roles.name as rolename']) |
145
|
|
|
->orderByDesc('forumpost.updated_at') |
|
|
|
|
146
|
|
|
->paginate(config('nntmux.items_per_page')); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Delete parent category from forum. |
151
|
|
|
*/ |
152
|
|
|
public static function deleteParent($parent): void |
153
|
|
|
{ |
154
|
|
|
self::query()->where('id', $parent)->orWhere('parentid', $parent)->delete(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Delete post from forum. |
159
|
|
|
*/ |
160
|
|
|
public static function deletePost($id): void |
161
|
|
|
{ |
162
|
|
|
$post = self::getPost($id); |
163
|
|
|
if ($post) { |
164
|
|
|
if ((int) $post['parentid'] === 0) { |
165
|
|
|
self::deleteParent($id); |
166
|
|
|
} else { |
167
|
|
|
self::query()->where('id', $id)->delete(); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Delete user from forum. |
174
|
|
|
*/ |
175
|
|
|
public static function deleteUser($id): void |
176
|
|
|
{ |
177
|
|
|
self::query()->where('users_id', $id)->delete(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public static function getCountForUser($uid): int |
181
|
|
|
{ |
182
|
|
|
$res = self::query()->where('users_id', $uid)->count('id'); |
183
|
|
|
|
184
|
|
|
return $res ?? 0; |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get range of posts for user. |
189
|
|
|
* |
190
|
|
|
* |
191
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] |
192
|
|
|
*/ |
193
|
|
|
public static function getForUserRange($uid, $start, $num) |
194
|
|
|
{ |
195
|
|
|
$range = self::query() |
196
|
|
|
->where('forumpost.users_id', $uid) |
197
|
|
|
->select(['forumpost.*', 'users.username']) |
198
|
|
|
->leftJoin('users', 'users.id', '=', 'forumpost.users_id') |
199
|
|
|
->orderByDesc('forumpost.created_at'); |
|
|
|
|
200
|
|
|
if ($start !== false) { |
201
|
|
|
$range->limit($num)->offset($start); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $range->get(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Edit forum post for user. |
209
|
|
|
*/ |
210
|
|
|
public static function editPost($id, $message, $uid): void |
211
|
|
|
{ |
212
|
|
|
$post = self::getPost($id); |
213
|
|
|
if ($post) { |
214
|
|
|
self::query()->where(['id' => $id, 'users_id' => $uid])->update(['message' => $message]); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Lock forum topic. |
220
|
|
|
*/ |
221
|
|
|
public static function lockUnlockTopic($id, $lock): void |
222
|
|
|
{ |
223
|
|
|
self::query()->where('id', $id)->orWhere('parentid', $id)->update(['locked' => $lock]); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|