| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- /**
- * water-report-pdf.php
- *
- * Print / PDF-export version of a completed water analysis report.
- * Normal access: ?rid=<id>&rand=<token>
- * Headless Chrome: ?rid=<id>&rand=<token>&ptoken=<one-time-token>
- */
- require_once __DIR__ . '/../../../config/database.php';
- require_once __DIR__ . '/../../../lib/auth.php';
- require_once __DIR__ . '/../../../lib/print_auth.php';
- require_once __DIR__ . '/../../../vendor/autoload.php';
- if (session_status() === PHP_SESSION_NONE) {
- session_start();
- }
- $recordId = (int) ($_GET['rid'] ?? 0);
- $randId = trim( $_GET['rand'] ?? '');
- $clientId = (int) ($_GET['cid'] ?? 0);
- $printMode = isset($_GET['ptoken']) || isset($_GET['print']);
- if (!$recordId || $randId === '') {
- http_response_code(400);
- die('Invalid request parameters');
- }
- $chromeAccess = authenticatePrintPage($recordId, $randId);
- try {
- $pdo = getDBConnection();
- $userId = $chromeAccess ? null : getCurrentUserId();
- $stmt = $pdo->prepare('SELECT * FROM water_records WHERE id = ? AND rand = ?');
- $stmt->execute([$recordId, $randId]);
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if (!$row) {
- http_response_code(404);
- die('Water record not found');
- }
- // Load saved report comments
- $savedComments = [
- 'general_details' => '',
- 'ai_interpretation' => '',
- 'recommended_details' => '',
- 'foliar_details' => '',
- ];
- $reportUserId = $userId ?? (int)($row['modx_user_id'] ?? 0);
- $stmtRpt = $pdo->prepare(
- 'SELECT comment FROM reports WHERE record_id = ? AND modx_user_id = ? ORDER BY id DESC LIMIT 1'
- );
- $stmtRpt->execute([$recordId, $reportUserId]);
- $savedRow = $stmtRpt->fetchColumn();
- if ($savedRow) {
- $decoded = json_decode($savedRow, true);
- if (is_array($decoded)) {
- $savedComments = array_merge($savedComments, $decoded);
- }
- }
- } catch (PDOException $e) {
- error_log('DB error in water-report-pdf.php: ' . $e->getMessage());
- die('Database error occurred');
- }
- $h = fn($v) => htmlspecialchars((string)($v ?? ''), ENT_QUOTES, 'UTF-8');
- $today = date('jS F Y');
- function formatReportText(string $text): string
- {
- if (trim($text) === '') {
- return '<p class="text-muted fst-italic">No content saved.</p>';
- }
- $parsedown = new Parsedown();
- $parsedown->setSafeMode(true);
- return $parsedown->text($text);
- }
- ?>
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>Water Analysis Report | Crop Monitor</title>
- <link rel="icon" href="/favicon.ico?v=2" type="image/x-icon">
- <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
- <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/all.css" rel="stylesheet" crossorigin="anonymous">
- <link href="/client-assets/css/dashboard.css" rel="stylesheet">
- <style>
- @media print {
- .d-print-none { display: none !important; }
- body { font-size: 11px; }
- }
- .report-section p { margin-bottom: 0.6rem; line-height: 1.6; }
- .report-section h1, .report-section h2,
- .report-section h3, .report-section h4 { font-size: 1rem; font-weight: 600; margin: 1rem 0 0.4rem; }
- .report-section ul, .report-section ol { padding-left: 1.4rem; margin-bottom: 0.6rem; }
- .report-section li { margin-bottom: 0.25rem; line-height: 1.5; }
- .section-header {
- background: #212529; color: #fff;
- padding: 6px 12px; font-weight: 600; margin-bottom: 0;
- }
- .section-body {
- border: 1px solid #dee2e6; border-top: 0;
- padding: 14px 16px; margin-bottom: 1.2rem;
- }
- .title-table td, .title-table th { padding: 2px 8px; }
- </style>
- </head>
- <body>
- <div class="container page" id="pdf-content">
- <?php if (!$row): ?>
- <div class="alert alert-danger mt-4">Record not found or access denied.</div>
- <?php else: ?>
- <!-- ── Header ──────────────────────────────────────────────────────────── -->
- <div class="row align-items-center mb-3 mt-3">
- <div class="col-3">
- <img class="img-fluid" src="/client-assets/images/crop-monitor.png"
- alt="Crop Monitor" style="max-height:55px;">
- </div>
- <div class="col-9 text-end">
- <div class="fw-bold h5 mb-0">Water Analysis Report</div>
- <div class="text-muted small"><?= $h($today) ?></div>
- </div>
- </div>
- <table class="title-table w-100 mb-3 small">
- <tbody>
- <tr>
- <td class="text-end fw-bold text-nowrap">CLIENT:</td>
- <td><?= $h($row['client_name']) ?></td>
- <td></td>
- <td class="text-end fw-bold text-nowrap">SAMPLE ID:</td>
- <td><?= $h($row['sample_id']) ?></td>
- </tr>
- <tr>
- <td class="text-end fw-bold text-nowrap">SITE ID:</td>
- <td><?= $h($row['site_id']) ?></td>
- <td></td>
- <td class="text-end fw-bold text-nowrap">DATE SAMPLED:</td>
- <td><?= $h($row['date_sampled']) ?></td>
- </tr>
- <tr>
- <td class="text-end fw-bold text-nowrap">ANALYSIS TYPE:</td>
- <td><?= $h($row['analysis_type']) ?></td>
- <td></td>
- <td class="text-end fw-bold text-nowrap">LAB NUMBER:</td>
- <td><?= $h($row['lab_no']) ?></td>
- </tr>
- </tbody>
- </table>
- <!-- ── Back / Download buttons ─────────────────────────────────────────── -->
- <div class="d-print-none mb-3 d-flex gap-2">
- <a href="/dashboard/crop-analysis/water-test-data/water-report.php?rid=<?= $recordId ?>&rand=<?= urlencode($randId) ?>"
- class="btn btn-outline-secondary btn-sm">
- ← Back to Report
- </a>
- <a href="/pdf-files/headlessChrome_pdf.php?type=water-report&rid=<?= $recordId ?>&rand=<?= urlencode($randId) ?>"
- class="btn btn-success btn-sm">
- <i class="fas fa-download me-1"></i>Download PDF
- </a>
- <button class="btn btn-outline-dark btn-sm" onclick="window.print()">
- <i class="fas fa-print me-1"></i>Print
- </button>
- </div>
- <hr>
- <div class="section-header">General Comment</div>
- <div class="section-body report-section">
- <?= formatReportText($savedComments['general_details'] ?? '') ?>
- </div>
- <div class="section-header">AI Water Interpretation</div>
- <div class="section-body report-section">
- <?= formatReportText($savedComments['ai_interpretation'] ?? '') ?>
- </div>
- <div class="section-header">Recommended Remedial Program</div>
- <div class="section-body report-section">
- <?= formatReportText($savedComments['recommended_details'] ?? '') ?>
- </div>
- <div class="section-header">Application Program</div>
- <div class="section-body report-section">
- <?= formatReportText($savedComments['foliar_details'] ?? '') ?>
- </div>
- <div class="mt-3 pt-3 border-top">
- <p class="text-muted fst-italic" style="font-size:0.7rem;">
- Any recommendations provided by Crop Monitor are advice only. We are not paid consultants
- and accept no responsibility for any of our suggestions.
- </p>
- </div>
- <?php endif; ?>
- </div><!-- /container -->
- <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
- </body>
- </html>
|