doclist: code cleanup and preparation
[minimedit.git] / widget / doclist.php
1 <?php
2 global $User;
3
4 $cal = [];
5 foreach (glob("$Page$Args/2*") as $url) {
6         $link = preg_replace('/\.html$/', '', $url);
7         $name = pathinfo($link, PATHINFO_BASENAME);
8         @list ($date, $suffix) = explode('.', $name, 2);
9         if (!isset($cal[$date])) {
10                 $cal[$date] = [];
11         }
12         if (!is_dir($url) and filesize($url)) {
13                 $cal[$date][$suffix] = $link;
14         }
15 }
16
17 $year = 3600 * 24 * 365;  # seconds per year
18 $scale = 8;  # em width per year
19 $mindate = strtotime($date) + ($year / $scale * 3);  # first point plus about 3em for centered text
20
21 print '<ul class="timeline">'."\n";
22 foreach (array_reverse($cal) as $title => $versions) {
23                 $time = 0;
24                 $subtime = ($mindate - strtotime($title)) / $year * $scale - $time;
25                 printf('<li style="left:%.1fem">', $subtime);
26                 if ($url = @$versions['']) {
27                         printf('<a href="/%s">%s</a>', $url, $title);
28                 }
29                 else {
30                         print '<span>';
31                         print $title;
32                         if ($versions and $User->admin("edit $Page$Args")) {
33                                 printf(' (%s)', implode(', ', array_map(
34                                         function ($format, $url) {
35                                                 return sprintf('<a href="/%s">%s</a>', $url, $format);
36                                         },
37                                         array_keys($versions), $versions
38                                 )));
39                         }
40                         print '</span>';
41                 }
42                 print "</li>\n";
43 }
44 print "</ul>\n\n";
45