You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
8.1 KiB
PHTML

<?php
use Engelsystem\Models\User\User;
/**
* @return string
*/
8 years ago
function admin_arrive_title()
{
return __('Arrive angels');
}
/**
* @return string
*/
8 years ago
function admin_arrive()
{
$msg = '';
$search = '';
$request = request();
if ($request->has('search')) {
8 years ago
$search = strip_request_item('search');
$search = trim($search);
}
8 years ago
$action = $request->get('action');
if (
$action == 'reset'
&& preg_match('/^\d+$/', $request->input('user'))
&& $request->hasPostData('submit')
) {
$user_id = $request->input('user');
$user_source = User::find($user_id);
if ($user_source) {
$user_source->state->arrived = false;
$user_source->state->arrival_date = null;
$user_source->state->save();
engelsystem_log('User set to not arrived: ' . User_Nick_render($user_source, true));
success(__('Reset done. Angel has not arrived.'));
throw_redirect(user_link($user_source->id));
8 years ago
} else {
$msg = error(__('Angel not found.'), true);
8 years ago
}
} elseif (
$action == 'arrived'
&& preg_match('/^\d+$/', $request->input('user'))
&& $request->hasPostData('submit')
) {
$user_id = $request->input('user');
$user_source = User::find($user_id);
if ($user_source) {
$user_source->state->arrived = true;
$user_source->state->arrival_date = new Carbon\Carbon();
$user_source->state->save();
engelsystem_log('User set has arrived: ' . User_Nick_render($user_source, true));
success(__('Angel has been marked as arrived.'));
throw_redirect(user_link($user_source->id));
8 years ago
} else {
$msg = error(__('Angel not found.'), true);
11 years ago
}
12 years ago
}
8 years ago
/** @var User[] $users */
$users = User::with('personalData')->orderBy('name')->get();
8 years ago
$arrival_count_at_day = [];
$planned_arrival_count_at_day = [];
$planned_departure_count_at_day = [];
$users_matched = [];
if ($search == '') {
8 years ago
$tokens = [];
} else {
$tokens = explode(' ', $search);
}
8 years ago
foreach ($users as $usr) {
if (count($tokens) > 0) {
$match = false;
$index = join(' ', $usr->attributesToArray());
foreach ($tokens as $token) {
$token = trim($token);
if (!empty($token) && stristr($index, $token)) {
8 years ago
$match = true;
break;
}
}
8 years ago
if (!$match) {
8 years ago
continue;
}
}
8 years ago
$usr->name = User_Nick_render($usr) . User_Pronoun_render($usr);
$plannedDepartureDate = $usr->personalData->planned_departure_date;
$arrivalDate = $usr->state->arrival_date;
$plannedArrivalDate = $usr->personalData->planned_arrival_date;
$usr['rendered_planned_departure_date'] = $plannedDepartureDate ? $plannedDepartureDate->format(__('Y-m-d')) : '-';
$usr['rendered_planned_arrival_date'] = $plannedArrivalDate ? $plannedArrivalDate->format(__('Y-m-d')) : '-';
$usr['rendered_arrival_date'] = $arrivalDate ? $arrivalDate->format(__('Y-m-d')) : '-';
$usr['arrived'] = icon_bool($usr->state->arrived);
$usr['actions'] = form([
form_hidden('action', $usr->state->arrived ? 'reset' : 'arrived'),
form_hidden('user', $usr->id),
form_submit(
'submit', $usr->state->arrived ? __('reset') : __('arrived'), 'btn-xs', true,
$usr->state->arrived ? 'default' : 'primary'
),
]);
8 years ago
if ($usr->state->arrival_date) {
$day = $usr->state->arrival_date->format('Y-m-d');
8 years ago
if (!isset($arrival_count_at_day[$day])) {
8 years ago
$arrival_count_at_day[$day] = 0;
}
8 years ago
$arrival_count_at_day[$day]++;
8 years ago
}
8 years ago
if ($usr->personalData->planned_arrival_date) {
$day = $usr->personalData->planned_arrival_date->format('Y-m-d');
8 years ago
if (!isset($planned_arrival_count_at_day[$day])) {
8 years ago
$planned_arrival_count_at_day[$day] = 0;
}
8 years ago
$planned_arrival_count_at_day[$day]++;
8 years ago
}
8 years ago
if ($usr->personalData->planned_departure_date && $usr->state->arrived) {
$day = $usr->personalData->planned_departure_date->format('Y-m-d');
8 years ago
if (!isset($planned_departure_count_at_day[$day])) {
8 years ago
$planned_departure_count_at_day[$day] = 0;
}
8 years ago
$planned_departure_count_at_day[$day]++;
8 years ago
}
8 years ago
8 years ago
$users_matched[] = $usr;
}
8 years ago
8 years ago
ksort($arrival_count_at_day);
ksort($planned_arrival_count_at_day);
ksort($planned_departure_count_at_day);
8 years ago
8 years ago
$arrival_at_day = [];
$arrival_sum = 0;
foreach ($arrival_count_at_day as $day => $count) {
$arrival_sum += $count;
$arrival_at_day[$day] = [
8 years ago
'day' => $day,
'count' => $count,
'sum' => $arrival_sum
];
8 years ago
}
8 years ago
8 years ago
$planned_arrival_at_day = [];
$planned_arrival_sum = 0;
foreach ($planned_arrival_count_at_day as $day => $count) {
$planned_arrival_sum += $count;
$planned_arrival_at_day[$day] = [
8 years ago
'day' => $day,
'count' => $count,
'sum' => $planned_arrival_sum
];
8 years ago
}
8 years ago
8 years ago
$planned_departure_at_day = [];
$planned_departure_sum = 0;
foreach ($planned_departure_count_at_day as $day => $count) {
$planned_departure_sum += $count;
$planned_departure_at_day[$day] = [
8 years ago
'day' => $day,
'count' => $count,
'sum' => $planned_departure_sum
];
8 years ago
}
8 years ago
8 years ago
return page_with_title(admin_arrive_title(), [
$msg . msg(),
8 years ago
form([
form_text('search', __('Search'), $search),
form_submit('submit', __('Search'))
], page_link_to('admin_arrive')),
8 years ago
table([
'name' => __('Nickname'),
'rendered_planned_arrival_date' => __('Planned arrival'),
'arrived' => __('Arrived?'),
'rendered_arrival_date' => __('Arrival date'),
'rendered_planned_departure_date' => __('Planned departure'),
'actions' => ''
8 years ago
], $users_matched),
div('row', [
div('col-md-4', [
heading(__('Planned arrival statistics'), 2),
8 years ago
bargraph('planned_arrives', 'day', [
'count' => __('arrived'),
'sum' => __('arrived sum')
8 years ago
], [
'count' => '#090',
'sum' => '#888'
], $planned_arrival_at_day),
table([
'day' => __('Date'),
'count' => __('Count'),
'sum' => __('Sum')
8 years ago
], $planned_arrival_at_day)
]),
div('col-md-4', [
heading(__('Arrival statistics'), 2),
8 years ago
bargraph('arrives', 'day', [
'count' => __('arrived'),
'sum' => __('arrived sum')
8 years ago
], [
'count' => '#090',
'sum' => '#888'
], $arrival_at_day),
table([
'day' => __('Date'),
'count' => __('Count'),
'sum' => __('Sum')
8 years ago
], $arrival_at_day)
]),
div('col-md-4', [
heading(__('Planned departure statistics'), 2),
8 years ago
bargraph('planned_departures', 'day', [
'count' => __('arrived'),
'sum' => __('arrived sum')
8 years ago
], [
'count' => '#090',
'sum' => '#888'
], $planned_departure_at_day),
table([
'day' => __('Date'),
'count' => __('Count'),
'sum' => __('Sum')
8 years ago
], $planned_departure_at_day)
])
])
]);
}