uploadsubmit.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * dashboard/crop-analysis/uploadsubmit.php
  4. *
  5. * Spreadsheet bulk-upload handler for soil records.
  6. * Requires the SpreadsheetReader library.
  7. *
  8. * NOTE: Column index → DB field mapping below must match the actual
  9. * spreadsheet format provided by the lab. Update $colMap as needed.
  10. */
  11. if (session_status() === PHP_SESSION_NONE) {
  12. session_start();
  13. }
  14. require_once __DIR__ . '/../../config/database.php';
  15. require_once __DIR__ . '/../../lib/auth.php';
  16. requireLogin();
  17. $readerBase = __DIR__ . '/../../client-assets/php/spreadsheet';
  18. $readerFile = $readerBase . '/php-excel-reader/excel_reader2.php';
  19. $spreadFile = $readerBase . '/SpreadsheetReader.php';
  20. if (!file_exists($readerFile) || !file_exists($spreadFile)) {
  21. http_response_code(500);
  22. exit('<p class="text-danger">Spreadsheet reader library not found.</p>');
  23. }
  24. require_once $readerFile;
  25. require_once $spreadFile;
  26. if (!isset($_POST['Submit'])) {
  27. exit;
  28. }
  29. $allowedMimes = [
  30. 'application/vnd.ms-excel',
  31. 'text/xls', 'text/xlsx', 'text/csv',
  32. 'application/vnd.oasis.opendocument.spreadsheet',
  33. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  34. ];
  35. if (!in_array($_FILES['file']['type'] ?? '', $allowedMimes, true)) {
  36. exit('<p class="text-danger">Invalid file type. Please upload an Excel or CSV file.</p>');
  37. }
  38. $uploadDir = __DIR__ . '/../../client-assets/uploads/';
  39. $uploadPath = $uploadDir . basename($_FILES['file']['name']);
  40. if (!move_uploaded_file($_FILES['file']['tmp_name'], $uploadPath)) {
  41. exit('<p class="text-danger">Failed to upload file.</p>');
  42. }
  43. $reader = new SpreadsheetReader($uploadPath);
  44. $totalSheet = count($reader->sheets());
  45. echo '<p>File uploaded — ' . (int) $totalSheet . ' sheet(s) found.</p>';
  46. $pdo = getDBConnection();
  47. $userId = getCurrentUserId();
  48. /**
  49. * Map spreadsheet column indices to soil_records column names.
  50. * Adjust indices to match your actual spreadsheet layout.
  51. */
  52. $colMap = [
  53. 0 => 'analysis_type',
  54. 1 => 'lab_no',
  55. 2 => 'batch_no',
  56. 3 => 'sample_id',
  57. 4 => 'site_id',
  58. 5 => 'crop',
  59. 6 => 'date_sampled',
  60. 7 => 'tec',
  61. 8 => 'cec',
  62. 9 => 'texture',
  63. 10 => 'gravel',
  64. 11 => 'colour',
  65. 12 => 'NO3_N',
  66. 13 => 'NH3_N',
  67. 14 => 'p_mehlick',
  68. 15 => 'p_bray2',
  69. 16 => 'p_morgan',
  70. 17 => 'k_morgan',
  71. 18 => 'ca_morgan',
  72. 19 => 'mg_morgan',
  73. 20 => 'na_morgan',
  74. 21 => 'ch_h2o',
  75. 22 => 'ocarbon',
  76. 23 => 'omatter',
  77. 24 => 'fe',
  78. 25 => 'ec',
  79. 26 => 'ph_cacl2',
  80. 27 => 'ph_h2o',
  81. 28 => 'paramag',
  82. 29 => 's_morgan',
  83. 30 => 'b_cacl2',
  84. 31 => 'mn_dtpa',
  85. 32 => 'zn_dtpa',
  86. 33 => 'fe_dtpa',
  87. 34 => 'cu_dtpa',
  88. 35 => 'al',
  89. 36 => 'sl_cacl2',
  90. 37 => 'm_dtpa',
  91. 38 => 'co_dtpa',
  92. 39 => 'se',
  93. 40 => 'ca_mehlick3',
  94. 41 => 'mg_mehlick3',
  95. 42 => 'k_mehlick3',
  96. 43 => 'na_mehlick3',
  97. 44 => 'al_mehlick3',
  98. ];
  99. $columns = array_values($colMap);
  100. $placeholders = implode(', ', array_fill(0, count($columns) + 2, '?'));
  101. $colList = 'modx_user_id, rand, ' . implode(', ', array_map(fn($c) => "`$c`", $columns));
  102. $stmt = $pdo->prepare(
  103. "INSERT INTO soil_records ($colList) VALUES ($placeholders)"
  104. );
  105. $inserted = 0;
  106. $skipped = 0;
  107. for ($i = 0; $i < $totalSheet; $i++) {
  108. $reader->ChangeSheet($i);
  109. $firstRow = true;
  110. foreach ($reader as $row) {
  111. if ($firstRow) { $firstRow = false; continue; } // skip header row
  112. $rand = mt_rand(10000, 99999);
  113. $values = [$userId, $rand];
  114. foreach ($colMap as $idx => $colName) {
  115. $val = isset($row[$idx]) ? trim((string) $row[$idx]) : null;
  116. $values[] = ($val === '') ? null : $val;
  117. }
  118. try {
  119. $stmt->execute($values);
  120. $inserted++;
  121. } catch (\PDOException $e) {
  122. $skipped++;
  123. }
  124. }
  125. }
  126. // Remove uploaded file after processing
  127. @unlink($uploadPath);
  128. echo '<p class="text-success">Import complete: '
  129. . (int) $inserted . ' record(s) inserted, '
  130. . (int) $skipped . ' skipped.</p>';