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
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
use View;
|
|
|
|
use Option;
|
2016-07-21 22:01:57 +08:00
|
|
|
use App\Models\User;
|
2016-09-04 15:35:12 +08:00
|
|
|
use App\Models\Texture;
|
|
|
|
use Illuminate\Http\Request;
|
2018-07-20 14:42:43 +08:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
class ClosetController extends Controller
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2017-04-23 23:58:22 +08:00
|
|
|
public function index()
|
|
|
|
{
|
2018-07-20 14:42:43 +08:00
|
|
|
return view('user.closet')->with('user', Auth::user());
|
2017-04-23 23:58:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getClosetData(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$category = $request->input('category', 'skin');
|
2019-03-02 22:58:37 +08:00
|
|
|
$page = abs($request->input('page', 1));
|
2019-03-14 23:55:49 +08:00
|
|
|
$perPage = (int) $request->input('perPage', 6);
|
2019-03-02 22:58:37 +08:00
|
|
|
$q = $request->input('q', null);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-14 23:55:49 +08:00
|
|
|
$perPage = $perPage > 0 ? $perPage : 6;
|
2017-11-06 11:07:24 +08:00
|
|
|
|
2019-03-14 23:55:49 +08:00
|
|
|
$user = auth()->user();
|
|
|
|
$closet = $user->closet();
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-14 23:55:49 +08:00
|
|
|
if ($category == 'cape') {
|
|
|
|
$closet = $closet->where('type', 'cape');
|
2016-10-16 20:04:21 +08:00
|
|
|
} else {
|
2019-03-14 23:55:49 +08:00
|
|
|
$closet = $closet->where(function ($query) {
|
|
|
|
return $query->where('type', 'steve')->orWhere('type', 'alex');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($q) {
|
|
|
|
$closet = $closet->where('item_name', 'like', "%$q%");
|
2016-10-16 20:04:21 +08:00
|
|
|
}
|
|
|
|
|
2019-03-14 23:55:49 +08:00
|
|
|
$closet->offset(($page - 1) * $perPage)->limit($perPage);
|
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
// Pagination
|
2019-03-14 23:55:49 +08:00
|
|
|
$items = $closet->get()->map(function ($t) {
|
|
|
|
$t->name = $t->pivot->item_name;
|
2019-03-15 00:03:54 +08:00
|
|
|
|
2019-03-14 23:55:49 +08:00
|
|
|
return $t;
|
|
|
|
});
|
|
|
|
$totalPages = ceil($items->count() / $perPage);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2017-04-23 23:58:22 +08:00
|
|
|
return response()->json([
|
|
|
|
'category' => $category,
|
2019-03-14 23:55:49 +08:00
|
|
|
'items' => $items,
|
|
|
|
'total_pages' => $totalPages,
|
2017-04-23 23:58:22 +08:00
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function add(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->validate($request, [
|
|
|
|
'tid' => 'required|integer',
|
2019-03-02 22:58:37 +08:00
|
|
|
'name' => 'required|no_special_chars',
|
2016-09-04 15:35:12 +08:00
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-03-14 23:55:49 +08:00
|
|
|
$user = Auth::user();
|
2018-07-20 14:42:43 +08:00
|
|
|
|
2019-03-14 23:55:49 +08:00
|
|
|
if ($user->getScore() < option('score_per_closet_item')) {
|
2016-12-28 13:08:17 +08:00
|
|
|
return json(trans('user.closet.add.lack-score'), 7);
|
|
|
|
}
|
|
|
|
|
2017-11-04 17:18:01 +08:00
|
|
|
$tid = $request->tid;
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! Texture::find($tid)) {
|
2017-11-04 17:18:01 +08:00
|
|
|
return json(trans('user.closet.add.not-found'), 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]);
|
|
|
|
$user->setScore(option('score_per_closet_item'), 'minus');
|
|
|
|
|
|
|
|
return json(trans('user.closet.add.success', ['name' => $request->input('name')]), 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-09-25 10:35:16 +08:00
|
|
|
public function rename(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'tid' => 'required|integer',
|
2019-03-02 22:58:37 +08:00
|
|
|
'new_name' => 'required|no_special_chars',
|
2016-09-25 10:35:16 +08:00
|
|
|
]);
|
|
|
|
|
2019-03-14 23:55:49 +08:00
|
|
|
$user = auth()->user();
|
2019-03-02 22:58:37 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
$user->closet()->updateExistingPivot($request->tid, ['item_name' => $request->new_name]);
|
2019-03-15 00:03:54 +08:00
|
|
|
|
2019-03-14 23:55:49 +08:00
|
|
|
return json(trans('user.closet.rename.success', ['name' => $request->new_name]), 0);
|
2016-09-25 10:35:16 +08:00
|
|
|
}
|
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
public function remove(Request $request)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$this->validate($request, [
|
2019-03-14 23:55:49 +08:00
|
|
|
'tid' => 'required|integer',
|
2016-09-04 15:35:12 +08:00
|
|
|
]);
|
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-14 23:55:49 +08:00
|
|
|
if ($user->closet()->where('tid', $request->tid)->count() == 0) {
|
|
|
|
return json(trans('user.closet.remove.non-existent'), 1);
|
|
|
|
}
|
2018-07-11 16:10:45 +08:00
|
|
|
|
2019-03-14 23:55:49 +08:00
|
|
|
$user->closet()->detach($request->tid);
|
2017-01-02 00:19:55 +08:00
|
|
|
|
2019-03-14 23:55:49 +08:00
|
|
|
if (option('return_score')) {
|
|
|
|
$user->setScore(option('score_per_closet_item'), 'plus');
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
2019-03-14 23:55:49 +08:00
|
|
|
|
|
|
|
return json(trans('user.closet.remove.success'), 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
}
|