list.php (6367B)
1 <?php
2
3 function list_notes(
4 string $user,
5 string $category = ""
6 ) {
7
8 /* gather note paths, based on given category */
9 $filenames = gather_notes($user, $category);
10
11 /* Sort filenames by edit timestamp */
12 usort($filenames, "sort_by_time");
13
14 /* prepare headline content */
15 if (empty($category)) {
16 $category_head = $GLOBALS["i18n_list"];
17 } else {
18 $category_head = $category;
19 }
20 $category_head = $category_head . " (" . count($filenames) . " " .
21 $GLOBALS["i18n_notes"] . ")";
22
23 ?>
24
25 <!-- Headline -->
26 <h4><?php echo $GLOBALS["i18n_category"] . ": " . $category_head; ?></h4>
27
28 <?php
29
30
31 /* loop through each filename and draw a table row */
32 foreach ($filenames as $filename) {
33
34 /* create id for list item */
35 $note_id = base64_encode(
36 $filename["category"] . "_" . $filename["name"]
37 );
38 ?>
39
40 <!-- note list item -->
41 <div id="<?php echo $note_id; ?>" class="list_item">
42
43 <?php
44 /* if note starts with an URL, link it in the title
45 * otherwise just print the title */
46 if ($filename["link"] !== false) {
47 ?>
48 <a href="<?php echo $filename["link"]; ?>" target="_blank">
49 <strong><?php echo $filename["name"]; ?></strong>
50 </a><br>
51
52 <?php } else { ?>
53 <strong><?php echo $filename["name"]; ?></strong><br>
54 <?php } ?>
55
56 <!-- date marker -->
57 <span class="inline">
58 <?php echo date("Y-m-d H:i:s", $filename["time"]); ?>
59 </span>
60
61 <!-- separator -->
62 <span class="inline">|</span>
63
64 <!-- category button -->
65 <form action="/" target="_self" method="post" class="inline">
66 <input
67 id="category"
68 name="category"
69 type="hidden"
70 value="<?php echo $filename["category"]; ?>"
71 >
72 <input
73 id="action"
74 name="action"
75 type="hidden"
76 value="list"
77 >
78 <input
79 type="submit"
80 class="likeanchor"
81 value="<?php echo $filename["category"]; ?>"
82 >
83 </form>
84
85 <!-- separator -->
86 <span class="inline">|</span>
87
88 <!-- edit button -->
89 <form action="/" method="post" class="inline">
90 <input
91 id="category"
92 name="category"
93 type="hidden"
94 value="<?php echo $filename["category"]; ?>"
95 >
96 <input
97 id="filename"
98 name="filename"
99 type="hidden"
100 value="<?php echo $filename["name"] . ".txt"; ?>"
101 >
102 <input
103 id="action"
104 name="action"
105 type="hidden"
106 value="show"
107 >
108 <input
109 type="submit"
110 class="likeanchor"
111 value="<?php echo $GLOBALS["i18n_edit"]; ?>"
112 >
113 </form>
114
115 <!-- separator -->
116 <span class="inline">|</span>
117
118 <!-- delete button -->
119 <form
120 action="/"
121 method="post"
122 id="<?php echo "del_" . $note_id; ?>"
123 class="inline"
124 >
125 <input
126 id="category"
127 name="category"
128 type="hidden"
129 value="<?php echo $filename["category"]; ?>"
130 >
131 <input
132 id="filename"
133 name="filename"
134 type="hidden"
135 value="<?php echo $filename["name"] . ".txt"; ?>"
136 >
137 <input
138 id="action"
139 name="action"
140 type="hidden"
141 value="delete"
142 >
143 <input
144 class="danger likeanchor"
145 type="button"
146 value="<?php echo $GLOBALS["i18n_delete"]; ?>"
147 onclick="deletion_confirm('<?php echo "del_" . $note_id; ?>');"
148 >
149 </form>
150
151 </div>
152
153 <?php
154
155 } // foreach-loop
156
157 } // function
158
159 function list_categories(
160 string $user
161 ) {
162
163 /* initilize categories array ncluding the "all" pseudo-category */
164 $categories = array(
165 array(
166 "name" => "",
167 "matches" => 0
168 )
169 );
170
171 /* Fetch all categories of the user and loop through them */
172 $categories_path = glob($GLOBALS["data_dir"] . "/" . $user . "/*");
173 foreach ($categories_path as $category_path) {
174
175 /* store names and number of notes for each category */
176 $n_notes = count(glob($category_path . "/" . "*.txt"));
177 array_push(
178 $categories,
179 array(
180 "name" => basename($category_path),
181 "matches" => $n_notes
182 )
183 );
184
185 /* add number of notes to the "all" pseudo-category */
186 $categories[0]["matches"] = $categories[0]["matches"] + $n_notes;
187
188 } // foreach-loop
189
190 /* sort categories by number of notes */
191 usort($categories, "sort_by_matches");
192
193 ?>
194
195 <!-- Headline -->
196 <h4><?php echo $GLOBALS["i18n_categories"]; ?></h4>
197
198 <div class="inline">
199
200 <?php
201
202 /* loop through each category and print its item */
203 foreach ($categories as $category) {
204
205 /* Skip categories that have no notes */
206 if ($category["matches"] < 1) {
207 continue;
208 }
209
210 /* create button label */
211 if (!empty($category["name"])) {
212 $btn_label = $category["name"] . ": " . $category["matches"] . " " .
213 $GLOBALS["i18n_notes"];
214 } else {
215 $btn_label = $GLOBALS["i18n_list"] . ": " . $category["matches"] . " " .
216 $GLOBALS["i18n_notes"];
217 }
218
219 ?>
220
221 <!-- Category button -->
222 <form action="/" target="_self" method="post" class="inline">
223 <input
224 id="category"
225 name="category"
226 type="hidden"
227 value="<?php echo $category["name"]; ?>"
228 >
229 <input
230 id="action"
231 name="action"
232 type="hidden"
233 value="list"
234 >
235 <input
236 type="submit"
237 value="<?php echo $btn_label; ?>"
238 >
239 </form>
240
241 <?php
242
243 } // foreach-loop
244
245 ?>
246
247 </div>
248
249 <?php
250
251 } // function
252
253
254 ?>