plantTestSubmit.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * controllers/plantTestSubmit.php
  4. *
  5. * Handles POST submission of plant tissue analysis test data.
  6. * Replaces dashboard/crop-analysis/plant-test-data/generating-plant-analysis.php
  7. */
  8. if (session_status() === PHP_SESSION_NONE) {
  9. session_start();
  10. }
  11. require_once __DIR__ . '/../config/database.php';
  12. require_once __DIR__ . '/../lib/auth.php';
  13. require_once __DIR__ . '/../lib/csrf.php';
  14. requireLogin();
  15. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  16. header('Location: /dashboard/crop-analysis/plant-test-data/');
  17. exit;
  18. }
  19. if (!verifyCsrfToken($_POST['csrf_token'] ?? '')) {
  20. http_response_code(403);
  21. exit('Invalid security token.');
  22. }
  23. $pdo = getDBConnection();
  24. $userId = getCurrentUserId();
  25. $date = date('Y-m-d');
  26. $rand = mt_rand(10000, 99999);
  27. $str = fn($key) => trim($_POST[$key] ?? '');
  28. $email = $str('email');
  29. $clientName = $str('name');
  30. $siteAddress = $str('site_address');
  31. $statePostcode = $str('state_postcode');
  32. $analysisType = $str('analysis_type');
  33. $labNo = $str('lab_no');
  34. $batchNo = $str('batch_no');
  35. $dateSampled = $str('date_sampled') ?: null;
  36. $sampleId = $str('sample_id');
  37. $siteId = $str('site_id');
  38. $cropType = $str('crop_type');
  39. $n = $str('n'); $p = $str('p'); $k = $str('k');
  40. $s = $str('s'); $mg = $str('mg'); $ca = $str('ca');
  41. $na = $str('na'); $fe = $str('fe'); $mn = $str('mn');
  42. $zn = $str('zn'); $cu = $str('cu'); $b = $str('b');
  43. $m = $str('m') ?: null;
  44. $co = $str('co') ?: null;
  45. $se = $str('se') ?: null;
  46. $cl = $str('cl') ?: null;
  47. $stmt = $pdo->prepare('
  48. INSERT INTO plant_records
  49. (client_records_id, modx_user_id, date, email, client_name, site_address,
  50. state_postcode, analysis_type, lab_no, batch_no, date_sampled, sample_id,
  51. site_id, crop_type, n, p, k, s, mg, ca, na, fe, mn, zn, cu, b,
  52. m, co, se, cl, rand)
  53. VALUES
  54. (0, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  55. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  56. ?, ?, ?, ?, ?)
  57. ');
  58. $stmt->execute([
  59. $userId, $date, $email, $clientName, $siteAddress,
  60. $statePostcode, $analysisType, $labNo, $batchNo, $dateSampled, $sampleId,
  61. $siteId, $cropType,
  62. $n, $p, $k, $s, $mg, $ca, $na, $fe, $mn, $zn, $cu, $b,
  63. $m, $co, $se, $cl, $rand,
  64. ]);
  65. $insertId = (int) $pdo->lastInsertId();
  66. header('Location: /dashboard/crop-analysis/plant-test-data/plant-analysis.php?rand=' . $rand
  67. . '&cid=' . urlencode($sampleId)
  68. . '&rid=' . $insertId
  69. . '&stid=' . urlencode($cropType));
  70. exit;