water-report.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * dashboard/crop-analysis/plant-test-data/plant-report.php
  4. *
  5. * Plant analysis report — editable sections with auto-save and Ollama AI interpretation.
  6. */
  7. require_once __DIR__ . '/../../../config/database.php';
  8. require_once __DIR__ . '/../../../lib/auth.php';
  9. require_once __DIR__ . '/../../../lib/csrf.php';
  10. requireLogin();
  11. $recordId = (int) ($_GET['rid'] ?? 0);
  12. $randId = trim( $_GET['rand'] ?? '');
  13. $clientId = (int) ($_GET['cid'] ?? 0);
  14. if (!$recordId || $randId === '') {
  15. http_response_code(400);
  16. die('Invalid request parameters');
  17. }
  18. try {
  19. $pdo = getDBConnection();
  20. $userId = getCurrentUserId();
  21. $stmt = $pdo->prepare('SELECT * FROM plant_records WHERE id = ? AND rand = ?');
  22. $stmt->execute([$recordId, $randId]);
  23. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  24. if (!$row) {
  25. http_response_code(404);
  26. die('Plant record not found');
  27. }
  28. // Load spec ranges
  29. $specs = [];
  30. if (!empty($row['crop_type'])) {
  31. $stmtSpec = $pdo->prepare('SELECT * FROM plant_specifications WHERE plant_type = ? LIMIT 1');
  32. $stmtSpec->execute([$row['crop_type']]);
  33. $specs = $stmtSpec->fetch(PDO::FETCH_ASSOC) ?: [];
  34. }
  35. // Load saved report comments
  36. $savedComments = [
  37. 'general_details' => '',
  38. 'ai_interpretation' => '',
  39. 'recommended_details' => '',
  40. 'foliar_details' => '',
  41. ];
  42. $stmtRpt = $pdo->prepare(
  43. 'SELECT comment FROM reports WHERE record_id = ? AND modx_user_id = ? ORDER BY id DESC LIMIT 1'
  44. );
  45. $stmtRpt->execute([$recordId, $userId]);
  46. $savedRow = $stmtRpt->fetchColumn();
  47. if ($savedRow) {
  48. $decoded = json_decode($savedRow, true);
  49. if (is_array($decoded)) {
  50. $savedComments = array_merge($savedComments, $decoded);
  51. }
  52. }
  53. } catch (PDOException $e) {
  54. error_log('DB error in plant-report.php: ' . $e->getMessage());
  55. die('Database error occurred');
  56. }
  57. $h = fn($v) => htmlspecialchars((string)($v ?? ''), ENT_QUOTES, 'UTF-8');
  58. $today = date('jS F Y');
  59. $pageTitle = 'Water Report' . (!empty($row['client_name']) ? ' — ' . $row['client_name'] : '');
  60. $siteName = 'Crop Monitor';
  61. include __DIR__ . '/../../../layouts/header.php';
  62. ?>
  63. <link rel="stylesheet" href="/client-assets/home/css/graphPrint.css" media="print">
  64. <style>
  65. @media print {
  66. .report-textarea {
  67. border:none;
  68. }
  69. }
  70. .report-textarea {
  71. overflow: hidden;
  72. resize: none;
  73. overflow-y: auto;
  74. }
  75. </style>