]> mj.ucw.cz Git - gallery.git/blob - gal2/UCW/Gallery/Web.pm
Gallery2: Archiving and generalized HTML extras
[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 strict;
7 use warnings;
8
9 use UCW::Gallery;
10 use UCW::CGI;
11 use File::Spec;
12
13 my $show_img;
14 my $want_archive;
15
16 my %args = (
17         'i'     => { 'var' => \$show_img, 'check' => '\d+' },
18         'a'     => { 'var' => \$want_archive },
19 );
20
21 sub error($) {
22         print "<p style='color:red'>Bad luck, the script is broken. Sorry.\n<p>$_[0]\n";
23         print "</body></html>\n";
24 }
25
26 sub get($$) {
27         my ($self, $key) = @_;
28         return $self->{gal}->get($key);
29 }
30
31 sub extras($$) {
32         my ($self, $key) = @_;
33         my $val = $self->get($key);
34         if (ref $val eq 'CODE') {
35                 return &$val($self->{gal});
36         } else {
37                 return $val;
38         }
39 }
40
41 sub html_top($) {
42         my ($self) = @_;
43         my $title = UCW::CGI::html_escape($self->get('Title'));
44         my $hextras = $self->extras('WebHeadExtras');
45         my $textras = $self->extras('WebTopExtras');
46         my $theme_hextras = $self->theme_head_extras;
47         print <<EOF ;
48 Content-Type: text/html
49
50 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
51 <html><head>
52 $hextras$theme_hextras<title>$title</title>
53 </head><body>
54 $textras
55 EOF
56         $UCW::CGI::ErrorHandler::error_hook = \&error;
57 }
58
59 sub html_bot($) {
60         my ($self) = @_;
61         print $self->extras('WebBotExtras'), "</body></html>\n";
62 }
63
64 sub show_img($) {
65         my ($self) = @_;
66
67         if ($show_img < 1 || $show_img > $self->{num_photos}) {
68                 UCW::CGI::http_error('404 No such photo');
69                 return;
70         }
71
72         my $meta = $self->{meta};
73         my $id = $meta->{sequence}->[$show_img-1];
74         my $m = $meta->{photo}->{$id} or die;
75         $self->html_top;
76
77         $self->show_links(($show_img > 1 ? ("?i=".($show_img-1)) : ""),
78                           ".",
79                           ($show_img < $self->{num_photos} ? ("?i=".($show_img+1)) : ""));
80
81         my $t = UCW::CGI::html_escape($m->{title});
82         my $w = $m->{w};
83         my $h = $m->{h};
84         print "<h1>$t</h1>\n" if $t ne "";
85         my $img = $self->get('PhotoUrlPrefix') . $id . '.jpg';
86         print "<p class=large><img src='$img' width=$w height=$h alt='$t'>\n";
87
88         $self->html_bot;
89 }
90
91 sub show_pre_thumbs($) {
92         my ($self) = @_;
93 }
94
95 sub show_post_thumbs($) {
96         my ($self) = @_;
97 }
98
99 sub show_list($) {
100         my ($self) = @_;
101         $self->html_top;
102
103         $self->show_links($self->get('BackURL'), $self->get('ParentURL'), $self->get('FwdURL'));
104         print "<h1>", $self->get('Title'), "</h1>\n";
105         my $subtitle = $self->get('SubTitle');
106         print "<h2>$subtitle</h2>\n" if $subtitle ne "";
107         $self->show_pre_thumbs;
108
109         my $meta = $self->{meta};
110         for my $idx (1..$self->{num_photos}) {
111                 my $id = $meta->{sequence}->[$idx-1];
112                 my $click_url;
113                 if ($self->get('WebImageSubpages')) {
114                         $click_url = "?i=$idx";
115                 } else {
116                         $click_url = $self->get('PhotoUrlPrefix') . "$id.jpg";
117                 }
118                 $self->show_thumb($meta, $id, $click_url);
119         }
120
121         $self->show_post_thumbs;
122         $self->html_bot();
123 }
124
125 sub dispatch($) {
126         my ($self) = @_;
127         binmode STDOUT, ':utf8';
128         UCW::CGI::parse_args(\%args);
129         $self->{meta} = $self->{gal}->read_meta(File::Spec->catfile($self->get('CacheDir'), 'cache.meta'));
130         $self->{num_photos} = scalar @{$self->{meta}->{sequence}};
131
132         if ($want_archive) {
133                 require UCW::Gallery::Archive;
134                 UCW::Gallery::Archive::send_archive($self->{gal}, $self->{meta});
135         } elsif ($show_img ne "") {
136                 $self->show_img;
137         } else {
138                 $self->show_list;
139         }
140 }
141
142 sub attach($$) {
143         my ($class, $gal) = @_;
144         my $self = { gal => $gal };
145         $gal->def(
146                 WebFE => $self,
147
148                 # Extras are either strings or functions called with the current gallery object as parameter
149                 WebHeadExtras => "",
150                 WebTopExtras => "",
151                 WebBotExtras => "",
152
153                 # Used by the theming logic
154                 WebThemeCSS => undef,
155
156                 # 1 if thumbnail link to sub-pages with images, 0 if they link directly to image files
157                 WebImageSubpages => 1,
158
159                 # If enabled, calling the CGI with a=zip produces a ZIP archive with all photos.
160                 WebAllowArchives => 1,
161         );
162         bless $self, $class;
163         return $self;
164 }
165
166 42;