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;
|
2020-09-06 23:50:36 +02:00
|
|
|
use Engelsystem\Models\Room;
|
2016-10-05 18:56:50 +02:00
|
|
|
use Engelsystem\ShiftsFilter;
|
2017-01-02 15:43:36 +01:00
|
|
|
use Engelsystem\ShiftsFilterRenderer;
|
2016-10-05 18:56:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Room controllers for managing everything room related.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* View a room with its shifts.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return array
|
2016-10-05 18:56:50 +02:00
|
|
|
*/
|
2021-11-30 23:29:51 +01:00
|
|
|
function room_controller(): array
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2018-11-12 14:41:23 +01:00
|
|
|
if (!auth()->can('view_rooms')) {
|
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
|
|
|
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
2017-12-10 15:02:37 +01:00
|
|
|
$room = load_room();
|
2017-07-17 23:08:15 +02:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$all_shifts = Shifts_by_room($room);
|
|
|
|
$days = [];
|
|
|
|
foreach ($all_shifts as $shift) {
|
2017-01-03 14:12:17 +01:00
|
|
|
$day = date('Y-m-d', $shift['start']);
|
2017-01-02 15:43:36 +01:00
|
|
|
if (!in_array($day, $days)) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$days[] = $day;
|
|
|
|
}
|
2016-10-05 18:56:50 +02:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
|
|
|
$shiftsFilter = new ShiftsFilter(
|
|
|
|
true,
|
2020-09-06 23:50:36 +02:00
|
|
|
[$room->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');
|
2021-12-28 23:39:26 +01:00
|
|
|
if (!empty($days) && !in_array($selected_day, $days)) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$selected_day = $days[0];
|
|
|
|
}
|
2017-07-18 21:38:53 +02:00
|
|
|
if ($request->has('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 [
|
2020-09-06 23:50:36 +02:00
|
|
|
$room->name,
|
2017-01-02 15:43:36 +01:00
|
|
|
Room_view($room, $shiftsFilterRenderer, $shiftCalendarRenderer)
|
|
|
|
];
|
2016-10-05 18:56:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch different room actions.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return array
|
2016-10-05 18:56:50 +02:00
|
|
|
*/
|
2021-11-30 23:29:51 +01:00
|
|
|
function rooms_controller(): array
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2017-07-18 21:38:53 +02: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) {
|
|
|
|
'view' => room_controller(),
|
|
|
|
'list' => throw_redirect(page_link_to('admin_rooms')),
|
|
|
|
default => throw_redirect(page_link_to('admin_rooms')),
|
|
|
|
};
|
2016-10-05 18:56:50 +02:00
|
|
|
}
|
2014-12-22 17:55:20 +01:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2020-09-06 23:50:36 +02:00
|
|
|
* @param Room $room
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2020-09-06 23:50:36 +02:00
|
|
|
function room_link(Room $room)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2020-09-06 23:50:36 +02:00
|
|
|
return page_link_to('rooms', ['action' => 'view', 'room_id' => $room->id]);
|
2014-12-22 17:55:20 +01:00
|
|
|
}
|
|
|
|
|
2016-10-05 18:56:50 +02:00
|
|
|
/**
|
|
|
|
* Loads room by request param room_id
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
2020-09-06 23:50:36 +02:00
|
|
|
* @return Room
|
2016-10-05 18:56:50 +02:00
|
|
|
*/
|
2017-12-10 15:02:37 +01:00
|
|
|
function load_room()
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2017-01-02 15:43:36 +01:00
|
|
|
if (!test_request_int('room_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
|
|
|
|
2020-09-06 23:50:36 +02:00
|
|
|
$room = Room::find(request()->input('room_id'));
|
|
|
|
if (!$room) {
|
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
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return $room;
|
2016-10-05 18:56:50 +02:00
|
|
|
}
|