blockSubmit.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * controllers/blockSubmit.php
  4. *
  5. * Handles Create and Edit (update) submissions for block_info records.
  6. * Redirects back to /dashboard/crop-cards/ on success or failure.
  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-cards/');
  17. exit;
  18. }
  19. if (!verifyCsrfToken($_POST['csrf_token'] ?? '')) {
  20. $_SESSION['flash_error'] = 'Invalid security token. Please try again.';
  21. header('Location: /dashboard/crop-cards/');
  22. exit;
  23. }
  24. $action = $_POST['action'] ?? '';
  25. $userId = getCurrentUserId();
  26. $pdo = getDBConnection();
  27. // Shared field sanitisation
  28. $name = trim($_POST['name'] ?? '');
  29. $blockId = trim($_POST['block_id'] ?? '');
  30. $location = trim($_POST['location'] ?? '');
  31. $areaHa = is_numeric($_POST['area_ha'] ?? '') ? (float) $_POST['area_ha'] : 0;
  32. $gps = trim($_POST['gps'] ?? '');
  33. $soilType = trim($_POST['analysis_type'] ?? '');
  34. if ($name === '' || $blockId === '') {
  35. $_SESSION['flash_error'] = 'Block ID and Block Name are required.';
  36. header('Location: /dashboard/crop-cards/');
  37. exit;
  38. }
  39. if ($action === 'create') {
  40. $stmt = $pdo->prepare('
  41. INSERT INTO block_info (modx_user_id, name, block_id, location, area, gps, analysis_type, status, date_added)
  42. VALUES (?, ?, ?, ?, ?, ?, ?, 0, CURDATE())
  43. ');
  44. $stmt->execute([$userId, $name, $blockId, $location, $areaHa, $gps, $soilType]);
  45. $_SESSION['flash_success'] = 'Paddock "' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . '" created.';
  46. header('Location: /dashboard/crop-cards/');
  47. exit;
  48. }
  49. if ($action === 'edit') {
  50. $recordId = (int) ($_POST['record_id'] ?? 0);
  51. if ($recordId <= 0) {
  52. $_SESSION['flash_error'] = 'Invalid record.';
  53. header('Location: /dashboard/crop-cards/');
  54. exit;
  55. }
  56. // Ownership check
  57. $check = $pdo->prepare('SELECT id FROM block_info WHERE id = ? AND modx_user_id = ? LIMIT 1');
  58. $check->execute([$recordId, $userId]);
  59. if (!$check->fetch()) {
  60. $_SESSION['flash_error'] = 'Record not found or access denied.';
  61. header('Location: /dashboard/crop-cards/');
  62. exit;
  63. }
  64. $stmt = $pdo->prepare('
  65. UPDATE block_info
  66. SET name = ?, block_id = ?, location = ?, area = ?, gps = ?, analysis_type = ?
  67. WHERE id = ? AND modx_user_id = ?
  68. ');
  69. $stmt->execute([$name, $blockId, $location, $areaHa, $gps, $soilType, $recordId, $userId]);
  70. $_SESSION['flash_success'] = 'Paddock "' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . '" updated.';
  71. // Return to paddock dashboard if the edit came from there
  72. $referer = $_POST['_referer'] ?? '';
  73. if (str_contains($referer, 'block-detail.php')) {
  74. header('Location: /dashboard/crop-cards/block-detail.php?rid=' . $recordId
  75. . '&id=' . urlencode($blockId));
  76. } else {
  77. header('Location: /dashboard/crop-cards/');
  78. }
  79. exit;
  80. }
  81. // Unknown action
  82. header('Location: /dashboard/crop-cards/');
  83. exit;