1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Http\Controllers\Admin\Forum;
7
8use App\Http\Controllers\Admin\Controller;
9use App\Models\Forum\Forum;
10use App\Models\Forum\ForumCover;
11use Auth;
12use Request;
13
14class ForumCoversController extends Controller
15{
16 private $params = [];
17
18 public function index()
19 {
20 $forums = Forum::with('cover')->get();
21
22 return ext_view('admin.forum.forum_covers.index', compact('forums'));
23 }
24
25 public function store()
26 {
27 if (($this->coverParams()['forum_id'] ?? null) === null) {
28 abort(422);
29 }
30
31 $cover = ForumCover::firstOrCreate(['forum_id' => $this->coverParams()['forum_id']]);
32
33 $cover->update($this->coverParams());
34
35 return redirect(route('admin.forum.forum-covers.index').'#forum-'.$cover->forum_id);
36 }
37
38 public function update($id)
39 {
40 $cover = ForumCover::findOrFail($id);
41
42 $cover->update($this->coverParams());
43
44 return redirect(route('admin.forum.forum-covers.index').'#forum-'.$cover->forum_id);
45 }
46
47 private function coverParams()
48 {
49 if (isset($this->params['cover']) === false) {
50 $this->params['cover'] = get_params(Request::all(), 'forum_cover', [
51 'forum_id:int',
52
53 'main_cover.cover_file:file',
54 'main_cover._delete:bool',
55
56 'default_topic_cover.cover_file:file',
57 'default_topic_cover._delete:bool',
58 ]);
59 $this->params['cover']['user_id'] = Auth::user()->user_id;
60 }
61
62 return $this->params['cover'];
63 }
64}