page: replace global variables by $Page object
[minimedit.git] / widget / doclist.php
1 <?php
2 global $User;
3
4 $cal = [];
5 foreach (glob("{$Page->link}/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 = 7;  # em width per year
20 $mindate = max(time(), strtotime($date) + ($year / $scale * 3));  # at least last entry plus 3em for centered text
21 $length = ($mindate - strtotime(key(current($cal)))) / $year * $scale;  # earliest entry position
22
23 printf('<ul class="timeline" style="background-size:%.1fem">'."\n", $length);
24 foreach (array_reverse($cal, TRUE) as $group => $rows) {
25         if (count($rows) > 1) {
26                 $time = ($mindate - strtotime("$group-12-31T23:59")) / $year * $scale;
27                 printf('<li class="range" style="left:%.1fem; width:%.1fem">',
28                         $time, $time + $scale > $length ? $length - $time + 2 : $scale
29                 );
30                 print "<strong>$group</strong><ul>\n";
31         }
32         else {
33                 $time = 0;
34         }
35
36         foreach ($rows as $title => $versions) {
37                 $subtime = ($mindate - strtotime($title)) / $year * $scale - $time;
38                 printf('<li style="left:%.1fem">', $subtime);
39                 if ($url = @$versions['']) {
40                         printf('<a href="/%s">%s</a>', $url, $title);
41                 }
42                 else {
43                         print '<span>';
44                         print $title;
45                         if ($versions and $User->admin("edit {$Page->link}")) {
46                                 printf(' (%s)', implode(', ', array_map(
47                                         function ($format, $url) {
48                                                 return sprintf('<a href="/%s">%s</a>', $url, $format);
49                                         },
50                                         array_keys($versions), $versions
51                                 )));
52                         }
53                         print '</span>';
54                 }
55                 print "</li>\n";
56         }
57
58         if (count($rows) > 1) {
59                 print "</ul></li>\n";
60         }
61 }
62 print "</ul>\n\n";
63