]> git.imager.perl.org - bse.git/blame - site/cgi-bin/printable.pl
re-work coupons to allow multiple coupon types
[bse.git] / site / cgi-bin / printable.pl
CommitLineData
88fdaf77
TC
1#!/usr/bin/perl -w
2use strict;
f8282904
TC
3use FindBin;
4use lib "$FindBin::Bin/modules";
88fdaf77
TC
5use CGI qw(:standard);
6use Carp; # 'verbose'; # remove the 'verbose' in production
e0ed81d7 7use BSE::TB::Articles;
0b406a07 8use BSE::Cfg;
efca965f 9use BSE::DB;
0b406a07
TC
10use BSE::Template;
11
12my $cfg = BSE::Cfg->new;
efca965f 13BSE::DB->init($cfg);
88fdaf77
TC
14
15my $id = param('id');
16defined $id or $id = 1;
17# the reason to avoid creating an articles object is that this
18# loads all of the articles into memory
19# hopefully a print template won't include too many references to
20# other articles anyway
e0ed81d7 21#my $articles = BSE::TB::Articles->new;
88fdaf77 22
e0ed81d7 23my $article = BSE::TB::Articles->getByPkey($id)
88fdaf77
TC
24 or error_page("No article with id $id found");
25
26eval "use $article->{generator}";
27die $@ if $@;
e0ed81d7 28my $generator = $article->{generator}->new(articles=>'BSE::TB::Articles', cfg => $cfg,
2d873eb6 29 top => $article);
88fdaf77
TC
30
31my $template = param('template');
32
33# make sure they don't try to work outside the
34my $file;
0b406a07 35my $type;
88fdaf77
TC
36if ($template) {
37 if ($template =~ m!^/! || $template =~ /\.\./ || $template !~ /\.tmpl$/i) {
38 error_page("Invalid template name '$template'");
39 }
40
0b406a07
TC
41 $file = file_from_template($template);
42 -e $file
88fdaf77 43 or error_page("Cannot find template '$template'");
0b406a07
TC
44
45 $type = $cfg->entry('printable types', $template)
46 || BSE::Template->html_type($cfg);
88fdaf77
TC
47}
48else {
49 for my $work ($article->{template}, 'printable.tmpl') {
50 next unless $work =~ /\S/;
51 $template = $work;
52 $file = file_from_template($work);
220c179a 53 last if $file && -e $file;
88fdaf77
TC
54 }
55 -e $file or error_page("No template available for this page");
0b406a07 56 $type = BSE::Template->html_type($cfg);
88fdaf77
TC
57}
58
59open TMPLT, "< $file"
60 or error_page("Cannot open template '$template'");
61my $text = do { local $/; <TMPLT> };
62close TMPLT;
63
64$text =~ s/<:\s*embed\s+(?:start|end)\s*:>//g;
65
0b406a07 66print "Content-Type: $type\n\n";
e0ed81d7 67print $generator->generate_low($text, $article, 'BSE::TB::Articles');
88fdaf77 68
aefcabcb 69
88fdaf77
TC
70sub error_page {
71 my ($error) = @_;
72 $error ||= "Unknown error";
73
220c179a 74 my %article;
e0ed81d7 75 my @cols = BSE::TB::Article->columns;
220c179a
TC
76 @article{@cols} = ('') x @cols;
77 $article{id} = -10;
78 $article{title} = "Error";
79 $article{parentid} = -1;
80
39e87dbd
AO
81 require BSE::Generate::Article;
82 my $gen = BSE::Generate::Article->new(cfg=>$cfg, top => \%article);
88fdaf77
TC
83 my %acts;
84 %acts =
85 (
e0ed81d7 86 $gen->baseActs('BSE::TB::Articles', \%acts, \%article),
88fdaf77
TC
87 error => sub { CGI::escapeHTML($error) },
88 );
aefcabcb
TC
89
90 BSE::Template->show_page('error', $cfg, \%acts);
88fdaf77
TC
91 exit;
92}
93
94sub file_from_template {
95 my ($template) = @_;
96
220c179a 97 return BSE::Template->find_source("printable/$template", $cfg);
88fdaf77 98}