| 1 | <?php |
| 2 | require_once("folderNode.php"); |
| 3 | require_once __DIR__ . '/templateRenderer.php'; |
| 4 | require_once __DIR__ . '/templateCssLinker.php'; |
| 5 | require_once __DIR__ . '/search.php'; |
| 6 | |
| 7 | $renderer = new TemplateRenderer(__DIR__ . DIRECTORY_SEPARATOR . 'templates'); |
| 8 | $isChapterOverview = false; |
| 9 | $q = isset($_GET['q']) ? (string) $_GET['q'] : ''; |
| 10 | $q = trim($q); |
| 11 | |
| 12 | $searchResults = []; |
| 13 | if ($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 | $chapNum = isset($_GET['ch']) ? $_GET['ch'] : 1; |
| 20 | |
| 21 | $data = loadFolderTree(__DIR__ . DIRECTORY_SEPARATOR . 'data/chapters'); |
| 22 | |
| 23 | $chaptersOrdered = $data->jsonRecursive(); |
| 24 | usort($chaptersOrdered, fn($a, $b) => ($a->number ?? 0) <=> ($b->number ?? 0)); |
| 25 | |
| 26 | try { |
| 27 | if ($chapNum === "all") { // for PDF rendering to be implemented later maybe |
| 28 | $html = $renderer->renderAll($chaptersOrdered); |
| 29 | } else { |
| 30 | $isChapterOverview = true; |
| 31 | $array = json_decode(json_encode($chaptersOrdered[$chapNum - 1]), true); |
| 32 | $html = $renderer->render($array); |
| 33 | } |
| 34 | } catch (Throwable $e) { |
| 35 | $html = '<pre class="error">' . htmlspecialchars($e->getMessage()) . '</pre>'; |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | ?> |
| 40 | <!DOCTYPE html> |
| 41 | <html lang="en"> |
| 42 | |
| 43 | <head> |
| 44 | <meta charset="UTF-8"> |
| 45 | <title>66 Wiki</title> |
| 46 | <link rel="stylesheet" href="main.css"> |
| 47 | <link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png"> |
| 48 | <link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png"> |
| 49 | <link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png"> |
| 50 | <link rel="manifest" href="site.webmanifest"> |
| 51 | <?php echoTemplateCssLinks(__DIR__ . DIRECTORY_SEPARATOR . 'templates', 'templates'); ?> |
| 52 | </head> |
| 53 | |
| 54 | <body> |
| 55 | |
| 56 | <div class="topBar"> |
| 57 | <div class="topBarInner"> |
| 58 | <a href="."><img class="headerImage" src="images/66logo.png" /></a> |
| 59 | <?php if ($q === '' && !isset($_GET['path'])): ?> |
| 60 | <div class="chapterPicker"> |
| 61 | <button class="menuButton" id="chapterMenuBtn" type="button" aria-expanded="false"> |
| 62 | Chapters ▾ |
| 63 | </button> |
| 64 | |
| 65 | <div class="chapterDropdown" id="chapterDropdown" role="menu" aria-label="Chapter list"> |
| 66 | <?php foreach ($chaptersOrdered as $ch): |
| 67 | $num = $ch->number ?? ''; |
| 68 | $title = $ch->title ?? ''; |
| 69 | $id = 'chapter-' . $num; |
| 70 | ?> |
| 71 | <a href="?ch=<?= $ch->number ?>"> |
| 72 | <button type="button"> |
| 73 | <?= htmlspecialchars("Chapter $num — $title", ENT_QUOTES) ?> |
| 74 | </button> |
| 75 | </a> |
| 76 | <?php endforeach; ?> |
| 77 | </div> |
| 78 | </div> |
| 79 | <?php endif; ?> |
| 80 | <form class="searchForm" method="get" action=""> |
| 81 | <input class="searchInput" type="search" name="q" value="<?= htmlspecialchars($q, ENT_QUOTES) ?>" |
| 82 | placeholder="Search rules, items, proficiencies…"> |
| 83 | <button class="searchSubmit" type="submit">Search</button> |
| 84 | </form> |
| 85 | |
| 86 | </div> |
| 87 | </div> |
| 88 | |
| 89 | <?php if ($q !== ''): ?> |
| 90 | <div class="page"> |
| 91 | <h2>Search results for “<?= htmlspecialchars($q, ENT_QUOTES) ?>”</h2> |
| 92 | |
| 93 | <?php if (!$searchResults): ?> |
| 94 | <p>No matches found.</p> |
| 95 | <?php else: ?> |
| 96 | <div> |
| 97 | <?php foreach ($searchResults as $r): ?> |
| 98 | <article class="searchResultArticle"> |
| 99 | <div class="searchResult"> |
| 100 | <strong><a |
| 101 | href="<?= "?path=" . DIRECTORY_SEPARATOR . "data" . htmlspecialchars($r['file'], ENT_QUOTES) ?>"><?= htmlspecialchars($r['name'] ?: '(no name)', ENT_QUOTES) ?></a></strong> |
| 102 | <small>type: <?= htmlspecialchars($r['type'], ENT_QUOTES) ?></small> |
| 103 | </div> |
| 104 | |
| 105 | <ul> |
| 106 | <?php foreach (array_slice($r['hits'], 0, 5) as $hit): ?> |
| 107 | <li> |
| 108 | <code><?= $hit['field'] ?></code>: |
| 109 | <?= $hit['snippet'] ?> |
| 110 | </li> |
| 111 | <?php endforeach; ?> |
| 112 | </ul> |
| 113 | </article> |
| 114 | <?php endforeach; ?> |
| 115 | </div> |
| 116 | <?php endif; ?> |
| 117 | </div> |
| 118 | <?php else: ?> |
| 119 | <content> |
| 120 | <?= $html ?> |
| 121 | <?php if ($isChapterOverview): ?> |
| 122 | <center> |
| 123 | <a href="?ch=<?= $chapNum + 2 ?>"><button class="menuButton nextChapterButton">Next Chapter</button></a> |
| 124 | </center> |
| 125 | <?php endif; ?> |
| 126 | </content> |
| 127 | <?php endif; ?> |
| 128 | |
| 129 | <script src="chapterScroll.js"></script> |
| 130 | <script src="inlineJump.js"></script> |
| 131 | </body> |
| 132 | |
| 133 | </html> |