]> mj.ucw.cz Git - gallery.git/blob - gal2/UCW/Gallery/Web.pm
Gallery2: Clean up remnants of archiving
[gallery.git] / gal2 / UCW / Gallery / Web.pm
1 # Simple Photo Gallery: Web Interface
2 # (c) 2003--2012 Martin Mares <mj@ucw.cz>
3
4 package UCW::Gallery::Web;
5
6 use UCW::Gallery qw(%CF);
7 use UCW::CGI;
8 use File::Spec;
9
10 my $show_img;
11
12 my %args = (
13         'i'     => { 'var' => \$show_img, 'check' => '\d+' },
14 );
15
16 our $meta;
17 our $num_photos;
18
19 sub error($) {
20         print "<p style='color:red'>Bad luck, the script is broken. Sorry.\n<p>$_[0]\n";
21         print "</body></html>\n";
22 }
23
24 sub show_links($$$) {
25         my ($prev, $up, $next) = @_;
26         print "<p class=parent>";
27         print "<span class=back style='width: $CF{'BackW'}px; height: $CF{'BackH'}px'>";
28         print "<a href='$prev'><img src='$CF{'BackImg'}' width=$CF{'BackW'} height=$CF{'BackH'} alt='Back'></a>" if $prev ne "";
29         print "</span>\n";
30         print "<span class=fwd style='width: $CF{'FwdW'}px; height: $CF{'FwdH'}px'>";
31         print "<a href='$next'><img src='$CF{'FwdImg'}' width=$CF{'FwdW'} height=$CF{'FwdH'} alt='Forward'></a>" if $next ne "";
32         print "</span>\n";
33         print "<a href='$up'><img src='$CF{'ParentImg'}' width=$CF{'ParentW'} height=$CF{'ParentH'} alt='Up'></a>\n" if $up ne "";
34 }
35
36 sub html_top() {
37         my $title = UCW::CGI::html_escape($CF{"Title"});
38         print <<EOF ;
39 Content-Type: text/html
40
41 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
42 <html><head>
43 $CF{"HeadExtras"}
44 <link rel=stylesheet href="$CF{"StyleSheet"}" type="text/css" media=all>
45 <title>$title</title>
46 </head><body>
47 $CF{"TopExtras"}
48 EOF
49         $UCW::CGI::error_hook = \&error;
50 }
51
52 sub html_bot() {
53         print "$CF{'BotExtras'}\n</body></html>\n";
54 }
55
56 sub show_img() {
57         if ($show_img < 1 || $show_img > $num_photos) {
58                 print "Status: 404\n";
59                 html_top();
60                 print "<h1>No such photo</h1>\n";
61                 html_bot();
62                 return;
63         }
64
65         my $id = $meta->{sequence}->[$show_img-1];
66         my $m = $meta->{photo}->{$id} or die;
67         html_top();
68
69         show_links(($show_img > 1 ? ("?i=".($show_img-1)) : ""),
70                    "?",
71                    ($show_img < $num_photos ? ("?i=".($show_img+1)) : ""));
72
73         my $t = UCW::CGI::html_escape($m->{title});
74         my $w = $m->{w};
75         my $h = $m->{h};
76         print "<h1>$t</h1>\n" if $t ne "";
77         my $img = $CF{'PhotoUrlPrefix'} . $id . ".jpg";
78         print "<p class=large><img src='$img' width=$w height=$h alt='$t'>\n";
79
80         html_bot();
81 }
82
83 sub show_list() {
84         html_top();
85
86         show_links($CF{'BackURL'}, $CF{'ParentURL'}, $CF{'FwdURL'});
87         print "<h1>$CF{'Title'}</h1>\n";
88         print "<h2>$CF{'SubTitle'}</h2>\n" if defined $CF{'SubTitle'};
89
90         my $thumb_fmt = $CF{'ThumbW'} . "x" . $CF{'ThumbH'};
91         my $thumb_meta = $meta->{thumb}->{$thumb_fmt} or die "No thumbnails for format $thumb_fmt found!\n";
92
93         for my $idx (1..$num_photos) {
94                 my $id = $meta->{sequence}->[$idx-1];
95                 my $m = $meta->{photo}->{$id} or die;
96                 my $tm = $thumb_meta->{$id} or die;
97
98                 my $annot = UCW::CGI::html_escape($m->{title});
99                 my $tw = $tm->{w};
100                 my $th = $tm->{h};
101                 my $thumb = $CF{'ThumbUrlPrefix'} . "$thumb_fmt/$id.jpg";
102                 my $side_w = $CF{"ThumbW"} + 2*$CF{"InteriorMargin"};
103                 my $side_h = $CF{"ThumbH"} + 2*$CF{"InteriorMargin"};
104                 my $box_w = $CF{"LeftW"} + $side_w + $CF{"RightW"};
105                 my $box_h = $CF{"TopH"} + $side_h + $CF{"BotH"};
106                 print "<div class=thf><div class=thumb>\n";
107                 print "<img src='$CF{'TopImg'}' width=$box_w height=$CF{'TopH'} alt='' class=tt>\n";
108                 print "<img src='$CF{'LeftImg'}' width=$CF{'LeftW'} height=$side_h alt='' class=tl>\n";
109                 my $ol = $CF{'LeftW'} + $CF{'InteriorMargin'} + int(($CF{'ThumbW'} - $tw)/2);
110                 my $ot = $CF{'TopH'} + $CF{'InteriorMargin'} + int(($CF{'ThumbH'} - $th)/2);
111                 my $tit = ($annot ne "") ? " title=\"$annot\"" : "";
112                 my $url = ($CF{"ImageSubpages"} ? "?i=$idx" : $orig);
113                 print "<a href='$url'><img src='$thumb' width=$w height=$h alt='$orig'$tit class=ti style='left: ${ol}px; top: ${ot}px'></a>\n";
114                 print "<img src='$CF{'RightImg'}' width=$CF{'RightW'} height=$side_h alt='' class=tr>\n";
115                 print "<img src='$CF{'BotImg'}' width=$box_w height=$CF{'BotH'} alt='' class=tb>\n";
116                 print "</div>\n";
117                 print "</div>\n\n";
118         }
119
120         html_bot();
121 }
122
123 sub Dispatch() {
124         UCW::CGI::parse_args(\%args);
125         UCW::Gallery::LoadConfig();
126         $meta = UCW::Gallery::ReadMeta(File::Spec->catfile($CF{'CacheDir'}, 'cache.meta'));
127         $num_photos = scalar @{$meta->{sequence}};
128
129         if ($show_img ne "") {
130                 show_img();
131         } else {
132                 show_list();
133         }
134 }
135
136 42;