From 6c8a11338391dda651b355ea3747ad82603901e4 Mon Sep 17 00:00:00 2001 From: msquare Date: Mon, 3 Oct 2016 17:41:14 +0200 Subject: introduce ShiftsFilter --- includes/controller/shift_entries_controller.php | 161 +++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 includes/controller/shift_entries_controller.php (limited to 'includes/controller/shift_entries_controller.php') diff --git a/includes/controller/shift_entries_controller.php b/includes/controller/shift_entries_controller.php new file mode 100644 index 00000000..a6121e75 --- /dev/null +++ b/includes/controller/shift_entries_controller.php @@ -0,0 +1,161 @@ + 0) { + $selected_type_id = $_REQUEST['angeltype_id']; + } + } else { + $user_id = $user['UID']; + } + + if (sql_num_query("SELECT * FROM `ShiftEntry` WHERE `SID`='" . sql_escape($shift['SID']) . "' AND `UID` = '" . sql_escape($user_id) . "'")) { + return error("This angel does already have an entry for this shift.", true); + } + + $freeloaded = $shift['freeloaded']; + $freeload_comment = $shift['freeload_comment']; + if (in_array("user_shifts_admin", $privileges)) { + $freeloaded = isset($_REQUEST['freeloaded']); + $freeload_comment = strip_request_item_nl('freeload_comment'); + } + + $comment = strip_request_item_nl('comment'); + $result = ShiftEntry_create([ + 'SID' => $shift_id, + 'TID' => $selected_type_id, + 'UID' => $user_id, + 'Comment' => $comment, + 'freeloaded' => $freeloaded, + 'freeload_comment' => $freeload_comment + ]); + if ($result === false) { + engelsystem_error('Unable to create shift entry.'); + } + + if ($type['restricted'] == 0 && sql_num_query("SELECT * FROM `UserAngelTypes` INNER JOIN `AngelTypes` ON `AngelTypes`.`id` = `UserAngelTypes`.`angeltype_id` WHERE `angeltype_id` = '" . sql_escape($selected_type_id) . "' AND `user_id` = '" . sql_escape($user_id) . "' ") == 0) { + sql_query("INSERT INTO `UserAngelTypes` (`user_id`, `angeltype_id`) VALUES ('" . sql_escape($user_id) . "', '" . sql_escape($selected_type_id) . "')"); + } + + $user_source = User($user_id); + engelsystem_log("User " . User_Nick_render($user_source) . " signed up for shift " . $shift['name'] . " from " . date("Y-m-d H:i", $shift['start']) . " to " . date("Y-m-d H:i", $shift['end'])); + success(_("You are subscribed. Thank you!") . ' ' . _("My shifts") . ' »'); + redirect(shift_link($shift)); + } + + if (in_array('user_shifts_admin', $privileges)) { + $users = sql_select("SELECT *, (SELECT count(*) FROM `ShiftEntry` WHERE `freeloaded`=1 AND `ShiftEntry`.`UID`=`User`.`UID`) AS `freeloaded` FROM `User` ORDER BY `Nick`"); + $users_select = []; + + foreach ($users as $usr) { + $users_select[$usr['UID']] = $usr['Nick'] . ($usr['freeloaded'] == 0 ? "" : " (" . _("Freeloader") . ")"); + } + $user_text = html_select_key('user_id', 'user_id', $users_select, $user['UID']); + + $angeltypes_source = sql_select("SELECT * FROM `AngelTypes` ORDER BY `name`"); + $angeltypes = []; + foreach ($angeltypes_source as $angeltype) { + $angeltypes[$angeltype['id']] = $angeltype['name']; + } + $angeltype_select = html_select_key('angeltype_id', 'angeltype_id', $angeltypes, $type['id']); + } else { + $user_text = User_Nick_render($user); + $angeltype_select = $type['name']; + } + + return ShiftEntry_edit_view($user_text, date("Y-m-d H:i", $shift['start']) . ' – ' . date('Y-m-d H:i', $shift['end']) . ' (' . shift_length($shift) . ')', $shift['Name'], $shift['name'], $angeltype_select, "", false, null, in_array('user_shifts_admin', $privileges)); +} + +/** + * Remove somebody from a shift. + */ +function shift_entry_delete_controller() { + global $privileges; + + if (! in_array('user_shifts_admin', $privileges)) { + redirect(page_link_to('user_shifts')); + } + + if (! isset($_REQUEST['entry_id']) || ! test_request_int('entry_id')) { + redirect(page_link_to('user_shifts')); + } + $entry_id = $_REQUEST['entry_id']; + + $shift_entry_source = sql_select(" + SELECT `User`.`Nick`, `ShiftEntry`.`Comment`, `ShiftEntry`.`UID`, `ShiftTypes`.`name`, `Shifts`.*, `Room`.`Name`, `AngelTypes`.`name` as `angel_type` + FROM `ShiftEntry` + JOIN `User` ON (`User`.`UID`=`ShiftEntry`.`UID`) + JOIN `AngelTypes` ON (`ShiftEntry`.`TID` = `AngelTypes`.`id`) + JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`) + JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`) + JOIN `Room` ON (`Shifts`.`RID` = `Room`.`RID`) + WHERE `ShiftEntry`.`id`='" . sql_escape($entry_id) . "'"); + if (count($shift_entry_source) > 0) { + $shift_entry_source = $shift_entry_source[0]; + + $result = ShiftEntry_delete($entry_id); + if ($result === false) { + engelsystem_error('Unable to delete shift entry.'); + } + + engelsystem_log("Deleted " . User_Nick_render($shift_entry_source) . "'s shift: " . $shift_entry_source['name'] . " at " . $shift_entry_source['Name'] . " from " . date("Y-m-d H:i", $shift_entry_source['start']) . " to " . date("Y-m-d H:i", $shift_entry_source['end']) . " as " . $shift_entry_source['angel_type']); + success(_("Shift entry deleted.")); + } else { + error(_("Entry not found.")); + } + redirect(page_link_to('user_shifts')); +} + +?> \ No newline at end of file -- cgit v1.2.3-54-g00ecf From 09c931dcf585da440879c52bb32b5eb9ef0fb9b4 Mon Sep 17 00:00:00 2001 From: msquare Date: Mon, 3 Oct 2016 17:55:49 +0200 Subject: fix missing variables --- includes/controller/shift_entries_controller.php | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'includes/controller/shift_entries_controller.php') diff --git a/includes/controller/shift_entries_controller.php b/includes/controller/shift_entries_controller.php index a6121e75..d704c299 100644 --- a/includes/controller/shift_entries_controller.php +++ b/includes/controller/shift_entries_controller.php @@ -4,12 +4,21 @@ * Sign up for a shift. */ function shift_entry_add_controller() { + global $privileges, $user; + if (isset($_REQUEST['shift_id']) && preg_match("/^[0-9]*$/", $_REQUEST['shift_id'])) { $shift_id = $_REQUEST['shift_id']; } else { redirect(page_link_to('user_shifts')); } + // 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 = Shift($shift_id); $shift['Name'] = $room_array[$shift['RID']]; if ($shift === false) { -- cgit v1.2.3-54-g00ecf From 27c9650dc4064ec5b40c5041d8291a7ce0e5305e Mon Sep 17 00:00:00 2001 From: msquare Date: Tue, 4 Oct 2016 21:20:38 +0200 Subject: reduce complexity of shifts controller --- includes/controller/angeltypes_controller.php | 6 --- includes/controller/shift_entries_controller.php | 3 -- includes/controller/shifts_controller.php | 28 +----------- includes/controller/shifttypes_controller.php | 3 -- includes/model/AngelType_model.php | 12 +++++- includes/model/ShiftTypes_model.php | 2 +- includes/model/Shifts_model.php | 55 +++++++++++++----------- 7 files changed, 44 insertions(+), 65 deletions(-) (limited to 'includes/controller/shift_entries_controller.php') diff --git a/includes/controller/angeltypes_controller.php b/includes/controller/angeltypes_controller.php index cd938893..ea451481 100644 --- a/includes/controller/angeltypes_controller.php +++ b/includes/controller/angeltypes_controller.php @@ -50,9 +50,6 @@ function angeltypes_about_controller() { } else { $angeltypes = AngelTypes(); } - if ($angeltypes === false) { - engelsystem_error("Unable to load angeltypes."); - } return [ _("Teams/Job description"), @@ -223,9 +220,6 @@ function angeltypes_list_controller() { } $angeltypes = AngelTypes_with_user($user); - if ($angeltypes === false) { - engelsystem_error("Unable to load angeltypes."); - } foreach ($angeltypes as &$angeltype) { $actions = [ diff --git a/includes/controller/shift_entries_controller.php b/includes/controller/shift_entries_controller.php index d704c299..9e252a4f 100644 --- a/includes/controller/shift_entries_controller.php +++ b/includes/controller/shift_entries_controller.php @@ -21,9 +21,6 @@ function shift_entry_add_controller() { $shift = Shift($shift_id); $shift['Name'] = $room_array[$shift['RID']]; - if ($shift === false) { - engelsystem_error('Unable to load shift.'); - } if ($shift == null) { redirect(page_link_to('user_shifts')); } diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php index ca6fd906..be0cf127 100644 --- a/includes/controller/shifts_controller.php +++ b/includes/controller/shifts_controller.php @@ -159,19 +159,13 @@ function shift_delete_controller() { $shift_id = $_REQUEST['delete_shift']; $shift = Shift($shift_id); - if ($shift === false) { - engelsystem_error('Unable to load shift.'); - } if ($shift == null) { redirect(page_link_to('user_shifts')); } // Schicht löschen bestätigt if (isset($_REQUEST['delete'])) { - $result = Shift_delete($shift_id); - if ($result === false) { - engelsystem_error('Unable to delete shift.'); - } + Shift_delete($shift_id); engelsystem_log("Deleted shift " . $shift['name'] . " from " . date("Y-m-d H:i", $shift['start']) . " to " . date("Y-m-d H:i", $shift['end'])); success(_("Shift deleted.")); @@ -196,33 +190,15 @@ function shift_controller() { } $shift = Shift($_REQUEST['shift_id']); - if ($shift === false) { - engelsystem_error('Unable to load shift.'); - } if ($shift == null) { - error(_('Shift could not be found.')); + error(_("Shift could not be found.")); redirect(page_link_to('user_shifts')); } $shifttype = ShiftType($shift['shifttype_id']); - if ($shifttype === false || $shifttype == null) { - engelsystem_error('Unable to load shift type.'); - } - $room = Room($shift['RID']); - if ($room === false || $room == null) { - engelsystem_error('Unable to load room.'); - } - $angeltypes = AngelTypes(); - if ($angeltypes === false) { - engelsystem_error('Unable to load angeltypes.'); - } - $user_shifts = Shifts_by_user($user); - if ($user_shifts === false) { - engelsystem_error('Unable to load users shifts.'); - } $signed_up = false; foreach ($user_shifts as $user_shift) { diff --git a/includes/controller/shifttypes_controller.php b/includes/controller/shifttypes_controller.php index 5e547895..456e39ae 100644 --- a/includes/controller/shifttypes_controller.php +++ b/includes/controller/shifttypes_controller.php @@ -48,9 +48,6 @@ function shifttype_edit_controller() { $description = ""; $angeltypes = AngelTypes(); - if ($angeltypes === false) { - engelsystem_error("Unable to load angel types."); - } if (isset($_REQUEST['shifttype_id'])) { $shifttype = ShiftType($_REQUEST['shifttype_id']); diff --git a/includes/model/AngelType_model.php b/includes/model/AngelType_model.php index a87dd461..d01cf905 100644 --- a/includes/model/AngelType_model.php +++ b/includes/model/AngelType_model.php @@ -91,7 +91,7 @@ function AngelType_validate_name($name, $angeltype) { * @param User $user */ function AngelTypes_with_user($user) { - return sql_select(" + $result = sql_select(" SELECT `AngelTypes`.*, `UserAngelTypes`.`id` as `user_angeltype_id`, `UserAngelTypes`.`confirm_user_id`, @@ -100,16 +100,24 @@ function AngelTypes_with_user($user) { LEFT JOIN `UserAngelTypes` ON `AngelTypes`.`id`=`UserAngelTypes`.`angeltype_id` AND `UserAngelTypes`.`user_id`=" . $user['UID'] . " ORDER BY `name`"); + if ($result === false) { + engelsystem_error("Unable to load angeltypes."); + } + return $result; } /** * Returns all angeltypes. */ function AngelTypes() { - return sql_select(" + $result = sql_select(" SELECT * FROM `AngelTypes` ORDER BY `name`"); + if ($result === false) { + engelsystem_error("Unable to load angeltypes."); + } + return $result; } /** diff --git a/includes/model/ShiftTypes_model.php b/includes/model/ShiftTypes_model.php index a9cf77bd..89704a65 100644 --- a/includes/model/ShiftTypes_model.php +++ b/includes/model/ShiftTypes_model.php @@ -51,7 +51,7 @@ function ShiftType_create($name, $angeltype_id, $description) { function ShiftType($shifttype_id) { $shifttype = sql_select("SELECT * FROM `ShiftTypes` WHERE `id`='" . sql_escape($shifttype_id) . "'"); if ($shifttype === false) { - return false; + engelsystem_error('Unable to load shift type.'); } if ($shifttype == null) { return null; diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index 52ced0fe..8c251209 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -93,9 +93,6 @@ function Shift_signup_allowed($shift, $angeltype, $user_angeltype = null, $user_ if ($user_shifts == null) { $user_shifts = Shifts_by_user($user); - if ($user_shifts === false) { - engelsystem_error('Unable to load users shifts.'); - } } $collides = Shift_collides($shift, $user_shifts); @@ -166,7 +163,11 @@ function Shift_delete_by_psid($shift_psid) { function Shift_delete($shift_id) { mail_shift_delete(Shift($shift_id)); - return sql_query("DELETE FROM `Shifts` WHERE `SID`='" . sql_escape($shift_id) . "'"); + $result = sql_query("DELETE FROM `Shifts` WHERE `SID`='" . sql_escape($shift_id) . "'"); + if ($result === false) { + engelsystem_error('Unable to delete shift.'); + } + return $result; } /** @@ -232,7 +233,7 @@ function Shift_create($shift) { * Return users shifts. */ function Shifts_by_user($user) { - return sql_select(" + $result = sql_select(" SELECT `ShiftTypes`.`id` as `shifttype_id`, `ShiftTypes`.`name`, `ShiftEntry`.*, `Shifts`.*, `Room`.* FROM `ShiftEntry` JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`) @@ -241,6 +242,10 @@ function Shifts_by_user($user) { WHERE `UID`='" . sql_escape($user['UID']) . "' ORDER BY `start` "); + if ($result === false) { + engelsystem_error('Unable to load users shifts.'); + } + return $result; } /** @@ -304,27 +309,29 @@ function Shift($shift_id) { $shiftsEntry_source = sql_select("SELECT `id`, `TID` , `UID` , `freeloaded` FROM `ShiftEntry` WHERE `SID`='" . sql_escape($shift_id) . "'"); if ($shifts_source === false) { - return false; + engelsystem_error('Unable to load shift.'); } - if (count($shifts_source) > 0) { - $result = $shifts_source[0]; - - $result['ShiftEntry'] = $shiftsEntry_source; - $result['NeedAngels'] = []; - - $temp = NeededAngelTypes_by_shift($shift_id); - foreach ($temp as $e) { - $result['NeedAngels'][] = [ - 'TID' => $e['angel_type_id'], - 'count' => $e['count'], - 'restricted' => $e['restricted'], - 'taken' => $e['taken'] - ]; - } - - return $result; + + if (empty($shifts_source)) { + return null; } - return null; + + $result = $shifts_source[0]; + + $result['ShiftEntry'] = $shiftsEntry_source; + $result['NeedAngels'] = []; + + $temp = NeededAngelTypes_by_shift($shift_id); + foreach ($temp as $e) { + $result['NeedAngels'][] = [ + 'TID' => $e['angel_type_id'], + 'count' => $e['count'], + 'restricted' => $e['restricted'], + 'taken' => $e['taken'] + ]; + } + + return $result; } /** -- cgit v1.2.3-54-g00ecf