| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- /**
- * dashboard/crop-analysis/plant-test-data/plant-report.php
- *
- * Plant analysis report — editable sections with auto-save and Ollama AI interpretation.
- */
- require_once __DIR__ . '/../../../config/database.php';
- require_once __DIR__ . '/../../../lib/auth.php';
- require_once __DIR__ . '/../../../lib/csrf.php';
- requireLogin();
- $recordId = (int) ($_GET['rid'] ?? 0);
- $randId = trim( $_GET['rand'] ?? '');
- $clientId = (int) ($_GET['cid'] ?? 0);
- if (!$recordId || $randId === '') {
- http_response_code(400);
- die('Invalid request parameters');
- }
- try {
- $pdo = getDBConnection();
- $userId = getCurrentUserId();
- $stmt = $pdo->prepare('SELECT * FROM plant_records WHERE id = ? AND rand = ?');
- $stmt->execute([$recordId, $randId]);
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if (!$row) {
- http_response_code(404);
- die('Plant record not found');
- }
- // Load spec ranges
- $specs = [];
- if (!empty($row['crop_type'])) {
- $stmtSpec = $pdo->prepare('SELECT * FROM plant_specifications WHERE plant_type = ? LIMIT 1');
- $stmtSpec->execute([$row['crop_type']]);
- $specs = $stmtSpec->fetch(PDO::FETCH_ASSOC) ?: [];
- }
- // Load saved report comments
- $savedComments = [
- 'general_details' => '',
- 'ai_interpretation' => '',
- 'recommended_details' => '',
- 'foliar_details' => '',
- ];
- $stmtRpt = $pdo->prepare(
- 'SELECT comment FROM reports WHERE record_id = ? AND modx_user_id = ? ORDER BY id DESC LIMIT 1'
- );
- $stmtRpt->execute([$recordId, $userId]);
- $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 plant-report.php: ' . $e->getMessage());
- die('Database error occurred');
- }
- $h = fn($v) => htmlspecialchars((string)($v ?? ''), ENT_QUOTES, 'UTF-8');
- $today = date('jS F Y');
- $pageTitle = 'Compost Report' . (!empty($row['client_name']) ? ' — ' . $row['client_name'] : '');
- $siteName = 'Crop Monitor';
- include __DIR__ . '/../../../layouts/header.php';
- ?>
- <link rel="stylesheet" href="/client-assets/home/css/graphPrint.css" media="print">
- <style>
- @media print {
- .report-textarea {
- border:none;
- }
- }
- .report-textarea {
- overflow: hidden;
- resize: none;
- overflow-y: auto;
- }
- </style>
|