soil-analysis.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /**
  3. * dashboard/crop-analysis/soil-test-data/soil-analysis.php
  4. *
  5. * Soil Analysis Results Display Page
  6. */
  7. require_once __DIR__.'/../../../config/database.php';
  8. require_once __DIR__.'/../../../lib/auth.php';
  9. require_once __DIR__.'/../../../lib/validation.php';
  10. require_once __DIR__.'/../../../lib/soil_calculations.php';
  11. if (session_status() === PHP_SESSION_NONE) {
  12. session_start();
  13. }
  14. requireLogin();
  15. $record_id = (float)($_GET['rid'] ?? 0);
  16. $rand_id = (float)($_GET['rand'] ?? 0);
  17. $client_id = (int) ($_GET['cid'] ?? 0);
  18. if (!$record_id || !$rand_id) {
  19. die('Invalid request parameters');
  20. }
  21. try {
  22. $pdo = getDBConnection();
  23. // Load soil record
  24. $stmt = $pdo->prepare("SELECT * FROM soil_records WHERE id = ? AND rand = ?");
  25. $stmt->execute([$record_id, $rand_id]);
  26. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  27. if (!$row) {
  28. die('Soil record not found');
  29. }
  30. // Load matching specifications row for this soil type
  31. $spec = [];
  32. if (!empty($row['soil_type'])) {
  33. $stmtSpec = $pdo->prepare("SELECT * FROM soil_specifications WHERE soil_type = ? LIMIT 1");
  34. $stmtSpec->execute([$row['soil_type']]);
  35. $spec = $stmtSpec->fetch(PDO::FETCH_ASSOC) ?: [];
  36. }
  37. } catch (PDOException $e) {
  38. error_log("Database error in soil-analysis.php: " . $e->getMessage());
  39. die('Database error occurred');
  40. }
  41. // ── Page setup ─────────────────────────────────────────────────────────────
  42. $client = htmlspecialchars($row['client_name'] ?? '', ENT_QUOTES, 'UTF-8');
  43. $address = htmlspecialchars($row['site_address'] ?? '', ENT_QUOTES, 'UTF-8');
  44. $state = htmlspecialchars($row['state_postcode'] ?? '', ENT_QUOTES, 'UTF-8');
  45. $email = htmlspecialchars($row['email'] ?? '', ENT_QUOTES, 'UTF-8');
  46. $labNo = htmlspecialchars($row['lab_no'] ?? '', ENT_QUOTES, 'UTF-8');
  47. $sampleDate = htmlspecialchars($row['date_sampled'] ?? '', ENT_QUOTES, 'UTF-8');
  48. $sample = htmlspecialchars($row['site_id'] ?? '', ENT_QUOTES, 'UTF-8');
  49. $crop = htmlspecialchars($row['sample_id'] ?? '', ENT_QUOTES, 'UTF-8');
  50. $today = date('jS F Y');
  51. $pageTitle = 'Soil Analysis Results' . ($client !== '' ? ' - ' . $client : '');
  52. include __DIR__.'/../../../layouts/header.php';
  53. ?>
  54. <link rel="stylesheet" href="/client-assets/home/css/graphPrint.css" media="print">
  55. <style>
  56. .progress { border-radius: 0 !important; }
  57. </style>
  58. <div class="container" id="content">
  59. <div class="row mb-2">
  60. <div class="col-md-3">
  61. <img class="img-fluid" src="/client-assets/images/crop-monitor.png" alt="Crop Monitor">
  62. </div>
  63. </div>
  64. <table class="title">
  65. <tbody>
  66. <tr>
  67. <th class="col-20"></th><th class="col-20"></th><th class="col-20"></th>
  68. <th class="col-20"></th><th class="col-20"></th>
  69. </tr>
  70. <tr>
  71. <td class="right"><b>DATE:</b></td>
  72. <td class="left"><?= $today ?></td>
  73. <td></td>
  74. <td class="right"><b>SAMPLE ID:</b></td>
  75. <td class="left"><?= $sample ?></td>
  76. </tr>
  77. <tr>
  78. <td class="right"><b>CLIENT:</b></td>
  79. <td class="left"><?= $client ?></td>
  80. <td></td>
  81. <td class="right"><b>DATE SAMPLED:</b></td>
  82. <td class="left"><?= $sampleDate ?></td>
  83. </tr>
  84. <tr>
  85. <td class="right"><b>ADDRESS:</b></td>
  86. <td class="left"><?= $address ?></td>
  87. <td></td>
  88. <td class="right"><b>LAB NUMBER:</b></td>
  89. <td class="left"><?= $labNo ?></td>
  90. </tr>
  91. <tr>
  92. <td class="right"></td>
  93. <td class="left"><?= $state ?></td>
  94. <td></td>
  95. <td class="right"><b>CROP:</b></td>
  96. <td class="left"><?= $crop ?></td>
  97. </tr>
  98. <tr>
  99. <td class="right"></td>
  100. <td class="left"><?= $email ?></td>
  101. <td></td><td></td><td></td>
  102. </tr>
  103. </tbody>
  104. </table>
  105. <div class="d-print-none">
  106. <div class="row p-2">
  107. <div class="col">
  108. <button type="button" class="btn btn-primary" onclick="generateGraph()">
  109. <i class="fas fa-chart-bar"></i> Generate Graph
  110. </button>
  111. </div>
  112. <div class="col">
  113. <div class="form-status-holder"></div>
  114. </div>
  115. </div>
  116. </div>
  117. <div class="row">
  118. <div class="col-md-12 text-center fw-bold h4">Soil Analysis Summary</div>
  119. </div>
  120. <table class="chart">
  121. <tbody>
  122. <!-- ── CHART HEADER ──────────────────────────────────────────── -->
  123. <tr class="chart-header">
  124. <th colspan="3" class="text-center col-md-6 border-left border-right border-top">ELEMENT</th>
  125. <th colspan="3" class="text-center col-md-6 border-right border-top">STATUS</th>
  126. </tr>
  127. <tr class="chart-header-sub">
  128. <th class="text-center col-18 border-left"></th>
  129. <th class="text-center col-15">DESIRED</th>
  130. <th class="text-center col-15">FOUND</th>
  131. <th class="text-center col-16 stripe-1">LIGHT</th>
  132. <th class="text-center col-16 stripe-1">MEDIUM</th>
  133. <th class="text-center col-16 border-right stripe-1">HEAVY</th>
  134. </tr>
  135. <tr>
  136. <td class="border-left"></td>
  137. <td class="border-left"></td>
  138. <td class="border-left nutrient-balance"></td>
  139. <td class="border-left"></td>
  140. <td class="border-left"></td>
  141. <td class="border-left border-right"></td>
  142. </tr>
  143. <?php
  144. soilRow($row, $spec, ['element'=>'cec', 'nutrient'=>'CEC', 'recV'=>'n', 'decimal'=>2, 'graph'=>'lightorangeGraph']);
  145. soilRow($row, $spec, ['element'=>'tec', 'nutrient'=>'TEC', 'max'=>'soil_type', 'recV'=>'max', 'rec_text'=>'c', 'decimal'=>2, 'graph'=>'lightorangeGraph']);
  146. ?>
  147. <tr class="chart-header-sub">
  148. <th class="text-center col-18 border-left text-dark bg-white"></th>
  149. <th class="text-center col-15 border-left text-dark bg-white"></th>
  150. <th class="text-center col-15 border-left nutrient-balance"></th>
  151. <th class="text-center col-16 border-left stripe-1">DEFICIT</th>
  152. <th class="text-center col-16 stripe-1">IDEAL</th>
  153. <th class="text-center col-16 border-right stripe-1">HIGH</th>
  154. </tr>
  155. <?php
  156. soilRow($row, $spec, ['element'=>'ph_h2o', 'nutrient'=>'pH-level (H20)', 'type'=>'pH', 'recV'=>'ph', 'decimal'=>1, 'graph'=>'lightorangeGraph']);
  157. soilRow($row, $spec, ['element'=>'ph_cacl2', 'nutrient'=>'pH-level (CaCl2)', 'type'=>'pH', 'recV'=>'n', 'decimal'=>1, 'graph'=>'lightorangeGraph']);
  158. soilRow($row, $spec, ['element'=>'ec', 'nutrient'=>'Conductivity (EC)','type'=>'mS/cm', 'decimal'=>2, 'graph'=>'lightorangeGraph']);
  159. soilRow($row, $spec, ['element'=>'ocarbon', 'nutrient'=>'Organic Carbon', 'type'=>'%', 'decimal'=>1, 'graph'=>'lightorangeGraph']);
  160. soilRow($row, $spec, ['element'=>'omatter', 'nutrient'=>'Organic Matter', 'type'=>'%', 'decimal'=>1, 'graph'=>'lightorangeGraph']);
  161. ?>
  162. <!-- ── MAJOR ELEMENTS ─────────────────────────────────────────── -->
  163. <tr class="chart-header-sub">
  164. <th colspan="3" class="col-16 border-left text-center lightgreen">MAJOR ELEMENTS</th>
  165. <th class="text-center col-16 stripe-1"></th>
  166. <th class="text-center col-16 stripe-1"></th>
  167. <th class="text-center col-16 border-right stripe-1"></th>
  168. </tr>
  169. <?php
  170. soilRow($row, $spec, ['element'=>'NO3_N', 'sbl'=>'NO<sub>3</sub>-N', 'nutrient'=>'Nitrate <small class="d-print-none">Nitrogen</small>', 'min'=>'10', 'max'=>'20', 'type'=>'ppm', 'text'=>'c', 'rec_text'=>'r', 'decimal'=>0, 'graph'=>'lightgreenGraph']);
  171. soilRow($row, $spec, ['element'=>'NH3_N', 'sbl'=>'NH<sub>3</sub>-N', 'nutrient'=>'Ammonium <small class="d-print-none">Nitrogen</small>', 'type'=>'ppm', 'text'=>'c', 'rec_text'=>'r', 'decimal'=>0, 'graph'=>'lightgreenGraph']);
  172. // p_mehlick, p_bray2, p_morgan excluded (commented in original)
  173. soilRow($row, $spec, ['element'=>'p_colwell', 'sbl'=>'P', 'nutrient'=>'Phosphate <small>(colwell)</small>', 'type'=>'ppm', 'text'=>'c', 'rec_text'=>'r', 'decimal'=>0, 'graph'=>'lightgreenGraph']);
  174. soilRow($row, $spec, ['element'=>'BS_ca_ppm', 'sbl'=>'Ca', 'nutrient'=>'Calcium', 'min'=>'ca_ppm_min', 'max'=>'ca_ppm_max', 'type'=>'ppm', 'text'=>'c', 'rec_text'=>'r', 'recV'=>'max', 'decimal'=>0, 'graph'=>'lightgreenGraph']);
  175. soilRow($row, $spec, ['element'=>'BS_mg_ppm', 'sbl'=>'Mg', 'nutrient'=>'Magnesium', 'min'=>'mg_ppm_min', 'max'=>'mg_ppm_max', 'type'=>'ppm', 'text'=>'c', 'rec_text'=>'r', 'recV'=>'max', 'decimal'=>0, 'graph'=>'lightgreenGraph']);
  176. soilRow($row, $spec, ['element'=>'BS_k_ppm', 'sbl'=>'K', 'nutrient'=>'Potassium', 'min'=>'k_ppm_min', 'max'=>'k_ppm_max', 'type'=>'ppm', 'text'=>'c', 'rec_text'=>'r', 'recV'=>'max', 'decimal'=>0, 'graph'=>'lightgreenGraph']);
  177. soilRow($row, $spec, ['element'=>'BS_na_ppm', 'sbl'=>'Na', 'nutrient'=>'Sodium', 'min'=>'na_ppm_min', 'max'=>'na_ppm_max', 'type'=>'ppm', 'text'=>'c', 'rec_text'=>'r', 'recV'=>'max', 'decimal'=>0, 'graph'=>'lightgreenGraph']);
  178. ?>
  179. <!-- ── TRACE ELEMENTS ─────────────────────────────────────────── -->
  180. <tr class="chart-header-sub">
  181. <th colspan="3" class="col-16 border-left text-center lightred">TRACE ELEMENTS</th>
  182. <th class="text-center col-16 stripe-1"></th>
  183. <th class="text-center col-16 stripe-1"></th>
  184. <th class="text-center col-16 border-right stripe-1"></th>
  185. </tr>
  186. <?php
  187. soilRow($row, $spec, ['element'=>'s_morgan', 'sbl'=>'S', 'nutrient'=>'Sulfur', 'type'=>'ppm', 'text'=>'c', 'rec_text'=>'r', 'decimal'=>2, 'graph'=>'lightredGraph']);
  188. soilRow($row, $spec, ['element'=>'b_cacl2', 'sbl'=>'B', 'nutrient'=>'Boron', 'type'=>'ppm', 'text'=>'c', 'rec_text'=>'r', 'decimal'=>2, 'graph'=>'lightredGraph']);
  189. soilRow($row, $spec, ['element'=>'mn_dtpa', 'sbl'=>'Mn', 'nutrient'=>'Manganese', 'type'=>'ppm', 'text'=>'c', 'rec_text'=>'r', 'decimal'=>2, 'graph'=>'lightredGraph']);
  190. soilRow($row, $spec, ['element'=>'cu_dtpa', 'sbl'=>'Cu', 'nutrient'=>'Copper', 'type'=>'ppm', 'text'=>'c', 'rec_text'=>'r', 'decimal'=>2, 'graph'=>'lightredGraph']);
  191. soilRow($row, $spec, ['element'=>'zn_dtpa', 'sbl'=>'Zn', 'nutrient'=>'Zinc', 'type'=>'ppm', 'text'=>'c', 'rec_text'=>'r', 'decimal'=>2, 'graph'=>'lightredGraph']);
  192. soilRow($row, $spec, ['element'=>'fe_dtpa', 'sbl'=>'Fe', 'nutrient'=>'Iron', 'type'=>'ppm', 'text'=>'c', 'rec_text'=>'r', 'decimal'=>2, 'graph'=>'lightredGraph']);
  193. soilRow($row, $spec, ['element'=>'al', 'sbl'=>'Al', 'nutrient'=>'Aluminium', 'type'=>'ppm', 'text'=>'c', 'rec_text'=>'r', 'decimal'=>2, 'graph'=>'lightredGraph']);
  194. soilRow($row, $spec, ['element'=>'sl_cacl2', 'sbl'=>'Si', 'nutrient'=>'Silicon', 'type'=>'ppm', 'text'=>'c', 'rec_text'=>'r', 'decimal'=>2, 'graph'=>'lightredGraph']);
  195. ?>
  196. <!-- ── BASE SATURATION ────────────────────────────────────────── -->
  197. <tr class="chart-header-sub">
  198. <th colspan="3" class="col-16 border-left text-center lightpurple">BASE SATURATION</th>
  199. <th class="text-center col-16 stripe-1"></th>
  200. <th class="text-center col-16 stripe-1"></th>
  201. <th class="text-center col-16 border-right stripe-1"></th>
  202. </tr>
  203. <?php
  204. soilRow($row, $spec, ['element'=>'BS_ca2', 'sbl'=>'Ca', 'nutrient'=>'Calcium', 'min'=>'cabs_min', 'max'=>'cabs_max', 'type'=>'%', 'text'=>'c', 'rec_text'=>'r', 'recV'=>'max', 'decimal'=>2, 'graph'=>'lightpurpleGraph']);
  205. soilRow($row, $spec, ['element'=>'BS_mg2', 'sbl'=>'Mg', 'nutrient'=>'Magnesium', 'min'=>'mgbs_min', 'max'=>'mgbs_max', 'type'=>'%', 'text'=>'c', 'rec_text'=>'r', 'recV'=>'max', 'decimal'=>2, 'graph'=>'lightpurpleGraph']);
  206. soilRow($row, $spec, ['element'=>'BS_k', 'sbl'=>'K', 'nutrient'=>'Potassium', 'min'=>'kbs_min', 'max'=>'kbs_max', 'type'=>'%', 'text'=>'c', 'rec_text'=>'r', 'recV'=>'max', 'decimal'=>2, 'graph'=>'lightpurpleGraph']);
  207. soilRow($row, $spec, ['element'=>'BS_na', 'sbl'=>'Na', 'nutrient'=>'Sodium', 'min'=>'nabs_min', 'max'=>'nabs_max', 'type'=>'%', 'text'=>'c', 'rec_text'=>'r', 'recV'=>'max', 'decimal'=>2, 'graph'=>'lightpurpleGraph']);
  208. soilRow($row, $spec, ['element'=>'BS_ob', 'nutrient'=>'Other Bases', 'max'=>'ob_rec', 'type'=>'%', 'text'=>'c', 'rec_text'=>'r', 'recV'=>'max', 'decimal'=>2, 'graph'=>'lightpurpleGraph']);
  209. soilRow($row, $spec, ['element'=>'BS_h', 'nutrient'=>'Hydrogen', 'max'=>'h_rec', 'type'=>'%', 'text'=>'c', 'rec_text'=>'r', 'recV'=>'max', 'decimal'=>2, 'graph'=>'lightpurpleGraph']);
  210. ?>
  211. <!-- ── SOLUBLE MORGAN 2 EXTRACT ───────────────────────────────── -->
  212. <tr class="chart-header-sub">
  213. <th colspan="3" class="col-16 border-left text-center lightgrey">SOLUBLE MORGAN 2 EXTRACT</th>
  214. <th class="text-center col-16 stripe-1"></th>
  215. <th class="text-center col-16 stripe-1"></th>
  216. <th class="text-center col-16 border-right stripe-1"></th>
  217. </tr>
  218. <?php
  219. soilRow($row, $spec, ['element'=>'s_morgan', 'sbl'=>'Ca', 'nutrient'=>'Calcium', 'type'=>'%', 'text'=>'c', 'rec_text'=>'r', 'decimal'=>0, 'graph'=>'lightgreyGraph']);
  220. soilRow($row, $spec, ['element'=>'b_cacl2', 'sbl'=>'Mg', 'nutrient'=>'Magnesium', 'type'=>'%', 'text'=>'c', 'rec_text'=>'r', 'decimal'=>0, 'graph'=>'lightgreyGraph']);
  221. soilRow($row, $spec, ['element'=>'mn_dtpa', 'sbl'=>'K', 'nutrient'=>'Potassium', 'type'=>'%', 'text'=>'c', 'rec_text'=>'r', 'decimal'=>0, 'graph'=>'lightgreyGraph']);
  222. ?>
  223. <!-- ── ADDITIONAL DATA ────────────────────────────────────────── -->
  224. <tr class="chart-header-sub">
  225. <th colspan="3" class="col-16 border-left text-center lightgrey">ADDITIONAL DATA</th>
  226. <th class="text-center col-16 stripe-1">LOW</th>
  227. <th class="text-center col-16 stripe-1">IDEAL</th>
  228. <th class="text-center col-16 border-right stripe-1">EXCELLENT</th>
  229. </tr>
  230. <?php
  231. soilRow($row, $spec, ['element'=>'s_morgan', 'sbl'=>'Ca', 'nutrient'=>'Calcium', 'type'=>'%', 'text'=>'c', 'rec_text'=>'r', 'decimal'=>0, 'graph'=>'lightgreyGraph']);
  232. ?>
  233. <!-- ── RATIOS ─────────────────────────────────────────────────── -->
  234. <tr class="chart-header-sub">
  235. <th colspan="3" class="col-16 border-left text-center lightblue">RATIOS</th>
  236. <th class="text-center col-16 stripe-1"></th>
  237. <th class="text-center col-16 stripe-1"></th>
  238. <th class="text-center col-16 border-right stripe-1"></th>
  239. </tr>
  240. <?php
  241. soilRatio($row, $spec, ['element'=>'ca_mehlick3', 'elementTwo'=>'mg_mehlick3', 'rec'=>'ca_mg_ratio', 'nutrient'=>'Ca:Mg Ratio', 'rec_text'=>'r', 'decimal'=>1, 'graph'=>'lightblueGraph']);
  242. soilRow ($row, $spec, ['element'=>'NH3_N', 'nutrient'=>'Total Nitrogen', 'type'=>'%', 'text'=>'c', 'rec_text'=>'r', 'decimal'=>1, 'graph'=>'lightblueGraph']);
  243. soilRow ($row, $spec, ['element'=>'ocarbon', 'nutrient'=>'Total Carbon', 'type'=>'%', 'text'=>'c', 'rec_text'=>'r', 'decimal'=>1, 'graph'=>'lightblueGraph']);
  244. soilRatio($row, $spec, ['element'=>'ocarbon', 'elementTwo'=>'NO3_N', 'rec'=>'c_n_ratio', 'nutrient'=>'C:N Ratio', 'type'=>':1', 'rec_text'=>'r', 'decimal'=>1, 'graph'=>'lightblueGraph']);
  245. ?>
  246. <tr>
  247. <td class="border-bottom border-left"></td>
  248. <td class="border-bottom border-left"></td>
  249. <td class="border-bottom border-left nutrient-balance"></td>
  250. <td class="border-bottom border-left"></td>
  251. <td class="border-bottom border-left"></td>
  252. <td class="border-bottom border-left border-right"></td>
  253. </tr>
  254. </tbody>
  255. </table>
  256. <footer class="py-4 bg-light mt-auto">
  257. <div class="container-fluid px-4">
  258. <div class="d-flex align-items-center justify-content-between small">
  259. <div class="text-muted">&copy; <?= date('Y') ?> Crop Management Platform. All Rights Reserved.</div>
  260. <div>
  261. <a href="/privacy-policy">Privacy Policy</a> &middot;
  262. <a href="/terms">Terms &amp; Conditions</a>
  263. </div>
  264. </div>
  265. </div>
  266. </footer>
  267. </div><!-- /.container -->
  268. <script>
  269. function generateGraph() {
  270. alert('Graph generation functionality will be implemented here.');
  271. }
  272. $(document).ready(function () {
  273. var timeoutId;
  274. $('form textarea, form input').on('input propertychange change', function () {
  275. clearTimeout(timeoutId);
  276. timeoutId = setTimeout(saveToDB, 1000);
  277. });
  278. function saveToDB() {
  279. var form = $('.report-form');
  280. $.ajax({
  281. url: '/controllers/save_soil_analysis.php',
  282. type: 'POST',
  283. data: form.serialize(),
  284. beforeSend: function () { $('.form-status-holder').html('Saving...'); },
  285. success: function () {
  286. var d = new Date();
  287. $('.form-status-holder').html('Saved! Last: ' + d.toLocaleTimeString());
  288. }
  289. });
  290. }
  291. $('.report-form').submit(function (e) {
  292. saveToDB();
  293. e.preventDefault();
  294. });
  295. });
  296. </script>