781 lines
36 KiB
PHP
781 lines
36 KiB
PHP
<?php include('../../include/headscript.php'); ?>
|
|
<?php
|
|
ini_set('display_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
$db = DBHandlerSelect::getInstance();
|
|
$pdo = $db->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);
|
|
?>
|
|
<!doctype html>
|
|
<html lang="it">
|
|
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<?php
|
|
$scriptDir = dirname($_SERVER['SCRIPT_NAME']);
|
|
$baseHref = dirname(dirname($scriptDir)) . '/';
|
|
?>
|
|
<base href="<?= $baseHref ?>">
|
|
<?php include('../../cssinclude.php'); ?>
|
|
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
|
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
|
|
<title>Scadenzario - Funzioni</title>
|
|
<script>
|
|
if (window.innerWidth > 1024) {
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
const wrapper = document.getElementById('appWrapper');
|
|
if (wrapper) wrapper.classList.add('toggled');
|
|
});
|
|
}
|
|
</script>
|
|
<style>
|
|
:root {
|
|
--scad-primary: #5a8fd8;
|
|
--scad-primary-hover: #4578c0;
|
|
--scad-heading: #2c3e6b;
|
|
--scad-card-bg: linear-gradient(135deg, #f0f4ff 0%, #e8eeff 100%);
|
|
--scad-card-border: #dde4f0;
|
|
--scad-muted: #6c757d;
|
|
}
|
|
|
|
.scad-card {
|
|
border: none;
|
|
border-radius: 0.75rem;
|
|
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
|
|
overflow: hidden;
|
|
}
|
|
|
|
.scad-card .card-header {
|
|
background: var(--scad-card-bg);
|
|
border-bottom: 1px solid var(--scad-card-border);
|
|
padding: 1rem 1.25rem;
|
|
}
|
|
|
|
.scad-card .card-header h5 {
|
|
font-weight: 700;
|
|
color: var(--scad-heading);
|
|
margin: 0;
|
|
font-size: 1.1rem;
|
|
}
|
|
|
|
.scad-card .card-body {
|
|
padding: 1.25rem;
|
|
}
|
|
|
|
.btn-scad-primary {
|
|
background: var(--scad-primary);
|
|
border: none;
|
|
color: #fff;
|
|
font-weight: 600;
|
|
font-size: 0.85rem;
|
|
padding: 0.5rem 1rem;
|
|
border-radius: 0.5rem;
|
|
}
|
|
|
|
.btn-scad-primary:hover {
|
|
background: var(--scad-primary-hover);
|
|
color: #fff;
|
|
}
|
|
|
|
.btn-scad-outline {
|
|
background: transparent;
|
|
border: 1.5px solid var(--scad-primary);
|
|
color: var(--scad-primary);
|
|
font-weight: 600;
|
|
font-size: 0.85rem;
|
|
padding: 0.45rem 1rem;
|
|
border-radius: 0.5rem;
|
|
}
|
|
|
|
.btn-scad-outline:hover {
|
|
background: var(--scad-primary);
|
|
color: #fff;
|
|
}
|
|
|
|
.btn-action {
|
|
width: 32px;
|
|
height: 32px;
|
|
padding: 0;
|
|
display: inline-flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
border: none;
|
|
border-radius: 0.4rem;
|
|
font-size: 0.85rem;
|
|
}
|
|
|
|
.btn-action-edit {
|
|
background: rgba(90, 143, 216, 0.12);
|
|
color: var(--scad-primary);
|
|
}
|
|
|
|
.btn-action-edit:hover {
|
|
background: var(--scad-primary);
|
|
color: #fff;
|
|
}
|
|
|
|
.btn-action-delete {
|
|
background: rgba(220, 53, 69, 0.12);
|
|
color: #dc3545;
|
|
}
|
|
|
|
.btn-action-delete:hover {
|
|
background: #dc3545;
|
|
color: #fff;
|
|
}
|
|
|
|
.function-table th {
|
|
font-size: 0.82rem;
|
|
color: var(--scad-heading);
|
|
background: #f8fafc;
|
|
border-bottom: 1px solid var(--scad-card-border);
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.function-table td {
|
|
font-size: 0.9rem;
|
|
vertical-align: middle;
|
|
}
|
|
|
|
.function-name {
|
|
font-weight: 700;
|
|
color: var(--scad-heading);
|
|
}
|
|
|
|
.function-description,
|
|
.function-notes {
|
|
color: var(--scad-muted);
|
|
font-size: 0.82rem;
|
|
margin-top: 2px;
|
|
max-width: 280px;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.contact-line {
|
|
font-size: 0.85rem;
|
|
line-height: 1.45;
|
|
}
|
|
|
|
.contact-line a {
|
|
text-decoration: none;
|
|
}
|
|
|
|
.badge-function-status {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
border-radius: 999px;
|
|
padding: 4px 10px;
|
|
font-size: 0.78rem;
|
|
font-weight: 700;
|
|
}
|
|
|
|
.badge-function-status.active {
|
|
background: #d1fae5;
|
|
color: #065f46;
|
|
}
|
|
|
|
.badge-function-status.inactive {
|
|
background: #e5e7eb;
|
|
color: #374151;
|
|
}
|
|
|
|
.function-card {
|
|
background: #fff;
|
|
border: 1px solid var(--scad-card-border);
|
|
border-radius: 0.6rem;
|
|
padding: 0.85rem 0.95rem;
|
|
margin-bottom: 0.6rem;
|
|
}
|
|
|
|
.function-card .fc-name {
|
|
font-weight: 700;
|
|
color: var(--scad-heading);
|
|
font-size: 0.95rem;
|
|
}
|
|
|
|
.function-card .fc-meta {
|
|
font-size: 0.82rem;
|
|
color: var(--scad-muted);
|
|
margin-top: 0.35rem;
|
|
}
|
|
|
|
.function-card .fc-stats {
|
|
display: flex;
|
|
gap: 0.75rem;
|
|
font-size: 0.8rem;
|
|
color: var(--scad-muted);
|
|
margin: 0.5rem 0;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
.function-card .fc-stats strong {
|
|
color: var(--scad-heading);
|
|
}
|
|
|
|
.empty-state {
|
|
text-align: center;
|
|
padding: 3rem 1rem;
|
|
color: var(--scad-muted);
|
|
}
|
|
|
|
.empty-state i {
|
|
font-size: 3rem;
|
|
opacity: 0.3;
|
|
margin-bottom: 1rem;
|
|
}
|
|
|
|
.modal-content {
|
|
border-radius: 0.75rem;
|
|
}
|
|
|
|
.modal-header {
|
|
background: var(--scad-card-bg);
|
|
border-bottom: 1px solid var(--scad-card-border);
|
|
}
|
|
|
|
@media (max-width: 575.98px) {
|
|
.scad-card .card-header {
|
|
flex-direction: column;
|
|
gap: 0.75rem;
|
|
align-items: flex-start !important;
|
|
}
|
|
|
|
.header-actions {
|
|
width: 100%;
|
|
}
|
|
|
|
.header-actions .btn {
|
|
width: 100%;
|
|
justify-content: center;
|
|
}
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<div class="wrapper" id="appWrapper">
|
|
<?php include('../../include/navbar.php'); ?>
|
|
<?php include('../../include/topbar.php'); ?>
|
|
|
|
<div class="page-wrapper">
|
|
<div class="page-content">
|
|
|
|
<nav aria-label="breadcrumb" class="mb-3">
|
|
<ol class="breadcrumb" style="background:transparent;padding:0;margin:0;font-size:0.85rem">
|
|
<li class="breadcrumb-item"><a href="scadenzario/index.php">Scadenzario</a></li>
|
|
<li class="breadcrumb-item active" aria-current="page">Funzioni</li>
|
|
</ol>
|
|
</nav>
|
|
|
|
<div class="card scad-card">
|
|
<div class="card-header d-flex align-items-center justify-content-between flex-wrap gap-2">
|
|
<h5><i class="fa-solid fa-briefcase me-2"></i>Funzioni</h5>
|
|
|
|
<div class="header-actions d-flex gap-2 flex-wrap">
|
|
<a href="scadenzario/index.php" class="btn btn-scad-outline d-inline-flex align-items-center gap-2">
|
|
<i class="fa-solid fa-arrow-left"></i><span>Scadenzario</span>
|
|
</a>
|
|
|
|
<button class="btn btn-scad-primary d-inline-flex align-items-center gap-2" id="btnAddFunction">
|
|
<i class="fa-solid fa-plus"></i><span>Nuova Funzione</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card-body">
|
|
<?php if (count($functions) === 0): ?>
|
|
<div class="empty-state">
|
|
<i class="fa-solid fa-briefcase"></i>
|
|
<p>Nessuna funzione definita.<br>Clicca <strong>"Nuova Funzione"</strong> per aggiungere la prima.</p>
|
|
</div>
|
|
<?php else: ?>
|
|
|
|
<div id="functionsList">
|
|
<div class="d-md-none">
|
|
<?php foreach ($functions as $f): ?>
|
|
<?php
|
|
$status = $f['status'] === 'inactive' ? 'inactive' : 'active';
|
|
$statusLabel = $status === 'active' ? 'Attiva' : 'Non attiva';
|
|
?>
|
|
<div class="function-card"
|
|
data-id="<?= (int)$f['id'] ?>"
|
|
data-name="<?= htmlspecialchars($f['name'], ENT_QUOTES, 'UTF-8') ?>"
|
|
data-description="<?= htmlspecialchars($f['description'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
|
|
data-person-full-name="<?= htmlspecialchars($f['person_full_name'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
|
|
data-phone="<?= htmlspecialchars($f['phone'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
|
|
data-email="<?= htmlspecialchars($f['email'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
|
|
data-notes="<?= htmlspecialchars($f['notes'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
|
|
data-sort-order="<?= (int)($f['sort_order'] ?? 0) ?>"
|
|
data-status="<?= htmlspecialchars($status, ENT_QUOTES, 'UTF-8') ?>"
|
|
data-in-use="<?= (int)$f['deadline_count'] ?>">
|
|
|
|
<div class="d-flex justify-content-between align-items-start gap-2">
|
|
<div class="fc-name"><?= htmlspecialchars($f['name'], ENT_QUOTES, 'UTF-8') ?></div>
|
|
<span class="badge-function-status <?= $status ?>"><?= $statusLabel ?></span>
|
|
</div>
|
|
|
|
<?php if (!empty($f['description'])): ?>
|
|
<div class="fc-meta"><?= htmlspecialchars($f['description'], ENT_QUOTES, 'UTF-8') ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (!empty($f['person_full_name'])): ?>
|
|
<div class="fc-meta"><strong>Referente:</strong> <?= htmlspecialchars($f['person_full_name'], ENT_QUOTES, 'UTF-8') ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if (!empty($f['phone']) || !empty($f['email'])): ?>
|
|
<div class="fc-meta">
|
|
<?php if (!empty($f['phone'])): ?>📞 <?= htmlspecialchars($f['phone'], ENT_QUOTES, 'UTF-8') ?><?php endif; ?>
|
|
<?php if (!empty($f['email'])): ?><?= !empty($f['phone']) ? '<br>' : '' ?>✉️ <?= htmlspecialchars($f['email'], ENT_QUOTES, 'UTF-8') ?><?php endif; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="fc-stats">
|
|
<span>Scadenze: <strong><?= (int)$f['deadline_count'] ?></strong></span>
|
|
<span>Aperte: <strong><?= (int)$f['open_count'] ?></strong></span>
|
|
<span>Ordine: <strong><?= (int)($f['sort_order'] ?? 0) ?></strong></span>
|
|
</div>
|
|
|
|
<div class="d-flex gap-1 justify-content-end">
|
|
<button class="btn-action btn-action-edit btn-edit" title="Modifica">
|
|
<i class="fa-solid fa-pen"></i>
|
|
</button>
|
|
<button class="btn-action btn-action-delete btn-delete" title="Elimina">
|
|
<i class="fa-solid fa-trash"></i>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
|
|
<div class="d-none d-md-block">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle mb-0 function-table">
|
|
<thead>
|
|
<tr>
|
|
<th style="width:70px" class="text-center">Ord.</th>
|
|
<th>Funzione</th>
|
|
<th style="width:220px">Referente</th>
|
|
<th style="width:230px">Contatti</th>
|
|
<th style="width:120px" class="text-center">Stato</th>
|
|
<th style="width:120px" class="text-center">Scadenze</th>
|
|
<th style="width:120px" class="text-center">Aperte</th>
|
|
<th style="width:120px" class="text-center">Azioni</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($functions as $f): ?>
|
|
<?php
|
|
$status = $f['status'] === 'inactive' ? 'inactive' : 'active';
|
|
$statusLabel = $status === 'active' ? 'Attiva' : 'Non attiva';
|
|
?>
|
|
<tr data-id="<?= (int)$f['id'] ?>"
|
|
data-name="<?= htmlspecialchars($f['name'], ENT_QUOTES, 'UTF-8') ?>"
|
|
data-description="<?= htmlspecialchars($f['description'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
|
|
data-person-full-name="<?= htmlspecialchars($f['person_full_name'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
|
|
data-phone="<?= htmlspecialchars($f['phone'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
|
|
data-email="<?= htmlspecialchars($f['email'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
|
|
data-notes="<?= htmlspecialchars($f['notes'] ?? '', ENT_QUOTES, 'UTF-8') ?>"
|
|
data-sort-order="<?= (int)($f['sort_order'] ?? 0) ?>"
|
|
data-status="<?= htmlspecialchars($status, ENT_QUOTES, 'UTF-8') ?>"
|
|
data-in-use="<?= (int)$f['deadline_count'] ?>">
|
|
|
|
<td class="text-center"><?= (int)($f['sort_order'] ?? 0) ?></td>
|
|
<td>
|
|
<div class="function-name"><?= htmlspecialchars($f['name'], ENT_QUOTES, 'UTF-8') ?></div>
|
|
<?php if (!empty($f['description'])): ?>
|
|
<div class="function-description" title="<?= htmlspecialchars($f['description'], ENT_QUOTES, 'UTF-8') ?>">
|
|
<?= htmlspecialchars($f['description'], ENT_QUOTES, 'UTF-8') ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php if (!empty($f['notes'])): ?>
|
|
<div class="function-notes" title="<?= htmlspecialchars($f['notes'], ENT_QUOTES, 'UTF-8') ?>">
|
|
Note: <?= htmlspecialchars($f['notes'], ENT_QUOTES, 'UTF-8') ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td><?= !empty($f['person_full_name']) ? htmlspecialchars($f['person_full_name'], ENT_QUOTES, 'UTF-8') : '<span class="text-muted">—</span>' ?></td>
|
|
<td>
|
|
<?php if (!empty($f['phone'])): ?>
|
|
<div class="contact-line">📞 <a href="tel:<?= htmlspecialchars($f['phone'], ENT_QUOTES, 'UTF-8') ?>"><?= htmlspecialchars($f['phone'], ENT_QUOTES, 'UTF-8') ?></a></div>
|
|
<?php endif; ?>
|
|
<?php if (!empty($f['email'])): ?>
|
|
<div class="contact-line">✉️ <a href="mailto:<?= htmlspecialchars($f['email'], ENT_QUOTES, 'UTF-8') ?>"><?= htmlspecialchars($f['email'], ENT_QUOTES, 'UTF-8') ?></a></div>
|
|
<?php endif; ?>
|
|
<?php if (empty($f['phone']) && empty($f['email'])): ?>
|
|
<span class="text-muted">—</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td class="text-center"><span class="badge-function-status <?= $status ?>"><?= $statusLabel ?></span></td>
|
|
<td class="text-center"><?= (int)$f['deadline_count'] ?></td>
|
|
<td class="text-center"><?= (int)$f['open_count'] ?></td>
|
|
<td class="text-center">
|
|
<div class="d-inline-flex gap-1">
|
|
<button class="btn-action btn-action-edit btn-edit" title="Modifica">
|
|
<i class="fa-solid fa-pen"></i>
|
|
</button>
|
|
<button class="btn-action btn-action-delete btn-delete" title="Elimina">
|
|
<i class="fa-solid fa-trash"></i>
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<?php include('../../include/footer.php'); ?>
|
|
</div>
|
|
|
|
<div class="modal fade" id="functionModal" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered modal-lg modal-dialog-scrollable">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="functionModalTitle">Nuova Funzione</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Chiudi"></button>
|
|
</div>
|
|
|
|
<form id="functionForm">
|
|
<div class="modal-body">
|
|
<input type="hidden" id="functionId" name="id" value="">
|
|
|
|
<div class="row">
|
|
<div class="col-12 col-md-8 mb-3">
|
|
<label for="functionName" class="form-label fw-semibold">Nome funzione <span class="text-danger">*</span></label>
|
|
<input type="text" class="form-control" id="functionName" name="name" maxlength="255" required placeholder="Es. RSPP, Medico del lavoro, RLS">
|
|
</div>
|
|
<div class="col-12 col-md-4 mb-3">
|
|
<label for="functionSortOrder" class="form-label fw-semibold">Ordine</label>
|
|
<input type="number" class="form-control" id="functionSortOrder" name="sort_order" min="0" step="1" value="0">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label for="functionDescription" class="form-label fw-semibold">Descrizione</label>
|
|
<textarea class="form-control" id="functionDescription" name="description" rows="2"></textarea>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label for="functionPersonFullName" class="form-label fw-semibold">Nome e cognome referente</label>
|
|
<input type="text" class="form-control" id="functionPersonFullName" name="person_full_name" maxlength="200" placeholder="Es. Mario Rossi">
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="col-12 col-md-6 mb-3">
|
|
<label for="functionPhone" class="form-label fw-semibold">Telefono</label>
|
|
<input type="text" class="form-control" id="functionPhone" name="phone" maxlength="80">
|
|
</div>
|
|
<div class="col-12 col-md-6 mb-3">
|
|
<label for="functionEmail" class="form-label fw-semibold">Email</label>
|
|
<input type="email" class="form-control" id="functionEmail" name="email" maxlength="190">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="col-12 col-md-6 mb-3">
|
|
<label for="functionStatus" class="form-label fw-semibold">Stato</label>
|
|
<select class="form-select" id="functionStatus" name="status">
|
|
<option value="active">Attiva</option>
|
|
<option value="inactive">Non attiva</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label for="functionNotes" class="form-label fw-semibold">Note operative</label>
|
|
<textarea class="form-control" id="functionNotes" name="notes" rows="3"></textarea>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-light" data-bs-dismiss="modal">Annulla</button>
|
|
<button type="submit" class="btn btn-scad-primary" id="functionSaveBtn">Salva</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include('../../jsinclude.php'); ?>
|
|
|
|
<script>
|
|
$(function() {
|
|
function escapeHtml(value) {
|
|
return String(value == null ? '' : value).replace(/[&<>'"]/g, function(c) {
|
|
return ({
|
|
'&': '&',
|
|
'<': '<',
|
|
'>': '>',
|
|
"'": ''',
|
|
'"': '"'
|
|
})[c];
|
|
});
|
|
}
|
|
|
|
function getRowData($row) {
|
|
return {
|
|
id: $row.data('id') || '',
|
|
name: $row.data('name') || '',
|
|
description: $row.data('description') || '',
|
|
person_full_name: $row.data('person-full-name') || '',
|
|
phone: $row.data('phone') || '',
|
|
email: $row.data('email') || '',
|
|
notes: $row.data('notes') || '',
|
|
sort_order: $row.data('sort-order') || 0,
|
|
status: $row.data('status') || 'active',
|
|
in_use: parseInt($row.data('in-use') || 0, 10)
|
|
};
|
|
}
|
|
|
|
function openModal(data) {
|
|
const isEdit = !!data;
|
|
|
|
$('#functionModalTitle').text(isEdit ? 'Modifica Funzione' : 'Nuova Funzione');
|
|
$('#functionId').val(isEdit ? data.id : '');
|
|
$('#functionName').val(isEdit ? data.name : '');
|
|
$('#functionDescription').val(isEdit ? data.description : '');
|
|
$('#functionPersonFullName').val(isEdit ? data.person_full_name : '');
|
|
$('#functionPhone').val(isEdit ? data.phone : '');
|
|
$('#functionEmail').val(isEdit ? data.email : '');
|
|
$('#functionNotes').val(isEdit ? data.notes : '');
|
|
$('#functionSortOrder').val(isEdit ? data.sort_order : 0);
|
|
$('#functionStatus').val(isEdit ? data.status : 'active');
|
|
$('#functionSaveBtn').prop('disabled', false).html('Salva');
|
|
|
|
new bootstrap.Modal('#functionModal').show();
|
|
}
|
|
|
|
$('#btnAddFunction').on('click', function() {
|
|
openModal(null);
|
|
});
|
|
|
|
$('#functionsList').on('click', '.btn-edit', function() {
|
|
const $row = $(this).closest('[data-id]');
|
|
openModal(getRowData($row));
|
|
});
|
|
|
|
$('#functionsList').on('click', '.btn-delete', function() {
|
|
const $row = $(this).closest('[data-id]');
|
|
const data = getRowData($row);
|
|
|
|
if (data.in_use > 0) {
|
|
Swal.fire({
|
|
icon: 'warning',
|
|
title: 'Impossibile eliminare',
|
|
text: 'La funzione "' + data.name + '" è utilizzata in ' + data.in_use + ' scadenza/e.'
|
|
});
|
|
return;
|
|
}
|
|
|
|
Swal.fire({
|
|
title: 'Eliminare "' + data.name + '"?',
|
|
icon: 'warning',
|
|
showCancelButton: true,
|
|
confirmButtonText: 'Elimina',
|
|
cancelButtonText: 'Annulla',
|
|
confirmButtonColor: '#dc3545'
|
|
}).then(function(result) {
|
|
if (!result.isConfirmed) return;
|
|
|
|
$.post(window.location.href.split('#')[0], {
|
|
ajax: '1',
|
|
action: 'delete',
|
|
id: data.id
|
|
})
|
|
.done(function(res) {
|
|
if (res.success) {
|
|
location.reload();
|
|
} else {
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: 'Errore',
|
|
text: res.message || 'Impossibile eliminare.'
|
|
});
|
|
}
|
|
})
|
|
.fail(function() {
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: 'Errore di rete'
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
$('#functionForm').on('submit', function(e) {
|
|
e.preventDefault();
|
|
|
|
const $btn = $('#functionSaveBtn');
|
|
const payload = {
|
|
ajax: '1',
|
|
action: 'save',
|
|
id: $('#functionId').val(),
|
|
name: $('#functionName').val().trim(),
|
|
description: $('#functionDescription').val().trim(),
|
|
person_full_name: $('#functionPersonFullName').val().trim(),
|
|
phone: $('#functionPhone').val().trim(),
|
|
email: $('#functionEmail').val().trim(),
|
|
notes: $('#functionNotes').val().trim(),
|
|
sort_order: $('#functionSortOrder').val(),
|
|
status: $('#functionStatus').val()
|
|
};
|
|
|
|
if (!payload.name) {
|
|
Swal.fire({
|
|
icon: 'warning',
|
|
title: 'Nome obbligatorio'
|
|
});
|
|
return;
|
|
}
|
|
|
|
$btn.prop('disabled', true).html('<span class="spinner-border spinner-border-sm me-1"></span>Salvataggio...');
|
|
|
|
$.post(window.location.href.split('#')[0], payload)
|
|
.done(function(res) {
|
|
if (res.success) {
|
|
location.reload();
|
|
} else {
|
|
$btn.prop('disabled', false).html('Salva');
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: 'Errore',
|
|
text: res.message || 'Impossibile salvare.'
|
|
});
|
|
}
|
|
})
|
|
.fail(function() {
|
|
$btn.prop('disabled', false).html('Salva');
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: 'Errore di rete'
|
|
});
|
|
});
|
|
});
|
|
});
|
|
</script>
|
|
</body>
|
|
|
|
</html>
|