From 6ff5e7997a2e70178513824d8a1d1eca14622e46 Mon Sep 17 00:00:00 2001 From: msquare Date: Sun, 2 Oct 2016 21:19:03 +0200 Subject: split user_shifts into different functions --- includes/controller/shifts_controller.php | 208 +++++++++++++++++ includes/engelsystem_provider.php | 2 + includes/pages/user_shifts.php | 357 ++---------------------------- 3 files changed, 225 insertions(+), 342 deletions(-) diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php index b29a819f..0d36aa49 100644 --- a/includes/controller/shifts_controller.php +++ b/includes/controller/shifts_controller.php @@ -12,6 +12,214 @@ function shift_edit_link($shift) { return page_link_to('user_shifts') . '&edit_shift=' . $shift['SID']; } +/** + * Edit a single shift. + */ +function shift_edit_controller() { + global $privileges; + + // Schicht bearbeiten + $msg = ""; + $valid = true; + + if (! in_array('admin_shifts', $privileges)) { + redirect(page_link_to('user_shifts')); + } + + if (! isset($_REQUEST['edit_shift']) || ! test_request_int('edit_shift')) { + redirect(page_link_to('user_shifts')); + } + $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']; + } + + // 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']; + } + } + + // 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']; + } + } + + $shifttype_id = $shift['shifttype_id']; + $title = $shift['title']; + $rid = $shift['RID']; + $start = $shift['start']; + $end = $shift['end']; + + if (isset($_REQUEST['submit'])) { + // Name/Bezeichnung der Schicht, darf leer sein + $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']])) { + $rid = $_REQUEST['rid']; + } else { + $valid = false; + $rid = $rooms[0]['RID']; + $msg .= error(_("Please select a room."), true); + } + + if (isset($_REQUEST['shifttype_id']) && isset($shifttypes[$_REQUEST['shifttype_id']])) { + $shifttype_id = $_REQUEST['shifttype_id']; + } else { + $valid = false; + $msg .= error(_('Please select a shifttype.'), true); + } + + if (isset($_REQUEST['start']) && $tmp = DateTime::createFromFormat("Y-m-d H:i", trim($_REQUEST['start']))) { + $start = $tmp->getTimestamp(); + } else { + $valid = false; + $msg .= error(_("Please enter a valid starting time for the shifts."), true); + } + + if (isset($_REQUEST['end']) && $tmp = DateTime::createFromFormat("Y-m-d H:i", trim($_REQUEST['end']))) { + $end = $tmp->getTimestamp(); + } else { + $valid = false; + $msg .= error(_("Please enter a valid ending time for the shifts."), true); + } + + if ($start >= $end) { + $valid = false; + $msg .= error(_("The ending time has to be after the starting time."), true); + } + + foreach ($needed_angel_types_source as $type) { + if (isset($_REQUEST['type_' . $type['id']]) && preg_match("/^[0-9]+$/", trim($_REQUEST['type_' . $type['id']]))) { + $needed_angel_types[$type['id']] = trim($_REQUEST['type_' . $type['id']]); + } else { + $valid = false; + $msg .= error(sprintf(_("Please check your input for needed angels of type %s."), $type['name']), true); + } + } + + if ($valid) { + $shift['shifttype_id'] = $shifttype_id; + $shift['title'] = $title; + $shift['RID'] = $rid; + $shift['start'] = $start; + $shift['end'] = $end; + + $result = Shift_update($shift); + if ($result === false) { + engelsystem_error('Unable to update shift.'); + } + sql_query("DELETE FROM `NeededAngelTypes` WHERE `shift_id`='" . sql_escape($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; + } + + 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)); + success(_("Shift updated.")); + + redirect(shift_link([ + 'SID' => $shift_id + ])); + } + } + + $angel_types = ""; + foreach ($types as $type) { + $angel_types .= form_spinner('type_' . $type['id'], $type['name'], $needed_angel_types[$type['id']]); + } + + return page_with_title(shifts_title(), [ + msg(), + '', + form([ + form_select('shifttype_id', _('Shifttype'), $shifttypes, $shifttype_id), + form_text('title', _("Title"), $title), + form_select('rid', _("Room:"), $room_array, $rid), + form_text('start', _("Start:"), date("Y-m-d H:i", $start)), + form_text('end', _("End:"), date("Y-m-d H:i", $end)), + '

' . _("Needed angels") . '

', + $angel_types, + form_submit('submit', _("Save")) + ]) + ]); +} + +function shift_delete_controller() { + global $privileges; + + if (! in_array('user_shifts_admin', $privileges)) { + redirect(page_link_to('user_shifts')); + } + + // Schicht komplett löschen (nur für admins/user mit user_shifts_admin privileg) + if (! isset($_REQUEST['delete_shift']) || ! preg_match("/^[0-9]*$/", $_REQUEST['delete_shift'])) { + redirect(page_link_to('user_shifts')); + } + $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.'); + } + + 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.")); + redirect(page_link_to('user_shifts')); + } + + return page_with_title(shifts_title(), [ + error(sprintf(_("Do you want to delete the shift %s from %s to %s?"), $shift['name'], date("Y-m-d H:i", $shift['start']), date("H:i", $shift['end'])), true), + '' . _("delete") . '' + ]); +} + function shift_controller() { global $user, $privileges; diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php index 84a73275..979102e0 100644 --- a/includes/engelsystem_provider.php +++ b/includes/engelsystem_provider.php @@ -28,6 +28,7 @@ require_once realpath(__DIR__ . '/../includes/view/AngelTypes_view.php'); require_once realpath(__DIR__ . '/../includes/view/EventConfig_view.php'); require_once realpath(__DIR__ . '/../includes/view/Questions_view.php'); require_once realpath(__DIR__ . '/../includes/view/Rooms_view.php'); +require_once realpath(__DIR__ . '/../includes/view/ShiftCalendarRenderer.php'); require_once realpath(__DIR__ . '/../includes/view/Shifts_view.php'); require_once realpath(__DIR__ . '/../includes/view/ShiftEntry_view.php'); require_once realpath(__DIR__ . '/../includes/view/ShiftTypes_view.php'); @@ -38,6 +39,7 @@ require_once realpath(__DIR__ . '/../includes/view/User_view.php'); require_once realpath(__DIR__ . '/../includes/controller/angeltypes_controller.php'); require_once realpath(__DIR__ . '/../includes/controller/event_config_controller.php'); require_once realpath(__DIR__ . '/../includes/controller/rooms_controller.php'); +require_once realpath(__DIR__ . '/../includes/controller/shift_entries_controller.php'); require_once realpath(__DIR__ . '/../includes/controller/shifts_controller.php'); require_once realpath(__DIR__ . '/../includes/controller/shifttypes_controller.php'); require_once realpath(__DIR__ . '/../includes/controller/users_controller.php'); diff --git a/includes/pages/user_shifts.php b/includes/pages/user_shifts.php index 9ad532ca..77ae8582 100644 --- a/includes/pages/user_shifts.php +++ b/includes/pages/user_shifts.php @@ -4,6 +4,9 @@ function shifts_title() { return _("Shifts"); } +/** + * Start different controllers for deleting shifts and shift_entries, edit shifts and add shift entries. + */ function user_shifts() { global $user, $privileges; @@ -11,345 +14,17 @@ function user_shifts() { redirect(page_link_to('user_myshifts')); } - // 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']; - } - // Löschen einzelner Schicht-Einträge (Also Belegung einer Schicht von Engeln) durch Admins - if (isset($_REQUEST['entry_id']) && in_array('user_shifts_admin', $privileges)) { - if (isset($_REQUEST['entry_id']) && test_request_int('entry_id')) { - $entry_id = $_REQUEST['entry_id']; - } else { - redirect(page_link_to('user_shifts')); - } - - $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')); - } elseif (isset($_REQUEST['edit_shift']) && in_array('admin_shifts', $privileges)) { - // Schicht bearbeiten - $msg = ""; - $valid = true; - - if (isset($_REQUEST['edit_shift']) && test_request_int('edit_shift')) { - $shift_id = $_REQUEST['edit_shift']; - } else { - redirect(page_link_to('user_shifts')); - } - - $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']; - } - - // 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']; - } - } - - // 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']; - } - } - - $shifttype_id = $shift['shifttype_id']; - $title = $shift['title']; - $rid = $shift['RID']; - $start = $shift['start']; - $end = $shift['end']; - - if (isset($_REQUEST['submit'])) { - // Name/Bezeichnung der Schicht, darf leer sein - $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']])) { - $rid = $_REQUEST['rid']; - } else { - $valid = false; - $rid = $rooms[0]['RID']; - $msg .= error(_("Please select a room."), true); - } - - if (isset($_REQUEST['shifttype_id']) && isset($shifttypes[$_REQUEST['shifttype_id']])) { - $shifttype_id = $_REQUEST['shifttype_id']; - } else { - $valid = false; - $msg .= error(_('Please select a shifttype.'), true); - } - - if (isset($_REQUEST['start']) && $tmp = DateTime::createFromFormat("Y-m-d H:i", trim($_REQUEST['start']))) { - $start = $tmp->getTimestamp(); - } else { - $valid = false; - $msg .= error(_("Please enter a valid starting time for the shifts."), true); - } - - if (isset($_REQUEST['end']) && $tmp = DateTime::createFromFormat("Y-m-d H:i", trim($_REQUEST['end']))) { - $end = $tmp->getTimestamp(); - } else { - $valid = false; - $msg .= error(_("Please enter a valid ending time for the shifts."), true); - } - - if ($start >= $end) { - $valid = false; - $msg .= error(_("The ending time has to be after the starting time."), true); - } - - foreach ($needed_angel_types_source as $type) { - if (isset($_REQUEST['type_' . $type['id']]) && preg_match("/^[0-9]+$/", trim($_REQUEST['type_' . $type['id']]))) { - $needed_angel_types[$type['id']] = trim($_REQUEST['type_' . $type['id']]); - } else { - $valid = false; - $msg .= error(sprintf(_("Please check your input for needed angels of type %s."), $type['name']), true); - } - } - - if ($valid) { - $shift['shifttype_id'] = $shifttype_id; - $shift['title'] = $title; - $shift['RID'] = $rid; - $shift['start'] = $start; - $shift['end'] = $end; - - $result = Shift_update($shift); - if ($result === false) { - engelsystem_error('Unable to update shift.'); - } - sql_query("DELETE FROM `NeededAngelTypes` WHERE `shift_id`='" . sql_escape($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; - } - - 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)); - success(_("Shift updated.")); - - redirect(shift_link([ - 'SID' => $shift_id - ])); - } - } - - $angel_types = ""; - foreach ($types as $type) { - $angel_types .= form_spinner('type_' . $type['id'], $type['name'], $needed_angel_types[$type['id']]); - } - - return page_with_title(shifts_title(), [ - msg(), - '', - form([ - form_select('shifttype_id', _('Shifttype'), $shifttypes, $shifttype_id), - form_text('title', _("Title"), $title), - form_select('rid', _("Room:"), $room_array, $rid), - form_text('start', _("Start:"), date("Y-m-d H:i", $start)), - form_text('end', _("End:"), date("Y-m-d H:i", $end)), - '

' . _("Needed angels") . '

', - $angel_types, - form_submit('submit', _("Save")) - ]) - ]); - } elseif (isset($_REQUEST['delete_shift']) && in_array('user_shifts_admin', $privileges)) { - // Schicht komplett löschen (nur für admins/user mit user_shifts_admin privileg) - if (isset($_REQUEST['delete_shift']) && preg_match("/^[0-9]*$/", $_REQUEST['delete_shift'])) { - $shift_id = $_REQUEST['delete_shift']; - } else { - redirect(page_link_to('user_shifts')); - } - - $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.'); - } - - 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.")); - redirect(page_link_to('user_shifts')); - } - - return page_with_title(shifts_title(), [ - error(sprintf(_("Do you want to delete the shift %s from %s to %s?"), $shift['name'], date("Y-m-d H:i", $shift['start']), date("H:i", $shift['end'])), true), - '' . _("delete") . '' - ]); + if (isset($_REQUEST['entry_id'])) { + return shift_entry_delete_controller(); + } elseif (isset($_REQUEST['edit_shift'])) { + return shift_edit_controller(); + } elseif (isset($_REQUEST['delete_shift'])) { + return shift_delete_controller(); } elseif (isset($_REQUEST['shift_id'])) { - if (isset($_REQUEST['shift_id']) && preg_match("/^[0-9]*$/", $_REQUEST['shift_id'])) { - $shift_id = $_REQUEST['shift_id']; - } else { - redirect(page_link_to('user_shifts')); - } - - $shift = Shift($shift_id); - $room; - $shift['Name'] = $room_array[$shift['RID']]; - if ($shift === false) { - engelsystem_error('Unable to load shift.'); - } - if ($shift == null) { - redirect(page_link_to('user_shifts')); - } - - if (isset($_REQUEST['type_id']) && preg_match("/^[0-9]*$/", $_REQUEST['type_id'])) { - $type_id = $_REQUEST['type_id']; - } else { - redirect(page_link_to('user_shifts')); - } - - if (in_array('user_shifts_admin', $privileges)) { - $type = sql_select("SELECT * FROM `AngelTypes` WHERE `id`='" . sql_escape($type_id) . "' LIMIT 1"); - } else { - $type = sql_select("SELECT * FROM `UserAngelTypes` JOIN `AngelTypes` ON (`UserAngelTypes`.`angeltype_id` = `AngelTypes`.`id`) WHERE `AngelTypes`.`id` = '" . sql_escape($type_id) . "' AND (`AngelTypes`.`restricted` = 0 OR (`UserAngelTypes`.`user_id` = '" . sql_escape($user['UID']) . "' AND NOT `UserAngelTypes`.`confirm_user_id` IS NULL)) LIMIT 1"); - } - - if (count($type) == 0) { - redirect(page_link_to('user_shifts')); - } - $type = $type[0]; - - if (! Shift_signup_allowed($shift, $type)) { - error(_('You are not allowed to sign up for this shift. Maybe shift is full or already running.')); - redirect(shift_link($shift)); - } - - if (isset($_REQUEST['submit'])) { - $selected_type_id = $type_id; - if (in_array('user_shifts_admin', $privileges)) { - if (isset($_REQUEST['user_id']) && preg_match("/^[0-9]*$/", $_REQUEST['user_id'])) { - $user_id = $_REQUEST['user_id']; - } else { - $user_id = $user['UID']; - } - - if (sql_num_query("SELECT * FROM `User` WHERE `UID`='" . sql_escape($user_id) . "' LIMIT 1") == 0) { - redirect(page_link_to('user_shifts')); - } - - if (isset($_REQUEST['angeltype_id']) && test_request_int('angeltype_id') && sql_num_query("SELECT * FROM `AngelTypes` WHERE `id`='" . sql_escape($_REQUEST['angeltype_id']) . "' LIMIT 1") > 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']; - } - $angeltyppe_select = html_select_key('angeltype_id', 'angeltype_id', $angeltypes, $type['id']); - } else { - $user_text = User_Nick_render($user); - $angeltyppe_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'], $angeltyppe_select, "", false, null, in_array('user_shifts_admin', $privileges)); - } else { - return view_user_shifts(); + return shift_entry_add_controller(); } + return view_user_shifts(); } function view_user_shifts() { @@ -463,19 +138,17 @@ function view_user_shifts() { if ($_SESSION['user_shifts']['start_day'] == $_SESSION['user_shifts']['end_day'] && $_SESSION['user_shifts']['start_time'] >= $_SESSION['user_shifts']['end_time']) { $_SESSION['user_shifts']['end_time'] = '23:59'; } - + + $starttime = now(); if (isset($_SESSION['user_shifts']['start_day'])) { $starttime = DateTime::createFromFormat("Y-m-d H:i", $_SESSION['user_shifts']['start_day'] . $_SESSION['user_shifts']['start_time']); $starttime = $starttime->getTimestamp(); - } else { - $starttime = now(); } - + + $endtime = now() + 24 * 60 * 60; if (isset($_SESSION['user_shifts']['end_day'])) { $endtime = DateTime::createFromFormat("Y-m-d H:i", $_SESSION['user_shifts']['end_day'] . $_SESSION['user_shifts']['end_time']); $endtime = $endtime->getTimestamp(); - } else { - $endtime = now() + 24 * 60 * 60; } if (! isset($_SESSION['user_shifts']['rooms']) || count($_SESSION['user_shifts']['rooms']) == 0) { -- cgit v1.2.3-54-g00ecf From 5e66007f0629ab5a9b4449ca6ea4070f372bd656 Mon Sep 17 00:00:00 2001 From: msquare Date: Sun, 2 Oct 2016 23:00:01 +0200 Subject: introduce ShiftsFilter --- includes/engelsystem_provider.php | 1 + includes/model/Shifts_model.php | 65 +++- includes/pages/user_shifts.php | 732 ++++++++++++++------------------------ includes/sys_page.php | 13 + templates/user_shifts.html | 1 - 5 files changed, 351 insertions(+), 461 deletions(-) diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php index 979102e0..c6d34d1a 100644 --- a/includes/engelsystem_provider.php +++ b/includes/engelsystem_provider.php @@ -18,6 +18,7 @@ require_once realpath(__DIR__ . '/../includes/model/NeededAngelTypes_model.php') require_once realpath(__DIR__ . '/../includes/model/Room_model.php'); require_once realpath(__DIR__ . '/../includes/model/ShiftEntry_model.php'); require_once realpath(__DIR__ . '/../includes/model/Shifts_model.php'); +require_once realpath(__DIR__ . '/../includes/model/ShiftsFilter.php'); require_once realpath(__DIR__ . '/../includes/model/ShiftTypes_model.php'); require_once realpath(__DIR__ . '/../includes/model/UserAngelTypes_model.php'); require_once realpath(__DIR__ . '/../includes/model/UserDriverLicenses_model.php'); diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index a827c6b5..b83d1a00 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -1,9 +1,70 @@ 0 AS 'has_special_needs' + FROM `Shifts` + INNER JOIN `Room` USING (`RID`) + INNER JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`) + LEFT JOIN ( + SELECT COUNT(*) AS special_needs , nat3.`shift_id` + FROM `NeededAngelTypes` AS nat3 + WHERE `shift_id` IS NOT NULL + GROUP BY nat3.`shift_id` + ) AS nat2 ON nat2.`shift_id` = `Shifts`.`SID` + INNER JOIN `NeededAngelTypes` AS nat + ON nat.`count` != 0 + AND nat.`angel_type_id` IN (" . implode(',', $shiftsFilter->getTypes()) . ") + AND ( + (nat2.`special_needs` > 0 AND nat.`shift_id` = `Shifts`.`SID`) + OR + ( + (nat2.`special_needs` = 0 OR nat2.`special_needs` IS NULL) + AND nat.`room_id` = `RID`) + ) + LEFT JOIN ( + SELECT se.`SID`, se.`TID`, COUNT(*) as count + FROM `ShiftEntry` AS se GROUP BY se.`SID`, se.`TID` + ) AS entries ON entries.`SID` = `Shifts`.`SID` AND entries.`TID` = nat.`angel_type_id` + WHERE `Shifts`.`RID` IN (" . implode(',', $shiftsFilter->getRooms()) . ") + AND `start` BETWEEN " . $shiftsFilter->getStartTime() . " AND " . $shiftsFilter->getEndTime(); + + if (count($shiftsFilter->getFilled()) == 1) { + if ($shiftsFilter->getFilled()[0] == ShiftsFilter::FILLED_FREE) { + $SQL .= " + AND ( + nat.`count` > entries.`count` OR entries.`count` IS NULL + OR EXISTS ( + SELECT `SID` + FROM `ShiftEntry` + WHERE `UID` = '" . sql_escape($user['UID']) . "' + AND `ShiftEntry`.`SID` = `Shifts`.`SID` + ) + )"; + } elseif ($_SESSION['user_shifts']['filled'][0] == 1) { + $SQL .= " + AND ( + nat.`count` <= entries.`count` + OR EXISTS ( + SELECT `SID` + FROM `ShiftEntry` + WHERE `UID` = '" . sql_escape($user['UID']) . "' + AND `ShiftEntry`.`SID` = `Shifts`.`SID` + ) + )"; + } + } + $SQL .= " + ORDER BY `start`"; + + return sql_select($SQL); +} /** * Check if a shift collides with other shifts (in time). - * @param Shift $shift - * @param array $shifts + * + * @param Shift $shift + * @param array $shifts */ function Shift_collides($shift, $shifts) { foreach ($shifts as $other_shift) { diff --git a/includes/pages/user_shifts.php b/includes/pages/user_shifts.php index 77ae8582..d3ad0aa2 100644 --- a/includes/pages/user_shifts.php +++ b/includes/pages/user_shifts.php @@ -1,4 +1,5 @@ setUserShiftsAdmin($user_shifts_admin); + $shiftsFilter->setFilled(check_request_int_array('filled')); + $shiftsFilter->setRooms(check_request_int_array('rooms')); + $shiftsFilter->setTypes(check_request_int_array('types')); + + $day = date('Y-m-d', time()); + $start_day = in_array($day, $days) ? $day : min($days); + if (isset($_REQUEST['start_day']) && in_array($_REQUEST['start_day'], $days)) { + $start_day = $_REQUEST['start_day']; + } + + $start_time = date("H:i"); + if (isset($_REQUEST['start_time']) && preg_match('#^\d{1,2}:\d\d$#', $_REQUEST['start_time'])) { + $start_time = $_REQUEST['start_time']; + } + + $day = date('Y-m-d', time() + 24 * 60 * 60); + $end_day = in_array($day, $days) ? $day : max($days); + if (isset($_REQUEST['end_day']) && in_array($_REQUEST['end_day'], $days)) { + $end_day = $_REQUEST['end_day']; + } + + $end_time = date("H:i"); + if (isset($_REQUEST['end_time']) && preg_match('#^\d{1,2}:\d\d$#', $_REQUEST['end_time'])) { + $end_time = $_REQUEST['end_time']; + } + + if ($start_day > $end_day) { + $end_day = $start_day; + } + if ($start_day == $end_day && $start_time >= $end_time) { + $end_time = "23:59"; + } + + $startdatetime = DateTime::createFromFormat("Y-m-d H:i", $start_day . " " . $start_time); + $shiftsFilter->setStartTime($startdatetime->getTimestamp()); + + $enddatetime = DateTime::createFromFormat("Y-m-d H:i", $end_day . " " . $end_time); + $shiftsFilter->setEndTime($enddatetime->getTimestamp()); + + return $shiftsFilter; +} + function view_user_shifts() { global $user, $privileges; global $ical_shifts; @@ -43,7 +95,6 @@ function view_user_shifts() { } $rooms = sql_select("SELECT `RID` AS `id`, `Name` AS `name` FROM `Room` WHERE `show`='Y' ORDER BY `Name`"); - if (count($rooms) == 0) { error(_("The administration has not configured any rooms yet.")); redirect('?'); @@ -57,137 +108,32 @@ function view_user_shifts() { if (empty($types)) { $types = sql_select("SELECT `id`, `name` FROM `AngelTypes` WHERE `restricted` = 0"); } - $filled = [ - [ - 'id' => '1', - 'name' => _("occupied") - ], - [ - 'id' => '0', - 'name' => _("free") - ] - ]; if (count($types) == 0) { error(_("The administration has not configured any angeltypes yet - or you are not subscribed to any angeltype.")); redirect('?'); } - if (! isset($_SESSION['user_shifts'])) { - $_SESSION['user_shifts'] = []; + if (! isset($_SESSION['ShiftsFilter'])) { + $room_ids = array_map('get_ids_from_array', $rooms); + $type_ids = array_map('get_ids_from_array', $types); + $_SESSION['ShiftsFilter'] = new ShiftsFilter(in_array('user_shifts_admin', $privileges), $room_ids, $type_ids); } + $_SESSION['ShiftsFilter'] = update_ShiftsFilter($_SESSION['ShiftsFilter'], in_array('user_shifts_admin', $privileges), $days); + $shiftsFilter = $_SESSION['ShiftsFilter']; - if (! isset($_SESSION['user_shifts']['filled'])) { - // User shift admins see free and occupied shifts by default - $_SESSION['user_shifts']['filled'] = in_array('user_shifts_admin', $privileges) ? [ - 0, - 1 - ] : [ - 0 - ]; + $shifts = Shifts_by_ShiftsFilter($shiftsFilter, $user); + if ($shifts === false) { + engelsystem_error("Unable to load shifts by filter."); } - foreach ([ - 'rooms', - 'types', - 'filled' - ] as $key) { - if (isset($_REQUEST[$key])) { - $filtered = array_filter($_REQUEST[$key], 'is_numeric'); - if (! empty($filtered)) { - $_SESSION['user_shifts'][$key] = $filtered; - } - unset($filtered); - } - if (! isset($_SESSION['user_shifts'][$key])) { - $_SESSION['user_shifts'][$key] = array_map('get_ids_from_array', $$key); - } - } - - if (isset($_REQUEST['rooms'])) { - if (isset($_REQUEST['new_style'])) { - $_SESSION['user_shifts']['new_style'] = true; - } else { - $_SESSION['user_shifts']['new_style'] = false; - } - } - if (! isset($_SESSION['user_shifts']['new_style'])) { - $_SESSION['user_shifts']['new_style'] = true; - } - foreach ([ - 'start', - 'end' - ] as $key) { - if (isset($_REQUEST[$key . '_day']) && in_array($_REQUEST[$key . '_day'], $days)) { - $_SESSION['user_shifts'][$key . '_day'] = $_REQUEST[$key . '_day']; - } - if (isset($_REQUEST[$key . '_time']) && preg_match('#^\d{1,2}:\d\d$#', $_REQUEST[$key . '_time'])) { - $_SESSION['user_shifts'][$key . '_time'] = $_REQUEST[$key . '_time']; - } - if (! isset($_SESSION['user_shifts'][$key . '_day'])) { - $time = date('Y-m-d', time() + ($key == 'end' ? 24 * 60 * 60 : 0)); - $_SESSION['user_shifts'][$key . '_day'] = in_array($time, $days) ? $time : ($key == 'end' ? max($days) : min($days)); - } - if (! isset($_SESSION['user_shifts'][$key . '_time'])) { - $_SESSION['user_shifts'][$key . '_time'] = date('H:i'); - } - } - if ($_SESSION['user_shifts']['start_day'] > $_SESSION['user_shifts']['end_day']) { - $_SESSION['user_shifts']['end_day'] = $_SESSION['user_shifts']['start_day']; - } - if ($_SESSION['user_shifts']['start_day'] == $_SESSION['user_shifts']['end_day'] && $_SESSION['user_shifts']['start_time'] >= $_SESSION['user_shifts']['end_time']) { - $_SESSION['user_shifts']['end_time'] = '23:59'; - } - - $starttime = now(); - if (isset($_SESSION['user_shifts']['start_day'])) { - $starttime = DateTime::createFromFormat("Y-m-d H:i", $_SESSION['user_shifts']['start_day'] . $_SESSION['user_shifts']['start_time']); - $starttime = $starttime->getTimestamp(); - } - - $endtime = now() + 24 * 60 * 60; - if (isset($_SESSION['user_shifts']['end_day'])) { - $endtime = DateTime::createFromFormat("Y-m-d H:i", $_SESSION['user_shifts']['end_day'] . $_SESSION['user_shifts']['end_time']); - $endtime = $endtime->getTimestamp(); - } - - if (! isset($_SESSION['user_shifts']['rooms']) || count($_SESSION['user_shifts']['rooms']) == 0) { - $_SESSION['user_shifts']['rooms'] = [ - 0 - ]; - } - - $SQL = "SELECT DISTINCT `Shifts`.*, `ShiftTypes`.`name`, `Room`.`Name` as `room_name`, nat2.`special_needs` > 0 AS 'has_special_needs' - FROM `Shifts` - INNER JOIN `Room` USING (`RID`) - INNER JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`) - LEFT JOIN (SELECT COUNT(*) AS special_needs , nat3.`shift_id` FROM `NeededAngelTypes` AS nat3 WHERE `shift_id` IS NOT NULL GROUP BY nat3.`shift_id`) AS nat2 ON nat2.`shift_id` = `Shifts`.`SID` - INNER JOIN `NeededAngelTypes` AS nat ON nat.`count` != 0 AND nat.`angel_type_id` IN (" . implode(',', $_SESSION['user_shifts']['types']) . ") AND ((nat2.`special_needs` > 0 AND nat.`shift_id` = `Shifts`.`SID`) OR ((nat2.`special_needs` = 0 OR nat2.`special_needs` IS NULL) AND nat.`room_id` = `RID`)) - LEFT JOIN (SELECT se.`SID`, se.`TID`, COUNT(*) as count FROM `ShiftEntry` AS se GROUP BY se.`SID`, se.`TID`) AS entries ON entries.`SID` = `Shifts`.`SID` AND entries.`TID` = nat.`angel_type_id` - WHERE `Shifts`.`RID` IN (" . implode(',', $_SESSION['user_shifts']['rooms']) . ") - AND `start` BETWEEN " . $starttime . " AND " . $endtime; - - if (count($_SESSION['user_shifts']['filled']) == 1) { - if ($_SESSION['user_shifts']['filled'][0] == 0) { - $SQL .= " - AND (nat.`count` > entries.`count` OR entries.`count` IS NULL OR EXISTS (SELECT `SID` FROM `ShiftEntry` WHERE `UID` = '" . sql_escape($user['UID']) . "' AND `ShiftEntry`.`SID` = `Shifts`.`SID`))"; - } elseif ($_SESSION['user_shifts']['filled'][0] == 1) { - $SQL .= " - AND (nat.`count` <= entries.`count` OR EXISTS (SELECT `SID` FROM `ShiftEntry` WHERE `UID` = '" . sql_escape($user['UID']) . "' AND `ShiftEntry`.`SID` = `Shifts`.`SID`))"; - } - } - $SQL .= " - ORDER BY `start`"; - - $shifts = sql_select($SQL); - $ownshifts_source = sql_select(" SELECT `ShiftTypes`.`name`, `Shifts`.* FROM `Shifts` INNER JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`) INNER JOIN `ShiftEntry` ON (`Shifts`.`SID` = `ShiftEntry`.`SID` AND `ShiftEntry`.`UID` = '" . sql_escape($user['UID']) . "') - WHERE `Shifts`.`RID` IN (" . implode(',', $_SESSION['user_shifts']['rooms']) . ") - AND `start` BETWEEN " . $starttime . " AND " . $endtime); + WHERE `Shifts`.`RID` IN (" . implode(',', $shiftsFilter->getRooms()) . ") + AND `start` BETWEEN " . $shiftsFilter->getStartTime() . " AND " . $shiftsFilter->getEndTime()); $ownshifts = []; foreach ($ownshifts_source as $ownshift) { $ownshifts[$ownshift['SID']] = $ownshift; @@ -198,368 +144,255 @@ function view_user_shifts() { /* * [0] => Array ( [SID] => 1 [start] => 1355958000 [end] => 1355961600 [RID] => 1 [name] => [URL] => [PSID] => [room_name] => test1 [has_special_needs] => 1 [is_full] => 0 ) */ - if ($_SESSION['user_shifts']['new_style']) { - $first = 15 * 60 * floor($starttime / (15 * 60)); - $maxshow = ceil(($endtime - $first) / (60 * 15)); - $block = []; - $todo = []; - $myrooms = $rooms; - - // delete un-selected rooms from array - foreach ($myrooms as $k => $v) { - if (array_search($v["id"], $_SESSION['user_shifts']['rooms']) === false) { - unset($myrooms[$k]); - } - // initialize $block array - $block[$v["id"]] = array_fill(0, $maxshow, 0); + $first = 15 * 60 * floor($shiftsFilter->getStartTime() / (15 * 60)); + $maxshow = ceil(($shiftsFilter->getEndTime() - $first) / (60 * 15)); + $block = []; + $todo = []; + $myrooms = $rooms; + + // delete un-selected rooms from array + foreach ($myrooms as $k => $v) { + if (array_search($v["id"], $shiftsFilter->getRooms()) === false) { + unset($myrooms[$k]); } - - // calculate number of parallel shifts in each timeslot for each room - foreach ($shifts as $k => $shift) { - $rid = $shift["RID"]; - $blocks = ($shift["end"] - $shift["start"]) / (15 * 60); - $firstblock = floor(($shift["start"] - $first) / (15 * 60)); - for ($i = $firstblock; $i < $blocks + $firstblock && $i < $maxshow; $i ++) { - $block[$rid][$i] ++; - } - $shifts[$k]['own'] = in_array($shift['SID'], array_keys($ownshifts)); + // initialize $block array + $block[$v["id"]] = array_fill(0, $maxshow, 0); + } + + // calculate number of parallel shifts in each timeslot for each room + foreach ($shifts as $k => $shift) { + $rid = $shift["RID"]; + $blocks = ($shift["end"] - $shift["start"]) / (15 * 60); + $firstblock = floor(($shift["start"] - $first) / (15 * 60)); + for ($i = $firstblock; $i < $blocks + $firstblock && $i < $maxshow; $i ++) { + $block[$rid][$i] ++; } - - $shifts_table = '
'; - foreach ($myrooms as $key => $room) { - $rid = $room["id"]; - if (array_sum($block[$rid]) == 0) { - // do not display columns without entries - unset($block[$rid]); - unset($myrooms[$key]); - continue; - } - $colspan = call_user_func_array('max', $block[$rid]); - if ($colspan == 0) { - $colspan = 1; - } - $todo[$rid] = array_fill(0, $maxshow, $colspan); - $shifts_table .= " 1) ? ' colspan="' . $colspan . '"' : '') . ">" . Room_name_render([ - 'RID' => $room['id'], - 'Name' => $room['name'] - ]) . "\n"; + $shifts[$k]['own'] = in_array($shift['SID'], array_keys($ownshifts)); + } + + $shifts_table = '
-
'; + foreach ($myrooms as $key => $room) { + $rid = $room["id"]; + if (array_sum($block[$rid]) == 0) { + // do not display columns without entries + unset($block[$rid]); + unset($myrooms[$key]); + continue; } - unset($block, $blocks, $firstblock, $colspan, $key, $room); - - $shifts_table .= ""; - for ($i = 0; $i < $maxshow; $i ++) { - $thistime = $first + ($i * 15 * 60); - if ($thistime % (24 * 60 * 60) == 23 * 60 * 60 && $endtime - $starttime > 24 * 60 * 60) { - $shifts_table .= ""; - foreach ($myrooms as $room) { - $rid = $room["id"]; - foreach ($shifts as $shift) { - if ($shift["RID"] == $rid) { - if (floor($shift["start"] / (15 * 60)) == $thistime / (15 * 60)) { - $blocks = ($shift["end"] - $shift["start"]) / (15 * 60); - if ($blocks < 1) { - $blocks = 1; - } - - $collides = in_array($shift['SID'], array_keys($ownshifts)); - if (! $collides) { - foreach ($ownshifts as $ownshift) { - if ($ownshift['start'] >= $shift['start'] && $ownshift['start'] < $shift['end'] || $ownshift['end'] > $shift['start'] && $ownshift['end'] <= $shift['end'] || $ownshift['start'] < $shift['start'] && $ownshift['end'] > $shift['end']) { - $collides = true; - break; - } + $colspan = call_user_func_array('max', $block[$rid]); + if ($colspan == 0) { + $colspan = 1; + } + $todo[$rid] = array_fill(0, $maxshow, $colspan); + $shifts_table .= " 1) ? ' colspan="' . $colspan . '"' : '') . ">" . Room_name_render([ + 'RID' => $room['id'], + 'Name' => $room['name'] + ]) . "\n"; + } + unset($block, $blocks, $firstblock, $colspan, $key, $room); + + $shifts_table .= ""; + for ($i = 0; $i < $maxshow; $i ++) { + $thistime = $first + ($i * 15 * 60); + if ($thistime % (24 * 60 * 60) == 23 * 60 * 60 && $shiftsFilter->getEndTime() - $shiftsFilter->getStartTime() > 24 * 60 * 60) { + $shifts_table .= ""; + foreach ($myrooms as $room) { + $rid = $room["id"]; + foreach ($shifts as $shift) { + if ($shift["RID"] == $rid) { + if (floor($shift["start"] / (15 * 60)) == $thistime / (15 * 60)) { + $blocks = ($shift["end"] - $shift["start"]) / (15 * 60); + if ($blocks < 1) { + $blocks = 1; + } + + $collides = in_array($shift['SID'], array_keys($ownshifts)); + if (! $collides) { + foreach ($ownshifts as $ownshift) { + if ($ownshift['start'] >= $shift['start'] && $ownshift['start'] < $shift['end'] || $ownshift['end'] > $shift['start'] && $ownshift['end'] <= $shift['end'] || $ownshift['start'] < $shift['start'] && $ownshift['end'] > $shift['end']) { + $collides = true; + break; } } - - $is_free = false; - $shifts_row = ''; - if (in_array('admin_shifts', $privileges)) { - $shifts_row .= '
' . table_buttons([ - button(page_link_to('user_shifts') . '&edit_shift=' . $shift['SID'], glyph('edit'), 'btn-xs'), - button(page_link_to('user_shifts') . '&delete_shift=' . $shift['SID'], glyph('trash'), 'btn-xs') - ]) . '
'; - } - $shifts_row .= Room_name_render([ - 'RID' => $room['id'], - 'Name' => $room['name'] - ]) . '
'; - $shifts_row .= '' . date('Y-m-d H:i', $shift['start']); - $shifts_row .= " – "; - $shifts_row .= date('H:i', $shift['end']); - $shifts_row .= "
"; - $shifts_row .= ShiftType($shift['shifttype_id'])['name']; - $shifts_row .= "
"; - if ($shift['title'] != '') { - $shifts_row .= $shift['title']; - $shifts_row .= "
"; - } - $shifts_row .= '
'; - $shifts_row .= '
'; - $query = "SELECT `NeededAngelTypes`.`count`, `AngelTypes`.`id`, `AngelTypes`.`restricted`, `UserAngelTypes`.`confirm_user_id`, `AngelTypes`.`name`, `UserAngelTypes`.`user_id` + } + + $is_free = false; + $shifts_row = ''; + if (in_array('admin_shifts', $privileges)) { + $shifts_row .= '
' . table_buttons([ + button(page_link_to('user_shifts') . '&edit_shift=' . $shift['SID'], glyph('edit'), 'btn-xs'), + button(page_link_to('user_shifts') . '&delete_shift=' . $shift['SID'], glyph('trash'), 'btn-xs') + ]) . '
'; + } + $shifts_row .= Room_name_render([ + 'RID' => $room['id'], + 'Name' => $room['name'] + ]) . '
'; + $shifts_row .= '' . date('Y-m-d H:i', $shift['start']); + $shifts_row .= " – "; + $shifts_row .= date('H:i', $shift['end']); + $shifts_row .= "
"; + $shifts_row .= ShiftType($shift['shifttype_id'])['name']; + $shifts_row .= "
"; + if ($shift['title'] != '') { + $shifts_row .= $shift['title']; + $shifts_row .= "
"; + } + $shifts_row .= '
'; + $shifts_row .= '
'; + $query = "SELECT `NeededAngelTypes`.`count`, `AngelTypes`.`id`, `AngelTypes`.`restricted`, `UserAngelTypes`.`confirm_user_id`, `AngelTypes`.`name`, `UserAngelTypes`.`user_id` FROM `NeededAngelTypes` JOIN `AngelTypes` ON (`NeededAngelTypes`.`angel_type_id` = `AngelTypes`.`id`) LEFT JOIN `UserAngelTypes` ON (`NeededAngelTypes`.`angel_type_id` = `UserAngelTypes`.`angeltype_id`AND `UserAngelTypes`.`user_id`='" . sql_escape($user['UID']) . "') WHERE `count` > 0 AND "; - if ($shift['has_special_needs']) { - $query .= "`shift_id` = '" . sql_escape($shift['SID']) . "'"; - } else { - $query .= "`room_id` = '" . sql_escape($shift['RID']) . "'"; - } - if (! empty($_SESSION['user_shifts']['types'])) { - $query .= " AND `angel_type_id` IN (" . implode(',', $_SESSION['user_shifts']['types']) . ") "; - } - $query .= " ORDER BY `AngelTypes`.`name`"; - $angeltypes = sql_select($query); - - if (count($angeltypes) > 0) { - foreach ($angeltypes as $angeltype) { - $entries = sql_select("SELECT * FROM `ShiftEntry` JOIN `User` ON (`ShiftEntry`.`UID` = `User`.`UID`) WHERE `SID`='" . sql_escape($shift['SID']) . "' AND `TID`='" . sql_escape($angeltype['id']) . "' ORDER BY `Nick`"); - $entry_list = []; - $freeloader = 0; - foreach ($entries as $entry) { - $style = ''; - if ($entry['freeloaded']) { - $freeloader ++; - $style = " text-decoration: line-through;"; - } - if (in_array('user_shifts_admin', $privileges)) { - $entry_list[] = "" . User_Nick_render($entry) . ' ' . table_buttons([ - button(page_link_to('user_shifts') . '&entry_id=' . $entry['id'], glyph('trash'), 'btn-xs') - ]) . ''; - } else { - $entry_list[] = "" . User_Nick_render($entry) . ""; - } + if ($shift['has_special_needs']) { + $query .= "`shift_id` = '" . sql_escape($shift['SID']) . "'"; + } else { + $query .= "`room_id` = '" . sql_escape($shift['RID']) . "'"; + } + if (! empty($shiftsFilter->getTypes())) { + $query .= " AND `angel_type_id` IN (" . implode(',', $shiftsFilter->getTypes()) . ") "; + } + $query .= " ORDER BY `AngelTypes`.`name`"; + $angeltypes = sql_select($query); + + if (count($angeltypes) > 0) { + foreach ($angeltypes as $angeltype) { + $entries = sql_select("SELECT * FROM `ShiftEntry` JOIN `User` ON (`ShiftEntry`.`UID` = `User`.`UID`) WHERE `SID`='" . sql_escape($shift['SID']) . "' AND `TID`='" . sql_escape($angeltype['id']) . "' ORDER BY `Nick`"); + $entry_list = []; + $freeloader = 0; + foreach ($entries as $entry) { + $style = ''; + if ($entry['freeloaded']) { + $freeloader ++; + $style = " text-decoration: line-through;"; } - if ($angeltype['count'] - count($entries) - $freeloader > 0) { - $inner_text = sprintf(ngettext("%d helper needed", "%d helpers needed", $angeltype['count'] - count($entries)), $angeltype['count'] - count($entries)); - // is the shift still running or alternatively is the user shift admin? - $user_may_join_shift = true; - - // you cannot join if user alread joined a parallel or this shift - $user_may_join_shift &= ! $collides; - - // you cannot join if user is not of this angel type - $user_may_join_shift &= isset($angeltype['user_id']); - - // you cannot join if you are not confirmed - if ($angeltype['restricted'] == 1 && isset($angeltype['user_id'])) { - $user_may_join_shift &= isset($angeltype['confirm_user_id']); - } - - // you can only join if the shift is in future or running - $user_may_join_shift &= time() < $shift['start']; - - // User shift admins may join anybody in every shift - $user_may_join_shift |= in_array('user_shifts_admin', $privileges); - if ($user_may_join_shift) { - $entry_list[] = '' . $inner_text . ' ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _('Sign up'), 'btn-xs'); + if (in_array('user_shifts_admin', $privileges)) { + $entry_list[] = "" . User_Nick_render($entry) . ' ' . table_buttons([ + button(page_link_to('user_shifts') . '&entry_id=' . $entry['id'], glyph('trash'), 'btn-xs') + ]) . ''; + } else { + $entry_list[] = "" . User_Nick_render($entry) . ""; + } + } + if ($angeltype['count'] - count($entries) - $freeloader > 0) { + $inner_text = sprintf(ngettext("%d helper needed", "%d helpers needed", $angeltype['count'] - count($entries)), $angeltype['count'] - count($entries)); + // is the shift still running or alternatively is the user shift admin? + $user_may_join_shift = true; + + // you cannot join if user alread joined a parallel or this shift + $user_may_join_shift &= ! $collides; + + // you cannot join if user is not of this angel type + $user_may_join_shift &= isset($angeltype['user_id']); + + // you cannot join if you are not confirmed + if ($angeltype['restricted'] == 1 && isset($angeltype['user_id'])) { + $user_may_join_shift &= isset($angeltype['confirm_user_id']); + } + + // you can only join if the shift is in future or running + $user_may_join_shift &= time() < $shift['start']; + + // User shift admins may join anybody in every shift + $user_may_join_shift |= in_array('user_shifts_admin', $privileges); + if ($user_may_join_shift) { + $entry_list[] = '' . $inner_text . ' ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _('Sign up'), 'btn-xs'); + } else { + if (time() > $shift['start']) { + $entry_list[] = $inner_text . ' (' . _('ended') . ')'; + } elseif ($angeltype['restricted'] == 1 && isset($angeltype['user_id']) && ! isset($angeltype['confirm_user_id'])) { + $entry_list[] = $inner_text . glyph('lock'); + } elseif ($angeltype['restricted'] == 1) { + $entry_list[] = $inner_text; + } elseif ($collides) { + $entry_list[] = $inner_text; } else { - if (time() > $shift['start']) { - $entry_list[] = $inner_text . ' (' . _('ended') . ')'; - } elseif ($angeltype['restricted'] == 1 && isset($angeltype['user_id']) && ! isset($angeltype['confirm_user_id'])) { - $entry_list[] = $inner_text . glyph('lock'); - } elseif ($angeltype['restricted'] == 1) { - $entry_list[] = $inner_text; - } elseif ($collides) { - $entry_list[] = $inner_text; - } else { - $entry_list[] = $inner_text . '
' . button(page_link_to('user_angeltypes') . '&action=add&angeltype_id=' . $angeltype['id'], sprintf(_('Become %s'), $angeltype['name']), 'btn-xs'); - } + $entry_list[] = $inner_text . '
' . button(page_link_to('user_angeltypes') . '&action=add&angeltype_id=' . $angeltype['id'], sprintf(_('Become %s'), $angeltype['name']), 'btn-xs'); } - - unset($inner_text); - $is_free = true; } - $shifts_row .= '' . AngelType_name_render($angeltype) . ': '; - $shifts_row .= join(", ", $entry_list); - $shifts_row .= '
'; + unset($inner_text); + $is_free = true; } - if (in_array('user_shifts_admin', $privileges)) { - $shifts_row .= ' ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _("Add more angels"), 'btn-xs'); - } - } - if ($shift['own'] && ! in_array('user_shifts_admin', $privileges)) { - $class = 'own'; - } elseif ($collides && ! in_array('user_shifts_admin', $privileges)) { - $class = 'collides'; - } elseif ($is_free) { - $class = 'free'; - } else { - $class = 'occupied'; + + $shifts_row .= '' . AngelType_name_render($angeltype) . ': '; + $shifts_row .= join(", ", $entry_list); + $shifts_row .= '
'; } - $shifts_table .= '"; - for ($j = 0; $j < $blocks && $i + $j < $maxshow; $j ++) { - $todo[$rid][$i + $j] --; + if (in_array('user_shifts_admin', $privileges)) { + $shifts_row .= ' ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _("Add more angels"), 'btn-xs'); } } - } - } - // fill up row with empty '; - } - } - $shifts_table .= "\n"; - } - $shifts_table .= '
-
"; - $shifts_table .= date('Y-m-dH:i', $thistime); - } elseif ($thistime % (60 * 60) == 0) { - $shifts_table .= "
"; - $shifts_table .= date("H:i", $thistime); - } else { - $shifts_table .= "
"; - } - $shifts_table .= "
"; + $shifts_table .= date('Y-m-dH:i', $thistime); + } elseif ($thistime % (60 * 60) == 0) { + $shifts_table .= "
"; + $shifts_table .= date("H:i", $thistime); + } else { + $shifts_table .= "
"; + } + $shifts_table .= "'; - $shifts_table .= $shifts_row; - $shifts_table .= " - while ($todo[$rid][$i] -- > 0) { - $shifts_table .= '
'; - } else { - $shifts_table = []; - foreach ($shifts as $shift) { - $info = []; - if ($_SESSION['user_shifts']['start_day'] != $_SESSION['user_shifts']['end_day']) { - $info[] = date("Y-m-d", $shift['start']); - } - $info[] = date("H:i", $shift['start']) . ' - ' . date("H:i", $shift['end']); - if (count($_SESSION['user_shifts']['rooms']) > 1) { - $info[] = Room_name_render([ - 'Name' => $shift['room_name'], - 'RID' => $shift['RID'] - ]); - } - - $shift_row = [ - 'info' => join('
', $info), - 'entries' => '' . $shift['name'] . '' . ($shift['title'] ? '
' . $shift['title'] : '') - ]; - - if (in_array('admin_shifts', $privileges)) { - $shift_row['info'] .= ' ' . table_buttons([ - button(page_link_to('user_shifts') . '&edit_shift=' . $shift['SID'], glyph('edit'), 'btn-xs'), - button(page_link_to('user_shifts') . '&delete_shift=' . $shift['SID'], glyph('trash'), 'btn-xs') - ]); - } - $shift_row['entries'] .= '
'; - $is_free = false; - $shift_has_special_needs = 0 < sql_num_query("SELECT `id` FROM `NeededAngelTypes` WHERE `shift_id` = " . $shift['SID']); - $query = "SELECT `NeededAngelTypes`.`count`, `AngelTypes`.`id`, `AngelTypes`.`restricted`, `UserAngelTypes`.`confirm_user_id`, `AngelTypes`.`name`, `UserAngelTypes`.`user_id` - FROM `NeededAngelTypes` - JOIN `AngelTypes` ON (`NeededAngelTypes`.`angel_type_id` = `AngelTypes`.`id`) - LEFT JOIN `UserAngelTypes` ON (`NeededAngelTypes`.`angel_type_id` = `UserAngelTypes`.`angeltype_id`AND `UserAngelTypes`.`user_id`='" . sql_escape($user['UID']) . "') - WHERE "; - if ($shift_has_special_needs) { - $query .= "`shift_id` = '" . sql_escape($shift['SID']) . "'"; - } else { - $query .= "`room_id` = '" . sql_escape($shift['RID']) . "'"; - } - $query .= " AND `count` > 0 "; - if (! empty($_SESSION['user_shifts']['types'])) { - $query .= "AND `angel_type_id` IN (" . implode(',', $_SESSION['user_shifts']['types']) . ") "; - } - $query .= "ORDER BY `AngelTypes`.`name`"; - $angeltypes = sql_select($query); - if (count($angeltypes) > 0) { - $my_shift = sql_num_query("SELECT * FROM `ShiftEntry` WHERE `SID`='" . sql_escape($shift['SID']) . "' AND `UID`='" . sql_escape($user['UID']) . "' LIMIT 1") > 0; - - foreach ($angeltypes as &$angeltype) { - $entries = sql_select("SELECT * FROM `ShiftEntry` JOIN `User` ON (`ShiftEntry`.`UID` = `User`.`UID`) WHERE `SID`='" . sql_escape($shift['SID']) . "' AND `TID`='" . sql_escape($angeltype['id']) . "' ORDER BY `Nick`"); - $entry_list = []; - $entry_nicks = []; - $freeloader = 0; - foreach ($entries as $entry) { - if (in_array('user_shifts_admin', $privileges)) { - $member = User_Nick_render($entry) . ' ' . table_buttons([ - button(page_link_to('user_shifts') . '&entry_id=' . $entry['id'], glyph('trash'), 'btn-xs') - ]); + if ($shift['own'] && ! in_array('user_shifts_admin', $privileges)) { + $class = 'own'; + } elseif ($collides && ! in_array('user_shifts_admin', $privileges)) { + $class = 'collides'; + } elseif ($is_free) { + $class = 'free'; } else { - $member = User_Nick_render($entry); - } - if ($entry['freeloaded']) { - $member = '' . $member . ''; - $freeloader ++; - } - $entry_list[] = $member; - $entry_nicks[] = $entry['Nick']; - } - $angeltype['taken'] = count($entries) - $freeloader; - $angeltype['angels'] = $entry_nicks; - - // do we need more angles of this type? - if ($angeltype['count'] - count($entries) + $freeloader > 0) { - $inner_text = sprintf(ngettext("%d helper needed", "%d helpers needed", $angeltype['count'] - count($entries) + $freeloader), $angeltype['count'] - count($entries) + $freeloader); - // is the shift still running or alternatively is the user shift admin? - $user_may_join_shift = true; - - /* you cannot join if user already joined this shift */ - $user_may_join_shift &= ! $my_shift; - - // you cannot join if user is not of this angel type - $user_may_join_shift &= isset($angeltype['user_id']); - - // you cannot join if you are not confirmed - if ($angeltype['restricted'] == 1 && isset($angeltype['user_id'])) { - $user_may_join_shift &= isset($angeltype['confirm_user_id']); + $class = 'occupied'; } - - // you can only join if the shift is in future or running - $user_may_join_shift &= time() < $shift['start']; - - // User shift admins may join anybody in every shift - $user_may_join_shift |= in_array('user_shifts_admin', $privileges); - if ($user_may_join_shift) { - $entry_list[] = '' . $inner_text . ' »'; - } else { - if (time() > $shift['end']) { - $entry_list[] = $inner_text . ' (vorbei)'; - } elseif ($angeltype['restricted'] == 1 && isset($angeltype['user_id']) && ! isset($angeltype['confirm_user_id'])) { - $entry_list[] = $inner_text . glyph("lock"); - } else { - $entry_list[] = $inner_text . ' ' . sprintf(_('Become %s'), $angeltype['name']) . ''; - } + $shifts_table .= ''; + $shifts_table .= $shifts_row; + $shifts_table .= ""; + for ($j = 0; $j < $blocks && $i + $j < $maxshow; $j ++) { + $todo[$rid][$i + $j] --; } - - unset($inner_text); - $is_free = true; } - - $shift_row['entries'] .= '' . $angeltype['name'] . ': '; - $shift_row['entries'] .= join(", ", $entry_list); - $shift_row['entries'] .= '
'; } - if (in_array('user_shifts_admin', $privileges)) { - $shift_row['entries'] .= '' . _('Add more angels') . ' »'; - } - $shifts_table[] = $shift_row; - $shift['angeltypes'] = $angeltypes; - $ical_shifts[] = $shift; + } + // fill up row with empty + while ($todo[$rid][$i] -- > 0) { + $shifts_table .= ''; } } - $shifts_table = table([ - 'info' => _("Time") . "/" . _("Room"), - 'entries' => _("Entries") - ], $shifts_table); + $shifts_table .= "\n"; } + $shifts_table .= ''; if ($user['api_key'] == "") { User_reset_api_key($user, false); } + $filled = [ + [ + 'id' => '1', + 'name' => _("occupied") + ], + [ + 'id' => '0', + 'name' => _("free") + ] + ]; + $start_day = date("Y-m-d", $shiftsFilter->getStartTime()); + $start_time = date("H:i", $shiftsFilter->getStartTime()); + $end_day = date("Y-m-d", $shiftsFilter->getEndTime()); + $end_time = date("H:i", $shiftsFilter->getEndTime()); + return page([ div('col-md-12', [ msg(), template_render('../templates/user_shifts.html', [ 'title' => shifts_title(), - 'room_select' => make_select($rooms, $_SESSION['user_shifts']['rooms'], "rooms", _("Rooms")), - 'start_select' => html_select_key("start_day", "start_day", array_combine($days, $days), $_SESSION['user_shifts']['start_day']), - 'start_time' => $_SESSION['user_shifts']['start_time'], - 'end_select' => html_select_key("end_day", "end_day", array_combine($days, $days), $_SESSION['user_shifts']['end_day']), - 'end_time' => $_SESSION['user_shifts']['end_time'], - 'type_select' => make_select($types, $_SESSION['user_shifts']['types'], "types", _("Angeltypes") . '1'), - 'filled_select' => make_select($filled, $_SESSION['user_shifts']['filled'], "filled", _("Occupancy")), - 'task_notice' => '1' . _("The tasks shown here are influenced by the preferences you defined in your settings!") . " " . _("Description of the jobs.") . "", - 'new_style_checkbox' => '', + 'room_select' => make_select($rooms, $shiftsFilter->getRooms(), "rooms", _("Rooms")), + 'start_select' => html_select_key("start_day", "start_day", array_combine($days, $days), $start_day), + 'start_time' => $start_time, + 'end_select' => html_select_key("end_day", "end_day", array_combine($days, $days), $end_day), + 'end_time' => $end_time, + 'type_select' => make_select($types, $shiftsFilter->getTypes(), "types", _("Angeltypes") . '1'), + 'filled_select' => make_select($filled, $shiftsFilter->getFilled(), "filled", _("Occupancy")), + 'task_notice' => '1' . _("The tasks shown here are influenced by the angeltypes you joined already!") . " " . _("Description of the jobs.") . "", 'shifts_table' => msg() . $shifts_table, 'ical_text' => '

' . _("iCal export") . '

' . sprintf(_("Export of shown shifts. iCal format or JSON format available (please keep secret, otherwise reset the api key)."), page_link_to_absolute('ical') . '&key=' . $user['api_key'], page_link_to_absolute('shifts_json_export') . '&key=' . $user['api_key'], page_link_to('user_myshifts') . '&reset') . '

', 'filter' => _("Filter") @@ -568,23 +401,6 @@ function view_user_shifts() { ]); } -function make_user_shifts_export_link($page, $key) { - $link = "&start_day=" . $_SESSION['user_shifts']['start_day']; - $link = "&start_time=" . $_SESSION['user_shifts']['start_time']; - $link = "&end_day=" . $_SESSION['user_shifts']['end_day']; - $link = "&end_time=" . $_SESSION['user_shifts']['end_time']; - foreach ($_SESSION['user_shifts']['rooms'] as $room) { - $link .= '&rooms[]=' . $room; - } - foreach ($_SESSION['user_shifts']['types'] as $type) { - $link .= '&types[]=' . $type; - } - foreach ($_SESSION['user_shifts']['filled'] as $filled) { - $link .= '&filled[]=' . $filled; - } - return page_link_to_absolute($page) . $link . '&export=user_shifts&key=' . $key; -} - function get_ids_from_array($array) { return $array["id"]; } diff --git a/includes/sys_page.php b/includes/sys_page.php index e336261d..27e3e8ba 100644 --- a/includes/sys_page.php +++ b/includes/sys_page.php @@ -18,6 +18,19 @@ function raw_output($output) { die(); } +/** + * Returns an int[] from given request param name. + * + * @param String $name + * Name of the request param + */ +function check_request_int_array($name) { + if (isset($_REQUEST[$name]) && is_array($_REQUEST[$name])) { + return array_filter($_REQUEST[$name], 'is_numeric'); + } + return []; +} + /** * Checks if given request item (name) can be parsed to a date. * If not parsable, given error message is put into msg() and null is returned. diff --git a/templates/user_shifts.html b/templates/user_shifts.html index de98b37a..e4aba6ce 100644 --- a/templates/user_shifts.html +++ b/templates/user_shifts.html @@ -45,7 +45,6 @@
%task_notice%
-
%new_style_checkbox%
-- cgit v1.2.3-54-g00ecf From d59809d6d2aafaad335fc614895865bdd0710087 Mon Sep 17 00:00:00 2001 From: msquare Date: Sun, 2 Oct 2016 23:32:10 +0200 Subject: bring back session filter store --- includes/pages/user_shifts.php | 80 +++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 37 deletions(-) diff --git a/includes/pages/user_shifts.php b/includes/pages/user_shifts.php index d3ad0aa2..89ead0cf 100644 --- a/includes/pages/user_shifts.php +++ b/includes/pages/user_shifts.php @@ -34,48 +34,54 @@ function user_shifts() { * @return the updated ShiftsFilter */ function update_ShiftsFilter(ShiftsFilter $shiftsFilter, $user_shifts_admin, $days) { - global $privileges; - $shiftsFilter->setUserShiftsAdmin($user_shifts_admin); - $shiftsFilter->setFilled(check_request_int_array('filled')); - $shiftsFilter->setRooms(check_request_int_array('rooms')); - $shiftsFilter->setTypes(check_request_int_array('types')); - - $day = date('Y-m-d', time()); - $start_day = in_array($day, $days) ? $day : min($days); - if (isset($_REQUEST['start_day']) && in_array($_REQUEST['start_day'], $days)) { - $start_day = $_REQUEST['start_day']; + if (isset($_REQUEST['filled'])) { + $shiftsFilter->setFilled(check_request_int_array('filled')); } - - $start_time = date("H:i"); - if (isset($_REQUEST['start_time']) && preg_match('#^\d{1,2}:\d\d$#', $_REQUEST['start_time'])) { - $start_time = $_REQUEST['start_time']; + if (isset($_REQUEST['rooms'])) { + $shiftsFilter->setRooms(check_request_int_array('rooms')); } - - $day = date('Y-m-d', time() + 24 * 60 * 60); - $end_day = in_array($day, $days) ? $day : max($days); - if (isset($_REQUEST['end_day']) && in_array($_REQUEST['end_day'], $days)) { - $end_day = $_REQUEST['end_day']; + if (isset($_REQUEST['types'])) { + $shiftsFilter->setTypes(check_request_int_array('types')); } - - $end_time = date("H:i"); - if (isset($_REQUEST['end_time']) && preg_match('#^\d{1,2}:\d\d$#', $_REQUEST['end_time'])) { - $end_time = $_REQUEST['end_time']; - } - - if ($start_day > $end_day) { - $end_day = $start_day; - } - if ($start_day == $end_day && $start_time >= $end_time) { - $end_time = "23:59"; + + if ((isset($_REQUEST['start_time']) && isset($_REQUEST['start_day']) && isset($_REQUEST['end_time']) && isset($_REQUEST['end_day'])) || $shiftsFilter->getStartTime() == null || $shiftsFilter->getEndTime() == null) { + $day = date('Y-m-d', time()); + $start_day = in_array($day, $days) ? $day : min($days); + if (isset($_REQUEST['start_day']) && in_array($_REQUEST['start_day'], $days)) { + $start_day = $_REQUEST['start_day']; + } + + $start_time = date("H:i"); + if (isset($_REQUEST['start_time']) && preg_match('#^\d{1,2}:\d\d$#', $_REQUEST['start_time'])) { + $start_time = $_REQUEST['start_time']; + } + + $day = date('Y-m-d', time() + 24 * 60 * 60); + $end_day = in_array($day, $days) ? $day : max($days); + if (isset($_REQUEST['end_day']) && in_array($_REQUEST['end_day'], $days)) { + $end_day = $_REQUEST['end_day']; + } + + $end_time = date("H:i"); + if (isset($_REQUEST['end_time']) && preg_match('#^\d{1,2}:\d\d$#', $_REQUEST['end_time'])) { + $end_time = $_REQUEST['end_time']; + } + + if ($start_day > $end_day) { + $end_day = $start_day; + } + if ($start_day == $end_day && $start_time >= $end_time) { + $end_time = "23:59"; + } + + $startdatetime = DateTime::createFromFormat("Y-m-d H:i", $start_day . " " . $start_time); + $shiftsFilter->setStartTime($startdatetime->getTimestamp()); + + $enddatetime = DateTime::createFromFormat("Y-m-d H:i", $end_day . " " . $end_time); + $shiftsFilter->setEndTime($enddatetime->getTimestamp()); } - $startdatetime = DateTime::createFromFormat("Y-m-d H:i", $start_day . " " . $start_time); - $shiftsFilter->setStartTime($startdatetime->getTimestamp()); - - $enddatetime = DateTime::createFromFormat("Y-m-d H:i", $end_day . " " . $end_time); - $shiftsFilter->setEndTime($enddatetime->getTimestamp()); - return $shiftsFilter; } @@ -119,7 +125,7 @@ function view_user_shifts() { $type_ids = array_map('get_ids_from_array', $types); $_SESSION['ShiftsFilter'] = new ShiftsFilter(in_array('user_shifts_admin', $privileges), $room_ids, $type_ids); } - $_SESSION['ShiftsFilter'] = update_ShiftsFilter($_SESSION['ShiftsFilter'], in_array('user_shifts_admin', $privileges), $days); + update_ShiftsFilter($_SESSION['ShiftsFilter'], in_array('user_shifts_admin', $privileges), $days); $shiftsFilter = $_SESSION['ShiftsFilter']; $shifts = Shifts_by_ShiftsFilter($shiftsFilter, $user); -- cgit v1.2.3-54-g00ecf 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 +++++++++++++++++++++++ includes/model/ShiftsFilter.php | 112 ++++++++++++++++ includes/view/ShiftCalendarRenderer.php | 11 ++ 3 files changed, 284 insertions(+) create mode 100644 includes/controller/shift_entries_controller.php create mode 100644 includes/model/ShiftsFilter.php create mode 100644 includes/view/ShiftCalendarRenderer.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 diff --git a/includes/model/ShiftsFilter.php b/includes/model/ShiftsFilter.php new file mode 100644 index 00000000..044b32dd --- /dev/null +++ b/includes/model/ShiftsFilter.php @@ -0,0 +1,112 @@ +user_shifts_admin = $user_shifts_admin; + $this->rooms = $rooms; + $this->types = $types; + + $this->filled = [ + ShiftsFilter::FILLED_FREE + ]; + + if ($user_shifts_admin) { + $this->filled[] = ShiftsFilter::FILLED_FILLED; + } + } + + public function getStartTime() { + return $this->startTime; + } + + public function setStartTime($startTime) { + $this->startTime = $startTime; + } + + public function getEndTime() { + return $this->endTime; + } + + public function setEndTime($endTime) { + $this->endTime = $endTime; + } + + public function getTypes() { + if (count($this->types) == 0) { + return [ + 0 + ]; + } + return $this->types; + } + + public function setTypes($types) { + $this->types = $types; + } + + public function getRooms() { + if (count($this->rooms) == 0) { + return [ + 0 + ]; + } + return $this->rooms; + } + + public function setRooms($rooms) { + $this->rooms = $rooms; + } + + public function isUserShiftsAdmin() { + return $this->userShiftsAdmin; + } + + public function setUserShiftsAdmin($userShiftsAdmin) { + $this->userShiftsAdmin = $userShiftsAdmin; + } + + public function getFilled() { + return $this->filled; + } + + public function setFilled($filled) { + $this->filled = $filled; + } +} + +?> \ No newline at end of file diff --git a/includes/view/ShiftCalendarRenderer.php b/includes/view/ShiftCalendarRenderer.php new file mode 100644 index 00000000..8356d033 --- /dev/null +++ b/includes/view/ShiftCalendarRenderer.php @@ -0,0 +1,11 @@ + \ 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(+) 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 f3a0ce865deb9603b77adc9c9237a55cd4d87eeb Mon Sep 17 00:00:00 2001 From: msquare Date: Mon, 3 Oct 2016 18:32:25 +0200 Subject: move sql queries from shifts controller to model --- includes/controller/shifts_controller.php | 70 ++++++++----------------------- includes/model/NeededAngelTypes_model.php | 52 ++++++++++++++++++++++- includes/model/Room_model.php | 11 ++++- includes/pages/admin_rooms.php | 32 +++++++------- includes/sys_page.php | 18 ++++++++ 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)), '

' . _("Needed angels") . '

', - $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 @@ 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 @@ -18,6 +18,24 @@ function raw_output($output) { die(); } +/** + * 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. * -- cgit v1.2.3-54-g00ecf From 51d270db282be5cac9af2993a5c5b2c567d95506 Mon Sep 17 00:00:00 2001 From: msquare Date: Mon, 3 Oct 2016 19:37:00 +0200 Subject: restore ical export --- includes/pages/user_shifts.php | 4 +++- includes/view/AngelTypes_view.php | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/includes/pages/user_shifts.php b/includes/pages/user_shifts.php index 89ead0cf..9fd306c9 100644 --- a/includes/pages/user_shifts.php +++ b/includes/pages/user_shifts.php @@ -44,7 +44,7 @@ function update_ShiftsFilter(ShiftsFilter $shiftsFilter, $user_shifts_admin, $da if (isset($_REQUEST['types'])) { $shiftsFilter->setTypes(check_request_int_array('types')); } - + if ((isset($_REQUEST['start_time']) && isset($_REQUEST['start_day']) && isset($_REQUEST['end_time']) && isset($_REQUEST['end_day'])) || $shiftsFilter->getStartTime() == null || $shiftsFilter->getEndTime() == null) { $day = date('Y-m-d', time()); $start_day = in_array($day, $days) ? $day : min($days); @@ -352,6 +352,8 @@ function view_user_shifts() { $shifts_table .= ''; $shifts_table .= $shifts_row; $shifts_table .= ""; + // also output that shift on ical export + $ical_shifts[] = $shift; for ($j = 0; $j < $blocks && $i + $j < $maxshow; $j ++) { $todo[$rid][$i + $j] --; } diff --git a/includes/view/AngelTypes_view.php b/includes/view/AngelTypes_view.php index cdaa9f12..10c02745 100644 --- a/includes/view/AngelTypes_view.php +++ b/includes/view/AngelTypes_view.php @@ -96,7 +96,7 @@ function AngelType_view($angeltype, $members, $user_angeltype, $admin_user_angel $page = [ msg(), - buttons($buttons) + buttons($buttons) ]; $page[] = '

' . _("Description") . '

'; -- cgit v1.2.3-54-g00ecf From 455e41720068740c523bfc4b15cf30e3811e4281 Mon Sep 17 00:00:00 2001 From: msquare Date: Tue, 4 Oct 2016 16:16:00 +0200 Subject: fix variable naming problems --- includes/controller/shifts_controller.php | 8 ++++---- includes/pages/admin_rooms.php | 2 +- includes/pages/user_shifts.php | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php index aa1ac5d9..3300c0d2 100644 --- a/includes/controller/shifts_controller.php +++ b/includes/controller/shifts_controller.php @@ -88,12 +88,12 @@ function shift_edit_controller() { $msg .= error(_("The ending time has to be after the starting time."), true); } - foreach ($needed_angel_types_source as $type) { - if (isset($_REQUEST['type_' . $type['id']]) && preg_match("/^[0-9]+$/", trim($_REQUEST['type_' . $type['id']]))) { - $needed_angel_types[$type['id']] = trim($_REQUEST['type_' . $type['id']]); + foreach ($needed_angel_types as $needed_angeltype_id => $needed_angeltype_name) { + if (isset($_REQUEST['type_' . $needed_angeltype_id]) && test_request_int('type_' . $needed_angeltype_id)) { + $needed_angel_types[$needed_angeltype_id] = trim($_REQUEST['type_' . $needed_angeltype_id]); } else { $valid = false; - $msg .= error(sprintf(_("Please check your input for needed angels of type %s."), $type['name']), true); + $msg .= error(sprintf(_("Please check your input for needed angels of type %s."), $needed_angeltype_name), true); } } diff --git a/includes/pages/admin_rooms.php b/includes/pages/admin_rooms.php index 7641eb18..61c0a2c0 100644 --- a/includes/pages/admin_rooms.php +++ b/includes/pages/admin_rooms.php @@ -118,7 +118,7 @@ function admin_rooms() { engelsystem_error("Unable to load angeltype."); } if ($angeltype != null) { - NeededAngelType_add(null, $angeltype_id, $room_id, $count); + NeededAngelType_add(null, $angeltype_id, $room_id, $angeltype_count); $needed_angeltype_info[] = $angeltype['name'] . ": " . $angeltype_count; } } diff --git a/includes/pages/user_shifts.php b/includes/pages/user_shifts.php index 9fd306c9..ad211181 100644 --- a/includes/pages/user_shifts.php +++ b/includes/pages/user_shifts.php @@ -9,7 +9,7 @@ function shifts_title() { * Start different controllers for deleting shifts and shift_entries, edit shifts and add shift entries. */ function user_shifts() { - global $user, $privileges; + global $user; if (User_is_freeloader($user)) { redirect(page_link_to('user_myshifts')); -- cgit v1.2.3-54-g00ecf From eec10ebfc5c14ab9b72d3b81b7d44a2e507f5473 Mon Sep 17 00:00:00 2001 From: msquare Date: Tue, 4 Oct 2016 16:50:06 +0200 Subject: reduce complexity of user angeltypes controller --- includes/controller/angeltypes_controller.php | 15 ----- includes/controller/shifttypes_controller.php | 3 - includes/controller/user_angeltypes_controller.php | 76 ++-------------------- .../controller/user_driver_licenses_controller.php | 3 - includes/controller/users_controller.php | 6 +- includes/model/AngelType_model.php | 2 +- includes/model/Shifts_model.php | 3 - includes/model/UserAngelTypes_model.php | 36 +++++++--- includes/model/User_model.php | 8 ++- includes/pages/admin_news.php | 3 - includes/pages/admin_questions.php | 10 --- includes/pages/admin_rooms.php | 3 - includes/pages/admin_user.php | 3 - includes/pages/user_messages.php | 6 -- includes/pages/user_news.php | 6 -- includes/pages/user_questions.php | 3 - includes/view/Shifts_view.php | 3 - 17 files changed, 41 insertions(+), 148 deletions(-) diff --git a/includes/controller/angeltypes_controller.php b/includes/controller/angeltypes_controller.php index cb2bb2fb..cd938893 100644 --- a/includes/controller/angeltypes_controller.php +++ b/includes/controller/angeltypes_controller.php @@ -71,9 +71,6 @@ function angeltype_delete_controller() { } $angeltype = AngelType($_REQUEST['angeltype_id']); - if ($angeltype === false) { - engelsystem_error("Unable to load angeltype."); - } if ($angeltype == null) { redirect(page_link_to('angeltypes')); } @@ -109,9 +106,6 @@ function angeltype_edit_controller() { if (isset($_REQUEST['angeltype_id'])) { $angeltype = AngelType($_REQUEST['angeltype_id']); - if ($angeltype === false) { - engelsystem_error("Unable to load angeltype."); - } if ($angeltype == null) { redirect(page_link_to('angeltypes')); } @@ -196,17 +190,11 @@ function angeltype_controller() { } $angeltype = AngelType($_REQUEST['angeltype_id']); - if ($angeltype === false) { - engelsystem_error("Unable to load angeltype."); - } if ($angeltype == null) { redirect(page_link_to('angeltypes')); } $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype); - if ($user_angeltype === false) { - engelsystem_error("Unable to load user angeltype."); - } $user_driver_license = UserDriverLicense($user['UID']); if ($user_driver_license === false) { @@ -277,9 +265,6 @@ function load_angeltype() { } $angeltype = AngelType($_REQUEST['angeltype_id']); - if ($angeltype === false) { - engelsystem_error("Unable to load angeltype."); - } if ($angeltype == null) { error(_("Angeltype doesn't exist.")); redirect(page_link_to('angeltypes')); diff --git a/includes/controller/shifttypes_controller.php b/includes/controller/shifttypes_controller.php index 88feffed..5e547895 100644 --- a/includes/controller/shifttypes_controller.php +++ b/includes/controller/shifttypes_controller.php @@ -128,9 +128,6 @@ function shifttype_controller() { $angeltype = null; if ($shifttype['angeltype_id'] != null) { $angeltype = AngelType($shifttype['angeltype_id']); - if ($angeltype === false) { - engelsystem_error('Unable to load angeltype.'); - } } return [ diff --git a/includes/controller/user_angeltypes_controller.php b/includes/controller/user_angeltypes_controller.php index 418f21f9..663f7826 100644 --- a/includes/controller/user_angeltypes_controller.php +++ b/includes/controller/user_angeltypes_controller.php @@ -7,9 +7,6 @@ function user_angeltypes_unconfirmed_hint() { global $user; $unconfirmed_user_angeltypes = User_unconfirmed_AngelTypes($user); - if ($unconfirmed_user_angeltypes === false) { - engelsystem_error("Unable to load user angeltypes."); - } if (count($unconfirmed_user_angeltypes) == 0) { return ''; } @@ -34,9 +31,6 @@ function user_angeltypes_delete_all_controller() { } $angeltype = AngelType($_REQUEST['angeltype_id']); - if ($angeltype === false) { - engelsystem_error("Unable to load angeltype."); - } if ($angeltype == null) { error(_("Angeltype doesn't exist.")); redirect(page_link_to('angeltypes')); @@ -48,10 +42,7 @@ function user_angeltypes_delete_all_controller() { } if (isset($_REQUEST['confirmed'])) { - $result = UserAngelTypes_delete_all($angeltype['id']); - if ($result === false) { - engelsystem_error("Unable to confirm all users."); - } + UserAngelTypes_delete_all($angeltype['id']); engelsystem_log(sprintf("Denied all users for angeltype %s", AngelType_name_render($angeltype))); success(sprintf(_("Denied all users for angeltype %s."), AngelType_name_render($angeltype))); @@ -76,18 +67,12 @@ function user_angeltypes_confirm_all_controller() { } $angeltype = AngelType($_REQUEST['angeltype_id']); - if ($angeltype === false) { - engelsystem_error("Unable to load angeltype."); - } if ($angeltype == null) { error(_("Angeltype doesn't exist.")); redirect(page_link_to('angeltypes')); } $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype); - if ($user_angeltype === false) { - engelsystem_error("Unable to load user angeltype."); - } if ($user_angeltype == null) { error(_("User angeltype doesn't exist.")); redirect(page_link_to('angeltypes')); @@ -99,10 +84,7 @@ function user_angeltypes_confirm_all_controller() { } if (isset($_REQUEST['confirmed'])) { - $result = UserAngelTypes_confirm_all($angeltype['id'], $user); - if ($result === false) { - engelsystem_error("Unable to confirm all users."); - } + UserAngelTypes_confirm_all($angeltype['id'], $user); engelsystem_log(sprintf("Confirmed all users for angeltype %s", AngelType_name_render($angeltype))); success(sprintf(_("Confirmed all users for angeltype %s."), AngelType_name_render($angeltype))); @@ -127,18 +109,12 @@ function user_angeltype_confirm_controller() { } $user_angeltype = UserAngelType($_REQUEST['user_angeltype_id']); - if ($user_angeltype === false) { - engelsystem_error("Unable to load user angeltype."); - } if ($user_angeltype == null) { error(_("User angeltype doesn't exist.")); redirect(page_link_to('angeltypes')); } $angeltype = AngelType($user_angeltype['angeltype_id']); - if ($angeltype === false) { - engelsystem_error("Unable to load angeltype."); - } if ($angeltype == null) { error(_("Angeltype doesn't exist.")); redirect(page_link_to('angeltypes')); @@ -150,9 +126,6 @@ function user_angeltype_confirm_controller() { } $user_source = User($user_angeltype['user_id']); - if ($user_source === false) { - engelsystem_error("Unable to load user."); - } if ($user_source == null) { error(_("User doesn't exist.")); redirect(page_link_to('angeltypes')); @@ -187,27 +160,18 @@ function user_angeltype_delete_controller() { } $user_angeltype = UserAngelType($_REQUEST['user_angeltype_id']); - if ($user_angeltype === false) { - engelsystem_error("Unable to load user angeltype."); - } if ($user_angeltype == null) { error(_("User angeltype doesn't exist.")); redirect(page_link_to('angeltypes')); } $angeltype = AngelType($user_angeltype['angeltype_id']); - if ($angeltype === false) { - engelsystem_error("Unable to load angeltype."); - } if ($angeltype == null) { error(_("Angeltype doesn't exist.")); redirect(page_link_to('angeltypes')); } $user_source = User($user_angeltype['user_id']); - if ($user_source === false) { - engelsystem_error("Unable to load user."); - } if ($user_source == null) { error(_("User doesn't exist.")); redirect(page_link_to('angeltypes')); @@ -261,37 +225,25 @@ function user_angeltype_update_controller() { } $user_angeltype = UserAngelType($_REQUEST['user_angeltype_id']); - if ($user_angeltype === false) { - engelsystem_error("Unable to load user angeltype."); - } if ($user_angeltype == null) { error(_("User angeltype doesn't exist.")); redirect(page_link_to('angeltypes')); } $angeltype = AngelType($user_angeltype['angeltype_id']); - if ($angeltype === false) { - engelsystem_error("Unable to load angeltype."); - } if ($angeltype == null) { error(_("Angeltype doesn't exist.")); redirect(page_link_to('angeltypes')); } $user_source = User($user_angeltype['user_id']); - if ($user_source === false) { - engelsystem_error("Unable to load user."); - } if ($user_source == null) { error(_("User doesn't exist.")); redirect(page_link_to('angeltypes')); } if (isset($_REQUEST['confirmed'])) { - $result = UserAngelType_update($user_angeltype['id'], $coordinator); - if ($result === false) { - engelsystem_error("Unable to update coordinator rights."); - } + UserAngelType_update($user_angeltype['id'], $coordinator); $success_message = sprintf($coordinator ? _("Added coordinator rights for %s to %s.") : _("Removed coordinator rights for %s from %s."), AngelType_name_render($angeltype), User_Nick_render($user_source)); engelsystem_log($success_message); @@ -326,26 +278,17 @@ function user_angeltype_add_controller() { // Load possible users, that are not in the angeltype already $users_source = Users_by_angeltype_inverted($angeltype); - if ($users_source === false) { - engelsystem_error("Unable to load users."); - } if (isset($_REQUEST['submit'])) { $user_source = load_user(); if (! UserAngelType_exists($user_source, $angeltype)) { $user_angeltype_id = UserAngelType_create($user_source, $angeltype); - if ($user_angeltype_id === false) { - engelsystem_error("Unable to create user angeltype."); - } engelsystem_log(sprintf("User %s added to %s.", User_Nick_render($user_source), AngelType_name_render($angeltype))); success(sprintf(_("User %s added to %s."), User_Nick_render($user_source), AngelType_name_render($angeltype))); - $result = UserAngelType_confirm($user_angeltype_id, $user_source); - if ($result === false) { - engelsystem_error("Unable to confirm user angeltype."); - } + UserAngelType_confirm($user_angeltype_id, $user_source); engelsystem_log(sprintf("User %s confirmed as %s.", User_Nick_render($user), AngelType_name_render($angeltype))); redirect(page_link_to('angeltypes') . '&action=view&angeltype_id=' . $angeltype['id']); @@ -365,9 +308,6 @@ function user_angeltype_join_controller($angeltype) { global $user, $privileges; $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype); - if ($user_angeltype === false) { - engelsystem_error("Unable to load user angeltype."); - } if ($user_angeltype != null) { error(sprintf(_("You are already a %s."), $angeltype['name'])); redirect(page_link_to('angeltypes')); @@ -375,19 +315,13 @@ function user_angeltype_join_controller($angeltype) { if (isset($_REQUEST['confirmed'])) { $user_angeltype_id = UserAngelType_create($user, $angeltype); - if ($user_angeltype_id === false) { - engelsystem_error("Unable to create user angeltype."); - } $success_message = sprintf(_("You joined %s."), $angeltype['name']); engelsystem_log(sprintf("User %s joined %s.", User_Nick_render($user), AngelType_name_render($angeltype))); success($success_message); if (in_array('admin_user_angeltypes', $privileges)) { - $result = UserAngelType_confirm($user_angeltype_id, $user); - if ($result === false) { - engelsystem_error("Unable to confirm user angeltype."); - } + UserAngelType_confirm($user_angeltype_id, $user); engelsystem_log(sprintf("User %s confirmed as %s.", User_Nick_render($user), AngelType_name_render($angeltype))); } diff --git a/includes/controller/user_driver_licenses_controller.php b/includes/controller/user_driver_licenses_controller.php index 5c629777..88d25395 100644 --- a/includes/controller/user_driver_licenses_controller.php +++ b/includes/controller/user_driver_licenses_controller.php @@ -73,9 +73,6 @@ function user_driver_license_edit_controller() { if (isset($_REQUEST['user_id'])) { $user_source = User($_REQUEST['user_id']); - if ($user_source === false) { - engelsystem_error('Unable to load angeltype.'); - } if ($user_source == null) { redirect(user_driver_license_edit_link()); } diff --git a/includes/controller/users_controller.php b/includes/controller/users_controller.php index 9ead4bca..beaf2538 100644 --- a/includes/controller/users_controller.php +++ b/includes/controller/users_controller.php @@ -142,17 +142,13 @@ function user_edit_vouchers_controller() { function user_controller() { global $privileges, $user; + $user_source = $user; if (isset($_REQUEST['user_id'])) { $user_source = User($_REQUEST['user_id']); - if ($user_source === false) { - engelsystem_error("Unable to load user."); - } if ($user_source == null) { error(_("User not found.")); redirect('?'); } - } else { - $user_source = $user; } $shifts = Shifts_by_user($user_source); diff --git a/includes/model/AngelType_model.php b/includes/model/AngelType_model.php index 73b746f0..a87dd461 100644 --- a/includes/model/AngelType_model.php +++ b/includes/model/AngelType_model.php @@ -135,7 +135,7 @@ function AngelType_ids() { function AngelType($angeltype_id) { $angelType_source = sql_select("SELECT * FROM `AngelTypes` WHERE `id`='" . sql_escape($angeltype_id) . "' LIMIT 1"); if ($angelType_source === false) { - return false; + engelsystem_error("Unable to load angeltype."); } if (count($angelType_source) > 0) { return $angelType_source[0]; diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index b83d1a00..26dbbecb 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -98,9 +98,6 @@ function Shift_signup_allowed($shift, $angeltype, $user_angeltype = null, $user_ if ($user_angeltype == null) { $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype); - if ($user_angeltype === false) { - engelsystem_error('Unable to load user angeltype.'); - } } $signed_up = false; diff --git a/includes/model/UserAngelTypes_model.php b/includes/model/UserAngelTypes_model.php index fe5293f8..74b298cc 100644 --- a/includes/model/UserAngelTypes_model.php +++ b/includes/model/UserAngelTypes_model.php @@ -42,7 +42,7 @@ function User_angeltypes($user) { * @param User $user */ function User_unconfirmed_AngelTypes($user) { - return sql_select(" + $result = sql_select(" SELECT `UserAngelTypes`.*, `AngelTypes`.`name`, @@ -56,6 +56,10 @@ function User_unconfirmed_AngelTypes($user) { AND `UnconfirmedMembers`.`confirm_user_id` IS NULL GROUP BY `UserAngelTypes`.`angeltype_id` ORDER BY `AngelTypes`.`name`"); + if ($result === false) { + engelsystem_error("Unable to load user angeltypes."); + } + return $result; } /** @@ -81,11 +85,15 @@ function User_is_AngelType_coordinator($user, $angeltype) { * @param bool $coordinator */ function UserAngelType_update($user_angeltype_id, $coordinator) { - return sql_query(" + $result = sql_query(" UPDATE `UserAngelTypes` SET `coordinator`=" . sql_bool($coordinator) . " WHERE `id`='" . sql_escape($user_angeltype_id) . "' LIMIT 1"); + if ($result === false) { + engelsystem_error("Unable to update coordinator rights."); + } + return $result; } /** @@ -94,10 +102,14 @@ function UserAngelType_update($user_angeltype_id, $coordinator) { * @param int $angeltype_id */ function UserAngelTypes_delete_all($angeltype_id) { - return sql_query(" + $result = sql_query(" DELETE FROM `UserAngelTypes` WHERE `angeltype_id`='" . sql_escape($angeltype_id) . "' AND `confirm_user_id` IS NULL"); + if ($result === false) { + engelsystem_error("Unable to delete all unconfirmed users."); + } + return $result; } /** @@ -107,11 +119,15 @@ function UserAngelTypes_delete_all($angeltype_id) { * @param User $confirm_user */ function UserAngelTypes_confirm_all($angeltype_id, $confirm_user) { - return sql_query(" + $result = sql_query(" UPDATE `UserAngelTypes` SET `confirm_user_id`='" . sql_escape($confirm_user['UID']) . "' WHERE `angeltype_id`='" . sql_escape($angeltype_id) . "' AND `confirm_user_id` IS NULL"); + if ($result === false) { + engelsystem_error("Unable to confirm all users."); + } + return $result; } /** @@ -121,11 +137,15 @@ function UserAngelTypes_confirm_all($angeltype_id, $confirm_user) { * @param User $confirm_user */ function UserAngelType_confirm($user_angeltype_id, $confirm_user) { - return sql_query(" + $result = sql_query(" UPDATE `UserAngelTypes` SET `confirm_user_id`='" . sql_escape($confirm_user['UID']) . "' WHERE `id`='" . sql_escape($user_angeltype_id) . "' LIMIT 1"); + if ($result === false) { + engelsystem_error("Unable to confirm user angeltype."); + } + return $result; } /** @@ -152,7 +172,7 @@ function UserAngelType_create($user, $angeltype) { `user_id`='" . sql_escape($user['UID']) . "', `angeltype_id`='" . sql_escape($angeltype['id']) . "'"); if ($result === false) { - return false; + engelsystem_error("Unable to create user angeltype."); } return sql_id(); } @@ -169,7 +189,7 @@ function UserAngelType($user_angeltype_id) { WHERE `id`='" . sql_escape($user_angeltype_id) . "' LIMIT 1"); if ($angeltype === false) { - return false; + engelsystem_error("Unable to load user angeltype."); } if (count($angeltype) == 0) { return null; @@ -191,7 +211,7 @@ function UserAngelType_by_User_and_AngelType($user, $angeltype) { AND `angeltype_id`='" . sql_escape($angeltype['id']) . "' LIMIT 1"); if ($angeltype === false) { - return false; + engelsystem_error("Unable to load user angeltype."); } if (count($angeltype) == 0) { return null; diff --git a/includes/model/User_model.php b/includes/model/User_model.php index 576bb3f5..ee849f72 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -113,12 +113,16 @@ function User_is_freeloader($user) { * @param Angeltype $angeltype */ function Users_by_angeltype_inverted($angeltype) { - return sql_select(" + $result = sql_select(" SELECT `User`.* FROM `User` LEFT JOIN `UserAngelTypes` ON (`User`.`UID`=`UserAngelTypes`.`user_id` AND `angeltype_id`='" . sql_escape($angeltype['id']) . "') WHERE `UserAngelTypes`.`id` IS NULL ORDER BY `Nick`"); + if ($result === false) { + engelsystem_error("Unable to load users."); + } + return $result; } /** @@ -165,7 +169,7 @@ function User_validate_Nick($nick) { function User($user_id) { $user_source = sql_select("SELECT * FROM `User` WHERE `UID`='" . sql_escape($user_id) . "' LIMIT 1"); if ($user_source === false) { - return false; + engelsystem_error("Unable to load user."); } if (count($user_source) > 0) { return $user_source[0]; diff --git a/includes/pages/admin_news.php b/includes/pages/admin_news.php index 4226e6ba..553473eb 100644 --- a/includes/pages/admin_news.php +++ b/includes/pages/admin_news.php @@ -22,9 +22,6 @@ function admin_news() { list($news) = $news; $user_source = User($news['UID']); - if ($user_source === false) { - engelsystem_error("Unable to load user."); - } $html .= form([ form_info(_("Date"), date("Y-m-d H:i", $news['Datum'])), diff --git a/includes/pages/admin_questions.php b/includes/pages/admin_questions.php index 8e7507da..8c16255c 100644 --- a/includes/pages/admin_questions.php +++ b/includes/pages/admin_questions.php @@ -26,9 +26,6 @@ function admin_questions() { $questions = sql_select("SELECT * FROM `Questions` WHERE `AID` IS NULL"); foreach ($questions as $question) { $user_source = User($question['UID']); - if ($user_source === false) { - engelsystem_error("Unable to load user."); - } $unanswered_questions_table[] = [ 'from' => User_Nick_render($user_source), @@ -45,14 +42,7 @@ function admin_questions() { $questions = sql_select("SELECT * FROM `Questions` WHERE NOT `AID` IS NULL"); foreach ($questions as $question) { $user_source = User($question['UID']); - if ($user_source === false) { - engelsystem_error("Unable to load user."); - } - $answer_user_source = User($question['AID']); - if ($answer_user_source === false) { - engelsystem_error("Unable to load user."); - } $answered_questions_table[] = [ 'from' => User_Nick_render($user_source), 'question' => str_replace("\n", "
", $question['Question']), diff --git a/includes/pages/admin_rooms.php b/includes/pages/admin_rooms.php index 61c0a2c0..186a676e 100644 --- a/includes/pages/admin_rooms.php +++ b/includes/pages/admin_rooms.php @@ -114,9 +114,6 @@ function admin_rooms() { $needed_angeltype_info = []; foreach ($angeltypes_count as $angeltype_id => $angeltype_count) { $angeltype = AngelType($angeltype_id); - if ($angeltype === false) { - engelsystem_error("Unable to load angeltype."); - } if ($angeltype != null) { NeededAngelType_add(null, $angeltype_id, $room_id, $angeltype_count); $needed_angeltype_info[] = $angeltype['name'] . ": " . $angeltype_count; diff --git a/includes/pages/admin_user.php b/includes/pages/admin_user.php index 56987e12..ef4eb2de 100644 --- a/includes/pages/admin_user.php +++ b/includes/pages/admin_user.php @@ -16,9 +16,6 @@ function admin_user() { $user_id = $_REQUEST['id']; if (! isset($_REQUEST['action'])) { $user_source = User($user_id); - if ($user_source === false) { - engelsystem_error('Unable to load user.'); - } if ($user_source == null) { error(_('This user does not exist.')); redirect(users_link()); diff --git a/includes/pages/user_messages.php b/includes/pages/user_messages.php index 4af2a0cb..eb07deea 100644 --- a/includes/pages/user_messages.php +++ b/includes/pages/user_messages.php @@ -47,13 +47,7 @@ function user_messages() { foreach ($messages as $message) { $sender_user_source = User($message['SUID']); - if ($sender_user_source === false) { - engelsystem_error(_("Unable to load user.")); - } $receiver_user_source = User($message['RUID']); - if ($receiver_user_source === false) { - engelsystem_error(_("Unable to load user.")); - } $messages_table_entry = [ 'new' => $message['isRead'] == 'N' ? '' : '', diff --git a/includes/pages/user_news.php b/includes/pages/user_news.php index c5791134..97f7ec83 100644 --- a/includes/pages/user_news.php +++ b/includes/pages/user_news.php @@ -62,9 +62,6 @@ function display_news($news) { $html .= ' ' . date("Y-m-d H:i", $news['Datum']) . ' '; $user_source = User($news['UID']); - if ($user_source === false) { - engelsystem_error(_("Unable to load user.")); - } $html .= User_Nick_render($user_source); if ($page != "news_comments") { @@ -94,9 +91,6 @@ function user_news_comments() { $comments = sql_select("SELECT * FROM `NewsComments` WHERE `Refid`='" . sql_escape($nid) . "' ORDER BY 'ID'"); foreach ($comments as $comment) { $user_source = User($comment['UID']); - if ($user_source === false) { - engelsystem_error(_("Unable to load user.")); - } $html .= '
'; $html .= '
' . nl2br($comment['Text']) . '
'; diff --git a/includes/pages/user_questions.php b/includes/pages/user_questions.php index 10e8fef6..7acdee78 100644 --- a/includes/pages/user_questions.php +++ b/includes/pages/user_questions.php @@ -13,9 +13,6 @@ function user_questions() { $answered_questions = sql_select("SELECT * FROM `Questions` WHERE NOT `AID` IS NULL AND `UID`='" . sql_escape($user['UID']) . "'"); foreach ($answered_questions as &$question) { $answer_user_source = User($question['AID']); - if ($answer_user_source === false) { - engelsystem_error(_("Unable to load user.")); - } $question['answer_user'] = User_Nick_render($answer_user_source); } diff --git a/includes/view/Shifts_view.php b/includes/view/Shifts_view.php index 95282eb5..1b910f5c 100644 --- a/includes/view/Shifts_view.php +++ b/includes/view/Shifts_view.php @@ -16,9 +16,6 @@ function Shift_signup_button_render($shift, $angeltype, $user_angeltype = null, if ($user_angeltype == null) { $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype); - if ($user_angeltype === false) { - engelsystem_error('Unable to load user angeltype.'); - } } if (Shift_signup_allowed($shift, $angeltype, $user_angeltype, $user_shifts)) { -- cgit v1.2.3-54-g00ecf From 42144ed21cbc7361857b375d719ec53b24314546 Mon Sep 17 00:00:00 2001 From: msquare Date: Tue, 4 Oct 2016 17:58:56 +0200 Subject: move static access to datetime parse function --- includes/controller/shifts_controller.php | 8 ++++---- includes/pages/admin_import.php | 4 ++-- includes/pages/admin_shifts.php | 14 +++++++------- includes/pages/guest_login.php | 4 ++-- includes/pages/user_settings.php | 8 ++++---- includes/pages/user_shifts.php | 7 ++----- includes/sys_page.php | 30 ++++++++++++++++++++++++++---- 7 files changed, 47 insertions(+), 28 deletions(-) diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php index 3300c0d2..ca6fd906 100644 --- a/includes/controller/shifts_controller.php +++ b/includes/controller/shifts_controller.php @@ -69,15 +69,15 @@ function shift_edit_controller() { $msg .= error(_('Please select a shifttype.'), true); } - if (isset($_REQUEST['start']) && $tmp = DateTime::createFromFormat("Y-m-d H:i", trim($_REQUEST['start']))) { - $start = $tmp->getTimestamp(); + if (isset($_REQUEST['start']) && $tmp = parse_date("Y-m-d H:i", $_REQUEST['start'])) { + $start = $tmp; } else { $valid = false; $msg .= error(_("Please enter a valid starting time for the shifts."), true); } - if (isset($_REQUEST['end']) && $tmp = DateTime::createFromFormat("Y-m-d H:i", trim($_REQUEST['end']))) { - $end = $tmp->getTimestamp(); + if (isset($_REQUEST['end']) && $tmp = parse_date("Y-m-d H:i", $_REQUEST['end'])) { + $end = $tmp; } else { $valid = false; $msg .= error(_("Please enter a valid ending time for the shifts."), true); diff --git a/includes/pages/admin_import.php b/includes/pages/admin_import.php index 2c36e681..4af09dca 100644 --- a/includes/pages/admin_import.php +++ b/includes/pages/admin_import.php @@ -312,8 +312,8 @@ function prepare_events($file, $shifttype_id, $add_minutes_start, $add_minutes_e 'event-id' }); $shifts_pb[$event_id] = [ 'shifttype_id' => $shifttype_id, - 'start' => DateTime::createFromFormat("Ymd\THis", $event->dtstart)->getTimestamp() - $add_minutes_start * 60, - 'end' => DateTime::createFromFormat("Ymd\THis", $event->dtend)->getTimestamp() + $add_minutes_end * 60, + 'start' => parse_date("Ymd\THis", $event->dtstart) - $add_minutes_start * 60, + 'end' => parse_date("Ymd\THis", $event->dtend) + $add_minutes_end * 60, 'RID' => $rooms_import[trim($event->location)], 'title' => trim($event->summary), 'URL' => trim($event->url), diff --git a/includes/pages/admin_shifts.php b/includes/pages/admin_shifts.php index 3dd22f3a..42a8c682 100644 --- a/includes/pages/admin_shifts.php +++ b/includes/pages/admin_shifts.php @@ -9,7 +9,7 @@ function admin_shifts() { $valid = true; $rid = 0; - $start = DateTime::createFromFormat("Y-m-d H:i", date("Y-m-d") . " 00:00")->getTimestamp(); + $start = parse_date("Y-m-d H:i", date("Y-m-d") . " 00:00"); $end = $start; $mode = 'single'; $angelmode = 'manually'; @@ -71,15 +71,15 @@ function admin_shifts() { error(_('Please select a location.')); } - if (isset($_REQUEST['start']) && $tmp = DateTime::createFromFormat("Y-m-d H:i", trim($_REQUEST['start']))) { - $start = $tmp->getTimestamp(); + if (isset($_REQUEST['start']) && $tmp = parse_date("Y-m-d H:i", $_REQUEST['start'])) { + $start = $tmp; } else { $valid = false; error(_('Please select a start time.')); } - if (isset($_REQUEST['end']) && $tmp = DateTime::createFromFormat("Y-m-d H:i", trim($_REQUEST['end']))) { - $end = $tmp->getTimestamp(); + if (isset($_REQUEST['end']) && $tmp = parse_date("Y-m-d H:i", $_REQUEST['end'])) { + $end = $tmp; } else { $valid = false; error(_('Please select an end time.')); @@ -188,7 +188,7 @@ function admin_shifts() { } while ($shift_end < $end); } elseif ($mode == 'variable') { rsort($change_hours); - $day = DateTime::createFromFormat("Y-m-d H:i", date("Y-m-d", $start) . " 00:00")->getTimestamp(); + $day = parse_date("Y-m-d H:i", date("Y-m-d", $start) . " 00:00"); $change_index = 0; // Ersten/nächsten passenden Schichtwechsel suchen foreach ($change_hours as $i => $change_hour) { @@ -205,7 +205,7 @@ function admin_shifts() { $shift_start = $start; do { - $day = DateTime::createFromFormat("Y-m-d H:i", date("Y-m-d", $shift_start) . " 00:00")->getTimestamp(); + $day = parse_date("Y-m-d H:i", date("Y-m-d", $shift_start) . " 00:00"); $shift_end = $day + $change_hours[$change_index] * 60 * 60; if ($shift_end > $end) { diff --git a/includes/pages/guest_login.php b/includes/pages/guest_login.php index 1f1b7ab2..9f688321 100644 --- a/includes/pages/guest_login.php +++ b/includes/pages/guest_login.php @@ -104,8 +104,8 @@ function guest_register() { $msg .= error(sprintf(_("Your password is too short (please use at least %s characters)."), MIN_PASSWORD_LENGTH), true); } - if (isset($_REQUEST['planned_arrival_date']) && DateTime::createFromFormat("Y-m-d", trim($_REQUEST['planned_arrival_date']))) { - $planned_arrival_date = DateTime::createFromFormat("Y-m-d", trim($_REQUEST['planned_arrival_date']))->getTimestamp(); + if (isset($_REQUEST['planned_arrival_date']) && $tmp = parse_date("Y-m-d", $_REQUEST['planned_arrival_date'])) { + $planned_arrival_date = $tmp; } else { $valid = false; $msg .= error(_("Please enter your planned date of arrival."), true); diff --git a/includes/pages/user_settings.php b/includes/pages/user_settings.php index b2ea5752..22ead68b 100644 --- a/includes/pages/user_settings.php +++ b/includes/pages/user_settings.php @@ -56,16 +56,16 @@ function user_settings() { $valid = false; } - if (isset($_REQUEST['planned_arrival_date']) && DateTime::createFromFormat("Y-m-d", trim($_REQUEST['planned_arrival_date']))) { - $planned_arrival_date = DateTime::createFromFormat("Y-m-d", trim($_REQUEST['planned_arrival_date']))->getTimestamp(); + if (isset($_REQUEST['planned_arrival_date']) && $tmp = parse_date("Y-m-d", $_REQUEST['planned_arrival_date'])) { + $planned_arrival_date = $tmp; } else { $valid = false; $msg .= error(_("Please enter your planned date of arrival."), true); } if (isset($_REQUEST['planned_departure_date']) && $_REQUEST['planned_departure_date'] != '') { - if (DateTime::createFromFormat("Y-m-d", trim($_REQUEST['planned_departure_date']))) { - $planned_departure_date = DateTime::createFromFormat("Y-m-d", trim($_REQUEST['planned_departure_date']))->getTimestamp(); + if ($tmp = parse_date("Y-m-d", $_REQUEST['planned_departure_date'])) { + $planned_departure_date = $tmp; } else { $valid = false; $msg .= error(_("Please enter your planned date of departure."), true); diff --git a/includes/pages/user_shifts.php b/includes/pages/user_shifts.php index ad211181..8b6dd1fc 100644 --- a/includes/pages/user_shifts.php +++ b/includes/pages/user_shifts.php @@ -75,11 +75,8 @@ function update_ShiftsFilter(ShiftsFilter $shiftsFilter, $user_shifts_admin, $da $end_time = "23:59"; } - $startdatetime = DateTime::createFromFormat("Y-m-d H:i", $start_day . " " . $start_time); - $shiftsFilter->setStartTime($startdatetime->getTimestamp()); - - $enddatetime = DateTime::createFromFormat("Y-m-d H:i", $end_day . " " . $end_time); - $shiftsFilter->setEndTime($enddatetime->getTimestamp()); + $shiftsFilter->setStartTime(parse_date("Y-m-d H:i", $start_day . " " . $start_time)); + $shiftsFilter->setEndTime(parse_date("Y-m-d H:i", $end_day . " " . $end_time)); } return $shiftsFilter; diff --git a/includes/sys_page.php b/includes/sys_page.php index e62909ab..f20a791c 100644 --- a/includes/sys_page.php +++ b/includes/sys_page.php @@ -1,5 +1,26 @@ getTimestamp(); +} + /** * Leitet den Browser an die übergebene URL weiter und hält das Script an. */ @@ -11,7 +32,8 @@ function redirect($url) { /** * Echoes given output and dies. * - * @param String $output + * @param String $output + * String to display */ function raw_output($output) { echo $output; @@ -20,7 +42,7 @@ 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 @@ -81,8 +103,8 @@ function check_request_date($name, $error_message = null, $null_allowed = false) * @return ValidationResult containing the parsed date */ function check_date($input, $error_message = null, $null_allowed = false) { - if (DateTime::createFromFormat("Y-m-d", trim($input))) { - return new ValidationResult(true, DateTime::createFromFormat("Y-m-d", trim($input))->getTimestamp()); + if ($tmp = parse_date("Y-m-d", trim($input))) { + return new ValidationResult(true, $tmp); } if ($null_allowed) { return new ValidationResult(true, null); -- cgit v1.2.3-54-g00ecf From 676f5e1627a64b770da0d6a1ce65aacf34335214 Mon Sep 17 00:00:00 2001 From: msquare Date: Tue, 4 Oct 2016 18:11:26 +0200 Subject: small code style improvements --- includes/pages/admin_news.php | 97 +++++++++++++++++++++---------------------- includes/sys_template.php | 6 +-- 2 files changed, 51 insertions(+), 52 deletions(-) diff --git a/includes/pages/admin_news.php b/includes/pages/admin_news.php index 553473eb..789fc728 100644 --- a/includes/pages/admin_news.php +++ b/includes/pages/admin_news.php @@ -5,63 +5,62 @@ function admin_news() { if (! isset($_GET["action"])) { redirect(page_link_to("news")); + } + + $html = '

' . _("Edit news entry") . '

' . msg(); + if (isset($_REQUEST['id']) && preg_match("/^[0-9]{1,11}$/", $_REQUEST['id'])) { + $news_id = $_REQUEST['id']; } else { - $html = '

' . _("Edit news entry") . '

' . msg(); - if (isset($_REQUEST['id']) && preg_match("/^[0-9]{1,11}$/", $_REQUEST['id'])) { - $news_id = $_REQUEST['id']; - } else { - return error("Incomplete call, missing News ID.", true); - } + return error("Incomplete call, missing News ID.", true); + } + + $news = sql_select("SELECT * FROM `News` WHERE `ID`='" . sql_escape($news_id) . "' LIMIT 1"); + if (empty($news)) { + return error("No News found.", true); + } + switch ($_REQUEST["action"]) { + default: + redirect(page_link_to('news')); + case 'edit': + list($news) = $news; + + $user_source = User($news['UID']); + + $html .= form([ + form_info(_("Date"), date("Y-m-d H:i", $news['Datum'])), + form_info(_("Author"), User_Nick_render($user_source)), + form_text('eBetreff', _("Subject"), $news['Betreff']), + form_textarea('eText', _("Message"), $news['Text']), + form_checkbox('eTreffen', _("Meeting"), $news['Treffen'] == 1, 1), + form_submit('submit', _("Save")) + ], page_link_to('admin_news&action=save&id=' . $news_id)); + + $html .= ' ' . _("Delete") . ''; + break; - $news = sql_select("SELECT * FROM `News` WHERE `ID`='" . sql_escape($news_id) . "' LIMIT 1"); - if (count($news) > 0) { - switch ($_REQUEST["action"]) { - default: - redirect(page_link_to('news')); - case 'edit': - list($news) = $news; - - $user_source = User($news['UID']); - - $html .= form([ - form_info(_("Date"), date("Y-m-d H:i", $news['Datum'])), - form_info(_("Author"), User_Nick_render($user_source)), - form_text('eBetreff', _("Subject"), $news['Betreff']), - form_textarea('eText', _("Message"), $news['Text']), - form_checkbox('eTreffen', _("Meeting"), $news['Treffen'] == 1, 1), - form_submit('submit', _("Save")) - ], page_link_to('admin_news&action=save&id=' . $news_id)); - - $html .= ' ' . _("Delete") . ''; - break; - - case 'save': - list($news) = $news; - - sql_query("UPDATE `News` SET + case 'save': + list($news) = $news; + + sql_query("UPDATE `News` SET `Datum`='" . sql_escape(time()) . "', `Betreff`='" . sql_escape($_POST["eBetreff"]) . "', `Text`='" . sql_escape($_POST["eText"]) . "', `UID`='" . sql_escape($user['UID']) . "', `Treffen`='" . sql_escape($_POST["eTreffen"]) . "' WHERE `ID`='" . sql_escape($news_id) . "'"); - engelsystem_log("News updated: " . $_POST["eBetreff"]); - success(_("News entry updated.")); - redirect(page_link_to("news")); - break; - - case 'delete': - list($news) = $news; - - sql_query("DELETE FROM `News` WHERE `ID`='" . sql_escape($news_id) . "' LIMIT 1"); - engelsystem_log("News deleted: " . $news['Betreff']); - success(_("News entry deleted.")); - redirect(page_link_to("news")); - break; - } - } else { - return error("No News found.", true); - } + engelsystem_log("News updated: " . $_POST["eBetreff"]); + success(_("News entry updated.")); + redirect(page_link_to("news")); + break; + + case 'delete': + list($news) = $news; + + sql_query("DELETE FROM `News` WHERE `ID`='" . sql_escape($news_id) . "' LIMIT 1"); + engelsystem_log("News deleted: " . $news['Betreff']); + success(_("News entry deleted.")); + redirect(page_link_to("news")); + break; } return $html . '
'; } diff --git a/includes/sys_template.php b/includes/sys_template.php index 2faf98c3..5b6caade 100644 --- a/includes/sys_template.php +++ b/includes/sys_template.php @@ -374,11 +374,11 @@ function render_table($columns, $rows, $data = true) { foreach ($rows as $row) { $html .= ''; foreach ($columns as $key => $column) { + $value = " "; if (isset($row[$key])) { - $html .= '' . $row[$key] . ''; - } else { - $html .= ' '; + $value = $row[$key]; } + $html .= '' . $value . ''; } $html .= ''; } -- cgit v1.2.3-54-g00ecf From aa628208ec6b4f484b37df44b3126fb28ab3f68e Mon Sep 17 00:00:00 2001 From: msquare Date: Tue, 4 Oct 2016 18:36:57 +0200 Subject: reduce complexity of shift view --- includes/model/Shifts_model.php | 6 +- includes/pages/user_shifts.php | 135 +++++++++++++++++++++++----------------- 2 files changed, 82 insertions(+), 59 deletions(-) diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index 26dbbecb..52ced0fe 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -57,7 +57,11 @@ function Shifts_by_ShiftsFilter(ShiftsFilter $shiftsFilter, $user) { $SQL .= " ORDER BY `start`"; - return sql_select($SQL); + $result = sql_select($SQL); + if ($result === false) { + engelsystem_error("Unable to load shifts by filter."); + } + return $result; } /** diff --git a/includes/pages/user_shifts.php b/includes/pages/user_shifts.php index 8b6dd1fc..d13f9701 100644 --- a/includes/pages/user_shifts.php +++ b/includes/pages/user_shifts.php @@ -28,10 +28,56 @@ function user_shifts() { return view_user_shifts(); } +/** + * Helper function that updates the start and end time from request data. + * Use update_ShiftsFilter(). + * + * @param ShiftsFilter $shiftsFilter + * The shiftfilter to update. + */ +function update_ShiftsFilter_timerange(ShiftsFilter $shiftsFilter) { + $day = date('Y-m-d', time()); + $start_day = in_array($day, $days) ? $day : min($days); + if (isset($_REQUEST['start_day']) && in_array($_REQUEST['start_day'], $days)) { + $start_day = $_REQUEST['start_day']; + } + + $start_time = date("H:i"); + if (isset($_REQUEST['start_time']) && preg_match('#^\d{1,2}:\d\d$#', $_REQUEST['start_time'])) { + $start_time = $_REQUEST['start_time']; + } + + $day = date('Y-m-d', time() + 24 * 60 * 60); + $end_day = in_array($day, $days) ? $day : max($days); + if (isset($_REQUEST['end_day']) && in_array($_REQUEST['end_day'], $days)) { + $end_day = $_REQUEST['end_day']; + } + + $end_time = date("H:i"); + if (isset($_REQUEST['end_time']) && preg_match('#^\d{1,2}:\d\d$#', $_REQUEST['end_time'])) { + $end_time = $_REQUEST['end_time']; + } + + if ($start_day > $end_day) { + $end_day = $start_day; + } + if ($start_day == $end_day && $start_time >= $end_time) { + $end_time = "23:59"; + } + + $shiftsFilter->setStartTime(parse_date("Y-m-d H:i", $start_day . " " . $start_time)); + $shiftsFilter->setEndTime(parse_date("Y-m-d H:i", $end_day . " " . $end_time)); +} + /** * Update given ShiftsFilter with filter params from user input * - * @return the updated ShiftsFilter + * @param ShiftsFilter $shiftsFilter + * The shifts filter to update from request data + * @param boolean $user_shifts_admin + * Has the user user_shift_admin privilege? + * @param string[] $days + * An array of available filter days */ function update_ShiftsFilter(ShiftsFilter $shiftsFilter, $user_shifts_admin, $days) { $shiftsFilter->setUserShiftsAdmin($user_shifts_admin); @@ -44,78 +90,54 @@ function update_ShiftsFilter(ShiftsFilter $shiftsFilter, $user_shifts_admin, $da if (isset($_REQUEST['types'])) { $shiftsFilter->setTypes(check_request_int_array('types')); } - if ((isset($_REQUEST['start_time']) && isset($_REQUEST['start_day']) && isset($_REQUEST['end_time']) && isset($_REQUEST['end_day'])) || $shiftsFilter->getStartTime() == null || $shiftsFilter->getEndTime() == null) { - $day = date('Y-m-d', time()); - $start_day = in_array($day, $days) ? $day : min($days); - if (isset($_REQUEST['start_day']) && in_array($_REQUEST['start_day'], $days)) { - $start_day = $_REQUEST['start_day']; - } - - $start_time = date("H:i"); - if (isset($_REQUEST['start_time']) && preg_match('#^\d{1,2}:\d\d$#', $_REQUEST['start_time'])) { - $start_time = $_REQUEST['start_time']; - } - - $day = date('Y-m-d', time() + 24 * 60 * 60); - $end_day = in_array($day, $days) ? $day : max($days); - if (isset($_REQUEST['end_day']) && in_array($_REQUEST['end_day'], $days)) { - $end_day = $_REQUEST['end_day']; - } - - $end_time = date("H:i"); - if (isset($_REQUEST['end_time']) && preg_match('#^\d{1,2}:\d\d$#', $_REQUEST['end_time'])) { - $end_time = $_REQUEST['end_time']; - } - - if ($start_day > $end_day) { - $end_day = $start_day; - } - if ($start_day == $end_day && $start_time >= $end_time) { - $end_time = "23:59"; - } - - $shiftsFilter->setStartTime(parse_date("Y-m-d H:i", $start_day . " " . $start_time)); - $shiftsFilter->setEndTime(parse_date("Y-m-d H:i", $end_day . " " . $end_time)); + update_ShiftsFilter_timerange($shiftsFilter); } - - return $shiftsFilter; } -function view_user_shifts() { - global $user, $privileges; - global $ical_shifts; - - $ical_shifts = []; +function load_rooms() { + $rooms = sql_select("SELECT `RID` AS `id`, `Name` AS `name` FROM `Room` WHERE `show`='Y' ORDER BY `Name`"); + if (count($rooms) == 0) { + error(_("The administration has not configured any rooms yet.")); + redirect('?'); + } + return $rooms; +} + +function load_days() { $days = sql_select_single_col(" SELECT DISTINCT DATE(FROM_UNIXTIME(`start`)) AS `id`, DATE(FROM_UNIXTIME(`start`)) AS `name` FROM `Shifts` ORDER BY `start`"); - if (count($days) == 0) { error(_("The administration has not configured any shifts yet.")); redirect('?'); } + return $days; +} + +function load_types() { + global $user; - $rooms = sql_select("SELECT `RID` AS `id`, `Name` AS `name` FROM `Room` WHERE `show`='Y' ORDER BY `Name`"); - if (count($rooms) == 0) { - error(_("The administration has not configured any rooms yet.")); + if (sql_num_query("SELECT `id`, `name` FROM `AngelTypes` WHERE `restricted` = 0") == 0) { + error(_("The administration has not configured any angeltypes yet - or you are not subscribed to any angeltype.")); redirect('?'); } - - if (in_array('user_shifts_admin', $privileges)) { - $types = sql_select("SELECT `id`, `name` FROM `AngelTypes` ORDER BY `AngelTypes`.`name`"); - } else { - $types = sql_select("SELECT `AngelTypes`.`id`, `AngelTypes`.`name`, (`AngelTypes`.`restricted`=0 OR (NOT `UserAngelTypes`.`confirm_user_id` IS NULL OR `UserAngelTypes`.`id` IS NULL)) as `enabled` FROM `AngelTypes` LEFT JOIN `UserAngelTypes` ON (`UserAngelTypes`.`angeltype_id`=`AngelTypes`.`id` AND `UserAngelTypes`.`user_id`='" . sql_escape($user['UID']) . "') ORDER BY `AngelTypes`.`name`"); - } + $types = sql_select("SELECT `AngelTypes`.`id`, `AngelTypes`.`name`, (`AngelTypes`.`restricted`=0 OR (NOT `UserAngelTypes`.`confirm_user_id` IS NULL OR `UserAngelTypes`.`id` IS NULL)) as `enabled` FROM `AngelTypes` LEFT JOIN `UserAngelTypes` ON (`UserAngelTypes`.`angeltype_id`=`AngelTypes`.`id` AND `UserAngelTypes`.`user_id`='" . sql_escape($user['UID']) . "') ORDER BY `AngelTypes`.`name`"); if (empty($types)) { - $types = sql_select("SELECT `id`, `name` FROM `AngelTypes` WHERE `restricted` = 0"); + return sql_select("SELECT `id`, `name` FROM `AngelTypes` WHERE `restricted` = 0"); } + return $types; +} + +function view_user_shifts() { + global $user, $privileges; + global $ical_shifts; - if (count($types) == 0) { - error(_("The administration has not configured any angeltypes yet - or you are not subscribed to any angeltype.")); - redirect('?'); - } + $ical_shifts = []; + $days = load_days(); + $rooms = load_rooms(); + $types = load_types(); if (! isset($_SESSION['ShiftsFilter'])) { $room_ids = array_map('get_ids_from_array', $rooms); @@ -126,9 +148,6 @@ function view_user_shifts() { $shiftsFilter = $_SESSION['ShiftsFilter']; $shifts = Shifts_by_ShiftsFilter($shiftsFilter, $user); - if ($shifts === false) { - engelsystem_error("Unable to load shifts by filter."); - } $ownshifts_source = sql_select(" SELECT `ShiftTypes`.`name`, `Shifts`.* -- cgit v1.2.3-54-g00ecf From a50b26490cf3c76d6dae293cb34b4cbff01133b6 Mon Sep 17 00:00:00 2001 From: msquare Date: Tue, 4 Oct 2016 18:52:52 +0200 Subject: reduce complexity of shiftsfilter update --- includes/pages/user_shifts.php | 37 ++++++------------------------------- includes/sys_page.php | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/includes/pages/user_shifts.php b/includes/pages/user_shifts.php index d13f9701..d415e5f5 100644 --- a/includes/pages/user_shifts.php +++ b/includes/pages/user_shifts.php @@ -35,38 +35,13 @@ function user_shifts() { * @param ShiftsFilter $shiftsFilter * The shiftfilter to update. */ -function update_ShiftsFilter_timerange(ShiftsFilter $shiftsFilter) { - $day = date('Y-m-d', time()); - $start_day = in_array($day, $days) ? $day : min($days); - if (isset($_REQUEST['start_day']) && in_array($_REQUEST['start_day'], $days)) { - $start_day = $_REQUEST['start_day']; - } - - $start_time = date("H:i"); - if (isset($_REQUEST['start_time']) && preg_match('#^\d{1,2}:\d\d$#', $_REQUEST['start_time'])) { - $start_time = $_REQUEST['start_time']; - } - - $day = date('Y-m-d', time() + 24 * 60 * 60); - $end_day = in_array($day, $days) ? $day : max($days); - if (isset($_REQUEST['end_day']) && in_array($_REQUEST['end_day'], $days)) { - $end_day = $_REQUEST['end_day']; - } +function update_ShiftsFilter_timerange(ShiftsFilter $shiftsFilter, $days) { + $shiftsFilter->setStartTime(check_request_datetime('start_day', 'start_time', $days, time())); + $shiftsFilter->setEndTime(check_request_datetime('end_day', 'end_time', $days, time() + 24 * 60 * 60)); - $end_time = date("H:i"); - if (isset($_REQUEST['end_time']) && preg_match('#^\d{1,2}:\d\d$#', $_REQUEST['end_time'])) { - $end_time = $_REQUEST['end_time']; + if ($shiftsFilter->getStartTime() > $shiftsFilter->getEndTime()) { + $shiftsFilter->setEndTime($shiftsFilter->getStartTime() + 24 * 60 * 60); } - - if ($start_day > $end_day) { - $end_day = $start_day; - } - if ($start_day == $end_day && $start_time >= $end_time) { - $end_time = "23:59"; - } - - $shiftsFilter->setStartTime(parse_date("Y-m-d H:i", $start_day . " " . $start_time)); - $shiftsFilter->setEndTime(parse_date("Y-m-d H:i", $end_day . " " . $end_time)); } /** @@ -91,7 +66,7 @@ function update_ShiftsFilter(ShiftsFilter $shiftsFilter, $user_shifts_admin, $da $shiftsFilter->setTypes(check_request_int_array('types')); } if ((isset($_REQUEST['start_time']) && isset($_REQUEST['start_day']) && isset($_REQUEST['end_time']) && isset($_REQUEST['end_day'])) || $shiftsFilter->getStartTime() == null || $shiftsFilter->getEndTime() == null) { - update_ShiftsFilter_timerange($shiftsFilter); + update_ShiftsFilter_timerange($shiftsFilter, $days); } } diff --git a/includes/sys_page.php b/includes/sys_page.php index f20a791c..3c548bab 100644 --- a/includes/sys_page.php +++ b/includes/sys_page.php @@ -4,6 +4,32 @@ * Provide page/request helper functions */ +/** + * Parse a date from da day and a time textfield. + * + * @param string $date_name + * Name of the textfield containing the day (format Y-m-d) + * @param string $time_name + * Name of the textfield containing the time (format H:i) + * @param string[] $allowed_days + * List of allowed days in format Y-m-d + * @param int $default_value + * Default value unix timestamp + */ +function check_request_datetime($date_name, $time_name, $allowed_days, $default_value) { + $time = date("H:i", $default_value); + $day = date("Y-m-d", $default_value); + + if (isset($_REQUEST[$time_name]) && preg_match('#^\d{1,2}:\d\d$#', trim($_REQUEST[$time_name]))) { + $time = trim($_REQUEST[$time_name]); + } + if (isset($_REQUEST[$date_name]) && in_array($_REQUEST[$date_name], $allowed_days)) { + $day = $_REQUEST[$date_name]; + } + + return parse_date("Y-m-d H:i", $day . " " . $time); +} + /** * Parse a date into unix timestamp * -- 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(-) 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 From 6fff6a6c1a5a4ae6c6977c3aeb4ac532ba09a395 Mon Sep 17 00:00:00 2001 From: msquare Date: Wed, 5 Oct 2016 18:56:50 +0200 Subject: begin room view and shifts filter renderer --- db/update.sql | 2 + includes/controller/rooms_controller.php | 80 ++++++++++++++++++++++++++++++++ includes/engelsystem_provider.php | 1 + includes/model/Shifts_model.php | 8 ++++ includes/pages/admin_rooms.php | 2 +- includes/sys_menu.php | 28 +++++++++++ includes/sys_template.php | 4 ++ includes/view/Rooms_view.php | 7 +++ includes/view/ShiftsFilterRenderer.php | 65 ++++++++++++++++++++++++++ public/index.php | 3 ++ 10 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 includes/view/ShiftsFilterRenderer.php diff --git a/db/update.sql b/db/update.sql index e69de29b..5c5688e7 100644 --- a/db/update.sql +++ b/db/update.sql @@ -0,0 +1,2 @@ +INSERT INTO `Privileges` (`id`, `name`, `desc`) VALUES (40, 'view_rooms', 'User can view rooms'); +INSERT INTO `GroupPrivileges` (`id`, `group_id`, `privilege_id`) VALUES (NULL, '-2', '40'); diff --git a/includes/controller/rooms_controller.php b/includes/controller/rooms_controller.php index 5d55e1b7..da5bdba1 100644 --- a/includes/controller/rooms_controller.php +++ b/includes/controller/rooms_controller.php @@ -1,7 +1,87 @@ setStartTime(time()); + $shiftsFilter->setEndTime(time() + 24 * 60 * 60); + + $shiftsFilterRenderer = new ShiftsFilterRenderer($shiftsFilter); + $shiftsFilterRenderer->enableDaySelection($days, EventConfig()); + + return [ + $room['Name'], + Room_view($room, $shiftsFilterRenderer) + ]; +} + +/** + * Dispatch different room actions. + */ +function rooms_controller() { + global $privileges; + + if (! isset($_REQUEST['action'])) { + $_REQUEST['action'] = 'list'; + } + + switch ($_REQUEST['action']) { + default: + case 'list': + redirect(page_link_to('admin_rooms')); + case 'view': + return room_controller(); + } +} function room_link($room) { + return page_link_to('rooms') . '&action=view&room_id=' . $room['RID']; +} + +function room_edit_link($room) { return page_link_to('admin_rooms') . '&show=edit&id=' . $room['RID']; } +/** + * Loads room by request param room_id + */ +function load_room() { + if (! test_request_int('room_id')) { + redirect(page_link_to()); + } + + $room = Room($_REQUEST['room_id']); + if ($room == null) { + redirect(page_link_to()); + } + + return $room; +} + ?> \ No newline at end of file diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php index c6d34d1a..dbf7c54f 100644 --- a/includes/engelsystem_provider.php +++ b/includes/engelsystem_provider.php @@ -30,6 +30,7 @@ require_once realpath(__DIR__ . '/../includes/view/EventConfig_view.php'); require_once realpath(__DIR__ . '/../includes/view/Questions_view.php'); require_once realpath(__DIR__ . '/../includes/view/Rooms_view.php'); require_once realpath(__DIR__ . '/../includes/view/ShiftCalendarRenderer.php'); +require_once realpath(__DIR__ . '/../includes/view/ShiftsFilterRenderer.php'); require_once realpath(__DIR__ . '/../includes/view/Shifts_view.php'); require_once realpath(__DIR__ . '/../includes/view/ShiftEntry_view.php'); require_once realpath(__DIR__ . '/../includes/view/ShiftTypes_view.php'); diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index 8c251209..6721c30e 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -1,6 +1,14 @@ 0 AS 'has_special_needs' FROM `Shifts` diff --git a/includes/pages/admin_rooms.php b/includes/pages/admin_rooms.php index 186a676e..7a0f7a31 100644 --- a/includes/pages/admin_rooms.php +++ b/includes/pages/admin_rooms.php @@ -9,7 +9,7 @@ function admin_rooms() { $rooms = []; foreach ($rooms_source as $room) { $rooms[] = [ - 'name' => $room['Name'], + 'name' => Room_name_render($room), 'from_pentabarf' => $room['FromPentabarf'] == 'Y' ? '✓' : '', 'public' => $room['show'] == 'Y' ? '✓' : '', 'actions' => buttons([ diff --git a/includes/sys_menu.php b/includes/sys_menu.php index 112c490f..a5971ace 100644 --- a/includes/sys_menu.php +++ b/includes/sys_menu.php @@ -129,6 +129,8 @@ function make_navigation() { } } + $menu = make_room_navigation($menu); + $admin_menu = []; $admin_pages = [ "admin_arrive" => admin_arrive_title(), @@ -158,6 +160,32 @@ function make_navigation() { return toolbar($menu); } +/** + * Adds room navigation to the given menu. + * + * @param string[] $menu + * Rendered menu + */ +function make_room_navigation($menu) { + global $privileges; + + $rooms = Rooms(); + $room_menu = []; + if (in_array('admin_rooms', $privileges)) { + $room_menu[] = toolbar_item_link(page_link_to('admin_rooms'), 'list', _("Manage rooms")); + } + if (count($room_menu) > 0) { + $room_menu[] = toolbar_item_divider(); + } + foreach ($rooms as $room) { + $room_menu[] = toolbar_item_link(room_link($room), 'map-marker', $room['Name']); + } + if (count($room_menu > 0)) { + $menu[] = toolbar_dropdown('map-marker', _("Rooms"), $room_menu); + } + return $menu; +} + function make_menu() { return make_navigation(); } diff --git a/includes/sys_template.php b/includes/sys_template.php index 5b6caade..9bede1ee 100644 --- a/includes/sys_template.php +++ b/includes/sys_template.php @@ -60,6 +60,10 @@ function toolbar($items = [], $right = false) { return ''; } +function toolbar_pills($items) { + return ''; +} + /** * Render a link for a toolbar. * diff --git a/includes/view/Rooms_view.php b/includes/view/Rooms_view.php index c820e983..4dbf2956 100644 --- a/includes/view/Rooms_view.php +++ b/includes/view/Rooms_view.php @@ -1,4 +1,11 @@ render() + ]); +} function Room_name_render($room) { global $privileges; diff --git a/includes/view/ShiftsFilterRenderer.php b/includes/view/ShiftsFilterRenderer.php new file mode 100644 index 00000000..94d53bb3 --- /dev/null +++ b/includes/view/ShiftsFilterRenderer.php @@ -0,0 +1,65 @@ +shiftsFilter = $shiftsFilter; + } + + /** + * Renders the filter. + * + * @return Generated HTML + */ + public function render() { + $toolbar = []; + if ($this->daySelectionEnabled && ! empty($this->days)) { + $today = date("Y-m-d"); + $selected_day = date("Y-m-d", $this->shiftsFilter->getStartTime()); + $day_dropdown_items = []; + foreach ($this->days as $day) { + $day_dropdown_items[] = toolbar_item_link('', '', $day); + } + $toolbar[] = toolbar_dropdown('', $selected_day, $day_dropdown_items, 'active'); + } + return toolbar_pills($toolbar); + } + + /** + * Should the filter display a day selection. + */ + public function enableDaySelection($days, $event_config) { + $this->daySelectionEnabled = true; + $this->days = $days; + $this->event_config = $event_config; + } + + /** + * Should the filter display a day selection. + */ + public function isDaySelectionEnabled() { + return $this->daySelectionEnabled; + } +} + +?> \ No newline at end of file diff --git a/public/index.php b/public/index.php index 996c2b77..e1af1000 100644 --- a/public/index.php +++ b/public/index.php @@ -9,6 +9,7 @@ $free_pages = [ 'credits', 'ical', 'login', + 'rooms', 'shifts', 'shifts_json_export', 'shifts_json_export_all', @@ -68,6 +69,8 @@ if (isset($_REQUEST['p']) && preg_match("/^[a-z0-9_]*$/i", $_REQUEST['p']) && (i list($title, $content) = shifttypes_controller(); } elseif ($page == "admin_event_config") { list($title, $content) = event_config_edit_controller(); + } elseif ($page == "rooms") { + list($title, $content) = rooms_controller(); } elseif ($page == "news") { $title = news_title(); $content = user_news(); -- cgit v1.2.3-54-g00ecf From 6d97aa2d273464e3cb6703a0527793c52583d4cd Mon Sep 17 00:00:00 2001 From: msquare Date: Wed, 5 Oct 2016 22:28:39 +0200 Subject: continue working on shifts calendar renderer --- includes/controller/rooms_controller.php | 22 +++++++++---- includes/model/AngelType_model.php | 11 +++---- includes/model/Shifts_model.php | 2 +- includes/view/Rooms_view.php | 6 ++-- includes/view/ShiftCalendarRenderer.php | 56 +++++++++++++++++++++++++++++++- includes/view/ShiftsFilterRenderer.php | 15 +++++---- 6 files changed, 89 insertions(+), 23 deletions(-) diff --git a/includes/controller/rooms_controller.php b/includes/controller/rooms_controller.php index da5bdba1..4573409a 100644 --- a/includes/controller/rooms_controller.php +++ b/includes/controller/rooms_controller.php @@ -1,6 +1,7 @@ setStartTime(time()); - $shiftsFilter->setEndTime(time() + 24 * 60 * 60); + ], AngelType_ids()); + $selected_day = date("Y-m-d"); + if (! empty($days)) { + $selected_day = $days[0]; + } + if (isset($_REQUEST['shifts_filter_day'])) { + $selected_day = $_REQUEST['shifts_filter_day']; + } + $shiftsFilter->setStartTime(parse_date("Y-m-d H:i", $selected_day . ' 00:00')); + $shiftsFilter->setEndTime(parse_date("Y-m-d H:i", $selected_day . ' 23:59')); $shiftsFilterRenderer = new ShiftsFilterRenderer($shiftsFilter); - $shiftsFilterRenderer->enableDaySelection($days, EventConfig()); + $shiftsFilterRenderer->enableDaySelection($days); + + $shifts = Shifts_by_ShiftsFilter($shiftsFilter, $user); return [ $room['Name'], - Room_view($room, $shiftsFilterRenderer) + Room_view($room, $shiftsFilterRenderer, new ShiftCalendarRenderer($shifts, $shiftsFilter)) ]; } diff --git a/includes/model/AngelType_model.php b/includes/model/AngelType_model.php index d01cf905..be15dc59 100644 --- a/includes/model/AngelType_model.php +++ b/includes/model/AngelType_model.php @@ -124,14 +124,11 @@ function AngelTypes() { * Returns AngelType id array */ function AngelType_ids() { - $angelType_source = sql_select("SELECT `id` FROM `AngelTypes`"); - if ($angelType_source === false) { - return false; - } - if (count($angelType_source) > 0) { - return $angelType_source; + $result = sql_select("SELECT `id` FROM `AngelTypes`"); + if ($result === false) { + engelsystem_error("Unable to load angeltypes."); } - return null; + return select_array($result, 'id', 'id'); } /** diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index 6721c30e..5a38abdd 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -49,7 +49,7 @@ function Shifts_by_ShiftsFilter(ShiftsFilter $shiftsFilter, $user) { AND `ShiftEntry`.`SID` = `Shifts`.`SID` ) )"; - } elseif ($_SESSION['user_shifts']['filled'][0] == 1) { + } elseif ($_SESSION['user_shifts']['filled'][0] == ShiftsFilter::FILLED_FILLED) { $SQL .= " AND ( nat.`count` <= entries.`count` diff --git a/includes/view/Rooms_view.php b/includes/view/Rooms_view.php index 4dbf2956..40ab9480 100644 --- a/includes/view/Rooms_view.php +++ b/includes/view/Rooms_view.php @@ -1,9 +1,11 @@ render() + $shiftsFilterRenderer->render(room_link($room)) , + $shiftCalendarRenderer->render() ]); } diff --git a/includes/view/ShiftCalendarRenderer.php b/includes/view/ShiftCalendarRenderer.php index 8356d033..41cf6f94 100644 --- a/includes/view/ShiftCalendarRenderer.php +++ b/includes/view/ShiftCalendarRenderer.php @@ -4,7 +4,61 @@ namespace Engelsystem; class ShiftCalendarRenderer { - public function __construct() { + /** + * 15m * 60s/m = 900s + */ + const MINUTES_PER_ROW = 900; + + private $shifts; + + private $shiftsFilter; + + public function __construct($shifts, ShiftsFilter $shiftsFilter) { + $this->shifts = $shifts; + $this->shiftsFilter = $shiftsFilter; + } + + public function render() { + $rooms = $this->rooms(); + $slotSizes = $this->calcSlotSizes($rooms); + + return ''; + } + + /** + * Calculates the slots for each room that appears in the shifts + */ + private function rooms() { + $rooms = []; + foreach ($this->shifts as $shift) { + if (! isset($rooms[$shift['RID']])) { + $rooms[$shift['RID']] = $shift['room_name']; + } + } + return $rooms; + } + + private function calcSlotSizes($rooms) { + $first_block_start_time = ShiftCalendarRenderer::MINUTES_PER_ROW * floor($this->shiftsFilter->getStartTime() / ShiftCalendarRenderer::MINUTES_PER_ROW); + $blocks_per_slot = ceil(($this->shiftsFilter->getEndTime() - $first_block_start_time) / ShiftCalendarRenderer::MINUTES_PER_ROW); + $parallel_blocks = []; + + // initialize $block array + foreach (array_keys($rooms) as $room_id) { + $parallel_blocks[$room_id] = array_fill(0, $blocks_per_slot, 0); + } + + // calculate number of parallel shifts in each timeslot for each room + foreach ($this->shifts as $shift) { + $room_id = $shift["RID"]; + $shift_blocks = ($shift["end"] - $shift["start"]) / ShiftCalendarRenderer::MINUTES_PER_ROW; + $firstblock = floor(($shift["start"] - $first_block_start_time) / ShiftCalendarRenderer::MINUTES_PER_ROW); + for ($block = $firstblock; $block < $shift_blocks + $firstblock && $block < $blocks_per_slot; $block ++) { + $parallel_blocks[$room_id][$block] ++; + } + } + + return array_map('max', $parallel_blocks); } } diff --git a/includes/view/ShiftsFilterRenderer.php b/includes/view/ShiftsFilterRenderer.php index 94d53bb3..ff9302f7 100644 --- a/includes/view/ShiftsFilterRenderer.php +++ b/includes/view/ShiftsFilterRenderer.php @@ -18,10 +18,14 @@ class ShiftsFilterRenderer { */ private $daySelectionEnabled = false; + /** + * Days that can be selected. + * Format Y-m-d + * + * @var string[] + */ private $days = []; - private $event_config = null; - public function __construct(ShiftsFilter $shiftsFilter) { $this->shiftsFilter = $shiftsFilter; } @@ -31,14 +35,14 @@ class ShiftsFilterRenderer { * * @return Generated HTML */ - public function render() { + public function render($link_base) { $toolbar = []; if ($this->daySelectionEnabled && ! empty($this->days)) { $today = date("Y-m-d"); $selected_day = date("Y-m-d", $this->shiftsFilter->getStartTime()); $day_dropdown_items = []; foreach ($this->days as $day) { - $day_dropdown_items[] = toolbar_item_link('', '', $day); + $day_dropdown_items[] = toolbar_item_link($link_base . '&shifts_filter_day=' . $day, '', $day); } $toolbar[] = toolbar_dropdown('', $selected_day, $day_dropdown_items, 'active'); } @@ -48,10 +52,9 @@ class ShiftsFilterRenderer { /** * Should the filter display a day selection. */ - public function enableDaySelection($days, $event_config) { + public function enableDaySelection($days) { $this->daySelectionEnabled = true; $this->days = $days; - $this->event_config = $event_config; } /** -- cgit v1.2.3-54-g00ecf From c48335e702361eec3818a6b1021d443fee9037f5 Mon Sep 17 00:00:00 2001 From: msquare Date: Fri, 7 Oct 2016 17:22:48 +0200 Subject: improve shift rendering --- includes/model/NeededAngelTypes_model.php | 8 +- includes/model/ShiftEntry_model.php | 6 +- includes/model/Shifts_model.php | 2 +- includes/sys_menu.php | 4 + includes/view/Rooms_view.php | 2 +- includes/view/ShiftCalendarRenderer.php | 237 +++++++++++++++++++++++++++++- includes/view/ShiftsFilterRenderer.php | 4 +- public/css/theme0.css | 33 ++--- public/css/theme1.css | 33 ++--- public/css/theme2.css | 33 ++--- public/css/theme3.css | 33 ++--- themes/base.less | 33 ++--- 12 files changed, 305 insertions(+), 123 deletions(-) diff --git a/includes/model/NeededAngelTypes_model.php b/includes/model/NeededAngelTypes_model.php index 77a23c3d..47c9626f 100644 --- a/includes/model/NeededAngelTypes_model.php +++ b/includes/model/NeededAngelTypes_model.php @@ -65,7 +65,7 @@ function NeededAngelTypes_by_shift($shiftId) { ORDER BY `room_id` DESC "); if ($needed_angeltypes_source === false) { - return false; + engelsystem_error("Unable to load needed angeltypes."); } // Use settings from room @@ -80,18 +80,16 @@ function NeededAngelTypes_by_shift($shiftId) { ORDER BY `room_id` DESC "); if ($needed_angeltypes_source === false) { - return false; + engelsystem_error("Unable to load needed angeltypes."); } } $needed_angeltypes = []; foreach ($needed_angeltypes_source as $angeltype) { $shift_entries = ShiftEntries_by_shift_and_angeltype($shiftId, $angeltype['angel_type_id']); - if ($shift_entries === false) { - return false; - } $angeltype['taken'] = count($shift_entries); + $angeltype['shift_entries'] = $shift_entries; $needed_angeltypes[] = $angeltype; } diff --git a/includes/model/ShiftEntry_model.php b/includes/model/ShiftEntry_model.php index 425b92e8..63127bc7 100644 --- a/includes/model/ShiftEntry_model.php +++ b/includes/model/ShiftEntry_model.php @@ -110,12 +110,16 @@ function ShiftEntries_finished_by_user($user) { * @param int $angeltype_id */ function ShiftEntries_by_shift_and_angeltype($shift_id, $angeltype_id) { - return sql_select(" + $result = sql_select(" SELECT * FROM `ShiftEntry` WHERE `SID`=" . sql_escape($shift_id) . " AND `TID`=" . sql_escape($angeltype_id) . " "); + if ($result === false) { + engelsystem_error("Unable to load shift entries."); + } + return $result; } /** diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index 5a38abdd..f232360e 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -2,7 +2,7 @@ use Engelsystem\ShiftsFilter; function Shifts_by_room($room) { - $result = sql_select("SELECT * FROM `Shifts` WHERE `RID`=" . sql_escape($room['RID'])); + $result = sql_select("SELECT * FROM `Shifts` WHERE `RID`=" . sql_escape($room['RID']) . " ORDER BY `start`"); if ($result === false) { engelsystem_error("Unable to load shifts."); } diff --git a/includes/sys_menu.php b/includes/sys_menu.php index a5971ace..9d70cf10 100644 --- a/includes/sys_menu.php +++ b/includes/sys_menu.php @@ -169,6 +169,10 @@ function make_navigation() { function make_room_navigation($menu) { global $privileges; + if (! in_array('view_rooms', $privileges)) { + return $menu; + } + $rooms = Rooms(); $room_menu = []; if (in_array('admin_rooms', $privileges)) { diff --git a/includes/view/Rooms_view.php b/includes/view/Rooms_view.php index 40ab9480..7afdc67b 100644 --- a/includes/view/Rooms_view.php +++ b/includes/view/Rooms_view.php @@ -11,7 +11,7 @@ function Room_view($room, ShiftsFilterRenderer $shiftsFilterRenderer, ShiftCalen function Room_name_render($room) { global $privileges; - if (in_array('admin_rooms', $privileges)) { + if (in_array('view_rooms', $privileges)) { return '' . glyph('map-marker') . $room['Name'] . ''; } return glyph('map-marker') . $room['Name']; diff --git a/includes/view/ShiftCalendarRenderer.php b/includes/view/ShiftCalendarRenderer.php index 41cf6f94..04ecdf61 100644 --- a/includes/view/ShiftCalendarRenderer.php +++ b/includes/view/ShiftCalendarRenderer.php @@ -9,6 +9,8 @@ class ShiftCalendarRenderer { */ const MINUTES_PER_ROW = 900; + const EMPTY_CELL = ''; + private $shifts; private $shiftsFilter; @@ -20,9 +22,222 @@ class ShiftCalendarRenderer { public function render() { $rooms = $this->rooms(); - $slotSizes = $this->calcSlotSizes($rooms); - return ''; + $first_block_start_time = $this->calcFirstBlockStartTime(); + $blocks_per_slot = $this->calcBlocksPerSlot($first_block_start_time); + + $slotSizes = $this->calcSlotSizes($rooms, $first_block_start_time, $blocks_per_slot); + + return $this->renderTable($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot); + } + + private function renderTableHead($rooms, $slotSizes) { + $shifts_table = '' . _("Time") . ''; + foreach ($rooms as $room_id => $room_name) { + $colspan = $slotSizes[$room_id]; + $shifts_table .= " 1) ? ' colspan="' . $colspan . '"' : '') . ">" . Room_name_render([ + 'RID' => $room_id, + 'Name' => $room_name + ]) . "\n"; + } + $shifts_table .= ""; + return $shifts_table; + } + + private function initTableBody($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot) { + // Slot sizes plus 1 for the time + $columns_needed = array_sum($slotSizes) + 1; + $table_line = array_fill(0, $columns_needed, ShiftCalendarRenderer::EMPTY_CELL); + $table = array_fill(0, $blocks_per_slot, $table_line); + + for ($block = 0; $block < $blocks_per_slot; $block ++) { + $thistime = $first_block_start_time + ($block * ShiftCalendarRenderer::MINUTES_PER_ROW); + if ($thistime % (24 * 60 * 60) == 23 * 60 * 60 && $this->shiftsFilter->getEndTime() - $this->shiftsFilter->getStartTime() > 24 * 60 * 60) { + $table[$block][0] = '' . date('Y-m-dH:i', $thistime) . ''; + } elseif ($thistime % (60 * 60) == 0) { + $table[$block][0] = '' . date('H:i', $thistime) . ''; + } else { + $table[$block][0] = ''; + } + } + + return $table; + } + + private function calcRoomSlots($rooms, $slotSizes) { + $result = []; + $slot = 1; // 1 for the time + foreach ($rooms as $room_id => $room_name) { + $result[$room_id] = $slot; + $slot += $slotSizes[$room_id]; + } + + return $result; + } + + private function collides() { + // TODO + return false; + } + + private function renderShift($shift) { + global $privileges, $user; + + $collides = $this->collides(); + $is_free = false; + $shifts_row = ''; + $header_buttons = ""; + if (in_array('admin_shifts', $privileges)) { + $header_buttons = '
' . table_buttons([ + button(page_link_to('user_shifts') . '&edit_shift=' . $shift['SID'], glyph('edit'), 'btn-xs'), + button(page_link_to('user_shifts') . '&delete_shift=' . $shift['SID'], glyph('trash'), 'btn-xs') + ]) . '
'; + } + $info_text = ""; + if ($shift['title'] != '') { + $info_text = glyph('info-sign') . $shift['title'] . '
'; + } + + $angeltypes = NeededAngelTypes_by_shift($shift['SID']); + foreach ($angeltypes as $angeltype) { + $entry_list = []; + $freeloader = 0; + foreach ($angeltype['shift_entries'] as $entry) { + $style = ''; + if ($entry['freeloaded']) { + $freeloader ++; + $style = " text-decoration: line-through;"; + } + if (in_array('user_shifts_admin', $privileges)) { + $entry_list[] = "" . User_Nick_render(User($entry['UID'])) . ' ' . table_buttons([ + button(page_link_to('user_shifts') . '&entry_id=' . $entry['id'], glyph('trash'), 'btn-xs') + ]) . ''; + } else { + $entry_list[] = "" . User_Nick_render(User($entry['UID'])) . ""; + } + } + if ($angeltype['count'] - count($angeltype['shift_entries']) - $freeloader > 0) { + $inner_text = sprintf(ngettext("%d helper needed", "%d helpers needed", $angeltype['count'] - count($angeltype['shift_entries'])), $angeltype['count'] - count($angeltype['shift_entries'])); + // is the shift still running or alternatively is the user shift admin? + $user_may_join_shift = true; + + // you cannot join if user alread joined a parallel or this shift + $user_may_join_shift &= ! $collides; + + // you cannot join if user is not of this angel type + $user_may_join_shift &= isset($angeltype['user_id']); + + // you cannot join if you are not confirmed + if ($angeltype['restricted'] == 1 && isset($angeltype['user_id'])) { + $user_may_join_shift &= isset($angeltype['confirm_user_id']); + } + + // you can only join if the shift is in future or running + $user_may_join_shift &= time() < $shift['start']; + + // User shift admins may join anybody in every shift + $user_may_join_shift |= in_array('user_shifts_admin', $privileges); + if ($user_may_join_shift) { + $entry_list[] = '' . $inner_text . ' ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _('Sign up'), 'btn-xs'); + } else { + if (time() > $shift['start']) { + $entry_list[] = $inner_text . ' (' . _('ended') . ')'; + } elseif ($angeltype['restricted'] == 1 && isset($angeltype['user_id']) && ! isset($angeltype['confirm_user_id'])) { + $entry_list[] = $inner_text . glyph('lock'); + } elseif ($angeltype['restricted'] == 1) { + $entry_list[] = $inner_text; + } elseif ($collides) { + $entry_list[] = $inner_text; + } else { + $entry_list[] = $inner_text . '
' . button(page_link_to('user_angeltypes') . '&action=add&angeltype_id=' . $angeltype['id'], sprintf(_('Become %s'), $angeltype['name']), 'btn-xs'); + } + } + + unset($inner_text); + $is_free = true; + } + + $shifts_row .= '
  • '; + $shifts_row .= '' . AngelType_name_render($angeltype) . ': '; + $shifts_row .= join(", ", $entry_list); + $shifts_row .= '
  • '; + } + if (in_array('user_shifts_admin', $privileges)) { + $shifts_row .= '
  • ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _("Add more angels"), 'btn-xs') . '
  • '; + } + if ($shifts_row != '') { + $shifts_row = '
      ' . $shifts_row . '
    '; + } + if (isset($shift['own']) && $shift['own'] && ! in_array('user_shifts_admin', $privileges)) { + $class = 'primary'; + } elseif ($collides && ! in_array('user_shifts_admin', $privileges)) { + $class = 'default'; + } elseif ($is_free) { + $class = 'danger'; + } else { + $class = 'success'; + } + + $blocks = ceil(($shift["end"] - $shift["start"]) / ShiftCalendarRenderer::MINUTES_PER_ROW); + if ($blocks < 1) { + $blocks = 1; + } + return [ + $blocks, + '' . div('panel panel-' . $class, [ + div('panel-heading', [ + date('H:i', $shift['start']), + '‐', + date('H:i', $shift['end']), + '—', + ShiftType($shift['shifttype_id'])['name'], + $header_buttons + ]), + div('panel-body', [ + $info_text, + Room_name_render([ + 'RID' => $shift['RID'], + 'Name' => $shift['room_name'] + ]) + ]), + $shifts_row + ]) . '' + ]; + } + + private function renderTableBody($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot) { + $table = $this->initTableBody($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot); + + $room_slots = $this->calcRoomSlots($rooms, $slotSizes); + + foreach ($this->shifts as $shift) { + list($blocks, $shift_content) = $this->renderShift($shift); + $start_block = floor(($shift['start'] - $first_block_start_time) / ShiftCalendarRenderer::MINUTES_PER_ROW); + $slot = $room_slots[$shift['RID']]; + while ($table[$start_block][$slot] != ShiftCalendarRenderer::EMPTY_CELL) { + $slot ++; + } + $table[$start_block][$slot] = $shift_content; + for ($block = 1; $block < $blocks; $block ++) { + $table[$start_block + $block][$slot] = ''; + } + } + + $result = ''; + foreach ($table as $table_line) { + $result .= '' . join('', $table_line) . ''; + } + $result .= ''; + return $result; + } + + private function renderTable($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot) { + return div('shifts-table', [ + '', + $this->renderTableHead($rooms, $slotSizes), + $this->renderTableBody($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot), + '
    ' + ]); } /** @@ -38,9 +253,21 @@ class ShiftCalendarRenderer { return $rooms; } - private function calcSlotSizes($rooms) { - $first_block_start_time = ShiftCalendarRenderer::MINUTES_PER_ROW * floor($this->shiftsFilter->getStartTime() / ShiftCalendarRenderer::MINUTES_PER_ROW); - $blocks_per_slot = ceil(($this->shiftsFilter->getEndTime() - $first_block_start_time) / ShiftCalendarRenderer::MINUTES_PER_ROW); + private function calcFirstBlockStartTime() { + $start_time = $this->shiftsFilter->getEndTime(); + foreach ($this->shifts as $shift) { + if ($shift['start'] < $start_time) { + $start_time = $shift['start']; + } + } + return ShiftCalendarRenderer::MINUTES_PER_ROW * floor(($start_time - 60 * 60) / ShiftCalendarRenderer::MINUTES_PER_ROW); + } + + private function calcBlocksPerSlot($first_block_start_time) { + return ceil(($this->shiftsFilter->getEndTime() - $first_block_start_time) / ShiftCalendarRenderer::MINUTES_PER_ROW); + } + + private function calcSlotSizes($rooms, $first_block_start_time, $blocks_per_slot) { $parallel_blocks = []; // initialize $block array diff --git a/includes/view/ShiftsFilterRenderer.php b/includes/view/ShiftsFilterRenderer.php index ff9302f7..289ed210 100644 --- a/includes/view/ShiftsFilterRenderer.php +++ b/includes/view/ShiftsFilterRenderer.php @@ -46,7 +46,9 @@ class ShiftsFilterRenderer { } $toolbar[] = toolbar_dropdown('', $selected_day, $day_dropdown_items, 'active'); } - return toolbar_pills($toolbar); + return div('form-group', [ + toolbar_pills($toolbar) + ]); } /** diff --git a/public/css/theme0.css b/public/css/theme0.css index 49f928e9..cb46b82c 100644 --- a/public/css/theme0.css +++ b/public/css/theme0.css @@ -6730,32 +6730,19 @@ body { .footer a { color: #777777; } -#shifts td.free { - border: 1px solid #d9534f; - background-color: #f2dede; -} -a#shifts td.free:hover, -a#shifts td.free:focus { - background-color: #e4b9b9; -} -#shifts td.occupied { - border: 1px solid #5cb85c; - background-color: #dff0d8; +#shifts.table td, +#shifts.table th { + background-color: #f0f0f0; } -a#shifts td.occupied:hover, -a#shifts td.occupied:focus { - background-color: #c1e2b3; -} -#shifts td.collides { - border: 1px solid #f0ad4e; - background-color: #fcf8e3; +#shifts.table .row-hour { + border-top-color: #777777; } -a#shifts td.collides:hover, -a#shifts td.collides:focus { - background-color: #f7ecb5; +#shifts.table td.shift { + height: 1px; + padding: 0px 5px 5px 0px; } -#shifts td.own { - border: 1px solid #777777; +#shifts.table td.shift .panel { + margin-bottom: 0px; } .row-day { border-top: 2px solid #777777; diff --git a/public/css/theme1.css b/public/css/theme1.css index 379f53e4..916d303a 100644 --- a/public/css/theme1.css +++ b/public/css/theme1.css @@ -6753,32 +6753,19 @@ body { .footer a { color: #888888; } -#shifts td.free { - border: 1px solid #d9534f; - background-color: #d9534f; -} -a#shifts td.free:hover, -a#shifts td.free:focus { - background-color: #c9302c; -} -#shifts td.occupied { - border: 1px solid #5cb85c; - background-color: #5cb85c; +#shifts.table td, +#shifts.table th { + background-color: #f0f0f0; } -a#shifts td.occupied:hover, -a#shifts td.occupied:focus { - background-color: #449d44; -} -#shifts td.collides { - border: 1px solid #f0ad4e; - background-color: #f0ad4e; +#shifts.table .row-hour { + border-top-color: #888888; } -a#shifts td.collides:hover, -a#shifts td.collides:focus { - background-color: #ec971f; +#shifts.table td.shift { + height: 1px; + padding: 0px 5px 5px 0px; } -#shifts td.own { - border: 1px solid #888888; +#shifts.table td.shift .panel { + margin-bottom: 0px; } .row-day { border-top: 2px solid #888888; diff --git a/public/css/theme2.css b/public/css/theme2.css index e73daad3..5bc90f5f 100644 --- a/public/css/theme2.css +++ b/public/css/theme2.css @@ -6730,32 +6730,19 @@ body { .footer a { color: #777777; } -#shifts td.free { - border: 1px solid #7f528b; - background-color: #f1eaf2; -} -a#shifts td.free:hover, -a#shifts td.free:focus { - background-color: #dbcadf; -} -#shifts td.occupied { - border: 1px solid #7b9c41; - background-color: #f0f5e7; +#shifts.table td, +#shifts.table th { + background-color: #f0f0f0; } -a#shifts td.occupied:hover, -a#shifts td.occupied:focus { - background-color: #d9e6c3; -} -#shifts td.collides { - border: 1px solid #e3a14d; - background-color: #ffffff; +#shifts.table .row-hour { + border-top-color: #777777; } -a#shifts td.collides:hover, -a#shifts td.collides:focus { - background-color: #e6e6e6; +#shifts.table td.shift { + height: 1px; + padding: 0px 5px 5px 0px; } -#shifts td.own { - border: 1px solid #777777; +#shifts.table td.shift .panel { + margin-bottom: 0px; } .row-day { border-top: 2px solid #777777; diff --git a/public/css/theme3.css b/public/css/theme3.css index 82228db5..15c2c68d 100644 --- a/public/css/theme3.css +++ b/public/css/theme3.css @@ -6739,32 +6739,19 @@ body { .footer a { color: #777777; } -#shifts td.free { - border: 1px solid #da1639; - background-color: #f9c3cd; -} -a#shifts td.free:hover, -a#shifts td.free:focus { - background-color: #f495a6; -} -#shifts td.occupied { - border: 1px solid #39ab50; - background-color: #c4eccc; +#shifts.table td, +#shifts.table th { + background-color: #f0f0f0; } -a#shifts td.occupied:hover, -a#shifts td.occupied:focus { - background-color: #9edfab; -} -#shifts td.collides { - border: 1px solid #dad216; - background-color: #f9f7c3; +#shifts.table .row-hour { + border-top-color: #777777; } -a#shifts td.collides:hover, -a#shifts td.collides:focus { - background-color: #f4f095; +#shifts.table td.shift { + height: 1px; + padding: 0px 5px 5px 0px; } -#shifts td.own { - border: 1px solid #777777; +#shifts.table td.shift .panel { + margin-bottom: 0px; } .row-day { border-top: 2px solid #777777; diff --git a/themes/base.less b/themes/base.less index 83602c3b..0a6d0d34 100644 --- a/themes/base.less +++ b/themes/base.less @@ -10,23 +10,22 @@ body { color: @text-muted; } -#shifts { - td { - &.free { - border: 1px solid @brand-danger; - .bg-danger(); - } - &.occupied { - border: 1px solid @brand-success; - .bg-success(); - } - &.collides { - border: 1px solid @brand-warning; - .bg-warning(); - } - &.own { - border: 1px solid @gray-light; - } +#shifts.table { + td, th { + background-color: #f0f0f0; + } + + .row-hour { + border-top-color: @gray-light; + } + + td.shift { + height: 1px; + padding: 0px 5px 5px 0px; + + .panel { + margin-bottom: 0px; + } } } -- cgit v1.2.3-54-g00ecf From 1d4811cca51a00de27328d5608dbc23e46854297 Mon Sep 17 00:00:00 2001 From: msquare Date: Sat, 8 Oct 2016 09:18:04 +0200 Subject: remove unused code --- includes/controller/rooms_controller.php | 2 -- includes/view/ShiftCalendarRenderer.php | 8 ++++---- includes/view/ShiftsFilterRenderer.php | 1 - 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/includes/controller/rooms_controller.php b/includes/controller/rooms_controller.php index 4573409a..0e8560d5 100644 --- a/includes/controller/rooms_controller.php +++ b/includes/controller/rooms_controller.php @@ -55,8 +55,6 @@ function room_controller() { * Dispatch different room actions. */ function rooms_controller() { - global $privileges; - if (! isset($_REQUEST['action'])) { $_REQUEST['action'] = 'list'; } diff --git a/includes/view/ShiftCalendarRenderer.php b/includes/view/ShiftCalendarRenderer.php index 04ecdf61..ab13a5f3 100644 --- a/includes/view/ShiftCalendarRenderer.php +++ b/includes/view/ShiftCalendarRenderer.php @@ -44,7 +44,7 @@ class ShiftCalendarRenderer { return $shifts_table; } - private function initTableBody($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot) { + private function initTableBody($slotSizes, $first_block_start_time, $blocks_per_slot) { // Slot sizes plus 1 for the time $columns_needed = array_sum($slotSizes) + 1; $table_line = array_fill(0, $columns_needed, ShiftCalendarRenderer::EMPTY_CELL); @@ -67,7 +67,7 @@ class ShiftCalendarRenderer { private function calcRoomSlots($rooms, $slotSizes) { $result = []; $slot = 1; // 1 for the time - foreach ($rooms as $room_id => $room_name) { + foreach (array_keys($rooms) as $room_id) { $result[$room_id] = $slot; $slot += $slotSizes[$room_id]; } @@ -81,7 +81,7 @@ class ShiftCalendarRenderer { } private function renderShift($shift) { - global $privileges, $user; + global $privileges; $collides = $this->collides(); $is_free = false; @@ -206,7 +206,7 @@ class ShiftCalendarRenderer { } private function renderTableBody($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot) { - $table = $this->initTableBody($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot); + $table = $this->initTableBody($slotSizes, $first_block_start_time, $blocks_per_slot); $room_slots = $this->calcRoomSlots($rooms, $slotSizes); diff --git a/includes/view/ShiftsFilterRenderer.php b/includes/view/ShiftsFilterRenderer.php index 289ed210..301f31a2 100644 --- a/includes/view/ShiftsFilterRenderer.php +++ b/includes/view/ShiftsFilterRenderer.php @@ -38,7 +38,6 @@ class ShiftsFilterRenderer { public function render($link_base) { $toolbar = []; if ($this->daySelectionEnabled && ! empty($this->days)) { - $today = date("Y-m-d"); $selected_day = date("Y-m-d", $this->shiftsFilter->getStartTime()); $day_dropdown_items = []; foreach ($this->days as $day) { -- cgit v1.2.3-54-g00ecf From d59b1c8812f62848acc2bf19f020abf4949e75ef Mon Sep 17 00:00:00 2001 From: msquare Date: Sun, 9 Oct 2016 09:40:11 +0200 Subject: fix phpunit config --- phpunit.xml | 24 ++++++++++++++++++++++++ test/model/LogEntriesModelTest.php | 32 ++++++++++++++++++++++++++++++++ test/model/LogEntries_model_test.php | 32 -------------------------------- test/model/RoomModelTest.php | 33 +++++++++++++++++++++++++++++++++ test/model/Room_model_test.php | 33 --------------------------------- test/phpunit.xml | 14 -------------- 6 files changed, 89 insertions(+), 79 deletions(-) create mode 100644 phpunit.xml create mode 100644 test/model/LogEntriesModelTest.php delete mode 100644 test/model/LogEntries_model_test.php create mode 100644 test/model/RoomModelTest.php delete mode 100644 test/model/Room_model_test.php delete mode 100644 test/phpunit.xml diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 00000000..5b6d97a1 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,24 @@ + + + + ./test/model/ + + + + + ./include/ + ./public/ + + + + + + diff --git a/test/model/LogEntriesModelTest.php b/test/model/LogEntriesModelTest.php new file mode 100644 index 00000000..b4cef725 --- /dev/null +++ b/test/model/LogEntriesModelTest.php @@ -0,0 +1,32 @@ +assertNotFalse(LogEntry_create('test', 'test_LogEntry_create')); + + // There should be one more log entry now + $this->assertEquals(count(LogEntries()), $count + 1); + } + + public function test_LogEntries_clear_all() { + $this->create_LogEntry(); + $this->assertTrue(count(LogEntries()) > 0); + $this->assertNotFalse(LogEntries_clear_all()); + $this->assertEquals(count(LogEntries()), 0); + } + + /** + * @after + */ + public function teardown() { + LogEntries_clear_all(); + } +} + +?> diff --git a/test/model/LogEntries_model_test.php b/test/model/LogEntries_model_test.php deleted file mode 100644 index cefc4177..00000000 --- a/test/model/LogEntries_model_test.php +++ /dev/null @@ -1,32 +0,0 @@ -assertNotFalse(LogEntry_create('test', 'test_LogEntry_create')); - - // There should be one more log entry now - $this->assertEquals(count(LogEntries()), $count + 1); - } - - public function test_LogEntries_clear_all() { - $this->create_LogEntry(); - $this->assertTrue(count(LogEntries()) > 0); - $this->assertNotFalse(LogEntries_clear_all()); - $this->assertEquals(count(LogEntries()), 0); - } - - /** - * @after - */ - public function teardown() { - LogEntries_clear_all(); - } -} - -?> \ No newline at end of file diff --git a/test/model/RoomModelTest.php b/test/model/RoomModelTest.php new file mode 100644 index 00000000..33e6ca37 --- /dev/null +++ b/test/model/RoomModelTest.php @@ -0,0 +1,33 @@ +room_id = Room_create('test', false, true, ''); + } + + public function test_Room() { + $this->create_Room(); + + $room = Room($this->room_id); + + $this->assertNotFalse($room); + $this->assertNotNull($room); + $this->assertEquals($room['Name'], 'test'); + + $this->assertNull(Room(- 1)); + } + + /** + * @after + */ + public function teardown() { + if ($this->room_id != null) { + Room_delete($this->room_id); + } + } +} + +?> diff --git a/test/model/Room_model_test.php b/test/model/Room_model_test.php deleted file mode 100644 index a36a9d9f..00000000 --- a/test/model/Room_model_test.php +++ /dev/null @@ -1,33 +0,0 @@ -room_id = Room_create('test', false, true, ''); - } - - public function test_Room() { - $this->create_Room(); - - $room = Room($this->room_id); - - $this->assertNotFalse($room); - $this->assertNotNull($room); - $this->assertEquals($room['Name'], 'test'); - - $this->assertNull(Room(- 1)); - } - - /** - * @after - */ - public function teardown() { - if ($this->room_id != null) { - Room_delete($this->room_id); - } - } -} - -?> \ No newline at end of file diff --git a/test/phpunit.xml b/test/phpunit.xml deleted file mode 100644 index d6007646..00000000 --- a/test/phpunit.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - model/* - - - - - - -- cgit v1.2.3-54-g00ecf From 902866ff3a603aa2cfbbcdb5b46f0f2e840cd22c Mon Sep 17 00:00:00 2001 From: msquare Date: Thu, 3 Nov 2016 18:28:22 +0100 Subject: add first design of new shift markup --- includes/view/ShiftCalendarRenderer.php | 23 +++--- public/css/theme0.css | 4 +- public/css/theme1.css | 4 +- public/css/theme2.css | 4 +- public/css/theme3.css | 4 +- shift_markup.html | 121 ++++++++++++++++++++++++++++++++ themes/base.less | 5 +- 7 files changed, 140 insertions(+), 25 deletions(-) create mode 100644 shift_markup.html diff --git a/includes/view/ShiftCalendarRenderer.php b/includes/view/ShiftCalendarRenderer.php index ab13a5f3..2ba9b9e6 100644 --- a/includes/view/ShiftCalendarRenderer.php +++ b/includes/view/ShiftCalendarRenderer.php @@ -108,13 +108,7 @@ class ShiftCalendarRenderer { $freeloader ++; $style = " text-decoration: line-through;"; } - if (in_array('user_shifts_admin', $privileges)) { - $entry_list[] = "" . User_Nick_render(User($entry['UID'])) . ' ' . table_buttons([ - button(page_link_to('user_shifts') . '&entry_id=' . $entry['id'], glyph('trash'), 'btn-xs') - ]) . ''; - } else { - $entry_list[] = "" . User_Nick_render(User($entry['UID'])) . ""; - } + $entry_list[] = "" . User_Nick_render(User($entry['UID'])) . ""; } if ($angeltype['count'] - count($angeltype['shift_entries']) - $freeloader > 0) { $inner_text = sprintf(ngettext("%d helper needed", "%d helpers needed", $angeltype['count'] - count($angeltype['shift_entries'])), $angeltype['count'] - count($angeltype['shift_entries'])); @@ -138,7 +132,7 @@ class ShiftCalendarRenderer { // User shift admins may join anybody in every shift $user_may_join_shift |= in_array('user_shifts_admin', $privileges); if ($user_may_join_shift) { - $entry_list[] = '' . $inner_text . ' ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _('Sign up'), 'btn-xs'); + $entry_list[] = '' . $inner_text . ' ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _('Sign up'), 'btn-xs btn-primary'); } else { if (time() > $shift['start']) { $entry_list[] = $inner_text . ' (' . _('ended') . ')'; @@ -182,15 +176,13 @@ class ShiftCalendarRenderer { if ($blocks < 1) { $blocks = 1; } + $shift_length = ($shift["end"] - $shift["start"]) / (60 * 60); + $shift_heading = date('H:i', $shift['start']) . ' ‐ ' . date('H:i', $shift['end']) . ' — ' . ShiftType($shift['shifttype_id'])['name']; return [ $blocks, - '' . div('panel panel-' . $class, [ + '' . div('panel panel-' . $class . '" style="min-height: ' . ($shift_length * 100) . 'px"', [ div('panel-heading', [ - date('H:i', $shift['start']), - '‐', - date('H:i', $shift['end']), - '—', - ShiftType($shift['shifttype_id'])['name'], + '' . $shift_heading . '', $header_buttons ]), div('panel-body', [ @@ -200,7 +192,8 @@ class ShiftCalendarRenderer { 'Name' => $shift['room_name'] ]) ]), - $shifts_row + $shifts_row, + div('shift-spacer') ]) . '' ]; } diff --git a/public/css/theme0.css b/public/css/theme0.css index cb46b82c..0784042a 100644 --- a/public/css/theme0.css +++ b/public/css/theme0.css @@ -6739,10 +6739,10 @@ body { } #shifts.table td.shift { height: 1px; - padding: 0px 5px 5px 0px; + padding: 0; } #shifts.table td.shift .panel { - margin-bottom: 0px; + margin: 0px 0px 0px 5px; } .row-day { border-top: 2px solid #777777; diff --git a/public/css/theme1.css b/public/css/theme1.css index 916d303a..795439df 100644 --- a/public/css/theme1.css +++ b/public/css/theme1.css @@ -6762,10 +6762,10 @@ body { } #shifts.table td.shift { height: 1px; - padding: 0px 5px 5px 0px; + padding: 0; } #shifts.table td.shift .panel { - margin-bottom: 0px; + margin: 0px 0px 0px 5px; } .row-day { border-top: 2px solid #888888; diff --git a/public/css/theme2.css b/public/css/theme2.css index 5bc90f5f..0112dc93 100644 --- a/public/css/theme2.css +++ b/public/css/theme2.css @@ -6739,10 +6739,10 @@ body { } #shifts.table td.shift { height: 1px; - padding: 0px 5px 5px 0px; + padding: 0; } #shifts.table td.shift .panel { - margin-bottom: 0px; + margin: 0px 0px 0px 5px; } .row-day { border-top: 2px solid #777777; diff --git a/public/css/theme3.css b/public/css/theme3.css index 15c2c68d..4be828ef 100644 --- a/public/css/theme3.css +++ b/public/css/theme3.css @@ -6748,10 +6748,10 @@ body { } #shifts.table td.shift { height: 1px; - padding: 0px 5px 5px 0px; + padding: 0; } #shifts.table td.shift .panel { - margin-bottom: 0px; + margin: 0px 0px 0px 5px; } .row-day { border-top: 2px solid #777777; diff --git a/shift_markup.html b/shift_markup.html new file mode 100644 index 00000000..8d8fa63b --- /dev/null +++ b/shift_markup.html @@ -0,0 +1,121 @@ + + + + + + + + +
    +
    +
    10:00
    +
    +
    +
    +
    11:00
    +
    +
    +
    +
    12:00
    +
    +
    +
    +
    13:00
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + + \ No newline at end of file diff --git a/themes/base.less b/themes/base.less index 0a6d0d34..c5ff50b2 100644 --- a/themes/base.less +++ b/themes/base.less @@ -21,10 +21,11 @@ body { td.shift { height: 1px; - padding: 0px 5px 5px 0px; + padding: 0; .panel { - margin-bottom: 0px; + margin: 0px 0px 0px 5px; + overflow: hidden; } } } -- cgit v1.2.3-54-g00ecf From cf8cc5f592d4935e857fb5434aaf00168bdcb4c5 Mon Sep 17 00:00:00 2001 From: msquare Date: Sat, 5 Nov 2016 10:06:06 +0100 Subject: change shift table to html5 --- includes/engelsystem_provider.php | 1 + includes/model/NeededAngelTypes_model.php | 2 +- includes/sys_template.php | 5 +- includes/view/ShiftCalendarLane.php | 63 +++++++ includes/view/ShiftCalendarRenderer.php | 286 +++++++++++++++++------------- public/css/theme0.css | 49 +++-- public/css/theme1.css | 49 +++-- public/css/theme2.css | 49 +++-- public/css/theme3.css | 49 +++-- shift_markup.html | 51 +----- themes/base.less | 71 +++++--- 11 files changed, 431 insertions(+), 244 deletions(-) create mode 100644 includes/view/ShiftCalendarLane.php diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php index dbf7c54f..0f8aa425 100644 --- a/includes/engelsystem_provider.php +++ b/includes/engelsystem_provider.php @@ -29,6 +29,7 @@ require_once realpath(__DIR__ . '/../includes/view/AngelTypes_view.php'); require_once realpath(__DIR__ . '/../includes/view/EventConfig_view.php'); require_once realpath(__DIR__ . '/../includes/view/Questions_view.php'); require_once realpath(__DIR__ . '/../includes/view/Rooms_view.php'); +require_once realpath(__DIR__ . '/../includes/view/ShiftCalendarLane.php'); require_once realpath(__DIR__ . '/../includes/view/ShiftCalendarRenderer.php'); require_once realpath(__DIR__ . '/../includes/view/ShiftsFilterRenderer.php'); require_once realpath(__DIR__ . '/../includes/view/Shifts_view.php'); diff --git a/includes/model/NeededAngelTypes_model.php b/includes/model/NeededAngelTypes_model.php index 47c9626f..48f8ed20 100644 --- a/includes/model/NeededAngelTypes_model.php +++ b/includes/model/NeededAngelTypes_model.php @@ -57,7 +57,7 @@ function NeededAngelTypes_delete_by_room($room_id) { */ function NeededAngelTypes_by_shift($shiftId) { $needed_angeltypes_source = sql_select(" - SELECT `NeededAngelTypes`.*, `AngelTypes`.`name`, `AngelTypes`.`restricted` + SELECT `NeededAngelTypes`.*, `AngelTypes`.`id`, `AngelTypes`.`name`, `AngelTypes`.`restricted` FROM `NeededAngelTypes` JOIN `AngelTypes` ON `AngelTypes`.`id` = `NeededAngelTypes`.`angel_type_id` WHERE `shift_id`='" . sql_escape($shiftId) . "' diff --git a/includes/sys_template.php b/includes/sys_template.php index 9bede1ee..6c1727a4 100644 --- a/includes/sys_template.php +++ b/includes/sys_template.php @@ -42,8 +42,11 @@ function glyph_bool($boolean) { } function div($class, $content = [], $dom_id = "") { + if (is_array($content)) { + $content = join("\n", $content); + } $dom_id = $dom_id != '' ? ' id="' . $dom_id . '"' : ''; - return '' . join("\n", $content) . '
    '; + return '' . $content . '
    '; } function heading($content, $number = 1) { diff --git a/includes/view/ShiftCalendarLane.php b/includes/view/ShiftCalendarLane.php new file mode 100644 index 00000000..33fccec3 --- /dev/null +++ b/includes/view/ShiftCalendarLane.php @@ -0,0 +1,63 @@ +header = $header; + $this->firstBlockStartTime = $firstBlockStartTime; + $this->blockCount = $blockCount; + } + + /** + * Adds a shift to the lane, but only if it fits. + * Returns true on success. + * + * @param Shift $shift + * The shift to add + * @return boolean true on success + */ + public function addShift($shift) { + if ($this->shiftFits($shift)) { + $this->shifts[] = $shift; + return true; + } + return false; + } + + /** + * Returns true if given shift fits into this lane. + * + * @param Shift $shift + * The shift to fit into this lane + */ + public function shiftFits($newShift) { + foreach ($this->shifts as $laneShift) { + if (! ($newShift['start'] >= $laneShift['end'] || $newShift['end'] <= $laneShift['start'])) { + return false; + } + } + return true; + } + + public function getHeader() { + return $this->header; + } + + public function getShifts() { + return $this->shifts; + } +} +?> \ No newline at end of file diff --git a/includes/view/ShiftCalendarRenderer.php b/includes/view/ShiftCalendarRenderer.php index 2ba9b9e6..85aedfa0 100644 --- a/includes/view/ShiftCalendarRenderer.php +++ b/includes/view/ShiftCalendarRenderer.php @@ -7,72 +7,186 @@ class ShiftCalendarRenderer { /** * 15m * 60s/m = 900s */ - const MINUTES_PER_ROW = 900; + const SECONDS_PER_ROW = 900; - const EMPTY_CELL = ''; + /** + * Height of a block in pixel. + * Do not change - corresponds with theme/css + */ + const BLOCK_HEIGHT = 30; + + /** + * Distance between two shifts in pixels + */ + const MARGIN = 5; - private $shifts; + private $lanes; private $shiftsFilter; + private $firstBlockStartTime = null; + + private $blocksPerSlot = null; + public function __construct($shifts, ShiftsFilter $shiftsFilter) { - $this->shifts = $shifts; $this->shiftsFilter = $shiftsFilter; + $this->firstBlockStartTime = $this->calcFirstBlockStartTime($shifts); + $this->lanes = $this->assignShiftsToLanes($shifts); } - public function render() { - $rooms = $this->rooms(); - - $first_block_start_time = $this->calcFirstBlockStartTime(); - $blocks_per_slot = $this->calcBlocksPerSlot($first_block_start_time); + /** + * Assigns the shifts to different lanes per room if they collide + * + * @param Shift[] $shifts + * The shifts to assign + * + * @return Returns an array that assigns a room_id to an array of ShiftCalendarLane containing the shifts + */ + private function assignShiftsToLanes($shifts) { + // array that assigns a room id to a list of lanes (per room) + $lanes = []; - $slotSizes = $this->calcSlotSizes($rooms, $first_block_start_time, $blocks_per_slot); + foreach ($shifts as $shift) { + $room_id = $shift['RID']; + if (! isset($lanes[$room_id])) { + // initialize room with one lane + $header = Room_name_render([ + 'RID' => $room_id, + 'Name' => $shift['room_name'] + ]); + $lanes[$room_id] = [ + new ShiftCalendarLane($header, $this->getFirstBlockStartTime(), $this->getBlocksPerSlot()) + ]; + } + // Try to add the shift to the existing lanes for this room + $shift_added = false; + foreach ($lanes[$room_id] as $lane) { + $shift_added = $lane->addShift($shift); + if ($shift_added == true) { + break; + } + } + // If all lanes for this room are busy, create a new lane and add shift to it + if ($shift_added == false) { + $newLane = new ShiftCalendarLane("", $this->getFirstBlockStartTime(), $this->getBlocksPerSlot()); + if (! $newLane->addShift($shift)) { + engelsystem_error("Unable to add shift to new lane."); + } + $lanes[$room_id][] = $newLane; + } + } - return $this->renderTable($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot); + return $lanes; + } + + public function getFirstBlockStartTime() { + return $this->firstBlockStartTime; } - private function renderTableHead($rooms, $slotSizes) { - $shifts_table = '' . _("Time") . ''; - foreach ($rooms as $room_id => $room_name) { - $colspan = $slotSizes[$room_id]; - $shifts_table .= " 1) ? ' colspan="' . $colspan . '"' : '') . ">" . Room_name_render([ - 'RID' => $room_id, - 'Name' => $room_name - ]) . "\n"; + public function getBlocksPerSlot() { + if ($this->blocksPerSlot == null) { + $this->blocksPerSlot = $this->calcBlocksPerSlot(); } - $shifts_table .= ""; - return $shifts_table; + return $this->blocksPerSlot; } - private function initTableBody($slotSizes, $first_block_start_time, $blocks_per_slot) { - // Slot sizes plus 1 for the time - $columns_needed = array_sum($slotSizes) + 1; - $table_line = array_fill(0, $columns_needed, ShiftCalendarRenderer::EMPTY_CELL); - $table = array_fill(0, $blocks_per_slot, $table_line); - - for ($block = 0; $block < $blocks_per_slot; $block ++) { - $thistime = $first_block_start_time + ($block * ShiftCalendarRenderer::MINUTES_PER_ROW); - if ($thistime % (24 * 60 * 60) == 23 * 60 * 60 && $this->shiftsFilter->getEndTime() - $this->shiftsFilter->getStartTime() > 24 * 60 * 60) { - $table[$block][0] = '' . date('Y-m-dH:i', $thistime) . ''; - } elseif ($thistime % (60 * 60) == 0) { - $table[$block][0] = '' . date('H:i', $thistime) . ''; - } else { - $table[$block][0] = ''; + /** + * Renders the whole calendar + * + * @return the generated html + */ + public function render() { + return div('shift-calendar', [ + $this->renderTimeLane(), + $this->renderShiftLanes() + ]); + } + + /** + * Renders the lanes containing the shifts + */ + private function renderShiftLanes() { + $html = ""; + foreach ($this->lanes as $room_id => $room_lanes) { + foreach ($room_lanes as $lane) { + $html .= $this->renderLane($lane); } } - - return $table; + return $html; } - private function calcRoomSlots($rooms, $slotSizes) { - $result = []; - $slot = 1; // 1 for the time - foreach (array_keys($rooms) as $room_id) { - $result[$room_id] = $slot; - $slot += $slotSizes[$room_id]; + /** + * Renders a single lane + * + * @param ShiftCalendarLane $lane + * The lane to render + */ + private function renderLane(ShiftCalendarLane $lane) { + $html = ""; + $rendered_until = $this->getFirstBlockStartTime(); + foreach ($lane->getShifts() as $shift) { + while ($rendered_until + ShiftCalendarRenderer::SECONDS_PER_ROW <= $shift['start']) { + $html .= $this->renderTick($rendered_until); + $rendered_until += ShiftCalendarRenderer::SECONDS_PER_ROW; + } + + list($shift_height, $shift_html) = $this->renderShift($shift); + $html .= $shift_html; + $rendered_until += $shift_height * ShiftCalendarRenderer::SECONDS_PER_ROW; + } + while ($rendered_until <= $this->shiftsFilter->getEndTime()) { + $html .= $this->renderTick($rendered_until); + $rendered_until += ShiftCalendarRenderer::SECONDS_PER_ROW; } - return $result; + return div('lane', [ + div('header', $lane->getHeader()), + $html + ]); + } + + /** + * Renders a tick/block for given time + * + * @param int $time + * unix timestamp + * @param boolean $label + * Should time labels be generated? + * @return rendered tick html + */ + private function renderTick($time, $label = false) { + if ($time % (24 * 60 * 60) == 23 * 60 * 60) { + if (! $label) { + return div('tick day'); + } + return div('tick day', [ + date('Y-m-dH:i', $time) + ]); + } elseif ($time % (60 * 60) == 0) { + if (! $label) { + return div('tick hour'); + } + return div('tick hour', [ + date('H:i', $time) + ]); + } + return div('tick'); + } + + /** + * Renders the left time lane including hour/day ticks + */ + private function renderTimeLane() { + $time_slot = [ + div('header', [ + _("Time") + ]) + ]; + for ($block = 0; $block < $this->getBlocksPerSlot(); $block ++) { + $thistime = $this->getFirstBlockStartTime() + ($block * ShiftCalendarRenderer::SECONDS_PER_ROW); + $time_slot[] = $this->renderTick($thistime, true); + } + return div('lane time', $time_slot); } private function collides() { @@ -172,15 +286,14 @@ class ShiftCalendarRenderer { $class = 'success'; } - $blocks = ceil(($shift["end"] - $shift["start"]) / ShiftCalendarRenderer::MINUTES_PER_ROW); + $blocks = ceil(($shift["end"] - $shift["start"]) / ShiftCalendarRenderer::SECONDS_PER_ROW); if ($blocks < 1) { $blocks = 1; } - $shift_length = ($shift["end"] - $shift["start"]) / (60 * 60); $shift_heading = date('H:i', $shift['start']) . ' ‐ ' . date('H:i', $shift['end']) . ' — ' . ShiftType($shift['shifttype_id'])['name']; return [ $blocks, - '' . div('panel panel-' . $class . '" style="min-height: ' . ($shift_length * 100) . 'px"', [ + '' . div('shift panel panel-' . $class . '" style="height: ' . ($blocks * ShiftCalendarRenderer::BLOCK_HEIGHT - ShiftCalendarRenderer::MARGIN) . 'px"', [ div('panel-heading', [ '' . $shift_heading . '', $header_buttons @@ -198,87 +311,18 @@ class ShiftCalendarRenderer { ]; } - private function renderTableBody($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot) { - $table = $this->initTableBody($slotSizes, $first_block_start_time, $blocks_per_slot); - - $room_slots = $this->calcRoomSlots($rooms, $slotSizes); - - foreach ($this->shifts as $shift) { - list($blocks, $shift_content) = $this->renderShift($shift); - $start_block = floor(($shift['start'] - $first_block_start_time) / ShiftCalendarRenderer::MINUTES_PER_ROW); - $slot = $room_slots[$shift['RID']]; - while ($table[$start_block][$slot] != ShiftCalendarRenderer::EMPTY_CELL) { - $slot ++; - } - $table[$start_block][$slot] = $shift_content; - for ($block = 1; $block < $blocks; $block ++) { - $table[$start_block + $block][$slot] = ''; - } - } - - $result = ''; - foreach ($table as $table_line) { - $result .= '' . join('', $table_line) . ''; - } - $result .= ''; - return $result; - } - - private function renderTable($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot) { - return div('shifts-table', [ - '', - $this->renderTableHead($rooms, $slotSizes), - $this->renderTableBody($rooms, $slotSizes, $first_block_start_time, $blocks_per_slot), - '
    ' - ]); - } - - /** - * Calculates the slots for each room that appears in the shifts - */ - private function rooms() { - $rooms = []; - foreach ($this->shifts as $shift) { - if (! isset($rooms[$shift['RID']])) { - $rooms[$shift['RID']] = $shift['room_name']; - } - } - return $rooms; - } - - private function calcFirstBlockStartTime() { + private function calcFirstBlockStartTime($shifts) { $start_time = $this->shiftsFilter->getEndTime(); - foreach ($this->shifts as $shift) { + foreach ($shifts as $shift) { if ($shift['start'] < $start_time) { $start_time = $shift['start']; } } - return ShiftCalendarRenderer::MINUTES_PER_ROW * floor(($start_time - 60 * 60) / ShiftCalendarRenderer::MINUTES_PER_ROW); - } - - private function calcBlocksPerSlot($first_block_start_time) { - return ceil(($this->shiftsFilter->getEndTime() - $first_block_start_time) / ShiftCalendarRenderer::MINUTES_PER_ROW); + return ShiftCalendarRenderer::SECONDS_PER_ROW * floor(($start_time - 60 * 60) / ShiftCalendarRenderer::SECONDS_PER_ROW); } - private function calcSlotSizes($rooms, $first_block_start_time, $blocks_per_slot) { - $parallel_blocks = []; - - // initialize $block array - foreach (array_keys($rooms) as $room_id) { - $parallel_blocks[$room_id] = array_fill(0, $blocks_per_slot, 0); - } - - // calculate number of parallel shifts in each timeslot for each room - foreach ($this->shifts as $shift) { - $room_id = $shift["RID"]; - $shift_blocks = ($shift["end"] - $shift["start"]) / ShiftCalendarRenderer::MINUTES_PER_ROW; - $firstblock = floor(($shift["start"] - $first_block_start_time) / ShiftCalendarRenderer::MINUTES_PER_ROW); - for ($block = $firstblock; $block < $shift_blocks + $firstblock && $block < $blocks_per_slot; $block ++) { - $parallel_blocks[$room_id][$block] ++; - } - } - - return array_map('max', $parallel_blocks); + private function calcBlocksPerSlot() { + return ceil(($this->shiftsFilter->getEndTime() - $this->getFirstBlockStartTime()) / ShiftCalendarRenderer::SECONDS_PER_ROW); } } diff --git a/public/css/theme0.css b/public/css/theme0.css index 0784042a..daba1eba 100644 --- a/public/css/theme0.css +++ b/public/css/theme0.css @@ -6730,25 +6730,46 @@ body { .footer a { color: #777777; } -#shifts.table td, -#shifts.table th { - background-color: #f0f0f0; +.shift-calendar { + display: flex; + flex-direction: row; + flex-wrap: nowrap; + align-itmes: stretch; + width: 100%; } -#shifts.table .row-hour { - border-top-color: #777777; +.shift-calendar .lane { + background: #f9f9f9; + min-width: 300px; + width: 300px; + flex-grow: 1; } -#shifts.table td.shift { - height: 1px; - padding: 0; +.shift-calendar .lane .header { + background: #fff; + height: 30px; + padding: 5px; +} +.shift-calendar .lane .tick { + height: 30px; + border-top: 1px solid #f4f4f4; +} +.shift-calendar .lane .tick.hour { + border-top: 2px solid #dddddd; + font-size: 0.9em; + padding-left: 5px; } -#shifts.table td.shift .panel { - margin: 0px 0px 0px 5px; +.shift-calendar .lane .tick.day { + border-top: 2px solid #337ab7; + font-size: 0.9em; + padding-left: 5px; } -.row-day { - border-top: 2px solid #777777; +.shift-calendar .lane.time { + min-width: 100px; + width: 100px; + flex-grow: 0; } -.row-header { - min-width: 90px; +.shift-calendar .shift { + margin: 0 5px 5px 0; + overflow: hidden; } .space-top { margin-top: 15px; diff --git a/public/css/theme1.css b/public/css/theme1.css index 795439df..e9b6b4cd 100644 --- a/public/css/theme1.css +++ b/public/css/theme1.css @@ -6753,25 +6753,46 @@ body { .footer a { color: #888888; } -#shifts.table td, -#shifts.table th { - background-color: #f0f0f0; +.shift-calendar { + display: flex; + flex-direction: row; + flex-wrap: nowrap; + align-itmes: stretch; + width: 100%; } -#shifts.table .row-hour { - border-top-color: #888888; +.shift-calendar .lane { + background: #080808; + min-width: 300px; + width: 300px; + flex-grow: 1; } -#shifts.table td.shift { - height: 1px; - padding: 0; +.shift-calendar .lane .header { + background: #fff; + height: 30px; + padding: 5px; +} +.shift-calendar .lane .tick { + height: 30px; + border-top: 1px solid #030303; +} +.shift-calendar .lane .tick.hour { + border-top: 2px solid #282828; + font-size: 0.9em; + padding-left: 5px; } -#shifts.table td.shift .panel { - margin: 0px 0px 0px 5px; +.shift-calendar .lane .tick.day { + border-top: 2px solid #428bca; + font-size: 0.9em; + padding-left: 5px; } -.row-day { - border-top: 2px solid #888888; +.shift-calendar .lane.time { + min-width: 100px; + width: 100px; + flex-grow: 0; } -.row-header { - min-width: 90px; +.shift-calendar .shift { + margin: 0 5px 5px 0; + overflow: hidden; } .space-top { margin-top: 15px; diff --git a/public/css/theme2.css b/public/css/theme2.css index 0112dc93..7646f08b 100644 --- a/public/css/theme2.css +++ b/public/css/theme2.css @@ -6730,25 +6730,46 @@ body { .footer a { color: #777777; } -#shifts.table td, -#shifts.table th { - background-color: #f0f0f0; +.shift-calendar { + display: flex; + flex-direction: row; + flex-wrap: nowrap; + align-itmes: stretch; + width: 100%; } -#shifts.table .row-hour { - border-top-color: #777777; +.shift-calendar .lane { + background: #f9f9f9; + min-width: 300px; + width: 300px; + flex-grow: 1; } -#shifts.table td.shift { - height: 1px; - padding: 0; +.shift-calendar .lane .header { + background: #fff; + height: 30px; + padding: 5px; +} +.shift-calendar .lane .tick { + height: 30px; + border-top: 1px solid #f4f4f4; +} +.shift-calendar .lane .tick.hour { + border-top: 2px solid #dddddd; + font-size: 0.9em; + padding-left: 5px; } -#shifts.table td.shift .panel { - margin: 0px 0px 0px 5px; +.shift-calendar .lane .tick.day { + border-top: 2px solid #758499; + font-size: 0.9em; + padding-left: 5px; } -.row-day { - border-top: 2px solid #777777; +.shift-calendar .lane.time { + min-width: 100px; + width: 100px; + flex-grow: 0; } -.row-header { - min-width: 90px; +.shift-calendar .shift { + margin: 0 5px 5px 0; + overflow: hidden; } .space-top { margin-top: 15px; diff --git a/public/css/theme3.css b/public/css/theme3.css index 4be828ef..014fee12 100644 --- a/public/css/theme3.css +++ b/public/css/theme3.css @@ -6739,25 +6739,46 @@ body { .footer a { color: #777777; } -#shifts.table td, -#shifts.table th { - background-color: #f0f0f0; +.shift-calendar { + display: flex; + flex-direction: row; + flex-wrap: nowrap; + align-itmes: stretch; + width: 100%; } -#shifts.table .row-hour { - border-top-color: #777777; +.shift-calendar .lane { + background: #f9f9f9; + min-width: 300px; + width: 300px; + flex-grow: 1; } -#shifts.table td.shift { - height: 1px; - padding: 0; +.shift-calendar .lane .header { + background: #fff; + height: 30px; + padding: 5px; +} +.shift-calendar .lane .tick { + height: 30px; + border-top: 1px solid #f4f4f4; +} +.shift-calendar .lane .tick.hour { + border-top: 2px solid #dddddd; + font-size: 0.9em; + padding-left: 5px; } -#shifts.table td.shift .panel { - margin: 0px 0px 0px 5px; +.shift-calendar .lane .tick.day { + border-top: 2px solid #f19224; + font-size: 0.9em; + padding-left: 5px; } -.row-day { - border-top: 2px solid #777777; +.shift-calendar .lane.time { + min-width: 100px; + width: 100px; + flex-grow: 0; } -.row-header { - min-width: 90px; +.shift-calendar .shift { + margin: 0 5px 5px 0; + overflow: hidden; } .space-top { margin-top: 15px; diff --git a/shift_markup.html b/shift_markup.html index 8d8fa63b..7ad5177a 100644 --- a/shift_markup.html +++ b/shift_markup.html @@ -4,52 +4,13 @@
    -
    10:00
    +
    Time
    +
    2016-12-27 00:00
    @@ -67,7 +28,10 @@
    -
    +
    + Bottle Sorting (Hall H) +
    +
    @@ -99,7 +63,8 @@
    -
    +
    +
    diff --git a/themes/base.less b/themes/base.less index c5ff50b2..98c12514 100644 --- a/themes/base.less +++ b/themes/base.less @@ -10,32 +10,59 @@ body { color: @text-muted; } -#shifts.table { - td, th { - background-color: #f0f0f0; +.shift-calendar { + display: flex; + flex-direction: row; + flex-wrap: nowrap; + align-itmes: stretch; + display: flex; + flex-direction: row; + flex-wrap: nowrap; + align-itmes: stretch; + width: 100%; + + .lane { + background: @table-bg-accent; + flex-grow: 1; + min-width: 300px; + width: 300px; + flex-grow: 1; + + .header { + background: #fff; + height: 30px; + padding: 5px; + } + + .tick { + height: 30px; + border-top: 1px solid darken(@table-bg-accent, 2%); + } + + .tick.hour { + border-top: 2px solid @table-border-color; + font-size: 0.9em; + padding-left: 5px; + } + + .tick.day { + border-top: 2px solid @brand-primary; + font-size: 0.9em; + padding-left: 5px; + } } - .row-hour { - border-top-color: @gray-light; + .lane.time { + flex-grow: 0; + min-width: 100px; + width: 100px; + flex-grow: 0; } - td.shift { - height: 1px; - padding: 0; - - .panel { - margin: 0px 0px 0px 5px; - overflow: hidden; - } - } -} - -.row-day { - border-top: 2px solid @gray-light; -} - -.row-header { - min-width: 90px; + .shift { + margin: 0 5px 5px 0; + overflow: hidden; + } } .space-top { -- cgit v1.2.3-54-g00ecf From db7f8c1ab3d4c53ce747db707f5b9480f5f0c024 Mon Sep 17 00:00:00 2001 From: msquare Date: Mon, 7 Nov 2016 20:50:15 +0100 Subject: split shift calendar renderer into different classes --- includes/engelsystem_provider.php | 1 + includes/view/ShiftCalendarRenderer.php | 127 +------------------- includes/view/ShiftCalendarShiftRenderer.php | 171 +++++++++++++++++++++++++++ 3 files changed, 175 insertions(+), 124 deletions(-) create mode 100644 includes/view/ShiftCalendarShiftRenderer.php diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php index 0f8aa425..f3d4f5b1 100644 --- a/includes/engelsystem_provider.php +++ b/includes/engelsystem_provider.php @@ -31,6 +31,7 @@ require_once realpath(__DIR__ . '/../includes/view/Questions_view.php'); require_once realpath(__DIR__ . '/../includes/view/Rooms_view.php'); require_once realpath(__DIR__ . '/../includes/view/ShiftCalendarLane.php'); require_once realpath(__DIR__ . '/../includes/view/ShiftCalendarRenderer.php'); +require_once realpath(__DIR__ . '/../includes/view/ShiftCalendarShiftRenderer.php'); require_once realpath(__DIR__ . '/../includes/view/ShiftsFilterRenderer.php'); require_once realpath(__DIR__ . '/../includes/view/Shifts_view.php'); require_once realpath(__DIR__ . '/../includes/view/ShiftEntry_view.php'); diff --git a/includes/view/ShiftCalendarRenderer.php b/includes/view/ShiftCalendarRenderer.php index 85aedfa0..70948ec5 100644 --- a/includes/view/ShiftCalendarRenderer.php +++ b/includes/view/ShiftCalendarRenderer.php @@ -107,7 +107,7 @@ class ShiftCalendarRenderer { */ private function renderShiftLanes() { $html = ""; - foreach ($this->lanes as $room_id => $room_lanes) { + foreach ($this->lanes as $room_lanes) { foreach ($room_lanes as $lane) { $html .= $this->renderLane($lane); } @@ -122,6 +122,7 @@ class ShiftCalendarRenderer { * The lane to render */ private function renderLane(ShiftCalendarLane $lane) { + $shift_renderer = new ShiftCalendarShiftRenderer(); $html = ""; $rendered_until = $this->getFirstBlockStartTime(); foreach ($lane->getShifts() as $shift) { @@ -130,7 +131,7 @@ class ShiftCalendarRenderer { $rendered_until += ShiftCalendarRenderer::SECONDS_PER_ROW; } - list($shift_height, $shift_html) = $this->renderShift($shift); + list($shift_height, $shift_html) = $shift_renderer->render($shift); $html .= $shift_html; $rendered_until += $shift_height * ShiftCalendarRenderer::SECONDS_PER_ROW; } @@ -189,128 +190,6 @@ class ShiftCalendarRenderer { return div('lane time', $time_slot); } - private function collides() { - // TODO - return false; - } - - private function renderShift($shift) { - global $privileges; - - $collides = $this->collides(); - $is_free = false; - $shifts_row = ''; - $header_buttons = ""; - if (in_array('admin_shifts', $privileges)) { - $header_buttons = '
    ' . table_buttons([ - button(page_link_to('user_shifts') . '&edit_shift=' . $shift['SID'], glyph('edit'), 'btn-xs'), - button(page_link_to('user_shifts') . '&delete_shift=' . $shift['SID'], glyph('trash'), 'btn-xs') - ]) . '
    '; - } - $info_text = ""; - if ($shift['title'] != '') { - $info_text = glyph('info-sign') . $shift['title'] . '
    '; - } - - $angeltypes = NeededAngelTypes_by_shift($shift['SID']); - foreach ($angeltypes as $angeltype) { - $entry_list = []; - $freeloader = 0; - foreach ($angeltype['shift_entries'] as $entry) { - $style = ''; - if ($entry['freeloaded']) { - $freeloader ++; - $style = " text-decoration: line-through;"; - } - $entry_list[] = "" . User_Nick_render(User($entry['UID'])) . ""; - } - if ($angeltype['count'] - count($angeltype['shift_entries']) - $freeloader > 0) { - $inner_text = sprintf(ngettext("%d helper needed", "%d helpers needed", $angeltype['count'] - count($angeltype['shift_entries'])), $angeltype['count'] - count($angeltype['shift_entries'])); - // is the shift still running or alternatively is the user shift admin? - $user_may_join_shift = true; - - // you cannot join if user alread joined a parallel or this shift - $user_may_join_shift &= ! $collides; - - // you cannot join if user is not of this angel type - $user_may_join_shift &= isset($angeltype['user_id']); - - // you cannot join if you are not confirmed - if ($angeltype['restricted'] == 1 && isset($angeltype['user_id'])) { - $user_may_join_shift &= isset($angeltype['confirm_user_id']); - } - - // you can only join if the shift is in future or running - $user_may_join_shift &= time() < $shift['start']; - - // User shift admins may join anybody in every shift - $user_may_join_shift |= in_array('user_shifts_admin', $privileges); - if ($user_may_join_shift) { - $entry_list[] = '' . $inner_text . ' ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _('Sign up'), 'btn-xs btn-primary'); - } else { - if (time() > $shift['start']) { - $entry_list[] = $inner_text . ' (' . _('ended') . ')'; - } elseif ($angeltype['restricted'] == 1 && isset($angeltype['user_id']) && ! isset($angeltype['confirm_user_id'])) { - $entry_list[] = $inner_text . glyph('lock'); - } elseif ($angeltype['restricted'] == 1) { - $entry_list[] = $inner_text; - } elseif ($collides) { - $entry_list[] = $inner_text; - } else { - $entry_list[] = $inner_text . '
    ' . button(page_link_to('user_angeltypes') . '&action=add&angeltype_id=' . $angeltype['id'], sprintf(_('Become %s'), $angeltype['name']), 'btn-xs'); - } - } - - unset($inner_text); - $is_free = true; - } - - $shifts_row .= '
  • '; - $shifts_row .= '' . AngelType_name_render($angeltype) . ': '; - $shifts_row .= join(", ", $entry_list); - $shifts_row .= '
  • '; - } - if (in_array('user_shifts_admin', $privileges)) { - $shifts_row .= '
  • ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _("Add more angels"), 'btn-xs') . '
  • '; - } - if ($shifts_row != '') { - $shifts_row = '
      ' . $shifts_row . '
    '; - } - if (isset($shift['own']) && $shift['own'] && ! in_array('user_shifts_admin', $privileges)) { - $class = 'primary'; - } elseif ($collides && ! in_array('user_shifts_admin', $privileges)) { - $class = 'default'; - } elseif ($is_free) { - $class = 'danger'; - } else { - $class = 'success'; - } - - $blocks = ceil(($shift["end"] - $shift["start"]) / ShiftCalendarRenderer::SECONDS_PER_ROW); - if ($blocks < 1) { - $blocks = 1; - } - $shift_heading = date('H:i', $shift['start']) . ' ‐ ' . date('H:i', $shift['end']) . ' — ' . ShiftType($shift['shifttype_id'])['name']; - return [ - $blocks, - '' . div('shift panel panel-' . $class . '" style="height: ' . ($blocks * ShiftCalendarRenderer::BLOCK_HEIGHT - ShiftCalendarRenderer::MARGIN) . 'px"', [ - div('panel-heading', [ - '' . $shift_heading . '', - $header_buttons - ]), - div('panel-body', [ - $info_text, - Room_name_render([ - 'RID' => $shift['RID'], - 'Name' => $shift['room_name'] - ]) - ]), - $shifts_row, - div('shift-spacer') - ]) . '' - ]; - } - private function calcFirstBlockStartTime($shifts) { $start_time = $this->shiftsFilter->getEndTime(); foreach ($shifts as $shift) { diff --git a/includes/view/ShiftCalendarShiftRenderer.php b/includes/view/ShiftCalendarShiftRenderer.php new file mode 100644 index 00000000..ee6ebcf0 --- /dev/null +++ b/includes/view/ShiftCalendarShiftRenderer.php @@ -0,0 +1,171 @@ +collides(); + $is_free = false; + $shifts_row = ''; + $info_text = ""; + if ($shift['title'] != '') { + $info_text = glyph('info-sign') . $shift['title'] . '
    '; + } + + $angeltypes = NeededAngelTypes_by_shift($shift['SID']); + foreach ($angeltypes as $angeltype) { + $shifts_row .= $this->renderShiftNeededAngeltype($shift, $angeltype, $collides); + } + if (in_array('user_shifts_admin', $privileges)) { + $shifts_row .= '
  • ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _("Add more angels"), 'btn-xs') . '
  • '; + } + if ($shifts_row != '') { + $shifts_row = '
      ' . $shifts_row . '
    '; + } + if (isset($shift['own']) && $shift['own'] && ! in_array('user_shifts_admin', $privileges)) { + $class = 'primary'; + } elseif ($collides && ! in_array('user_shifts_admin', $privileges)) { + $class = 'default'; + } elseif ($is_free) { + $class = 'danger'; + } else { + $class = 'success'; + } + + $blocks = ceil(($shift["end"] - $shift["start"]) / ShiftCalendarRenderer::SECONDS_PER_ROW); + if ($blocks < 1) { + $blocks = 1; + } + return [ + $blocks, + '' . div('shift panel panel-' . $class . '" style="height: ' . ($blocks * ShiftCalendarRenderer::BLOCK_HEIGHT - ShiftCalendarRenderer::MARGIN) . 'px"', [ + $this->renderShiftHead($shift), + div('panel-body', [ + $info_text, + Room_name_render([ + 'RID' => $shift['RID'], + 'Name' => $shift['room_name'] + ]) + ]), + $shifts_row, + div('shift-spacer') + ]) . '' + ]; + } + + /** + * Renders a list entry containing the needed angels for an angeltype + * + * @param Shift $shift + * The shift which is rendered + * @param Angeltype $angeltype + * The angeltype, containing informations about needed angeltypes and already signed up angels + * @param boolean $collides + * true if the shift collides with the users shifts + */ + private function renderShiftNeededAngeltype($shift, $angeltype, $collides) { + global $privileges; + + $entry_list = []; + $freeloader = 0; + foreach ($angeltype['shift_entries'] as $entry) { + $style = ''; + if ($entry['freeloaded']) { + $freeloader ++; + $style = " text-decoration: line-through;"; + } + $entry_list[] = "" . User_Nick_render(User($entry['UID'])) . ""; + } + if ($angeltype['count'] - count($angeltype['shift_entries']) - $freeloader > 0) { + $inner_text = sprintf(ngettext("%d helper needed", "%d helpers needed", $angeltype['count'] - count($angeltype['shift_entries'])), $angeltype['count'] - count($angeltype['shift_entries'])); + // is the shift still running or alternatively is the user shift admin? + $user_may_join_shift = true; + + // you cannot join if user alread joined a parallel or this shift + $user_may_join_shift &= ! $collides; + + // you cannot join if user is not of this angel type + $user_may_join_shift &= isset($angeltype['user_id']); + + // you cannot join if you are not confirmed + if ($angeltype['restricted'] == 1 && isset($angeltype['user_id'])) { + $user_may_join_shift &= isset($angeltype['confirm_user_id']); + } + + // you can only join if the shift is in future or running + $user_may_join_shift &= time() < $shift['start']; + + // User shift admins may join anybody in every shift + $user_may_join_shift |= in_array('user_shifts_admin', $privileges); + if ($user_may_join_shift) { + $entry_list[] = '' . $inner_text . ' ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _('Sign up'), 'btn-xs btn-primary'); + } else { + if (time() > $shift['start']) { + $entry_list[] = $inner_text . ' (' . _('ended') . ')'; + } elseif ($angeltype['restricted'] == 1 && isset($angeltype['user_id']) && ! isset($angeltype['confirm_user_id'])) { + $entry_list[] = $inner_text . glyph('lock'); + } elseif ($angeltype['restricted'] == 1) { + $entry_list[] = $inner_text; + } elseif ($collides) { + $entry_list[] = $inner_text; + } else { + $entry_list[] = $inner_text . '
    ' . button(page_link_to('user_angeltypes') . '&action=add&angeltype_id=' . $angeltype['id'], sprintf(_('Become %s'), $angeltype['name']), 'btn-xs'); + } + } + + unset($inner_text); + $is_free = true; + } + + $shifts_row = '
  • '; + $shifts_row .= '' . AngelType_name_render($angeltype) . ': '; + $shifts_row .= join(", ", $entry_list); + $shifts_row .= '
  • '; + return $shifts_row; + } + + /** + * Renders the shift header + * + * @param Shift $shift + * The shift + */ + private function renderShiftHead($shift) { + global $privileges; + + $header_buttons = ""; + if (in_array('admin_shifts', $privileges)) { + $header_buttons = '
    ' . table_buttons([ + button(page_link_to('user_shifts') . '&edit_shift=' . $shift['SID'], glyph('edit'), 'btn-xs'), + button(page_link_to('user_shifts') . '&delete_shift=' . $shift['SID'], glyph('trash'), 'btn-xs') + ]) . '
    '; + } + $shift_heading = date('H:i', $shift['start']) . ' ‐ ' . date('H:i', $shift['end']) . ' — ' . ShiftType($shift['shifttype_id'])['name']; + return div('panel-heading', [ + '' . $shift_heading . '', + $header_buttons + ]); + } + + /** + * Does the shift collide with the user's shifts + */ + private function collides() { + // TODO + return false; + } +} + +?> \ No newline at end of file -- cgit v1.2.3-54-g00ecf From a8e3e91448127715d5e8d10df9b059c04d88f64e Mon Sep 17 00:00:00 2001 From: msquare Date: Mon, 7 Nov 2016 20:55:37 +0100 Subject: implement shift calendar into user shift page --- includes/pages/user_shifts.php | 227 +---------------------------------------- 1 file changed, 3 insertions(+), 224 deletions(-) diff --git a/includes/pages/user_shifts.php b/includes/pages/user_shifts.php index d415e5f5..e5fd27ef 100644 --- a/includes/pages/user_shifts.php +++ b/includes/pages/user_shifts.php @@ -1,5 +1,6 @@ Array ( [SID] => 1 [start] => 1355958000 [end] => 1355961600 [RID] => 1 [name] => [URL] => [PSID] => [room_name] => test1 [has_special_needs] => 1 [is_full] => 0 ) - */ - $first = 15 * 60 * floor($shiftsFilter->getStartTime() / (15 * 60)); - $maxshow = ceil(($shiftsFilter->getEndTime() - $first) / (60 * 15)); - $block = []; - $todo = []; - $myrooms = $rooms; - - // delete un-selected rooms from array - foreach ($myrooms as $k => $v) { - if (array_search($v["id"], $shiftsFilter->getRooms()) === false) { - unset($myrooms[$k]); - } - // initialize $block array - $block[$v["id"]] = array_fill(0, $maxshow, 0); - } - - // calculate number of parallel shifts in each timeslot for each room - foreach ($shifts as $k => $shift) { - $rid = $shift["RID"]; - $blocks = ($shift["end"] - $shift["start"]) / (15 * 60); - $firstblock = floor(($shift["start"] - $first) / (15 * 60)); - for ($i = $firstblock; $i < $blocks + $firstblock && $i < $maxshow; $i ++) { - $block[$rid][$i] ++; - } - $shifts[$k]['own'] = in_array($shift['SID'], array_keys($ownshifts)); - } - - $shifts_table = '
    '; - foreach ($myrooms as $key => $room) { - $rid = $room["id"]; - if (array_sum($block[$rid]) == 0) { - // do not display columns without entries - unset($block[$rid]); - unset($myrooms[$key]); - continue; - } - $colspan = call_user_func_array('max', $block[$rid]); - if ($colspan == 0) { - $colspan = 1; - } - $todo[$rid] = array_fill(0, $maxshow, $colspan); - $shifts_table .= " 1) ? ' colspan="' . $colspan . '"' : '') . ">" . Room_name_render([ - 'RID' => $room['id'], - 'Name' => $room['name'] - ]) . "\n"; - } - unset($block, $blocks, $firstblock, $colspan, $key, $room); - - $shifts_table .= ""; - for ($i = 0; $i < $maxshow; $i ++) { - $thistime = $first + ($i * 15 * 60); - if ($thistime % (24 * 60 * 60) == 23 * 60 * 60 && $shiftsFilter->getEndTime() - $shiftsFilter->getStartTime() > 24 * 60 * 60) { - $shifts_table .= ""; - foreach ($myrooms as $room) { - $rid = $room["id"]; - foreach ($shifts as $shift) { - if ($shift["RID"] == $rid) { - if (floor($shift["start"] / (15 * 60)) == $thistime / (15 * 60)) { - $blocks = ($shift["end"] - $shift["start"]) / (15 * 60); - if ($blocks < 1) { - $blocks = 1; - } - - $collides = in_array($shift['SID'], array_keys($ownshifts)); - if (! $collides) { - foreach ($ownshifts as $ownshift) { - if ($ownshift['start'] >= $shift['start'] && $ownshift['start'] < $shift['end'] || $ownshift['end'] > $shift['start'] && $ownshift['end'] <= $shift['end'] || $ownshift['start'] < $shift['start'] && $ownshift['end'] > $shift['end']) { - $collides = true; - break; - } - } - } - - $is_free = false; - $shifts_row = ''; - if (in_array('admin_shifts', $privileges)) { - $shifts_row .= '
    ' . table_buttons([ - button(page_link_to('user_shifts') . '&edit_shift=' . $shift['SID'], glyph('edit'), 'btn-xs'), - button(page_link_to('user_shifts') . '&delete_shift=' . $shift['SID'], glyph('trash'), 'btn-xs') - ]) . '
    '; - } - $shifts_row .= Room_name_render([ - 'RID' => $room['id'], - 'Name' => $room['name'] - ]) . '
    '; - $shifts_row .= '' . date('Y-m-d H:i', $shift['start']); - $shifts_row .= " – "; - $shifts_row .= date('H:i', $shift['end']); - $shifts_row .= "
    "; - $shifts_row .= ShiftType($shift['shifttype_id'])['name']; - $shifts_row .= "
    "; - if ($shift['title'] != '') { - $shifts_row .= $shift['title']; - $shifts_row .= "
    "; - } - $shifts_row .= '
    '; - $shifts_row .= '
    '; - $query = "SELECT `NeededAngelTypes`.`count`, `AngelTypes`.`id`, `AngelTypes`.`restricted`, `UserAngelTypes`.`confirm_user_id`, `AngelTypes`.`name`, `UserAngelTypes`.`user_id` - FROM `NeededAngelTypes` - JOIN `AngelTypes` ON (`NeededAngelTypes`.`angel_type_id` = `AngelTypes`.`id`) - LEFT JOIN `UserAngelTypes` ON (`NeededAngelTypes`.`angel_type_id` = `UserAngelTypes`.`angeltype_id`AND `UserAngelTypes`.`user_id`='" . sql_escape($user['UID']) . "') - WHERE - `count` > 0 - AND "; - if ($shift['has_special_needs']) { - $query .= "`shift_id` = '" . sql_escape($shift['SID']) . "'"; - } else { - $query .= "`room_id` = '" . sql_escape($shift['RID']) . "'"; - } - if (! empty($shiftsFilter->getTypes())) { - $query .= " AND `angel_type_id` IN (" . implode(',', $shiftsFilter->getTypes()) . ") "; - } - $query .= " ORDER BY `AngelTypes`.`name`"; - $angeltypes = sql_select($query); - - if (count($angeltypes) > 0) { - foreach ($angeltypes as $angeltype) { - $entries = sql_select("SELECT * FROM `ShiftEntry` JOIN `User` ON (`ShiftEntry`.`UID` = `User`.`UID`) WHERE `SID`='" . sql_escape($shift['SID']) . "' AND `TID`='" . sql_escape($angeltype['id']) . "' ORDER BY `Nick`"); - $entry_list = []; - $freeloader = 0; - foreach ($entries as $entry) { - $style = ''; - if ($entry['freeloaded']) { - $freeloader ++; - $style = " text-decoration: line-through;"; - } - if (in_array('user_shifts_admin', $privileges)) { - $entry_list[] = "" . User_Nick_render($entry) . ' ' . table_buttons([ - button(page_link_to('user_shifts') . '&entry_id=' . $entry['id'], glyph('trash'), 'btn-xs') - ]) . ''; - } else { - $entry_list[] = "" . User_Nick_render($entry) . ""; - } - } - if ($angeltype['count'] - count($entries) - $freeloader > 0) { - $inner_text = sprintf(ngettext("%d helper needed", "%d helpers needed", $angeltype['count'] - count($entries)), $angeltype['count'] - count($entries)); - // is the shift still running or alternatively is the user shift admin? - $user_may_join_shift = true; - - // you cannot join if user alread joined a parallel or this shift - $user_may_join_shift &= ! $collides; - - // you cannot join if user is not of this angel type - $user_may_join_shift &= isset($angeltype['user_id']); - - // you cannot join if you are not confirmed - if ($angeltype['restricted'] == 1 && isset($angeltype['user_id'])) { - $user_may_join_shift &= isset($angeltype['confirm_user_id']); - } - - // you can only join if the shift is in future or running - $user_may_join_shift &= time() < $shift['start']; - - // User shift admins may join anybody in every shift - $user_may_join_shift |= in_array('user_shifts_admin', $privileges); - if ($user_may_join_shift) { - $entry_list[] = '' . $inner_text . ' ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _('Sign up'), 'btn-xs'); - } else { - if (time() > $shift['start']) { - $entry_list[] = $inner_text . ' (' . _('ended') . ')'; - } elseif ($angeltype['restricted'] == 1 && isset($angeltype['user_id']) && ! isset($angeltype['confirm_user_id'])) { - $entry_list[] = $inner_text . glyph('lock'); - } elseif ($angeltype['restricted'] == 1) { - $entry_list[] = $inner_text; - } elseif ($collides) { - $entry_list[] = $inner_text; - } else { - $entry_list[] = $inner_text . '
    ' . button(page_link_to('user_angeltypes') . '&action=add&angeltype_id=' . $angeltype['id'], sprintf(_('Become %s'), $angeltype['name']), 'btn-xs'); - } - } - - unset($inner_text); - $is_free = true; - } - - $shifts_row .= '' . AngelType_name_render($angeltype) . ': '; - $shifts_row .= join(", ", $entry_list); - $shifts_row .= '
    '; - } - if (in_array('user_shifts_admin', $privileges)) { - $shifts_row .= ' ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _("Add more angels"), 'btn-xs'); - } - } - if ($shift['own'] && ! in_array('user_shifts_admin', $privileges)) { - $class = 'own'; - } elseif ($collides && ! in_array('user_shifts_admin', $privileges)) { - $class = 'collides'; - } elseif ($is_free) { - $class = 'free'; - } else { - $class = 'occupied'; - } - $shifts_table .= '"; - // also output that shift on ical export - $ical_shifts[] = $shift; - for ($j = 0; $j < $blocks && $i + $j < $maxshow; $j ++) { - $todo[$rid][$i + $j] --; - } - } - } - } - // fill up row with empty '; - } - } - $shifts_table .= "\n"; - } - $shifts_table .= '
    -
    "; - $shifts_table .= date('Y-m-dH:i', $thistime); - } elseif ($thistime % (60 * 60) == 0) { - $shifts_table .= "
    "; - $shifts_table .= date("H:i", $thistime); - } else { - $shifts_table .= "
    "; - } - $shifts_table .= "'; - $shifts_table .= $shifts_row; - $shifts_table .= " - while ($todo[$rid][$i] -- > 0) { - $shifts_table .= '
    '; - if ($user['api_key'] == "") { User_reset_api_key($user, false); } @@ -379,6 +157,7 @@ function view_user_shifts() { $end_day = date("Y-m-d", $shiftsFilter->getEndTime()); $end_time = date("H:i", $shiftsFilter->getEndTime()); + $shiftCalendarRenderer = new ShiftCalendarRenderer($shifts, $shiftsFilter); return page([ div('col-md-12', [ msg(), @@ -392,7 +171,7 @@ function view_user_shifts() { 'type_select' => make_select($types, $shiftsFilter->getTypes(), "types", _("Angeltypes") . '1'), 'filled_select' => make_select($filled, $shiftsFilter->getFilled(), "filled", _("Occupancy")), 'task_notice' => '1' . _("The tasks shown here are influenced by the angeltypes you joined already!") . " " . _("Description of the jobs.") . "", - 'shifts_table' => msg() . $shifts_table, + 'shifts_table' => msg() . $shiftCalendarRenderer->render(), 'ical_text' => '

    ' . _("iCal export") . '

    ' . sprintf(_("Export of shown shifts. iCal format or JSON format available (please keep secret, otherwise reset the api key)."), page_link_to_absolute('ical') . '&key=' . $user['api_key'], page_link_to_absolute('shifts_json_export') . '&key=' . $user['api_key'], page_link_to('user_myshifts') . '&reset') . '

    ', 'filter' => _("Filter") ]) -- cgit v1.2.3-54-g00ecf From 125f34133d9c6b39cb9937e0ce29764ab86d1346 Mon Sep 17 00:00:00 2001 From: msquare Date: Mon, 7 Nov 2016 21:11:05 +0100 Subject: remove unused var --- includes/view/ShiftCalendarShiftRenderer.php | 1 - 1 file changed, 1 deletion(-) diff --git a/includes/view/ShiftCalendarShiftRenderer.php b/includes/view/ShiftCalendarShiftRenderer.php index ee6ebcf0..a0791f85 100644 --- a/includes/view/ShiftCalendarShiftRenderer.php +++ b/includes/view/ShiftCalendarShiftRenderer.php @@ -126,7 +126,6 @@ class ShiftCalendarShiftRenderer { } unset($inner_text); - $is_free = true; } $shifts_row = '
  • '; -- cgit v1.2.3-54-g00ecf From 22520532c78b3a032aec6ececb7623ba094da8de Mon Sep 17 00:00:00 2001 From: msquare Date: Mon, 7 Nov 2016 21:24:05 +0100 Subject: fix shift occupied calculation --- includes/view/ShiftCalendarShiftRenderer.php | 50 +++++++++++++++++++--------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/includes/view/ShiftCalendarShiftRenderer.php b/includes/view/ShiftCalendarShiftRenderer.php index a0791f85..e86f7baa 100644 --- a/includes/view/ShiftCalendarShiftRenderer.php +++ b/includes/view/ShiftCalendarShiftRenderer.php @@ -17,23 +17,12 @@ class ShiftCalendarShiftRenderer { global $privileges; $collides = $this->collides(); - $is_free = false; - $shifts_row = ''; $info_text = ""; if ($shift['title'] != '') { $info_text = glyph('info-sign') . $shift['title'] . '
    '; } + list($is_free, $shifts_row) = $this->renderShiftNeededAngeltypes($shift, $collides); - $angeltypes = NeededAngelTypes_by_shift($shift['SID']); - foreach ($angeltypes as $angeltype) { - $shifts_row .= $this->renderShiftNeededAngeltype($shift, $angeltype, $collides); - } - if (in_array('user_shifts_admin', $privileges)) { - $shifts_row .= '
  • ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _("Add more angels"), 'btn-xs') . '
  • '; - } - if ($shifts_row != '') { - $shifts_row = '
      ' . $shifts_row . '
    '; - } if (isset($shift['own']) && $shift['own'] && ! in_array('user_shifts_admin', $privileges)) { $class = 'primary'; } elseif ($collides && ! in_array('user_shifts_admin', $privileges)) { @@ -45,9 +34,7 @@ class ShiftCalendarShiftRenderer { } $blocks = ceil(($shift["end"] - $shift["start"]) / ShiftCalendarRenderer::SECONDS_PER_ROW); - if ($blocks < 1) { - $blocks = 1; - } + $blocks = max(1, $blocks); return [ $blocks, '' . div('shift panel panel-' . $class . '" style="height: ' . ($blocks * ShiftCalendarRenderer::BLOCK_HEIGHT - ShiftCalendarRenderer::MARGIN) . 'px"', [ @@ -65,6 +52,32 @@ class ShiftCalendarShiftRenderer { ]; } + private function renderShiftNeededAngeltypes($shift, $collides) { + global $privileges; + + $html = ""; + $is_free = false; + $angeltypes = NeededAngelTypes_by_shift($shift['SID']); + foreach ($angeltypes as $angeltype) { + list($angeltype_free, $angeltype_html) = $this->renderShiftNeededAngeltype($shift, $angeltype, $collides); + $is_free |= $angeltype_free; + $html .= $angeltype_html; + } + if (in_array('user_shifts_admin', $privileges)) { + $html .= '
  • ' . button(page_link_to('user_shifts') . '&shift_id=' . $shift['SID'] . '&type_id=' . $angeltype['id'], _("Add more angels"), 'btn-xs') . '
  • '; + } + if ($html != '') { + return [ + $is_free, + '
      ' . $html . '
    ' + ]; + } + return [ + $is_free, + "" + ]; + } + /** * Renders a list entry containing the needed angels for an angeltype * @@ -78,6 +91,7 @@ class ShiftCalendarShiftRenderer { private function renderShiftNeededAngeltype($shift, $angeltype, $collides) { global $privileges; + $is_free = false; $entry_list = []; $freeloader = 0; foreach ($angeltype['shift_entries'] as $entry) { @@ -126,13 +140,17 @@ class ShiftCalendarShiftRenderer { } unset($inner_text); + $is_free = true; } $shifts_row = '
  • '; $shifts_row .= '' . AngelType_name_render($angeltype) . ': '; $shifts_row .= join(", ", $entry_list); $shifts_row .= '
  • '; - return $shifts_row; + return [ + $is_free, + $shifts_row + ]; } /** -- cgit v1.2.3-54-g00ecf