From afb77d22ba35dfbee74bbbee95626d55edef8898 Mon Sep 17 00:00:00 2001 From: msquare Date: Sun, 10 Dec 2017 18:56:40 +0100 Subject: [PATCH] move room db queries to model --- includes/model/NeededAngelTypes_model.php | 13 +++ includes/model/Room_model.php | 129 ++++++++++++++++++---- includes/model/Shifts_model.php | 6 + includes/pages/admin_import.php | 5 +- includes/pages/admin_rooms.php | 58 ++-------- includes/pages/admin_shifts.php | 4 +- 6 files changed, 142 insertions(+), 73 deletions(-) diff --git a/includes/model/NeededAngelTypes_model.php b/includes/model/NeededAngelTypes_model.php index d8de5e69..53313fe8 100644 --- a/includes/model/NeededAngelTypes_model.php +++ b/includes/model/NeededAngelTypes_model.php @@ -54,6 +54,19 @@ function NeededAngelTypes_delete_by_room($room_id) ); } +/** + * Returns all needed angeltypes by room. + * + * @param int $room_id + * @return array + */ +function NeededAngelTypes_by_room($room_id) { + return DB::select( + 'SELECT `angel_type_id`, `count` FROM `NeededAngelTypes` WHERE `room_id`=?', + [$room_id] + ); +} + /** * Returns all needed angeltypes and already taken needs. * 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 + ]); } diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index 8a1b69f9..1fc7fe89 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -26,6 +26,12 @@ function Shifts_by_angeltype($angeltype) { ', [$angeltype['id'], $angeltype['id']]); } +/** + * Returns all shifts with a PSID (from frab import) + */ +function Shifts_from_frab() { + return DB::select('SELECT * FROM `Shifts` WHERE `PSID` IS NOT NULL ORDER BY `start`'); +} /** * @param array $room diff --git a/includes/pages/admin_import.php b/includes/pages/admin_import.php index ccc62b9b..9883c0d5 100644 --- a/includes/pages/admin_import.php +++ b/includes/pages/admin_import.php @@ -253,11 +253,10 @@ function admin_import() list($rooms_new, $rooms_deleted) = prepare_rooms($import_file); foreach ($rooms_new as $room) { $result = Room_create($room, true, null, null); - $rooms_import[trim($room)] = $result; } foreach ($rooms_deleted as $room) { - DB::delete('DELETE FROM `Room` WHERE `Name`=? LIMIT 1', [$room]); + Room_delete_by_name($room); } list($events_new, $events_updated, $events_deleted) = prepare_events( @@ -378,7 +377,7 @@ function prepare_events($file, $shifttype_id, $add_minutes_start, $add_minutes_e ]; } - $shifts = DB::select('SELECT * FROM `Shifts` WHERE `PSID` IS NOT NULL ORDER BY `start`'); + $shifts = Shifts_from_frab(); $shifts_db = []; foreach ($shifts as $shift) { $shifts_db[$shift['PSID']] = $shift; diff --git a/includes/pages/admin_rooms.php b/includes/pages/admin_rooms.php index c5b7b610..55b52cd9 100644 --- a/includes/pages/admin_rooms.php +++ b/includes/pages/admin_rooms.php @@ -1,7 +1,4 @@ has('name') && strlen(strip_request_item('name')) > 0) { - $name = strip_request_item('name'); - if ( - isset($room) - && count(DB::select( - 'SELECT RID FROM `Room` WHERE `Name`=? AND NOT `RID`=?', - [$name, $room_id] - )) > 0 - ) { + $result = Room_validate_name(strip_request_item('name'), $room_id); + if(!$result->isValid()) { $valid = false; $msg .= error(_('This name is already in use.'), true); + } else { + $name = $result->getValue(); } } else { $valid = false; @@ -116,38 +106,10 @@ function admin_rooms() } if ($valid) { - if (!empty($room_id)) { - 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 - ); - } else { + if (empty($room_id)) { $room_id = Room_create($name, $from_frab, $map_url, $description); - - engelsystem_log( - 'Room created: ' . $name - . ', frab import: ' . ($from_frab ? 'Yes' : '') - . ', map_url: ' . $map_url - . ', description: ' . $description - ); + } else { + Room_update($room_id, $name, $from_frab, $map_url, $description); } NeededAngelTypes_delete_by_room($room_id); diff --git a/includes/pages/admin_shifts.php b/includes/pages/admin_shifts.php index 721d0f4f..223ed074 100644 --- a/includes/pages/admin_shifts.php +++ b/includes/pages/admin_shifts.php @@ -29,8 +29,8 @@ function admin_shifts() $title = ''; $shifttype_id = null; - // Locations laden (auch unsichtbare - fuer Erzengel ist das ok) - $rooms = DB::select('SELECT `RID`, `Name` FROM `Room` ORDER BY `Name`'); + // Locations laden + $rooms = Rooms(); $room_array = []; foreach ($rooms as $room) { $room_array[$room['RID']] = $room['Name'];