2016-10-03 17:41:14 +02:00
|
|
|
<?php
|
|
|
|
|
2017-01-21 13:58:53 +01:00
|
|
|
use Engelsystem\Database\DB;
|
|
|
|
|
2016-10-03 17:41:14 +02:00
|
|
|
/**
|
|
|
|
* Sign up for a shift.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return string
|
2016-10-03 17:41:14 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function shift_entry_add_controller()
|
|
|
|
{
|
|
|
|
global $privileges, $user;
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
2017-01-03 03:22:48 +01:00
|
|
|
$shift_id = 0;
|
2017-08-30 14:59:27 +02:00
|
|
|
if ($request->has('shift_id') && preg_match('/^\d+$/', $request->input('shift_id'))) {
|
2017-07-18 21:38:53 +02:00
|
|
|
$shift_id = $request->input('shift_id');
|
2017-01-02 03:57:23 +01:00
|
|
|
} else {
|
|
|
|
redirect(page_link_to('user_shifts'));
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
|
|
|
// Locations laden
|
2017-01-21 13:58:53 +01:00
|
|
|
$rooms = Rooms();
|
2017-01-02 03:57:23 +01:00
|
|
|
$room_array = [];
|
|
|
|
foreach ($rooms as $room) {
|
|
|
|
$room_array[$room['RID']] = $room['Name'];
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$shift = Shift($shift_id);
|
|
|
|
if ($shift == null) {
|
|
|
|
redirect(page_link_to('user_shifts'));
|
|
|
|
}
|
2017-08-29 16:21:25 +02:00
|
|
|
$shift['Name'] = $room_array[$shift['RID']];
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-09-16 17:14:45 +02:00
|
|
|
$type_id = null;
|
2017-08-30 14:59:27 +02:00
|
|
|
if ($request->has('type_id') && preg_match('/^\d+$/', $request->input('type_id'))) {
|
2017-07-18 21:38:53 +02:00
|
|
|
$type_id = $request->input('type_id');
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
if (in_array('user_shifts_admin', $privileges) || in_array('shiftentry_edit_angeltype_supporter', $privileges)) {
|
2017-09-16 17:14:45 +02:00
|
|
|
if($type_id == null) {
|
|
|
|
// If no angeltype id is given, then select first existing angeltype.
|
|
|
|
$needed_angeltypes = NeededAngelTypes_by_shift($shift_id);
|
|
|
|
if(count($needed_angeltypes) > 0) {
|
|
|
|
$type_id = $needed_angeltypes[0]['id'];
|
|
|
|
}
|
|
|
|
}
|
2017-01-02 03:57:23 +01:00
|
|
|
$type = AngelType($type_id);
|
|
|
|
} else {
|
2017-01-21 13:58:53 +01:00
|
|
|
// TODO: Move queries to model
|
2017-07-28 20:11:09 +02:00
|
|
|
$type = DB::selectOne('
|
2017-01-21 13:58:53 +01:00
|
|
|
SELECT *
|
|
|
|
FROM `UserAngelTypes`
|
2017-01-02 15:43:36 +01:00
|
|
|
JOIN `AngelTypes` ON (`UserAngelTypes`.`angeltype_id` = `AngelTypes`.`id`)
|
2017-01-21 13:58:53 +01:00
|
|
|
WHERE `AngelTypes`.`id` = ?
|
2017-01-02 15:43:36 +01:00
|
|
|
AND (
|
|
|
|
`AngelTypes`.`restricted` = 0
|
|
|
|
OR (
|
2017-01-21 13:58:53 +01:00
|
|
|
`UserAngelTypes`.`user_id` = ?
|
2017-01-02 15:43:36 +01:00
|
|
|
AND NOT `UserAngelTypes`.`confirm_user_id` IS NULL
|
|
|
|
)
|
|
|
|
)
|
2017-01-21 13:58:53 +01:00
|
|
|
', [$type_id, $user['UID']]);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-21 13:58:53 +01:00
|
|
|
if (empty($type)) {
|
2017-01-02 03:57:23 +01:00
|
|
|
redirect(page_link_to('user_shifts'));
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
|
|
|
if (
|
2017-07-18 21:38:53 +02:00
|
|
|
$request->has('user_id')
|
2017-08-30 14:59:27 +02:00
|
|
|
&& preg_match('/^\d+$/', $request->input('user_id'))
|
2017-01-02 15:43:36 +01:00
|
|
|
&& (
|
|
|
|
in_array('user_shifts_admin', $privileges)
|
|
|
|
|| in_array('shiftentry_edit_angeltype_supporter', $privileges)
|
|
|
|
)
|
|
|
|
) {
|
2017-07-18 21:38:53 +02:00
|
|
|
$user_id = $request->input('user_id');
|
2017-01-02 03:57:23 +01:00
|
|
|
} else {
|
|
|
|
$user_id = $user['UID'];
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $type);
|
|
|
|
$shift_entries = ShiftEntries_by_shift_and_angeltype($shift['SID'], $type['id']);
|
2017-01-02 15:43:36 +01:00
|
|
|
|
|
|
|
$shift_signup_allowed = Shift_signup_allowed(
|
|
|
|
User($user_id),
|
|
|
|
$shift,
|
|
|
|
$type,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
$needed_angeltype,
|
|
|
|
$shift_entries
|
|
|
|
);
|
|
|
|
if (!$shift_signup_allowed->isSignupAllowed()) {
|
2017-01-03 14:12:17 +01:00
|
|
|
error(_('You are not allowed to sign up for this shift. Maybe shift is full or already running.'));
|
2017-01-02 03:57:23 +01:00
|
|
|
redirect(shift_link($shift));
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-07-18 21:38:53 +02:00
|
|
|
if ($request->has('submit')) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$selected_type_id = $type_id;
|
2017-01-02 15:43:36 +01:00
|
|
|
if (in_array('user_shifts_admin', $privileges) || in_array('shiftentry_edit_angeltype_supporter',
|
|
|
|
$privileges)
|
|
|
|
) {
|
2017-01-21 13:58:53 +01:00
|
|
|
|
|
|
|
if (count(DB::select('SELECT `UID` FROM `User` WHERE `UID`=? LIMIT 1', [$user_id])) == 0) {
|
2017-01-02 03:57:23 +01:00
|
|
|
redirect(page_link_to('user_shifts'));
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
|
|
|
if (
|
2017-07-18 21:38:53 +02:00
|
|
|
$request->has('angeltype_id')
|
2017-01-02 15:43:36 +01:00
|
|
|
&& test_request_int('angeltype_id')
|
2017-01-21 13:58:53 +01:00
|
|
|
&& count(DB::select(
|
|
|
|
'SELECT `id` FROM `AngelTypes` WHERE `id`=? LIMIT 1',
|
2017-07-18 21:38:53 +02:00
|
|
|
[$request->input('angeltype_id')]
|
2017-01-21 13:58:53 +01:00
|
|
|
)) > 0
|
2017-01-02 15:43:36 +01:00
|
|
|
) {
|
2017-07-18 21:38:53 +02:00
|
|
|
$selected_type_id = $request->input('angeltype_id');
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-21 13:58:53 +01:00
|
|
|
if (count(DB::select(
|
|
|
|
'SELECT `id` FROM `ShiftEntry` WHERE `SID`= ? AND `UID` = ?',
|
|
|
|
[$shift['SID'], $user_id]))
|
|
|
|
) {
|
2017-12-19 19:17:50 +01:00
|
|
|
return error(_('This angel does already have an entry for this shift.'), true);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-21 13:58:53 +01:00
|
|
|
$freeloaded = isset($shift['freeloaded']) ? $shift['freeloaded'] : false;
|
|
|
|
$freeload_comment = isset($shift['freeload_comment']) ? $shift['freeload_comment'] : '';
|
2017-01-21 19:37:42 +01:00
|
|
|
if (in_array('user_shifts_admin', $privileges)) {
|
2017-07-18 21:38:53 +02:00
|
|
|
$freeloaded = $request->has('freeloaded');
|
2017-01-02 03:57:23 +01:00
|
|
|
$freeload_comment = strip_request_item_nl('freeload_comment');
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
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-01-02 15:43:36 +01:00
|
|
|
'SID' => $shift_id,
|
|
|
|
'TID' => $selected_type_id,
|
|
|
|
'UID' => $user_id,
|
|
|
|
'Comment' => $comment,
|
|
|
|
'freeloaded' => $freeloaded,
|
|
|
|
'freeload_comment' => $freeload_comment
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (
|
|
|
|
$type['restricted'] == 0
|
2017-01-21 13:58:53 +01:00
|
|
|
&& count(DB::select('
|
2017-01-21 19:37:42 +01:00
|
|
|
SELECT `UserAngelTypes`.`id` FROM `UserAngelTypes`
|
2017-01-02 15:43:36 +01:00
|
|
|
INNER JOIN `AngelTypes` ON `AngelTypes`.`id` = `UserAngelTypes`.`angeltype_id`
|
2017-01-21 13:58:53 +01:00
|
|
|
WHERE `angeltype_id` = ?
|
|
|
|
AND `user_id` = ?
|
|
|
|
', [$selected_type_id, $user_id])) == 0
|
2017-01-02 15:43:36 +01:00
|
|
|
) {
|
2017-01-21 13:58:53 +01:00
|
|
|
DB::insert(
|
|
|
|
'INSERT INTO `UserAngelTypes` (`user_id`, `angeltype_id`) VALUES (?, ?)',
|
|
|
|
[$user_id, $selected_type_id]
|
|
|
|
);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$user_source = User($user_id);
|
2017-01-21 13:58:53 +01:00
|
|
|
engelsystem_log(
|
|
|
|
'User ' . User_Nick_render($user_source)
|
|
|
|
. ' signed up for shift ' . $shift['name']
|
|
|
|
. ' from ' . date('Y-m-d H:i', $shift['start'])
|
|
|
|
. ' to ' . date('Y-m-d H:i', $shift['end'])
|
|
|
|
);
|
2017-01-03 14:12:17 +01:00
|
|
|
success(_('You are subscribed. Thank you!') . ' <a href="' . page_link_to('user_myshifts') . '">' . _('My shifts') . ' »</a>');
|
2017-01-02 03:57:23 +01:00
|
|
|
redirect(shift_link($shift));
|
2016-10-03 17:41:14 +02:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
$angeltype_select = '';
|
2017-01-02 03:57:23 +01:00
|
|
|
if (in_array('user_shifts_admin', $privileges)) {
|
2017-01-21 13:58:53 +01:00
|
|
|
$users = DB::select('
|
|
|
|
SELECT *,
|
|
|
|
(
|
|
|
|
SELECT count(*)
|
|
|
|
FROM `ShiftEntry`
|
|
|
|
WHERE `freeloaded`=1
|
|
|
|
AND `ShiftEntry`.`UID`=`User`.`UID`
|
|
|
|
) AS `freeloaded`
|
2017-01-02 15:43:36 +01:00
|
|
|
FROM `User`
|
|
|
|
ORDER BY `Nick`
|
2017-01-03 14:12:17 +01:00
|
|
|
');
|
2017-01-02 03:57:23 +01:00
|
|
|
$users_select = [];
|
|
|
|
foreach ($users as $usr) {
|
2017-01-03 14:12:17 +01:00
|
|
|
$users_select[$usr['UID']] = $usr['Nick'] . ($usr['freeloaded'] == 0 ? '' : ' (' . _('Freeloader') . ')');
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
$user_text = html_select_key('user_id', 'user_id', $users_select, $user['UID']);
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-21 13:58:53 +01:00
|
|
|
$angeltypes_source = DB::select('SELECT `id`, `name` FROM `AngelTypes` ORDER BY `name`');
|
2017-01-02 03:57:23 +01:00
|
|
|
$angeltypes = [];
|
|
|
|
foreach ($angeltypes_source as $angeltype) {
|
|
|
|
$angeltypes[$angeltype['id']] = $angeltype['name'];
|
|
|
|
}
|
|
|
|
$angeltype_select = html_select_key('angeltype_id', 'angeltype_id', $angeltypes, $type['id']);
|
2017-12-07 18:44:43 +01:00
|
|
|
} elseif (in_array('shiftentry_edit_angeltype_supporter', $privileges) && User_is_AngelType_supporter($user, $type)) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$users = Users_by_angeltype($type);
|
|
|
|
$users_select = [];
|
|
|
|
foreach ($users as $usr) {
|
2017-01-02 15:43:36 +01:00
|
|
|
if (!$type['restricted'] || $usr['confirm_user_id'] != null) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$users_select[$usr['UID']] = $usr['Nick'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$user_text = html_select_key('user_id', 'user_id', $users_select, $user['UID']);
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$angeltypes_source = User_angeltypes($user);
|
|
|
|
$angeltypes = [];
|
|
|
|
foreach ($angeltypes_source as $angeltype) {
|
|
|
|
if ($angeltype['supporter']) {
|
|
|
|
$angeltypes[$angeltype['id']] = $angeltype['name'];
|
|
|
|
}
|
|
|
|
$angeltype_select = html_select_key('angeltype_id', 'angeltype_id', $angeltypes, $type['id']);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$user_text = User_Nick_render($user);
|
|
|
|
$angeltype_select = $type['name'];
|
2016-12-24 17:00:03 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
return ShiftEntry_edit_view(
|
|
|
|
$user_text,
|
2017-01-03 14:12:17 +01:00
|
|
|
date('Y-m-d H:i', $shift['start'])
|
2017-01-03 03:22:48 +01:00
|
|
|
. ' – '
|
|
|
|
. date('Y-m-d H:i', $shift['end'])
|
|
|
|
. ' (' . shift_length($shift) . ')',
|
|
|
|
$shift['Name'],
|
|
|
|
$shift['name'],
|
2017-01-03 14:12:17 +01:00
|
|
|
$angeltype_select, '',
|
2017-01-03 03:22:48 +01:00
|
|
|
false,
|
|
|
|
null,
|
|
|
|
in_array('user_shifts_admin', $privileges)
|
|
|
|
);
|
2016-10-03 17:41:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-19 20:58:01 +01:00
|
|
|
* Load a shift entry from get parameter entry_id.
|
2016-10-03 17:41:14 +02:00
|
|
|
*/
|
2017-12-19 20:58:01 +01:00
|
|
|
function shift_entry_load() {
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-07-18 21:38:53 +02:00
|
|
|
if (!$request->has('entry_id') || !test_request_int('entry_id')) {
|
2017-01-02 03:57:23 +01:00
|
|
|
redirect(page_link_to('user_shifts'));
|
|
|
|
}
|
2017-12-19 20:58:01 +01:00
|
|
|
$shiftEntry = ShiftEntry($request->input('entry_id'));
|
|
|
|
if($shiftEntry == null) {
|
|
|
|
error(_('Shift entry not found.'));
|
|
|
|
redirect(page_link_to('user_shifts'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $shiftEntry;
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-12-19 20:58:01 +01:00
|
|
|
/**
|
|
|
|
* Remove somebody from a shift.
|
|
|
|
*/
|
|
|
|
function shift_entry_delete_controller()
|
|
|
|
{
|
|
|
|
global $privileges, $user;
|
|
|
|
$request = request();
|
|
|
|
$shiftEntry = shift_entry_load();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-12-19 20:58:01 +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 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));
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-03 03:22:48 +01:00
|
|
|
|
2017-12-19 20:58:01 +01:00
|
|
|
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);
|
2016-10-03 17:41:14 +02:00
|
|
|
}
|