= ?";
$params[] = $filters['date_from'];
}
if (!empty($filters['date_to'])) {
$where[] = "DATE(created_at) <= ?";
$params[] = $filters['date_to'];
}
if (!empty($filters['search'])) {
$where[] = "(action_description LIKE ? OR username LIKE ?)";
$search_term = '%' . $filters['search'] . '%';
$params[] = $search_term;
$params[] = $search_term;
}
$where_clause = !empty($where) ? 'WHERE ' . implode(' AND ', $where) : '';
$logs = $db->fetchAll(
"SELECT * FROM activity_logs
$where_clause
ORDER BY created_at DESC",
$params
);
// Classe semplice per generare PDF (HTML per stampa)
class LogPDF {
private $content = '';
public function addTitle($title) {
$this->content .= "
$title
";
}
public function addSubtitle($subtitle) {
$this->content .= "$subtitle
";
}
public function addText($text) {
$this->content .= "$text
";
}
public function addTable($headers, $rows) {
$this->content .= "";
$this->content .= "";
foreach ($headers as $header) {
$this->content .= "| $header | ";
}
$this->content .= "
";
$odd = true;
foreach ($rows as $row) {
$bgColor = $odd ? '#ffffff' : '#f8f9fa';
$this->content .= "";
foreach ($row as $cell) {
$this->content .= "| $cell | ";
}
$this->content .= "
";
$odd = !$odd;
}
$this->content .= "
";
}
public function output($filename) {
header('Content-Type: text/html; charset=utf-8');
header('Content-Disposition: inline; filename="' . $filename . '.html"');
echo "
$filename
";
echo "";
echo "";
echo "";
echo "
";
echo $this->content;
echo "";
echo "
Generato il " . date('d/m/Y H:i') . " da " . htmlspecialchars($_SESSION['username']) . " - " . APP_NAME . "
";
echo "
";
echo "";
}
}
$pdf = new LogPDF();
// Titolo
$pdf->addTitle('Log Attività Sistema');
// Informazioni sul report
$info_text = "Report generato il " . date('d/m/Y H:i');
if (!empty($filters)) {
$info_text .= " - Filtri applicati: ";
$filter_parts = [];
if (!empty($filters['user_id'])) {
$user = $db->fetchOne("SELECT username FROM users WHERE id = ?", [$filters['user_id']]);
$filter_parts[] = "Utente: " . htmlspecialchars($user['username']);
}
if (!empty($filters['action_type'])) {
$filter_parts[] = "Azione: " . htmlspecialchars($filters['action_type']);
}
if (!empty($filters['entity_type'])) {
$filter_parts[] = "Entità: " . htmlspecialchars($filters['entity_type']);
}
if (!empty($filters['date_from'])) {
$filter_parts[] = "Dal: " . formatDate($filters['date_from']);
}
if (!empty($filters['date_to'])) {
$filter_parts[] = "Al: " . formatDate($filters['date_to']);
}
if (!empty($filters['search'])) {
$filter_parts[] = "Ricerca: " . htmlspecialchars($filters['search']);
}
$info_text .= implode(', ', $filter_parts);
}
$pdf->addText($info_text);
$pdf->addText("Totale log: " . count($logs));
// Prepara i dati per la tabella
$headers = ['Data/Ora', 'Utente', 'Azione', 'Descrizione', 'Entità', 'IP'];
$rows = [];
foreach ($logs as $log) {
$action_badge = strtoupper(htmlspecialchars($log['action_type']));
$entity_info = '-';
if ($log['entity_type']) {
$entity_info = htmlspecialchars($log['entity_type']);
if ($log['entity_id']) {
$entity_info .= ' #' . $log['entity_id'];
}
}
$rows[] = [
formatDateTime($log['created_at']),
htmlspecialchars($log['username']),
$action_badge,
htmlspecialchars($log['action_description']),
$entity_info,
htmlspecialchars($log['ip_address'])
];
}
$pdf->addTable($headers, $rows);
// Riepilogo per tipo di azione
$action_summary = $db->fetchAll(
"SELECT action_type, COUNT(*) as count
FROM activity_logs
$where_clause
GROUP BY action_type
ORDER BY count DESC",
$params
);
if (!empty($action_summary)) {
$pdf->addSubtitle('Riepilogo per Tipo di Azione');
$summary_headers = ['Tipo Azione', 'Numero Occorrenze'];
$summary_rows = [];
foreach ($action_summary as $summary) {
$summary_rows[] = [
strtoupper(htmlspecialchars($summary['action_type'])),
number_format($summary['count'])
];
}
$pdf->addTable($summary_headers, $summary_rows);
}
// Riepilogo per utente
$user_summary = $db->fetchAll(
"SELECT username, COUNT(*) as count
FROM activity_logs
$where_clause
GROUP BY username
ORDER BY count DESC",
$params
);
if (!empty($user_summary)) {
$pdf->addSubtitle('Riepilogo per Utente');
$user_headers = ['Utente', 'Numero Azioni'];
$user_rows = [];
foreach ($user_summary as $summary) {
$user_rows[] = [
htmlspecialchars($summary['username']),
number_format($summary['count'])
];
}
$pdf->addTable($user_headers, $user_rows);
}
// Output del PDF
$filename = 'log_attivita_' . date('Y-m-d_H-i-s');
$pdf->output($filename);
// Log dell'esportazione
logActivity('export', 'Esportazione log attività in PDF', 'logs', null);