BlueGit

timfa 66wiki

Files

66wiki / web / index.php

index.php

logo back to home page
timfa Tim · pushed by timfa · 11 months ago
8e22e661 History
index.php 4.2 KiB Download Raw
1<?php
2require_once("folderNode.php");
3require_once __DIR__ . '/templateRenderer.php';
4require_once __DIR__ . '/templateCssLinker.php';
5require_once __DIR__ . '/search.php';
6
7$renderer = new TemplateRenderer(__DIR__ . DIRECTORY_SEPARATOR . 'templates');
8
9$q = isset($_GET['q']) ? (string) $_GET['q'] : '';
10$q = trim($q);
11
12$searchResults = [];
13if ($q !== '') {
14 $searchResults = searchAllJson(__DIR__ . DIRECTORY_SEPARATOR . 'data', $q);
15} else {
16 if (isset($_GET['path'])) {
17 $html = $renderer->renderFromJsonFile($_GET['path']);
18 } else {
19 $data = loadFolderTree(__DIR__ . DIRECTORY_SEPARATOR . 'data/chapters');
20
21 $chaptersOrdered = $data->jsonRecursive();
22 usort($chaptersOrdered, fn($a, $b) => ($a->number ?? 0) <=> ($b->number ?? 0));
23
24 try {
25 $html = $renderer->renderAll($chaptersOrdered);
26 } catch (Throwable $e) {
27 $html = '<pre class="error">' . htmlspecialchars($e->getMessage()) . '</pre>';
28 }
29 }
30}
31?>
32<!DOCTYPE html>
33<html lang="en">
34
35<head>
36 <meta charset="UTF-8">
37 <title>66 Wiki</title>
38 <link rel="stylesheet" href="main.css">
39 <?php echoTemplateCssLinks(__DIR__ . DIRECTORY_SEPARATOR . 'templates', 'templates'); ?>
40</head>
41
42<body>
43
44 <div class="topBar">
45 <div class="topBarInner">
46 <a href="."><img class="headerImage" src="images/66logo.png" /></a>
47 <?php if ($q === '' && !isset($_GET['path'])): ?>
48 <div class="chapterPicker">
49 <button class="menuButton" id="chapterMenuBtn" type="button" aria-expanded="false">
50 Chapters ▾
51 </button>
52
53 <div class="chapterDropdown" id="chapterDropdown" role="menu" aria-label="Chapter list">
54 <?php foreach ($chaptersOrdered as $ch):
55 $num = $ch->number ?? '';
56 $title = $ch->title ?? '';
57 $id = 'chapter-' . $num;
58 ?>
59 <button type="button" data-target="<?= htmlspecialchars($id, ENT_QUOTES) ?>">
60 <?= htmlspecialchars("Chapter $num — $title", ENT_QUOTES) ?>
61 </button>
62 <?php endforeach; ?>
63 </div>
64 </div>
65 <?php endif; ?>
66 <form class="searchForm" method="get" action="">
67 <input class="searchInput" type="search" name="q" value="<?= htmlspecialchars($q, ENT_QUOTES) ?>"
68 placeholder="Search rules, items, proficiencies…">
69 <button class="searchSubmit" type="submit">Search</button>
70 </form>
71
72 </div>
73 </div>
74
75 <?php if ($q !== ''): ?>
76 <div class="page">
77 <h2>Search results for “<?= htmlspecialchars($q, ENT_QUOTES) ?>”</h2>
78
79 <?php if (!$searchResults): ?>
80 <p>No matches found.</p>
81 <?php else: ?>
82 <div>
83 <?php foreach ($searchResults as $r): ?>
84 <article class="searchResultArticle">
85 <div class="searchResult">
86 <strong><a
87 href="<?= "?path=" . DIRECTORY_SEPARATOR . "data" . htmlspecialchars($r['file'], ENT_QUOTES) ?>"><?= htmlspecialchars($r['name'] ?: '(no name)', ENT_QUOTES) ?></a></strong>
88 <small>type: <?= htmlspecialchars($r['type'], ENT_QUOTES) ?></small>
89 </div>
90
91 <ul>
92 <?php foreach (array_slice($r['hits'], 0, 5) as $hit): ?>
93 <li>
94 <code><?= $hit['field'] ?></code>:
95 <?= $hit['snippet'] ?>
96 </li>
97 <?php endforeach; ?>
98 </ul>
99 </article>
100 <?php endforeach; ?>
101 </div>
102 <?php endif; ?>
103 </div>
104 <?php else: ?>
105 <content>
106 <?= $html ?>
107 </content>
108 <?php endif; ?>
109
110 <script src="chapterScroll.js"></script>
111</body>
112
113</html>