2011-06-02 16:56:45 +02:00
|
|
|
<?php
|
2020-09-06 23:50:36 +02:00
|
|
|
|
2022-07-31 19:17:44 +02:00
|
|
|
use Engelsystem\Helpers\Carbon;
|
2020-09-06 23:50:36 +02:00
|
|
|
use Engelsystem\Models\Room;
|
2022-07-31 19:17:44 +02:00
|
|
|
use Engelsystem\Models\User\User;
|
2020-09-06 23:50:36 +02:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function admin_rooms_title()
|
|
|
|
{
|
2018-08-29 21:55:32 +02:00
|
|
|
return __('Rooms');
|
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_rooms()
|
|
|
|
{
|
2017-12-10 18:56:40 +01:00
|
|
|
$rooms_source = Rooms();
|
2017-01-02 03:57:23 +01:00
|
|
|
$rooms = [];
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
foreach ($rooms_source as $room) {
|
|
|
|
$rooms[] = [
|
2017-12-25 23:12:52 +01:00
|
|
|
'name' => Room_name_render($room),
|
2022-10-18 17:34:08 +02:00
|
|
|
'dect' => icon_bool($room->dect),
|
2021-07-22 21:22:21 +02:00
|
|
|
'map_url' => icon_bool($room->map_url),
|
2017-12-25 23:12:52 +01:00
|
|
|
'actions' => table_buttons([
|
|
|
|
button(
|
2020-09-06 23:50:36 +02:00
|
|
|
page_link_to('admin_rooms', ['show' => 'edit', 'id' => $room->id]),
|
2018-08-29 21:55:32 +02:00
|
|
|
__('edit'),
|
2021-07-23 01:52:19 +02:00
|
|
|
'btn-sm'
|
2017-12-25 23:12:52 +01:00
|
|
|
),
|
|
|
|
button(
|
2020-09-06 23:50:36 +02:00
|
|
|
page_link_to('admin_rooms', ['show' => 'delete', 'id' => $room->id]),
|
2018-08-29 21:55:32 +02:00
|
|
|
__('delete'),
|
2021-07-23 01:52:19 +02:00
|
|
|
'btn-sm'
|
2017-12-25 23:12:52 +01:00
|
|
|
)
|
2017-01-02 15:43:36 +01:00
|
|
|
])
|
|
|
|
];
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-12-25 23:12:52 +01:00
|
|
|
$room = null;
|
2017-07-18 21:38:53 +02:00
|
|
|
if ($request->has('show')) {
|
2017-01-03 14:12:17 +01:00
|
|
|
$msg = '';
|
|
|
|
$name = '';
|
2017-12-10 15:02:37 +01:00
|
|
|
$map_url = null;
|
|
|
|
$description = null;
|
2022-10-18 17:34:08 +02:00
|
|
|
$dect = null;
|
2017-01-03 03:22:48 +01:00
|
|
|
$room_id = 0;
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-12-10 18:56:40 +01:00
|
|
|
$angeltypes_source = AngelTypes();
|
2017-01-02 03:57:23 +01:00
|
|
|
$angeltypes = [];
|
|
|
|
$angeltypes_count = [];
|
|
|
|
foreach ($angeltypes_source as $angeltype) {
|
|
|
|
$angeltypes[$angeltype['id']] = $angeltype['name'];
|
|
|
|
$angeltypes_count[$angeltype['id']] = 0;
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if (test_request_int('id')) {
|
2020-09-06 23:50:36 +02:00
|
|
|
$room = Room::find($request->input('id'));
|
|
|
|
if (!$room) {
|
2019-09-08 02:25:49 +02:00
|
|
|
throw_redirect(page_link_to('admin_rooms'));
|
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_id = $room->id;
|
|
|
|
$name = $room->name;
|
|
|
|
$map_url = $room->map_url;
|
|
|
|
$description = $room->description;
|
2022-10-18 17:34:08 +02:00
|
|
|
$dect = $room->dect;
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-12-10 18:56:40 +01:00
|
|
|
$needed_angeltypes = NeededAngelTypes_by_room($room_id);
|
2017-01-02 03:57:23 +01:00
|
|
|
foreach ($needed_angeltypes as $needed_angeltype) {
|
|
|
|
$angeltypes_count[$needed_angeltype['angel_type_id']] = $needed_angeltype['count'];
|
|
|
|
}
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-07-18 21:38:53 +02:00
|
|
|
if ($request->input('show') == 'edit') {
|
2018-11-20 16:02:03 +01:00
|
|
|
if ($request->hasPostData('submit')) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$valid = true;
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2022-07-20 19:08:51 +02:00
|
|
|
if ($request->has('name') && strlen(strip_request_tags('name')) > 0) {
|
|
|
|
$result = Room_validate_name(strip_request_tags('name'), $room_id);
|
2017-12-25 23:12:52 +01:00
|
|
|
if (!$result->isValid()) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$valid = false;
|
2018-08-29 21:55:32 +02:00
|
|
|
$msg .= error(__('This name is already in use.'), true);
|
2017-12-10 18:56:40 +01:00
|
|
|
} else {
|
|
|
|
$name = $result->getValue();
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$valid = false;
|
2018-08-29 21:55:32 +02:00
|
|
|
$msg .= error(__('Please enter a name.'), true);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-12-10 15:02:37 +01:00
|
|
|
if ($request->has('map_url')) {
|
|
|
|
$map_url = strip_request_item('map_url');
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-12-10 15:02:37 +01:00
|
|
|
if ($request->has('description')) {
|
2017-12-25 23:12:52 +01:00
|
|
|
$description = strip_request_item_nl('description');
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2022-10-18 17:34:08 +02:00
|
|
|
if ($request->has('dect')) {
|
|
|
|
$dect = strip_request_item_nl('dect');
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
foreach ($angeltypes as $angeltype_id => $angeltype) {
|
2017-08-29 16:21:25 +02:00
|
|
|
$angeltypes_count[$angeltype_id] = 0;
|
|
|
|
$queryKey = 'angeltype_count_' . $angeltype_id;
|
|
|
|
if (!$request->has($queryKey)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match('/^\d{1,4}$/', $request->input($queryKey))) {
|
|
|
|
$angeltypes_count[$angeltype_id] = $request->input($queryKey);
|
2017-01-02 03:57:23 +01:00
|
|
|
} else {
|
|
|
|
$valid = false;
|
2017-12-25 23:12:52 +01:00
|
|
|
$msg .= error(sprintf(
|
2018-08-29 21:55:32 +02:00
|
|
|
__('Please enter needed angels for type %s.'),
|
2017-12-25 23:12:52 +01:00
|
|
|
$angeltype
|
|
|
|
), true);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($valid) {
|
2017-12-10 18:56:40 +01:00
|
|
|
if (empty($room_id)) {
|
2022-10-18 17:34:08 +02:00
|
|
|
$room_id = Room_create($name, $map_url, $description, $dect);
|
2017-12-10 18:56:40 +01:00
|
|
|
} else {
|
2022-10-18 17:34:08 +02:00
|
|
|
Room_update($room_id, $name, $map_url, $description, $dect);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
NeededAngelTypes_delete_by_room($room_id);
|
|
|
|
$needed_angeltype_info = [];
|
|
|
|
foreach ($angeltypes_count as $angeltype_id => $angeltype_count) {
|
|
|
|
$angeltype = AngelType($angeltype_id);
|
2018-01-14 17:47:26 +01:00
|
|
|
if (!empty($angeltype)) {
|
2017-01-02 03:57:23 +01:00
|
|
|
NeededAngelType_add(null, $angeltype_id, $room_id, $angeltype_count);
|
2017-12-25 23:12:52 +01:00
|
|
|
if ($angeltype_count > 0) {
|
2017-12-12 20:04:36 +01:00
|
|
|
$needed_angeltype_info[] = $angeltype['name'] . ': ' . $angeltype_count;
|
|
|
|
}
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
|
|
|
engelsystem_log(
|
2017-01-03 14:12:17 +01:00
|
|
|
'Set needed angeltypes of room ' . $name
|
|
|
|
. ' to: ' . join(', ', $needed_angeltype_info)
|
2017-01-02 15:43:36 +01:00
|
|
|
);
|
2018-08-29 21:55:32 +02:00
|
|
|
success(__('Room saved.'));
|
2019-09-08 02:25:49 +02:00
|
|
|
throw_redirect(page_link_to('admin_rooms'));
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$angeltypes_count_form = [];
|
|
|
|
foreach ($angeltypes as $angeltype_id => $angeltype) {
|
|
|
|
$angeltypes_count_form[] = div('col-lg-4 col-md-6 col-xs-6', [
|
2017-01-02 15:43:36 +01:00
|
|
|
form_spinner('angeltype_count_' . $angeltype_id, $angeltype, $angeltypes_count[$angeltype_id])
|
|
|
|
]);
|
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 page_with_title(admin_rooms_title(), [
|
2017-01-02 15:43:36 +01:00
|
|
|
buttons([
|
2018-08-29 21:55:32 +02:00
|
|
|
button(page_link_to('admin_rooms'), __('back'), 'back')
|
2017-01-02 15:43:36 +01:00
|
|
|
]),
|
|
|
|
$msg,
|
|
|
|
form([
|
|
|
|
div('row', [
|
|
|
|
div('col-md-6', [
|
2018-12-18 13:22:10 +01:00
|
|
|
form_text('name', __('Name'), $name, false, 35),
|
2022-10-18 17:34:08 +02:00
|
|
|
form_text('dect', __('DECT'), $dect),
|
2018-08-29 21:55:32 +02:00
|
|
|
form_text('map_url', __('Map URL'), $map_url),
|
|
|
|
form_info('', __('The map url is used to display an iframe on the room page.')),
|
|
|
|
form_textarea('description', __('Description'), $description),
|
|
|
|
form_info('', __('Please use markdown for the description.')),
|
2017-01-02 15:43:36 +01:00
|
|
|
]),
|
|
|
|
div('col-md-6', [
|
|
|
|
div('row', [
|
|
|
|
div('col-md-12', [
|
2018-08-29 21:55:32 +02:00
|
|
|
form_info(__('Needed angels:'))
|
2017-01-02 15:43:36 +01:00
|
|
|
]),
|
|
|
|
join($angeltypes_count_form)
|
|
|
|
])
|
|
|
|
])
|
|
|
|
]),
|
2018-08-29 21:55:32 +02:00
|
|
|
form_submit('submit', __('Save'))
|
2017-01-02 15:43:36 +01:00
|
|
|
])
|
2019-12-14 10:35:42 +01:00
|
|
|
], true);
|
2017-07-18 21:38:53 +02:00
|
|
|
} elseif ($request->input('show') == 'delete') {
|
2018-11-20 16:02:03 +01:00
|
|
|
if ($request->hasPostData('ack')) {
|
2020-09-06 23:50:36 +02:00
|
|
|
$room = Room::find($room_id);
|
|
|
|
$shifts = Shifts_by_room($room);
|
2019-09-07 01:44:15 +02:00
|
|
|
foreach ($shifts as $shift) {
|
|
|
|
$shift = Shift($shift['SID']);
|
2022-07-31 19:17:44 +02:00
|
|
|
foreach ($shift['ShiftEntry'] as $entry) {
|
|
|
|
$type = AngelType($entry['TID']);
|
|
|
|
event('shift.entry.deleting', [
|
|
|
|
'user' => User::find($entry['user_id']),
|
|
|
|
'start' => Carbon::createFromTimestamp($shift['start']),
|
|
|
|
'end' => Carbon::createFromTimestamp($shift['end']),
|
|
|
|
'name' => $shift['name'],
|
|
|
|
'title' => $shift['title'],
|
|
|
|
'type' => $type['name'],
|
|
|
|
'room' => $room,
|
|
|
|
'freeloaded' => (bool)$entry['freeloaded'],
|
|
|
|
]);
|
|
|
|
}
|
2019-09-07 01:44:15 +02:00
|
|
|
}
|
|
|
|
|
2020-09-06 23:50:36 +02:00
|
|
|
Room_delete($room);
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2018-08-29 21:55:32 +02:00
|
|
|
success(sprintf(__('Room %s deleted.'), $name));
|
2019-09-08 02:25:49 +02:00
|
|
|
throw_redirect(page_link_to('admin_rooms'));
|
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 page_with_title(admin_rooms_title(), [
|
2017-01-02 15:43:36 +01:00
|
|
|
buttons([
|
2018-08-29 21:55:32 +02:00
|
|
|
button(page_link_to('admin_rooms'), __('back'), 'back')
|
2017-01-02 15:43:36 +01:00
|
|
|
]),
|
2018-08-29 21:55:32 +02:00
|
|
|
sprintf(__('Do you want to delete room %s?'), $name),
|
2018-11-20 16:02:03 +01:00
|
|
|
form([
|
|
|
|
form_submit('ack', __('Delete'), 'delete btn-danger'),
|
|
|
|
], page_link_to('admin_rooms', ['show' => 'delete', 'id' => $room_id])),
|
2019-12-14 10:35:42 +01:00
|
|
|
], true);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2012-04-26 22:37:37 +02:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return page_with_title(admin_rooms_title(), [
|
2017-01-02 15:43:36 +01:00
|
|
|
buttons([
|
2018-08-29 21:55:32 +02:00
|
|
|
button(page_link_to('admin_rooms', ['show' => 'edit']), __('add'))
|
2017-01-02 15:43:36 +01:00
|
|
|
]),
|
|
|
|
msg(),
|
|
|
|
table([
|
2018-08-29 21:55:32 +02:00
|
|
|
'name' => __('Name'),
|
2022-10-18 17:34:08 +02:00
|
|
|
'dect' => __('DECT'),
|
2018-08-29 21:55:32 +02:00
|
|
|
'map_url' => __('Map'),
|
2017-12-25 23:12:52 +01:00
|
|
|
'actions' => ''
|
2017-01-02 15:43:36 +01:00
|
|
|
], $rooms)
|
2019-12-14 10:35:42 +01:00
|
|
|
], true);
|
2011-06-02 16:56:45 +02:00
|
|
|
}
|