add search for closet

This commit is contained in:
printempw 2016-10-16 20:04:21 +08:00
parent b01d0fded2
commit 7f0e9a4178
3 changed files with 54 additions and 11 deletions

View File

@ -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'))))

View File

@ -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);
}
/**

View File

@ -27,10 +27,23 @@
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title" title="{{ trans('user.closet.switch-category') }}" data-toggle="tooltip" data-placement="bottom">
@if ($q)
搜索结果
@else
<a href="?category=skin" {{ ($category == "skin") ? 'class=selected' : "" }}>{{ trans('general.skin') }}</a>
/
<a href="?category=cape" {{ ($category == "cape") ? 'class=selected' : "" }}>{{ trans('general.cape') }}</a>
@endif
</h3>
<div class="box-tools pull-right">
<div class="has-feedback">
<form method="get" action="" class="user-search-form">
<input type="text" name="q" class="form-control input-sm" placeholder="输入,回车搜索" value="{{ $q }}">
<span class="glyphicon glyphicon-search form-control-feedback"></span>
</form>
</div>
</div><!-- /.box-tools -->
</div><!-- /.box-header -->
<div class="box-body">
@ -57,26 +70,32 @@
</div>
@empty
<div class="empty-msg">
@if($q)
{{ trans('skinlib.general.no-result') }}
@else
{!! trans('user.closet.empty-msg', ['url' => url('skinlib')]) !!}
@endif
</div>
@endforelse
@endforelse
</div>
<div class="box-footer">
<ul class="pagination pagination-sm no-margin pull-right">
<li><a href="?page=1">«</a></li>
<?php $base_url = $q ? "?q=$q&" : "?"; ?>
<li><a href="{{ $base_url }}page=1">«</a></li>
@if ($page != 1)
<li><a href="?page={{ $page-1 }}">{{ $page - 1 }}</a></li>
<li><a href="{{ $base_url }}page={{ $page-1 }}">{{ $page - 1 }}</a></li>
@endif
<li><a href="?page={{ $page }}" class="active">{{ $page }}</a></li>
<li><a href="{{ $base_url }}page={{ $page }}" class="active">{{ $page }}</a></li>
@if ($total_pages > $page)
<li><a href="?page={{ $page+1 }}">{{ $page+1 }}</a></li>
<li><a href="{{ $base_url }}page={{ $page+1 }}">{{ $page+1 }}</a></li>
@endif
<li><a href="?page={{ $total_pages }}">»</a></li>
<li><a href="{{ $base_url }}page={{ $total_pages }}">»</a></li>
</ul>
<p class="pull-right">{{ trans('general.pagination', ['page' => $page, 'total' => $total_pages]) }}</p>
</div>