index.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. require_once __DIR__ . '/../../../config/database.php';
  3. require_once __DIR__ . '/../../../lib/auth.php';
  4. require_once __DIR__ . '/../../../lib/csrf.php';
  5. if (session_status() === PHP_SESSION_NONE) {
  6. session_start();
  7. }
  8. requireLogin();
  9. $pageTitle = 'Water Test Analysis';
  10. $siteName = 'Crop Monitor';
  11. include __DIR__ . '/../../../layouts/header.php';
  12. include __DIR__ . '/../../../layouts/navbar.php';
  13. ?>
  14. <style>
  15. .btn-append {
  16. color: #495057;
  17. background-color: #e9ecef;
  18. border: 1px solid #ced4da;
  19. }
  20. </style>
  21. <div id="layoutSidenav">
  22. <div id="layoutSidenav_nav">
  23. <?php include __DIR__ . '/../../../layouts/sidebar.php'; ?>
  24. </div>
  25. <div id="layoutSidenav_content">
  26. <main>
  27. <div class="container-fluid px-4">
  28. <h1 class="mt-4"><?= htmlspecialchars($pageTitle, ENT_QUOTES, 'UTF-8') ?></h1>
  29. <ol class="breadcrumb mb-4">
  30. <li class="breadcrumb-item"><a href="/dashboard/dashboard.php">Dashboard</a></li>
  31. <li class="breadcrumb-item active">Water Test Analysis</li>
  32. </ol>
  33. <div class="row">
  34. <div class="container">
  35. <h3>Water Test Details</h3>
  36. <span class="text-danger small">* required fields.</span>
  37. <?php include __DIR__ . '/../../../components/clientDetailsForm.php'; ?>
  38. <form method="post" action="/controllers/waterTestSubmit.php" id="WatercsvForm" class="needs-validation" novalidate>
  39. <input type="hidden" name="csrf_token" value="<?= htmlspecialchars(generateCsrfToken(), ENT_QUOTES, 'UTF-8') ?>">
  40. <hr>
  41. <div class="row">
  42. <div class="col">
  43. <small class="form-text text-muted">Lab No</small>
  44. <input type="text" class="form-control form-control-sm" name="lab_no" id="lab_no" placeholder="Lab No">
  45. </div>
  46. <div class="col">
  47. <small class="form-text text-muted">Batch No</small>
  48. <input type="text" class="form-control form-control-sm" name="batch_no" id="batch_no" placeholder="Batch No">
  49. </div>
  50. <div class="col">
  51. <small class="form-text text-muted">Date Sampled</small>
  52. <input type="date" class="form-control form-control-sm" name="date_sampled" id="date_sampled" placeholder="Date Sampled">
  53. </div>
  54. <div class="col">
  55. <small class="form-text text-muted">Sample ID <span class="text-danger">*</span></small>
  56. <input type="text" class="form-control form-control-sm" name="sample_id" id="sample_id" placeholder="Sample Id" required>
  57. </div>
  58. <div class="col">
  59. <small class="form-text text-muted">Site ID</small>
  60. <input type="text" class="form-control form-control-sm" name="site_id" id="site_id" placeholder="Paddock Id">
  61. </div>
  62. </div>
  63. <hr>
  64. <label class="col"><b>Analysis Inputs</b></label>
  65. <div class="row mt-2">
  66. <div class="col-md-2">
  67. <small class="form-text text-muted">pH <span class="text-danger">*</span></small>
  68. <div class="input-group input-group-sm mb-3">
  69. <input type="text" class="form-control form-control-sm" name="ph" id="ph" placeholder="pH" required>
  70. <span class="input-group-text">pH</span>
  71. </div>
  72. </div>
  73. <div class="col-md-2">
  74. <small class="form-text text-muted">Conductivity</small>
  75. <div class="input-group input-group-sm mb-3">
  76. <input type="text" class="form-control form-control-sm" name="cond_dsm" id="cond_dsm" placeholder="Conductivity">
  77. <span class="input-group-text">dS/m</span>
  78. </div>
  79. </div>
  80. <div class="col-md-2">
  81. <small class="form-text text-muted">Bicarbonate (HCO3-)</small>
  82. <div class="input-group input-group-sm mb-3">
  83. <input type="text" class="form-control form-control-sm" name="hco3" id="hco3" placeholder="HCO3-">
  84. <span class="input-group-text">mg/L</span>
  85. </div>
  86. </div>
  87. </div>
  88. <div class="row">
  89. <?php
  90. $elements = [
  91. ['n', 'Nitrogen', 10000],
  92. ['p', 'Phosphorus', 10000],
  93. ['k', 'Potassium', 10000],
  94. ['s', 'Sulphur', 10000],
  95. ['mg', 'Magnesium', 10000],
  96. ['ca', 'Calcium', 10000],
  97. ];
  98. foreach ($elements as [$id, $label, $factor]): ?>
  99. <div class="col-md-2">
  100. <small class="form-text text-muted"><?= htmlspecialchars($label, ENT_QUOTES, 'UTF-8') ?></small>
  101. <div class="input-group input-group-sm mb-3">
  102. <input type="text" class="form-control form-control-sm"
  103. name="<?= $id ?>" id="<?= $id ?>"
  104. placeholder="<?= htmlspecialchars($id . ' - ' . $label, ENT_QUOTES, 'UTF-8') ?>">
  105. <input class="btn btn-append" type="button"
  106. id="btn_<?= $id ?>" value="ppm"
  107. onclick="conversion(this, document.getElementById('<?= $id ?>'), <?= $factor ?>)">
  108. </div>
  109. </div>
  110. <?php endforeach; ?>
  111. </div>
  112. <div class="row">
  113. <?php
  114. $elements2 = [
  115. ['na', 'Sodium', 10000],
  116. ['fe', 'Iron', 10000],
  117. ['mn', 'Manganese', 10000],
  118. ['zn', 'Zinc', 10000],
  119. ['cu', 'Copper', 10000],
  120. ['b', 'Boron', 10000],
  121. ];
  122. foreach ($elements2 as [$id, $label, $factor]): ?>
  123. <div class="col-md-2">
  124. <small class="form-text text-muted"><?= htmlspecialchars($label, ENT_QUOTES, 'UTF-8') ?></small>
  125. <div class="input-group input-group-sm mb-3">
  126. <input type="text" class="form-control form-control-sm"
  127. name="<?= $id ?>" id="<?= $id ?>"
  128. placeholder="<?= htmlspecialchars($id . ' - ' . $label, ENT_QUOTES, 'UTF-8') ?>">
  129. <input class="btn btn-append" type="button"
  130. id="btn_<?= $id ?>" value="ppm"
  131. onclick="conversion(this, document.getElementById('<?= $id ?>'), <?= $factor ?>)">
  132. </div>
  133. </div>
  134. <?php endforeach; ?>
  135. </div>
  136. <div class="row">
  137. <?php
  138. $elements3 = [
  139. ['m', 'Molybdenum', 10000],
  140. ['co', 'Cobalt', 10000],
  141. ['se', 'Selenium', 10000],
  142. ['ch', 'Chloride', 10000],
  143. ];
  144. foreach ($elements3 as [$id, $label, $factor]): ?>
  145. <div class="col-md-2">
  146. <small class="form-text text-muted"><?= htmlspecialchars($label, ENT_QUOTES, 'UTF-8') ?></small>
  147. <div class="input-group input-group-sm mb-3">
  148. <input type="text" class="form-control form-control-sm"
  149. name="<?= $id ?>" id="<?= $id ?>"
  150. placeholder="<?= htmlspecialchars($id . ' - ' . $label, ENT_QUOTES, 'UTF-8') ?>">
  151. <input class="btn btn-append" type="button"
  152. id="btn_<?= $id ?>" value="ppm"
  153. onclick="conversion(this, document.getElementById('<?= $id ?>'), <?= $factor ?>)">
  154. </div>
  155. </div>
  156. <?php endforeach; ?>
  157. </div>
  158. <button type="submit" class="btn btn-success">Submit</button>
  159. </form>
  160. <?php include __DIR__ . '/../../../components/newClientModal.php'; ?>
  161. <hr>
  162. <div class="card mt-3">
  163. <div class="card-body">
  164. <h5 class="card-title">Excel/CSV Upload</h5>
  165. <p class="card-text">Download a CSV of this form for easy filling or upload a filled form to pre-populate.</p>
  166. <div class="input-group mt-3">
  167. <input type="file" class="form-control" id="upload" accept=".csv">
  168. <button class="btn btn-success" type="button" id="download">Download</button>
  169. </div>
  170. </div>
  171. </div>
  172. </div>
  173. </div>
  174. </div>
  175. </main>
  176. <?php include __DIR__ . '/../../../layouts/footer.php'; ?>
  177. </div>
  178. </div>
  179. <script>
  180. function conversion(btnEl, element, factor) {
  181. var val = parseFloat(element.value) || 0;
  182. if (btnEl.value === 'ppm') {
  183. btnEl.value = '%';
  184. element.value = (val / factor).toFixed(4);
  185. } else {
  186. btnEl.value = 'ppm';
  187. element.value = (val * factor).toFixed(2);
  188. }
  189. }
  190. // CSV download
  191. document.getElementById('download').addEventListener('click', function () {
  192. var data = [['id', 'value']];
  193. document.querySelectorAll('#WatercsvForm input:not([type=hidden]):not([type=button]):not([type=submit]), #WatercsvForm select').forEach(function (el) {
  194. if (el.id) data.push([el.id, el.value]);
  195. });
  196. var csv = data.map(function (r) { return r.join(','); }).join('\n');
  197. var today = new Date();
  198. var fname = today.getDate() + '' + (today.getMonth() + 1) + '' + today.getFullYear() + '-water.csv';
  199. var link = document.createElement('a');
  200. link.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv));
  201. link.setAttribute('download', fname);
  202. link.click();
  203. });
  204. // CSV upload
  205. document.getElementById('upload').addEventListener('change', function (e) {
  206. var file = e.target.files[0];
  207. if (!file) return;
  208. var reader = new FileReader();
  209. reader.onload = function (ev) {
  210. ev.target.result.split('\n').slice(1).forEach(function (line) {
  211. var parts = line.split(',');
  212. var el = document.getElementById(parts[0]);
  213. if (el) el.value = parts[1] || '';
  214. });
  215. };
  216. reader.readAsText(file);
  217. });
  218. // Bootstrap validation
  219. (function () {
  220. 'use strict';
  221. document.querySelectorAll('.needs-validation').forEach(function (form) {
  222. form.addEventListener('submit', function (event) {
  223. if (!form.checkValidity()) {
  224. event.preventDefault();
  225. event.stopPropagation();
  226. }
  227. form.classList.add('was-validated');
  228. }, false);
  229. });
  230. })();
  231. </script>