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 ++++++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) (limited to 'includes/controller/shifts_controller.php') 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; -- cgit v1.2.3-70-g09d2 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(-) (limited to 'includes/controller/shifts_controller.php') 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-70-g09d2 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(-) (limited to 'includes/controller/shifts_controller.php') 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-70-g09d2 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(-) (limited to 'includes/controller/shifts_controller.php') 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-70-g09d2 From 27c9650dc4064ec5b40c5041d8291a7ce0e5305e Mon Sep 17 00:00:00 2001 From: msquare Date: Tue, 4 Oct 2016 21:20:38 +0200 Subject: reduce complexity of shifts controller --- includes/controller/angeltypes_controller.php | 6 --- includes/controller/shift_entries_controller.php | 3 -- includes/controller/shifts_controller.php | 28 +----------- includes/controller/shifttypes_controller.php | 3 -- includes/model/AngelType_model.php | 12 +++++- includes/model/ShiftTypes_model.php | 2 +- includes/model/Shifts_model.php | 55 +++++++++++++----------- 7 files changed, 44 insertions(+), 65 deletions(-) (limited to 'includes/controller/shifts_controller.php') diff --git a/includes/controller/angeltypes_controller.php b/includes/controller/angeltypes_controller.php index cd938893..ea451481 100644 --- a/includes/controller/angeltypes_controller.php +++ b/includes/controller/angeltypes_controller.php @@ -50,9 +50,6 @@ function angeltypes_about_controller() { } else { $angeltypes = AngelTypes(); } - if ($angeltypes === false) { - engelsystem_error("Unable to load angeltypes."); - } return [ _("Teams/Job description"), @@ -223,9 +220,6 @@ function angeltypes_list_controller() { } $angeltypes = AngelTypes_with_user($user); - if ($angeltypes === false) { - engelsystem_error("Unable to load angeltypes."); - } foreach ($angeltypes as &$angeltype) { $actions = [ diff --git a/includes/controller/shift_entries_controller.php b/includes/controller/shift_entries_controller.php index d704c299..9e252a4f 100644 --- a/includes/controller/shift_entries_controller.php +++ b/includes/controller/shift_entries_controller.php @@ -21,9 +21,6 @@ function shift_entry_add_controller() { $shift = Shift($shift_id); $shift['Name'] = $room_array[$shift['RID']]; - if ($shift === false) { - engelsystem_error('Unable to load shift.'); - } if ($shift == null) { redirect(page_link_to('user_shifts')); } diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php index ca6fd906..be0cf127 100644 --- a/includes/controller/shifts_controller.php +++ b/includes/controller/shifts_controller.php @@ -159,19 +159,13 @@ function shift_delete_controller() { $shift_id = $_REQUEST['delete_shift']; $shift = Shift($shift_id); - if ($shift === false) { - engelsystem_error('Unable to load shift.'); - } if ($shift == null) { redirect(page_link_to('user_shifts')); } // Schicht löschen bestätigt if (isset($_REQUEST['delete'])) { - $result = Shift_delete($shift_id); - if ($result === false) { - engelsystem_error('Unable to delete shift.'); - } + Shift_delete($shift_id); engelsystem_log("Deleted shift " . $shift['name'] . " from " . date("Y-m-d H:i", $shift['start']) . " to " . date("Y-m-d H:i", $shift['end'])); success(_("Shift deleted.")); @@ -196,33 +190,15 @@ function shift_controller() { } $shift = Shift($_REQUEST['shift_id']); - if ($shift === false) { - engelsystem_error('Unable to load shift.'); - } if ($shift == null) { - error(_('Shift could not be found.')); + error(_("Shift could not be found.")); redirect(page_link_to('user_shifts')); } $shifttype = ShiftType($shift['shifttype_id']); - if ($shifttype === false || $shifttype == null) { - engelsystem_error('Unable to load shift type.'); - } - $room = Room($shift['RID']); - if ($room === false || $room == null) { - engelsystem_error('Unable to load room.'); - } - $angeltypes = AngelTypes(); - if ($angeltypes === false) { - engelsystem_error('Unable to load angeltypes.'); - } - $user_shifts = Shifts_by_user($user); - if ($user_shifts === false) { - engelsystem_error('Unable to load users shifts.'); - } $signed_up = false; foreach ($user_shifts as $user_shift) { diff --git a/includes/controller/shifttypes_controller.php b/includes/controller/shifttypes_controller.php index 5e547895..456e39ae 100644 --- a/includes/controller/shifttypes_controller.php +++ b/includes/controller/shifttypes_controller.php @@ -48,9 +48,6 @@ function shifttype_edit_controller() { $description = ""; $angeltypes = AngelTypes(); - if ($angeltypes === false) { - engelsystem_error("Unable to load angel types."); - } if (isset($_REQUEST['shifttype_id'])) { $shifttype = ShiftType($_REQUEST['shifttype_id']); diff --git a/includes/model/AngelType_model.php b/includes/model/AngelType_model.php index a87dd461..d01cf905 100644 --- a/includes/model/AngelType_model.php +++ b/includes/model/AngelType_model.php @@ -91,7 +91,7 @@ function AngelType_validate_name($name, $angeltype) { * @param User $user */ function AngelTypes_with_user($user) { - return sql_select(" + $result = sql_select(" SELECT `AngelTypes`.*, `UserAngelTypes`.`id` as `user_angeltype_id`, `UserAngelTypes`.`confirm_user_id`, @@ -100,16 +100,24 @@ function AngelTypes_with_user($user) { LEFT JOIN `UserAngelTypes` ON `AngelTypes`.`id`=`UserAngelTypes`.`angeltype_id` AND `UserAngelTypes`.`user_id`=" . $user['UID'] . " ORDER BY `name`"); + if ($result === false) { + engelsystem_error("Unable to load angeltypes."); + } + return $result; } /** * Returns all angeltypes. */ function AngelTypes() { - return sql_select(" + $result = sql_select(" SELECT * FROM `AngelTypes` ORDER BY `name`"); + if ($result === false) { + engelsystem_error("Unable to load angeltypes."); + } + return $result; } /** diff --git a/includes/model/ShiftTypes_model.php b/includes/model/ShiftTypes_model.php index a9cf77bd..89704a65 100644 --- a/includes/model/ShiftTypes_model.php +++ b/includes/model/ShiftTypes_model.php @@ -51,7 +51,7 @@ function ShiftType_create($name, $angeltype_id, $description) { function ShiftType($shifttype_id) { $shifttype = sql_select("SELECT * FROM `ShiftTypes` WHERE `id`='" . sql_escape($shifttype_id) . "'"); if ($shifttype === false) { - return false; + engelsystem_error('Unable to load shift type.'); } if ($shifttype == null) { return null; diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index 52ced0fe..8c251209 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -93,9 +93,6 @@ function Shift_signup_allowed($shift, $angeltype, $user_angeltype = null, $user_ if ($user_shifts == null) { $user_shifts = Shifts_by_user($user); - if ($user_shifts === false) { - engelsystem_error('Unable to load users shifts.'); - } } $collides = Shift_collides($shift, $user_shifts); @@ -166,7 +163,11 @@ function Shift_delete_by_psid($shift_psid) { function Shift_delete($shift_id) { mail_shift_delete(Shift($shift_id)); - return sql_query("DELETE FROM `Shifts` WHERE `SID`='" . sql_escape($shift_id) . "'"); + $result = sql_query("DELETE FROM `Shifts` WHERE `SID`='" . sql_escape($shift_id) . "'"); + if ($result === false) { + engelsystem_error('Unable to delete shift.'); + } + return $result; } /** @@ -232,7 +233,7 @@ function Shift_create($shift) { * Return users shifts. */ function Shifts_by_user($user) { - return sql_select(" + $result = sql_select(" SELECT `ShiftTypes`.`id` as `shifttype_id`, `ShiftTypes`.`name`, `ShiftEntry`.*, `Shifts`.*, `Room`.* FROM `ShiftEntry` JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`) @@ -241,6 +242,10 @@ function Shifts_by_user($user) { WHERE `UID`='" . sql_escape($user['UID']) . "' ORDER BY `start` "); + if ($result === false) { + engelsystem_error('Unable to load users shifts.'); + } + return $result; } /** @@ -304,27 +309,29 @@ function Shift($shift_id) { $shiftsEntry_source = sql_select("SELECT `id`, `TID` , `UID` , `freeloaded` FROM `ShiftEntry` WHERE `SID`='" . sql_escape($shift_id) . "'"); if ($shifts_source === false) { - return false; + engelsystem_error('Unable to load shift.'); } - if (count($shifts_source) > 0) { - $result = $shifts_source[0]; - - $result['ShiftEntry'] = $shiftsEntry_source; - $result['NeedAngels'] = []; - - $temp = NeededAngelTypes_by_shift($shift_id); - foreach ($temp as $e) { - $result['NeedAngels'][] = [ - 'TID' => $e['angel_type_id'], - 'count' => $e['count'], - 'restricted' => $e['restricted'], - 'taken' => $e['taken'] - ]; - } - - return $result; + + if (empty($shifts_source)) { + return null; } - return null; + + $result = $shifts_source[0]; + + $result['ShiftEntry'] = $shiftsEntry_source; + $result['NeedAngels'] = []; + + $temp = NeededAngelTypes_by_shift($shift_id); + foreach ($temp as $e) { + $result['NeedAngels'][] = [ + 'TID' => $e['angel_type_id'], + 'count' => $e['count'], + 'restricted' => $e['restricted'], + 'taken' => $e['taken'] + ]; + } + + return $result; } /** -- cgit v1.2.3-70-g09d2