This commit was generated by cvs2svn to compensate for changes in r2526,
[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                 small_custview myexit http_header);
14
15 =head1 NAME
16
17 FS::CGI - Subroutines for the web interface
18
19 =head1 SYNOPSIS
20
21   use FS::CGI qw(header menubar idiot eidiot popurl);
22
23   print header( 'Title', '' );
24   print header( 'Title', menubar('item', 'URL', ... ) );
25
26   idiot "error message"; 
27   eidiot "error message";
28
29   $url = popurl; #returns current url
30   $url = popurl(3); #three levels up
31
32 =head1 DESCRIPTION
33
34 Provides a few common subroutines for the web interface.
35
36 =head1 SUBROUTINES
37
38 =over 4
39
40 =item header TITLE, MENUBAR
41
42 Returns an HTML header.
43
44 =cut
45
46 sub header {
47   my($title,$menubar,$etc)=@_; #$etc is for things like onLoad= etc.
48   #use Carp;
49   $etc = '' unless defined $etc;
50
51   my $x =  <<END;
52     <HTML>
53       <HEAD>
54         <TITLE>
55           $title
56         </TITLE>
57         <META HTTP-Equiv="Cache-Control" Content="no-cache">
58         <META HTTP-Equiv="Pragma" Content="no-cache">
59         <META HTTP-Equiv="Expires" Content="0"> 
60       </HEAD>
61       <BODY BGCOLOR="#e8e8e8"$etc>
62           <FONT SIZE=7>
63             $title
64           </FONT>
65           <BR><BR>
66 END
67   $x .=  $menubar. "<BR><BR>" if $menubar;
68   $x;
69 }
70
71 =item http_header
72
73 Sets an http header.
74
75 =cut
76
77 sub http_header {
78   my ( $header, $value ) = @_;
79   if (exists $ENV{MOD_PERL}) {
80     if ( defined $main::Response
81          && $main::Response->isa('Apache::ASP::Response') ) {  #Apache::ASP
82       if ( $header =~ /^Content-Type$/ ) {
83         $main::Response->{ContentType} = $value;
84       } else {
85         $main::Response->AddHeader( $header => $value );
86       }
87     } elsif ( defined $HTML::Mason::Commands::r  ) { #Mason
88       ## is this the correct pacakge for $r ???  for 1.0x and 1.1x ?
89       if ( $header =~ /^Content-Type$/ ) {
90         $HTML::Mason::Commands::r->content_type($value);
91       } else {
92         $HTML::Mason::Commands::r->header_out( $header => $value );
93       }
94     } else {
95       die "http_header called in unknown environment";
96     }
97   } else {
98     die "http_header called not running under mod_perl";
99   }
100
101 }
102
103 =item menubar ITEM, URL, ...
104
105 Returns an HTML menubar.
106
107 =cut
108
109 sub menubar { #$menubar=menubar('Main Menu', '../', 'Item', 'url', ... );
110   my($item,$url,@html);
111   while (@_) {
112     ($item,$url)=splice(@_,0,2);
113     push @html, qq!<A HREF="$url">$item</A>!;
114   }
115   join(' | ',@html);
116 }
117
118 =item idiot ERROR
119
120 This is depriciated.  Don't use it.
121
122 Sends an HTML error message.
123
124 =cut
125
126 sub idiot {
127   #warn "idiot depriciated";
128   my($error)=@_;
129 #  my $cgi = &FS::UID::cgi();
130 #  if ( $cgi->isa('CGI::Base') ) {
131 #    no strict 'subs';
132 #    &CGI::Base::SendHeaders;
133 #  } else {
134 #    print $cgi->header( @FS::CGI::header );
135 #  }
136   print <<END;
137 <HTML>
138   <HEAD>
139     <TITLE>Error processing your request</TITLE>
140     <META HTTP-Equiv="Cache-Control" Content="no-cache">
141     <META HTTP-Equiv="Pragma" Content="no-cache">
142     <META HTTP-Equiv="Expires" Content="0"> 
143   </HEAD>
144   <BODY>
145     <CENTER>
146     <H4>Error processing your request</H4>
147     </CENTER>
148     Your request could not be processed because of the following error:
149     <P><B>$error</B>
150   </BODY>
151 </HTML>
152 END
153
154 }
155
156 =item eidiot ERROR
157
158 This is depriciated.  Don't use it.
159
160 Sends an HTML error message, then exits.
161
162 =cut
163
164 sub eidiot {
165   warn "eidiot depriciated";
166   $HTML::Mason::Commands::r->send_http_header
167     if defined $HTML::Mason::Commands::r;
168   idiot(@_);
169   &myexit();
170 }
171
172 =item myexit
173
174 You probably shouldn't use this; but if you must:
175
176 If running under mod_perl, calles Apache::exit, otherwise, calls exit.
177
178 =cut
179
180 sub myexit {
181   if (exists $ENV{MOD_PERL}) {
182
183     if ( defined $main::Response
184          && $main::Response->isa('Apache::ASP::Response') ) {  #Apache::ASP
185       $main::Response->End();
186       require Apache;
187       Apache::exit();
188     } elsif ( defined $HTML::Mason::Commands::m  ) { #Mason
189       #$HTML::Mason::Commands::m->flush_buffer();
190       $HTML::Mason::Commands::m->abort();
191       die "shouldn't fall through to here (mason \$m->abort didn't)";
192     } else {
193       #??? well, it is $ENV{MOD_PERL}
194       warn "running under unknown mod_perl environment; trying Apache::exit()";
195       require Apache;
196       Apache::exit();
197     }
198   } else {
199     exit;
200   }
201 }
202
203 =item popurl LEVEL
204
205 Returns current URL with LEVEL levels of path removed from the end (default 0).
206
207 =cut
208
209 sub popurl {
210   my($up)=@_;
211   my $cgi = &FS::UID::cgi;
212   my $url = new URI::URL ( $cgi->isa('Apache') ? $cgi->uri : $cgi->url );
213   my(@path)=$url->path_components;
214   splice @path, 0-$up;
215   $url->path_components(@path);
216   my $x = $url->as_string;
217   $x .= '/' unless $x =~ /\/$/;
218   $x;
219 }
220
221 =item table
222
223 Returns HTML tag for beginning a table.
224
225 =cut
226
227 sub table {
228   my $col = shift;
229   if ( $col ) {
230     qq!<TABLE BGCOLOR="$col" BORDER=1 WIDTH="100%" CELLSPACING=0 CELLPADDING=2 BORDERCOLOR="#999999">!;
231   } else { 
232     '<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=2 BORDERCOLOR="#999999">';
233   }
234 }
235
236 =item itable
237
238 Returns HTML tag for beginning an (invisible) table.
239
240 =cut
241
242 sub itable {
243   my $col = shift;
244   my $cellspacing = shift || 0;
245   if ( $col ) {
246     qq!<TABLE BGCOLOR="$col" BORDER=0 CELLSPACING=$cellspacing WIDTH="100%">!;
247   } else {
248     qq!<TABLE BORDER=0 CELLSPACING=$cellspacing WIDTH="100%">!;
249   }
250 }
251
252 =item ntable
253
254 This is getting silly.
255
256 =cut
257
258 sub ntable {
259   my $col = shift;
260   my $cellspacing = shift || 0;
261   if ( $col ) {
262     qq!<TABLE BGCOLOR="$col" BORDER=0 CELLSPACING=$cellspacing>!;
263   } else {
264     '<TABLE BORDER CELLSPACING=0 CELLPADDING=2 BORDERCOLOR="#999999">';
265   }
266
267 }
268
269 =item small_custview CUSTNUM || CUST_MAIN_OBJECT, COUNTRYDEFAULT
270
271 Sheesh. I should just switch to Mason.
272
273 =cut
274
275 sub small_custview {
276   use FS::Record qw(qsearchs);
277   use FS::cust_main;
278
279   my $arg = shift;
280   my $countrydefault = shift || 'US';
281
282   my $cust_main = ref($arg) ? $arg
283                   : qsearchs('cust_main', { 'custnum' => $arg } )
284     or die "unknown custnum $arg";
285
286   my $html = 'Customer #<B>'. $cust_main->custnum. '</B>'.
287     ntable('#e8e8e8'). '<TR><TD>'. ntable("#cccccc",2).
288     '<TR><TD ALIGN="right" VALIGN="top">Billing<BR>Address</TD><TD BGCOLOR="#ffffff">'.
289     $cust_main->getfield('last'). ', '. $cust_main->first. '<BR>';
290
291   $html .= $cust_main->company. '<BR>' if $cust_main->company;
292   $html .= $cust_main->address1. '<BR>';
293   $html .= $cust_main->address2. '<BR>' if $cust_main->address2;
294   $html .= $cust_main->city. ', '. $cust_main->state. '  '. $cust_main->zip. '<BR>';
295   $html .= $cust_main->country. '<BR>'
296     if $cust_main->country && $cust_main->country ne $countrydefault;
297
298   $html .= '</TD></TR></TABLE></TD>';
299
300   if ( defined $cust_main->dbdef_table->column('ship_last') ) {
301
302     my $pre = $cust_main->ship_last ? 'ship_' : '';
303
304     $html .= '<TD>'. ntable("#cccccc",2).
305       '<TR><TD ALIGN="right" VALIGN="top">Service<BR>Address</TD><TD BGCOLOR="#ffffff">'.
306       $cust_main->get("${pre}last"). ', '.
307       $cust_main->get("${pre}first"). '<BR>';
308     $html .= $cust_main->get("${pre}company"). '<BR>'
309       if $cust_main->get("${pre}company");
310     $html .= $cust_main->get("${pre}address1"). '<BR>';
311     $html .= $cust_main->get("${pre}address2"). '<BR>'
312       if $cust_main->get("${pre}address2");
313     $html .= $cust_main->get("${pre}city"). ', '.
314              $cust_main->get("${pre}state"). '  '.
315              $cust_main->get("${pre}ship_zip"). '<BR>';
316     $html .= $cust_main->get("${pre}country"). '<BR>'
317       if $cust_main->get("${pre}country")
318          && $cust_main->get("${pre}country") ne $countrydefault;
319
320     $html .= '</TD></TR></TABLE></TD>';
321   }
322
323   $html .= '</TR></TABLE>';
324
325   $html .= '<BR>Balance: <B>$'. $cust_main->balance. '</B><BR>';
326
327   # last payment might be good here too?
328
329   $html;
330 }
331
332 =back
333
334 =head1 BUGS
335
336 Not OO.
337
338 Not complete.
339
340 small_custview sooooo doesn't belong here.  i should just switch to Mason.
341
342 =head1 SEE ALSO
343
344 L<CGI>, L<CGI::Base>
345
346 =cut
347
348 1;
349
350