inbox.php 14 KB

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