summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
authormsquare <msquare@notrademark.de>2016-10-03 18:32:25 +0200
committermsquare <msquare@notrademark.de>2016-10-03 18:32:25 +0200
commitf3a0ce865deb9603b77adc9c9237a55cd4d87eeb (patch)
tree2fb5cc4658fd8ff9b379c2bb6227e8bf8c9a7f37 /includes
parent09c931dcf585da440879c52bb32b5eb9ef0fb9b4 (diff)
move sql queries from shifts controller to model
Diffstat (limited to 'includes')
-rw-r--r--includes/controller/shifts_controller.php70
-rw-r--r--includes/model/NeededAngelTypes_model.php52
-rw-r--r--includes/model/Room_model.php11
-rw-r--r--includes/pages/admin_rooms.php32
-rw-r--r--includes/sys_page.php18
5 files changed, 114 insertions, 69 deletions
diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php
index 0d36aa49..aa1ac5d9 100644
--- a/includes/controller/shifts_controller.php
+++ b/includes/controller/shifts_controller.php
@@ -31,51 +31,16 @@ function shift_edit_controller() {
}
$shift_id = $_REQUEST['edit_shift'];
- // Locations laden
- $rooms = sql_select("SELECT * FROM `Room` WHERE `show`='Y' ORDER BY `Name`");
- $room_array = [];
- foreach ($rooms as $room) {
- $room_array[$room['RID']] = $room['Name'];
- }
-
- $shift = sql_select("
- SELECT `ShiftTypes`.`name`, `Shifts`.*, `Room`.* FROM `Shifts`
- JOIN `Room` ON (`Shifts`.`RID` = `Room`.`RID`)
- JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
- WHERE `SID`='" . sql_escape($shift_id) . "'");
- if (count($shift) == 0) {
- redirect(page_link_to('user_shifts'));
- }
- $shift = $shift[0];
-
- // Engeltypen laden
- $types = sql_select("SELECT * FROM `AngelTypes` ORDER BY `name`");
- $angel_types = [];
- $needed_angel_types = [];
- foreach ($types as $type) {
- $angel_types[$type['id']] = $type;
- $needed_angel_types[$type['id']] = 0;
- }
-
- $shifttypes_source = ShiftTypes();
- $shifttypes = [];
- foreach ($shifttypes_source as $shifttype) {
- $shifttypes[$shifttype['id']] = $shifttype['name'];
- }
+ $shift = Shift($shift_id);
- // Benötigte Engeltypen vom Raum
- $needed_angel_types_source = sql_select("SELECT `AngelTypes`.*, `NeededAngelTypes`.`count` FROM `AngelTypes` LEFT JOIN `NeededAngelTypes` ON (`NeededAngelTypes`.`angel_type_id` = `AngelTypes`.`id` AND `NeededAngelTypes`.`room_id`='" . sql_escape($shift['RID']) . "') ORDER BY `AngelTypes`.`name`");
- foreach ($needed_angel_types_source as $type) {
- if ($type['count'] != "") {
- $needed_angel_types[$type['id']] = $type['count'];
- }
- }
+ $room = select_array(Rooms(), 'RID', 'Name');
+ $angeltypes = select_array(AngelTypes(), 'id', 'name');
+ $shifttypes = select_array(ShiftTypes(), 'id', 'name');
- // Benötigte Engeltypen von der Schicht
- $needed_angel_types_source = sql_select("SELECT `AngelTypes`.*, `NeededAngelTypes`.`count` FROM `AngelTypes` LEFT JOIN `NeededAngelTypes` ON (`NeededAngelTypes`.`angel_type_id` = `AngelTypes`.`id` AND `NeededAngelTypes`.`shift_id`='" . sql_escape($shift_id) . "') ORDER BY `AngelTypes`.`name`");
- foreach ($needed_angel_types_source as $type) {
- if ($type['count'] != "") {
- $needed_angel_types[$type['id']] = $type['count'];
+ $needed_angel_types = select_array(NeededAngelTypes_by_shift($shift_id), 'id', 'count');
+ foreach (array_keys($angeltypes) as $angeltype_id) {
+ if (! isset($needed_angel_types[$angeltype_id])) {
+ $needed_angel_types[$angeltype_id] = 0;
}
}
@@ -90,11 +55,10 @@ function shift_edit_controller() {
$title = strip_request_item('title');
// Auswahl der sichtbaren Locations für die Schichten
- if (isset($_REQUEST['rid']) && preg_match("/^[0-9]+$/", $_REQUEST['rid']) && isset($room_array[$_REQUEST['rid']])) {
+ if (isset($_REQUEST['rid']) && preg_match("/^[0-9]+$/", $_REQUEST['rid']) && isset($room[$_REQUEST['rid']])) {
$rid = $_REQUEST['rid'];
} else {
$valid = false;
- $rid = $rooms[0]['RID'];
$msg .= error(_("Please select a room."), true);
}
@@ -144,11 +108,11 @@ function shift_edit_controller() {
if ($result === false) {
engelsystem_error('Unable to update shift.');
}
- sql_query("DELETE FROM `NeededAngelTypes` WHERE `shift_id`='" . sql_escape($shift_id) . "'");
+ NeededAngelTypes_delete_by_shift($shift_id);
$needed_angel_types_info = [];
foreach ($needed_angel_types as $type_id => $count) {
- sql_query("INSERT INTO `NeededAngelTypes` SET `shift_id`='" . sql_escape($shift_id) . "', `angel_type_id`='" . sql_escape($type_id) . "', `count`='" . sql_escape($count) . "'");
- $needed_angel_types_info[] = $angel_types[$type_id]['name'] . ": " . $count;
+ NeededAngelType_add($shift_id, $type_id, null, $count);
+ $needed_angel_types_info[] = $angeltypes[$type_id] . ": " . $count;
}
engelsystem_log("Updated shift '" . $shifttypes[$shifttype_id] . ", " . $title . "' from " . date("Y-m-d H:i", $start) . " to " . date("Y-m-d H:i", $end) . " with angel types " . join(", ", $needed_angel_types_info));
@@ -160,9 +124,9 @@ function shift_edit_controller() {
}
}
- $angel_types = "";
- foreach ($types as $type) {
- $angel_types .= form_spinner('type_' . $type['id'], $type['name'], $needed_angel_types[$type['id']]);
+ $angel_types_spinner = "";
+ foreach ($angeltypes as $angeltype_id => $angeltype_name) {
+ $angel_types_spinner .= form_spinner('type_' . $angeltype_id, $angeltype_name, $needed_angel_types[$angeltype_id]);
}
return page_with_title(shifts_title(), [
@@ -171,11 +135,11 @@ function shift_edit_controller() {
form([
form_select('shifttype_id', _('Shifttype'), $shifttypes, $shifttype_id),
form_text('title', _("Title"), $title),
- form_select('rid', _("Room:"), $room_array, $rid),
+ form_select('rid', _("Room:"), $room, $rid),
form_text('start', _("Start:"), date("Y-m-d H:i", $start)),
form_text('end', _("End:"), date("Y-m-d H:i", $end)),
'<h2>' . _("Needed angels") . '</h2>',
- $angel_types,
+ $angel_types_spinner,
form_submit('submit', _("Save"))
])
]);
diff --git a/includes/model/NeededAngelTypes_model.php b/includes/model/NeededAngelTypes_model.php
index 96ceca83..77a23c3d 100644
--- a/includes/model/NeededAngelTypes_model.php
+++ b/includes/model/NeededAngelTypes_model.php
@@ -1,9 +1,59 @@
<?php
/**
+ * Entity needed angeltypes describes how many angels of given type are needed for a shift or in a room.
+ */
+
+/**
+ * Insert a new needed angel type.
+ *
+ * @param int $shift_id
+ * The shift. Can be null, but then a room_id must be given.
+ * @param int $angeltype_id
+ * The angeltype
+ * @param int $room_id
+ * The room. Can be null, but then a shift_id must be given.
+ * @param int $count
+ * How many angels are needed?
+ */
+function NeededAngelType_add($shift_id, $angeltype_id, $room_id, $count) {
+ $result = sql_query("
+ INSERT INTO `NeededAngelTypes` SET
+ `shift_id`=" . sql_null($shift_id) . ",
+ `angel_type_id`='" . sql_escape($angeltype_id) . "',
+ `room_id`=" . sql_null($room_id) . ",
+ `count`='" . sql_escape($count) . "'");
+ if ($result === false) {
+ return false;
+ }
+ return sql_id();
+}
+
+/**
+ * Deletes all needed angel types from given shift.
+ *
+ * @param int $shift_id
+ * id of the shift
+ */
+function NeededAngelTypes_delete_by_shift($shift_id) {
+ return sql_query("DELETE FROM `NeededAngelTypes` WHERE `shift_id`='" . sql_escape($shift_id) . "'");
+}
+
+/**
+ * Deletes all needed angel types from given room.
+ *
+ * @param int $room_id
+ * id of the room
+ */
+function NeededAngelTypes_delete_by_room($room_id) {
+ return sql_query("DELETE FROM `NeededAngelTypes` WHERE `room_id`='" . sql_escape($room_id) . "'");
+}
+
+/**
* Returns all needed angeltypes and already taken needs.
*
- * @param shiftID id of shift
+ * @param int $shiftID
+ * id of shift
*/
function NeededAngelTypes_by_shift($shiftId) {
$needed_angeltypes_source = sql_select("
diff --git a/includes/model/Room_model.php b/includes/model/Room_model.php
index 4d03260a..6b6e269e 100644
--- a/includes/model/Room_model.php
+++ b/includes/model/Room_model.php
@@ -1,8 +1,17 @@
<?php
/**
+ * returns a list of rooms.
+ * @param boolean $show_all returns also hidden rooms when true
+ */
+function Rooms($show_all = false) {
+ return sql_select("SELECT * FROM `Room`" . ($show_all ? "" : " WHERE `show`='Y'") . " ORDER BY `Name`");
+}
+
+/**
* Delete a room
- * @param int $room_id
+ *
+ * @param int $room_id
*/
function Room_delete($room_id) {
return sql_query("DELETE FROM `Room` WHERE `RID`=" . sql_escape($room_id));
diff --git a/includes/pages/admin_rooms.php b/includes/pages/admin_rooms.php
index d34d4386..7641eb18 100644
--- a/includes/pages/admin_rooms.php
+++ b/includes/pages/admin_rooms.php
@@ -36,20 +36,24 @@ function admin_rooms() {
}
if (test_request_int('id')) {
- $room = sql_select("SELECT * FROM `Room` WHERE `RID`='" . sql_escape($_REQUEST['id']) . "'");
- if (count($room) > 0) {
- $room_id = $_REQUEST['id'];
- $name = $room[0]['Name'];
- $from_pentabarf = $room[0]['FromPentabarf'];
- $public = $room[0]['show'];
- $number = $room[0]['Number'];
- $needed_angeltypes = sql_select("SELECT * FROM `NeededAngelTypes` WHERE `room_id`='" . sql_escape($room_id) . "'");
- foreach ($needed_angeltypes as $needed_angeltype) {
- $angeltypes_count[$needed_angeltype['angel_type_id']] = $needed_angeltype['count'];
- }
- } else {
+ $room = Room($_REQUEST['id']);
+ if ($room === false) {
+ engelsystem_error("Unable to load room.");
+ }
+ if ($room == null) {
redirect(page_link_to('admin_rooms'));
}
+
+ $room_id = $_REQUEST['id'];
+ $name = $room['Name'];
+ $from_pentabarf = $room['FromPentabarf'];
+ $public = $room['show'];
+ $number = $room['Number'];
+
+ $needed_angeltypes = sql_select("SELECT * FROM `NeededAngelTypes` WHERE `room_id`='" . sql_escape($room_id) . "'");
+ foreach ($needed_angeltypes as $needed_angeltype) {
+ $angeltypes_count[$needed_angeltype['angel_type_id']] = $needed_angeltype['count'];
+ }
}
if ($_REQUEST['show'] == 'edit') {
@@ -106,7 +110,7 @@ function admin_rooms() {
engelsystem_log("Room created: " . $name . ", pentabarf import: " . $from_pentabarf . ", public: " . $public . ", number: " . $number);
}
- sql_query("DELETE FROM `NeededAngelTypes` WHERE `room_id`='" . sql_escape($room_id) . "'");
+ NeededAngelTypes_delete_by_room($room_id);
$needed_angeltype_info = [];
foreach ($angeltypes_count as $angeltype_id => $angeltype_count) {
$angeltype = AngelType($angeltype_id);
@@ -114,7 +118,7 @@ function admin_rooms() {
engelsystem_error("Unable to load angeltype.");
}
if ($angeltype != null) {
- sql_query("INSERT INTO `NeededAngelTypes` SET `room_id`='" . sql_escape($room_id) . "', `angel_type_id`='" . sql_escape($angeltype_id) . "', `count`='" . sql_escape($angeltype_count) . "'");
+ NeededAngelType_add(null, $angeltype_id, $room_id, $count);
$needed_angeltype_info[] = $angeltype['name'] . ": " . $angeltype_count;
}
}
diff --git a/includes/sys_page.php b/includes/sys_page.php
index 27e3e8ba..e62909ab 100644
--- a/includes/sys_page.php
+++ b/includes/sys_page.php
@@ -19,6 +19,24 @@ function raw_output($output) {
}
/**
+ * Helper function for transforming list of entities into array for select boxes.
+ *
+ * @param array $data
+ * The data array
+ * @param string $key_name
+ * name of the column to use as id/key
+ * @param string $value_name
+ * name of the column to use as displayed value
+ */
+function select_array($data, $key_name, $value_name) {
+ $ret = [];
+ foreach ($data as $value) {
+ $ret[$value[$key_name]] = $value[$value_name];
+ }
+ return $ret;
+}
+
+/**
* Returns an int[] from given request param name.
*
* @param String $name