]> mj.ucw.cz Git - gallery.git/blob - UCW/Gallery/Web.pm
gal-mj-digikam: Use image title
[gallery.git] / 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 strict;
7 use warnings;
8
9 use UCW::Gallery;
10 use UCW::CGI;
11 use File::Spec;
12
13 my $show_img;
14
15 my %args = (
16         'i'     => { 'var' => \$show_img, 'check' => '\d+' },
17 );
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 get($$) {
25         my ($self, $key) = @_;
26         return $self->{gal}->get($key);
27 }
28
29 sub html_top($) {
30         my ($self) = @_;
31         my $title = UCW::CGI::html_escape($self->get('Title'));
32         my $hextras = $self->get('WebHeadExtras');
33         my $textras = $self->get('WebTopExtras');
34         my $theme_hextras = $self->theme_head_extras;
35         print <<EOF ;
36 Content-Type: text/html
37
38 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
39 <html><head>
40 $hextras
41 $theme_hextras
42 <title>$title</title>
43 </head><body>
44 $textras
45 EOF
46         $UCW::CGI::error_hook = \&error;
47 }
48
49 sub html_bot($) {
50         my ($self) = @_;
51         print $self->get('WebBotExtras'), "\n</body></html>\n";
52 }
53
54 sub show_img($) {
55         my ($self) = @_;
56
57         if ($show_img < 1 || $show_img > $self->{num_photos}) {
58                 print "Status: 404\n";
59                 $self->html_top;
60                 print "<h1>No such photo</h1>\n";
61                 $self->html_bot;
62                 return;
63         }
64
65         my $meta = $self->{meta};
66         my $id = $meta->{sequence}->[$show_img-1];
67         my $m = $meta->{photo}->{$id} or die;
68         $self->html_top;
69
70         $self->show_links(($show_img > 1 ? ("?i=".($show_img-1)) : ""),
71                           "?",
72                           ($show_img < $self->{num_photos} ? ("?i=".($show_img+1)) : ""));
73
74         my $t = UCW::CGI::html_escape($m->{title});
75         my $w = $m->{w};
76         my $h = $m->{h};
77         print "<h1>$t</h1>\n" if $t ne "";
78         my $img = $self->get('PhotoUrlPrefix') . $id . '.jpg';
79         print "<p class=large><img src='$img' width=$w height=$h alt='$t'>\n";
80
81         $self->html_bot;
82 }
83
84 sub show_list($) {
85         my ($self) = @_;
86         $self->html_top;
87
88         $self->show_links($self->get('BackURL'), $self->get('ParentURL'), $self->get('FwdURL'));
89         print "<h1>", $self->get('Title'), "</h1>\n";
90         my $subtitle = $self->get('SubTitle');
91         print "<h2>$subtitle</h2>\n" if $subtitle ne "";
92
93         my $meta = $self->{meta};
94         for my $idx (1..$self->{num_photos}) {
95                 my $id = $meta->{sequence}->[$idx-1];
96                 my $click_url;
97                 if ($self->get('WebImageSubpages')) {
98                         $click_url = "?i=$idx";
99                 } else {
100                         $click_url = $self->get('PhotoUrlPrefix') . "$id.jpg";
101                 }
102                 $self->show_thumb($meta, $id, $click_url);
103         }
104
105         $self->html_bot();
106 }
107
108 sub dispatch($) {
109         my ($self) = @_;
110         UCW::CGI::parse_args(\%args);
111         $self->{meta} = UCW::Gallery::ReadMeta(File::Spec->catfile($self->get('CacheDir'), 'cache.meta'));
112         $self->{num_photos} = scalar @{$self->{meta}->{sequence}};
113
114         if ($show_img ne "") {
115                 $self->show_img;
116         } else {
117                 $self->show_list;
118         }
119 }
120
121 sub attach($$) {
122         my ($class, $gal) = @_;
123         my $self = { gal => $gal };
124         $gal->def(
125                 WebFE => $self,
126                 WebHeadExtras => "",
127                 WebTopExtras => "",
128                 WebBotExtras => "",
129                 WebThemeCSS => undef,
130                 WebImageSubpages => 1,
131         );
132         bless $self, $class;
133         return $self;
134 }
135
136 42;