@ -1,23 +1,24 @@
<?php
<?php
use Engelsystem\ShiftSignupState;
use Engelsystem\ShiftSignupState;
/**
/**
* Route shift entry actions.
* Route shift entry actions.
*
* @return array
*/
*/
function shift_entries_controller()
function shift_entries_controller()
{
{
global $user;
global $user;
$action = strip_request_item('action');
$action = strip_request_item('action');
if ($action == null) {
if ($action == null) {
redirect(user_link($user));
redirect(user_link($user));
}
}
switch ($action) {
switch ($action) {
case 'create':
case 'create':
return shift_entry_create_controller();
return shift_entry_create_controller();
case 'edit':
return shift_entry_edit_controller();
case 'delete':
case 'delete':
return shift_entry_delete_controller();
return shift_entry_delete_controller();
}
}
@ -25,35 +26,37 @@ function shift_entries_controller()
/**
/**
* Sign up for a shift.
* Sign up for a shift.
*
* @return array
*/
*/
function shift_entry_create_controller()
function shift_entry_create_controller()
{
{
global $privileges, $user;
global $privileges, $user;
$request = request();
$request = request();
if (User_is_freeloader($user)) {
if (User_is_freeloader($user)) {
redirect(page_link_to('user_myshifts'));
redirect(page_link_to('user_myshifts'));
}
}
$shift = Shift($request->input('shift_id'));
$shift = Shift($request->input('shift_id'));
if ($shift == null) {
if ($shift == null) {
redirect(user_link($user));
redirect(user_link($user));
}
}
$angeltype = AngelType($request->input('angeltype_id'));
$angeltype = AngelType($request->input('angeltype_id'));
if (in_array('user_shifts_admin', $privileges)) {
if (in_array('user_shifts_admin', $privileges)) {
return shift_entry_create_controller_admin($shift, $angeltype);
return shift_entry_create_controller_admin($shift, $angeltype);
}
}
if ($angeltype == null) {
if ($angeltype == null) {
redirect(user_link($user));
redirect(user_link($user));
}
}
if (User_is_AngelType_supporter($user, $angeltype)) {
if (User_is_AngelType_supporter($user, $angeltype)) {
return shift_entry_create_controller_supporter($shift, $angeltype);
return shift_entry_create_controller_supporter($shift, $angeltype);
}
}
return shift_entry_create_controller_user($shift, $angeltype);
return shift_entry_create_controller_user($shift, $angeltype);
}
}
@ -61,58 +64,59 @@ function shift_entry_create_controller()
* Sign up for a shift.
* Sign up for a shift.
* Case: Admin
* Case: Admin
*
*
* @param array $shift
* @param array $shift
* @param array $angeltype
* @param array $angeltype
* @return array
*/
*/
function shift_entry_create_controller_admin($shift, $angeltype)
function shift_entry_create_controller_admin($shift, $angeltype)
{
{
global $user;
global $user;
$request = request();
$request = request();
$signup_user = $user;
$signup_user = $user;
if ($request->has('user_id')) {
if ($request->has('user_id')) {
$signup_user = User($request->input('user_id'));
$signup_user = User($request->input('user_id'));
}
}
if($signup_user == null) {
if ($signup_user == null) {
redirect(shift_link($shift));
redirect(shift_link($shift));
}
}
$angeltypes = AngelTypes();
$angeltypes = AngelTypes();
if($request->has('angeltype_id')) {
if ($request->has('angeltype_id')) {
$angeltype = AngelType($request->input('angeltype_id'));
$angeltype = AngelType($request->input('angeltype_id'));
}
}
if($angeltype == null) {
if ($angeltype == null) {
if(count($angeltypes) == 0) {
if (count($angeltypes) == 0) {
redirect(shift_link($shift));
redirect(shift_link($shift));
}
}
$angeltype = $angeltypes[0];
$angeltype = $angeltypes[0];
}
}
if ($request->has('submit')) {
if ($request->has('submit')) {
ShiftEntry_create([
ShiftEntry_create([
'SID' => $shift['SID'],
'SID' => $shift['SID'],
'TID' => $angeltype['id'],
'TID' => $angeltype['id'],
'UID' => $signup_user['UID'],
'UID' => $signup_user['UID'],
'Comment' => '',
'Comment' => '',
'freeloaded' => false,
'freeloaded' => false,
'freeload_comment' => ''
'freeload_comment' => ''
]);
]);
success(sprintf(_('%s has been subscribed to the shift.'), User_Nick_render($signup_user)));
success(sprintf(_('%s has been subscribed to the shift.'), User_Nick_render($signup_user)));
redirect(shift_link($shift));
redirect(shift_link($shift));
}
}
$users = Users();
$users = Users();
$users_select = [];
$users_select = [];
foreach ($users as $u) {
foreach ($users as $u) {
$users_select[$u['UID']] = $u['Nick'];
$users_select[$u['UID']] = $u['Nick'];
}
}
$angeltypes_select = [];
$angeltypes_select = [];
foreach($angeltypes as $a) {
foreach ($angeltypes as $a) {
$angeltypes_select[$a['id']] = $a['name'];
$angeltypes_select[$a['id']] = $a['name'];
}
}
$room = Room($shift['RID']);
$room = Room($shift['RID']);
return [
return [
ShiftEntry_create_title(),
ShiftEntry_create_title(),
@ -124,53 +128,62 @@ function shift_entry_create_controller_admin($shift, $angeltype)
* Sign up for a shift.
* Sign up for a shift.
* Case: Supporter
* Case: Supporter
*
*
* @param array $shift
* @param array $shift
* @param array $angeltype
* @param array $angeltype
* @return array
*/
*/
function shift_entry_create_controller_supporter($shift, $angeltype)
function shift_entry_create_controller_supporter($shift, $angeltype)
{
{
global $user;
global $user;
$request = request();
$request = request();
$signup_user = $user;
$signup_user = $user;
if ($request->has('user_id')) {
if ($request->has('user_id')) {
$signup_user = User($request->input('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.'));
error(_('User is not in angeltype.'));
redirect(shift_link($shift));
redirect(shift_link($shift));
}
}
$needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype);
$needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype);
$shift_entries = ShiftEntries_by_shift_and_angeltype($shift['SID'], $angeltype['id']);
$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);
$shift_signup_state = Shift_signup_allowed(
if (! $shift_signup_state->isSignupAllowed()) {
$signup_user,
$shift,
$angeltype,
null,
null,
$needed_angeltype,
$shift_entries
);
if (!$shift_signup_state->isSignupAllowed()) {
if ($shift_signup_state->getState() == ShiftSignupState::OCCUPIED) {
if ($shift_signup_state->getState() == ShiftSignupState::OCCUPIED) {
error(_('This shift is already occupied.'));
error(_('This shift is already occupied.'));
}
}
redirect(shift_link($shift));
redirect(shift_link($shift));
}
}
if ($request->has('submit')) {
if ($request->has('submit')) {
ShiftEntry_create([
ShiftEntry_create([
'SID' => $shift['SID'],
'SID' => $shift['SID'],
'TID' => $angeltype['id'],
'TID' => $angeltype['id'],
'UID' => $signup_user['UID'],
'UID' => $signup_user['UID'],
'Comment' => '',
'Comment' => '',
'freeloaded' => false,
'freeloaded' => false,
'freeload_comment' => ''
'freeload_comment' => ''
]);
]);
success(sprintf(_('%s has been subscribed to the shift.'), User_Nick_render($signup_user)));
success(sprintf(_('%s has been subscribed to the shift.'), User_Nick_render($signup_user)));
redirect(shift_link($shift));
redirect(shift_link($shift));
}
}
$users = Users_by_angeltype($angeltype);
$users = Users_by_angeltype($angeltype);
$users_select = [];
$users_select = [];
foreach ($users as $u) {
foreach ($users as $u) {
$users_select[$u['UID']] = $u['Nick'];
$users_select[$u['UID']] = $u['Nick'];
}
}
$room = Room($shift['RID']);
$room = Room($shift['RID']);
return [
return [
ShiftEntry_create_title(),
ShiftEntry_create_title(),
@ -180,9 +193,11 @@ function shift_entry_create_controller_supporter($shift, $angeltype)
/**
/**
* Generates an error message for the given shift signup state.
* Generates an error message for the given shift signup state.
*
* @param ShiftSignupState $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) {
if ($shift_signup_state->getState() == ShiftSignupState::ANGELTYPE) {
error(_('You need be accepted member of the angeltype.'));
error(_('You need be accepted member of the angeltype.'));
} elseif ($shift_signup_state->getState() == ShiftSignupState::COLLIDES) {
} elseif ($shift_signup_state->getState() == ShiftSignupState::COLLIDES) {
@ -202,43 +217,52 @@ function shift_entry_error_message(ShiftSignupState $shift_signup_state) {
* Sign up for a shift.
* Sign up for a shift.
* Case: User
* Case: User
*
*
* @param array $shift
* @param array $shift
* @param array $angeltype
* @param array $angeltype
* @return array
*/
*/
function shift_entry_create_controller_user($shift, $angeltype)
function shift_entry_create_controller_user($shift, $angeltype)
{
{
global $user;
global $user;
$request = request();
$request = request();
$signup_user = $user;
$signup_user = $user;
$needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype);
$needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype);
$shift_entries = ShiftEntries_by_shift_and_angeltype($shift['SID'], $angeltype['id']);
$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);
$shift_signup_state = Shift_signup_allowed(
if (! $shift_signup_state->isSignupAllowed()) {
$signup_user,
$shift,
$angeltype,
null,
null,
$needed_angeltype,
$shift_entries
);
if (!$shift_signup_state->isSignupAllowed()) {
shift_entry_error_message($shift_signup_state);
shift_entry_error_message($shift_signup_state);
redirect(shift_link($shift));
redirect(shift_link($shift));
}
}
$comment = '';
$comment = '';
if ($request->has('submit')) {
if ($request->has('submit')) {
$comment = strip_request_item_nl('comment');
$comment = strip_request_item_nl('comment');
ShiftEntry_create([
ShiftEntry_create([
'SID' => $shift['SID'],
'SID' => $shift['SID'],
'TID' => $angeltype['id'],
'TID' => $angeltype['id'],
'UID' => $signup_user['UID'],
'UID' => $signup_user['UID'],
'Comment' => $comment,
'Comment' => $comment,
'freeloaded' => false,
'freeloaded' => false,
'freeload_comment' => ''
'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);
UserAngelType_create($signup_user, $angeltype);
}
}
success(_('You are subscribed. Thank you!'));
success(_('You are subscribed. Thank you!'));
redirect(shift_link($shift));
redirect(shift_link($shift));
}
}
$room = Room($shift['RID']);
$room = Room($shift['RID']);
return [
return [
ShiftEntry_create_title(),
ShiftEntry_create_title(),
@ -249,13 +273,16 @@ function shift_entry_create_controller_user($shift, $angeltype)
/**
/**
* Link to create a shift entry.
* Link to create a shift entry.
*
*
* @param array $shift
* @param array $angeltype
* @param array $params
* @return string URL
* @return string URL
*/
*/
function shift_entry_create_link($shift, $angeltype, $params = [])
function shift_entry_create_link($shift, $angeltype, $params = [])
{
{
$params = array_merge([
$params = array_merge([
'action' => 'create',
'action' => 'create',
'shift_id' => $shift['SID'],
'shift_id' => $shift['SID'],
'angeltype_id' => $angeltype['id']
'angeltype_id' => $angeltype['id']
], $params);
], $params);
return page_link_to('shift_entries', $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.
* Link to create a shift entry as admin.
*
*
* @param array $shift
* @param array $params
* @return string URL
* @return string URL
*/
*/
function shift_entry_create_link_admin($shift, $params = [])
function shift_entry_create_link_admin($shift, $params = [])
{
{
$params = array_merge([
$params = array_merge([
'action' => 'create',
'action' => 'create',
'shift_id' => $shift['SID']
'shift_id' => $shift['SID']
], $params);
], $params);
return page_link_to('shift_entries', $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.
* Load a shift entry from get parameter shift_entry_id.
*
* @return array
*/
*/
function shift_entry_load()
function shift_entry_load()
{
{
$request = request();
$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'));
redirect(page_link_to('user_shifts'));
}
}
$shiftEntry = ShiftEntry($request->input('shift_entry_id'));
$shiftEntry = ShiftEntry($request->input('shift_entry_id'));
@ -290,40 +321,42 @@ function shift_entry_load()
error(_('Shift entry not found.'));
error(_('Shift entry not found.'));
redirect(page_link_to('user_shifts'));
redirect(page_link_to('user_shifts'));
}
}
return $shiftEntry;
return $shiftEntry;
}
}
/**
/**
* Remove somebody from a shift.
* Remove somebody from a shift.
*
* @return array
*/
*/
function shift_entry_delete_controller()
function shift_entry_delete_controller()
{
{
global $user;
global $user;
$request = request();
$request = request();
$shiftEntry = shift_entry_load();
$shiftEntry = shift_entry_load();
$shift = Shift($shiftEntry['SID']);
$shift = Shift($shiftEntry['SID']);
$angeltype = AngelType($shiftEntry['TID']);
$angeltype = AngelType($shiftEntry['TID']);
$signout_user = User($shiftEntry['UID']);
$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.'));
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));
redirect(user_link($signout_user));
}
}
if ($request->has('continue')) {
if ($request->has('continue')) {
ShiftEntry_delete($shiftEntry);
ShiftEntry_delete($shiftEntry);
success(_('Shift entry removed.'));
success(_('Shift entry removed.'));
redirect(shift_link($shift));
redirect(shift_link($shift));
}
}
if ($user['UID'] == $signout_user['UID']) {
if ($user['UID'] == $signout_user['UID']) {
return [
return [
ShiftEntry_delete_title(),
ShiftEntry_delete_title(),
ShiftEntry_delete_view($shiftEntry, $shift, $angeltype, $signout_user)
ShiftEntry_delete_view($shiftEntry, $shift, $angeltype, $signout_user)
];
];
}
}
return [
return [
ShiftEntry_delete_title(),
ShiftEntry_delete_title(),
ShiftEntry_delete_view_admin($shiftEntry, $shift, $angeltype, $signout_user)
ShiftEntry_delete_view_admin($shiftEntry, $shift, $angeltype, $signout_user)
@ -333,14 +366,14 @@ function shift_entry_delete_controller()
/**
/**
* Link to delete a shift entry.
* Link to delete a shift entry.
*
*
* @param array $shiftEntry
* @param array $shiftEntry
*
* @param array $params
* @return string URL
* @return string URL
*/
*/
function shift_entry_delete_link($shiftEntry, $params = [])
function shift_entry_delete_link($shiftEntry, $params = [])
{
{
$params = array_merge([
$params = array_merge([
'action' => 'delete',
'action' => 'delete',
'shift_entry_id' => $shiftEntry['id']
'shift_entry_id' => $shiftEntry['id']
], $params);
], $params);
return page_link_to('shift_entries', $params);
return page_link_to('shift_entries', $params);