inbox.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', 1);
  4. require_once __DIR__ . '/../config/database.php';
  5. require_once __DIR__ . '/../lib/auth.php';
  6. if (session_status() === PHP_SESSION_NONE) {
  7. session_start();
  8. }
  9. requireLogin();
  10. $pageTitle = 'Report History';
  11. $siteName = 'Crop Monitor';
  12. $pdo = getDBConnection();
  13. $userId = getCurrentUserId();
  14. function countRecords(\PDO $pdo, string $table, int $userId, int $status = 0): int {
  15. $stmt = $pdo->prepare("SELECT COUNT(*) FROM `$table` WHERE modx_user_id = ? AND status = ?");
  16. $stmt->execute([$userId, $status]);
  17. return (int) $stmt->fetchColumn();
  18. }
  19. $counts = [
  20. 'soil' => countRecords($pdo, 'soil_records', $userId, 0),
  21. 'plant' => countRecords($pdo, 'plant_records', $userId, 0),
  22. 'water' => countRecords($pdo, 'water_records', $userId, 0),
  23. 'animal' => countRecords($pdo, 'animal_records', $userId, 0),
  24. 'archived'=> countRecords($pdo, 'soil_records', $userId, 2),
  25. 'deleted' => countRecords($pdo, 'soil_records', $userId, 3),
  26. ];
  27. function fetchHistory(\PDO $pdo, string $table, int $userId, string $select = 'id, rand, lab_no, sample_id, site_id, crop_type, date_sampled'): array {
  28. $stmt = $pdo->prepare(
  29. "SELECT {$select}
  30. FROM `{$table}`
  31. WHERE modx_user_id = ? AND status = 0
  32. ORDER BY id DESC LIMIT 100"
  33. );
  34. $stmt->execute([$userId]);
  35. return $stmt->fetchAll();
  36. }
  37. $soilRows = fetchHistory($pdo, 'soil_records', $userId);
  38. $plantRows = fetchHistory($pdo, 'plant_records', $userId);
  39. $waterRows = fetchHistory($pdo, 'water_records', $userId, 'id, rand, lab_no, sample_id, site_id, date_sampled');
  40. $animalRows = fetchHistory($pdo, 'animal_records', $userId, 'id, rand, lab_no, sample_id, NULL AS site_id, date_sampled');
  41. $h = fn($v) => htmlspecialchars((string) $v, ENT_QUOTES, 'UTF-8');
  42. include __DIR__ . '/../layouts/header.php';
  43. include __DIR__ . '/../layouts/navbar.php';
  44. ?>
  45. <div id="layoutSidenav">
  46. <div id="layoutSidenav_nav">
  47. <?php include __DIR__ . '/../layouts/sidebar.php'; ?>
  48. </div>
  49. <div id="layoutSidenav_content">
  50. <main>
  51. <div class="container-fluid px-4">
  52. <h1 class="mt-4"><?= $h($pageTitle) ?></h1>
  53. <ol class="breadcrumb mb-4">
  54. <li class="breadcrumb-item"><a href="/dashboard/dashboard.php">Dashboard</a></li>
  55. <li class="breadcrumb-item active">Report History</li>
  56. </ol>
  57. <div class="row inbox-mail">
  58. <!-- Left sidebar -->
  59. <div class="col-12 col-md-3">
  60. <div class="input-group mb-3">
  61. <input type="text" class="form-control" placeholder="Search...">
  62. <span class="input-group-text"><i class="fa fa-search"></i></span>
  63. </div>
  64. <div class="alert alert-danger py-0"><h4>Reports</h4></div>
  65. <div class="d-flex align-items-start list-group" id="v-pills-tab" role="tablist" aria-orientation="vertical">
  66. <a id="soil-reports-tab" data-bs-toggle="pill" data-bs-target="#soil-reports"
  67. type="button" role="tab"
  68. class="text-secondary list-group-item list-group-item-action list-group-item-light active">
  69. <i class="fas fa-icicles fa-rotate-180"></i> Soil Analysis
  70. <span class="badge bg-secondary float-end"><?= $counts['soil'] ?></span>
  71. </a>
  72. <a id="plant-reports-tab" data-bs-toggle="pill" data-bs-target="#plant-reports"
  73. type="button" role="tab"
  74. class="text-secondary list-group-item list-group-item-action list-group-item-light">
  75. <i class="fab fa-pagelines"></i> Plant Analysis
  76. <span class="badge bg-success float-end"><?= $counts['plant'] ?></span>
  77. </a>
  78. <a id="water-reports-tab" data-bs-toggle="pill" data-bs-target="#water-reports"
  79. type="button" role="tab"
  80. class="text-secondary list-group-item list-group-item-action list-group-item-light">
  81. <i class="fa fa-tint"></i> Water Analysis
  82. <span class="badge bg-primary float-end"><?= $counts['water'] ?></span>
  83. </a>
  84. <a id="dietary-reports-tab" data-bs-toggle="pill" data-bs-target="#dietary-reports"
  85. type="button" role="tab"
  86. class="text-secondary list-group-item list-group-item-action list-group-item-light">
  87. <i class="fas fa-dog"></i> Animal Dietary Balance
  88. <span class="badge bg-warning text-dark float-end"><?= $counts['animal'] ?></span>
  89. </a>
  90. <a id="compost-reports-tab" data-bs-toggle="pill" data-bs-target="#compost-reports"
  91. type="button" role="tab"
  92. class="text-secondary list-group-item list-group-item-action list-group-item-light">
  93. <i class="fas fa-cloud"></i> Compost Test Data
  94. <span class="badge bg-secondary float-end">0</span>
  95. </a>
  96. </div>
  97. <br>
  98. <div class="list-group">
  99. <span class="list-group-item fw-bold">Folders</span>
  100. <a href="#" class="list-group-item list-group-item-action list-group-item-light text-warning">
  101. <i class="fa fa-folder text-warning"></i> Archived
  102. <span class="badge bg-warning text-dark float-end"><?= $counts['archived'] ?></span>
  103. </a>
  104. <a href="#" class="list-group-item list-group-item-action list-group-item-light text-danger">
  105. <i class="fa fa-folder text-danger"></i> Deleted
  106. <span class="badge bg-danger float-end"><?= $counts['deleted'] ?></span>
  107. </a>
  108. </div>
  109. </div>
  110. <!-- Tab content -->
  111. <div class="col-12 col-md-9 tab-content">
  112. <!-- Soil -->
  113. <div class="tab-pane fade show active" id="soil-reports" role="tabpanel">
  114. <h5 class="mt-2">Soil Analysis Records</h5>
  115. <table class="table table-hover table-sm">
  116. <thead class="table-dark">
  117. <tr>
  118. <th>Lab No</th><th>Sample ID</th><th>Site ID</th>
  119. <th>Crop</th><th>Date Sampled</th><th class="text-end">Actions</th>
  120. </tr>
  121. </thead>
  122. <tbody>
  123. <?php if (empty($soilRows)): ?>
  124. <tr><td colspan="6" class="text-center text-muted">No records found.</td></tr>
  125. <?php else: foreach ($soilRows as $r): ?>
  126. <tr>
  127. <td><?= $h($r['lab_no']) ?></td>
  128. <td><?= $h($r['sample_id']) ?></td>
  129. <td><?= $h($r['site_id']) ?></td>
  130. <td><?= $h($r['crop_type']) ?></td>
  131. <td><?= $h($r['date_sampled']) ?></td>
  132. <td class="text-end">
  133. <a href="/dashboard/crop-analysis/soil-test-data/soil-analysis.php?rid=<?= (int)$r['id'] ?>&rand=<?= (float)$r['rand'] ?>"
  134. class="btn btn-sm btn-outline-success">View</a>
  135. </td>
  136. </tr>
  137. <?php endforeach; endif; ?>
  138. </tbody>
  139. </table>
  140. </div>
  141. <!-- Plant -->
  142. <div class="tab-pane fade" id="plant-reports" role="tabpanel">
  143. <h5 class="mt-2">Plant Analysis Records</h5>
  144. <table class="table table-hover table-sm">
  145. <thead class="table-dark">
  146. <tr>
  147. <th>Lab No</th><th>Sample ID</th><th>Site ID</th>
  148. <th>Crop</th><th>Date Sampled</th><th class="text-end">Actions</th>
  149. </tr>
  150. </thead>
  151. <tbody>
  152. <?php if (empty($plantRows)): ?>
  153. <tr><td colspan="6" class="text-center text-muted">No records found.</td></tr>
  154. <?php else: foreach ($plantRows as $r): ?>
  155. <tr>
  156. <td><?= $h($r['lab_no']) ?></td>
  157. <td><?= $h($r['sample_id']) ?></td>
  158. <td><?= $h($r['site_id']) ?></td>
  159. <td><?= $h($r['crop_type']) ?></td>
  160. <td><?= $h($r['date_sampled']) ?></td>
  161. <td class="text-end">
  162. <a href="/dashboard/crop-analysis/plant-test-data/plant-analysis.php?rid=<?= (int)$r['id'] ?>&rand=<?= (float)$r['rand'] ?>"
  163. class="btn btn-sm btn-outline-success">View</a>
  164. </td>
  165. </tr>
  166. <?php endforeach; endif; ?>
  167. </tbody>
  168. </table>
  169. </div>
  170. <!-- Water -->
  171. <div class="tab-pane fade" id="water-reports" role="tabpanel">
  172. <h5 class="mt-2">Water Analysis Records</h5>
  173. <table class="table table-hover table-sm">
  174. <thead class="table-dark">
  175. <tr>
  176. <th>Lab No</th><th>Sample ID</th><th>Site ID</th>
  177. <th>Date Sampled</th><th class="text-end">Actions</th>
  178. </tr>
  179. </thead>
  180. <tbody>
  181. <?php if (empty($waterRows)): ?>
  182. <tr><td colspan="5" class="text-center text-muted">No records found.</td></tr>
  183. <?php else: foreach ($waterRows as $r): ?>
  184. <tr>
  185. <td><?= $h($r['lab_no']) ?></td>
  186. <td><?= $h($r['sample_id']) ?></td>
  187. <td><?= $h($r['site_id']) ?></td>
  188. <td><?= $h($r['date_sampled']) ?></td>
  189. <td class="text-end">
  190. <a href="/dashboard/crop-analysis/water-test-data/water-analysis-pdf.php?rid=<?= (int)$r['id'] ?>&rand=<?= (float)$r['rand'] ?>"
  191. class="btn btn-sm btn-outline-success">View</a>
  192. </td>
  193. </tr>
  194. <?php endforeach; endif; ?>
  195. </tbody>
  196. </table>
  197. </div>
  198. <!-- Animal Dietary -->
  199. <div class="tab-pane fade" id="dietary-reports" role="tabpanel">
  200. <h5 class="mt-2">Animal Dietary Balance Records</h5>
  201. <table class="table table-hover table-sm">
  202. <thead class="table-dark">
  203. <tr>
  204. <th>Lab No</th><th>Sample ID</th><th>Site ID</th>
  205. <th>Date Sampled</th><th class="text-end">Actions</th>
  206. </tr>
  207. </thead>
  208. <tbody>
  209. <?php if (empty($animalRows)): ?>
  210. <tr><td colspan="5" class="text-center text-muted">No records found.</td></tr>
  211. <?php else: foreach ($animalRows as $r): ?>
  212. <tr>
  213. <td><?= $h($r['lab_no']) ?></td>
  214. <td><?= $h($r['sample_id']) ?></td>
  215. <td><?= $h($r['site_id']) ?></td>
  216. <td><?= $h($r['date_sampled']) ?></td>
  217. <td class="text-end">
  218. <a href="/dashboard/crop-analysis/animal-dietary-balance/animal-dietary-balance.php?rid=<?= (int)$r['id'] ?>&rand=<?= (float)$r['rand'] ?>"
  219. class="btn btn-sm btn-outline-success">View</a>
  220. </td>
  221. </tr>
  222. <?php endforeach; endif; ?>
  223. </tbody>
  224. </table>
  225. </div>
  226. <!-- Compost -->
  227. <div class="tab-pane fade" id="compost-reports" role="tabpanel">
  228. <h5 class="mt-2">Compost Test Records</h5>
  229. <p class="text-muted">Compost records pending migration.</p>
  230. </div>
  231. </div>
  232. </div>
  233. </div>
  234. </main>
  235. <?php include __DIR__ . '/../layouts/footer.php'; ?>
  236. </div>
  237. </div>