From a0268f1c47ea77a05158b352a1ac750be116f949 Mon Sep 17 00:00:00 2001 From: IceToast Date: Wed, 15 Feb 2023 00:25:59 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Sorting=20on=20admin/user?= =?UTF-8?q?s=20datatable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Admin/UserController.php | 71 ++++++---- .../default/views/admin/users/index.blade.php | 126 ++++++++++++------ 2 files changed, 124 insertions(+), 73 deletions(-) diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 329fe04c..74453c6f 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -7,6 +7,7 @@ use App\Events\UserUpdateCreditsEvent; use App\Http\Controllers\Controller; use App\Models\User; use App\Notifications\DynamicNotification; +use App\Traits\DatatablesSortable; use Exception; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\View\Factory; @@ -26,6 +27,8 @@ use Spatie\QueryBuilder\QueryBuilder; class UserController extends Controller { + use DatatablesSortable; + private Pterodactyl $pterodactyl; public function __construct(Pterodactyl $pterodactyl) @@ -132,7 +135,7 @@ class UserController extends Controller ]); } - if (! is_null($request->input('new_password'))) { + if (!is_null($request->input('new_password'))) { $request->validate([ 'new_password' => 'required|string|min:8', 'new_password_confirmation' => 'required|same:new_password', @@ -259,7 +262,7 @@ class UserController extends Controller public function toggleSuspended(User $user) { try { - ! $user->isSuspended() ? $user->suspend() : $user->unSuspend(); + !$user->isSuspended() ? $user->suspend() : $user->unSuspend(); } catch (Exception $exception) { return redirect()->back()->with('error', $exception->getMessage()); } @@ -270,32 +273,42 @@ class UserController extends Controller /** * @throws Exception */ - public function dataTable() + public function dataTable(Request $request) { - $query = User::with(['discordUser', 'servers'])->select('users.*'); + $query = User::withCount(['servers'])->with('discordUser'); + // manually count referrals in user_referrals table + $query->addSelect(DB::raw('(SELECT COUNT(*) FROM user_referrals WHERE user_referrals.referral_id = users.id) as referrals_count')); + + + if ($request->has('order')) { + $query = $this->sortByColumn($request->input('order'), $request->input('columns'), $query); + } + return datatables($query) ->addColumn('avatar', function (User $user) { - return ''; + return ''; }) ->addColumn('credits', function (User $user) { - return ' '.$user->credits(); + return ' ' . $user->credits(); }) ->addColumn('verified', function (User $user) { return $user->getVerifiedStatus(); }) - ->addColumn('servers', function (User $user) { - return $user->servers->count(); + ->addColumn('servers_count', function (User $user) { + return $user->servers_count; }) - ->addColumn('referrals', function (User $user) { - return DB::table('user_referrals')->where('referral_id', '=', $user->id)->count(); + ->addColumn('referrals_count', function (User $user) { + return $user->referrals_count; }) ->addColumn('discordId', function (User $user) { return $user->discordUser ? $user->discordUser->id : ''; }) ->addColumn('last_seen', function (User $user) { - return ['display' => $user->last_seen ? $user->last_seen->diffForHumans() : '', - 'raw' => $user->last_seen ? strtotime($user->last_seen) : '', ]; + return [ + 'display' => $user->last_seen ? $user->last_seen->diffForHumans() : __('Never'), + 'raw' => $user->last_seen ? strtotime($user->last_seen) : '', + ]; }) ->addColumn('actions', function (User $user) { $suspendColor = $user->isSuspended() ? 'btn-success' : 'btn-warning'; @@ -303,19 +316,19 @@ class UserController extends Controller $suspendText = $user->isSuspended() ? __('Unsuspend') : __('Suspend'); return ' - - - - -
- '.csrf_field().' - -
-
- '.csrf_field().' - '.method_field('DELETE').' - -
+ + + + +
+ ' . csrf_field() . ' + +
+
+ ' . csrf_field() . ' + ' . method_field('DELETE') . ' + +
'; }) ->editColumn('role', function (User $user) { @@ -334,14 +347,14 @@ class UserController extends Controller break; } - return ''.$user->role.''; + return '' . $user->role . ''; }) ->editColumn('name', function (User $user) { - return ''.strip_tags($user->name).''; + return '' . strip_tags($user->name) . ''; }) - /*->orderColumn('last_seen', function ($query) { + ->orderColumn('last_seen', function ($query) { $query->orderBy('last_seen', "desc"); - })*/ + }) ->rawColumns(['avatar', 'name', 'credits', 'role', 'usage', 'referrals', 'actions', 'last_seen']) ->make(true); } diff --git a/themes/default/views/admin/users/index.blade.php b/themes/default/views/admin/users/index.blade.php index b6be7a74..de2dc76e 100644 --- a/themes/default/views/admin/users/index.blade.php +++ b/themes/default/views/admin/users/index.blade.php @@ -6,12 +6,13 @@
-

{{__('Users')}}

+

{{ __('Users') }}

@@ -27,9 +28,9 @@
-
{{__('Users')}}
- {{__('Notify')}} +
{{ __('Users') }}
+ {{ __('Notify') }}
@@ -37,21 +38,21 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + @@ -68,41 +69,78 @@ - - - @endsection
discordIdippterodactyl_id{{__('Avatar')}}{{__('Name')}}{{__('Role')}}{{__('Email')}}{{CREDITS_DISPLAY_NAME}}{{__('Servers')}}{{__("Referrals")}}{{__('Verified')}}{{__('Last seen')}}
discordIdippterodactyl_id{{ __('Avatar') }}{{ __('Name') }}{{ __('Role') }}{{ __('Email') }}{{ CREDITS_DISPLAY_NAME }}{{ __('Servers') }}{{ __('Referrals') }}{{ __('Verified') }}{{ __('Last seen') }}