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\ShiftCalendarRenderer;
use Engelsystem\ShiftsFilter;
use Illuminate\Database\Eloquent\Collection;
/**
* Route user actions.
@ -267,15 +268,16 @@ function users_list_controller()
$order_by = 'name';
if ($request->has('OrderBy') && in_array($request->input('OrderBy'), [
'name',
'last_name',
'first_name',
'last_name',
'dect',
'email',
'shirt_size',
'arrived',
'got_voucher',
'freeloads',
'active',
'force_active',
'got_shirt',
'shirt_size',
'planned_arrival_date',
'planned_departure_date',
'last_login_at',
@ -283,15 +285,24 @@ function users_list_controller()
$order_by = $request->input('OrderBy');
}
/** @var User[] $users */
/** @var User[]|Collection $users */
$users = User::with(['contact', 'personalData', 'state'])
->orderBy($order_by)
->orderBy('name')
->get();
foreach ($users as $user) {
$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 [
__('All users'),
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['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['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);