]> git.sesse.net Git - ccbs/blob - html/ccbs.pm
Add a "general facts" to the score screen.
[ccbs] / html / ccbs.pm
1 package ccbs;
2 use Template;
3 use CGI;
4 use DBI;
5 use strict;
6 use warnings;
7
8 our $ccbs_dbdebug = 0;
9         
10 sub print_header {
11         print CGI::header(-type=>'text/html; charset=utf-8');
12 }
13
14 sub db_connect {
15         $ccbs_dbdebug = defined(shift) ? 1 : 0;
16
17         my $dbh = DBI->connect("dbi:Pg:dbname=ccbs;host=sql.samfundet.no", "ccbs", "GeT|>>B_")
18                 or die "Couldn't connect to database";
19         $dbh->{RaiseError} = 1;
20         return $dbh;
21 }
22
23 sub db_fetch_all {
24         my ($dbh, $sql, @parms) = @_;
25         my $q = $dbh->prepare($sql)
26                 or die "Could not prepare query: " . $dbh->errstr;
27         $q->execute(@parms)
28                 or die "Could not execute query: " . $dbh->errstr;
29
30         if ($ccbs_dbdebug) {
31                 warn $sql;
32                 warn "params=" . join(', ', @parms);
33         }
34         
35         my @ret = ();
36         while (my $ref = $q->fetchrow_hashref()) {
37                 if ($ccbs_dbdebug) {
38                         my $dbstr = "";
39                         for my $k (sort keys %$ref) {
40                                 $dbstr .= " " . $k . "=" . $ref->{$k};
41                         }
42                         warn $dbstr;
43                 }
44                 push @ret, $ref;
45         }
46
47         $q->finish;
48         return \@ret;
49 }
50         
51 sub process_template {
52         my ($page, $title, $vars) = @_;
53         $vars->{'page'} = $page;
54         $vars->{'title'} = $title;
55         
56         my $config = {
57                 INCLUDE_PATH => 'templates/',
58                 INTERPOLATE  => 1,
59                 POST_CHOMP   => 1,
60                 EVAL_PERL    => 1,
61         };
62         my $template = Template->new($config);
63
64         my $output = '';
65         $template->process('main.tmpl', $vars, \$output)
66                 or die $template->error();
67
68         print $output;
69 }
70
71 $SIG{__DIE__} = sub {
72         # Gosh! Net::Resolver::DNS is brain-damaged.
73         my $msg = shift;
74         return if $msg =~ m#Win32/Registry.pm#;
75
76         ccbs::print_header();
77         ccbs::process_template('error.tmpl', 'Internal Server Error',
78                 { message => $msg });
79 };
80
81 1;