index.php 28 KB

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