doclist: sublists of multiple dates grouped by year
[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         $group = substr($date, 0, 4);
10         if (!isset($cal[$group][$date])) {
11                 $cal[$group][$date] = [];
12         }
13         if (!is_dir($url) and filesize($url)) {
14                 $cal[$group][$date][$suffix] = $link;
15         }
16 }
17
18 $year = 3600 * 24 * 365;  # seconds per year
19 $scale = 8;  # em width per year
20 $mindate = strtotime($date) + ($year / $scale * 3);  # first point plus about 3em for centered text
21
22 print '<ul class="timeline">'."\n";
23 foreach (array_reverse($cal, TRUE) as $group => $rows) {
24         if (count($rows) > 1) {
25                 $time = ($mindate - strtotime("$group-12-31T23:59")) / $year * $scale;
26                 printf('<li class="range" style="left:%.1fem; width:%sem">', $time, $scale);
27                 print "<strong>$group</strong><ul>\n";
28         }
29         else {
30                 $time = 0;
31         }
32
33         foreach ($rows as $title => $versions) {
34                 $subtime = ($mindate - strtotime($title)) / $year * $scale - $time;
35                 printf('<li style="left:%.1fem">', $subtime);
36                 if ($url = @$versions['']) {
37                         printf('<a href="/%s">%s</a>', $url, $title);
38                 }
39                 else {
40                         print '<span>';
41                         print $title;
42                         if ($versions and $User->admin("edit $Page$Args")) {
43                                 printf(' (%s)', implode(', ', array_map(
44                                         function ($format, $url) {
45                                                 return sprintf('<a href="/%s">%s</a>', $url, $format);
46                                         },
47                                         array_keys($versions), $versions
48                                 )));
49                         }
50                         print '</span>';
51                 }
52                 print "</li>\n";
53         }
54
55         if (count($rows) > 1) {
56                 print "</ul></li>\n";
57         }
58 }
59 print "</ul>\n\n";
60