commit f8c07a943928414189e31ff16c427bc76609a898
parent 780ea87f2f8f186c9f8d159eb16b04527eba7325
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date: Sat, 26 Oct 2024 19:21:20 +0200
feat: implement search function
Diffstat:
4 files changed, 237 insertions(+), 0 deletions(-)
diff --git a/config/i18n.php b/config/i18n.php
@@ -23,6 +23,9 @@ if ($lang == "en") {
$GLOBALS["i18n_cookie"] = "Keep me logged in! (this will set a cookie)";
$GLOBALS["i18n_cookie_session"] = "No, only temporarily";
$GLOBALS["i18n_cookie_long"] = "Yes, keep me logged in for 30 days";
+ $GLOBALS["i18n_search"] = "Search";
+ $GLOBALS["i18n_search_placeholder"] = "Search your notes...";
+ $GLOBALS["i18n_matches"] = "Matches";
}
if ($lang == "de") {
$GLOBALS["i18n_note"] = "Notiz";
@@ -42,6 +45,9 @@ if ($lang == "de") {
$GLOBALS["i18n_cookie"] = "Eingelogged bleiben (das setzt einen Cookie)";
$GLOBALS["i18n_cookie_session"] = "Nein, nur temporär";
$GLOBALS["i18n_cookie_long"] = "Ja, lass mich für 30 Tage eingelogged";
+ $GLOBALS["i18n_search"] = "Suchen";
+ $GLOBALS["i18n_search_placeholder"] = "Notizen durchsuchen...";
+ $GLOBALS["i18n_matches"] = "Funde";
}
diff --git a/index.php b/index.php
@@ -20,6 +20,7 @@
$filename = validate_input_string(gather_post("filename"));
$filepath_t1 = gather_post("filepath_t1");
$content = gather_post("content");
+ $query = gather_post("query");
$user = validate_input_string(gather_post("user"));
$pass = gather_post("pass");
$token = ""; // initilise empty
@@ -135,6 +136,9 @@
<!-- Headline -->
<h1>Rememori</h1>
+ <!-- Search bar -->
+ <?php search_bar($query); ?>
+
</header>
<?php
@@ -168,6 +172,14 @@
);
}
+ /* Search action */
+ if ($action == "search") {
+ search_notes(
+ $user,
+ $query
+ );
+ }
+
?>
</body>
diff --git a/lib/helpers.php b/lib/helpers.php
@@ -33,6 +33,11 @@ function sort_by_time(array $a, array $b) {
return $b["time"] - $a["time"];
}
+function sort_by_matches(array $a, array $b) {
+ // sort from most to fewest (reversed)
+ return $b["matches"] - $a["matches"];
+}
+
function link_in_first_line(string $filepath) {
/* read first line of file */
diff --git a/lib/search.php b/lib/search.php
@@ -0,0 +1,214 @@
+<?php
+
+function search_bar(string $query) {
+
+?>
+
+<form action="/" method="post" class="inline">
+ <input
+ id="query"
+ name="query"
+ type="text"
+ placeholder="<?php echo $GLOBALS["i18n_search_placeholder"]; ?>"
+ value="<?php echo $query; ?>"
+ >
+ <input
+ id="action"
+ name="action"
+ type="hidden"
+ value="search"
+ >
+ <input
+ type="submit"
+ value="<?php echo $GLOBALS["i18n_search"]; ?>"
+ >
+</form>
+
+<?php
+}
+
+function search_notes(
+ string $user,
+ string $query
+) {
+
+ /* gather all notes of the user */
+ $filenames = gather_notes($user);
+
+ /* split query into words */
+ $query = explode(" ", $query);
+
+ /* loop through each file */
+ for ($i = 0; $i < count($filenames); $i++) {
+
+ $filename = $filenames[$i];
+
+ /* read file content. also add filename and link */
+ $content = read_note(
+ $user,
+ $filename["category"],
+ $filename["name"] . ".txt"
+ ) . " " . $filename["name"]. " " . $filename["link"];
+
+ /* collect number of matches */
+ $matches = 0;
+ foreach ($query as $word) {
+ $matches = $matches + preg_match('/' . $word . '/i', $content);
+ }
+
+ /* add number of matches to the filesnames array */
+ $filenames[$i]["matches"] = $matches;
+
+ }
+
+ /* Sort filenames by number of matches */
+ usort($filenames, "sort_by_matches");
+
+ /* loop through each filename and draw a table row */
+ foreach ($filenames as $filename) {
+
+ /* if there are no matches, skip it! */
+ if ($filename["matches"] < 1) {
+ continue;
+ }
+?>
+
+ <!-- note list item -->
+ <div
+ id="<?php echo $filename["category"] . "_" . $filename["name"]; ?>"
+ class="notes_list_item"
+ >
+
+<?php
+ /* if note starts with an URL, link it in the title
+ * otherwise just print the title */
+ if ($filename["link"] !== false) {
+?>
+ <a href="<?php echo $filename["link"]; ?>" target="_blank">
+ <strong><?php echo htmlentities($filename["name"]); ?></strong>
+ </a><br>
+
+<?php } else { ?>
+ <strong><?php echo htmlentities($filename["name"]); ?></strong><br>
+<?php } ?>
+
+ <!-- matches marker -->
+ <span class="inline">
+ <?php echo $filename["matches"] . " " . $GLOBALS["i18n_matches"]; ?>
+ </span>
+
+ <!-- separator -->
+ <span class="inline">|</span>
+
+ <!-- date marker -->
+ <span class="inline">
+ <?php echo date("Y-m-d H:i:s", $filename["time"]); ?>
+ </span>
+
+ <!-- separator -->
+ <span class="inline">|</span>
+
+ <!-- category button -->
+ <form action="/" target="_self" method="post" class="inline">
+ <input
+ id="category"
+ name="category"
+ type="hidden"
+ value="<?php echo $filename["category"]; ?>"
+ >
+ <input
+ id="action"
+ name="action"
+ type="hidden"
+ value="list"
+ >
+ <input
+ type="submit"
+ class="likeanchor"
+ value="<?php echo htmlentities($filename["category"]); ?>"
+ >
+ </form>
+
+ <!-- separator -->
+ <span class="inline">|</span>
+
+ <!-- edit button -->
+ <form action="/" method="post" class="inline">
+ <input
+ id="category"
+ name="category"
+ type="hidden"
+ value="<?php echo $filename["category"]; ?>"
+ >
+ <input
+ id="filename"
+ name="filename"
+ type="hidden"
+ value="<?php echo $filename["name"] . ".txt"; ?>"
+ >
+ <input
+ id="action"
+ name="action"
+ type="hidden"
+ value="show"
+ >
+ <input
+ type="submit"
+ class="likeanchor"
+ value="<?php echo $GLOBALS["i18n_edit"]; ?>"
+ >
+ </form>
+
+ <!-- separator -->
+ <span class="inline">|</span>
+
+ <!-- delete button -->
+<?php
+ $del_id = hash(
+ "sha256",
+ $user . "/" . $filename["category"] . "/" . $filename["name"],
+ false
+ );
+?>
+ <form
+ action="/"
+ method="post"
+ id="<?php echo $del_id; ?>"
+ class="inline"
+ >
+ <input
+ id="category"
+ name="category"
+ type="hidden"
+ value="<?php echo $filename["category"]; ?>"
+ >
+ <input
+ id="filename"
+ name="filename"
+ type="hidden"
+ value="<?php echo $filename["name"] . ".txt"; ?>"
+ >
+ <input
+ id="action"
+ name="action"
+ type="hidden"
+ value="delete"
+ >
+ <input
+ class="danger likeanchor"
+ type="button"
+ value="<?php echo $GLOBALS["i18n_delete"]; ?>"
+ onclick="deletion_confirm('<?php echo $del_id; ?>');"
+ >
+ </form>
+
+ </div>
+
+<?php
+
+ } // foreach-loop
+
+} // function
+
+?>
+