@ -1,8 +1,11 @@
<?php
use Engelsystem\ShiftSignupState;
/**
* Route shift entry actions.
*
* @return array
*/
function shift_entries_controller()
{
@ -16,8 +19,6 @@ function shift_entries_controller()
switch ($action) {
case 'create':
return shift_entry_create_controller();
case 'edit':
return shift_entry_edit_controller();
case 'delete':
return shift_entry_delete_controller();
}
@ -25,6 +26,8 @@ function shift_entries_controller()
/**
* Sign up for a shift.
*
* @return array
*/
function shift_entry_create_controller()
{
@ -63,6 +66,7 @@ function shift_entry_create_controller()
*
* @param array $shift
* @param array $angeltype
* @return array
*/
function shift_entry_create_controller_admin($shift, $angeltype)
{
@ -73,16 +77,16 @@ function shift_entry_create_controller_admin($shift, $angeltype)
if ($request->has('user_id')) {
$signup_user = User($request->input('user_id'));
}
if($signup_user == null) {
if ($signup_user == null) {
redirect(shift_link($shift));
}
$angeltypes = AngelTypes();
if($request->has('angeltype_id')) {
if ($request->has('angeltype_id')) {
$angeltype = AngelType($request->input('angeltype_id'));
}
if($angeltype == null) {
if(count($angeltypes) == 0) {
if ($angeltype == null) {
if (count($angeltypes) == 0) {
redirect(shift_link($shift));
}
$angeltype = $angeltypes[0];
@ -90,11 +94,11 @@ function shift_entry_create_controller_admin($shift, $angeltype)
if ($request->has('submit')) {
ShiftEntry_create([
'SID' => $shift['SID'],
'TID' => $angeltype['id'],
'UID' => $signup_user['UID'],
'Comment' => '',
'freeloaded' => false,
'SID' => $shift['SID'],
'TID' => $angeltype['id'],
'UID' => $signup_user['UID'],
'Comment' => '',
'freeloaded' => false,
'freeload_comment' => ''
]);
@ -109,7 +113,7 @@ function shift_entry_create_controller_admin($shift, $angeltype)
}
$angeltypes_select = [];
foreach($angeltypes as $a) {
foreach ($angeltypes as $a) {
$angeltypes_select[$a['id']] = $a['name'];
}
@ -126,6 +130,7 @@ function shift_entry_create_controller_admin($shift, $angeltype)
*
* @param array $shift
* @param array $angeltype
* @return array
*/
function shift_entry_create_controller_supporter($shift, $angeltype)
{
@ -136,15 +141,23 @@ function shift_entry_create_controller_supporter($shift, $angeltype)
if ($request->has('user_id')) {
$signup_user = User($request->input('user_id'));
}
if (! UserAngelType_exists($signup_user, $angeltype)) {
if (!UserAngelType_exists($signup_user, $angeltype)) {
error(_('User is not in angeltype.'));
redirect(shift_link($shift));
}
$needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype);
$shift_entries = ShiftEntries_by_shift_and_angeltype($shift['SID'], $angeltype['id']);
$shift_signup_state = Shift_signup_allowed($signup_user, $shift, $angeltype, null, null, $needed_angeltype, $shift_entries);
if (! $shift_signup_state->isSignupAllowed()) {
$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.'));
}
@ -153,11 +166,11 @@ function shift_entry_create_controller_supporter($shift, $angeltype)
if ($request->has('submit')) {
ShiftEntry_create([
'SID' => $shift['SID'],
'TID' => $angeltype['id'],
'UID' => $signup_user['UID'],
'Comment' => '',
'freeloaded' => false,
'SID' => $shift['SID'],
'TID' => $angeltype['id'],
'UID' => $signup_user['UID'],
'Comment' => '',
'freeloaded' => false,
'freeload_comment' => ''
]);
@ -180,9 +193,11 @@ function shift_entry_create_controller_supporter($shift, $angeltype)
/**
* Generates an error message for the given shift signup state.
*
* @param ShiftSignupState $shift_signup_state
*/
function shift_entry_error_message(ShiftSignupState $shift_signup_state) {
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) {
@ -204,6 +219,7 @@ function shift_entry_error_message(ShiftSignupState $shift_signup_state) {
*
* @param array $shift
* @param array $angeltype
* @return array
*/
function shift_entry_create_controller_user($shift, $angeltype)
{
@ -213,8 +229,16 @@ function shift_entry_create_controller_user($shift, $angeltype)
$signup_user = $user;
$needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype);
$shift_entries = ShiftEntries_by_shift_and_angeltype($shift['SID'], $angeltype['id']);
$shift_signup_state = Shift_signup_allowed($signup_user, $shift, $angeltype, null, null, $needed_angeltype, $shift_entries);
if (! $shift_signup_state->isSignupAllowed()) {
$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));
}
@ -223,15 +247,15 @@ function shift_entry_create_controller_user($shift, $angeltype)
if ($request->has('submit')) {
$comment = strip_request_item_nl('comment');
ShiftEntry_create([
'SID' => $shift['SID'],
'TID' => $angeltype['id'],
'UID' => $signup_user['UID'],
'Comment' => $comment,
'freeloaded' => false,
'SID' => $shift['SID'],
'TID' => $angeltype['id'],
'UID' => $signup_user['UID'],
'Comment' => $comment,
'freeloaded' => false,
'freeload_comment' => ''
]);
if ($angeltype['restricted'] == false & & ! UserAngelType_exists($signup_user, $angeltype)) {
if ($angeltype['restricted'] == false & & !UserAngelType_exists($signup_user, $angeltype)) {
UserAngelType_create($signup_user, $angeltype);
}
@ -249,13 +273,16 @@ function shift_entry_create_controller_user($shift, $angeltype)
/**
* Link to create a shift entry.
*
* @param array $shift
* @param array $angeltype
* @param array $params
* @return string URL
*/
function shift_entry_create_link($shift, $angeltype, $params = [])
{
$params = array_merge([
'action' => 'create',
'shift_id' => $shift['SID'],
'action' => 'create',
'shift_id' => $shift['SID'],
'angeltype_id' => $angeltype['id']
], $params);
return page_link_to('shift_entries', $params);
@ -264,12 +291,14 @@ function shift_entry_create_link($shift, $angeltype, $params = [])
/**
* Link to create a shift entry as admin.
*
* @param array $shift
* @param array $params
* @return string URL
*/
function shift_entry_create_link_admin($shift, $params = [])
{
$params = array_merge([
'action' => 'create',
'action' => 'create',
'shift_id' => $shift['SID']
], $params);
return page_link_to('shift_entries', $params);
@ -277,12 +306,14 @@ function shift_entry_create_link_admin($shift, $params = [])
/**
* Load a shift entry from get parameter shift_entry_id.
*
* @return array
*/
function shift_entry_load()
{
$request = request();
if (! $request->has('shift_entry_id') || ! test_request_int('shift_entry_id')) {
if (!$request->has('shift_entry_id') || !test_request_int('shift_entry_id')) {
redirect(page_link_to('user_shifts'));
}
$shiftEntry = ShiftEntry($request->input('shift_entry_id'));
@ -296,6 +327,8 @@ function shift_entry_load()
/**
* Remove somebody from a shift.
*
* @return array
*/
function shift_entry_delete_controller()
{
@ -306,7 +339,7 @@ function shift_entry_delete_controller()
$shift = Shift($shiftEntry['SID']);
$angeltype = AngelType($shiftEntry['TID']);
$signout_user = User($shiftEntry['UID']);
if (! Shift_signout_allowed($shift, $angeltype, $signout_user)) {
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));
}
@ -334,13 +367,13 @@ function shift_entry_delete_controller()
* Link to delete a shift entry.
*
* @param array $shiftEntry
*
* @param array $params
* @return string URL
*/
function shift_entry_delete_link($shiftEntry, $params = [])
{
$params = array_merge([
'action' => 'delete',
'action' => 'delete',
'shift_entry_id' => $shiftEntry['id']
], $params);
return page_link_to('shift_entries', $params);