]> mj.ucw.cz Git - gallery.git/blob - gal2/UCW/Gallery/Web.pm
Gallery2: More objectification
[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
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::ErrorHandler::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                 UCW::CGI::http_error(404, 'No such photo');
59                 return;
60         }
61
62         my $meta = $self->{meta};
63         my $id = $meta->{sequence}->[$show_img-1];
64         my $m = $meta->{photo}->{$id} or die;
65         $self->html_top;
66
67         $self->show_links(($show_img > 1 ? ("?i=".($show_img-1)) : ""),
68                           "?",
69                           ($show_img < $self->{num_photos} ? ("?i=".($show_img+1)) : ""));
70
71         my $t = UCW::CGI::html_escape($m->{title});
72         my $w = $m->{w};
73         my $h = $m->{h};
74         print "<h1>$t</h1>\n" if $t ne "";
75         my $img = $self->get('PhotoUrlPrefix') . $id . '.jpg';
76         print "<p class=large><img src='$img' width=$w height=$h alt='$t'>\n";
77
78         $self->html_bot;
79 }
80
81 sub show_list($) {
82         my ($self) = @_;
83         $self->html_top;
84
85         $self->show_links($self->get('BackURL'), $self->get('ParentURL'), $self->get('FwdURL'));
86         print "<h1>", $self->get('Title'), "</h1>\n";
87         my $subtitle = $self->get('SubTitle');
88         print "<h2>$subtitle</h2>\n" if $subtitle ne "";
89
90         my $meta = $self->{meta};
91         for my $idx (1..$self->{num_photos}) {
92                 my $id = $meta->{sequence}->[$idx-1];
93                 my $click_url;
94                 if ($self->get('WebImageSubpages')) {
95                         $click_url = "?i=$idx";
96                 } else {
97                         $click_url = $self->get('PhotoUrlPrefix') . "$id.jpg";
98                 }
99                 $self->show_thumb($meta, $id, $click_url);
100         }
101
102         $self->html_bot();
103 }
104
105 sub dispatch($) {
106         my ($self) = @_;
107         UCW::CGI::parse_args(\%args);
108         $self->{meta} = $self->{gal}->read_meta(File::Spec->catfile($self->get('CacheDir'), 'cache.meta'));
109         $self->{num_photos} = scalar @{$self->{meta}->{sequence}};
110
111         if ($show_img ne "") {
112                 $self->show_img;
113         } else {
114                 $self->show_list;
115         }
116 }
117
118 sub attach($$) {
119         my ($class, $gal) = @_;
120         my $self = { gal => $gal };
121         $gal->def(
122                 WebFE => $self,
123                 WebHeadExtras => "",
124                 WebTopExtras => "",
125                 WebBotExtras => "",
126                 WebThemeCSS => undef,
127                 WebImageSubpages => 1,
128         );
129         bless $self, $class;
130         return $self;
131 }
132
133 42;