engelsystem/includes/controller/public_dashboard_controller...

141 lines
4.3 KiB
PHP
Raw Normal View History

2017-12-11 22:26:36 +01:00
<?php
2022-11-09 00:02:30 +01:00
use Engelsystem\Models\AngelType;
2023-10-15 19:25:55 +02:00
use Engelsystem\Models\Location;
2023-02-13 21:19:45 +01:00
use Engelsystem\Models\News;
2023-01-03 22:19:03 +01:00
use Engelsystem\Models\Shifts\Shift;
2020-10-27 17:13:46 +01:00
use Engelsystem\ShiftsFilter;
2020-09-06 23:50:36 +02:00
2017-12-11 22:26:36 +01:00
/**
* Loads all data for the public dashboard
2017-12-25 23:12:52 +01:00
*
* @return array
2017-12-11 22:26:36 +01:00
*/
function public_dashboard_controller()
{
2020-10-27 17:13:46 +01:00
$filter = null;
2020-11-24 01:18:05 +01:00
if (request()->get('filtered')) {
2023-10-15 19:25:55 +02:00
$requestLocations = check_request_int_array('locations');
2020-11-24 01:18:05 +01:00
$requestAngelTypes = check_request_int_array('types');
2023-10-15 19:25:55 +02:00
if (!$requestLocations && !$requestAngelTypes) {
2020-11-24 01:18:05 +01:00
$sessionFilter = collect(session()->get('shifts-filter', []));
2023-10-15 19:25:55 +02:00
$requestLocations = $sessionFilter->get('locations', []);
2020-11-24 01:18:05 +01:00
$requestAngelTypes = $sessionFilter->get('types', []);
}
$angelTypes = collect(unrestricted_angeltypes());
2023-10-15 19:25:55 +02:00
$locations = $requestLocations ?: Location::orderBy('name')->get()->pluck('id')->toArray();
2020-11-24 01:18:05 +01:00
$angelTypes = $requestAngelTypes ?: $angelTypes->pluck('id')->toArray();
$filterValues = [
'userShiftsAdmin' => false,
'filled' => [],
2023-10-15 19:25:55 +02:00
'locations' => $locations,
2020-11-24 01:18:05 +01:00
'types' => $angelTypes,
'startTime' => null,
'endTime' => null,
];
2020-10-27 17:13:46 +01:00
$filter = new ShiftsFilter();
2020-11-24 01:18:05 +01:00
$filter->sessionImport($filterValues);
2020-10-27 17:13:46 +01:00
}
2017-12-12 21:57:57 +01:00
$stats = [
2020-10-27 17:13:46 +01:00
'needed-3-hours' => stats_angels_needed_three_hours($filter),
'needed-night' => stats_angels_needed_for_nightshifts($filter),
'angels-working' => stats_currently_working($filter),
'hours-to-work' => stats_hours_to_work($filter),
2017-12-12 21:57:57 +01:00
];
2017-12-25 23:12:52 +01:00
2020-10-27 17:13:46 +01:00
$free_shifts_source = Shifts_free(time(), time() + 12 * 60 * 60, $filter);
$free_shifts = [];
foreach ($free_shifts_source as $shift) {
2023-01-03 22:19:03 +01:00
$shift = Shift($shift);
2020-10-27 17:13:46 +01:00
$free_shift = public_dashboard_controller_free_shift($shift, $filter);
2017-12-25 23:12:52 +01:00
if (count($free_shift['needed_angels']) > 0) {
$free_shifts[] = $free_shift;
}
}
2017-12-25 23:12:52 +01:00
$highlighted_news = News::whereIsHighlighted(true)
2023-02-13 21:19:45 +01:00
->orderBy('updated_at')
->limit(1)
->get();
2017-12-11 22:26:36 +01:00
return [
__('Public Dashboard'),
public_dashboard_view($stats, $free_shifts, $highlighted_news),
2017-12-11 22:26:36 +01:00
];
}
/**
2017-12-25 21:29:00 +01:00
* Gathers information for free shifts to display.
*
2023-01-03 22:19:03 +01:00
* @param Shift $shift
2020-10-27 17:13:46 +01:00
* @param ShiftsFilter|null $filter
*
2017-12-25 23:12:52 +01:00
* @return array
*/
2023-01-03 22:19:03 +01:00
function public_dashboard_controller_free_shift(Shift $shift, ShiftsFilter $filter = null)
{
2023-01-03 22:19:03 +01:00
// ToDo move to model and return one
$free_shift = [
2023-01-03 22:19:03 +01:00
'id' => $shift->id,
2017-12-25 23:12:52 +01:00
'style' => 'default',
2023-01-03 22:19:03 +01:00
'start' => $shift->start->format('H:i'),
'end' => $shift->end->format('H:i'),
'duration' => round(($shift->end->timestamp - $shift->start->timestamp) / 3600),
'shifttype_name' => $shift->shiftType->name,
'title' => $shift->title,
2023-10-15 19:25:55 +02:00
'location_name' => $shift->location->name,
2023-01-03 22:19:03 +01:00
'needed_angels' => public_dashboard_needed_angels($shift->neededAngels, $filter),
];
2017-12-25 23:12:52 +01:00
2023-01-03 22:19:03 +01:00
if (time() + 3 * 60 * 60 > $shift->start->timestamp) {
$free_shift['style'] = 'warning';
}
2023-01-03 22:19:03 +01:00
if (time() > $shift->start->timestamp) {
$free_shift['style'] = 'danger';
}
2017-12-25 23:12:52 +01:00
return $free_shift;
}
/**
2017-12-25 21:29:00 +01:00
* Gathers information for needed angels on dashboard
*
2020-10-27 17:13:46 +01:00
* @param array $needed_angels
* @param ShiftsFilter|null $filter
*
2017-12-25 23:12:52 +01:00
* @return array
*/
2020-10-27 17:13:46 +01:00
function public_dashboard_needed_angels($needed_angels, ShiftsFilter $filter = null)
{
$result = [];
foreach ($needed_angels as $needed_angel) {
$need = $needed_angel['count'] - $needed_angel['taken'];
2023-01-18 13:02:11 +01:00
if ($need > 0 && (!$filter || in_array($needed_angel['angel_type_id'], $filter->getTypes()))) {
$angeltype = AngelType::find($needed_angel['angel_type_id']);
2022-11-09 00:02:30 +01:00
if ($angeltype->show_on_dashboard) {
$result[] = [
2017-12-25 23:12:52 +01:00
'need' => $need,
'angeltype_name' => $angeltype->name,
];
}
}
}
return $result;
}
/**
* Returns url to public dashboard
2017-12-25 23:12:52 +01:00
*
2020-10-27 17:13:46 +01:00
* @param array $parameters
*
2017-12-25 23:12:52 +01:00
* @return string
*/
2020-10-27 17:13:46 +01:00
function public_dashboard_link(array $parameters = []): string
{
2020-10-27 17:13:46 +01:00
return page_link_to('public-dashboard', $parameters);
}