diff options
author | Jonas Kohl | 2024-09-17 14:51:23 +0200 |
---|---|---|
committer | Jonas Kohl | 2024-09-17 14:51:23 +0200 |
commit | a65d424263adfbff9629c7d91a613e4504c84613 (patch) | |
tree | 7d89c6d85168427782ae8c24625db14df90e376a /src/application | |
parent | f6dd78734f86f8daed7c5d472e7a199301095ff8 (diff) |
Add search
Diffstat (limited to 'src/application')
-rw-r--r-- | src/application/i18n.php | 2 | ||||
-rw-r--r-- | src/application/messages/de.msg | 20 | ||||
-rw-r--r-- | src/application/mystic/forum/Database.php | 27 | ||||
-rw-r--r-- | src/application/views/form_search.php | 28 | ||||
-rw-r--r-- | src/application/views/nav_guest.php | 1 | ||||
-rw-r--r-- | src/application/views/nav_logged_in.php | 1 | ||||
-rw-r--r-- | src/application/views/view_search_results.php | 33 |
7 files changed, 111 insertions, 1 deletions
diff --git a/src/application/i18n.php b/src/application/i18n.php index a1f2f24..a7bdd82 100644 --- a/src/application/i18n.php +++ b/src/application/i18n.php @@ -173,7 +173,7 @@ function i18n_get(string $msgid, array $params = [], ?string $context = null): s if ($context !== null) $key = $context . "\x7F" . $msgid; - $msg = ($__i18n_msg_store[$__i18n_current_locale] ?? [])[$msgid] ?? $key; + $msg = ($__i18n_msg_store[$__i18n_current_locale] ?? [])[$key] ?? $msgid; uksort($params, fn(string $a, string $b): int => strlen($b) <=> strlen($a)); return str_replace( diff --git a/src/application/messages/de.msg b/src/application/messages/de.msg index ed8178e..784c257 100644 --- a/src/application/messages/de.msg +++ b/src/application/messages/de.msg @@ -5,6 +5,14 @@ metadata({ } }) +: "." +? "Number formatting" += "," + +: "," +? "Number formatting" += "." + : "Log in" = "Anmelden" @@ -318,3 +326,15 @@ metadata({ : "Language:" = "Sprache:" + +: "Search" += "Suche" + +: "Search results for “%query%”" += "Suchergebnisse für „%query%“" + +: "No results for this search" += "Keine Ergebnisse für diese Suche" + +: "%result_count% result(s) in %search_duration% second(s)" += "%result_count% Treffer in %search_duration% Sekunde(n)" diff --git a/src/application/mystic/forum/Database.php b/src/application/mystic/forum/Database.php index a1e67ea..8ebc36b 100644 --- a/src/application/mystic/forum/Database.php +++ b/src/application/mystic/forum/Database.php @@ -414,6 +414,33 @@ class Database { return $items; } + public function execCustomQuery(string $query, ?array $customQueryParams = null, ?string $entityClassName = null): array { + if ($customQueryParams === null) + $result = \pg_query($this->connection, $query); + else + $result = \pg_query_params($this->connection, $query, $customQueryParams); + if ($result === false) + throw new \RuntimeException("Query failed: " . \pg_last_error($this->connection)); + $cols = null; + if ($entityClassName !== null) { + $reflClass = new ReflectionClass($entityClassName); + $cols = self::getColumns($reflClass); + } + $rowsOrItems = []; + while (($row = \pg_fetch_assoc($result)) !== false) { + if ($entityClassName !== null) { + $entity = new $entityClassName(); + foreach ($cols as $colName => $colProps) + self::assignValue($entity, $colProps, $row[$colName]); + $rowsOrItems []= $entity; + } else { + $rowsOrItems []= $row; + } + } + \pg_free_result($result); + return $rowsOrItems; + } + public function delete(Entity &$entity): bool { $entityClassName = get_class($entity); $tableName = self::getTableName($entityClassName); diff --git a/src/application/views/form_search.php b/src/application/views/form_search.php new file mode 100644 index 0000000..edc68b8 --- /dev/null +++ b/src/application/views/form_search.php @@ -0,0 +1,28 @@ +<?php + +use mystic\forum\utils\RequestUtils; + +$lastFormUri = ""; +$lastForm = RequestUtils::getLastForm($lastFormUri) ?? []; +if ($lastFormUri !== $_SERVER["REQUEST_URI"]) $lastForm = []; +RequestUtils::clearLastForm(); + +?> +<div class="page-header margin-top-0"> + <h1><?= __("Search") ?></h1> +</div> +<?php +if (($_formError = RequestUtils::getAndClearFormError()) !== null) { + _view("alert_error", ["message" => $_formError]); +} +?> +<form action="<?= htmlentities($_SERVER["REQUEST_URI"]) ?>" method="post"> + <div class="form-group"> + <div class="input-group"> + <input class="form-control" type="search" id="i_query" name="query" value="<?= htmlentities($lastForm["query"] ?? $query ?? "") ?>" required autofocus> + <div class="input-group-btn"> + <button class="btn btn-primary" type="submit"><?= __("Search") ?></button> + </div> + </div> + </div> +</form> diff --git a/src/application/views/nav_guest.php b/src/application/views/nav_guest.php index 1c65a50..88c551b 100644 --- a/src/application/views/nav_guest.php +++ b/src/application/views/nav_guest.php @@ -1,4 +1,5 @@ <ul class="nav navbar-nav navbar-right"> +<li<?= $GLOBALS["action"] === "search" ? ' class="active"' : '' ?>><a href="?_action=search"><span class="glyphicon glyphicon-search" aria-hidden="true"></span><span class="sr-only"><?= __("Search") ?></span></a></li> <li<?= $GLOBALS["action"] === "auth" ? ' class="active"' : '' ?>><a href="?_action=auth&next=<?= htmlentities(urlencode($_SERVER["REQUEST_URI"])) ?>"><?= __("Log in") ?></a></li> <?php if (REGISTRATION_ENABLED): ?> <li<?= $GLOBALS["action"] === "register" ? ' class="active"' : '' ?>><a href="?_action=register&next=<?= htmlentities(urlencode($_SERVER["REQUEST_URI"])) ?>"><?= __("Register") ?></a></li> diff --git a/src/application/views/nav_logged_in.php b/src/application/views/nav_logged_in.php index 39c65cb..0f77f90 100644 --- a/src/application/views/nav_logged_in.php +++ b/src/application/views/nav_logged_in.php @@ -6,6 +6,7 @@ use mystic\forum\orm\User; "user" => ($user->id === User::SUPERUSER_ID) ? ('<strong class="text-danger">' . htmlentities($user->displayName) . '</strong>') : ('<strong>' . htmlentities($user->displayName) . '</strong>') ]) ?> </p></li> +<li<?= $GLOBALS["action"] === "search" ? ' class="active"' : '' ?>><a href="?_action=search"><span class="glyphicon glyphicon-search" aria-hidden="true"></span><span class="sr-only"><?= __("Search") ?></span></a></li> <li><a href="?_action=viewuser&user=<?= htmlentities(urlencode($user->id)) ?>"><span class="glyphicon glyphicon-user" aria-hidden="true"></span><span class="sr-only">View profile</span></a></li> <li><a href="?_action=logout&next=<?= htmlentities(urlencode($_SERVER["REQUEST_URI"])) ?>"><span class="glyphicon glyphicon-log-out" aria-hidden="true"></span><span class="sr-only">Log out</span></a></li> </ul> diff --git a/src/application/views/view_search_results.php b/src/application/views/view_search_results.php new file mode 100644 index 0000000..19a6978 --- /dev/null +++ b/src/application/views/view_search_results.php @@ -0,0 +1,33 @@ +<?php +use mystic\forum\utils\StringUtils; +?> + +<?php if (count($posts) > 0): ?> + <p><?= __("%result_count% result(s) in %search_duration% second(s)", [ + "result_count" => count($posts), + "search_duration" => number_format($search_duration, 2, __(".", context: "Number formatting"), __(",", context: "Number formatting")), + ]) ?></p> + <div class="list-group margin-top"> + <?php foreach ($posts as $post): + if ($post->deleted) continue; + $hasAttachments = count($attachments[$post->id]) > 0; + ?> + <a href="?_action=viewtopic&topic=<?= htmlentities(urlencode($post->topicId)) ?>#post-<?= htmlentities(urlencode($post->id)) ?>" class="list-group-item"> + <?php if ($hasAttachments): ?> + <span class="badge"><span class="glyphicon glyphicon-paperclip"></span></span> + <?php endif; ?> + <?= htmlentities(StringUtils::truncate(strip_tags(renderPost($post->content)), 100)) ?><br> + <span class="text-muted"><?= __("posted by %author% on %post_date% in %topic%", [ + "author" => '<em>' . htmlentities($users[$post->authorId]?->displayName ?? __("unknown")) . '</em>', + "post_date" => '<span class="_time">' . htmlentities($post->postDate->format("c")) . '</span>', + "topic" => '<em>' . htmlentities($topics[$post->topicId]?->title ?? "unknown") . '</em>', + ]) ?></span> + </a> + <?php endforeach; ?> + </div> +<?php else: ?> + <div class="well icon-well text-info margin-top margin-bottom"> + <span class="glyphicon glyphicon-info-sign color-info" aria-hidden="true"></span> + <em><?= __("No results for this search") ?></em> + </div> +<?php endif; ?> |