engelsystem/includes/model/Room_model.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

123 lines
2.5 KiB
PHP
Raw Normal View History

2013-12-29 15:08:33 +01:00
<?php
2017-12-25 23:12:52 +01:00
2020-09-06 23:50:36 +02:00
use Engelsystem\Models\Room;
2017-12-10 18:56:40 +01:00
use Engelsystem\ValidationResult;
2020-09-06 23:50:36 +02:00
use Illuminate\Support\Collection;
2017-12-10 18:56:40 +01:00
/**
* Validate a name for a room.
*
2017-12-25 23:12:52 +01:00
* @param string $name The new name
* @param int $room_id The room id
2017-12-10 18:56:40 +01:00
* @return ValidationResult
*/
2020-09-06 23:50:36 +02:00
function Room_validate_name(string $name, int $room_id)
2017-12-10 18:56:40 +01:00
{
$valid = true;
if (empty($name)) {
$valid = false;
}
2017-12-25 23:12:52 +01:00
2020-09-06 23:50:36 +02:00
$roomCount = Room::query()
->where('name', $name)
->where('id', '!=', $room_id)
->count();
if ($roomCount) {
2017-12-10 18:56:40 +01:00
$valid = false;
}
2020-09-06 23:50:36 +02:00
2017-12-10 18:56:40 +01:00
return new ValidationResult($valid, $name);
}
/**
* returns a list of rooms.
2017-01-02 15:43:36 +01:00
*
2020-09-06 23:50:36 +02:00
* @return Room[]|Collection
*/
function Rooms()
2017-01-02 03:57:23 +01:00
{
return Room::orderBy('name')->get();
}
2017-11-24 12:01:19 +01:00
/**
* Returns Room id array
*
2020-09-06 23:50:36 +02:00
* @return int[]
2017-11-24 12:01:19 +01:00
*/
function Room_ids()
{
2020-09-06 23:50:36 +02:00
return Room::query()
->select('id')
->pluck('id')
->toArray();
2017-11-24 12:01:19 +01:00
}
2013-12-29 15:08:33 +01:00
/**
2015-05-14 17:20:46 +02:00
* Delete a room
*
2020-09-06 23:50:36 +02:00
* @param Room $room
2013-12-29 15:08:33 +01:00
*/
2020-09-06 23:50:36 +02:00
function Room_delete(Room $room)
2017-01-02 03:57:23 +01:00
{
2020-09-06 23:50:36 +02:00
$room->delete();
engelsystem_log('Room deleted: ' . $room->name);
2017-12-10 18:56:40 +01:00
}
2015-05-14 17:20:46 +02:00
/**
* Create a new room
*
2020-09-06 23:50:36 +02:00
* @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
2015-05-14 17:20:46 +02:00
*/
2022-10-18 17:34:08 +02:00
function Room_create(string $name, string $map_url = null, string $description = null, string $dect = null)
2017-01-02 03:57:23 +01:00
{
2020-09-06 23:50:36 +02:00
$room = new Room();
$room->name = $name;
$room->description = $description;
$room->map_url = $map_url;
2022-10-18 17:34:08 +02:00
$room->dect = $dect;
2020-09-06 23:50:36 +02:00
$room->save();
2017-12-25 23:12:52 +01:00
2017-12-10 18:56:40 +01:00
engelsystem_log(
'Room created: ' . $name
2022-10-18 17:34:08 +02:00
. ', dect: ' . $dect
2017-12-10 18:56:40 +01:00
. ', map_url: ' . $map_url
. ', description: ' . $description
2017-01-03 03:22:48 +01:00
);
2017-12-25 23:12:52 +01:00
2020-09-06 23:50:36 +02:00
return $room->id;
2017-12-10 18:56:40 +01:00
}
2017-12-10 18:56:40 +01:00
/**
* Update a room
2017-12-10 18:56:40 +01:00
*
2020-09-06 23:50:36 +02:00
* @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
2017-12-10 18:56:40 +01:00
*/
2022-10-18 17:34:08 +02:00
function Room_update(
int $room_id,
string $name,
string $map_url = null,
string $description = null,
string $dect = null
) {
2020-09-06 23:50:36 +02:00
$room = Room::find($room_id);
$room->name = $name;
$room->description = $description ?: null;
$room->map_url = $map_url ?: null;
2022-10-18 17:34:08 +02:00
$room->dect = $dect ?: null;
2020-09-06 23:50:36 +02:00
$room->save();
2017-12-25 23:12:52 +01:00
2017-12-10 18:56:40 +01:00
engelsystem_log(
2017-12-25 23:12:52 +01:00
'Room updated: ' . $name .
2022-10-18 17:34:08 +02:00
', dect: ' . $dect .
2017-12-25 23:12:52 +01:00
', map_url: ' . $map_url .
2017-12-10 18:56:40 +01:00
', description: ' . $description
);
2013-12-29 15:08:33 +01:00
}