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;
7
8use App\Models\UserCoverPreset;
9use Symfony\Component\HttpFoundation\Response;
10
11class UserCoverPresetsController extends Controller
12{
13 public function __construct()
14 {
15 $this->middleware('auth');
16
17 parent::__construct();
18 }
19
20 public function batchActivate(): Response
21 {
22 $params = get_params(\Request::all(), null, [
23 'ids:int[]',
24 'active:bool',
25 ]);
26 if (!isset($params['active'])) {
27 abort(422, 'parameter "active" is missing');
28 }
29 UserCoverPreset::whereKey($params['ids'] ?? [])->update(['active' => $params['active']]);
30
31 return response(null, 204);
32 }
33
34 public function index(): Response
35 {
36 priv_check('UserCoverPresetManage')->ensureCan();
37
38 return ext_view('user_cover_presets.index', [
39 'items' => UserCoverPreset::orderBy('id', 'ASC')->get(),
40 ]);
41 }
42
43 public function store(): Response
44 {
45 priv_check('UserCoverPresetManage')->ensureCan();
46
47 try {
48 $files = \Request::file('files') ?? [];
49 foreach ($files as $file) {
50 $item = \DB::transaction(function () use ($file) {
51 $item = UserCoverPreset::create();
52 $item->file()->store($file->getRealPath());
53 $item->saveOrExplode();
54
55 return $item;
56 });
57 $hash ??= "#cover-{$item->getKey()}";
58 }
59 \Session::flash('popup', osu_trans('user_cover_presets.store.ok'));
60 } catch (\Throwable $e) {
61 \Session::flash('popup', osu_trans('user_cover_presets.store.failed', ['error' => $e->getMessage()]));
62 }
63
64 return ujs_redirect(route('user-cover-presets.index').($hash ?? ''));
65 }
66
67 public function update(string $id): Response
68 {
69 priv_check('UserCoverPresetManage')->ensureCan();
70
71 $item = UserCoverPreset::findOrFail($id);
72 $params = get_params(\Request::all(), null, [
73 'file:file',
74 'active:bool',
75 ], ['null_missing' => true]);
76
77 if ($params['file'] !== null) {
78 $item->file()->store($params['file']);
79 $item->save();
80 }
81 if ($params['active'] !== null) {
82 $item->update(['active' => $params['active']]);
83 }
84
85 return response(null, 204);
86 }
87}