initial checkin of module files for proper perl installation
[freeside.git] / FS / FS / CGI.pm
1 package FS::CGI;
2
3 use strict;
4 use vars qw(@EXPORT_OK @ISA);
5 use Exporter;
6 use CGI;
7 use URI::URL;
8 use CGI::Carp qw(fatalsToBrowser);
9 use FS::UID;
10
11 @ISA = qw(Exporter);
12 @EXPORT_OK = qw(header menubar idiot eidiot popurl table itable ntable);
13
14 =head1 NAME
15
16 FS::CGI - Subroutines for the web interface
17
18 =head1 SYNOPSIS
19
20   use FS::CGI qw(header menubar idiot eidiot popurl);
21
22   print header( 'Title', '' );
23   print header( 'Title', menubar('item', 'URL', ... ) );
24
25   idiot "error message"; 
26   eidiot "error message";
27
28   $url = popurl; #returns current url
29   $url = popurl(3); #three levels up
30
31 =head1 DESCRIPTION
32
33 Provides a few common subroutines for the web interface.
34
35 =head1 SUBROUTINES
36
37 =over 4
38
39 =item header TITLE, MENUBAR
40
41 Returns an HTML header.
42
43 =cut
44
45 sub header {
46   my($title,$menubar)=@_;
47
48   my $x =  <<END;
49     <HTML>
50       <HEAD>
51         <TITLE>
52           $title
53         </TITLE>
54       </HEAD>
55       <BODY BGCOLOR="#e8e8e8">
56           <FONT SIZE=7>
57             $title
58           </FONT>
59           <BR><BR>
60 END
61   $x .=  $menubar. "<BR><BR>" if $menubar;
62   $x;
63 }
64
65 =item menubar ITEM, URL, ...
66
67 Returns an HTML menubar.
68
69 =cut
70
71 sub menubar { #$menubar=menubar('Main Menu', '../', 'Item', 'url', ... );
72   my($item,$url,@html);
73   while (@_) {
74     ($item,$url)=splice(@_,0,2);
75     push @html, qq!<A HREF="$url">$item</A>!;
76   }
77   join(' | ',@html);
78 }
79
80 =item idiot ERROR
81
82 This is depriciated.  Don't use it.
83
84 Sends headers and an HTML error message.
85
86 =cut
87
88 sub idiot {
89   #warn "idiot depriciated";
90   my($error)=@_;
91   my $cgi = &FS::UID::cgi();
92   if ( $cgi->isa('CGI::Base') ) {
93     no strict 'subs';
94     &CGI::Base::SendHeaders;
95   } else {
96     print $cgi->header( '-expires' => 'now' );
97   }
98   print <<END;
99 <HTML>
100   <HEAD>
101     <TITLE>Error processing your request</TITLE>
102   </HEAD>
103   <BODY>
104     <CENTER>
105     <H4>Error processing your request</H4>
106     </CENTER>
107     Your request could not be processed because of the following error:
108     <P><B>$error</B>
109   </BODY>
110 </HTML>
111 END
112
113 }
114
115 =item eidiot ERROR
116
117 This is depriciated.  Don't use it.
118
119 Sends headers and an HTML error message, then exits.
120
121 =cut
122
123 sub eidiot {
124   #warn "eidiot depriciated";
125   idiot(@_);
126   exit;
127 }
128
129 =item popurl LEVEL
130
131 Returns current URL with LEVEL levels of path removed from the end (default 0).
132
133 =cut
134
135 sub popurl {
136   my($up)=@_;
137   my($cgi)=&FS::UID::cgi;
138   my($url)=new URI::URL $cgi->url;
139   my(@path)=$url->path_components;
140   splice @path, 0-$up;
141   $url->path_components(@path);
142   my $x = $url->as_string;
143   $x .= '/' unless $x =~ /\/$/;
144   $x;
145 }
146
147 =item table
148
149 Returns HTML tag for beginning a table.
150
151 =cut
152
153 sub table {
154   my $col = shift;
155   if ( $col ) {
156     qq!<TABLE BGCOLOR="$col" BORDER=1 WIDTH="100%">!;
157   } else { 
158     "<TABLE BORDER=1>";
159   }
160 }
161
162 =item itable
163
164 Returns HTML tag for beginning an (invisible) table.
165
166 =cut
167
168 sub itable {
169   my $col = shift;
170   my $cellspacing = shift || 0;
171   if ( $col ) {
172     qq!<TABLE BGCOLOR="$col" BORDER=0 CELLSPACING=$cellspacing WIDTH="100%">!;
173   } else {
174     qq!<TABLE BORDER=0 CELLSPACING=$cellspacing WIDTH="100%">!;
175   }
176 }
177
178 =item ntable
179
180 This is getting silly.
181
182 =cut
183
184 sub ntable {
185   my $col = shift;
186   my $cellspacing = shift || 0;
187   if ( $col ) {
188     qq!<TABLE BGCOLOR="$col" BORDER=0 CELLSPACING=$cellspacing>!;
189   } else {
190     "<TABLE BORDER>";
191   }
192
193 }
194
195 =back
196
197 =head1 BUGS
198
199 Not OO.
200
201 Not complete.
202
203 =head1 SEE ALSO
204
205 L<CGI>, L<CGI::Base>
206
207 =cut
208
209 1;
210
211