engelsystem/includes/pages/admin_free.php

121 lines
3.6 KiB
PHP
Raw Normal View History

<?php
use Engelsystem\Database\DB;
use Engelsystem\Models\User\User;
2018-10-17 01:30:10 +02:00
use Illuminate\Database\Query\JoinClause;
2017-01-03 03:22:48 +01:00
/**
* @return string
*/
2017-01-02 03:57:23 +01:00
function admin_free_title()
{
return __('Free angels');
2013-11-25 21:04:58 +01:00
}
2017-01-03 03:22:48 +01:00
/**
* @return string
*/
2017-01-02 03:57:23 +01:00
function admin_free()
{
$request = request();
2017-01-02 15:43:36 +01:00
2017-01-03 14:12:17 +01:00
$search = '';
if ($request->has('search')) {
2017-01-02 03:57:23 +01:00
$search = strip_request_item('search');
}
2017-01-02 15:43:36 +01:00
$angel_types_source = DB::select('SELECT `id`, `name` FROM `AngelTypes` ORDER BY `name`');
2017-01-02 03:57:23 +01:00
$angel_types = [
2017-01-02 15:43:36 +01:00
'' => 'alle Typen'
];
2017-01-02 03:57:23 +01:00
foreach ($angel_types_source as $angel_type) {
$angel_types[$angel_type['id']] = $angel_type['name'];
}
2017-01-02 15:43:36 +01:00
2018-10-17 01:30:10 +02:00
$angelType = $request->input('angeltype', '');
$query = User::query()
->select('users.*')
->leftJoin('ShiftEntry', 'users.id', 'ShiftEntry.UID')
->leftJoin('users_state', 'users.id', 'users_state.user_id')
->leftJoin('Shifts', function ($join) {
/** @var JoinClause $join */
$join->on('ShiftEntry.SID', '=', 'Shifts.SID')
->where('Shifts.start', '<', time())
->where('Shifts.end', '>', time());
})
->where('users_state.arrived', '=', 1)
->whereNull('Shifts.SID')
->groupBy('users.id');
if (!empty($angelType)) {
$query->join('UserAngelTypes', function ($join) use ($angelType, $request, $query) {
/** @var JoinClause $join */
$join->on('UserAngelTypes.user_id', '=', 'users.id')
->where('UserAngelTypes.angeltype_id', '=', $angelType);
if ($request->has('confirmed_only')) {
$join->whereNotNull('UserAngelTypes.confirm_user_id');
}
});
}
2017-01-02 15:43:36 +01:00
2018-10-17 01:30:10 +02:00
$users = $query->get();
2017-01-02 03:57:23 +01:00
$free_users_table = [];
2017-01-03 14:12:17 +01:00
if ($search == '') {
2017-01-02 03:57:23 +01:00
$tokens = [];
} else {
2017-01-03 14:12:17 +01:00
$tokens = explode(' ', $search);
}
2017-01-02 03:57:23 +01:00
foreach ($users as $usr) {
if (count($tokens) > 0) {
$match = false;
$index = join('', $usr->toArray());
2017-01-02 03:57:23 +01:00
foreach ($tokens as $t) {
if (stristr($index, trim($t))) {
$match = true;
break;
}
}
2017-01-02 15:43:36 +01:00
if (!$match) {
2017-01-02 03:57:23 +01:00
continue;
}
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$free_users_table[] = [
2017-01-02 15:43:36 +01:00
'name' => User_Nick_render($usr),
'shift_state' => User_shift_state_render($usr),
'dect' => $usr->contact->dect,
'email' => $usr->settings->email_human ? ($usr->contact->email ? $usr->contact->email : $usr->email) : glyph('eye-close'),
2017-01-02 15:43:36 +01:00
'actions' =>
auth()->can('admin_user')
? button(page_link_to('admin_user', ['id' => $usr->id]), __('edit'), 'btn-xs')
2017-01-02 15:43:36 +01:00
: ''
];
2017-01-02 03:57:23 +01:00
}
return page_with_title(admin_free_title(), [
2017-01-02 15:43:36 +01:00
form([
div('row', [
div('col-md-4', [
form_text('search', __('Search'), $search)
2017-01-02 15:43:36 +01:00
]),
div('col-md-4', [
form_select('angeltype', __('Angeltype'), $angel_types, $angelType)
2017-01-02 15:43:36 +01:00
]),
div('col-md-2', [
form_checkbox('confirmed_only', __('Only confirmed'), $request->has('confirmed_only'))
2017-01-02 15:43:36 +01:00
]),
div('col-md-2', [
form_submit('submit', __('Search'))
2017-01-02 15:43:36 +01:00
])
])
]),
table([
'name' => __('Nick'),
2017-01-02 15:43:36 +01:00
'shift_state' => '',
'dect' => __('DECT'),
'email' => __('E-Mail'),
2017-01-02 15:43:36 +01:00
'actions' => ''
], $free_users_table)
]);
}