197 lines
7.6 KiB
PHP
197 lines
7.6 KiB
PHP
<?php
|
|
require_once 'auth.php';
|
|
require_login();
|
|
require_once '../db/config.php';
|
|
$pdo = db();
|
|
require_once '../includes/fpdf/fpdf.php';
|
|
require_once '../mail/MailService.php';
|
|
require_once 'i18n.php';
|
|
|
|
// Get case ID from URL
|
|
if (!isset($_GET['case_id']) || !is_numeric($_GET['case_id'])) {
|
|
die('Invalid Case ID');
|
|
}
|
|
$case_id = intval($_GET['case_id']);
|
|
|
|
// Fetch case details
|
|
$stmt = $pdo->prepare('SELECT c.*, cat.name_en as category_name FROM cases c JOIN categories cat ON c.category_id = cat.id WHERE c.id = ?');
|
|
$stmt->execute([$case_id]);
|
|
$case = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$case) {
|
|
die('Case not found');
|
|
}
|
|
|
|
// Fetch data for PDF
|
|
$donations_stmt = $pdo->prepare('SELECT * FROM donations WHERE case_id = ? ORDER BY created_at DESC');
|
|
$donations_stmt->execute([$case_id]);
|
|
$donations = $donations_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$logs_stmt = $pdo->prepare('SELECT al.*, u.email FROM audit_logs al LEFT JOIN users u ON al.user_id = u.id WHERE al.case_id = ? ORDER BY al.created_at DESC');
|
|
$logs_stmt->execute([$case_id]);
|
|
$audit_logs = $logs_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// PDF Generation Class
|
|
class PDF extends FPDF
|
|
{
|
|
function Header() {
|
|
if (file_exists('../assets/images/logo_1770967720.jpg')) {
|
|
$this->Image('../assets/images/logo_1770967720.jpg', 10, 6, 30);
|
|
}
|
|
$this->SetFont('Arial', 'B', 15);
|
|
$this->Cell(80);
|
|
$this->Cell(30, 10, 'Case Report', 0, 0, 'C');
|
|
$this->Ln(20);
|
|
}
|
|
function Footer() {
|
|
$this->SetY(-15);
|
|
$this->SetFont('Arial', 'I', 8);
|
|
$this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
|
|
}
|
|
}
|
|
|
|
function generate_pdf($case, $donations, $audit_logs)
|
|
{
|
|
$pdf = new PDF();
|
|
$pdf->AliasNbPages();
|
|
$pdf->AddPage();
|
|
$pdf->SetFont('Times', '', 12);
|
|
|
|
// Case Details
|
|
$pdf->SetFont('Arial', 'B', 16);
|
|
$pdf->Cell(0, 10, 'Case Details', 0, 1, 'L');
|
|
$pdf->SetFont('Arial', '', 12);
|
|
$pdf->Cell(40, 10, 'Case ID:', 0, 0); $pdf->Cell(0, 10, $case['id'], 0, 1);
|
|
$pdf->Cell(40, 10, 'Title:', 0, 0); $pdf->Cell(0, 10, $case['title_en'], 0, 1);
|
|
$pdf->Cell(40, 10, 'Category:', 0, 0); $pdf->Cell(0, 10, $case['category_name'], 0, 1);
|
|
$pdf->Cell(40, 10, 'Goal Amount:', 0, 0); $pdf->Cell(0, 10, 'OMR ' . number_format($case['goal'], 3), 0, 1);
|
|
$pdf->Cell(40, 10, 'Raised Amount:', 0, 0); $pdf->Cell(0, 10, 'OMR ' . number_format($case['raised'], 3), 0, 1);
|
|
$pdf->Cell(40, 10, 'Status:', 0, 0); $pdf->Cell(0, 10, ucfirst($case['status']), 0, 1);
|
|
$pdf->Ln(10);
|
|
|
|
// Donations
|
|
$pdf->SetFont('Arial', 'B', 16);
|
|
$pdf->Cell(0, 10, 'Donations', 0, 1, 'L');
|
|
$pdf->SetFont('Arial', 'B', 10);
|
|
$pdf->Cell(25, 7, 'ID', 1, 0, 'C'); $pdf->Cell(50, 7, 'Donor', 1, 0, 'C'); $pdf->Cell(35, 7, 'Amount', 1, 0, 'C'); $pdf->Cell(40, 7, 'Date', 1, 0, 'C'); $pdf->Cell(30, 7, 'Status', 1, 1, 'C');
|
|
$pdf->SetFont('Arial', '', 10);
|
|
if (empty($donations)) {
|
|
$pdf->Cell(180, 10, 'No donations for this case.', 1, 1, 'C');
|
|
} else {
|
|
foreach ($donations as $donation) {
|
|
$pdf->Cell(25, 7, $donation['id'], 1, 0, 'C');
|
|
$pdf->Cell(50, 7, htmlspecialchars($donation['donor_name']), 1, 0, 'L');
|
|
$pdf->Cell(35, 7, 'OMR ' . number_format($donation['amount'], 3), 1, 0, 'R');
|
|
$pdf->Cell(40, 7, $donation['created_at'], 1, 0, 'C');
|
|
$pdf->Cell(30, 7, ucfirst($donation['status']), 1, 1, 'C');
|
|
}
|
|
}
|
|
$pdf->Ln(10);
|
|
|
|
// History
|
|
$pdf->SetFont('Arial', 'B', 16);
|
|
$pdf->Cell(0, 10, 'Case History', 0, 1, 'L');
|
|
$pdf->SetFont('Arial', 'B', 10);
|
|
$pdf->Cell(20, 7, 'Log ID', 1, 0, 'C'); $pdf->Cell(30, 7, 'User', 1, 0, 'C'); $pdf->Cell(80, 7, 'Action', 1, 0, 'C'); $pdf->Cell(50, 7, 'Timestamp', 1, 1, 'C');
|
|
$pdf->SetFont('Arial', '', 9);
|
|
if (empty($audit_logs)) {
|
|
$pdf->Cell(180, 10, 'No history for this case.', 1, 1, 'C');
|
|
} else {
|
|
foreach ($audit_logs as $log) {
|
|
$pdf->Cell(20, 7, $log['id'], 1, 0, 'C');
|
|
$pdf->Cell(30, 7, htmlspecialchars($log['email']), 1, 0, 'C');
|
|
$pdf->Cell(80, 7, htmlspecialchars($log['action']), 1, 0, 'L');
|
|
$pdf->Cell(50, 7, $log['created_at'], 1, 1, 'C');
|
|
}
|
|
}
|
|
|
|
return $pdf;
|
|
}
|
|
|
|
// Handle Email Sending
|
|
$email_msg = '';
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['send_email'])) {
|
|
$recipient = filter_var($_POST['recipient_email'], FILTER_VALIDATE_EMAIL);
|
|
$message = htmlspecialchars($_POST['message']);
|
|
|
|
if ($recipient) {
|
|
// Generate and save PDF
|
|
$pdf = generate_pdf($case, $donations, $audit_logs);
|
|
$tmp_dir = '../tmp';
|
|
if (!is_dir($tmp_dir)) mkdir($tmp_dir, 0775, true);
|
|
$filename = "Case_Report_" . $case_id . "_" . time() . ".pdf";
|
|
$filepath = $tmp_dir . '/' . $filename;
|
|
$pdf->Output('F', $filepath);
|
|
|
|
// Send email
|
|
$subject = "Case Report: " . $case['title_en'];
|
|
$res = MailService::sendMail($recipient, $subject, $message, null, [], [$filepath]);
|
|
|
|
// Clean up and set message
|
|
unlink($filepath);
|
|
if ($res['success']) {
|
|
$email_msg = '<div class="alert alert-success">Email sent successfully!</div>';
|
|
} else {
|
|
$email_msg = '<div class="alert alert-danger">Failed to send email. Error: ' . ($res['error'] ?? 'Unknown') . '</div>';
|
|
}
|
|
} else {
|
|
$email_msg = '<div class="alert alert-danger">Invalid recipient email address.</div>';
|
|
}
|
|
}
|
|
|
|
// Handle PDF view
|
|
if (isset($_GET['view']) && $_GET['view'] === 'pdf') {
|
|
$pdf = generate_pdf($case, $donations, $audit_logs);
|
|
$pdf->Output('I', 'Case_Report_' . $case_id . '.pdf');
|
|
exit;
|
|
}
|
|
|
|
$is_rtl = (get_current_lang() === 'ar');
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="<?= get_current_lang() ?>" dir="<?= $is_rtl ? 'rtl' : 'ltr' ?>">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Case Report - <?= htmlspecialchars($case['title_en']) ?></title>
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
|
|
<link rel="stylesheet" href="admin.css">
|
|
</head>
|
|
<body>
|
|
<?php include 'sidebar.php'; ?>
|
|
<div class="main-content">
|
|
<h2 class="mb-4">Case Report: <?= htmlspecialchars($case['title_en']) ?></h2>
|
|
|
|
<?= $email_msg ?>
|
|
|
|
<div class="card">
|
|
<div class="card-header d-flex justify-content-between align-items-center">
|
|
<h5 class="mb-0">Actions</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<a href="?case_id=<?= $case_id ?>&view=pdf" target="_blank" class="btn btn-primary"><i class="bi bi-printer"></i> Download PDF</a>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card mt-4">
|
|
<div class="card-header">
|
|
<h5 class="mb-0">Email Report</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<form method="POST">
|
|
<div class="mb-3">
|
|
<label for="recipient_email" class="form-label">Recipient Email</label>
|
|
<input type="email" class="form-control" id="recipient_email" name="recipient_email" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="message" class="form-label">Message (optional)</label>
|
|
<textarea class="form-control" id="message" name="message" rows="3"></textarea>
|
|
</div>
|
|
<button type="submit" name="send_email" class="btn btn-success"><i class="bi bi-envelope"></i> Send Email</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|