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;
|
|
|
|
use App\Models\Closet;
|
2016-09-04 15:35:12 +08:00
|
|
|
use App\Models\Texture;
|
2016-07-21 22:01:57 +08:00
|
|
|
use App\Models\ClosetModel;
|
2016-09-04 15:35:12 +08:00
|
|
|
use Illuminate\Http\Request;
|
2016-08-29 19:47:30 +08:00
|
|
|
use App\Exceptions\PrettyPageException;
|
2016-10-23 11:41:52 +08:00
|
|
|
use App\Services\Repositories\UserRepository;
|
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
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
/**
|
|
|
|
* Instance of Closet.
|
|
|
|
*
|
|
|
|
* @var \App\Models\Closet
|
|
|
|
*/
|
2016-08-16 22:52:00 +08:00
|
|
|
private $closet;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-08-16 22:52:00 +08:00
|
|
|
public function __construct()
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-08-28 10:05:21 +08:00
|
|
|
$this->closet = new Closet(session('uid'));
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-10-23 11:41:52 +08:00
|
|
|
public function index(Request $request, UserRepository $users)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-09-04 15:35:12 +08:00
|
|
|
$category = $request->input('category', 'skin');
|
|
|
|
$page = $request->input('page', 1);
|
2016-09-27 22:31:37 +08:00
|
|
|
$page = $page <= 0 ? 1 : $page;
|
2016-10-16 20:04:21 +08:00
|
|
|
$q = $request->input('q', null);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-11-13 12:20:30 +08:00
|
|
|
$items = [];
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-11-13 12:20:30 +08:00
|
|
|
if ($q) {
|
|
|
|
foreach (['skin', 'cape'] as $category) {
|
|
|
|
// do search
|
|
|
|
foreach ($this->closet->getItems($category) as $item) {
|
|
|
|
if (strstr($item->name, $q)) {
|
|
|
|
$items[$category][] = $item;
|
|
|
|
}
|
2016-10-16 20:04:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-11-13 12:20:30 +08:00
|
|
|
$items['skin'] = $this->closet->getItems('skin');
|
|
|
|
$items['cape'] = $this->closet->getItems('cape');
|
2016-10-16 20:04:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// pagination
|
2016-11-13 12:20:30 +08:00
|
|
|
$total_pages = [];
|
2016-10-16 20:04:21 +08:00
|
|
|
|
2016-11-13 12:20:30 +08:00
|
|
|
foreach ($items as $key => $value) {
|
|
|
|
$total_pages[] = ceil(count($items[$key]) / 6);
|
|
|
|
$items[$key] = array_slice($value, ($page-1)*6, 6);
|
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-11-13 12:20:30 +08:00
|
|
|
return view('user.closet')->with('items', $items)
|
|
|
|
->with('page', $page)
|
|
|
|
->with('q', $q)
|
|
|
|
->with('category', $category)
|
|
|
|
->with('total_pages', max($total_pages))
|
|
|
|
->with('user', $users->get(session('uid')));
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function info()
|
|
|
|
{
|
2016-09-10 21:39:45 +08:00
|
|
|
return json($this->closet->getItems());
|
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',
|
2016-09-25 10:35:16 +08:00
|
|
|
'name' => 'required|no_special_chars'
|
2016-09-04 15:35:12 +08:00
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
if ($this->closet->add($request->tid, $request->name)) {
|
|
|
|
$t = Texture::find($request->tid);
|
2016-07-21 22:01:57 +08:00
|
|
|
$t->likes += 1;
|
|
|
|
$t->save();
|
|
|
|
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.closet.add.success', ['name' => $request->input('name')]), 0);
|
2016-09-10 21:39:45 +08:00
|
|
|
} else {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.closet.add.repeated'), 1);
|
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',
|
|
|
|
'new_name' => 'required|no_special_chars'
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($this->closet->rename($request->tid, $request->new_name)) {
|
|
|
|
return json(trans('user.closet.rename.success', ['name' => $request->new_name]), 0);
|
|
|
|
} else {
|
|
|
|
return json(trans('user.closet.remove.non-existent'), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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, [
|
|
|
|
'tid' => 'required|integer'
|
|
|
|
]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-09-04 15:35:12 +08:00
|
|
|
if ($this->closet->remove($request->tid)) {
|
|
|
|
$t = Texture::find($request->tid);
|
2016-07-21 22:01:57 +08:00
|
|
|
$t->likes = $t->likes - 1;
|
|
|
|
$t->save();
|
|
|
|
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.closet.remove.success'), 0);
|
2016-09-10 21:39:45 +08:00
|
|
|
} else {
|
2016-09-24 22:49:20 +08:00
|
|
|
return json(trans('user.closet.remove.non-existent'), 0);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|