summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
authormsquare <msquare@notrademark.de>2017-12-10 18:56:40 +0100
committermsquare <msquare@notrademark.de>2017-12-10 18:56:40 +0100
commitafb77d22ba35dfbee74bbbee95626d55edef8898 (patch)
treed8d35dc5a7848276f83bcab3d1a792e7fb2e1b66 /includes
parentaae8c77ed1a7b0d323f1d32c71b5fd12ff15d4bf (diff)
move room db queries to model
Diffstat (limited to 'includes')
-rw-r--r--includes/model/NeededAngelTypes_model.php13
-rw-r--r--includes/model/Room_model.php129
-rw-r--r--includes/model/Shifts_model.php6
-rw-r--r--includes/pages/admin_import.php5
-rw-r--r--includes/pages/admin_rooms.php58
-rw-r--r--includes/pages/admin_shifts.php4
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
@@ -55,6 +55,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.
*
* @param int $shiftId id of shift
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 @@
<?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.
@@ -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 @@
<?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);
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'];