95 lines
3.3 KiB
PHP
95 lines
3.3 KiB
PHP
<?php
|
|
require_once(__DIR__ . '/auth_check.php');
|
|
header('Content-Type: application/json');
|
|
require_once(__DIR__ . '/../../class/db-functions.php');
|
|
|
|
try {
|
|
$db = DBHandlerSelect::getInstance();
|
|
$pdo = $db->getConnection();
|
|
|
|
$start = $_GET['start'] ?? null;
|
|
$end = $_GET['end'] ?? null;
|
|
$filterStatus = $_GET['status'] ?? '';
|
|
$filterDept = $_GET['department'] ?? '';
|
|
$filterEmployee = $_GET['employee'] ?? '';
|
|
|
|
$sql = "SELECT DISTINCT d.id, d.topic, d.due_date, d.status, d.notification_days, d.departments,
|
|
s.name AS subject_name, s.color AS subject_color
|
|
FROM scad_deadlines d
|
|
LEFT JOIN scad_subjects s ON s.id = d.subject_id
|
|
LEFT JOIN scad_deadline_employee de ON de.deadline_id = d.id
|
|
LEFT JOIN employees e ON e.id = de.employee_id";
|
|
|
|
$where = [];
|
|
$params = [];
|
|
|
|
if ($start && $end) {
|
|
$where[] = "d.due_date >= ? AND d.due_date <= ?";
|
|
$params[] = $start;
|
|
$params[] = $end;
|
|
}
|
|
|
|
if ($filterStatus === 'non-completata') {
|
|
$where[] = "d.status != 'completed'";
|
|
} elseif ($filterStatus === 'completata') {
|
|
$where[] = "d.status = 'completed'";
|
|
} elseif ($filterStatus === 'scaduta') {
|
|
$where[] = "d.status = 'active' AND d.due_date < CURDATE()";
|
|
} elseif ($filterStatus === 'in-scadenza') {
|
|
$where[] = "d.status = 'active' AND d.due_date >= CURDATE() AND d.due_date <= DATE_ADD(CURDATE(), INTERVAL d.notification_days DAY)";
|
|
} elseif ($filterStatus === 'attiva') {
|
|
$where[] = "d.status = 'active' AND d.due_date > DATE_ADD(CURDATE(), INTERVAL d.notification_days DAY)";
|
|
}
|
|
|
|
if ($filterDept) {
|
|
$where[] = "(e.department = ? OR FIND_IN_SET(?, d.departments))";
|
|
$params[] = $filterDept;
|
|
$params[] = $filterDept;
|
|
}
|
|
|
|
if ($filterEmployee) {
|
|
$where[] = "CONCAT(e.first_name, ' ', e.last_name) = ?";
|
|
$params[] = $filterEmployee;
|
|
}
|
|
|
|
if (!empty($where)) {
|
|
$sql .= " WHERE " . implode(' AND ', $where);
|
|
}
|
|
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
$deadlines = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$today = date('Y-m-d');
|
|
$events = [];
|
|
|
|
foreach ($deadlines as $d) {
|
|
$isCompleted = $d['status'] === 'completed';
|
|
$isOverdue = !$isCompleted && $d['due_date'] < $today;
|
|
$approachDate = date('Y-m-d', strtotime($today . ' + ' . (int)$d['notification_days'] . ' days'));
|
|
$isApproaching = !$isCompleted && !$isOverdue && $d['due_date'] <= $approachDate;
|
|
|
|
if ($isCompleted) { $color = '#198754'; }
|
|
elseif ($isOverdue) { $color = '#dc3545'; }
|
|
elseif ($isApproaching) { $color = '#e8930c'; }
|
|
else { $color = '#5a8fd8'; }
|
|
|
|
$title = $d['topic'];
|
|
if (!empty($d['subject_name'])) $title = $d['subject_name'] . ': ' . $title;
|
|
|
|
$events[] = [
|
|
'id' => $d['id'],
|
|
'title' => $title,
|
|
'start' => $d['due_date'],
|
|
'backgroundColor' => $color,
|
|
'borderColor' => !empty($d['subject_color']) ? $d['subject_color'] : $color,
|
|
'url' => 'scadenzario/detail.php?id=' . $d['id'],
|
|
];
|
|
}
|
|
|
|
echo json_encode($events);
|
|
|
|
} catch (Exception $e) {
|
|
echo json_encode([]);
|
|
}
|