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.

114 lines
2.3 KiB
PHTML

<?php
4 years ago
use Engelsystem\Models\Room;
use Engelsystem\ValidationResult;
4 years ago
use Illuminate\Support\Collection;
/**
* Validate a name for a room.
*
* @param string $name The new name
* @param int $room_id The room id
* @return ValidationResult
*/
4 years ago
function Room_validate_name(string $name, int $room_id)
{
$valid = true;
if (empty($name)) {
$valid = false;
}
4 years ago
$roomCount = Room::query()
->where('name', $name)
->where('id', '!=', $room_id)
->count();
if ($roomCount) {
$valid = false;
}
4 years ago
return new ValidationResult($valid, $name);
}
/**
* returns a list of rooms.
8 years ago
*
4 years ago
* @return Room[]|Collection
*/
function Rooms()
8 years ago
{
return Room::orderBy('name')->get();
}
/**
* Returns Room id array
*
4 years ago
* @return int[]
*/
function Room_ids()
{
4 years ago
return Room::query()
->select('id')
->pluck('id')
->toArray();
}
/**
* Delete a room
*
4 years ago
* @param Room $room
*/
4 years ago
function Room_delete(Room $room)
8 years ago
{
4 years ago
$room->delete();
engelsystem_log('Room deleted: ' . $room->name);
}
/**
* Create a new room
*
4 years ago
* @param string $name Name of the room
* @param string|null $map_url URL to a map tha can be displayed in an iframe
* @param string|null $description
*
* @return null|int
*/
4 years ago
function Room_create(string $name, string $map_url = null, string $description = null)
8 years ago
{
4 years ago
$room = new Room();
$room->name = $name;
$room->description = $description;
$room->map_url = $map_url;
$room->save();
engelsystem_log(
'Room created: ' . $name
. ', map_url: ' . $map_url
. ', description: ' . $description
);
4 years ago
return $room->id;
}
/**
* Update a room
*
4 years ago
* @param int $room_id The rooms id
* @param string $name Name of the room
* @param string|null $map_url URL to a map tha can be displayed in an iframe
* @param string|null $description Markdown description
*/
4 years ago
function Room_update(int $room_id, string $name, string $map_url = null, string $description = null)
{
4 years ago
$room = Room::find($room_id);
$room->name = $name;
$room->description = $description ?: null;
$room->map_url = $map_url ?: null;
$room->save();
engelsystem_log(
'Room updated: ' . $name .
', map_url: ' . $map_url .
', description: ' . $description
);
}