use Apache::exit instead of exit in &eidiot - Registry wasn't overriding exit
[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   use Apache;
128   Apache::exit;
129 }
130
131 =item popurl LEVEL
132
133 Returns current URL with LEVEL levels of path removed from the end (default 0).
134
135 =cut
136
137 sub popurl {
138   my($up)=@_;
139   my($cgi)=&FS::UID::cgi;
140   my($url)=new URI::URL $cgi->url;
141   my(@path)=$url->path_components;
142   splice @path, 0-$up;
143   $url->path_components(@path);
144   my $x = $url->as_string;
145   $x .= '/' unless $x =~ /\/$/;
146   $x;
147 }
148
149 =item table
150
151 Returns HTML tag for beginning a table.
152
153 =cut
154
155 sub table {
156   my $col = shift;
157   if ( $col ) {
158     qq!<TABLE BGCOLOR="$col" BORDER=1 WIDTH="100%">!;
159   } else { 
160     "<TABLE BORDER=1>";
161   }
162 }
163
164 =item itable
165
166 Returns HTML tag for beginning an (invisible) table.
167
168 =cut
169
170 sub itable {
171   my $col = shift;
172   my $cellspacing = shift || 0;
173   if ( $col ) {
174     qq!<TABLE BGCOLOR="$col" BORDER=0 CELLSPACING=$cellspacing WIDTH="100%">!;
175   } else {
176     qq!<TABLE BORDER=0 CELLSPACING=$cellspacing WIDTH="100%">!;
177   }
178 }
179
180 =item ntable
181
182 This is getting silly.
183
184 =cut
185
186 sub ntable {
187   my $col = shift;
188   my $cellspacing = shift || 0;
189   if ( $col ) {
190     qq!<TABLE BGCOLOR="$col" BORDER=0 CELLSPACING=$cellspacing>!;
191   } else {
192     "<TABLE BORDER>";
193   }
194
195 }
196
197 =back
198
199 =head1 BUGS
200
201 Not OO.
202
203 Not complete.
204
205 =head1 SEE ALSO
206
207 L<CGI>, L<CGI::Base>
208
209 =cut
210
211 1;
212
213