Files
itflow/api/v1/events/event_model.php
johnnyq ecbb974fff API stage 3, calendar events, trips, accounts and money, contracts, and files and folders: calendars can be read, and calendar events read with their attendees, created once or repeating for a client or nobody, changed, sent as invitations, skipped for a day of a series and deleted, following calendarEventCan(); trips can be read, logged, changed and deleted through tripSave() and tripDelete(), anyone their own and someone else's with Financial; accounts can be read with their balances, added, changed, archived, unarchived and deleted while nothing is on them, other income and transfers read, recorded, changed and deleted, and recurring expenses read, set up, changed, paused and resumed, recorded now, moved on past missed runs and deleted, keeping reconciled lines as they were; contracts can be read with their prepaid hours and signing state, added, changed, made active, renewed, cancelled and deleted, sent for signature or the link sent again, the request withdrawn, prepaid hours added with a draft invoice or deleted, tickets' time drawn or put back, and the billing recurring invoice made, linked or unlinked; a client's files can be read, uploaded as base64, downloaded, renamed, moved, archived, unarchived and deleted once archived, and folders read, made, renamed, moved and deleted with their contents moved up; new api/v1/form_input.php builds a save function's input from the API's field names; the upload allow-list moves from agent/post/file.php into fileUploadExtensions() so Upload Files and the API check the same list; enforce_api_rbac.php maps the new resources, lets events, calendars and trips check their own rules, puts the new state changes at Modify and treats files/download as a read; 26.11.9 changelog entries
2026-11-13 23:51:10 -05:00

98 lines
4.7 KiB
PHP

<?php
// An event's fields from POST, as calendarEventFormSet() takes them - with what the
// event has now ($event_row) for those not sent, on an update. $client_id is the
// client it is for (0: nobody's). Sets $event_set (the SET clause) and $event_attendees
// (null to leave them), or turns the request down with 400 and why.
if (!function_exists('apiDeny')) {
http_response_code(404);
exit();
}
$event_row = $event_row ?? null;
$event_refuse = function ($message) use (&$return_arr) {
http_response_code(400);
$return_arr['success'] = "False";
$return_arr['message'] = $message;
echo json_encode($return_arr);
exit();
};
$sent = function ($field, $current) {
return array_key_exists($field, $_POST) ? (is_scalar($_POST[$field]) ? (string) $_POST[$field] : '') : (string) $current;
};
// When - "2026-11-20 14:00", or a day for an all-day event
$all_day = intval($sent('event_all_day', $event_row['event_all_day'] ?? 0)) ? 1 : 0;
$when = function ($field, $current) use ($sent, $all_day, $event_refuse) {
$value = trim($sent($field, $current !== null ? substr((string) $current, 0, 16) : ''));
if ($value === '') {
return ['', ''];
}
if (!preg_match('/^(\d{4}-\d{2}-\d{2})(?:[ T](\d{2}:\d{2}))?$/', $value, $m) || !calendarIsDate($m[1])) {
$event_refuse("$field is a date and time, such as 2026-11-20 14:00" . ($all_day ? ' - or a day, 2026-11-20, for an all-day event.' : '.'));
}
return [$m[1], $m[2] ?? ''];
};
[$start_date, $start_time] = $when('event_start', $event_row['event_start'] ?? null);
[$end_date, $end_time] = $when('event_end', $event_row['event_end'] ?? null);
if ($start_date === '') {
$event_refuse('event_start is when it starts, such as 2026-11-20 14:00.');
}
if (!$all_day && $start_time === '' && array_key_exists('event_start', $_POST)) {
$event_refuse('event_start needs a time, such as 2026-11-20 14:00 - or send event_all_day 1.');
}
$repeat = $sent('event_repeat', $event_row['event_repeat'] ?? '');
if ($repeat !== '' && !isset(CALENDAR_REPEATS[$repeat])) {
$event_refuse('event_repeat is Day, Week, Month or Year - or empty for once.');
}
$calendar_id = intval($sent('event_calendar_id', $event_row['event_calendar_id'] ?? 0));
if (!$calendar_id || !mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM calendars WHERE calendar_id = $calendar_id AND calendar_archived_at IS NULL"))) {
$event_refuse('event_calendar_id is the calendar it goes on - see calendars/read.php.');
}
$location_id = intval($sent('event_location_id', $client_id && intval($event_row['event_client_id'] ?? 0) === intval($client_id) ? ($event_row['event_location_id'] ?? 0) : 0));
if ($location_id && (!$client_id || !assetClientOwnedId('location', $location_id, $client_id))) {
$event_refuse("event_location_id isn't one of the client's locations.");
}
$error = '';
$event_set = calendarEventFormSet([
'title' => $sent('event_title', $event_row['event_title'] ?? ''),
'calendar' => $calendar_id,
'all_day' => $all_day,
'start_date' => $start_date,
'start_time' => $start_time,
'end_date' => $end_date,
'end_time' => $all_day ? '' : $end_time,
'repeat' => $repeat,
'repeat_interval' => $sent('event_repeat_interval', $event_row['event_repeat_interval'] ?? 1),
'repeat_until' => $sent('event_repeat_until', $event_row['event_repeat_until'] ?? ''),
'location' => $sent('event_location', $event_row['event_location'] ?? ''),
'location_id' => $location_id,
'description' => $sent('event_description', $event_row['event_description'] ?? ''),
], $client_id, $error);
if ($event_set === '') {
$event_refuse($error);
}
// Who is on it - agents, the client's contacts and anyone by email - when any of them is sent
$event_attendees = null;
if (array_key_exists('attendee_user_ids', $_POST) || array_key_exists('attendee_contact_ids', $_POST) || array_key_exists('attendee_emails', $_POST)) {
$current = $event_row ? calendarEventAttendees($event_row['event_id']) : [];
$keep = function ($kind) use ($current) {
return array_values(array_map(function ($a) use ($kind) {
return $kind === 'guest' ? $a['attendee_email'] : intval($a[$kind === 'agent' ? 'attendee_user_id' : 'attendee_contact_id']);
}, array_filter($current, function ($a) use ($kind) {
return $a['kind'] === $kind;
})));
};
$emails = $_POST['attendee_emails'] ?? $keep('guest');
$event_attendees = [
array_key_exists('attendee_user_ids', $_POST) ? (array) $_POST['attendee_user_ids'] : $keep('agent'),
array_key_exists('attendee_contact_ids', $_POST) ? (array) $_POST['attendee_contact_ids'] : $keep('contact'),
is_array($emails) ? $emails : preg_split('/[\s,;]+/', (string) $emails, -1, PREG_SPLIT_NO_EMPTY),
];
}