From afb77d22ba35dfbee74bbbee95626d55edef8898 Mon Sep 17 00:00:00 2001 From: msquare Date: Sun, 10 Dec 2017 18:56:40 +0100 Subject: move room db queries to model --- includes/model/Room_model.php | 129 +++++++++++++++++++++++++++++++++++------- 1 file changed, 109 insertions(+), 20 deletions(-) (limited to 'includes/model/Room_model.php') diff --git a/includes/model/Room_model.php b/includes/model/Room_model.php index 79536e93..098968c9 100644 --- a/includes/model/Room_model.php +++ b/includes/model/Room_model.php @@ -1,6 +1,30 @@ 0) { + $valid = false; + } + return new ValidationResult($valid, $name); +} /** * returns a list of rooms. @@ -26,20 +50,38 @@ function Room_ids() /** * Delete a room * - * @param int $room_id + * @param int $room_id */ function Room_delete($room_id) { - DB::delete('DELETE FROM `Room` WHERE `RID` = ?', [$room_id]); + DB::delete('DELETE FROM `Room` WHERE `RID` = ?', [ + $room_id + ]); +} + +/** + * Delete a room by its name + * + * @param string $name + */ +function Room_delete_by_name($name) +{ + DB::delete('DELETE FROM `Room` WHERE `Name` = ?', [ + $name + ]); } /** * Create a new room * - * @param string $name Name of the room - * @param boolean $from_frab Is this a frab imported room? - * @param string $map_url URL to a map tha can be displayed in an iframe - * @param description markdown description + * @param string $name + * Name of the room + * @param boolean $from_frab + * Is this a frab imported room? + * @param string $map_url + * URL to a map tha can be displayed in an iframe + * @param + * description markdown description * @return false|int */ function Room_create($name, $from_frab, $map_url, $description) @@ -47,23 +89,70 @@ function Room_create($name, $from_frab, $map_url, $description) DB::insert(' INSERT INTO `Room` (`Name`, `from_frab`, `map_url`, `description`) VALUES (?, ?, ?, ?) - ', - [ - $name, - (int) $from_frab, - $map_url, - $description, - ] + ', [ + $name, + (int) $from_frab, + $map_url, + $description + ]); + $result = DB::getPdo()->lastInsertId(); + + engelsystem_log( + 'Room created: ' . $name + . ', frab import: ' . ($from_frab ? 'Yes' : '') + . ', map_url: ' . $map_url + . ', description: ' . $description ); + + return $result; +} - return DB::getPdo()->lastInsertId(); +/** + * update a room + * + * @param string $name + * Name of the room + * @param boolean $from_frab + * Is this a frab imported room? + * @param string $map_url + * URL to a map tha can be displayed in an iframe + * @param + * description markdown description + */ +function Room_update($room_id, $name, $from_frab, $map_url, $description) +{ + $result = DB::update(' + UPDATE `Room` + SET + `Name`=?, + `from_frab`=?, + `map_url`=?, + `description`=? + WHERE `RID`=? + LIMIT 1', [ + $name, + (int) $from_frab, + $map_url, + $description, + $room_id + ]); + + engelsystem_log( + 'Room updated: ' . $name . + ', frab import: ' . ($from_frab ? 'Yes' : '') . + ', map_url: ' . $map_url . + ', description: ' . $description + ); + + return $result; } /** * Returns room by id. * - * @param int $room_id RID - * @param bool $onlyVisible + * @param int $room_id + * RID + * @param bool $onlyVisible * @return array|false */ function Room($room_id) @@ -71,7 +160,7 @@ function Room($room_id) return DB::selectOne(' SELECT * FROM `Room` - WHERE `RID` = ?', - [$room_id] - ); + WHERE `RID` = ?', [ + $room_id + ]); } -- cgit v1.2.3-54-g00ecf