commit a6ecc1ae9c7c3063ac66edcdb0cf4fe209439c9b
parent cbe614dcedc90379bd7638939eeeedeba51455cd
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date: Sun, 27 Oct 2024 14:37:10 +0100
feat: implement list of categories as its own page and make it "home"
Diffstat:
6 files changed, 136 insertions(+), 74 deletions(-)
diff --git a/assets/css/custom.css b/assets/css/custom.css
@@ -15,7 +15,7 @@ textarea#content {
}
/* Ensure the notes list items do not overflow and break properly */
-.notes_list_item {
+.list_item {
overflow: hidden;
word-break: break-all;
margin-bottom: 1em;
diff --git a/config/i18n.php b/config/i18n.php
@@ -8,12 +8,13 @@ $lang = in_array($lang, $acceptLang) ? $lang : "en";
if ($lang == "en") {
$GLOBALS["i18n_development"] = "Sourcecode";
$GLOBALS["i18n_note"] = "Note";
+ $GLOBALS["i18n_notes"] = "Notes";
$GLOBALS["i18n_category"] = "Category";
$GLOBALS["i18n_filename"] = "File name";
$GLOBALS["i18n_save"] = "Save note";
$GLOBALS["i18n_edit"] = "Edit note";
$GLOBALS["i18n_new"] = "New note";
- $GLOBALS["i18n_list"] = "All";
+ $GLOBALS["i18n_list"] = "All notes";
$GLOBALS["i18n_related"] = "Related Notes";
$GLOBALS["i18n_login"] = "Login";
$GLOBALS["i18n_user"] = "Username";
@@ -34,6 +35,7 @@ if ($lang == "en") {
if ($lang == "de") {
$GLOBALS["i18n_development"] = "Quellcode";
$GLOBALS["i18n_note"] = "Notiz";
+ $GLOBALS["i18n_notes"] = "Notizen";
$GLOBALS["i18n_category"] = "Kategorie";
$GLOBALS["i18n_categories"] = "Kategorien";
$GLOBALS["i18n_filename"] = "Dateiname";
@@ -41,7 +43,7 @@ if ($lang == "de") {
$GLOBALS["i18n_edit"] = "Ändern";
$GLOBALS["i18n_delete"] = "Löschen";
$GLOBALS["i18n_new"] = "Neue Notiz";
- $GLOBALS["i18n_list"] = "Alle";
+ $GLOBALS["i18n_list"] = "Alle Notizen";
$GLOBALS["i18n_related"] = "Ähnliche Notizen";
$GLOBALS["i18n_login"] = "Login";
$GLOBALS["i18n_logout"] = "Logout";
diff --git a/index.php b/index.php
@@ -38,9 +38,9 @@
$token = explode("|", $_COOKIE["session"])[1];
}
- /* if no action is given, set it to "list" initially */
+ /* if no action is given, set it to "categories" initially */
if (empty($action)) {
- $action = "list";
+ $action = "categories";
}
/* Check authentification of user */
@@ -170,10 +170,13 @@
/* Listing action */
if ($action == "list") {
- category_menu($user);
list_notes($user, $category);
}
+ if ($action == "categories") {
+ list_categories($user);
+ }
+
/* Show action */
if ($action == "show") {
show_note(
diff --git a/lib/list.php b/lib/list.php
@@ -11,6 +11,23 @@ function list_notes(
/* Sort filenames by edit timestamp */
usort($filenames, "sort_by_time");
+ /* prepare headline content */
+ if (empty($category)) {
+ $category_head = $GLOBALS["i18n_list"];
+ } else {
+ $category_head = $category;
+ }
+ $category_head = $category_head . " (" . count($filenames) . " " .
+ $GLOBALS["i18n_notes"] . ")";
+
+?>
+
+ <!-- Headline -->
+ <h4><?php echo $GLOBALS["i18n_category"] . ": " . $category_head; ?></h4>
+
+<?php
+
+
/* loop through each filename and draw a table row */
foreach ($filenames as $filename) {
?>
@@ -18,7 +35,7 @@ function list_notes(
<!-- note list item -->
<div
id="<?php echo $filename["category"] . "_" . $filename["name"]; ?>"
- class="notes_list_item"
+ class="list_item"
>
<?php
@@ -144,4 +161,99 @@ function list_notes(
} // function
+function list_categories(
+ string $user
+) {
+
+ /* initilize categories array ncluding the "all" pseudo-category */
+ $categories = array(
+ array(
+ "name" => "",
+ "matches" => 0
+ )
+ );
+
+ /* Fetch all categories of the user and loop through them */
+ $categories_path = glob($GLOBALS["data_dir"] . "/" . $user . "/*");
+ foreach ($categories_path as $category_path) {
+
+ /* store names and number of notes for each category */
+ $n_notes = count(glob($category_path . "/" . "*.txt"));
+ array_push(
+ $categories,
+ array(
+ "name" => basename($category_path),
+ "matches" => $n_notes
+ )
+ );
+
+ /* add number of notes to the "all" pseudo-category */
+ $categories[0]["matches"] = $categories[0]["matches"] + $n_notes;
+
+ } // foreach-loop
+
+ /* sort categories by number of notes */
+ usort($categories, "sort_by_matches");
+
+?>
+
+<!-- Headline -->
+<h4><?php echo $GLOBALS["i18n_categories"]; ?></h4>
+
+<div class="inline">
+
+<?php
+
+ /* loop through each category and print its item */
+ foreach ($categories as $category) {
+
+ /* Skip categories that have no notes */
+ if ($category["matches"] < 1) {
+ continue;
+ }
+
+ /* create button label */
+ if (!empty($category["name"])) {
+ $btn_label = $category["name"] . ": " . $category["matches"] . " " .
+ $GLOBALS["i18n_notes"];
+ } else {
+ $btn_label = $GLOBALS["i18n_list"] . ": " . $category["matches"] . " " .
+ $GLOBALS["i18n_notes"];
+ }
+
+?>
+
+ <!-- Category button -->
+ <form action="/" target="_self" method="post" class="inline">
+ <input
+ id="category"
+ name="category"
+ type="hidden"
+ value="<?php echo $category["name"]; ?>"
+ >
+ <input
+ id="action"
+ name="action"
+ type="hidden"
+ value="list"
+ >
+ <input
+ type="submit"
+ value="<?php echo $btn_label; ?>"
+ >
+ </form>
+
+<?php
+
+ } // foreach-loop
+
+?>
+
+</div>
+
+<?php
+
+} // function
+
+
?>
diff --git a/lib/menus.php b/lib/menus.php
@@ -10,7 +10,7 @@ function top_navigation(
<!-- Navigation Buttons -->
<nav>
- <!-- All Notes: List-Action -->
+ <!-- All Categories: Categories-Action -->
<form action="/" method="post" class="inline">
<input
id="category"
@@ -22,12 +22,12 @@ function top_navigation(
id="action"
name="action"
type="hidden"
- value="list"
+ value="categories"
>
<input
class="likenavitem"
type="submit"
- value="<?php echo $GLOBALS["i18n_list"]; ?>"
+ value="<?php echo $GLOBALS["i18n_categories"]; ?>"
>
</form>
@@ -90,65 +90,3 @@ function top_navigation(
} // function
?>
-
-<?php
-
-function category_menu(string $user) {
-
-?>
-
-<!-- List all categories -->
-<details>
- <summary><?php echo $GLOBALS["i18n_categories"]; ?></summary>
- <div class="inline">
- <form action="/" target="_self" method="post" class="inline">
- <input
- id="action"
- name="action"
- type="hidden"
- value="list"
- >
- <input
- type="submit"
- value="<?php echo $GLOBALS["i18n_list"]; ?>"
- >
- </form>
-
-<?php
- /* Fetch all categories of the user */
- $categories_path = glob($GLOBALS["data_dir"] . "/" . $user . "/*");
- foreach ($categories_path as $category_path) {
-
- if (count(glob($category_path . "/" . "*.txt")) > 0) {
-?>
-
- <form action="/" target="_self" method="post" class="inline">
- <input
- id="category"
- name="category"
- type="hidden"
- value="<?php echo basename($category_path); ?>"
- >
- <input
- id="action"
- name="action"
- type="hidden"
- value="list"
- >
- <input
- type="submit"
- value="<?php echo basename($category_path); ?>"
- >
- </form>
-
-<?php
- } // if-statement
- } // foreach-loop
-?>
-
- </div>
-</details>
-
-<?php
-} // function
-?>
diff --git a/lib/search.php b/lib/search.php
@@ -36,7 +36,7 @@ function search_notes(
$filenames = gather_notes($user);
/* split query into words */
- $query = explode(" ", $query);
+ $queries = explode(" ", $query);
/* loop through each file */
for ($i = 0; $i < count($filenames); $i++) {
@@ -52,7 +52,7 @@ function search_notes(
/* collect number of matches */
$matches = 0;
- foreach ($query as $word) {
+ foreach ($queries as $word) {
$matches = $matches + preg_match('/' . $word . '/i', $content);
}
@@ -64,6 +64,13 @@ function search_notes(
/* Sort filenames by number of matches */
usort($filenames, "sort_by_matches");
+?>
+
+ <!-- Headline -->
+ <h4><?php echo $GLOBALS["i18n_search"] . ": " . $query; ?></h4>
+
+<?php
+
/* loop through each filename and draw a table row */
foreach ($filenames as $filename) {