summaryrefslogtreecommitdiff
path: root/includes/model/Room_model.php
blob: f9eaf31dbbcd9406cc1993a37b31cdcdc3abb7ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php

use Engelsystem\Database\DB;
use Engelsystem\ValidationResult;

/**
 * Validate a name for a room.
 *
 * @param string $name    The new name
 * @param int    $room_id The room id
 * @return ValidationResult
 */
function Room_validate_name($name, $room_id)
{
    $valid = true;
    if (empty($name)) {
        $valid = false;
    }

    if (count(DB::select('SELECT RID FROM `Room` WHERE `Name`=? AND NOT `RID`=?', [
            $name,
            $room_id
        ])) > 0) {
        $valid = false;
    }
    return new ValidationResult($valid, $name);
}

/**
 * returns a list of rooms.
 *
 * @return array
 */
function Rooms()
{
    return DB::select('SELECT * FROM `Room` ORDER BY `Name`');
}

/**
 * Returns Room id array
 *
 * @return array
 */
function Room_ids()
{
    $result = DB::select('SELECT `RID` FROM `Room`');
    return select_array($result, 'RID', 'RID');
}

/**
 * Delete a room
 *
 * @param int $room_id
 */
function Room_delete($room_id)
{
    $room = Room($room_id);
    DB::delete('DELETE FROM `Room` WHERE `RID` = ?', [
        $room_id
    ]);
    engelsystem_log('Room deleted: ' . $room['Name']);
}

/**
 * Create a new room
 *
 * @param string  $name      Name of the room
 * @param string  $map_url   URL to a map tha can be displayed in an iframe
 * @param string description Markdown description
 * @return false|int
 */
function Room_create($name, $map_url, $description)
{
    DB::insert('
          INSERT INTO `Room` (`Name`, `map_url`, `description`)
           VALUES (?, ?, ?)
        ', [
        $name,
        $map_url,
        $description
    ]);
    $result = DB::getPdo()->lastInsertId();

    engelsystem_log(
        'Room created: ' . $name
        . ', map_url: ' . $map_url
        . ', description: ' . $description
    );

    return $result;
}

/**
 * Update a room
 *
 * @param int     $room_id     The rooms id
 * @param string  $name        Name of the room
 * @param string  $map_url     URL to a map tha can be displayed in an iframe
 * @param string  $description Markdown description
 * @return int
 */
function Room_update($room_id, $name, $map_url, $description)
{
    $result = DB::update('
        UPDATE `Room`
        SET
            `Name`=?,
            `map_url`=?,
            `description`=?
        WHERE `RID`=?
        LIMIT 1', [
        $name,
        $map_url,
        $description,
        $room_id
    ]);

    engelsystem_log(
        'Room updated: ' . $name .
        ', map_url: ' . $map_url .
        ', description: ' . $description
    );

    return $result;
}

/**
 * Returns room by id.
 *
 * @param int $room_id RID
 * @return array|null
 */
function Room($room_id)
{
    $room = DB::selectOne('
        SELECT *
        FROM `Room`
        WHERE `RID` = ?', [
        $room_id
    ]);

    return empty($room) ? null : $room;
}