index.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <?php
  2. // web/index.php
  3. declare(strict_types=1);
  4. // ---- DB config from env ----
  5. $DB_HOST = getenv('MYSQL_HOST') ?: 'db';
  6. $DB_NAME = getenv('MYSQL_DATABASE') ?: 'councils';
  7. $DB_USER = getenv('MYSQL_USER') ?: 'root';
  8. $DB_PASS = getenv('MYSQL_PASSWORD') ?: getenv('MYSQL_ROOT_PASSWORD');
  9. $DB_PORT = (int)(getenv('MYSQL_PORT') ?: 3306);
  10. // ---- Connect ----
  11. $dsn = "mysql:host={$DB_HOST};port={$DB_PORT};dbname={$DB_NAME};charset=utf8mb4";
  12. $pdo = new PDO($dsn, $DB_USER, $DB_PASS, [
  13. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  14. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  15. ]);
  16. // ---- Inputs ----
  17. $q = trim((string)($_GET['q'] ?? ''));
  18. $councilKeySel = trim((string)($_GET['council_key'] ?? '')); // table name like da_meandervalley
  19. $appTypeSel = trim((string)($_GET['app_type'] ?? ''));
  20. $includeClosed = isset($_GET['include_closed']);
  21. $sort = (string)($_GET['sort'] ?? 'close'); // close|council|address|ref
  22. $knownAppTypes = [
  23. 'Residential', 'Commercial', 'Industrial', 'Subdivision',
  24. 'Rural/Agriculture', 'Tourism/Visitor Accommodation',
  25. 'Outbuilding/Shed', 'Change of Use', 'Demolition', 'Signage', 'Other',
  26. ];
  27. // ---- Discover da_* tables ----
  28. $allTables = [];
  29. $st = $pdo->query("SHOW TABLES");
  30. while ($row = $st->fetch(PDO::FETCH_NUM)) {
  31. $t = $row[0];
  32. if (strpos($t, 'da_') === 0) $allTables[] = $t;
  33. }
  34. // Exclude tables you don't want to UNION
  35. $exclude = ['geo_cache', 'da_plandata', 'da_plan_data', 'da_dorset_stages']; // add more here if needed
  36. $allTables = array_values(array_filter($allTables, fn($t) => !in_array($t, $exclude, true)));
  37. if (!$allTables) {
  38. http_response_code(200);
  39. echo "<h1>No da_* tables found</h1>";
  40. exit;
  41. }
  42. // Helpers
  43. /**
  44. * Return "`existing_col` AS `alias`" from the first column that exists,
  45. * otherwise "'' AS `alias`".
  46. */
  47. function aliasFirstExisting(PDO $pdo, string $table, array $candidates, string $alias): string {
  48. foreach ($candidates as $c) {
  49. if (tableHasColumn($pdo, $table, $c)) {
  50. return "`$c` AS `$alias`";
  51. }
  52. }
  53. return "'' AS `$alias`";
  54. }
  55. /** Abort if $table is not a safe da_* or da_*_stages identifier. */
  56. function validate_table_name(string $table): void {
  57. if (!preg_match('/\Ada_[a-z0-9_]+\z/', $table)) {
  58. http_response_code(500);
  59. exit("Invalid table name");
  60. }
  61. }
  62. function h($s) { return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }
  63. function council_name_from_key(string $k): string {
  64. $base = preg_replace('/^da_/', '', $k);
  65. $base = str_replace('_', ' ', $base);
  66. $map = [
  67. 'flinders_council' => 'Flinders',
  68. 'southernmidlands' => 'Southern Midlands',
  69. ];
  70. if (isset($map[$base])) return $map[$base];
  71. return ucwords($base);
  72. }
  73. function days_left(?string $ymd): ?int {
  74. if (!$ymd) return null;
  75. $d = DateTime::createFromFormat('Y-m-d', $ymd);
  76. if (!$d) return null;
  77. $today = new DateTime('today');
  78. return (int)$today->diff($d)->format('%r%a');
  79. }
  80. function tableHasColumn(PDO $pdo, string $table, string $col): bool {
  81. validate_table_name($table);
  82. $q = $pdo->prepare("SHOW COLUMNS FROM `{$table}` LIKE ?");
  83. $q->execute([$col]);
  84. return (bool)$q->fetch();
  85. }
  86. function fmt_date(?string $ymd, string $fmt = 'd M y'): string {
  87. if (!$ymd) return '';
  88. $dt = DateTime::createFromFormat('Y-m-d', $ymd);
  89. return $dt ? $dt->format($fmt) : '';
  90. }
  91. function tableExists(PDO $pdo, string $table): bool {
  92. $st = $pdo->prepare("SHOW TABLES LIKE ?");
  93. $st->execute([$table]);
  94. return (bool)$st->fetch(PDO::FETCH_NUM);
  95. }
  96. /** Return the first non-empty from an array of possible keys */
  97. function firstField(array $row, array $candidates, $default = null) {
  98. foreach ($candidates as $k) {
  99. if (array_key_exists($k, $row) && $row[$k] !== null && $row[$k] !== '') {
  100. return $row[$k];
  101. }
  102. }
  103. return $default;
  104. }
  105. // Build council options
  106. $councilOptions = array_map(fn($t) => ['key' => $t, 'label' => council_name_from_key($t)], $allTables);
  107. usort($councilOptions, fn($a, $b) => strcasecmp($a['label'], $b['label']));
  108. $validKeys = array_column($councilOptions, 'key');
  109. if ($councilKeySel !== '' && !in_array($councilKeySel, $validKeys, true)) {
  110. $councilKeySel = '';
  111. }
  112. // Apply table filter
  113. $tables = $councilKeySel ? [$councilKeySel] : $allTables;
  114. // ---- Prefetch *_stages into [$council_key][$council_reference] ----
  115. $stagesByRef = [];
  116. foreach ($tables as $t) {
  117. $stageT = $t . '_stages';
  118. if (!tableExists($pdo, $stageT)) continue;
  119. validate_table_name($stageT);
  120. // Pull everything (small tables) – adjust/WHERE if needed
  121. $st2 = $pdo->query("SELECT * FROM `{$stageT}`");
  122. while ($r = $st2->fetch()) {
  123. $ref = $r['council_reference'] ?? null;
  124. if (!$ref) continue;
  125. // Normalise column names from varying scrapers
  126. $milestone = firstField($r, ['milestone']);
  127. $stageDesc = firstField($r, ['stage_desc','stage','stage_description']);
  128. $opened = firstField($r, ['opened','open_date']);
  129. $target = firstField($r, ['target_date','target']);
  130. $completed = firstField($r, ['completed','completed_date']);
  131. $status = firstField($r, ['status','stage_status']);
  132. $stagesByRef[$t][$ref][] = [
  133. 'milestone' => $milestone,
  134. 'stage' => $stageDesc,
  135. 'opened' => $opened,
  136. 'target' => $target,
  137. 'completed' => $completed,
  138. 'status' => $status,
  139. ];
  140. }
  141. // Optional: sort stages by opened date (NULLs last)
  142. if (!empty($stagesByRef[$t])) {
  143. foreach ($stagesByRef[$t] as &$lst) {
  144. usort($lst, function($a,$b){
  145. $A = $a['opened'] ?: '9999-12-31';
  146. $B = $b['opened'] ?: '9999-12-31';
  147. return strcmp($A,$B);
  148. });
  149. }
  150. unset($lst);
  151. }
  152. }
  153. // Build UNION with per-table optional columns
  154. $selects = [];
  155. foreach ($tables as $t) {
  156. // description can be named differently across scrapers
  157. $descriptionExpr = aliasFirstExisting(
  158. $pdo,
  159. $t,
  160. ['description','proposal','application_details','work_description','details','brief_description','desc'],
  161. 'description'
  162. );
  163. $cols = [
  164. "'{$t}' AS council_key",
  165. $descriptionExpr,
  166. "date_received",
  167. "date_received_raw",
  168. "on_notice_to",
  169. "on_notice_to_raw",
  170. "address",
  171. "council_reference",
  172. "property_id",
  173. ];
  174. // optional columns
  175. $cols[] = tableHasColumn($pdo, $t, 'address_std') ? "address_std" : "'' AS address_std";
  176. $cols[] = tableHasColumn($pdo, $t, 'applicant') ? "applicant" : "'' AS applicant";
  177. $cols[] = tableHasColumn($pdo, $t, 'owner') ? "owner" : "'' AS owner";
  178. $cols[] = tableHasColumn($pdo, $t, 'title_reference') ? "title_reference" : "'' AS title_reference";
  179. $cols[] = tableHasColumn($pdo, $t, 'document_url') ? "COALESCE(document_url,'') AS document_url" : "'' AS document_url";
  180. $cols[] = tableHasColumn($pdo, $t, 'local_document_url') ? "COALESCE(local_document_url,'') AS local_document_url" : "'' AS local_document_url";
  181. $cols[] = tableHasColumn($pdo, $t, 'documents_json') ? "documents_json" : "NULL AS documents_json";
  182. $cols[] = tableHasColumn($pdo, $t, 'application_type') ? "application_type" : "NULL AS application_type";
  183. validate_table_name($t);
  184. $selects[] = "SELECT ".implode(", ", $cols)." FROM `{$t}`";
  185. }
  186. $sql = "SELECT * FROM (".implode(" UNION ALL ", $selects).") AS x";
  187. // Where
  188. $where = [];
  189. $params = [];
  190. if (!$includeClosed) {
  191. // show items that are still open today or later, or unknown close date
  192. $where[] = "(x.on_notice_to IS NULL OR x.on_notice_to >= CURDATE())";
  193. }
  194. if ($q !== '') {
  195. $where[] = "(x.address LIKE ? OR x.description LIKE ? OR x.council_reference LIKE ? OR x.address_std LIKE ?)";
  196. $like = "%{$q}%";
  197. array_push($params, $like, $like, $like, $like);
  198. }
  199. if ($appTypeSel !== '' && in_array($appTypeSel, $knownAppTypes, true)) {
  200. $where[] = "x.application_type = ?";
  201. $params[] = $appTypeSel;
  202. }
  203. if ($where) $sql .= " WHERE ".implode(" AND ", $where);
  204. // Sort
  205. // Sort
  206. $dir = strtolower((string)($_GET['dir'] ?? 'desc')) === 'asc' ? 'ASC' : 'DESC';
  207. switch ($sort) {
  208. case 'council':
  209. // within a council, order by date
  210. $order = "x.council_key ASC, x.date_received $dir, x.address ASC";
  211. break;
  212. case 'address':
  213. $order = "x.address $dir";
  214. break;
  215. case 'ref':
  216. $order = "x.council_reference $dir";
  217. break;
  218. case 'close':
  219. default:
  220. // primary sort by close date
  221. $order = "(x.on_notice_to IS NULL) ASC, x.on_notice_to $dir, x.council_key ASC, x.address ASC";
  222. }
  223. $sql .= " ORDER BY {$order}";
  224. // Query
  225. $st = $pdo->prepare($sql);
  226. $st->execute($params);
  227. $rows = $st->fetchAll();
  228. // Build type breakdown from results (no extra query)
  229. $typeCount = [];
  230. foreach ($rows as $r) {
  231. $t = trim((string)($r['application_type'] ?? ''));
  232. if ($t !== '') $typeCount[$t] = ($typeCount[$t] ?? 0) + 1;
  233. }
  234. arsort($typeCount);
  235. $classifiedCount = array_sum($typeCount);
  236. ?>
  237. <!doctype html>
  238. <html lang="en">
  239. <head>
  240. <meta charset="utf-8">
  241. <meta name="viewport" content="width=device-width, initial-scale=1">
  242. <title>Advertised DAs</title>
  243. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet">
  244. <style>
  245. body { padding: 24px; }
  246. header form .form-control, header form select { min-width: 220px; }
  247. .badge-council { background:#6c757d; }
  248. .badge-open { background:#198754; }
  249. .badge-today { background:#ffc107; }
  250. .badge-closed { background:#dc3545; }
  251. .badge-unknown { background:#ced4da; }
  252. .badge-apptype { background:#0d6efd; }
  253. .muted { color:#6c757d; }
  254. .accordion-button .meta { margin-left:auto; display:flex; gap:.5rem; align-items:center; }
  255. .nowrap { white-space:nowrap; }
  256. </style>
  257. </head>
  258. <body>
  259. <header class="container mb-3">
  260. <h1 class="h3 mb-3">Currently Advertised Applications</h1>
  261. <a class="btn btn-outline-secondary btn-sm" href="councils.php">Council status</a>
  262. <form method="get" class="row g-2 align-items-center">
  263. <div class="col-auto">
  264. <input type="text" name="q" class="form-control" placeholder="Search address, ref, description" value="<?=h($q)?>">
  265. </div>
  266. <div class="col-auto">
  267. <select name="council_key" class="form-select">
  268. <option value="">All councils</option>
  269. <?php foreach ($councilOptions as $opt): ?>
  270. <option value="<?= h($opt['key']) ?>" <?= $opt['key']===$councilKeySel ? 'selected' : '' ?>>
  271. <?= h($opt['label']) ?>
  272. </option>
  273. <?php endforeach; ?>
  274. </select>
  275. </div>
  276. <div class="col-auto">
  277. <select name="app_type" class="form-select">
  278. <option value="">All types</option>
  279. <?php foreach ($knownAppTypes as $at): ?>
  280. <option value="<?= h($at) ?>" <?= $at === $appTypeSel ? 'selected' : '' ?>><?= h($at) ?></option>
  281. <?php endforeach; ?>
  282. </select>
  283. </div>
  284. <div class="col-auto form-check">
  285. <input class="form-check-input" type="checkbox" name="include_closed" value="1" id="incClosed" <?= $includeClosed ? 'checked' : '' ?>>
  286. <label class="form-check-label" for="incClosed">include closed</label>
  287. </div>
  288. <div class="col-auto">
  289. <select name="sort" class="form-select">
  290. <option value="close" <?= $sort==='close'?'selected':'' ?>>sort by close date</option>
  291. <option value="council" <?= $sort==='council'?'selected':'' ?>>sort by council</option>
  292. <option value="address" <?= $sort==='address'?'selected':'' ?>>sort by address</option>
  293. <option value="ref" <?= $sort==='ref'?'selected':'' ?>>sort by reference</option>
  294. </select>
  295. </div>
  296. <div class="col-auto">
  297. <select name="dir" class="form-select">
  298. <option value="desc" <?= (($_GET['dir'] ?? '')==='desc') ? 'selected' : '' ?>>newest first</option>
  299. <option value="asc" <?= (($_GET['dir'] ?? '')!=='desc') ? 'selected' : '' ?>>oldest first</option>
  300. </select>
  301. </div>
  302. <div class="col-auto">
  303. <button type="submit" class="btn btn-primary">Apply</button>
  304. </div>
  305. </form>
  306. <div class="muted mt-2"><?= count($rows) ?> item(s)<?php if ($classifiedCount > 0): ?>, <?= $classifiedCount ?> classified<?php endif; ?></div>
  307. <?php if (!empty($typeCount)): ?>
  308. <div class="mt-2 d-flex flex-wrap gap-1 align-items-center">
  309. <?php
  310. // Build URL preserving current filters but swapping app_type
  311. $baseParams = array_filter([
  312. 'q' => $q,
  313. 'council_key' => $councilKeySel,
  314. 'sort' => $sort !== 'close' ? $sort : null,
  315. 'dir' => $dir !== 'DESC' ? strtolower($dir) : null,
  316. 'include_closed'=> $includeClosed ? '1' : null,
  317. ]);
  318. ?>
  319. <?php if ($appTypeSel !== ''): ?>
  320. <a class="btn btn-sm btn-outline-secondary" href="?<?= h(http_build_query($baseParams)) ?>">All types</a>
  321. <?php endif; ?>
  322. <?php foreach ($typeCount as $type => $n):
  323. $linkParams = $baseParams + ['app_type' => $type];
  324. $active = ($appTypeSel === $type);
  325. ?>
  326. <a class="btn btn-sm <?= $active ? 'btn-primary' : 'btn-outline-primary' ?>"
  327. href="?<?= h(http_build_query($linkParams)) ?>">
  328. <?= h($type) ?> <span class="badge <?= $active ? 'text-bg-light' : 'text-bg-primary' ?>"><?= $n ?></span>
  329. </a>
  330. <?php endforeach; ?>
  331. </div>
  332. <?php endif; ?>
  333. </header>
  334. <div class="container">
  335. <div class="accordion" id="advertisedDA">
  336. <?php foreach ($rows as $i => $r):
  337. $uid = 'acc' . $i;
  338. $cname = council_name_from_key($r['council_key']);
  339. $days = days_left($r['on_notice_to']);
  340. $closeLabel = $r['on_notice_to'] ? h($r['on_notice_to']) : h($r['on_notice_to_raw']);
  341. $statusBadge = '';
  342. if ($r['on_notice_to']) {
  343. if ($days !== null && $days < 0) {
  344. $statusBadge = '<span class="badge badge-closed">closed</span>';
  345. } elseif ($days === 0) {
  346. $statusBadge = '<span class="badge badge-today">today</span>';
  347. } elseif ($days > 0) {
  348. $statusBadge = '<span class="badge badge-open">due '.$days.'d</span>';
  349. }
  350. } else {
  351. $statusBadge = '<span class="badge badge-unknown">unknown</span>';
  352. }
  353. $appType = trim((string)($r['application_type'] ?? ''));
  354. // prefer local file, fallback to council URL
  355. $docLocal = trim((string)($r['local_document_url'] ?? ''));
  356. $docWeb = trim((string)($r['document_url'] ?? ''));
  357. $docHref = $docLocal !== '' ? $docLocal : $docWeb;
  358. // multi-document list (e.g. Launceston)
  359. $docList = [];
  360. $rawJson = $r['documents_json'] ?? '';
  361. if ($rawJson !== '' && $rawJson !== null) {
  362. $decoded = json_decode($rawJson, true);
  363. if (is_array($decoded)) {
  364. foreach ($decoded as $d) {
  365. $href = trim((string)($d['local_url'] ?? ''));
  366. if ($href === '') $href = trim((string)($d['url'] ?? ''));
  367. $name = trim((string)($d['name'] ?? ''));
  368. if ($name === '') $name = 'Document';
  369. if ($href !== '') $docList[] = ['href' => $href, 'name' => $name];
  370. }
  371. }
  372. }
  373. ?>
  374. <div class="accordion-item">
  375. <h2 class="accordion-header" id="<?= $uid ?>-head">
  376. <button class="accordion-button collapsed py-2" type="button"
  377. data-bs-toggle="collapse"
  378. data-bs-target="#<?= $uid ?>-body"
  379. aria-expanded="false"
  380. aria-controls="<?= $uid ?>-body">
  381. <div class="col-2 align-self-start"><?= h($r['council_reference'] ?: '[no ref]') ?></div>
  382. <div class="col align-self-center"><?= h(($r['address_std'] ?: $r['address']) ?: '[no address]') ?></div>
  383. <div class="col align-self-emd meta ">
  384. <div class="col"><span class="badge badge-council"><?= h($cname) ?></span></div>
  385. <?php if ($closeLabel): ?>
  386. <div class="col"><span class="badge text-bg-light border nowrap">Close <?= $closeLabel ?></span></div>
  387. <?php endif; ?>
  388. <div class="col me-1"><?= $statusBadge ?></div>
  389. <?php if ($appType !== ''): ?>
  390. <div class="col"><span class="badge badge-apptype"><?= h($appType) ?></span></div>
  391. <?php endif; ?>
  392. </div>
  393. </button>
  394. </h2>
  395. <div id="<?= $uid ?>-body" class="accordion-collapse collapse" aria-labelledby="<?= $uid ?>-head" data-bs-parent="#advertisedDA">
  396. <div class="accordion-body">
  397. <div class="row gy-2">
  398. <div class="col-md-3">
  399. <label class="form-label">Reference</label>
  400. <input type="text" class="form-control form-control-sm" value="<?= h($r['council_reference']) ?>" disabled readonly>
  401. </div>
  402. <div class="col-md-3">
  403. <label class="form-label">Title</label>
  404. <input type="text" class="form-control form-control-sm" value="<?= h($r['title_reference'] ?? '') ?>" disabled readonly>
  405. </div>
  406. <div class="col-md-3">
  407. <label class="form-label">Property ID</label>
  408. <input type="text" class="form-control form-control-sm" value="<?= h($r['property_id'] ?? '') ?>" disabled readonly>
  409. </div>
  410. <div class="col-md-3">
  411. <label class="form-label">Council</label>
  412. <input type="text" class="form-control form-control-sm" value="<?= h($cname) ?>" disabled readonly>
  413. </div>
  414. <div class="col-6">
  415. <label class="form-label">Owner</label>
  416. <input type="text" class="form-control form-control-sm" value="<?= h($r['owner'] ?? '') ?>" disabled readonly>
  417. </div>
  418. <div class="col-6">
  419. <label class="form-label">Applicant</label>
  420. <input type="text" class="form-control form-control-sm" value="<?= h($r['applicant'] ?? '') ?>" disabled readonly>
  421. </div>
  422. <div class="col-12">
  423. <label class="form-label">Address</label>
  424. <input type="text" class="form-control form-control-sm" value="<?= h(($r['address_std'] ?: $r['address']) ?? '') ?>" disabled readonly>
  425. </div>
  426. <div class="col-12">
  427. <label class="form-label">Description</label>
  428. <textarea class="form-control form-control-sm" rows="2" disabled readonly><?= h($r['description']) ?></textarea>
  429. </div>
  430. <div class="col-md-4">
  431. <label class="form-label">Received date</label>
  432. <input type="text" class="form-control form-control-sm" value="<?= h(fmt_date($r['date_received']) ?: ($r['date_received_raw'] ?? '')) ?>" disabled readonly>
  433. </div>
  434. <div class="col-md-4">
  435. <label class="form-label">Close date</label>
  436. <input type="text" class="form-control form-control-sm" value="<?= h(fmt_date($r['on_notice_to']) ?: ($r['on_notice_to_raw'] ?? '')) ?>" disabled readonly>
  437. </div>
  438. <div class="col-md-4">
  439. <label class="form-label">Days remaining</label>
  440. <input type="text" class="form-control form-control-sm" value="<?= is_null($days) ? '' : $days ?>" disabled readonly>
  441. </div>
  442. <?php if ($appType !== ''): ?>
  443. <div class="col-md-4">
  444. <label class="form-label">Application type</label>
  445. <input type="text" class="form-control form-control-sm" value="<?= h($appType) ?>" disabled readonly>
  446. </div>
  447. <?php endif; ?>
  448. <div class="col-12">
  449. <?php if (!empty($docList)): ?>
  450. <div class="d-flex flex-wrap gap-2">
  451. <?php foreach ($docList as $doc): ?>
  452. <a class="btn btn-sm btn-outline-secondary" href="<?= h($doc['href']) ?>" target="_blank" rel="noopener"><?= h($doc['name']) ?></a>
  453. <?php endforeach; ?>
  454. </div>
  455. <?php elseif ($docHref !== ''): ?>
  456. <a class="btn btn-outline-secondary" href="<?= h($docHref) ?>" target="_blank" rel="noopener">Open document</a>
  457. <?php else: ?>
  458. <span class="text-muted">No document link</span>
  459. <?php endif; ?>
  460. </div>
  461. </div>
  462. <?php
  463. $refKey = (string)$r['council_reference'];
  464. $cKey = (string)$r['council_key'];
  465. $hasStages = !empty($stagesByRef[$cKey][$refKey]);
  466. $stageRows = $hasStages ? $stagesByRef[$cKey][$refKey] : [];
  467. ?>
  468. <?php if ($hasStages): ?>
  469. <div class="col-12 mt-3">
  470. <h6 class="mb-2">Stages</h6>
  471. <div class="table-responsive">
  472. <table class="table table-sm table-bordered align-middle mb-0">
  473. <thead class="table-light">
  474. <tr>
  475. <th style="width:18%">Milestone</th>
  476. <th>Stage</th>
  477. <th style="width:12%">Opened</th>
  478. <th style="width:12%">Target</th>
  479. <th style="width:12%">Completed</th>
  480. <th style="width:14%">Status</th>
  481. </tr>
  482. </thead>
  483. <tbody>
  484. <?php foreach ($stageRows as $sr): ?>
  485. <tr>
  486. <td><?= h($sr['milestone'] ?? '') ?></td>
  487. <td><?= h($sr['stage'] ?? '') ?></td>
  488. <td class="nowrap"><?= h(fmt_date($sr['opened'] ?? null)) ?></td>
  489. <td class="nowrap"><?= h(fmt_date($sr['target'] ?? null)) ?></td>
  490. <td class="nowrap"><?= h(fmt_date($sr['completed'] ?? null)) ?></td>
  491. <td><?= h($sr['status'] ?? '') ?></td>
  492. </tr>
  493. <?php endforeach; ?>
  494. </tbody>
  495. </table>
  496. </div>
  497. </div>
  498. <?php endif; ?>
  499. </div>
  500. </div>
  501. </div>
  502. <?php endforeach; ?>
  503. </div>
  504. </div>
  505. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js"></script>
  506. </body>
  507. </html>