You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
engelsystem/includes/controller/shift_entries_controller.php

292 lines
9.9 KiB
PHTML

<?php
use Engelsystem\Database\DB;
/**
* Sign up for a shift.
*
* @return string
*/
8 years ago
function shift_entry_add_controller()
{
global $privileges, $user;
8 years ago
$shift_id = 0;
if (isset($_REQUEST['shift_id']) && preg_match('/^[0-9]*$/', $_REQUEST['shift_id'])) {
8 years ago
$shift_id = $_REQUEST['shift_id'];
} else {
redirect(page_link_to('user_shifts'));
}
8 years ago
// Locations laden
$rooms = Rooms();
8 years ago
$room_array = [];
foreach ($rooms as $room) {
$room_array[$room['RID']] = $room['Name'];
}
8 years ago
8 years ago
$shift = Shift($shift_id);
$shift['Name'] = $room_array[$shift['RID']];
if ($shift == null) {
redirect(page_link_to('user_shifts'));
}
8 years ago
$type_id = 0;
if (isset($_REQUEST['type_id']) && preg_match('/^[0-9]*$/', $_REQUEST['type_id'])) {
8 years ago
$type_id = $_REQUEST['type_id'];
} else {
redirect(page_link_to('user_shifts'));
}
8 years ago
8 years ago
if (in_array('user_shifts_admin', $privileges) || in_array('shiftentry_edit_angeltype_supporter', $privileges)) {
$type = AngelType($type_id);
} else {
// TODO: Move queries to model
$type = DB::select('
SELECT *
FROM `UserAngelTypes`
8 years ago
JOIN `AngelTypes` ON (`UserAngelTypes`.`angeltype_id` = `AngelTypes`.`id`)
WHERE `AngelTypes`.`id` = ?
8 years ago
AND (
`AngelTypes`.`restricted` = 0
OR (
`UserAngelTypes`.`user_id` = ?
8 years ago
AND NOT `UserAngelTypes`.`confirm_user_id` IS NULL
)
)
', [$type_id, $user['UID']]);
$type = array_shift($type);
8 years ago
}
8 years ago
if (empty($type)) {
8 years ago
redirect(page_link_to('user_shifts'));
}
8 years ago
if (
isset($_REQUEST['user_id'])
&& preg_match('/^[0-9]*$/', $_REQUEST['user_id'])
8 years ago
&& (
in_array('user_shifts_admin', $privileges)
|| in_array('shiftentry_edit_angeltype_supporter', $privileges)
)
) {
8 years ago
$user_id = $_REQUEST['user_id'];
} else {
$user_id = $user['UID'];
}
8 years ago
8 years ago
$needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $type);
$shift_entries = ShiftEntries_by_shift_and_angeltype($shift['SID'], $type['id']);
8 years ago
$shift_signup_allowed = Shift_signup_allowed(
User($user_id),
$shift,
$type,
null,
null,
$needed_angeltype,
$shift_entries
);
if (!$shift_signup_allowed->isSignupAllowed()) {
error(_('You are not allowed to sign up for this shift. Maybe shift is full or already running.'));
8 years ago
redirect(shift_link($shift));
}
8 years ago
8 years ago
if (isset($_REQUEST['submit'])) {
$selected_type_id = $type_id;
8 years ago
if (in_array('user_shifts_admin', $privileges) || in_array('shiftentry_edit_angeltype_supporter',
$privileges)
) {
if (count(DB::select('SELECT `UID` FROM `User` WHERE `UID`=? LIMIT 1', [$user_id])) == 0) {
8 years ago
redirect(page_link_to('user_shifts'));
}
8 years ago
if (
isset($_REQUEST['angeltype_id'])
&& test_request_int('angeltype_id')
&& count(DB::select(
'SELECT `id` FROM `AngelTypes` WHERE `id`=? LIMIT 1',
[$_REQUEST['angeltype_id']]
)) > 0
8 years ago
) {
8 years ago
$selected_type_id = $_REQUEST['angeltype_id'];
}
}
8 years ago
if (count(DB::select(
'SELECT `id` FROM `ShiftEntry` WHERE `SID`= ? AND `UID` = ?',
[$shift['SID'], $user_id]))
) {
return error('This angel does already have an entry for this shift.', true);
8 years ago
}
8 years ago
$freeloaded = isset($shift['freeloaded']) ? $shift['freeloaded'] : false;
$freeload_comment = isset($shift['freeload_comment']) ? $shift['freeload_comment'] : '';
if (in_array('user_shifts_admin', $privileges)) {
8 years ago
$freeloaded = isset($_REQUEST['freeloaded']);
$freeload_comment = strip_request_item_nl('freeload_comment');
}
8 years ago
8 years ago
$comment = strip_request_item_nl('comment');
$result = ShiftEntry_create([
8 years ago
'SID' => $shift_id,
'TID' => $selected_type_id,
'UID' => $user_id,
'Comment' => $comment,
'freeloaded' => $freeloaded,
'freeload_comment' => $freeload_comment
]);
8 years ago
if ($result === false) {
engelsystem_error('Unable to create shift entry.');
}
8 years ago
if (
$type['restricted'] == 0
&& count(DB::select('
SELECT `UserAngelTypes`.`id` FROM `UserAngelTypes`
8 years ago
INNER JOIN `AngelTypes` ON `AngelTypes`.`id` = `UserAngelTypes`.`angeltype_id`
WHERE `angeltype_id` = ?
AND `user_id` = ?
', [$selected_type_id, $user_id])) == 0
8 years ago
) {
DB::insert(
'INSERT INTO `UserAngelTypes` (`user_id`, `angeltype_id`) VALUES (?, ?)',
[$user_id, $selected_type_id]
);
8 years ago
}
8 years ago
8 years ago
$user_source = User($user_id);
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'])
);
success(_('You are subscribed. Thank you!') . ' <a href="' . page_link_to('user_myshifts') . '">' . _('My shifts') . ' &raquo;</a>');
8 years ago
redirect(shift_link($shift));
}
8 years ago
$angeltype_select = '';
8 years ago
if (in_array('user_shifts_admin', $privileges)) {
$users = DB::select('
SELECT *,
(
SELECT count(*)
FROM `ShiftEntry`
WHERE `freeloaded`=1
AND `ShiftEntry`.`UID`=`User`.`UID`
) AS `freeloaded`
8 years ago
FROM `User`
ORDER BY `Nick`
');
8 years ago
$users_select = [];
foreach ($users as $usr) {
$users_select[$usr['UID']] = $usr['Nick'] . ($usr['freeloaded'] == 0 ? '' : ' (' . _('Freeloader') . ')');
8 years ago
}
$user_text = html_select_key('user_id', 'user_id', $users_select, $user['UID']);
8 years ago
$angeltypes_source = DB::select('SELECT `id`, `name` FROM `AngelTypes` ORDER BY `name`');
8 years ago
$angeltypes = [];
foreach ($angeltypes_source as $angeltype) {
$angeltypes[$angeltype['id']] = $angeltype['name'];
}
$angeltype_select = html_select_key('angeltype_id', 'angeltype_id', $angeltypes, $type['id']);
} elseif (in_array('shiftentry_edit_angeltype_supporter', $privileges)) {
$users = Users_by_angeltype($type);
$users_select = [];
foreach ($users as $usr) {
8 years ago
if (!$type['restricted'] || $usr['confirm_user_id'] != null) {
8 years ago
$users_select[$usr['UID']] = $usr['Nick'];
}
}
$user_text = html_select_key('user_id', 'user_id', $users_select, $user['UID']);
8 years ago
8 years ago
$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'];
}
8 years ago
return ShiftEntry_edit_view(
$user_text,
date('Y-m-d H:i', $shift['start'])
. ' &ndash; '
. date('Y-m-d H:i', $shift['end'])
. ' (' . shift_length($shift) . ')',
$shift['Name'],
$shift['name'],
$angeltype_select, '',
false,
null,
in_array('user_shifts_admin', $privileges)
);
}
/**
* Remove somebody from a shift.
*/
8 years ago
function shift_entry_delete_controller()
{
global $privileges, $user;
8 years ago
if (!isset($_REQUEST['entry_id']) || !test_request_int('entry_id')) {
8 years ago
redirect(page_link_to('user_shifts'));
}
$entry_id = $_REQUEST['entry_id'];
8 years ago
$shift_entry_source = DB::select('
8 years ago
SELECT
`User`.`Nick`,
`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]
);
8 years ago
if (count($shift_entry_source) > 0) {
$shift_entry_source = array_shift($shift_entry_source);
8 years ago
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'])))
) {
8 years ago
redirect(page_link_to('user_shifts'));
}
8 years ago
8 years ago
$result = ShiftEntry_delete($entry_id);
if ($result === false) {
engelsystem_error('Unable to delete shift entry.');
}
8 years ago
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']
8 years ago
);
success(_('Shift entry deleted.'));
8 years ago
} else {
error(_('Entry not found.'));
8 years ago
}
8 years ago
redirect(shift_link($shift_entry_source));
}