getConnection();
function scadJsonResponse(array $data): void
{
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data);
exit;
}
function scadNullableString($value): ?string
{
$value = trim((string)($value ?? ''));
return $value !== '' ? $value : null;
}
function scadNormalizeStatus(string $status): string
{
return in_array($status, ['active', 'inactive'], true) ? $status : 'active';
}
function scadValidateEmail($email): ?string
{
$email = scadNullableString($email);
if ($email !== null && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new Exception('Email non valida.');
}
return $email;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ajax']) && $_POST['ajax'] === '1') {
try {
$action = $_POST['action'] ?? '';
if ($action === 'save') {
$id = isset($_POST['id']) && $_POST['id'] !== '' ? (int)$_POST['id'] : 0;
$name = trim($_POST['name'] ?? '');
$description = scadNullableString($_POST['description'] ?? null);
$personFullName = scadNullableString($_POST['person_full_name'] ?? null);
$phone = scadNullableString($_POST['phone'] ?? null);
$email = scadValidateEmail($_POST['email'] ?? null);
$notes = scadNullableString($_POST['notes'] ?? null);
$sortOrder = isset($_POST['sort_order']) && $_POST['sort_order'] !== '' ? (int)$_POST['sort_order'] : 0;
$status = scadNormalizeStatus(trim($_POST['status'] ?? 'active'));
if ($name === '') {
scadJsonResponse(['success' => false, 'message' => 'Il nome funzione รจ obbligatorio.']);
}
if ($id > 0) {
$stmt = $pdo->prepare("\n UPDATE scad_functions\n SET name = :name,\n description = :description,\n person_full_name = :person_full_name,\n phone = :phone,\n email = :email,\n notes = :notes,\n sort_order = :sort_order,\n status = :status,\n updated_at = NOW()\n WHERE id = :id\n ");
$stmt->execute([
'name' => $name,
'description' => $description,
'person_full_name' => $personFullName,
'phone' => $phone,
'email' => $email,
'notes' => $notes,
'sort_order' => $sortOrder,
'status' => $status,
'id' => $id,
]);
scadJsonResponse(['success' => true, 'message' => 'Funzione aggiornata correttamente.']);
}
$stmt = $pdo->prepare("\n INSERT INTO scad_functions\n (name, description, person_full_name, phone, email, notes, sort_order, status, created_at, updated_at)\n VALUES\n (:name, :description, :person_full_name, :phone, :email, :notes, :sort_order, :status, NOW(), NOW())\n ");
$stmt->execute([
'name' => $name,
'description' => $description,
'person_full_name' => $personFullName,
'phone' => $phone,
'email' => $email,
'notes' => $notes,
'sort_order' => $sortOrder,
'status' => $status,
]);
scadJsonResponse(['success' => true, 'message' => 'Funzione creata correttamente.']);
}
if ($action === 'delete') {
$id = (int)($_POST['id'] ?? 0);
if ($id <= 0) {
scadJsonResponse(['success' => false, 'message' => 'ID funzione non valido.']);
}
$stmtUse = $pdo->prepare('SELECT COUNT(*) FROM scad_deadlines WHERE function_id = ?');
$stmtUse->execute([$id]);
$inUse = (int)$stmtUse->fetchColumn();
if ($inUse > 0) {
scadJsonResponse([
'success' => false,
'message' => 'Impossibile eliminare: la funzione รจ utilizzata in ' . $inUse . ' scadenza/e.'
]);
}
$stmt = $pdo->prepare('DELETE FROM scad_functions WHERE id = :id');
$stmt->execute(['id' => $id]);
scadJsonResponse(['success' => true, 'message' => 'Funzione eliminata correttamente.']);
}
scadJsonResponse(['success' => false, 'message' => 'Azione non riconosciuta.']);
} catch (Exception $e) {
scadJsonResponse(['success' => false, 'message' => $e->getMessage()]);
}
}
$functions = $pdo->query("\n SELECT f.*,\n (SELECT COUNT(*) FROM scad_deadlines d WHERE d.function_id = f.id) AS deadline_count,\n (SELECT COUNT(*) FROM scad_deadlines d WHERE d.function_id = f.id AND d.status <> 'completed') AS open_count\n FROM scad_functions f\n ORDER BY COALESCE(f.sort_order, 0) ASC, f.name ASC\n")->fetchAll(PDO::FETCH_ASSOC);
?>
Scadenzario - Funzioni
Nessuna funzione definita.
Clicca "Nuova Funzione" per aggiungere la prima.
= htmlspecialchars($f['name'], ENT_QUOTES, 'UTF-8') ?>
= $statusLabel ?>
= htmlspecialchars($f['description'], ENT_QUOTES, 'UTF-8') ?>
Referente: = htmlspecialchars($f['person_full_name'], ENT_QUOTES, 'UTF-8') ?>
๐ = htmlspecialchars($f['phone'], ENT_QUOTES, 'UTF-8') ?>
= !empty($f['phone']) ? '
' : '' ?>โ๏ธ = htmlspecialchars($f['email'], ENT_QUOTES, 'UTF-8') ?>
Scadenze: = (int)$f['deadline_count'] ?>
Aperte: = (int)$f['open_count'] ?>
Ordine: = (int)($f['sort_order'] ?? 0) ?>
| Ord. |
Funzione |
Referente |
Contatti |
Stato |
Scadenze |
Aperte |
Azioni |
| = (int)($f['sort_order'] ?? 0) ?> |
= htmlspecialchars($f['name'], ENT_QUOTES, 'UTF-8') ?>
= htmlspecialchars($f['description'], ENT_QUOTES, 'UTF-8') ?>
Note: = htmlspecialchars($f['notes'], ENT_QUOTES, 'UTF-8') ?>
|
= !empty($f['person_full_name']) ? htmlspecialchars($f['person_full_name'], ENT_QUOTES, 'UTF-8') : 'โ' ?> |
โ
|
= $statusLabel ?> |
= (int)$f['deadline_count'] ?> |
= (int)$f['open_count'] ?> |
|