Skip to content

Commit 649b18c

Browse files
committed
support for server filtering
closes pterodactyl#125
1 parent 84a4c8b commit 649b18c

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
77

88
### Added
99
* Return node configuration from remote API by using `/api/nodes/{id}/config` endpoint. Only accepts SSL connections.
10+
* Support for filtering servers within Admin CP to narrow down results by name, email, allocation, or defined fields.
1011

1112
### Changed
1213
* Creating a user, server, or node now returns `HTTP/1.1 200` and a JSON element with the user/server/node's ID.

app/Http/Controllers/Admin/ServersController.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,40 @@ public function __construct()
5151

5252
public function getIndex(Request $request)
5353
{
54-
return view('admin.servers.index', [
55-
'servers' => Models\Server::select(
54+
$query = Models\Server::select(
55+
'servers.*',
56+
'nodes.name as a_nodeName',
57+
'users.email as a_ownerEmail',
58+
'allocations.ip',
59+
'allocations.port',
60+
'allocations.ip_alias'
61+
)->join('nodes', 'servers.node', '=', 'nodes.id')
62+
->join('users', 'servers.owner', '=', 'users.id')
63+
->join('allocations', 'servers.allocation', '=', 'allocations.id');
64+
65+
if ($request->input('filter') && !is_null($request->input('filter'))) {
66+
preg_match_all('/[^\s"\']+|"([^"]*)"|\'([^\']*)\'/', urldecode($request->input('filter')), $matches);
67+
foreach($matches[0] as $match) {
68+
$match = str_replace('"', '', $match);
69+
if (strpos($match, ':')) {
70+
list($field, $term) = explode(':', $match);
71+
$field = (strpos($field, '.')) ? $field : 'servers.' . $field;
72+
$query->orWhere($field, 'LIKE', '%' . $term . '%');
73+
} else {
74+
$query->where('servers.name', 'LIKE', '%' . $match . '%');
75+
$query->orWhere('servers.username', 'LIKE', '%' . $match . '%');
76+
$query->orWhere('users.email', 'LIKE', '%' . $match . '%');
77+
$query->orWhere('allocations.port', 'LIKE', '%' . $match . '%');
78+
$query->orWhere('allocations.ip', 'LIKE', '%' . $match . '%');
79+
}
80+
}
81+
}
82+
83+
try {
84+
$servers = $query->paginate(20);
85+
} catch (\Exception $ex) {
86+
Alert::warning('There was an error with the search parameters provided.');
87+
$servers = Models\Server::select(
5688
'servers.*',
5789
'nodes.name as a_nodeName',
5890
'users.email as a_ownerEmail',
@@ -62,7 +94,11 @@ public function getIndex(Request $request)
6294
)->join('nodes', 'servers.node', '=', 'nodes.id')
6395
->join('users', 'servers.owner', '=', 'users.id')
6496
->join('allocations', 'servers.allocation', '=', 'allocations.id')
65-
->paginate(20),
97+
->paginate(20);
98+
}
99+
100+
return view('admin.servers.index', [
101+
'servers' => $servers
66102
]);
67103
}
68104

resources/views/admin/servers/index.blade.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@
3030
<li class="active">Servers</li>
3131
</ul>
3232
<h3>All Servers</h3><hr />
33+
<form method="GET" style="margin-bottom:20px;">
34+
<div class="input-group">
35+
<input type="text" name="filter" class="form-control" value="{{ urldecode(Input::get('filter')) }}" placeholder="search term" />
36+
<div class="input-group-btn">
37+
<button type="submit" class="btn btn-sm btn-primary">Filter Servers</button>
38+
</div>
39+
</div>
40+
</form>
3341
<table class="table table-bordered table-hover">
3442
<thead>
3543
<tr>

0 commit comments

Comments
 (0)