added new create from calendar feature

This commit is contained in:
2025-08-14 07:34:18 +02:00
parent 060a6300ed
commit 58b53f1404
2 changed files with 155 additions and 19 deletions

View File

@@ -671,6 +671,74 @@ class WarehouseShippingNoteController extends TTCrud {
}
}
protected function getRecentCalendarEventsAction()
{
$worker = UserModel::getOne($this->user->id);
if (!$worker || !empty($worker->name)) {
die($worker->name);
self::returnJson([]);
return;
}
$calendars = CalendarModel::getAll();
// loop through all calendars and find the one with user_id as the worker's id
$calendarId = null;
foreach ($calendars as $calendar) {
if ($calendar->user_id == $worker->id) {
$calendarId = $calendar->go_calendar_id;
break;
}
}
$eventsJson = CalendarModel::getCalendarEvents($this->user);
$allEvents = json_decode($eventsJson, true)['data'] ?? [];
if (!is_array($allEvents)) {
self::returnJson([]);
return;
}
$endDate = new DateTime();
$startDate = (new DateTime())->modify('-3 days');
$startTimestamp = $startDate->setTime(0, 0, 0)->getTimestamp();
$endTimestamp = $endDate->setTime(23, 59, 59)->getTimestamp();
$filteredEvents = array_filter($allEvents, function ($event) use ($startTimestamp, $endTimestamp, $calendarId) {
if (!isset($event['cstart']) && !isset($event['category']) || (intval($event['calendar_id']['calendar_id']) !== $calendarId)) {
return false;
}
$eventStartTimestamp = strtotime($event['cstart']['cstart'] ?? $event['cstart']);
if (empty($event['location']['location']) || empty($event['category']['category'])) {
return false;
}
if (date('H', $eventStartTimestamp) < 5 || date('H', $eventStartTimestamp) > 21) {
return false;
}
return $eventStartTimestamp >= $startTimestamp && $eventStartTimestamp <= $endTimestamp;
});
usort($filteredEvents, function ($a, $b) {
$timeA = strtotime($a['cstart']['cstart'] ?? $a['cstart']);
$timeB = strtotime($b['cstart']['cstart'] ?? $b['cstart']);
return $timeB <=> $timeA;
});
$limitedEvents = array_slice($filteredEvents, 0, 20);
$finalResponse = array_map(function ($event) {
$eventStart = $event['cstart']['cstart'] ?? $event['cstart'];
return [
'date' => date('Y-m-d H:i:s', strtotime($eventStart)),
'location' => $event['location']['location'] ?? '',
'category' => $event['category']['category'] ?? ''
];
}, $limitedEvents);
self::returnJson($finalResponse);
}
protected function createNewLogAction() {
$postData = json_decode(file_get_contents('php://input'), true);