Users overview: Fixed sorting

This commit is contained in:
Igor Scheller 2020-04-24 20:01:26 +02:00 committed by msquare
parent 8c0ac0f7a1
commit 4d8e568ff8
2 changed files with 17 additions and 6 deletions

View File

@ -5,6 +5,7 @@ use Engelsystem\Models\User\State;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Engelsystem\ShiftCalendarRenderer; use Engelsystem\ShiftCalendarRenderer;
use Engelsystem\ShiftsFilter; use Engelsystem\ShiftsFilter;
use Illuminate\Database\Eloquent\Collection;
/** /**
* Route user actions. * Route user actions.
@ -267,15 +268,16 @@ function users_list_controller()
$order_by = 'name'; $order_by = 'name';
if ($request->has('OrderBy') && in_array($request->input('OrderBy'), [ if ($request->has('OrderBy') && in_array($request->input('OrderBy'), [
'name', 'name',
'last_name',
'first_name', 'first_name',
'last_name',
'dect', 'dect',
'email',
'shirt_size',
'arrived', 'arrived',
'got_voucher',
'freeloads',
'active', 'active',
'force_active', 'force_active',
'got_shirt', 'got_shirt',
'shirt_size',
'planned_arrival_date', 'planned_arrival_date',
'planned_departure_date', 'planned_departure_date',
'last_login_at', 'last_login_at',
@ -283,15 +285,24 @@ function users_list_controller()
$order_by = $request->input('OrderBy'); $order_by = $request->input('OrderBy');
} }
/** @var User[] $users */ /** @var User[]|Collection $users */
$users = User::with(['contact', 'personalData', 'state']) $users = User::with(['contact', 'personalData', 'state'])
->orderBy($order_by)
->orderBy('name') ->orderBy('name')
->get(); ->get();
foreach ($users as $user) { foreach ($users as $user) {
$user->setAttribute('freeloads', count(ShiftEntries_freeloaded_by_user($user->id))); $user->setAttribute('freeloads', count(ShiftEntries_freeloaded_by_user($user->id)));
} }
$users = $users->sortBy(function (User $user) use ($order_by) {
$userData = $user->toArray();
$data = [];
array_walk_recursive($userData, function ($value, $key) use (&$data) {
$data[$key] = $value;
});
return isset($data[$order_by]) ? $data[$order_by] : null;
});
return [ return [
__('All users'), __('All users'),
Users_view( Users_view(

View File

@ -231,7 +231,7 @@ function Users_view(
} }
$user_table_headers['arrived'] = Users_table_header_link('arrived', __('Arrived'), $order_by); $user_table_headers['arrived'] = Users_table_header_link('arrived', __('Arrived'), $order_by);
$user_table_headers['got_voucher'] = Users_table_header_link('got_voucher', __('Voucher'), $order_by); $user_table_headers['got_voucher'] = Users_table_header_link('got_voucher', __('Voucher'), $order_by);
$user_table_headers['freeloads'] = __('Freeloads'); $user_table_headers['freeloads'] = Users_table_header_link('freeloads', __('Freeloads'), $order_by);
$user_table_headers['active'] = Users_table_header_link('active', __('Active'), $order_by); $user_table_headers['active'] = Users_table_header_link('active', __('Active'), $order_by);
$user_table_headers['force_active'] = Users_table_header_link('force_active', __('Forced'), $order_by); $user_table_headers['force_active'] = Users_table_header_link('force_active', __('Forced'), $order_by);
$user_table_headers['got_shirt'] = Users_table_header_link('got_shirt', __('T-Shirt'), $order_by); $user_table_headers['got_shirt'] = Users_table_header_link('got_shirt', __('T-Shirt'), $order_by);