diff options
Diffstat (limited to 'includes/controller')
-rw-r--r-- | includes/controller/rooms_controller.php | 7 | ||||
-rw-r--r-- | includes/controller/shifts_controller.php | 100 | ||||
-rw-r--r-- | includes/controller/shifttypes_controller.php | 167 | ||||
-rw-r--r-- | includes/controller/users_controller.php | 49 |
4 files changed, 298 insertions, 25 deletions
diff --git a/includes/controller/rooms_controller.php b/includes/controller/rooms_controller.php new file mode 100644 index 00000000..5d55e1b7 --- /dev/null +++ b/includes/controller/rooms_controller.php @@ -0,0 +1,7 @@ +<?php + +function room_link($room) { + return page_link_to('admin_rooms') . '&show=edit&id=' . $room['RID']; +} + +?>
\ No newline at end of file diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php index 868e903b..90753217 100644 --- a/includes/controller/shifts_controller.php +++ b/includes/controller/shifts_controller.php @@ -1,5 +1,96 @@ <?php +function shift_link($shift) { + return page_link_to('shifts') . '&action=view&shift_id=' . $shift['SID']; +} + +function shift_delete_link($shift) { + return page_link_to('user_shifts') . '&delete_shift=' . $shift['SID']; +} + +function shift_edit_link($shift) { + return page_link_to('user_shifts') . '&edit_shift=' . $shift['SID']; +} + +function shift_controller() { + global $user, $privileges; + + if (! in_array('user_shifts', $privileges)) + redirect(page_link_to('?')); + + if (! isset($_REQUEST['shift_id'])) + redirect(page_link_to('user_shifts')); + + $shift = Shift($_REQUEST['shift_id']); + if ($shift === false) + engelsystem_error('Unable to load shift.'); + if ($shift == null) { + 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) + if ($user_shift['SID'] == $shift['SID']) { + $signed_up = true; + break; + } + + return [ + $shift['name'], + Shift_view($shift, $shifttype, $room, in_array('admin_shifts', $privileges), $angeltypes, in_array('user_shifts_admin', $privileges), in_array('admin_rooms', $privileges), in_array('shifttypes', $privileges), $user_shifts, $signed_up) + ]; +} + +function shifts_controller() { + if (! isset($_REQUEST['action'])) + redirect(page_link_to('user_shifts')); + + switch ($_REQUEST['action']) { + default: + redirect(page_link_to('?')); + case 'view': + return shift_controller(); + case 'next': + return shift_next_controller(); + } +} + +/** + * Redirects the user to his next shift. + */ +function shift_next_controller() { + global $user, $privileges; + + if (! in_array('user_shifts', $privileges)) + redirect(page_link_to('?')); + + $upcoming_shifts = ShiftEntries_upcoming_for_user($user); + if ($upcoming_shifts === false) + return false; + + if (count($upcoming_shifts) > 0) + redirect(shift_link($upcoming_shifts[0])); + + redirect(page_link_to('user_shifts')); +} + /** * Export all shifts using api-key. */ @@ -48,7 +139,14 @@ function shifts_json_export_controller() { require_once realpath(__DIR__ . '/../pages/user_shifts.php'); view_user_shifts(); } else { - $ical_shifts = sql_select("SELECT `Shifts`.*, `Room`.`Name` as `room_name` FROM `ShiftEntry` INNER JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`) INNER JOIN `Room` ON (`Shifts`.`RID` = `Room`.`RID`) WHERE `UID`=" . sql_escape($user['UID']) . " ORDER BY `start`"); + $ical_shifts = sql_select(" + SELECT `ShiftTypes`.`name`, `Shifts`.*, `Room`.`Name` as `room_name` + FROM `ShiftEntry` + INNER JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`) + INNER JOIN `ShiftTypes` ON (`Shifts`.`shifttype_id`=`ShiftTypes`.`id`) + INNER JOIN `Room` ON (`Shifts`.`RID` = `Room`.`RID`) + WHERE `UID`=" . sql_escape($user['UID']) . " + ORDER BY `start`"); } header("Content-Type: application/json; charset=utf-8"); diff --git a/includes/controller/shifttypes_controller.php b/includes/controller/shifttypes_controller.php new file mode 100644 index 00000000..443ce470 --- /dev/null +++ b/includes/controller/shifttypes_controller.php @@ -0,0 +1,167 @@ +<?php + +function shifttype_link($shifttype) { + return page_link_to('shifttypes') . '&action=view&shifttype_id=' . $shifttype['id']; +} + +/** + * Delete a shifttype. + */ +function shifttype_delete_controller() { + if (! isset($_REQUEST['shifttype_id'])) + redirect(page_link_to('shifttypes')); + $shifttype = ShiftType($_REQUEST['shifttype_id']); + if ($shifttype === false) + engelsystem_error('Unable to load shifttype.'); + if ($shifttype == null) + redirect(page_link_to('shifttypes')); + + if (isset($_REQUEST['confirmed'])) { + $result = ShiftType_delete($shifttype['id']); + if ($result === false) + engelsystem_error('Unable to delete shifttype.'); + + engelsystem_log('Deleted shifttype ' . $shifttype['name']); + success(sprintf(_('Shifttype %s deleted.'), $shifttype['name'])); + redirect(page_link_to('shifttypes')); + } + + return array( + sprintf(_("Delete shifttype %s"), $shifttype['name']), + ShiftType_delete_view($shifttype) + ); +} + +/** + * Edit or create shift type. + */ +function shifttype_edit_controller() { + $shifttype_id = null; + $name = ""; + $angeltype_id = null; + $description = ""; + + $angeltypes = AngelTypes(); + if ($angeltypes === false) + engelsystem_error("Unable to load angel types."); + + if (isset($_REQUEST['shifttype_id'])) { + $shifttype = ShiftType($_REQUEST['shifttype_id']); + if ($shifttype === false) + engelsystem_error('Unable to load shifttype.'); + if ($shifttype == null) { + error(_('Shifttype not found.')); + redirect(page_link_to('shifttypes')); + } + $shifttype_id = $shifttype['id']; + $name = $shifttype['name']; + $angeltype_id = $shifttype['angeltype_id']; + $description = $shifttype['description']; + } + + if (isset($_REQUEST['submit'])) { + $ok = true; + + if (isset($_REQUEST['name']) && $_REQUEST['name'] != '') + $name = strip_request_item('name'); + else { + $ok = false; + error(_('Please enter a name.')); + } + + if (isset($_REQUEST['angeltype_id']) && preg_match("/^[0-9]+$/", $_REQUEST['angeltype_id'])) + $angeltype_id = $_REQUEST['angeltype_id']; + else + $angeltype_id = null; + + if (isset($_REQUEST['description'])) + $description = strip_request_item_nl('description'); + + if ($ok) { + if ($shifttype_id) { + $result = ShiftType_update($shifttype_id, $name, $angeltype_id, $description); + if ($result === false) + engelsystem_error('Unable to update shifttype.'); + engelsystem_log('Updated shifttype ' . $name); + success(_('Updated shifttype.')); + } else { + $shifttype_id = ShiftType_create($name, $angeltype_id, $description); + if ($shifttype_id === false) + engelsystem_error('Unable to create shifttype.'); + engelsystem_log('Created shifttype ' . $name); + success(_('Created shifttype.')); + } + redirect(page_link_to('shifttypes') . '&action=view&shifttype_id=' . $shifttype_id); + } + } + + return [ + shifttypes_title(), + ShiftType_edit_view($name, $angeltype_id, $angeltypes, $description, $shifttype_id) + ]; +} + +function shifttype_controller() { + if (! isset($_REQUEST['shifttype_id'])) + redirect(page_link_to('shifttypes')); + $shifttype = ShiftType($_REQUEST['shifttype_id']); + if ($shifttype === false) + engelsystem_error('Unable to load shifttype.'); + if ($shifttype == null) + redirect(page_link_to('shifttypes')); + + $angeltype = null; + if ($shifttype['angeltype_id'] != null) { + $angeltype = AngelType($shifttype['angeltype_id']); + if ($angeltype === false) + engelsystem_error('Unable to load angeltype.'); + } + + return [ + $shifttype['name'], + ShiftType_view($shifttype, $angeltype) + ]; +} + +/** + * List all shift types. + */ +function shifttypes_list_controller() { + $shifttypes = ShiftTypes(); + if ($shifttypes === false) + engelsystem_error("Unable to load shifttypes."); + + return [ + shifttypes_title(), + ShiftTypes_list_view($shifttypes) + ]; +} + +/** + * Text for shift type related links. + */ +function shifttypes_title() { + return _("Shifttypes"); +} + +/** + * Route shift type actions + */ +function shifttypes_controller() { + if (! isset($_REQUEST['action'])) + $_REQUEST['action'] = 'list'; + + switch ($_REQUEST['action']) { + default: + case 'list': + return shifttypes_list_controller(); + case 'view': + return shifttype_controller(); + case 'edit': + return shifttype_edit_controller(); + case 'delete': + return shifttype_delete_controller(); + } +} + +?>
\ No newline at end of file diff --git a/includes/controller/users_controller.php b/includes/controller/users_controller.php index 2bccc609..3a23835c 100644 --- a/includes/controller/users_controller.php +++ b/includes/controller/users_controller.php @@ -5,13 +5,13 @@ */ function users_controller() { global $privileges, $user; - + if (! isset($user)) redirect(page_link_to('')); - + if (! isset($_REQUEST['action'])) $_REQUEST['action'] = 'list'; - + switch ($_REQUEST['action']) { default: case 'list': @@ -27,16 +27,17 @@ function users_controller() { function user_controller() { global $privileges, $user; - + if (isset($_REQUEST['user_id'])) { $user_source = User($_REQUEST['user_id']); } else $user_source = $user; - + $admin_user_privilege = in_array('admin_user', $privileges); - + $shifts = Shifts_by_user($user_source); foreach ($shifts as &$shift) { + // TODO: Move queries to model $shift['needed_angeltypes'] = sql_select("SELECT DISTINCT `AngelTypes`.* FROM `ShiftEntry` JOIN `AngelTypes` ON `ShiftEntry`.`TID`=`AngelTypes`.`id` WHERE `ShiftEntry`.`SID`=" . sql_escape($shift['SID']) . " ORDER BY `AngelTypes`.`name`"); foreach ($shift['needed_angeltypes'] as &$needed_angeltype) { $needed_angeltype['users'] = sql_select(" @@ -47,13 +48,13 @@ function user_controller() { AND `ShiftEntry`.`TID`=" . sql_escape($needed_angeltype['id'])); } } - + if ($user_source['api_key'] == "") User_reset_api_key($user_source, false); - + return array( $user_source['Nick'], - User_view($user_source, $admin_user_privilege, User_is_freeloader($user_source), User_angeltypes($user_source), User_groups($user_source), $shifts, $user['UID'] == $user_source['UID']) + User_view($user_source, $admin_user_privilege, User_is_freeloader($user_source), User_angeltypes($user_source), User_groups($user_source), $shifts, $user['UID'] == $user_source['UID']) ); } @@ -62,24 +63,24 @@ function user_controller() { */ function users_list_controller() { global $privileges; - + if (! in_array('admin_user', $privileges)) redirect(page_link_to('')); - + $order_by = 'Nick'; if (isset($_REQUEST['OrderBy']) && in_array($_REQUEST['OrderBy'], User_sortable_columns())) $order_by = $_REQUEST['OrderBy']; - + $users = Users($order_by); if ($users === false) engelsystem_error('Unable to load users.'); - + foreach ($users as &$user) $user['freeloads'] = count(ShiftEntries_freeloaded_by_user($user)); - + return array( _('All users'), - Users_view($users, $order_by, User_arrived_count(), User_active_count(), User_force_active_count(), ShiftEntries_freeleaded_count(), User_tshirts_count()) + Users_view($users, $order_by, User_arrived_count(), User_active_count(), User_force_active_count(), ShiftEntries_freeleaded_count(), User_tshirts_count()) ); } @@ -96,10 +97,10 @@ function user_password_recovery_controller() { error(_("Token is not correct.")); redirect(page_link_to('login')); } - + if (isset($_REQUEST['submit'])) { $ok = true; - + if (isset($_REQUEST['password']) && strlen($_REQUEST['password']) >= MIN_PASSWORD_LENGTH) { if ($_REQUEST['password'] != $_REQUEST['password2']) { $ok = false; @@ -109,22 +110,22 @@ function user_password_recovery_controller() { $ok = false; error(_("Your password is to short (please use at least 6 characters).")); } - + if ($ok) { $result = set_password($user_source['UID'], $_REQUEST['password']); if ($result === false) engelsystem_error(_("Password could not be updated.")); - + success(_("Password saved.")); redirect(page_link_to('login')); } } - + return User_password_set_view(); } else { if (isset($_REQUEST['submit'])) { $ok = true; - + if (isset($_REQUEST['email']) && strlen(strip_request_item('email')) > 0) { $email = strip_request_item('email'); if (check_email($email)) { @@ -143,7 +144,7 @@ function user_password_recovery_controller() { $ok = false; error(_("Please enter your e-mail.")); } - + if ($ok) { $token = User_generate_password_recovery_token($user_source); if ($token === false) @@ -151,12 +152,12 @@ function user_password_recovery_controller() { $result = engelsystem_email_to_user($user_source, _("Password recovery"), sprintf(_("Please visit %s to recover your password."), page_link_to_absolute('user_password_recovery') . '&token=' . $token)); if ($result === false) engelsystem_error("Unable to send password recovery email."); - + success(_("We sent an email containing your password recovery link.")); redirect(page_link_to('login')); } } - + return User_password_recovery_view(); } } |