blessing-skin-server/app/Http/Controllers/ClosetController.php

150 lines
4.4 KiB
PHP
Raw Normal View History

2016-07-21 22:01:57 +08:00
<?php
2016-08-28 10:05:21 +08:00
namespace App\Http\Controllers;
2016-07-21 22:01:57 +08:00
use App\Models\Texture;
2019-12-14 11:10:37 +08:00
use App\Models\User;
use Auth;
use Blessing\Filter;
2020-03-21 11:06:24 +08:00
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
2016-07-21 22:01:57 +08:00
class ClosetController extends Controller
2016-07-21 22:01:57 +08:00
{
2019-12-14 14:30:38 +08:00
public function index(Filter $filter)
{
2019-12-14 14:30:38 +08:00
$grid = [
'layout' => [
['md-8', 'md-4'],
],
'widgets' => [
[
[
'user.widgets.email-verification',
'user.widgets.closet.list',
],
['shared.previewer'],
],
],
];
$grid = $filter->apply('grid:user.closet', $grid);
2019-03-23 19:52:14 +08:00
return view('user.closet')
2019-12-14 14:30:38 +08:00
->with('grid', $grid)
2019-04-01 21:45:59 +08:00
->with('extra', [
2019-12-14 11:10:37 +08:00
'unverified' => option('require_verification') && !auth()->user()->verified,
2019-04-01 21:45:59 +08:00
'rule' => trans('user.player.player-name-rule.'.option('player_name_rule')),
'length' => trans(
'user.player.player-name-length',
['min' => option('player_name_length_min'), 'max' => option('player_name_length_max')]
),
]);
}
public function getClosetData(Request $request)
2016-07-21 22:01:57 +08:00
{
$category = $request->input('category', 'skin');
2016-07-21 22:01:57 +08:00
2020-03-21 11:06:24 +08:00
return auth()
->user()
->closet()
->when($category === 'cape', function (Builder $query) {
return $query->where('type', 'cape');
}, function (Builder $query) {
return $query->whereIn('type', ['steve', 'alex']);
})
->when($request->input('q'), function (Builder $query, $search) {
2020-03-21 11:08:41 +08:00
return $query->like('item_name', $search);
2020-03-21 11:06:24 +08:00
})
->paginate(6);
2016-07-21 22:01:57 +08:00
}
2020-03-24 18:05:46 +08:00
public function allIds()
{
return auth()->user()->closet()->pluck('texture_tid');
}
2019-09-07 11:15:23 +08:00
public function add(Request $request)
2016-07-21 22:01:57 +08:00
{
$this->validate($request, [
2019-12-14 11:10:37 +08:00
'tid' => 'required|integer',
'name' => 'required',
]);
2016-07-21 22:01:57 +08:00
2019-03-14 23:55:49 +08:00
$user = Auth::user();
2019-03-23 00:20:28 +08:00
if ($user->score < option('score_per_closet_item')) {
return json(trans('user.closet.add.lack-score'), 7);
}
2017-11-04 17:18:01 +08:00
$tid = $request->tid;
2019-03-20 23:28:04 +08:00
$texture = Texture::find($tid);
2019-12-14 11:10:37 +08:00
if (!$texture) {
2017-11-04 17:18:01 +08:00
return json(trans('user.closet.add.not-found'), 1);
}
2020-03-14 14:59:52 +08:00
if (!$texture->public && ($texture->uploader != $user->uid && !$user->isAdmin())) {
2019-04-28 11:38:38 +08:00
return json(trans('skinlib.show.private'), 1);
}
2019-03-14 23:55:49 +08:00
if ($user->closet()->where('tid', $request->tid)->count() > 0) {
2016-09-24 22:49:20 +08:00
return json(trans('user.closet.add.repeated'), 1);
2016-07-21 22:01:57 +08:00
}
2019-03-14 23:55:49 +08:00
$user->closet()->attach($tid, ['item_name' => $request->name]);
2019-07-30 14:29:02 +08:00
$user->score -= option('score_per_closet_item');
$user->save();
2019-03-14 23:55:49 +08:00
$texture->likes++;
$texture->save();
2019-08-24 10:22:26 +08:00
$uploader = User::find($texture->uploader);
2019-03-20 23:28:04 +08:00
if ($uploader && $uploader->uid != $user->uid) {
$uploader->score += option('score_award_per_like', 0);
$uploader->save();
}
2019-03-14 23:55:49 +08:00
return json(trans('user.closet.add.success', ['name' => $request->input('name')]), 0);
2016-07-21 22:01:57 +08:00
}
2019-04-28 11:38:38 +08:00
public function rename(Request $request, $tid)
2016-09-25 10:35:16 +08:00
{
$this->validate($request, ['name' => 'required']);
2019-03-14 23:55:49 +08:00
$user = auth()->user();
2019-03-14 23:55:49 +08:00
if ($user->closet()->where('tid', $request->tid)->count() == 0) {
2017-11-04 17:18:01 +08:00
return json(trans('user.closet.remove.non-existent'), 1);
2016-09-25 10:35:16 +08:00
}
2019-03-14 23:55:49 +08:00
2019-04-28 11:38:38 +08:00
$user->closet()->updateExistingPivot($request->tid, ['item_name' => $request->name]);
2019-04-28 11:38:38 +08:00
return json(trans('user.closet.rename.success', ['name' => $request->name]), 0);
2016-09-25 10:35:16 +08:00
}
2019-09-07 11:15:23 +08:00
public function remove($tid)
2016-07-21 22:01:57 +08:00
{
2019-03-14 23:55:49 +08:00
$user = auth()->user();
2016-07-21 22:01:57 +08:00
2019-03-20 23:28:04 +08:00
if ($user->closet()->where('tid', $tid)->count() == 0) {
2019-03-14 23:55:49 +08:00
return json(trans('user.closet.remove.non-existent'), 1);
}
2018-07-11 16:10:45 +08:00
2019-03-20 23:28:04 +08:00
$user->closet()->detach($tid);
2019-03-14 23:55:49 +08:00
if (option('return_score')) {
2019-07-30 14:29:02 +08:00
$user->score += option('score_per_closet_item');
$user->save();
2016-07-21 22:01:57 +08:00
}
2019-03-14 23:55:49 +08:00
$texture = Texture::find($tid);
$texture->likes--;
$texture->save();
2019-08-24 10:22:26 +08:00
$uploader = User::find($texture->uploader);
2019-03-20 23:28:04 +08:00
$uploader->score -= option('score_award_per_like', 0);
$uploader->save();
2019-03-14 23:55:49 +08:00
return json(trans('user.closet.remove.success'), 0);
2016-07-21 22:01:57 +08:00
}
}