]> mj.ucw.cz Git - gallery.git/blob - gal/UCW/Gallery/Web.pm
Unify photo_name
[gallery.git] / gal / 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);
36         } else {
37                 return $val;
38         }
39 }
40
41 # For use by extras hooks
42 sub gallery($) {
43         my ($self) = @_;
44         return $self->{gal};
45 }
46
47 # For use by extras hooks: return true if we are showing an image page, false for index page
48 sub showing_image($) {
49         my ($self) = @_;
50         return $show_img ne "";
51 }
52
53 sub html_top($) {
54         my ($self) = @_;
55         my $title = UCW::CGI::html_escape($self->get('Title'));
56         my $hextras = $self->extras('WebHeadExtras');
57         my $theme_hextras = $self->theme_head_extras;
58         print <<EOF ;
59 Content-Type: text/html
60
61 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
62 <html><head>
63 $hextras$theme_hextras<title>$title</title>
64 </head><body>
65 EOF
66
67         $UCW::CGI::ErrorHandler::error_hook = \&error;
68
69         # WebTopExtras are evaluated separately, since they can override the error hook
70         print $self->extras('WebTopExtras');
71 }
72
73 sub html_bot($) {
74         my ($self) = @_;
75         print $self->extras('WebBotExtras'), "</body></html>\n";
76 }
77
78 sub show_img($) {
79         my ($self) = @_;
80
81         if ($show_img < 1 || $show_img > $self->{num_photos}) {
82                 UCW::CGI::http_error('404 No such photo');
83                 return;
84         }
85
86         my $meta = $self->{meta};
87         my $id = $meta->{sequence}->[$show_img-1];
88         my $m = $meta->{photo}->{$id} or die;
89         $self->html_top;
90
91         $self->show_links(($show_img > 1 ? ("?i=".($show_img-1)) : ""),
92                           ".",
93                           ($show_img < $self->{num_photos} ? ("?i=".($show_img+1)) : ""));
94
95         my $t = UCW::CGI::html_escape($m->{title});
96         my $w = $m->{w};
97         my $h = $m->{h};
98         print "<h1>$t</h1>\n" if $t ne "";
99         my $img = $self->get('PhotoUrlPrefix') . $self->{gal}->photo_file_name($m, $id);
100         print "<p class=large><img src='$img' width=$w height=$h alt='$t'>\n";
101
102         $self->html_bot;
103 }
104
105 sub show_pre_thumbs($) {
106         my ($self) = @_;
107 }
108
109 sub show_post_thumbs($) {
110         my ($self) = @_;
111 }
112
113 sub show_list($) {
114         my ($self) = @_;
115         $self->html_top;
116
117         $self->show_links($self->get('BackURL'), $self->get('ParentURL'), $self->get('FwdURL'));
118         print "<h1>", $self->get('Title'), "</h1>\n";
119         my $subtitle = $self->get('SubTitle');
120         print "<h2>$subtitle</h2>\n" if $subtitle ne "";
121         $self->show_pre_thumbs;
122
123         my $meta = $self->{meta};
124         for my $idx (1..$self->{num_photos}) {
125                 my $id = $meta->{sequence}->[$idx-1];
126                 my $m = $meta->{photo}->{$id};
127                 my $click_url;
128                 if ($self->get('WebImageSubpages')) {
129                         $click_url = "?i=$idx";
130                 } else {
131                         $click_url = $self->get('PhotoUrlPrefix') . $self->{gal}->photo_file_name($m, $id);
132                 }
133                 $self->show_thumb($meta, $id, $click_url);
134         }
135
136         $self->show_post_thumbs;
137         $self->html_bot();
138 }
139
140 sub dispatch($) {
141         my ($self) = @_;
142         binmode STDOUT, ':utf8';
143         UCW::CGI::parse_args(\%args);
144         $self->{meta} = $self->{gal}->read_meta(File::Spec->catfile($self->get('CacheDir'), 'cache.meta'));
145         $self->{num_photos} = scalar @{$self->{meta}->{sequence}};
146
147         if ($want_archive) {
148                 require UCW::Gallery::Archive;
149                 UCW::Gallery::Archive::send_archive($self->{gal}, $self->{meta});
150         } elsif ($show_img ne "") {
151                 $self->show_img;
152         } else {
153                 $self->show_list;
154         }
155 }
156
157 sub attach($$) {
158         my ($class, $gal) = @_;
159         my $self = { gal => $gal };
160         $gal->def(
161                 WebFE => $self,
162
163                 # Extras are either strings or functions called with the current gallery object as parameter
164                 WebHeadExtras => "",
165                 WebTopExtras => "",
166                 WebBotExtras => "",
167
168                 # Used by the theming logic
169                 WebThemeCSS => undef,
170
171                 # 1 if thumbnail link to sub-pages with images, 0 if they link directly to image files
172                 WebImageSubpages => 1,
173
174                 # If enabled, calling the CGI with a=zip produces a ZIP archive with all photos.
175                 WebAllowArchives => 1,
176         );
177         bless $self, $class;
178         return $self;
179 }
180
181 42;