]> mj.ucw.cz Git - gallery.git/blobdiff - gal2/UCW/Gallery/Web.pm
Gallery2: Archiving and generalized HTML extras
[gallery.git] / gal2 / UCW / Gallery / Web.pm
index ac333619c9b9e0684f9fb63af800bd7bfe8c8ba0..dbf913a63de4858f97fbdb8ef1359cb08cbf4d47 100644 (file)
 
 package UCW::Gallery::Web;
 
-use UCW::Gallery qw(%CF);
+use strict;
+use warnings;
+
+use UCW::Gallery;
 use UCW::CGI;
 use File::Spec;
 
 my $show_img;
-my $send_archive;
+my $want_archive;
 
 my %args = (
        'i'     => { 'var' => \$show_img, 'check' => '\d+' },
-       'a'     => { 'var' => \$send_archive }
+       'a'     => { 'var' => \$want_archive },
 );
 
-our $meta;
-our $num_photos;
-
-sub new() {
-       if ($send_archive && $CF{'AllowArchives'}) {
-               # FIXME?
-               require UCW::Gallery::Archive;
-               return new UCW::Gallery::Archive($send_archive);
-       } else {
-               return bless {};
-       }
-}
-
 sub error($) {
        print "<p style='color:red'>Bad luck, the script is broken. Sorry.\n<p>$_[0]\n";
        print "</body></html>\n";
 }
 
-sub show_links($$$) {
-       my ($prev, $up, $next) = @_;
-       print "<p class=parent>";
-       print "<span class=back style='width: $CF{'BackW'}px; height: $CF{'BackH'}px'>";
-       print "<a href='$prev'><img src='$CF{'BackImg'}' width=$CF{'BackW'} height=$CF{'BackH'} alt='Back'></a>" if $prev ne "";
-       print "</span>\n";
-       print "<span class=fwd style='width: $CF{'FwdW'}px; height: $CF{'FwdH'}px'>";
-       print "<a href='$next'><img src='$CF{'FwdImg'}' width=$CF{'FwdW'} height=$CF{'FwdH'} alt='Forward'></a>" if $next ne "";
-       print "</span>\n";
-       print "<a href='$up'><img src='$CF{'ParentImg'}' width=$CF{'ParentW'} height=$CF{'ParentH'} alt='Up'></a>\n" if $up ne "";
+sub get($$) {
+       my ($self, $key) = @_;
+       return $self->{gal}->get($key);
+}
+
+sub extras($$) {
+       my ($self, $key) = @_;
+       my $val = $self->get($key);
+       if (ref $val eq 'CODE') {
+               return &$val($self->{gal});
+       } else {
+               return $val;
+       }
 }
 
-sub html_top() {
-       my $title = UCW::CGI::html_escape($CF{"Title"});
+sub html_top($) {
+       my ($self) = @_;
+       my $title = UCW::CGI::html_escape($self->get('Title'));
+       my $hextras = $self->extras('WebHeadExtras');
+       my $textras = $self->extras('WebTopExtras');
+       my $theme_hextras = $self->theme_head_extras;
        print <<EOF ;
 Content-Type: text/html
 
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html><head>
-$CF{"HeadExtras"}
-<link rel=stylesheet href="$CF{"StyleSheet"}" type="text/css" media=all>
-<title>$title</title>
+$hextras$theme_hextras<title>$title</title>
 </head><body>
-$CF{"TopExtras"}
+$textras
 EOF
-       $UCW::CGI::error_hook = \&error;
+       $UCW::CGI::ErrorHandler::error_hook = \&error;
 }
 
-sub html_bot() {
-       print "$CF{'BotExtras'}\n</body></html>\n";
+sub html_bot($) {
+       my ($self) = @_;
+       print $self->extras('WebBotExtras'), "</body></html>\n";
 }
 
-sub show_img() {
-       if ($show_img < 1 || $show_img > $num_photos) {
-               print "Status: 404\n";
-               html_top();
-               print "<h1>No such photo</h1>\n";
-               html_bot();
+sub show_img($) {
+       my ($self) = @_;
+
+       if ($show_img < 1 || $show_img > $self->{num_photos}) {
+               UCW::CGI::http_error('404 No such photo');
                return;
        }
 
+       my $meta = $self->{meta};
        my $id = $meta->{sequence}->[$show_img-1];
        my $m = $meta->{photo}->{$id} or die;
-       html_top();
+       $self->html_top;
 
-       show_links(($show_img > 1 ? ("?i=".($show_img-1)) : ""),
-                  "?",
-                  ($show_img < $num_photos ? ("?i=".($show_img+1)) : ""));
+       $self->show_links(($show_img > 1 ? ("?i=".($show_img-1)) : ""),
+                         ".",
+                         ($show_img < $self->{num_photos} ? ("?i=".($show_img+1)) : ""));
 
        my $t = UCW::CGI::html_escape($m->{title});
        my $w = $m->{w};
        my $h = $m->{h};
        print "<h1>$t</h1>\n" if $t ne "";
-       my $img = $CF{'PhotoUrlPrefix'} . $id . ".jpg";
+       my $img = $self->get('PhotoUrlPrefix') . $id . '.jpg';
        print "<p class=large><img src='$img' width=$w height=$h alt='$t'>\n";
 
-       html_bot();
+       $self->html_bot;
+}
+
+sub show_pre_thumbs($) {
+       my ($self) = @_;
 }
 
-sub show_list() {
-       html_top();
+sub show_post_thumbs($) {
+       my ($self) = @_;
+}
 
-       show_links($CF{'BackURL'}, $CF{'ParentURL'}, $CF{'FwdURL'});
-       print "<h1>$CF{'Title'}</h1>\n";
-       print "<h2>$CF{'SubTitle'}</h2>\n" if defined $CF{'SubTitle'};
+sub show_list($) {
+       my ($self) = @_;
+       $self->html_top;
 
-       my $thumb_fmt = $CF{'ThumbW'} . "x" . $CF{'ThumbH'};
-       my $thumb_meta = $meta->{thumb}->{$thumb_fmt} or die "No thumbnails for format $thumb_fmt found!\n";
+       $self->show_links($self->get('BackURL'), $self->get('ParentURL'), $self->get('FwdURL'));
+       print "<h1>", $self->get('Title'), "</h1>\n";
+       my $subtitle = $self->get('SubTitle');
+       print "<h2>$subtitle</h2>\n" if $subtitle ne "";
+       $self->show_pre_thumbs;
 
-       for my $idx (1..$num_photos) {
+       my $meta = $self->{meta};
+       for my $idx (1..$self->{num_photos}) {
                my $id = $meta->{sequence}->[$idx-1];
-               my $m = $meta->{photo}->{$id} or die;
-               my $tm = $thumb_meta->{$id} or die;
-
-               my $annot = UCW::CGI::html_escape($m->{title});
-               my $tw = $tm->{w};
-               my $th = $tm->{h};
-               my $thumb = $CF{'ThumbUrlPrefix'} . "/$thumb_fmt/$id.jpg";
-               my $side_w = $CF{"ThumbW"} + 2*$CF{"InteriorMargin"};
-               my $side_h = $CF{"ThumbH"} + 2*$CF{"InteriorMargin"};
-               my $box_w = $CF{"LeftW"} + $side_w + $CF{"RightW"};
-               my $box_h = $CF{"TopH"} + $side_h + $CF{"BotH"};
-               print "<div class=thf><div class=thumb>\n";
-               print "<img src='$CF{'TopImg'}' width=$box_w height=$CF{'TopH'} alt='' class=tt>\n";
-               print "<img src='$CF{'LeftImg'}' width=$CF{'LeftW'} height=$side_h alt='' class=tl>\n";
-               my $ol = $CF{'LeftW'} + $CF{'InteriorMargin'} + int(($CF{'ThumbW'} - $tw)/2);
-               my $ot = $CF{'TopH'} + $CF{'InteriorMargin'} + int(($CF{'ThumbH'} - $th)/2);
-               my $tit = ($annot ne "") ? " title=\"$annot\"" : "";
-               my $url = ($CF{"ImageSubpages"} ? "?i=$id" : $orig);
-               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";
-               print "<img src='$CF{'RightImg'}' width=$CF{'RightW'} height=$side_h alt='' class=tr>\n";
-               print "<img src='$CF{'BotImg'}' width=$box_w height=$CF{'BotH'} alt='' class=tb>\n";
-               print "</div>\n";
-               print "</div>\n\n";
+               my $click_url;
+               if ($self->get('WebImageSubpages')) {
+                       $click_url = "?i=$idx";
+               } else {
+                       $click_url = $self->get('PhotoUrlPrefix') . "$id.jpg";
+               }
+               $self->show_thumb($meta, $id, $click_url);
        }
 
-       html_bot();
+       $self->show_post_thumbs;
+       $self->html_bot();
 }
 
-sub Dispatch() {
+sub dispatch($) {
+       my ($self) = @_;
+       binmode STDOUT, ':utf8';
        UCW::CGI::parse_args(\%args);
-       UCW::Gallery::LoadConfig();
-       $meta = UCW::Gallery::ReadMeta(File::Spec->catfile($CF{'CacheDir'}, 'cache.meta'));
-       $num_photos = scalar @{$meta->{sequence}};
+       $self->{meta} = $self->{gal}->read_meta(File::Spec->catfile($self->get('CacheDir'), 'cache.meta'));
+       $self->{num_photos} = scalar @{$self->{meta}->{sequence}};
 
-       if ($show_img ne "") {
-               show_img();
+       if ($want_archive) {
+               require UCW::Gallery::Archive;
+               UCW::Gallery::Archive::send_archive($self->{gal}, $self->{meta});
+       } elsif ($show_img ne "") {
+               $self->show_img;
        } else {
-               show_list();
+               $self->show_list;
        }
 }
 
+sub attach($$) {
+       my ($class, $gal) = @_;
+       my $self = { gal => $gal };
+       $gal->def(
+               WebFE => $self,
+
+               # Extras are either strings or functions called with the current gallery object as parameter
+               WebHeadExtras => "",
+               WebTopExtras => "",
+               WebBotExtras => "",
+
+               # Used by the theming logic
+               WebThemeCSS => undef,
+
+               # 1 if thumbnail link to sub-pages with images, 0 if they link directly to image files
+               WebImageSubpages => 1,
+
+               # If enabled, calling the CGI with a=zip produces a ZIP archive with all photos.
+               WebAllowArchives => 1,
+       );
+       bless $self, $class;
+       return $self;
+}
+
 42;