|
| 1 | +<?php |
| 2 | +namespace BNETDocs\Templates\Search; |
| 3 | +$title = 'Search'; |
| 4 | +$results = $this->getContext()->results; |
| 5 | +$user_input = $this->getContext()->user_input ?? ''; |
| 6 | +if ($results) |
| 7 | +{ |
| 8 | + $sections = [ |
| 9 | + 'Comments' => $results->getComments(), |
| 10 | + 'Documents' => $results->getDocuments(), |
| 11 | + 'News Posts' => $results->getNewsPosts(), |
| 12 | + 'Packets' => $results->getPackets(), |
| 13 | + 'Servers' => $results->getServers(), |
| 14 | + 'Users' => $results->getUsers(), |
| 15 | + ]; |
| 16 | +} |
| 17 | +require('./Includes/header.inc.phtml'); ?> |
| 18 | +<div class="container"> |
| 19 | +<?php if (!$results instanceof \BNETDocs\Libraries\Search\Results || $results->isEmpty()): ?> |
| 20 | + <h1 class="mt-4 mb-3">No Results</h1> |
| 21 | + <p class="text-muted">Your search for <code><?= filter_var($user_input, FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?></code> returned no results.</p> |
| 22 | +<?php else: |
| 23 | + $nonEmptyCount = 0; |
| 24 | + foreach ($sections as $items) { |
| 25 | + if (!empty($items)) $nonEmptyCount++; |
| 26 | + } |
| 27 | + $collapse_all_by_default = ($nonEmptyCount >= 2); |
| 28 | +?> |
| 29 | + <h1 class="mt-4 mb-3">Search Results</h1> |
| 30 | + <p class="text-muted">Query: <code><?= filter_var($user_input, FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?></code></p> |
| 31 | + |
| 32 | + <div class="mb-3"> |
| 33 | + <button class="btn btn-sm btn-outline-secondary mr-2" id="expand-all">Expand All</button> |
| 34 | + <button class="btn btn-sm btn-outline-secondary" id="collapse-all">Collapse All</button> |
| 35 | + </div> |
| 36 | + |
| 37 | + <div id="searchAccordion"> |
| 38 | + <?php foreach ($sections as $label => $items): ?> |
| 39 | + <?php if (!empty($items)): ?> |
| 40 | + <?php $collapseId = strtolower(str_replace(' ', '-', $label)); ?> |
| 41 | + <div class="card mb-3"> |
| 42 | + <div class="card-header" id="heading-<?= $collapseId ?>"> |
| 43 | + <h5 class="mb-0"> |
| 44 | + <button class="btn btn-link text-white" data-toggle="collapse" data-target="#collapse-<?= $collapseId ?>" aria-expanded="<?= $collapse_all_by_default ? 'false' : 'true' ?>" aria-controls="collapse-<?= $collapseId ?>"> |
| 45 | + <?= $label ?> (<?= count($items) ?>) |
| 46 | + </button> |
| 47 | + </h5> |
| 48 | + </div> |
| 49 | + <div id="collapse-<?= $collapseId ?>" class="collapse<?= $collapse_all_by_default ? '' : ' show' ?>" aria-labelledby="heading-<?= $collapseId ?>" data-parent="#searchAccordion"> |
| 50 | + <div class="card-body p-0"> |
| 51 | + <?php if ($label === 'Comments'): ?> |
| 52 | + <?php |
| 53 | + $comments = $items; |
| 54 | + $comment_show_parent = true; |
| 55 | + require('./Comment/Section.inc.phtml'); |
| 56 | + ?> |
| 57 | + <?php else: ?> |
| 58 | + <ul class="list-group list-group-flush"> |
| 59 | + <?php foreach ($items as $item): ?> |
| 60 | + <li class="list-group-item"> |
| 61 | + <?php |
| 62 | + $url = '#'; |
| 63 | + $title = 'Untitled'; |
| 64 | + $preview = ''; |
| 65 | + $tags = []; |
| 66 | + |
| 67 | + switch (true) { |
| 68 | + case $item instanceof \BNETDocs\Libraries\Document: |
| 69 | + $url = $item->getURI(); |
| 70 | + $title = $item->getTitle(); |
| 71 | + $preview = $item->getBrief(true) ?: $item->getContent(true); |
| 72 | + $tags = $item->getTags(); |
| 73 | + break; |
| 74 | + |
| 75 | + case $item instanceof \BNETDocs\Libraries\News\Post: |
| 76 | + $url = $item->getURI(); |
| 77 | + $title = $item->getTitle(); |
| 78 | + $preview = $item->getContent(true); |
| 79 | + $tags = $item->getTags(); |
| 80 | + break; |
| 81 | + |
| 82 | + case $item instanceof \BNETDocs\Libraries\Packet\Packet: |
| 83 | + $url = $item->getURI(); |
| 84 | + $title = $item->getLabel(); |
| 85 | + $preview = $item->getFormat() ?: $item->getRemarks(true); |
| 86 | + $tags = $item->getTags(); |
| 87 | + break; |
| 88 | + |
| 89 | + case $item instanceof \BNETDocs\Libraries\Server\Server: |
| 90 | + $url = $item->getURI(); |
| 91 | + $title = $item->getLabel() . ' (' . $item->getAddress() . ':' . $item->getPort() . ')'; |
| 92 | + $tags = $item->getTags(); |
| 93 | + break; |
| 94 | + |
| 95 | + case $item instanceof \BNETDocs\Libraries\User\User: |
| 96 | + $url = $item->getURI(); |
| 97 | + $title = $item->getName(); |
| 98 | + $tags = $item->getTags(); |
| 99 | + break; |
| 100 | + |
| 101 | + default: |
| 102 | + $title = 'ID ' . $item->getId(); |
| 103 | + break; |
| 104 | + } |
| 105 | + |
| 106 | + $titleSafe = filter_var($title, FILTER_SANITIZE_FULL_SPECIAL_CHARS); |
| 107 | + $previewSafe = filter_var(mb_strimwidth(strip_tags($preview), 0, 200, '…'), FILTER_SANITIZE_FULL_SPECIAL_CHARS); |
| 108 | + ?> |
| 109 | + <a href="<?= $url ?>"><strong><?= $titleSafe ?></strong></a> |
| 110 | + <?php if (!empty($previewSafe)): ?> |
| 111 | + <div class="text-muted small mt-1"><?= $previewSafe ?></div> |
| 112 | + <?php endif; ?> |
| 113 | + <?php if (!empty($tags)): ?> |
| 114 | + <div class="mt-2"> |
| 115 | + <?php foreach ($tags as $tag): ?> |
| 116 | + <span class="badge badge-secondary"><?= filter_var($tag, FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?></span> |
| 117 | + <?php endforeach; ?> |
| 118 | + </div> |
| 119 | + <?php endif; ?> |
| 120 | + </li> |
| 121 | + <?php endforeach; ?> |
| 122 | + </ul> |
| 123 | + <?php endif; ?> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + <?php endif; ?> |
| 128 | + <?php endforeach; ?> |
| 129 | + </div> |
| 130 | +<?php endif; ?> |
| 131 | +</div> |
| 132 | + |
| 133 | +<script> |
| 134 | + document.addEventListener('DOMContentLoaded', function () { |
| 135 | + document.getElementById('expand-all').addEventListener('click', function () { |
| 136 | + document.querySelectorAll('#searchAccordion .collapse').forEach(el => { |
| 137 | + if (!el.classList.contains('show')) $(el).collapse('show'); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + document.getElementById('collapse-all').addEventListener('click', function () { |
| 142 | + document.querySelectorAll('#searchAccordion .collapse').forEach(el => { |
| 143 | + if (el.classList.contains('show')) $(el).collapse('hide'); |
| 144 | + }); |
| 145 | + }); |
| 146 | + }); |
| 147 | +</script> |
| 148 | +<?php require('./Includes/footer.inc.phtml'); ?> |
0 commit comments