engelsystem/includes/controller/shift_entries_controller.php

266 lines
9.6 KiB
PHP
Raw Normal View History

2016-10-03 17:41:14 +02:00
<?php
/**
* 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-01-03 03:22:48 +01:00
$shift_id = 0;
2017-01-03 14:12:17 +01:00
if (isset($_REQUEST['shift_id']) && preg_match('/^[0-9]*$/', $_REQUEST['shift_id'])) {
2017-01-02 03:57:23 +01:00
$shift_id = $_REQUEST['shift_id'];
} else {
redirect(page_link_to('user_shifts'));
}
2017-01-02 15:43:36 +01:00
// Locations laden
2017-01-03 14:12:17 +01:00
$rooms = sql_select('SELECT * FROM `Room` WHERE `show`=\'Y\' ORDER BY `Name`');
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);
$shift['Name'] = $room_array[$shift['RID']];
if ($shift == null) {
redirect(page_link_to('user_shifts'));
}
2017-01-02 15:43:36 +01:00
2017-01-03 03:22:48 +01:00
$type_id = 0;
2017-01-03 14:12:17 +01:00
if (isset($_REQUEST['type_id']) && preg_match('/^[0-9]*$/', $_REQUEST['type_id'])) {
2017-01-02 03:57:23 +01:00
$type_id = $_REQUEST['type_id'];
} else {
redirect(page_link_to('user_shifts'));
}
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)) {
$type = AngelType($type_id);
} else {
2017-01-02 15:43:36 +01:00
$type = sql_select("
SELECT * FROM `UserAngelTypes`
JOIN `AngelTypes` ON (`UserAngelTypes`.`angeltype_id` = `AngelTypes`.`id`)
WHERE `AngelTypes`.`id` = '" . sql_escape($type_id) . "'
AND (
`AngelTypes`.`restricted` = 0
OR (
`UserAngelTypes`.`user_id` = '" . sql_escape($user['UID']) . "'
AND NOT `UserAngelTypes`.`confirm_user_id` IS NULL
)
)
");
2017-01-02 03:57:23 +01:00
$type = $type[0];
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
if ($type == null) {
redirect(page_link_to('user_shifts'));
}
2017-01-02 15:43:36 +01:00
if (
isset($_REQUEST['user_id'])
2017-01-03 14:12:17 +01:00
&& preg_match('/^[0-9]*$/', $_REQUEST['user_id'])
2017-01-02 15:43:36 +01:00
&& (
in_array('user_shifts_admin', $privileges)
|| in_array('shiftentry_edit_angeltype_supporter', $privileges)
)
) {
2017-01-02 03:57:23 +01:00
$user_id = $_REQUEST['user_id'];
} 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-01-02 03:57:23 +01:00
if (isset($_REQUEST['submit'])) {
$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-02 03:57:23 +01:00
if (sql_num_query("SELECT * FROM `User` WHERE `UID`='" . sql_escape($user_id) . "' LIMIT 1") == 0) {
redirect(page_link_to('user_shifts'));
}
2017-01-02 15:43:36 +01:00
if (
isset($_REQUEST['angeltype_id'])
&& test_request_int('angeltype_id')
&& sql_num_query("SELECT * FROM `AngelTypes` WHERE `id`='" . sql_escape($_REQUEST['angeltype_id']) . "' LIMIT 1") > 0
) {
2017-01-02 03:57:23 +01:00
$selected_type_id = $_REQUEST['angeltype_id'];
}
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
if (sql_num_query("SELECT * FROM `ShiftEntry` WHERE `SID`='" . sql_escape($shift['SID']) . "' AND `UID` = '" . sql_escape($user_id) . "'")) {
return error("This angel does already have an entry for this shift.", true);
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$freeloaded = $shift['freeloaded'];
$freeload_comment = $shift['freeload_comment'];
if (in_array("user_shifts_admin", $privileges)) {
$freeloaded = isset($_REQUEST['freeloaded']);
$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');
$result = 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
]);
2017-01-02 03:57:23 +01:00
if ($result === false) {
engelsystem_error('Unable to create shift entry.');
}
2017-01-02 15:43:36 +01:00
if (
$type['restricted'] == 0
&& sql_num_query("
SELECT * FROM `UserAngelTypes`
INNER JOIN `AngelTypes` ON `AngelTypes`.`id` = `UserAngelTypes`.`angeltype_id`
WHERE `angeltype_id` = '" . sql_escape($selected_type_id) . "'
AND `user_id` = '" . sql_escape($user_id) . "'
") == 0
) {
2017-01-02 03:57:23 +01:00
sql_query("INSERT INTO `UserAngelTypes` (`user_id`, `angeltype_id`) VALUES ('" . sql_escape($user_id) . "', '" . sql_escape($selected_type_id) . "')");
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$user_source = User($user_id);
2017-01-03 14:12:17 +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']));
success(_('You are subscribed. Thank you!') . ' <a href="' . page_link_to('user_myshifts') . '">' . _('My shifts') . ' &raquo;</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-03 14:12:17 +01:00
$users = sql_select('
2017-01-02 15:43:36 +01:00
SELECT *, (SELECT count(*) FROM `ShiftEntry` WHERE `freeloaded`=1 AND `ShiftEntry`.`UID`=`User`.`UID`) AS `freeloaded`
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-03 14:12:17 +01:00
$angeltypes_source = sql_select('SELECT * 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']);
} elseif (in_array('shiftentry_edit_angeltype_supporter', $privileges)) {
$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'];
}
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
. ' &ndash; '
. 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
}
/**
* Remove somebody from a shift.
*/
2017-01-02 03:57:23 +01:00
function shift_entry_delete_controller()
{
global $privileges, $user;
2017-01-02 15:43:36 +01:00
if (!isset($_REQUEST['entry_id']) || !test_request_int('entry_id')) {
2017-01-02 03:57:23 +01:00
redirect(page_link_to('user_shifts'));
}
$entry_id = $_REQUEST['entry_id'];
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$shift_entry_source = sql_select("
2017-01-02 15:43:36 +01:00
SELECT
`User`.`Nick`,
`ShiftEntry`.`Comment`,
`ShiftEntry`.`UID`,
`ShiftTypes`.`name`,
`Shifts`.*,
`Room`.`Name`,
`AngelTypes`.`name` AS `angel_type`,
`AngelTypes`.`id` AS `angeltype_id`
2016-10-03 17:41:14 +02:00
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`='" . sql_escape($entry_id) . "'");
2017-01-02 03:57:23 +01:00
if (count($shift_entry_source) > 0) {
$shift_entry_source = $shift_entry_source[0];
2017-01-02 15:43:36 +01:00
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'])))
) {
2017-01-02 03:57:23 +01:00
redirect(page_link_to('user_shifts'));
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$result = ShiftEntry_delete($entry_id);
if ($result === false) {
engelsystem_error('Unable to delete shift entry.');
}
2017-01-02 15:43:36 +01:00
engelsystem_log(
2017-01-03 14:12:17 +01:00
'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']
2017-01-02 15:43:36 +01:00
);
2017-01-03 14:12:17 +01:00
success(_('Shift entry deleted.'));
2017-01-02 03:57:23 +01:00
} else {
2017-01-03 14:12:17 +01:00
error(_('Entry not found.'));
2017-01-02 03:57:23 +01:00
}
2017-01-03 03:22:48 +01:00
2017-01-02 03:57:23 +01:00
redirect(shift_link($shift_entry_source));
2016-10-03 17:41:14 +02:00
}