/**
 * Job description for all angeltypes (public to everyone)
 */
function angeltypes_about_controller()
{
    global $privileges, $user;
    if (isset($user)) {
        $angeltypes = AngelTypes_with_user($user);
    } else {
        $angeltypes = AngelTypes();
    }
    if ($angeltypes === false) {
        engelsystem_error("Unable to load angeltypes.");
    }
    return array(_("Teams/Job description"), AngelTypes_about_view($angeltypes, isset($user)));
}
function shift_controller()
{
    global $user, $privileges;
    if (!in_array('user_shifts', $privileges)) {
        redirect(page_link_to('?'));
    }
    if (!isset($_REQUEST['shift_id'])) {
        redirect(page_link_to('user_shifts'));
    }
    $shift = Shift($_REQUEST['shift_id']);
    if ($shift === false) {
        engelsystem_error('Unable to load shift.');
    }
    if ($shift == null) {
        error(_('Shift could not be found.'));
        redirect(page_link_to('user_shifts'));
    }
    $shifttype = ShiftType($shift['shifttype_id']);
    if ($shifttype === false || $shifttype == null) {
        engelsystem_error('Unable to load shift type.');
    }
    $room = Room($shift['RID']);
    if ($room === false || $room == null) {
        engelsystem_error('Unable to load room.');
    }
    $angeltypes = AngelTypes();
    if ($angeltypes === false) {
        engelsystem_error('Unable to load angeltypes.');
    }
    $user_shifts = Shifts_by_user($user);
    if ($user_shifts === false) {
        engelsystem_error('Unable to load users shifts.');
    }
    $signed_up = false;
    foreach ($user_shifts as $user_shift) {
        if ($user_shift['SID'] == $shift['SID']) {
            $signed_up = true;
            break;
        }
    }
    return [$shift['name'], Shift_view($shift, $shifttype, $room, in_array('admin_shifts', $privileges), $angeltypes, in_array('user_shifts_admin', $privileges), in_array('admin_rooms', $privileges), in_array('shifttypes', $privileges), $user_shifts, $signed_up)];
}
/**
 * Edit or create shift type.
 */
function shifttype_edit_controller()
{
    $shifttype_id = null;
    $name = "";
    $angeltype_id = null;
    $description = "";
    $angeltypes = AngelTypes();
    if ($angeltypes === false) {
        engelsystem_error("Unable to load angel types.");
    }
    if (isset($_REQUEST['shifttype_id'])) {
        $shifttype = ShiftType($_REQUEST['shifttype_id']);
        if ($shifttype === false) {
            engelsystem_error('Unable to load shifttype.');
        }
        if ($shifttype == null) {
            error(_('Shifttype not found.'));
            redirect(page_link_to('shifttypes'));
        }
        $shifttype_id = $shifttype['id'];
        $name = $shifttype['name'];
        $angeltype_id = $shifttype['angeltype_id'];
        $description = $shifttype['description'];
    }
    if (isset($_REQUEST['submit'])) {
        $ok = true;
        if (isset($_REQUEST['name']) && $_REQUEST['name'] != '') {
            $name = strip_request_item('name');
        } else {
            $ok = false;
            error(_('Please enter a name.'));
        }
        if (isset($_REQUEST['angeltype_id']) && preg_match("/^[0-9]+\$/", $_REQUEST['angeltype_id'])) {
            $angeltype_id = $_REQUEST['angeltype_id'];
        } else {
            $angeltype_id = null;
        }
        if (isset($_REQUEST['description'])) {
            $description = strip_request_item_nl('description');
        }
        if ($ok) {
            if ($shifttype_id) {
                $result = ShiftType_update($shifttype_id, $name, $angeltype_id, $description);
                if ($result === false) {
                    engelsystem_error('Unable to update shifttype.');
                }
                engelsystem_log('Updated shifttype ' . $name);
                success(_('Updated shifttype.'));
            } else {
                $shifttype_id = ShiftType_create($name, $angeltype_id, $description);
                if ($shifttype_id === false) {
                    engelsystem_error('Unable to create shifttype.');
                }
                engelsystem_log('Created shifttype ' . $name);
                success(_('Created shifttype.'));
            }
            redirect(page_link_to('shifttypes') . '&action=view&shifttype_id=' . $shifttype_id);
        }
    }
    return [shifttypes_title(), ShiftType_edit_view($name, $angeltype_id, $angeltypes, $description, $shifttype_id)];
}