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