jobd: unify error/status reporting
[girocco.git] / Girocco / CGI.pm
blobb7baa2710328f48fd6d0a96927a2ba2ff9beb9b1
1 package Girocco::CGI;
3 use strict;
4 use warnings;
6 use Girocco::Config;
8 BEGIN {
9 our $VERSION = '0.1';
10 our @ISA = qw(Exporter);
11 our @EXPORT = qw(html_esc);
13 use CGI qw(:standard :escapeHTML -nosticky);
14 use CGI::Util qw(unescape);
15 use CGI::Carp qw(fatalsToBrowser);
19 sub new {
20 my $class = shift;
21 my ($heading, $section, $extrahead) = @_;
22 my $gcgi = {};
24 $section ||= 'administration';
25 $extrahead ||= '';
27 $gcgi->{cgi} = CGI->new;
29 my $cgiurl = $gcgi->{cgi}->url(-absolute => 1);
30 ($gcgi->{srcname}) = ($cgiurl =~ m#^.*/\([a-zA-Z0-9_.\/-]+?\.cgi\)$#); #
31 $gcgi->{srcname} = "cgi/".$gcgi->{srcname} if $gcgi->{srcname};
33 print $gcgi->{cgi}->header(-type=>'text/html', -charset => 'utf-8');
35 print <<EOT;
36 <?xml version="1.0" encoding="utf-8"?>
37 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
38 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
40 <head>
41 <title>$Girocco::Config::name :: $heading</title>
42 <link rel="stylesheet" type="text/css" href="$Girocco::Config::gitwebfiles/gitweb.css"/>
43 <link rel="stylesheet" type="text/css" href="$Girocco::Config::gitwebfiles/girocco.css"/>
44 <link rel="shortcut icon" href="$Girocco::Config::gitwebfiles/git-favicon.png" type="image/png"/>
45 <script src="$Girocco::Config::gitwebfiles/mootools.js" type="text/javascript"></script>
46 <script src="$Girocco::Config::gitwebfiles/girocco.js" type="text/javascript"></script>
47 $extrahead</head>
49 <body>
51 <div class="page_header">
52 <a href="http://git.or.cz/" title="Git homepage"><img src="$Girocco::Config::gitwebfiles/git-logo.png" width="72" height="27" alt="git" style="float:right; border-width:0px;"/></a>
53 <a href="$Girocco::Config::gitweburl">$Girocco::Config::name</a> / $section / $heading
54 </div>
56 EOT
58 bless $gcgi, $class;
61 sub DESTROY {
62 my $self = shift;
63 if ($self->{srcname} and $Girocco::Config::giroccourl) {
64 my $hb = $Girocco::Config::giroccobranch ?
65 "hb=$Girocco::Config::giroccobranch;" : "";
66 print <<EOT;
67 <div align="right">
68 <a href="$Girocco::Config::giroccourl?a=blob;${hb}f=$self->{srcname}">(view source)</a>
69 </div>
70 EOT
72 print <<EOT;
73 </body>
74 </html>
75 EOT
78 sub cgi {
79 my $self = shift;
80 $self->{cgi};
83 sub err {
84 my $self = shift;
85 print "<p style=\"color: red\">@_</p>\n";
86 $self->{err}++;
89 sub err_check {
90 my $self = shift;
91 my $err = $self->{err};
92 my $s = $err == 1 ? '' : 's';
93 $err and print "<p style=\"font-weight: bold\">Operation aborted due to $err error$s.</p>\n";
94 $err;
97 sub wparam {
98 my $self = shift;
99 my ($param) = @_;
100 my $val = $self->{cgi}->param($param);
101 defined $val and $val =~ s/^\s*(.*?)\s*$/$1/;
102 $val;
105 sub srcname {
106 my $self = shift;
107 my ($srcname) = @_;
108 $self->{srcname} = $srcname if $srcname;
109 $self->{srcname};
112 sub html_esc {
113 my ($str) = @_;
114 $str =~ s/&/&amp;/g;
115 $str =~ s/</&lt;/g; $str =~ s/>/&gt;/g;
116 $str =~ s/"/&quot;/g;
117 $str;
120 sub print_form_fields {
121 my $self = shift;
122 my ($fieldmap, $valuemap, @fields) = @_;
124 foreach my $field (map { $fieldmap->{$_} } @fields) {
125 print '<tr><td class="formlabel">'.$field->[0].':</td><td>';
126 if ($field->[2] eq 'text') {
127 print '<input type="text" name="'.$field->[1].'" size="80"';
128 print ' value="'.$valuemap->{$field->[1]}.'"' if $valuemap;
129 print ' />';
130 } else {
131 print '<textarea name="'.$field->[1].'" rows="5" cols="80">';
132 print $valuemap->{$field->[1]} if $valuemap;
133 print '</textarea>';
135 print "</td></tr>\n";