soilTestSubmit.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. error_reporting(E_ALL);
  3. //error_reporting(E_ALL ^ E_NOTICE);
  4. ini_set('display_errors', 1);
  5. /**
  6. * controllers/soilTestSubmit.php
  7. *
  8. * Secure soil test form submission handler.
  9. * Replaces modX [[!soilformSubmit]] snippet.
  10. */
  11. // Start session if not already started
  12. if (session_status() === PHP_SESSION_NONE) {
  13. session_start();
  14. }
  15. // Include configuration and dependencies
  16. require_once __DIR__ . '/../config/database.php';
  17. require_once __DIR__ . '/../lib/auth.php';
  18. require_once __DIR__ . '/../lib/validation.php';
  19. require_once __DIR__ . '/../lib/csrf.php';
  20. // Check authentication
  21. if (!isLoggedIn()) {
  22. http_response_code(403);
  23. die('Access denied: User not authenticated');
  24. }
  25. // Check CSRF token
  26. if (!verifyCsrfToken($_POST['csrf_token'] ?? '')) {
  27. http_response_code(403);
  28. die('CSRF token validation failed');
  29. }
  30. // Only process POST requests
  31. if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_POST['SoilcsvForm'])) {
  32. http_response_code(405);
  33. die('Method not allowed');
  34. }
  35. try {
  36. // Validate and sanitize input data
  37. $input = validateSoilTestData($_POST);
  38. // Perform soil analysis calculations
  39. $calculations = calculateSoilAnalysis($input);
  40. // Generate unique identifier for this record
  41. $rand = mt_rand(10000, 99999);
  42. // Insert data into database
  43. $recordId = insertSoilRecord($input, $calculations, $rand);
  44. // Log successful submission
  45. error_log("Soil test record created: ID {$recordId}, User: {$_SESSION['user_id']}");
  46. // Redirect to results page
  47. $redirectUrl = "/dashboard/crop-analysis/soil-analysis.php?rand={$rand}&cid={$input['sample_id']}&rid={$recordId}&stid=" . urlencode($input['crop_type']);
  48. header("Location: {$redirectUrl}");
  49. exit;
  50. } catch (ValidationException $e) {
  51. http_response_code(400);
  52. die('Validation error: ' . htmlspecialchars($e->getMessage()));
  53. } catch (PDOException $e) {
  54. error_log("Database error in soil test submission: " . $e->getMessage());
  55. http_response_code(500);
  56. die('Database error occurred. Please try again later.');
  57. } catch (Exception $e) {
  58. error_log("Unexpected error in soil test submission: " . $e->getMessage());
  59. http_response_code(500);
  60. die('An unexpected error occurred. Please try again later.');
  61. }
  62. /**
  63. * Validate and sanitize soil test form data
  64. */
  65. function validateSoilTestData(array $post): array
  66. {
  67. $validated = [];
  68. // Client information
  69. $rawClientId = $post['client_id'] ?? '';
  70. if ($rawClientId === '' || $rawClientId === 'new') {
  71. $validated['client_id'] = null;
  72. } else {
  73. $validated['client_id'] = filter_var($rawClientId, FILTER_VALIDATE_INT);
  74. if ($validated['client_id'] === false) {
  75. throw new ValidationException('Invalid client ID');
  76. }
  77. }
  78. $validated['name'] = sanitizeString($post['name'] ?? '', 100);
  79. $validated['company'] = sanitizeString($post['company'] ?? '', 100);
  80. $validated['email'] = filter_var($post['email'] ?? '', FILTER_VALIDATE_EMAIL);
  81. if ($validated['email'] === false) {
  82. throw new ValidationException('Invalid email address');
  83. }
  84. $validated['site_address'] = sanitizeString($post['site_address'] ?? '', 255);
  85. $validated['state_postcode'] = sanitizeString($post['state_postcode'] ?? '', 100);
  86. // Analysis details
  87. $validated['lab_no'] = sanitizeString($post['lab_no'] ?? '', 50);
  88. $validated['batch_no'] = sanitizeString($post['batch_no'] ?? '', 50);
  89. $validated['sample_id'] = sanitizeString($post['sample_id'] ?? '', 50);
  90. $validated['site_id'] = sanitizeString($post['site_id'] ?? '', 50);
  91. $validated['crop_type'] = sanitizeString($post['crop_type'] ?? '', 50);
  92. $validated['soil_type'] = sanitizeString($post['soil_type'] ?? '', 50);
  93. $validated['date_sampled'] = $post['date_sampled'] ?? '';
  94. if (!empty($validated['date_sampled'])) {
  95. $date = DateTime::createFromFormat('Y-m-d', $validated['date_sampled']);
  96. if (!$date) {
  97. throw new ValidationException('Invalid date sampled format');
  98. }
  99. $validated['date_sampled'] = $date->format('Y-m-d');
  100. }
  101. // Physical properties
  102. $validated['texture'] = sanitizeString($post['texture'] ?? '', 50);
  103. $validated['gravel'] = validateNumeric($post['gravel'] ?? '', 0, 100);
  104. $validated['colour'] = sanitizeString($post['colour'] ?? '', 50);
  105. $validated['ocarbon'] = validateNumeric($post['ocarbon'] ?? '', 0, 100);
  106. $validated['omatter'] = validateNumeric($post['omatter'] ?? '', 0, 100);
  107. // Chemical properties
  108. $validated['ph_cacl2'] = validateNumeric($post['ph_cacl2'] ?? '', 0, 14);
  109. $validated['ph_h2o'] = validateNumeric($post['ph_h2o'] ?? '', 0, 14);
  110. $validated['paramag'] = validateNumeric($post['paramag'] ?? '');
  111. $validated['ec'] = validateNumeric($post['ec'] ?? '', 0);
  112. // Nutrient analysis
  113. $nutrientFields = [
  114. 'NO3_N', 'NH3_N', 'p_mehlick', 'p_bray2', 'p_morgan', 'k_morgan',
  115. 'ca_morgan', 'mg_morgan', 'na_morgan', 'ch_h2o', 'fe', 's_morgan',
  116. 'b_cacl2', 'mn_dtpa', 'zn_dtpa', 'fe_dtpa', 'cu_dtpa', 'al',
  117. 'sl_cacl2', 'm_dtpa', 'co_dtpa', 'se'
  118. ];
  119. foreach ($nutrientFields as $field) {
  120. $validated[$field] = validateNumeric($post[$field] ?? '', 0);
  121. }
  122. // Base saturation
  123. $validated['tec'] = validateNumeric($post['tec'] ?? '');
  124. $validated['cec'] = validateNumeric($post['cec'] ?? '');
  125. $validated['ca_mehlick3'] = validateNumeric($post['ca_mehlick3'] ?? '');
  126. $validated['mg_mehlick3'] = validateNumeric($post['mg_mehlick3'] ?? '');
  127. $validated['k_mehlick3'] = validateNumeric($post['k_mehlick3'] ?? '');
  128. $validated['na_mehlick3'] = validateNumeric($post['na_mehlick3'] ?? '');
  129. $validated['al_mehlick3'] = validateNumeric($post['al_mehlick3'] ?? '');
  130. // Additional calculations
  131. $validated['c_total'] = validateNumeric($post['c_total'] ?? '');
  132. $validated['n_total'] = validateNumeric($post['n_total'] ?? '');
  133. return $validated;
  134. }
  135. /**
  136. * Perform soil analysis calculations
  137. */
  138. function calculateSoilAnalysis(array $data): array
  139. {
  140. $calculations = [];
  141. // pH lookup table
  142. $phRange = [
  143. 30 => [75.0, 11.4], 31 => [74.0, 11.2], 32 => [73.0, 11.0], 33 => [72.0, 10.8],
  144. 34 => [71.0, 10.6], 35 => [70.0, 10.4], 36 => [69.0, 10.2], 37 => [68.0, 10.0],
  145. 38 => [67.0, 9.8], 39 => [66.0, 9.6], 40 => [65.0, 9.4], 41 => [63.0, 9.2],
  146. 42 => [61.0, 9.0], 43 => [59.0, 8.8], 44 => [57.0, 8.6], 45 => [55.0, 8.4],
  147. 46 => [53.0, 8.2], 47 => [51.0, 8.0], 48 => [49.0, 7.8], 49 => [47.0, 7.6],
  148. 50 => [45.0, 7.4], 51 => [42.0, 7.2], 52 => [39.0, 7.0], 53 => [36.0, 6.8],
  149. 54 => [33.0, 6.6], 55 => [30.0, 6.4], 56 => [27.0, 6.2], 57 => [24.0, 6.0],
  150. 58 => [21.0, 5.8], 59 => [18.0, 5.6], 60 => [15.0, 5.4], 61 => [13.5, 5.3],
  151. 62 => [12.0, 5.2], 63 => [10.5, 5.1], 64 => [9.0, 5.0], 65 => [7.5, 4.9],
  152. 66 => [6.0, 4.8], 67 => [4.5, 4.7], 68 => [3.0, 4.6], 69 => [1.5, 4.5],
  153. 70 => [0.0, 4.4], 71 => [0.0, 4.3], 72 => [0.0, 4.2], 73 => [0.0, 4.1],
  154. 74 => [0.0, 4.0], 75 => [0.0, 3.9], 76 => [0.0, 3.8], 77 => [0.0, 3.7],
  155. 78 => [0.0, 3.6], 79 => [0.0, 3.5], 80 => [0.0, 3.4], 81 => [0.0, 3.3],
  156. 82 => [0.0, 3.2], 83 => [0.0, 3.1], 84 => [0.0, 3.0], 85 => [0.0, 2.9],
  157. 86 => [0.0, 2.8], 87 => [0.0, 2.7], 88 => [0.0, 2.6], 89 => [0.0, 2.5],
  158. 90 => [0.0, 2.4], 91 => [0.0, 2.3], 92 => [0.0, 2.2], 93 => [0.0, 2.1],
  159. 94 => [0.0, 2.0], 95 => [0.0, 1.9], 96 => [0.0, 1.8], 97 => [0.0, 1.7],
  160. 98 => [0.0, 1.6], 99 => [0.0, 1.5], 100 => [0.0, 1.4],
  161. ];
  162. // Base saturation calculations
  163. $ph = $data['ph_h2o'];
  164. $aluminium = $data['al_mehlick3'];
  165. $phLookup = round($ph * 10);
  166. $hydrogen = $phRange[$phLookup][0] ?? 0;
  167. $otherbases = $phRange[$phLookup][1] ?? 0;
  168. $calculations['h_rec'] = round($hydrogen, 2);
  169. $calculations['ob_rec'] = round($otherbases, 2);
  170. if ($aluminium < 0) {
  171. $otherbases = 0;
  172. }
  173. // Calculate hydrogen and other bases results
  174. $obresult = 0;
  175. $hresult = 0;
  176. if ($otherbases > 0) {
  177. while ((($obresult * 100) / ($data['cec'] + $obresult + $hresult)) <= $otherbases) {
  178. $obresult += 0.001;
  179. $hresult = ($obresult * $hydrogen) / $otherbases;
  180. }
  181. $obresult -= 0.001;
  182. if ($hresult != 0) {
  183. $hresult -= 0.001;
  184. }
  185. } else {
  186. while ((($hresult * 100) / $data['tec']) <= $hydrogen) {
  187. $hresult += 0.001;
  188. }
  189. $hresult -= 0.001;
  190. }
  191. $tecTemp = $data['cec'] + $obresult + $hresult;
  192. $calculations['tec'] = round($tecTemp, 2);
  193. $calculations['cec'] = round($data['cec'], 2);
  194. // Base saturation percentages and recommendations
  195. $tec = $calculations['tec'];
  196. if ($tec >= 1 && $tec <= 3) {
  197. $calculations['cabs_max'] = 60.00; $calculations['mgbs_max'] = 20.00; $calculations['kbs'] = 5.00; $calculations['kbs_max'] = 7.00; $calculations['nabs_max'] = 1.50;
  198. } elseif ($tec > 3 && $tec <= 5) {
  199. $calculations['cabs_max'] = 62.00; $calculations['mgbs_max'] = 18.00; $calculations['kbs'] = 5.00; $calculations['kbs_max'] = 7.00; $calculations['nabs_max'] = 1.50;
  200. } elseif ($tec > 5 && $tec <= 7) {
  201. $calculations['cabs_max'] = 64.00; $calculations['mgbs_max'] = 16.00; $calculations['kbs'] = 4.00; $calculations['kbs_max'] = 7.00; $calculations['nabs_max'] = 1.50;
  202. } elseif ($tec > 7 && $tec <= 9) {
  203. $calculations['cabs_max'] = 65.00; $calculations['mgbs_max'] = 15.00; $calculations['kbs'] = 4.00; $calculations['kbs_max'] = 7.00; $calculations['nabs_max'] = 1.50;
  204. } elseif ($tec > 9 && $tec <= 11) {
  205. $calculations['cabs_max'] = 67.00; $calculations['mgbs_max'] = 13.00; $calculations['kbs'] = 4.00; $calculations['kbs_max'] = 7.00; $calculations['nabs_max'] = 1.50;
  206. } elseif ($tec > 11 && $tec <= 30) {
  207. $calculations['cabs_max'] = 68.00; $calculations['mgbs_max'] = 12.00; $calculations['kbs'] = 4.00; $calculations['kbs_max'] = 7.00; $calculations['nabs_max'] = 1.50;
  208. } else {
  209. $calculations['cabs_max'] = 70.00; $calculations['mgbs_max'] = 10.00; $calculations['kbs'] = 3.00; $calculations['kbs_max'] = 6.00; $calculations['nabs_max'] = 1.50;
  210. }
  211. // Calculate percentages and PPM values
  212. $calculations['cabs_tec'] = round(($data['ca_mehlick3'] / $tec) * 100, 2);
  213. $calculations['mgbs_tec'] = round(($data['mg_mehlick3'] / $tec) * 100, 2);
  214. $calculations['kbs_tec'] = round(($data['k_mehlick3'] / $tec) * 100, 2);
  215. $calculations['nabs_tec'] = round(($data['na_mehlick3'] / $tec) * 100, 2);
  216. $calculations['albs_tec'] = round(($data['al_mehlick3'] / $tec) * 100, 2);
  217. $calculations['BS_ca_ppm'] = $data['ca_mehlick3'] * 200;
  218. $calculations['BS_mg_ppm'] = $data['mg_mehlick3'] * 120;
  219. $calculations['BS_k_ppm'] = $data['k_mehlick3'] * 390;
  220. $calculations['BS_na_ppm'] = $data['na_mehlick3'] * 230;
  221. $calculations['BS_al_ppm'] = $data['al_mehlick3'] * 90;
  222. // Calculate ratios
  223. $calculations['ca_mg_ratio'] = $data['mg_mehlick3'] > 0 ? round($data['ca_mehlick3'] / $data['mg_mehlick3'], 2) : 0;
  224. $calculations['c_n_ratio'] = $data['n_total'] > 0 ? round($data['c_total'] / $data['n_total'], 2) : 0;
  225. return $calculations;
  226. }
  227. /**
  228. * Insert soil record into database
  229. */
  230. function insertSoilRecord(array $data, array $calculations, int $rand): int
  231. {
  232. global $pdo;
  233. $sql = "INSERT INTO soil_records (
  234. client_records_id, modx_user_id, date, email, client_name, site_address, state_postcode,
  235. analysis_type, lab_no, batch_no, sample_id, site_id, crop_type, soil_type, date_sampled,
  236. tec, cec, texture, gravel, colour, NO3_N, NH3_N, p_mehlick, p_bray2, p_morgan,
  237. k_morgan, ca_morgan, mg_morgan, na_morgan, ch_h2o, ocarbon, omatter, fe, ec,
  238. ph_cacl2, ph_h2o, paramag, s_morgan, b_cacl2, mn_dtpa, zn_dtpa, fe_dtpa, cu_dtpa,
  239. al, sl_cacl2, m_dtpa, co_dtpa, se, ca_mehlick3, BS_ca_ppm, mg_mehlick3, BS_mg_ppm,
  240. k_mehlick3, BS_k_ppm, na_mehlick3, BS_na_ppm, al_mehlick3, BS_al_ppm,
  241. BS_ca2, BS_mg2, BS_k, BS_na, BS_al3, BS_ob, BS_h,
  242. cabs_min, ca_ppm_min, cabs_max, ca_ppm_max, mgbs_min, mg_ppm_min, mgbs_max, mg_ppm_max,
  243. kbs_min, k_ppm_min, kbs_max, k_ppm_max, nabs_min, na_ppm_min, nabs_max, na_ppm_max,
  244. albs_min, al_ppm_min, albs_max, al_ppm_max, ob_rec, h_rec, ca_mg_ratio, rand
  245. ) VALUES (
  246. :client_id, :modx_user_id, NOW(), :email, :client_name, :site_address, :state_postcode,
  247. 'Soil Test', :lab_no, :batch_no, :sample_id, :site_id, :crop_type, :soil_type, :date_sampled,
  248. :tec, :cec, :texture, :gravel, :colour, :NO3_N, :NH3_N, :p_mehlick, :p_bray2, :p_morgan,
  249. :k_morgan, :ca_morgan, :mg_morgan, :na_morgan, :ch_h2o, :ocarbon, :omatter, :fe, :ec,
  250. :ph_cacl2, :ph_h2o, :paramag, :s_morgan, :b_cacl2, :mn_dtpa, :zn_dtpa, :fe_dtpa, :cu_dtpa,
  251. :al, :sl_cacl2, :m_dtpa, :co_dtpa, :se, :ca_mehlick3, :BS_ca_ppm, :mg_mehlick3, :BS_mg_ppm,
  252. :k_mehlick3, :BS_k_ppm, :na_mehlick3, :BS_na_ppm, :al_mehlick3, :BS_al_ppm,
  253. :cabs_tec, :mgbs_tec, :kbs_tec, :nabs_tec, :al_mehlick3, :ob_rec, :h_rec,
  254. 0, 0, :cabs_max, :ca_ppm_max, 0, 0, :mgbs_max, :mg_ppm_max,
  255. :kbs, :k_ppm_min, :kbs_max, :k_ppm_max, 0.50, :na_ppm_min, :nabs_max, :na_ppm_max,
  256. 0, 0, 0.5, :al_ppm_max, :ob_rec, :h_rec, :ca_mg_ratio, :rand
  257. )";
  258. $stmt = $pdo->prepare($sql);
  259. $stmt->execute([
  260. 'client_id' => $data['client_id'],
  261. 'modx_user_id' => $_SESSION['user_id'],
  262. 'email' => $data['email'],
  263. 'client_name' => $data['name'],
  264. 'site_address' => $data['site_address'],
  265. 'state_postcode' => $data['state_postcode'],
  266. 'lab_no' => $data['lab_no'],
  267. 'batch_no' => $data['batch_no'],
  268. 'sample_id' => $data['sample_id'],
  269. 'site_id' => $data['site_id'],
  270. 'crop_type' => $data['crop_type'],
  271. 'soil_type' => $data['soil_type'],
  272. 'date_sampled' => $data['date_sampled'],
  273. 'tec' => $calculations['tec'],
  274. 'cec' => $calculations['cec'],
  275. 'texture' => $data['texture'] ?: null,
  276. 'gravel' => $data['gravel'],
  277. 'colour' => $data['colour'] ?: null,
  278. 'NO3_N' => $data['NO3_N'],
  279. 'NH3_N' => $data['NH3_N'],
  280. 'p_mehlick' => $data['p_mehlick'],
  281. 'p_bray2' => $data['p_bray2'],
  282. 'p_morgan' => $data['p_morgan'],
  283. 'k_morgan' => $data['k_morgan'],
  284. 'ca_morgan' => $data['ca_morgan'],
  285. 'mg_morgan' => $data['mg_morgan'],
  286. 'na_morgan' => $data['na_morgan'],
  287. 'ch_h2o' => $data['ch_h2o'],
  288. 'ocarbon' => $data['ocarbon'],
  289. 'omatter' => $data['omatter'],
  290. 'fe' => $data['fe'],
  291. 'ec' => $data['ec'],
  292. 'ph_cacl2' => $data['ph_cacl2'],
  293. 'ph_h2o' => $data['ph_h2o'],
  294. 'paramag' => $data['paramag'] ?: null,
  295. 's_morgan' => $data['s_morgan'],
  296. 'b_cacl2' => $data['b_cacl2'],
  297. 'mn_dtpa' => $data['mn_dtpa'],
  298. 'zn_dtpa' => $data['zn_dtpa'],
  299. 'fe_dtpa' => $data['fe_dtpa'],
  300. 'cu_dtpa' => $data['cu_dtpa'],
  301. 'al' => $data['al'],
  302. 'sl_cacl2' => $data['sl_cacl2'],
  303. 'm_dtpa' => $data['m_dtpa'],
  304. 'co_dtpa' => $data['co_dtpa'],
  305. 'se' => $data['se'],
  306. 'ca_mehlick3' => $data['ca_mehlick3'],
  307. 'BS_ca_ppm' => $calculations['BS_ca_ppm'],
  308. 'mg_mehlick3' => $data['mg_mehlick3'],
  309. 'BS_mg_ppm' => $calculations['BS_mg_ppm'],
  310. 'k_mehlick3' => $data['k_mehlick3'],
  311. 'BS_k_ppm' => $calculations['BS_k_ppm'],
  312. 'na_mehlick3' => $data['na_mehlick3'],
  313. 'BS_na_ppm' => $calculations['BS_na_ppm'],
  314. 'al_mehlick3' => $data['al_mehlick3'],
  315. 'BS_al_ppm' => $calculations['BS_al_ppm'],
  316. 'cabs_tec' => $calculations['cabs_tec'],
  317. 'mgbs_tec' => $calculations['mgbs_tec'],
  318. 'kbs_tec' => $calculations['kbs_tec'],
  319. 'nabs_tec' => $calculations['nabs_tec'],
  320. 'cabs_max' => $calculations['cabs_max'],
  321. 'ca_ppm_max' => $calculations['tec'] * $calculations['cabs_max'] * 2,
  322. 'mgbs_max' => $calculations['mgbs_max'],
  323. 'mg_ppm_max' => $calculations['tec'] * $calculations['mgbs_max'] * 1.2,
  324. 'kbs' => $calculations['kbs'],
  325. 'k_ppm_min' => $calculations['tec'] * $calculations['kbs'] * 3.9,
  326. 'kbs_max' => $calculations['kbs_max'],
  327. 'k_ppm_max' => $calculations['tec'] * $calculations['kbs_max'] * 3.9,
  328. 'na_ppm_min' => $calculations['tec'] * 0.5 * 2.3,
  329. 'nabs_max' => $calculations['nabs_max'],
  330. 'na_ppm_max' => $calculations['tec'] * $calculations['nabs_max'] * 2.3,
  331. 'al_ppm_max' => $calculations['tec'] * 0.5 * 0.9,
  332. 'ob_rec' => $calculations['ob_rec'],
  333. 'h_rec' => $calculations['h_rec'],
  334. 'ca_mg_ratio' => $calculations['ca_mg_ratio'],
  335. 'rand' => $rand
  336. ]);
  337. return $pdo->lastInsertId();
  338. }
  339. // ValidationException, sanitizeString(), validateNumeric() are provided by lib/validation.php