commit 61cef2a694108a07c0484168dc48c4f9a6836aae
parent 57cc9167b4182e51c3bda1048c2022fadddf6b6c
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date: Fri, 20 Dec 2024 10:33:31 +0100
feat: introduce gather_categories() help function
Diffstat:
2 files changed, 42 insertions(+), 29 deletions(-)
diff --git a/lib/helpers.php b/lib/helpers.php
@@ -85,6 +85,46 @@ function link_in_first_line(string $filepath) {
}
+function gather_categories(
+ string $user
+) {
+
+ /* initilize categories array including the "all" pseudo-category */
+ $categories = array(
+ array(
+ "name" => $GLOBALS["i18n_list"],
+ "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");
+
+ /* return categories array */
+ return $categories;
+
+} // function
+
+
function gather_notes(
string $user,
string $category = ""
diff --git a/lib/list.php b/lib/list.php
@@ -160,35 +160,8 @@ function list_categories(
string $user
) {
- /* initilize categories array ncluding the "all" pseudo-category */
- $categories = array(
- array(
- "name" => $GLOBALS["i18n_list"],
- "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");
+ /* Gather all categories */
+ $categories = gather_categories($user);
?>