engelsystem/includes/controller/shift_entries_controller.php

385 lines
10 KiB
PHP
Raw Normal View History

2016-10-03 17:41:14 +02:00
<?php
2017-12-25 23:12:52 +01:00
use Engelsystem\ShiftSignupState;
2016-10-03 17:41:14 +02:00
/**
* Route shift entry actions.
2017-12-25 23:12:52 +01:00
*
* @return array
*/
function shift_entries_controller()
{
global $user;
2017-12-27 13:50:53 +01:00
if (!isset($user)) {
2017-12-26 20:41:35 +01:00
redirect(page_link_to('login'));
}
2017-12-25 23:12:52 +01:00
$action = strip_request_item('action');
2018-01-14 17:47:26 +01:00
if (empty($action)) {
redirect(user_link($user));
}
2017-12-25 23:12:52 +01:00
switch ($action) {
case 'create':
return shift_entry_create_controller();
case 'delete':
return shift_entry_delete_controller();
}
}
2016-10-03 17:41:14 +02:00
/**
* Sign up for a shift.
2017-12-25 23:12:52 +01:00
*
* @return array
2016-10-03 17:41:14 +02:00
*/
function shift_entry_create_controller()
2017-01-02 03:57:23 +01:00
{
global $privileges, $user;
$request = request();
2017-12-25 23:12:52 +01:00
if (User_is_freeloader($user)) {
redirect(page_link_to('user_myshifts'));
2017-01-02 03:57:23 +01:00
}
2017-12-25 23:12:52 +01:00
$shift = Shift($request->input('shift_id'));
2018-01-14 17:47:26 +01:00
if (empty($shift)) {
redirect(user_link($user));
2017-01-02 03:57:23 +01:00
}
2017-12-25 23:12:52 +01:00
$angeltype = AngelType($request->input('angeltype_id'));
2017-12-25 23:12:52 +01:00
if (in_array('user_shifts_admin', $privileges)) {
return shift_entry_create_controller_admin($shift, $angeltype);
2017-01-02 03:57:23 +01:00
}
2017-12-25 23:12:52 +01:00
2018-01-14 17:47:26 +01:00
if (empty($angeltype)) {
redirect(user_link($user));
2017-01-02 03:57:23 +01:00
}
2017-12-25 23:12:52 +01:00
if (User_is_AngelType_supporter($user, $angeltype)) {
return shift_entry_create_controller_supporter($shift, $angeltype);
2017-01-02 03:57:23 +01:00
}
2017-12-25 23:12:52 +01:00
return shift_entry_create_controller_user($shift, $angeltype);
}
2017-01-02 15:43:36 +01:00
/**
* Sign up for a shift.
* Case: Admin
*
2017-12-25 23:12:52 +01:00
* @param array $shift
* @param array $angeltype
* @return array
*/
function shift_entry_create_controller_admin($shift, $angeltype)
{
global $user;
$request = request();
2017-12-25 23:12:52 +01:00
$signup_user = $user;
if ($request->has('user_id')) {
$signup_user = User($request->input('user_id'));
2017-01-02 03:57:23 +01:00
}
2018-01-14 17:47:26 +01:00
if (empty($signup_user)) {
2017-01-02 03:57:23 +01:00
redirect(shift_link($shift));
}
2017-12-25 23:12:52 +01:00
$angeltypes = AngelTypes();
2017-12-25 23:12:52 +01:00
if ($request->has('angeltype_id')) {
$angeltype = AngelType($request->input('angeltype_id'));
}
2018-01-14 17:47:26 +01:00
if (empty($angeltype)) {
2017-12-25 23:12:52 +01:00
if (count($angeltypes) == 0) {
redirect(shift_link($shift));
2017-01-02 03:57:23 +01:00
}
$angeltype = $angeltypes[0];
}
2017-12-25 23:12:52 +01:00
if ($request->has('submit')) {
ShiftEntry_create([
2017-12-25 23:12:52 +01:00
'SID' => $shift['SID'],
'TID' => $angeltype['id'],
'UID' => $signup_user['UID'],
'Comment' => '',
'freeloaded' => false,
'freeload_comment' => ''
]);
2017-12-25 23:12:52 +01:00
success(sprintf(__('%s has been subscribed to the shift.'), User_Nick_render($signup_user)));
redirect(shift_link($shift));
}
2017-12-25 23:12:52 +01:00
$users = Users();
$users_select = [];
foreach ($users as $u) {
$users_select[$u['UID']] = $u['Nick'];
}
2017-12-25 23:12:52 +01:00
$angeltypes_select = [];
2017-12-25 23:12:52 +01:00
foreach ($angeltypes as $a) {
$angeltypes_select[$a['id']] = $a['name'];
}
2017-12-25 23:12:52 +01:00
$room = Room($shift['RID']);
return [
ShiftEntry_create_title(),
ShiftEntry_create_view_admin($shift, $room, $angeltype, $angeltypes_select, $signup_user, $users_select)
];
}
2017-01-02 15:43:36 +01:00
/**
* Sign up for a shift.
* Case: Supporter
*
2017-12-25 23:12:52 +01:00
* @param array $shift
* @param array $angeltype
* @return array
*/
function shift_entry_create_controller_supporter($shift, $angeltype)
{
global $user;
$request = request();
2017-12-25 23:12:52 +01:00
$signup_user = $user;
if ($request->has('user_id')) {
$signup_user = User($request->input('user_id'));
}
2017-12-25 23:12:52 +01:00
if (!UserAngelType_exists($signup_user, $angeltype)) {
error(__('User is not in angeltype.'));
redirect(shift_link($shift));
}
2017-12-25 23:12:52 +01:00
$needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype);
$shift_entries = ShiftEntries_by_shift_and_angeltype($shift['SID'], $angeltype['id']);
2017-12-25 23:12:52 +01:00
$shift_signup_state = Shift_signup_allowed(
$signup_user,
$shift,
$angeltype,
null,
null,
$needed_angeltype,
$shift_entries
);
if (!$shift_signup_state->isSignupAllowed()) {
if ($shift_signup_state->getState() == ShiftSignupState::OCCUPIED) {
error(__('This shift is already occupied.'));
2017-01-02 03:57:23 +01:00
}
redirect(shift_link($shift));
}
2017-12-25 23:12:52 +01:00
if ($request->has('submit')) {
ShiftEntry_create([
2017-12-25 23:12:52 +01:00
'SID' => $shift['SID'],
'TID' => $angeltype['id'],
'UID' => $signup_user['UID'],
'Comment' => '',
'freeloaded' => false,
'freeload_comment' => ''
]);
2017-12-25 23:12:52 +01:00
success(sprintf(__('%s has been subscribed to the shift.'), User_Nick_render($signup_user)));
redirect(shift_link($shift));
}
2017-12-25 23:12:52 +01:00
$users = Users_by_angeltype($angeltype);
$users_select = [];
foreach ($users as $u) {
$users_select[$u['UID']] = $u['Nick'];
}
2017-12-25 23:12:52 +01:00
$room = Room($shift['RID']);
return [
ShiftEntry_create_title(),
ShiftEntry_create_view_supporter($shift, $room, $angeltype, $signup_user, $users_select)
];
}
2017-01-02 15:43:36 +01:00
/**
* Generates an error message for the given shift signup state.
2017-12-25 23:12:52 +01:00
*
* @param ShiftSignupState $shift_signup_state
*/
2017-12-25 23:12:52 +01:00
function shift_entry_error_message(ShiftSignupState $shift_signup_state)
{
if ($shift_signup_state->getState() == ShiftSignupState::ANGELTYPE) {
error(__('You need be accepted member of the angeltype.'));
} elseif ($shift_signup_state->getState() == ShiftSignupState::COLLIDES) {
error(__('This shift collides with one of your shifts.'));
} elseif ($shift_signup_state->getState() == ShiftSignupState::OCCUPIED) {
error(__('This shift is already occupied.'));
} elseif ($shift_signup_state->getState() == ShiftSignupState::SHIFT_ENDED) {
error(__('This shift ended already.'));
} elseif ($shift_signup_state->getState() == ShiftSignupState::NOT_ARRIVED) {
error(__('You are not marked as arrived.'));
} elseif ($shift_signup_state->getState() == ShiftSignupState::SIGNED_UP) {
error(__('You are signed up for this shift.'));
}
}
/**
* Sign up for a shift.
* Case: User
*
2017-12-25 23:12:52 +01:00
* @param array $shift
* @param array $angeltype
* @return array
*/
function shift_entry_create_controller_user($shift, $angeltype)
{
global $user;
$request = request();
2017-12-25 23:12:52 +01:00
$signup_user = $user;
$needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype);
$shift_entries = ShiftEntries_by_shift_and_angeltype($shift['SID'], $angeltype['id']);
2017-12-25 23:12:52 +01:00
$shift_signup_state = Shift_signup_allowed(
$signup_user,
$shift,
$angeltype,
null,
null,
$needed_angeltype,
$shift_entries
);
if (!$shift_signup_state->isSignupAllowed()) {
shift_entry_error_message($shift_signup_state);
redirect(shift_link($shift));
}
2017-12-25 23:12:52 +01:00
$comment = '';
if ($request->has('submit')) {
2017-01-02 03:57:23 +01:00
$comment = strip_request_item_nl('comment');
2017-07-23 11:46:54 +02:00
ShiftEntry_create([
2017-12-25 23:12:52 +01:00
'SID' => $shift['SID'],
'TID' => $angeltype['id'],
'UID' => $signup_user['UID'],
'Comment' => $comment,
'freeloaded' => false,
'freeload_comment' => ''
2017-01-02 15:43:36 +01:00
]);
2017-12-25 23:12:52 +01:00
if ($angeltype['restricted'] == false && !UserAngelType_exists($signup_user, $angeltype)) {
UserAngelType_create($signup_user, $angeltype);
2017-01-02 03:57:23 +01:00
}
2017-12-25 23:12:52 +01:00
success(__('You are subscribed. Thank you!'));
2017-01-02 03:57:23 +01:00
redirect(shift_link($shift));
2016-10-03 17:41:14 +02:00
}
2017-12-25 23:12:52 +01:00
$room = Room($shift['RID']);
return [
ShiftEntry_create_title(),
ShiftEntry_create_view_user($shift, $room, $angeltype, $comment)
];
}
2017-01-02 15:43:36 +01:00
/**
* Link to create a shift entry.
*
2017-12-25 23:12:52 +01:00
* @param array $shift
* @param array $angeltype
* @param array $params
* @return string URL
*/
function shift_entry_create_link($shift, $angeltype, $params = [])
{
$params = array_merge([
2017-12-25 23:12:52 +01:00
'action' => 'create',
'shift_id' => $shift['SID'],
'angeltype_id' => $angeltype['id']
], $params);
return page_link_to('shift_entries', $params);
}
2017-01-02 15:43:36 +01:00
/**
* Link to create a shift entry as admin.
*
2017-12-25 23:12:52 +01:00
* @param array $shift
* @param array $params
* @return string URL
*/
function shift_entry_create_link_admin($shift, $params = [])
{
$params = array_merge([
2017-12-25 23:12:52 +01:00
'action' => 'create',
'shift_id' => $shift['SID']
], $params);
return page_link_to('shift_entries', $params);
2016-10-03 17:41:14 +02:00
}
/**
* Load a shift entry from get parameter shift_entry_id.
2017-12-25 23:12:52 +01:00
*
* @return array
2016-10-03 17:41:14 +02:00
*/
function shift_entry_load()
{
$request = request();
2017-12-25 23:12:52 +01:00
if (!$request->has('shift_entry_id') || !test_request_int('shift_entry_id')) {
2017-01-02 03:57:23 +01:00
redirect(page_link_to('user_shifts'));
}
$shiftEntry = ShiftEntry($request->input('shift_entry_id'));
2018-01-14 17:47:26 +01:00
if (empty($shiftEntry)) {
error(__('Shift entry not found.'));
redirect(page_link_to('user_shifts'));
}
2017-12-25 23:12:52 +01:00
return $shiftEntry;
}
2017-01-02 15:43:36 +01:00
/**
* Remove somebody from a shift.
2017-12-25 23:12:52 +01:00
*
* @return array
*/
function shift_entry_delete_controller()
{
2017-12-19 22:24:06 +01:00
global $user;
$request = request();
$shiftEntry = shift_entry_load();
2017-12-25 23:12:52 +01:00
$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 necessary, ask your supporter or heaven to do so.'));
redirect(user_link($signout_user));
}
2017-12-25 23:12:52 +01:00
if ($request->has('continue')) {
ShiftEntry_delete($shiftEntry);
success(__('Shift entry removed.'));
redirect(shift_link($shift));
2017-01-02 03:57:23 +01:00
}
2017-12-25 23:12:52 +01:00
if ($user['UID'] == $signout_user['UID']) {
return [
ShiftEntry_delete_title(),
ShiftEntry_delete_view($shiftEntry, $shift, $angeltype, $signout_user)
];
}
2017-12-25 23:12:52 +01:00
return [
ShiftEntry_delete_title(),
ShiftEntry_delete_view_admin($shiftEntry, $shift, $angeltype, $signout_user)
];
}
/**
* Link to delete a shift entry.
*
2017-12-25 23:12:52 +01:00
* @param array $shiftEntry
* @param array $params
* @return string URL
*/
function shift_entry_delete_link($shiftEntry, $params = [])
{
$params = array_merge([
2017-12-25 23:12:52 +01:00
'action' => 'delete',
'shift_entry_id' => $shiftEntry['id']
], $params);
return page_link_to('shift_entries', $params);
2016-10-03 17:41:14 +02:00
}