From fd85034e7f2277730f4ea4de49dade6d125832dd Mon Sep 17 00:00:00 2001 From: msquare Date: Tue, 19 Dec 2017 20:58:01 +0100 Subject: redo shift signoff and icons for delete/confirm/acknowledgment questions --- includes/controller/angeltypes_controller.php | 6 +- includes/controller/shift_entries_controller.php | 79 ++++++++++------------ includes/controller/user_angeltypes_controller.php | 8 +-- includes/model/ShiftEntry_model.php | 21 ++++-- includes/model/Shifts_model.php | 32 +++++++++ includes/pages/user_myshifts.php | 50 ++------------ includes/pages/user_shifts.php | 3 +- includes/view/AngelTypes_view.php | 17 ++--- includes/view/ShiftEntry_view.php | 71 +++++++++++++++++++ includes/view/ShiftTypes_view.php | 6 +- includes/view/UserAngelTypes_view.php | 51 +++++--------- includes/view/User_view.php | 14 +--- 12 files changed, 197 insertions(+), 161 deletions(-) (limited to 'includes') diff --git a/includes/controller/angeltypes_controller.php b/includes/controller/angeltypes_controller.php index a47c1340..621b5cbd 100644 --- a/includes/controller/angeltypes_controller.php +++ b/includes/controller/angeltypes_controller.php @@ -40,11 +40,13 @@ function angeltypes_controller() * Path to angeltype view. * * @param int $angeltype_id AngelType id + * @param array $params additional params * @return string */ -function angeltype_link($angeltype_id) +function angeltype_link($angeltype_id, $params = []) { - return page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype_id]); + $params = array_merge(['action' => 'view', 'angeltype_id' => $angeltype_id], $params); + return page_link_to('angeltypes', $params); } /** diff --git a/includes/controller/shift_entries_controller.php b/includes/controller/shift_entries_controller.php index c28a3e9c..0b37be6c 100644 --- a/includes/controller/shift_entries_controller.php +++ b/includes/controller/shift_entries_controller.php @@ -233,58 +233,49 @@ function shift_entry_add_controller() } /** - * Remove somebody from a shift. + * Load a shift entry from get parameter entry_id. */ -function shift_entry_delete_controller() -{ - global $privileges, $user; +function shift_entry_load() { $request = request(); if (!$request->has('entry_id') || !test_request_int('entry_id')) { redirect(page_link_to('user_shifts')); } - $entry_id = $request->input('entry_id'); - - $shift_entry_source = DB::selectOne(' - SELECT - `User`.`Nick`, - `User`.`Gekommen`, - `ShiftEntry`.`Comment`, - `ShiftEntry`.`UID`, - `ShiftTypes`.`name`, - `Shifts`.*, - `Room`.`Name`, - `AngelTypes`.`name` AS `angel_type`, - `AngelTypes`.`id` AS `angeltype_id` - 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`=?', - [$entry_id] - ); - if (!empty($shift_entry_source)) { - if (!in_array('user_shifts_admin', $privileges) && (!in_array('shiftentry_edit_angeltype_supporter', - $privileges) || !User_is_AngelType_supporter($user, AngelType($shift_entry_source['angeltype_id']))) - ) { - redirect(page_link_to('user_shifts')); - } + $shiftEntry = ShiftEntry($request->input('entry_id')); + if($shiftEntry == null) { + error(_('Shift entry not found.')); + redirect(page_link_to('user_shifts')); + } + + return $shiftEntry; +} - ShiftEntry_delete($entry_id); +/** + * Remove somebody from a shift. + */ +function shift_entry_delete_controller() +{ + global $privileges, $user; + $request = request(); + $shiftEntry = shift_entry_load(); - 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.')); + $shift = Shift($shiftEntry['SID']); + $angeltype = AngelType($shiftEntry['TID']); + $signout_user = User($shiftEntry['UID']); + if(!Shift_signout_allowed($shift, $angeltype, $signout_user)) { + error(_('You are not allowed to remove this shift entry. If neccessary, ask your supporter or heaven to do so.')); + redirect(user_link($signout_user)); + } + + if($request->has('continue')) { + ShiftEntry_delete($shiftEntry); + success(_('Shift entry removed.')); + redirect(shift_link($shift)); } - redirect(shift_link($shift_entry_source)); + if($user['UID'] == $signout_user['UID']) { + return ShiftEntry_delete_view($shiftEntry, $shift, $angeltype, $signout_user); + } + + return ShiftEntry_delete_view_admin($shiftEntry, $shift, $angeltype, $signout_user); } diff --git a/includes/controller/user_angeltypes_controller.php b/includes/controller/user_angeltypes_controller.php index 8f21fa32..543ba150 100644 --- a/includes/controller/user_angeltypes_controller.php +++ b/includes/controller/user_angeltypes_controller.php @@ -90,13 +90,7 @@ function user_angeltypes_confirm_all_controller() redirect(page_link_to('angeltypes')); } - $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype); - if ($user_angeltype == null) { - error(_('User angeltype doesn\'t exist.')); - redirect(page_link_to('angeltypes')); - } - - if (!in_array('admin_user_angeltypes', $privileges) && !$user_angeltype['supporter']) { + if (!in_array('admin_user_angeltypes', $privileges) && !User_is_AngelType_supporter($user, $angeltype)) { error(_('You are not allowed to confirm all users for this angeltype.')); redirect(page_link_to('angeltypes')); } diff --git a/includes/model/ShiftEntry_model.php b/includes/model/ShiftEntry_model.php index bcc28f05..01b2369e 100644 --- a/includes/model/ShiftEntry_model.php +++ b/includes/model/ShiftEntry_model.php @@ -134,13 +134,26 @@ function ShiftEntry($shift_entry_id) /** * Delete a shift entry. * - * @param int $shift_entry_id + * @param array $shiftEntry */ -function ShiftEntry_delete($shift_entry_id) +function ShiftEntry_delete($shiftEntry) { - $shift_entry = ShiftEntry($shift_entry_id); - mail_shift_removed(User($shift_entry['UID']), Shift($shift_entry['SID'])); + mail_shift_removed(User($shiftEntry['UID']), Shift($shiftEntry['SID'])); DB::delete('DELETE FROM `ShiftEntry` WHERE `id` = ?', [$shift_entry_id]); + + $signout_user = User($shiftEntry['UID']); + $shift = Shift($shiftEntry['SID']); + $shifttype = ShiftType($shift['shifttype_id']); + $room = Room($shift['RID']); + $angeltype = AngelType($shiftEntry['TID']); + + engelsystem_log( + 'Shift signout: '. User_Nick_render($signout_user) . ' from shift ' . $shifttype['name'] + . ' at ' . $room['Name'] + . ' from ' . date('Y-m-d H:i', $shift['start']) + . ' to ' . date('Y-m-d H:i', $shift['end']) + . ' as ' . $angeltype['name'] + ); } /** diff --git a/includes/model/Shifts_model.php b/includes/model/Shifts_model.php index 8cbbe2aa..f3bb042a 100644 --- a/includes/model/Shifts_model.php +++ b/includes/model/Shifts_model.php @@ -402,6 +402,38 @@ function Shift_signup_allowed_admin($needed_angeltype, $shift_entries) return new ShiftSignupState(ShiftSignupState::FREE, $free_entries); } +/** + * Check if an angel can signout from a shift. + * + * @param $shift The shift + * @param $angeltype The angeltype + * @param $signout_user The user that was signed up for the shift + * + * @return bool + */ +function Shift_signout_allowed($shift, $angeltype, $signout_user) { + global $user, $privileges; + + // user shifts admin can sign out any user at any time + if (in_array('user_shifts_admin', $privileges)) { + return true; + } + + // angeltype supporter can sign out any user at any time from their supported angeltype + if ( + in_array('shiftentry_edit_angeltype_supporter', $privileges) + && User_is_AngelType_supporter($user, $angeltype) + ) { + return true; + } + + if($signout_user['UID'] == $user['UID'] && $shift['start'] > time() + config('last_unsubscribe') * 3600) { + return true; + } + + return false; +} + /** * Check if an angel can sign up for given shift. * diff --git a/includes/pages/user_myshifts.php b/includes/pages/user_myshifts.php index 836bd566..f605792f 100644 --- a/includes/pages/user_myshifts.php +++ b/includes/pages/user_myshifts.php @@ -26,12 +26,12 @@ function user_myshifts() && preg_match('/^\d{1,}$/', $request->input('id')) && count(DB::select('SELECT `UID` FROM `User` WHERE `UID`=?', [$request->input('id')])) > 0 ) { - $user_id = $request->input('id'); + $shift_entry_id = $request->input('id'); } else { - $user_id = $user['UID']; + $shift_entry_id = $user['UID']; } - $shifts_user = DB::selectOne('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$user_id]); + $shifts_user = DB::selectOne('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$shift_entry_id]); if ($request->has('reset')) { if ($request->input('reset') == 'ack') { @@ -47,7 +47,7 @@ function user_myshifts() button(page_link_to('user_myshifts', ['reset' => 'ack']), _('Continue'), 'btn-danger') ]); } elseif ($request->has('edit') && preg_match('/^\d+$/', $request->input('edit'))) { - $user_id = $request->input('edit'); + $shift_entry_id = $request->input('edit'); $shift = DB::selectOne(' SELECT `ShiftEntry`.`freeloaded`, @@ -68,7 +68,7 @@ function user_myshifts() LIMIT 1 ', [ - $user_id, + $shift_entry_id, $shifts_user['UID'], ] ); @@ -92,7 +92,7 @@ function user_myshifts() if ($valid) { ShiftEntry_update([ - 'id' => $user_id, + 'id' => $shift_entry_id, 'Comment' => $comment, 'freeloaded' => $freeloaded, 'freeload_comment' => $freeload_comment @@ -124,44 +124,6 @@ function user_myshifts() } else { redirect(page_link_to('user_myshifts')); } - } elseif ($request->has('cancel') && preg_match('/^\d+$/', $request->input('cancel'))) { - $user_id = $request->input('cancel'); - $shift = DB::selectOne(' - SELECT * - FROM `Shifts` - INNER JOIN `ShiftEntry` USING (`SID`) - WHERE `ShiftEntry`.`id`=? AND `UID`=? - ', - [ - $user_id, - $shifts_user['UID'], - ] - ); - if (count($shift) > 0) { - if ( - ($shift['start'] > time() + config('last_unsubscribe') * 3600) - || in_array('user_shifts_admin', $privileges) - ) { - ShiftEntry_delete($user_id); - - $room = Room($shift['RID']); - $angeltype = AngelType($shift['TID']); - $shifttype = ShiftType($shift['shifttype_id']); - - engelsystem_log( - 'Deleted own shift: ' . $shifttype['name'] - . ' at ' . $room['Name'] - . ' from ' . date('Y-m-d H:i', $shift['start']) - . ' to ' . date('Y-m-d H:i', $shift['end']) - . ' as ' . $angeltype['name'] - ); - success(_('Shift canceled.')); - } else { - error(_('It\'s too late to sign yourself off the shift. If neccessary, ask the dispatcher to do so.')); - } - } else { - redirect(user_link($shifts_user)); - } } redirect(page_link_to('users', ['action' => 'view', 'user_id' => $shifts_user['UID']])); diff --git a/includes/pages/user_shifts.php b/includes/pages/user_shifts.php index 355a7bb6..9ff9ba1a 100644 --- a/includes/pages/user_shifts.php +++ b/includes/pages/user_shifts.php @@ -31,8 +31,7 @@ function user_shifts() // Löschen einzelner Schicht-Einträge (Also Belegung einer Schicht von Engeln) durch Admins if ($request->has('entry_id')) { - shift_entry_delete_controller(); - return ''; + return shift_entry_delete_controller(); } elseif ($request->has('edit_shift')) { return shift_edit_controller(); } elseif ($request->has('delete_shift')) { diff --git a/includes/view/AngelTypes_view.php b/includes/view/AngelTypes_view.php index fa9136c8..a8b34df8 100644 --- a/includes/view/AngelTypes_view.php +++ b/includes/view/AngelTypes_view.php @@ -50,14 +50,14 @@ function AngelType_delete_view($angeltype) return page_with_title(sprintf(_('Delete angeltype %s'), $angeltype['name']), [ info(sprintf(_('Do you want to delete angeltype %s?'), $angeltype['name']), true), buttons([ - button(page_link_to('angeltypes'), _('cancel'), 'cancel'), + button(page_link_to('angeltypes'), glyph('remove') . _('cancel')), button( page_link_to( 'angeltypes', ['action' => 'delete', 'angeltype_id' => $angeltype['id'], 'confirmed' => 1] ), - _('delete'), - 'ok' + glyph('ok') . _('delete'), + 'btn-danger' ) ]) ]); @@ -153,7 +153,7 @@ function AngelType_view_buttons($angeltype, $user_angeltype, $admin_angeltypes, } $buttons[] = button( page_link_to('user_angeltypes', ['action' => 'delete', 'user_angeltype_id' => $user_angeltype['id']]), - _('leave'), 'cancel' + _('leave') ); } @@ -442,13 +442,11 @@ function AngelType_view_info( $info[] = buttons([ button( page_link_to('user_angeltypes', ['action' => 'confirm_all', 'angeltype_id' => $angeltype['id']]), - _('confirm all'), - 'ok' + glyph('ok') . _('confirm all') ), button( page_link_to('user_angeltypes', ['action' => 'delete_all', 'angeltype_id' => $angeltype['id']]), - _('deny all'), - 'cancel' + glyph('remove') . _('deny all') ) ]); $info[] = table($table_headers, $members_unconfirmed); @@ -523,8 +521,7 @@ function AngelTypes_about_view_angeltype($angeltype) 'user_angeltypes', ['action' => 'delete', 'user_angeltype_id' => $angeltype['user_angeltype_id']] ), - _('leave'), - 'cancel' + _('leave') ); } else { $buttons[] = button( diff --git a/includes/view/ShiftEntry_view.php b/includes/view/ShiftEntry_view.php index 2e638df6..6dceac10 100644 --- a/includes/view/ShiftEntry_view.php +++ b/includes/view/ShiftEntry_view.php @@ -1,5 +1,76 @@ 1]), glyph('ok') . _('delete'), 'btn-danger') + ]) + ]); +} + +/** + * Sign off from a shift, asking for ack. + * + * @param array $shiftEntry + * @param array $shift + * @param array $angeltype + * @param array $signoff_user + * + * @return string HTML + */ +function ShiftEntry_delete_view($shiftEntry, $shift, $angeltype, $signoff_user) { + return page_with_title(ShiftEntry_delete_title(), [ + info(sprintf( + _('Do you want to sign off from your shift %s from %s to %s as %s?'), + $shift['name'], + date('Y-m-d H:i', $shift['start']), + date('Y-m-d H:i', $shift['end']), + $angeltype['name'] + ), true), + buttons([ + button(user_link($signoff_user), glyph('remove') . _('cancel')), + button(ShiftEntry_delete_link($shiftEntry, ['continue' => 1]), glyph('ok') . _('delete'), 'btn-danger') + ]) + ]); +} + +/** + * Link to delete a shift entry. + * @param array $shiftEntry + * + * @return string URL + */ +function ShiftEntry_delete_link($shiftEntry, $params = []) { + $params = array_merge(['entry_id' => $shiftEntry['id']], $params); + return page_link_to('user_shifts', $params); +} + +/** + * Title for deleting a shift entry. + */ +function ShiftEntry_delete_title() { + return _('Shift sign off'); +} + /** * Display form for adding/editing a shift entry. * diff --git a/includes/view/ShiftTypes_view.php b/includes/view/ShiftTypes_view.php index 74e0d7c0..7dee4521 100644 --- a/includes/view/ShiftTypes_view.php +++ b/includes/view/ShiftTypes_view.php @@ -22,14 +22,14 @@ function ShiftType_delete_view($shifttype) return page_with_title(sprintf(_('Delete shifttype %s'), $shifttype['name']), [ info(sprintf(_('Do you want to delete shifttype %s?'), $shifttype['name']), true), buttons([ - button(page_link_to('shifttypes'), _('cancel'), 'cancel'), + button(page_link_to('shifttypes'), glyph('remove') . _('cancel')), button( page_link_to( 'shifttypes', ['action' => 'delete', 'shifttype_id' => $shifttype['id'], 'confirmed' => 1] ), - _('delete'), - 'ok btn-danger' + glyph('ok') . _('delete'), + 'btn-danger' ) ]) ]); diff --git a/includes/view/UserAngelTypes_view.php b/includes/view/UserAngelTypes_view.php index 98f6c3e9..c46c1ee1 100644 --- a/includes/view/UserAngelTypes_view.php +++ b/includes/view/UserAngelTypes_view.php @@ -21,8 +21,7 @@ function UserAngelType_update_view($user_angeltype, $user, $angeltype, $supporte buttons([ button( page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]), - _('cancel'), - 'cancel' + glyph('remove') . _('cancel') ), button( page_link_to('user_angeltypes', [ @@ -31,8 +30,8 @@ function UserAngelType_update_view($user_angeltype, $user, $angeltype, $supporte 'supporter' => ($supporter ? '1' : '0'), 'confirmed' => 1, ]), - _('yes'), - 'ok' + glyph('ok') . _('yes'), + 'btn-primary' ) ]) ]); @@ -53,16 +52,15 @@ function UserAngelTypes_delete_all_view($angeltype) 'angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']] ), - _('cancel'), - 'cancel' + glyph('remove') . _('cancel') ), button( page_link_to( 'user_angeltypes', ['action' => 'delete_all', 'angeltype_id' => $angeltype['id'], 'confirmed' => 1] ), - _('yes'), - 'ok' + glyph('ok') . _('yes'), + 'btn-primary' ) ]) ]); @@ -78,13 +76,12 @@ function UserAngelTypes_confirm_all_view($angeltype) msg(), info(sprintf(_('Do you really want to confirm all users for %s?'), $angeltype['name']), true), buttons([ - button(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]), _('cancel'), - 'cancel'), + button(angeltype_link($angeltype['id']), glyph('remove') . _('cancel')), button( page_link_to('user_angeltypes', ['action' => 'confirm_all', 'angeltype_id' => $angeltype['id'], 'confirmed' => 1]), - _('yes'), - 'ok' + glyph('ok') . _('yes'), + 'btn-primary' ) ]) ]); @@ -102,18 +99,14 @@ function UserAngelType_confirm_view($user_angeltype, $user, $angeltype) msg(), info(sprintf(_('Do you really want to confirm %s for %s?'), User_Nick_render($user), $angeltype['name']), true), buttons([ - button( - page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]), - _('cancel'), - 'cancel' - ), + button(angeltype_link($angeltype['id']), glyph('remove') . _('cancel')), button( page_link_to( 'user_angeltypes', ['action' => 'confirm', 'user_angeltype_id' => $user_angeltype['id'], 'confirmed' => 1] ), - _('yes'), - 'ok' + glyph('ok') . _('yes'), + 'btn-primary' ) ]) ]); @@ -131,16 +124,12 @@ function UserAngelType_delete_view($user_angeltype, $user, $angeltype) msg(), info(sprintf(_('Do you really want to delete %s from %s?'), User_Nick_render($user), $angeltype['name']), true), buttons([ - button( - page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]), - _('cancel'), - 'cancel' - ), + button(angeltype_link($angeltype['id']), glyph('remove') . _('cancel')), button( page_link_to('user_angeltypes', ['action' => 'delete', 'user_angeltype_id' => $user_angeltype['id'], 'confirmed' => 1]), - _('yes'), - 'ok' + glyph('ok') . _('yes'), + 'btn-primary' ) ]) ]); @@ -187,18 +176,14 @@ function UserAngelType_join_view($user, $angeltype) msg(), info(sprintf(_('Do you really want to add %s to %s?'), User_Nick_render($user), $angeltype['name']), true), buttons([ - button( - page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]), - _('cancel'), - 'cancel' - ), + button(angeltype_link($angeltype['id']), glyph('remove') . _('cancel')), button( page_link_to( 'user_angeltypes', ['action' => 'add', 'angeltype_id' => $angeltype['id'], 'user_id' => $user['UID'], 'confirmed' => 1] ), - _('save'), - 'ok' + glyph('ok') . _('save'), + 'btn-primary' ) ]) ]); diff --git a/includes/view/User_view.php b/includes/view/User_view.php index 04fb4d04..e47603fb 100644 --- a/includes/view/User_view.php +++ b/includes/view/User_view.php @@ -363,19 +363,9 @@ function User_view_myshift($shift, $user_source, $its_me) 'btn-xs' ); } - if ( - ($shift['start'] > time() + config('last_unsubscribe') * 3600) - || in_array('user_shifts_admin', $privileges) - ) { - $parameters = [ - 'cancel' => $shift['id'], - 'id' => $user_source['UID'], - ]; - if ($its_me) { - $parameters['id'] = ''; - } + if (Shift_signout_allowed($shift, ['id' => $shift['TID']], $user_source)) { $myshift['actions'][] = button( - page_link_to('user_myshifts', $parameters), + ShiftEntry_delete_link($shift), glyph('trash') . _('sign off'), 'btn-xs' ); -- cgit v1.2.3-54-g00ecf