engelsystem/includes/controller/locations_controller.php

108 lines
2.6 KiB
PHP
Raw Normal View History

2014-12-22 17:55:20 +01:00
<?php
2017-08-28 16:21:10 +02:00
2022-11-09 00:02:30 +01:00
use Engelsystem\Models\AngelType;
2023-10-15 19:25:55 +02:00
use Engelsystem\Models\Location;
use Engelsystem\ShiftsFilter;
2017-01-02 15:43:36 +01:00
use Engelsystem\ShiftsFilterRenderer;
/**
2023-10-15 19:25:55 +02:00
* Location controllers for managing everything location related.
*/
/**
2023-10-15 19:25:55 +02:00
* View a location with its shifts.
2017-01-03 03:22:48 +01:00
*
* @return array
*/
2023-10-15 19:25:55 +02:00
function location_controller(): array
2017-01-02 03:57:23 +01:00
{
2023-10-15 19:25:55 +02:00
if (!auth()->can('view_locations')) {
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to());
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
$request = request();
2023-10-15 19:25:55 +02:00
$location = load_location();
2023-10-15 19:25:55 +02:00
$all_shifts = $location->shifts->sortBy('start');
2017-01-02 03:57:23 +01:00
$days = [];
foreach ($all_shifts as $shift) {
2023-01-03 22:19:03 +01:00
$day = $shift->start->format('Y-m-d');
2023-02-04 02:43:47 +01:00
if (!isset($days[$day])) {
$days[$day] = $shift->start->format(__('Y-m-d'));
2017-01-02 03:57:23 +01:00
}
}
2017-01-02 15:43:36 +01:00
$shiftsFilter = new ShiftsFilter(
true,
2023-10-15 19:25:55 +02:00
[$location->id],
2022-11-09 00:02:30 +01:00
AngelType::query()->get('id')->pluck('id')->toArray()
2017-01-02 15:43:36 +01:00
);
2017-01-03 14:12:17 +01:00
$selected_day = date('Y-m-d');
2023-02-04 02:43:47 +01:00
if (!empty($days) && !isset($days[$selected_day])) {
$selected_day = array_key_first($days);
2017-01-02 03:57:23 +01:00
}
if ($request->input('shifts_filter_day')) {
$selected_day = $request->input('shifts_filter_day');
2017-01-02 03:57:23 +01:00
}
2017-01-03 14:12:17 +01:00
$shiftsFilter->setStartTime(parse_date('Y-m-d H:i', $selected_day . ' 00:00'));
$shiftsFilter->setEndTime(parse_date('Y-m-d H:i', $selected_day . ' 23:59'));
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$shiftsFilterRenderer = new ShiftsFilterRenderer($shiftsFilter);
$shiftsFilterRenderer->enableDaySelection($days);
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$shiftCalendarRenderer = shiftCalendarRendererByShiftFilter($shiftsFilter);
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
return [
2023-10-15 19:25:55 +02:00
$location->name,
location_view($location, $shiftsFilterRenderer, $shiftCalendarRenderer),
2017-01-02 15:43:36 +01:00
];
}
/**
2023-10-15 19:25:55 +02:00
* Dispatch different location actions.
2017-01-03 03:22:48 +01:00
*
* @return array
*/
2023-10-15 19:25:55 +02:00
function locations_controller(): array
2017-01-02 03:57:23 +01:00
{
$request = request();
$action = $request->input('action');
if (!$request->has('action')) {
$action = 'list';
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2022-12-22 00:08:54 +01:00
return match ($action) {
2023-10-15 19:25:55 +02:00
'view' => location_controller(),
'list' => throw_redirect(page_link_to('admin/locations')),
default => throw_redirect(page_link_to('admin/locations')),
2022-12-22 00:08:54 +01:00
};
}
2014-12-22 17:55:20 +01:00
2017-01-03 03:22:48 +01:00
/**
2023-10-15 19:25:55 +02:00
* @param Location $location
2017-01-03 03:22:48 +01:00
* @return string
*/
2023-10-15 19:25:55 +02:00
function location_link(Location $location)
2017-01-02 03:57:23 +01:00
{
2023-10-15 19:25:55 +02:00
return page_link_to('locations', ['action' => 'view', 'location_id' => $location->id]);
2014-12-22 17:55:20 +01:00
}
/**
2023-10-15 19:25:55 +02:00
* Loads location by request param location_id
2017-01-03 03:22:48 +01:00
*
2023-10-15 19:25:55 +02:00
* @return Location
*/
2023-10-15 19:25:55 +02:00
function load_location()
2017-01-02 03:57:23 +01:00
{
2023-10-15 19:25:55 +02:00
if (!test_request_int('location_id')) {
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to());
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2023-10-15 19:25:55 +02:00
$location = Location::find(request()->input('location_id'));
if (!$location) {
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to());
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2023-10-15 19:25:55 +02:00
return $location;
}