move room db queries to model

This commit is contained in:
msquare 2017-12-10 18:56:40 +01:00
parent aae8c77ed1
commit afb77d22ba
6 changed files with 142 additions and 73 deletions

View File

@ -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.
*

View File

@ -1,6 +1,30 @@
<?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.
@ -30,16 +54,34 @@ function Room_ids()
*/
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,22 +89,69 @@ 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
]);
$result = DB::getPdo()->lastInsertId();
engelsystem_log(
'Room created: ' . $name
. ', frab import: ' . ($from_frab ? 'Yes' : '')
. ', map_url: ' . $map_url
. ', description: ' . $description
);
return $result;
}
/**
* 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 DB::getPdo()->lastInsertId();
return $result;
}
/**
* Returns room by id.
*
* @param int $room_id RID
* @param int $room_id
* RID
* @param bool $onlyVisible
* @return array|false
*/
@ -71,7 +160,7 @@ function Room($room_id)
return DB::selectOne('
SELECT *
FROM `Room`
WHERE `RID` = ?',
[$room_id]
);
WHERE `RID` = ?', [
$room_id
]);
}

View File

@ -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

View File

@ -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;

View File

@ -1,7 +1,4 @@
<?php
use Engelsystem\Database\DB;
/**
* @return string
*/
@ -15,7 +12,7 @@ function admin_rooms_title()
*/
function admin_rooms()
{
$rooms_source = DB::select('SELECT * FROM `Room` ORDER BY `Name`');
$rooms_source = Rooms();
$rooms = [];
$request = request();
@ -40,7 +37,7 @@ function admin_rooms()
$description = null;
$room_id = 0;
$angeltypes_source = DB::select('SELECT `id`, `name` FROM `AngelTypes` ORDER BY `name`');
$angeltypes_source = AngelTypes();
$angeltypes = [];
$angeltypes_count = [];
foreach ($angeltypes_source as $angeltype) {
@ -60,10 +57,7 @@ function admin_rooms()
$map_url = $room['map_url'];
$description = $room['description'];
$needed_angeltypes = DB::select(
'SELECT `angel_type_id`, `count` FROM `NeededAngelTypes` WHERE `room_id`=?',
[$room_id]
);
$needed_angeltypes = NeededAngelTypes_by_room($room_id);
foreach ($needed_angeltypes as $needed_angeltype) {
$angeltypes_count[$needed_angeltype['angel_type_id']] = $needed_angeltype['count'];
}
@ -74,16 +68,12 @@ function admin_rooms()
$valid = true;
if ($request->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);

View File

@ -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'];