summaryrefslogtreecommitdiff
path: root/includes/controller/shifts_controller.php
diff options
context:
space:
mode:
authormsquare <msquare@notrademark.de>2016-11-09 17:43:56 +0100
committerGitHub <noreply@github.com>2016-11-09 17:43:56 +0100
commitd43eb41d25d0d5c0509417247030dd6c21118cf6 (patch)
tree2c3f78bf8fbd4215e70af7dbc2ce30cfef674816 /includes/controller/shifts_controller.php
parentd5d2acc7d80920eef5f0ed779a3738a12d5db348 (diff)
parent22520532c78b3a032aec6ececb7623ba094da8de (diff)
Merge pull request #274 from engelsystem/task-164-shift-view
Task 164 shift view
Diffstat (limited to 'includes/controller/shifts_controller.php')
-rw-r--r--includes/controller/shifts_controller.php186
1 files changed, 167 insertions, 19 deletions
diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php
index b29a819f..be0cf127 100644
--- a/includes/controller/shifts_controller.php
+++ b/includes/controller/shifts_controller.php
@@ -12,6 +12,172 @@ 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'];
+
+ $shift = Shift($shift_id);
+
+ $room = select_array(Rooms(), 'RID', 'Name');
+ $angeltypes = select_array(AngelTypes(), 'id', 'name');
+ $shifttypes = select_array(ShiftTypes(), 'id', 'name');
+
+ $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;
+ }
+ }
+
+ $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[$_REQUEST['rid']])) {
+ $rid = $_REQUEST['rid'];
+ } else {
+ $valid = false;
+ $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 = 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 = 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);
+ }
+
+ if ($start >= $end) {
+ $valid = false;
+ $msg .= error(_("The ending time has to be after the starting time."), true);
+ }
+
+ 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."), $needed_angeltype_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.');
+ }
+ NeededAngelTypes_delete_by_shift($shift_id);
+ $needed_angel_types_info = [];
+ foreach ($needed_angel_types as $type_id => $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));
+ success(_("Shift updated."));
+
+ redirect(shift_link([
+ 'SID' => $shift_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(), [
+ msg(),
+ '<noscript>' . info(_("This page is much more comfortable with javascript."), true) . '</noscript>',
+ form([
+ form_select('shifttype_id', _('Shifttype'), $shifttypes, $shifttype_id),
+ form_text('title', _("Title"), $title),
+ form_select('rid', _("Room:"), $room, $rid),
+ form_text('start', _("Start:"), date("Y-m-d H:i", $start)),
+ form_text('end', _("End:"), date("Y-m-d H:i", $end)),
+ '<h2>' . _("Needed angels") . '</h2>',
+ $angel_types_spinner,
+ 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 == null) {
+ redirect(page_link_to('user_shifts'));
+ }
+
+ // Schicht löschen bestätigt
+ if (isset($_REQUEST['delete'])) {
+ 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."));
+ 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),
+ '<a class="button" href="?p=user_shifts&delete_shift=' . $shift_id . '&delete">' . _("delete") . '</a>'
+ ]);
+}
+
function shift_controller() {
global $user, $privileges;
@@ -24,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) {