From 7f0e9a4178b31c435dcb7b2e96ae2f64294a781e Mon Sep 17 00:00:00 2001 From: printempw Date: Sun, 16 Oct 2016 20:04:21 +0800 Subject: [PATCH] add search for closet --- app/Http/Controllers/ClosetController.php | 21 +++++++++++++-- app/Models/Closet.php | 13 +++++++--- resources/views/user/closet.tpl | 31 ++++++++++++++++++----- 3 files changed, 54 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/ClosetController.php b/app/Http/Controllers/ClosetController.php index d3ca918d..6a85e2c5 100644 --- a/app/Http/Controllers/ClosetController.php +++ b/app/Http/Controllers/ClosetController.php @@ -30,13 +30,30 @@ class ClosetController extends Controller $category = $request->input('category', 'skin'); $page = $request->input('page', 1); $page = $page <= 0 ? 1 : $page; + $q = $request->input('q', null); - $items = array_slice($this->closet->getItems($category), ($page-1)*6, 6); + if ($q) { + $result = []; - $total_pages = ceil(count($this->closet->getItems($category)) / 6); + foreach ($this->closet->getItems() as $item) { + if (strstr($item->name, $q)) { + $result[] = $item; + } + } + + $items = $result; + } else { + $items = $this->closet->getItems($category); + } + + // pagination + $items = array_slice($items, ($page-1)*6, 6); + + $total_pages = ceil(count($items) / 6); echo View::make('user.closet')->with('items', $items) ->with('page', $page) + ->with('q', $q) ->with('category', $category) ->with('total_pages', $total_pages) ->with('user', (new User(session('uid')))) diff --git a/app/Models/Closet.php b/app/Models/Closet.php index 1631d983..752bd983 100644 --- a/app/Models/Closet.php +++ b/app/Models/Closet.php @@ -87,13 +87,20 @@ class Closet /** * Get array of instances of App\Models\Texture. * - * @param string $category + * @param string $category skin|cape|all * @return array */ - public function getItems($category = "skin") + public function getItems($category = "all") { + if ($category == "all") { + $items = array_merge($this->textures_skin, $this->textures_cape); + } else { + $property = "textures_$category"; + $items = $this->$property; + } + // reverse the array to sort desc by add_at - return array_reverse(($category == "skin") ? $this->textures_skin : $this->textures_cape); + return array_reverse($items); } /** diff --git a/resources/views/user/closet.tpl b/resources/views/user/closet.tpl index 9e11b02d..755df1c2 100644 --- a/resources/views/user/closet.tpl +++ b/resources/views/user/closet.tpl @@ -27,10 +27,23 @@

+ @if ($q) + 搜索结果 + @else {{ trans('general.skin') }} / {{ trans('general.cape') }} + @endif

+ +
+
+
+ + +
+
+
@@ -57,26 +70,32 @@
@empty
+ @if($q) + {{ trans('skinlib.general.no-result') }} + @else {!! trans('user.closet.empty-msg', ['url' => url('skinlib')]) !!} + @endif
- @endforelse + @endforelse