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. * Returns all needed angeltypes and already taken needs.
* *

View File

@ -1,6 +1,30 @@
<?php <?php
use Engelsystem\Database\DB; 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. * returns a list of rooms.
@ -26,20 +50,38 @@ function Room_ids()
/** /**
* Delete a room * Delete a room
* *
* @param int $room_id * @param int $room_id
*/ */
function Room_delete($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 * Create a new room
* *
* @param string $name Name of the room * @param string $name
* @param boolean $from_frab Is this a frab imported room? * Name of the room
* @param string $map_url URL to a map tha can be displayed in an iframe * @param boolean $from_frab
* @param description markdown description * 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 * @return false|int
*/ */
function Room_create($name, $from_frab, $map_url, $description) function Room_create($name, $from_frab, $map_url, $description)
@ -47,23 +89,70 @@ function Room_create($name, $from_frab, $map_url, $description)
DB::insert(' DB::insert('
INSERT INTO `Room` (`Name`, `from_frab`, `map_url`, `description`) INSERT INTO `Room` (`Name`, `from_frab`, `map_url`, `description`)
VALUES (?, ?, ?, ?) VALUES (?, ?, ?, ?)
', ', [
[ $name,
$name, (int) $from_frab,
(int) $from_frab, $map_url,
$map_url, $description
$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. * Returns room by id.
* *
* @param int $room_id RID * @param int $room_id
* @param bool $onlyVisible * RID
* @param bool $onlyVisible
* @return array|false * @return array|false
*/ */
function Room($room_id) function Room($room_id)
@ -71,7 +160,7 @@ function Room($room_id)
return DB::selectOne(' return DB::selectOne('
SELECT * SELECT *
FROM `Room` FROM `Room`
WHERE `RID` = ?', WHERE `RID` = ?', [
[$room_id] $room_id
); ]);
} }

View File

@ -26,6 +26,12 @@ function Shifts_by_angeltype($angeltype) {
', [$angeltype['id'], $angeltype['id']]); ', [$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 * @param array $room

View File

@ -253,11 +253,10 @@ function admin_import()
list($rooms_new, $rooms_deleted) = prepare_rooms($import_file); list($rooms_new, $rooms_deleted) = prepare_rooms($import_file);
foreach ($rooms_new as $room) { foreach ($rooms_new as $room) {
$result = Room_create($room, true, null, null); $result = Room_create($room, true, null, null);
$rooms_import[trim($room)] = $result; $rooms_import[trim($room)] = $result;
} }
foreach ($rooms_deleted as $room) { 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( 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 = []; $shifts_db = [];
foreach ($shifts as $shift) { foreach ($shifts as $shift) {
$shifts_db[$shift['PSID']] = $shift; $shifts_db[$shift['PSID']] = $shift;

View File

@ -1,7 +1,4 @@
<?php <?php
use Engelsystem\Database\DB;
/** /**
* @return string * @return string
*/ */
@ -15,7 +12,7 @@ function admin_rooms_title()
*/ */
function admin_rooms() function admin_rooms()
{ {
$rooms_source = DB::select('SELECT * FROM `Room` ORDER BY `Name`'); $rooms_source = Rooms();
$rooms = []; $rooms = [];
$request = request(); $request = request();
@ -40,7 +37,7 @@ function admin_rooms()
$description = null; $description = null;
$room_id = 0; $room_id = 0;
$angeltypes_source = DB::select('SELECT `id`, `name` FROM `AngelTypes` ORDER BY `name`'); $angeltypes_source = AngelTypes();
$angeltypes = []; $angeltypes = [];
$angeltypes_count = []; $angeltypes_count = [];
foreach ($angeltypes_source as $angeltype) { foreach ($angeltypes_source as $angeltype) {
@ -60,10 +57,7 @@ function admin_rooms()
$map_url = $room['map_url']; $map_url = $room['map_url'];
$description = $room['description']; $description = $room['description'];
$needed_angeltypes = DB::select( $needed_angeltypes = NeededAngelTypes_by_room($room_id);
'SELECT `angel_type_id`, `count` FROM `NeededAngelTypes` WHERE `room_id`=?',
[$room_id]
);
foreach ($needed_angeltypes as $needed_angeltype) { foreach ($needed_angeltypes as $needed_angeltype) {
$angeltypes_count[$needed_angeltype['angel_type_id']] = $needed_angeltype['count']; $angeltypes_count[$needed_angeltype['angel_type_id']] = $needed_angeltype['count'];
} }
@ -74,16 +68,12 @@ function admin_rooms()
$valid = true; $valid = true;
if ($request->has('name') && strlen(strip_request_item('name')) > 0) { if ($request->has('name') && strlen(strip_request_item('name')) > 0) {
$name = strip_request_item('name'); $result = Room_validate_name(strip_request_item('name'), $room_id);
if ( if(!$result->isValid()) {
isset($room)
&& count(DB::select(
'SELECT RID FROM `Room` WHERE `Name`=? AND NOT `RID`=?',
[$name, $room_id]
)) > 0
) {
$valid = false; $valid = false;
$msg .= error(_('This name is already in use.'), true); $msg .= error(_('This name is already in use.'), true);
} else {
$name = $result->getValue();
} }
} else { } else {
$valid = false; $valid = false;
@ -116,38 +106,10 @@ function admin_rooms()
} }
if ($valid) { if ($valid) {
if (!empty($room_id)) { 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 {
$room_id = Room_create($name, $from_frab, $map_url, $description); $room_id = Room_create($name, $from_frab, $map_url, $description);
} else {
engelsystem_log( Room_update($room_id, $name, $from_frab, $map_url, $description);
'Room created: ' . $name
. ', frab import: ' . ($from_frab ? 'Yes' : '')
. ', map_url: ' . $map_url
. ', description: ' . $description
);
} }
NeededAngelTypes_delete_by_room($room_id); NeededAngelTypes_delete_by_room($room_id);

View File

@ -29,8 +29,8 @@ function admin_shifts()
$title = ''; $title = '';
$shifttype_id = null; $shifttype_id = null;
// Locations laden (auch unsichtbare - fuer Erzengel ist das ok) // Locations laden
$rooms = DB::select('SELECT `RID`, `Name` FROM `Room` ORDER BY `Name`'); $rooms = Rooms();
$room_array = []; $room_array = [];
foreach ($rooms as $room) { foreach ($rooms as $room) {
$room_array[$room['RID']] = $room['Name']; $room_array[$room['RID']] = $room['Name'];