1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* rmarchiv.tk |
5
|
|
|
* (c) 2016-2017 by Marcel 'ryg' Hering |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace App\Http\Controllers; |
9
|
|
|
|
10
|
|
|
use Carbon\Carbon; |
11
|
|
|
use App\Events\Obyx; |
12
|
|
|
use App\Models\BoardCat; |
13
|
|
|
use App\Models\BoardPoll; |
14
|
|
|
use App\Models\BoardPost; |
15
|
|
|
use App\Models\BoardThread; |
16
|
|
|
use Illuminate\Http\Request; |
17
|
|
|
use App\Models\BoardPollVote; |
18
|
|
|
use App\Helpers\DatabaseHelper; |
19
|
|
|
use App\Models\BoardPollAnswer; |
20
|
|
|
use Cmgmyr\Messenger\Models\Thread; |
21
|
|
|
use Illuminate\Support\Facades\Input; |
22
|
|
|
|
23
|
|
|
class BoardController extends Controller |
24
|
|
|
{ |
25
|
|
|
public function index() |
26
|
|
|
{ |
27
|
|
|
$cats = BoardCat::with('last_user', 'threads')->orderBy('order')->get(); |
28
|
|
|
|
29
|
|
|
return view('board.index', [ |
30
|
|
|
'cats' => $cats, |
31
|
|
|
]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function show_cat($catid) |
35
|
|
|
{ |
36
|
|
|
$thr = BoardThread::with('user', 'cat', 'last_user', 'posts') |
37
|
|
|
->whereCatId($catid) |
38
|
|
|
->orderBy('board_threads.pinned', 'desc') |
39
|
|
|
->orderBy('board_threads.last_created_at', 'desc') |
40
|
|
|
->orderBy('board_threads.id', 'desc') |
41
|
|
|
->paginate(25); |
42
|
|
|
|
43
|
|
|
$cat = BoardCat::whereId($catid)->first(); |
44
|
|
|
|
45
|
|
|
return view('board.threads.index', [ |
46
|
|
|
'threads' => $thr, |
47
|
|
|
'cat' => $cat, |
48
|
|
|
]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function create_cat() |
52
|
|
|
{ |
53
|
|
|
if (\Auth::user()->hasRole(['admin', 'owner', 'moderator'])) { |
54
|
|
|
$cats = \DB::table('board_cats') |
55
|
|
|
->select([ |
56
|
|
|
'id as catid', |
57
|
|
|
'title as cattitle', |
58
|
|
|
'order as catorder', |
59
|
|
|
'created_at as catdate', |
60
|
|
|
]) |
61
|
|
|
->selectRaw('(SELECT COUNT(id) FROM board_threads WHERE cat_id = board_cats.id) as catthreads') |
62
|
|
|
->selectRaw('(SELECT COUNT(id) FROM board_posts WHERE cat_id = board_cats.id) as catposts') |
63
|
|
|
->orderBy('board_cats.order') |
64
|
|
|
->get(); |
65
|
|
|
|
66
|
|
|
return view('board.cats.create', [ |
67
|
|
|
'cats' => $cats, |
68
|
|
|
]); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function order_cat($catid, $direction) |
73
|
|
|
{ |
74
|
|
|
if (\Auth::user()->hasRole(['admin', 'owner', 'moderator'])) { |
75
|
|
|
if ($direction == 'up') { |
76
|
|
|
\DB::table('board_cats') |
77
|
|
|
->where('id', '=', $catid) |
78
|
|
|
->increment('order'); |
79
|
|
|
} else { |
80
|
|
|
\DB::table('board_cats') |
81
|
|
|
->where('id', '=', $catid) |
82
|
|
|
->decrement('order'); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return redirect()->action('BoardController@create_cat'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function store_cat(Request $request) |
90
|
|
|
{ |
91
|
|
|
$this->validate($request, [ |
92
|
|
|
'name' => 'required', |
93
|
|
|
'desc' => 'required', |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
if (\Auth::user()->hasRole(['admin', 'owner', 'moderator'])) { |
97
|
|
|
\DB::table('board_cats')->insert([ |
98
|
|
|
'order' => 0, |
99
|
|
|
'title' => $request->get('name'), |
100
|
|
|
'desc' => $request->get('desc'), |
101
|
|
|
'created_at' => \Auth::id(), |
102
|
|
|
]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return redirect()->action('BoardController@create_cat'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function show_thread($threadid) |
109
|
|
|
{ |
110
|
|
|
$posts = BoardPost::with('user', 'thread', 'cat')->whereThreadId($threadid)->orderBy('id')->paginate(25); |
111
|
|
|
$poll = BoardPoll::whereThreadId($threadid)->first(); |
|
|
|
|
112
|
|
|
|
113
|
|
|
$pollanswers = null; |
114
|
|
|
$canvote = 1; |
|
|
|
|
115
|
|
|
$votecount = 0; |
|
|
|
|
116
|
|
|
$votes = null; |
|
|
|
|
117
|
|
|
|
118
|
|
|
if ($poll) { |
119
|
|
|
$pollanswers = BoardPollAnswer::with('votes')->wherePollId($poll->id)->get(); |
120
|
|
|
$polls = BoardPollVote::wherePollId($poll->id)->get(); |
|
|
|
|
121
|
|
|
$votecount = $polls->count(); |
|
|
|
|
122
|
|
|
|
123
|
|
|
$votes = $polls->where('user_id', '=', \Auth::id()); |
124
|
|
|
|
125
|
|
|
if ($votes->count() != 0) { |
126
|
|
|
$canvote = 0; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
DatabaseHelper::setThreadViewDate($threadid); |
131
|
|
|
|
132
|
|
|
if (! Input::get('page')) { |
133
|
|
|
return redirect('board/thread/'.$threadid.'?page='.$posts->lastPage()); |
134
|
|
|
} else { |
135
|
|
|
return view('board.threads.show', [ |
136
|
|
|
'posts' => $posts, |
137
|
|
|
'poll' => $poll, |
138
|
|
|
'answers' => $pollanswers, |
139
|
|
|
'votecount' => $votecount, |
140
|
|
|
'canvote' => $canvote, |
141
|
|
|
'votes' => $votes, |
142
|
|
|
]); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function store_thread(Request $request) |
147
|
|
|
{ |
148
|
|
|
$date = Carbon::now(); |
149
|
|
|
|
150
|
|
|
$threadid = \DB::table('board_threads')->insertGetId([ |
151
|
|
|
'cat_id' => $request->get('category'), |
152
|
|
|
'user_id' => \Auth::id(), |
153
|
|
|
'title' => $request->get('topic'), |
154
|
|
|
'closed' => 0, |
155
|
|
|
'pinned' => 0, |
156
|
|
|
'last_user_id' => \Auth::id(), |
157
|
|
|
'created_at' => $date, |
158
|
|
|
'last_created_at' => $date, |
159
|
|
|
]); |
160
|
|
|
|
161
|
|
|
event(new Obyx('thread-add', \Auth::id())); |
162
|
|
|
|
163
|
|
|
\DB::table('board_posts')->insert([ |
164
|
|
|
'cat_id' => $request->get('category'), |
165
|
|
|
'thread_id' => $threadid, |
166
|
|
|
'user_id' => \Auth::id(), |
167
|
|
|
'content_md' => $request->get('msg'), |
168
|
|
|
'content_html' => \Markdown::convertToHtml($request->get('msg')), |
169
|
|
|
'created_at' => $date, |
170
|
|
|
]); |
171
|
|
|
|
172
|
|
|
DatabaseHelper::setThreadViewDate($threadid); |
173
|
|
|
|
174
|
|
|
return redirect()->action('BoardController@show_thread', [$threadid]); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function store_post(Request $request, $threadid) |
178
|
|
|
{ |
179
|
|
|
$this->validate($request, [ |
180
|
|
|
'catid' => 'required', |
181
|
|
|
'msg' => 'required', |
182
|
|
|
]); |
183
|
|
|
|
184
|
|
|
$check = BoardThread::whereId($threadid)->first(); |
185
|
|
|
if ($check->closed == 0) { |
186
|
|
|
$date = Carbon::now(); |
187
|
|
|
|
188
|
|
|
$pid = \DB::table('board_posts')->insertGetId([ |
189
|
|
|
'user_id' => \Auth::id(), |
190
|
|
|
'cat_id' => $request->get('catid'), |
191
|
|
|
'thread_id' => $threadid, |
192
|
|
|
'content_md' => $request->get('msg'), |
193
|
|
|
'content_html' => \Markdown::convertToHtml($request->get('msg')), |
194
|
|
|
'created_at' => $date, |
195
|
|
|
]); |
196
|
|
|
|
197
|
|
|
\DB::table('board_threads') |
198
|
|
|
->where('id', '=', $threadid) |
199
|
|
|
->update([ |
200
|
|
|
'last_created_at' => $date, |
201
|
|
|
'last_user_id' => \Auth::id(), |
202
|
|
|
]); |
203
|
|
|
|
204
|
|
|
\DB::table('board_cats') |
205
|
|
|
->where('id', '=', $request->get('catid')) |
206
|
|
|
->update([ |
207
|
|
|
'last_created_at' => $date, |
208
|
|
|
'last_user_id' => \Auth::id(), |
209
|
|
|
]); |
210
|
|
|
|
211
|
|
|
event(new Obyx('post-add', \Auth::id())); |
212
|
|
|
|
213
|
|
|
$url = \URL::route('board.thread.show', [$threadid]).'#c'.$pid; |
214
|
|
|
} else { |
215
|
|
|
$url = \URL::route('board.thread.show', [$threadid]); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return redirect()->to($url); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function thread_close_switch($id, $state) |
222
|
|
|
{ |
223
|
|
|
if (\Auth::check()) { |
224
|
|
|
if (\Auth::user()->can('mod-threads')) { |
225
|
|
|
if (is_numeric($id)) { |
226
|
|
|
if ($state == 1 || $state == 0) { |
227
|
|
|
\DB::table('board_threads') |
228
|
|
|
->where('id', '=', $id) |
229
|
|
|
->update([ |
230
|
|
|
'closed' => $state, |
231
|
|
|
]); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return redirect()->action('BoardController@show_thread', $id); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function post_edit($threadid, $postid) |
|
|
|
|
241
|
|
|
{ |
242
|
|
|
$post = BoardPost::whereId($postid)->first(); |
243
|
|
|
|
244
|
|
|
return view('board.post.edit', [ |
245
|
|
|
'post' => $post, |
246
|
|
|
]); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function post_update(Request $request, $threadid, $postid) |
250
|
|
|
{ |
251
|
|
|
if (\Auth::check()) { |
252
|
|
|
$this->validate($request, [ |
253
|
|
|
'thread_id' => 'required', |
254
|
|
|
'post_id' => 'required', |
255
|
|
|
'msg' => 'required', |
256
|
|
|
'title' => 'required', |
257
|
|
|
]); |
258
|
|
|
|
259
|
|
|
$post = BoardPost::whereId($postid)->first(); |
|
|
|
|
260
|
|
|
$post->content_md = $request->get('msg'); |
|
|
|
|
261
|
|
|
$post->content_html = \Markdown::convertToHtml($request->get('msg')); |
262
|
|
|
$post->save(); |
263
|
|
|
|
264
|
|
|
$thread = BoardThread::whereId($threadid)->first(); |
265
|
|
|
|
266
|
|
|
$ttitle = $request->get('title'); |
267
|
|
|
|
268
|
|
|
if ($ttitle != $thread->title and $ttitle != '') { |
|
|
|
|
269
|
|
|
$thread->title = $request->get('title'); |
270
|
|
|
$thread->save(); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
return redirect()->action('BoardController@show_thread', $threadid); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function create_vote($threadid) |
278
|
|
|
{ |
279
|
|
|
$check = BoardPoll::whereThreadId($threadid)->get(); |
|
|
|
|
280
|
|
|
$thread = BoardThread::whereId($threadid)->first(); |
281
|
|
|
$cat = $thread->cat; |
|
|
|
|
282
|
|
|
|
283
|
|
|
$edit = 0; |
284
|
|
|
|
285
|
|
|
if ($check) { |
286
|
|
|
$edit = 1; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return view('board.threads.vote', [ |
290
|
|
|
'edit' => $edit, |
291
|
|
|
'thread_id' => $threadid, |
292
|
|
|
'thread' => $thread, |
293
|
|
|
'cat' => $cat, |
294
|
|
|
]); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function store_vote(Request $request, $threadid) |
|
|
|
|
298
|
|
|
{ |
299
|
|
|
$this->validate($request, [ |
300
|
|
|
'thread_id' => 'required', |
301
|
|
|
'question' => 'required', |
302
|
|
|
'answer0' => 'required', |
303
|
|
|
'answer1' => 'required', |
304
|
|
|
]); |
305
|
|
|
|
306
|
|
|
$poll = new BoardPoll(); |
|
|
|
|
307
|
|
|
$poll->user_id = \Auth::id(); |
|
|
|
|
308
|
|
|
$poll->title = $request->get('question'); |
|
|
|
|
309
|
|
|
$poll->thread_id = $request->get('thread_id'); |
310
|
|
|
$poll->save(); |
311
|
|
|
|
312
|
|
|
for ($i = 0; $i < 10; $i++) { |
313
|
|
|
$pollAnswers = new BoardPollAnswer(); |
|
|
|
|
314
|
|
|
$pollAnswers->title = $request->get('answer'.$i); |
|
|
|
|
315
|
|
|
$pollAnswers->user_id = \Auth::id(); |
316
|
|
|
$pollAnswers->poll_id = $poll->id; |
317
|
|
|
$pollAnswers->save(); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
return redirect()->action('BoardController@show_thread', $request->get('thread_id')); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
public function add_vote(Request $request) |
324
|
|
|
{ |
325
|
|
|
if (\Auth::check()) { |
326
|
|
|
$c = BoardPollVote::where('poll_id', '=', $request->get('poll_id')) |
327
|
|
|
->where('user_id', '=', \Auth::id())->first(); |
328
|
|
|
|
329
|
|
|
if ($c) { |
330
|
|
|
$c->poll_id = $request->get('poll_id'); |
|
|
|
|
331
|
|
|
$c->user_id = \Auth::id(); |
|
|
|
|
332
|
|
|
$c->answer_id = $request->get('answer_id'); |
333
|
|
|
$c->save(); |
334
|
|
|
} else { |
335
|
|
|
$vote = new BoardPollVote(); |
|
|
|
|
336
|
|
|
$vote->poll_id = $request->get('poll_id'); |
|
|
|
|
337
|
|
|
$vote->user_id = \Auth::id(); |
|
|
|
|
338
|
|
|
$vote->answer_id = $request->get('answer_id'); |
339
|
|
|
$vote->save(); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
return redirect()->action('BoardController@show_thread', $request->get('thread_id')); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
public function update_vote(Request $request, $threadid) |
|
|
|
|
347
|
|
|
{ |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.