blockSubmit.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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, status, date_added)
  42. VALUES (?, ?, ?, ?, ?, ?, 0, CURDATE())
  43. ');
  44. $stmt->execute([$userId, $name, $blockId, $location, (int) $areaHa, $gps]);
  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 = ?
  67. WHERE id = ? AND modx_user_id = ?
  68. ');
  69. $stmt->execute([$name, $blockId, $location, (int) $areaHa, $gps, $recordId, $userId]);
  70. $_SESSION['flash_success'] = 'Paddock "' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . '" updated.';
  71. header('Location: /dashboard/crop-cards/');
  72. exit;
  73. }
  74. // Unknown action
  75. header('Location: /dashboard/crop-cards/');
  76. exit;