not supporting Apache::ASP anymore
[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 rooturl 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   use Carp;
48   carp 'FS::CGI::header deprecated; include /elements/header.html instead';
49
50   my($title,$menubar,$etc)=@_; #$etc is for things like onLoad= etc.
51   $etc = '' unless defined $etc;
52
53   my $x =  <<END;
54     <HTML>
55       <HEAD>
56         <TITLE>
57           $title
58         </TITLE>
59         <META HTTP-Equiv="Cache-Control" Content="no-cache">
60         <META HTTP-Equiv="Pragma" Content="no-cache">
61         <META HTTP-Equiv="Expires" Content="0"> 
62       </HEAD>
63       <BODY BGCOLOR="#e8e8e8"$etc>
64           <FONT SIZE=6>
65             <CENTER>$title</CENTER>
66           </FONT>
67           <BR><!--<BR>-->
68 END
69   $x .=  $menubar. "<BR><BR>" if $menubar;
70   $x;
71 }
72
73 =item http_header
74
75 Sets an http header.
76
77 =cut
78
79 sub http_header {
80   my ( $header, $value ) = @_;
81   if (exists $ENV{MOD_PERL}) {
82     if ( defined $HTML::Mason::Commands::r  ) { #Mason
83       ## is this the correct pacakge for $r ???  for 1.0x and 1.1x ?
84       if ( $header =~ /^Content-Type$/ ) {
85         $HTML::Mason::Commands::r->content_type($value);
86       } else {
87         $HTML::Mason::Commands::r->header_out( $header => $value );
88       }
89     } else {
90       die "http_header called in unknown environment";
91     }
92   } else {
93     die "http_header called not running under mod_perl";
94   }
95
96 }
97
98 =item menubar ITEM, URL, ...
99
100 Returns an HTML menubar.
101
102 =cut
103
104 sub menubar { #$menubar=menubar('Main Menu', '../', 'Item', 'url', ... );
105   use Carp;
106   carp 'FS::CGI::menubar deprecated; include /elements/menubar.html instead';
107
108   my($item,$url,@html);
109   while (@_) {
110     ($item,$url)=splice(@_,0,2);
111     next if $item =~ /^\s*Main\s+Menu\s*$/i;
112     push @html, qq!<A HREF="$url">$item</A>!;
113   }
114   join(' | ',@html);
115 }
116
117 =item idiot ERROR
118
119 This is depriciated.  Don't use it.
120
121 Sends an HTML error message.
122
123 =cut
124
125 sub idiot {
126   #warn "idiot depriciated";
127   my($error)=@_;
128 #  my $cgi = &FS::UID::cgi();
129 #  if ( $cgi->isa('CGI::Base') ) {
130 #    no strict 'subs';
131 #    &CGI::Base::SendHeaders;
132 #  } else {
133 #    print $cgi->header( @FS::CGI::header );
134 #  }
135   print <<END;
136 <HTML>
137   <HEAD>
138     <TITLE>Error processing your request</TITLE>
139     <META HTTP-Equiv="Cache-Control" Content="no-cache">
140     <META HTTP-Equiv="Pragma" Content="no-cache">
141     <META HTTP-Equiv="Expires" Content="0"> 
142   </HEAD>
143   <BODY>
144     <CENTER>
145     <H4>Error processing your request</H4>
146     </CENTER>
147     Your request could not be processed because of the following error:
148     <P><B>$error</B>
149   </BODY>
150 </HTML>
151 END
152
153 }
154
155 =item eidiot ERROR
156
157 This is depriciated.  Don't use it.
158
159 Sends an HTML error message, then exits.
160
161 =cut
162
163 sub eidiot {
164   warn "eidiot depriciated";
165   $HTML::Mason::Commands::r->send_http_header
166     if defined $HTML::Mason::Commands::r;
167   idiot(@_);
168   &myexit();
169 }
170
171 =item myexit
172
173 You probably shouldn't use this; but if you must:
174
175 If running under mod_perl, calles Apache::exit, otherwise, calls exit.
176
177 =cut
178
179 sub myexit {
180   if (exists $ENV{MOD_PERL}) {
181
182     if ( defined $HTML::Mason::Commands::m  ) { #Mason
183       #$HTML::Mason::Commands::m->flush_buffer();
184       $HTML::Mason::Commands::m->abort();
185       die "shouldn't fall through to here (mason \$m->abort didn't)";
186     } else {
187       #??? well, it is $ENV{MOD_PERL}
188       warn "running under unknown mod_perl environment; trying Apache::exit()";
189       require Apache;
190       Apache::exit();
191     }
192   } else {
193     exit;
194   }
195 }
196
197 =item popurl LEVEL
198
199 Returns current URL with LEVEL levels of path removed from the end (default 0).
200
201 =cut
202
203 sub popurl {
204   my($up)=@_;
205   my $cgi = &FS::UID::cgi;
206   my $url_string = $cgi->isa('Apache') ? $cgi->uri : $cgi->url;
207   $url_string =~ s/\?.*//;
208   my $url = new URI::URL ( $url_string );
209   my(@path)=$url->path_components;
210   splice @path, 0-$up;
211   $url->path_components(@path);
212   my $x = $url->as_string;
213   $x .= '/' unless $x =~ /\/$/;
214   $x;
215 }
216
217 =item rooturl 
218
219 =cut
220
221 sub rooturl {
222   # better to start with the client-provided URL
223   my $cgi = &FS::UID::cgi;
224   my $url_string = $cgi->isa('Apache') ? $cgi->uri : $cgi->url;
225   $url_string =~ s/\?.*//;
226
227   #even though this is kludgy
228   $url_string =~ s{ / index\.html /? $ }
229                   {/}x;
230   $url_string =~
231     s{
232        /
233        (browse|config|docs|edit|graph|misc|search|view|pref|rt)
234        /
235        (process/)?
236        ([\w\-\.\/]+)
237        $
238      }
239      {}x;
240
241   $url_string .= '/' unless $url_string =~ /\/$/;
242
243   $url_string;
244
245 }
246
247 =item table
248
249 Returns HTML tag for beginning a table.
250
251 =cut
252
253 sub table {
254   use Carp;
255   carp 'FS::CGI::table deprecated; include /elements/table.html instead';
256
257   my $col = shift;
258   if ( $col ) {
259     qq!<TABLE BGCOLOR="$col" BORDER=1 WIDTH="100%" CELLSPACING=0 CELLPADDING=2 BORDERCOLOR="#999999">!;
260   } else { 
261     '<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=2 BORDERCOLOR="#999999">';
262   }
263 }
264
265 =item itable
266
267 Returns HTML tag for beginning an (invisible) table.
268
269 =cut
270
271 sub itable {
272   my $col = shift;
273   my $cellspacing = shift || 0;
274   my $width = ( scalar(@_) && shift ) ? '' : 'WIDTH="100%"';  #bah
275   if ( $col ) {
276     qq!<TABLE BGCOLOR="$col" BORDER=0 CELLSPACING=$cellspacing $width>!;
277   } else {
278     qq!<TABLE BORDER=0 CELLSPACING=$cellspacing $width>!;
279   }
280 }
281
282 =item ntable
283
284 This is getting silly.
285
286 =cut
287
288 sub ntable {
289   my $col = shift;
290   my $cellspacing = shift || 0;
291   if ( $col ) {
292     qq!<TABLE BGCOLOR="$col" BORDER=0 CELLSPACING=$cellspacing>!;
293   } else {
294     '<TABLE BORDER CELLSPACING=0 CELLPADDING=2 BORDERCOLOR="#999999">';
295   }
296
297 }
298
299 =item small_custview CUSTNUM || CUST_MAIN_OBJECT, COUNTRYDEFAULT, NOBALANCE_FLAG, URL
300
301 Sheesh. I should just switch to Mason.
302
303 =cut
304
305 sub small_custview {
306   use FS::Record qw(qsearchs);
307   use FS::cust_main;
308
309   my $arg = shift;
310   my $countrydefault = shift || 'US';
311   my $nobalance = shift;
312   my $url = shift;
313
314   my $cust_main = ref($arg) ? $arg
315                   : qsearchs('cust_main', { 'custnum' => $arg } )
316     or die "unknown custnum $arg";
317
318   my $html;
319   
320   $html = qq!View <A HREF="$url?! . $cust_main->custnum . '">'
321     if $url;
322
323   $html .= 'Customer #<B>'. $cust_main->custnum. '</B></A>'.
324     ' - <B><FONT COLOR="'. $cust_main->statuscolor. '">'.
325     ucfirst($cust_main->status). '</FONT></B>'.
326     ntable('#e8e8e8'). '<TR><TD VALIGN="top">'. ntable("#cccccc",2).
327     '<TR><TD ALIGN="right" VALIGN="top">Billing<BR>Address</TD><TD BGCOLOR="#ffffff">'.
328     $cust_main->getfield('last'). ', '. $cust_main->first. '<BR>';
329
330   $html .= $cust_main->company. '<BR>' if $cust_main->company;
331   $html .= $cust_main->address1. '<BR>';
332   $html .= $cust_main->address2. '<BR>' if $cust_main->address2;
333   $html .= $cust_main->city. ', '. $cust_main->state. '  '. $cust_main->zip. '<BR>';
334   $html .= $cust_main->country. '<BR>'
335     if $cust_main->country && $cust_main->country ne $countrydefault;
336
337   $html .= '</TD></TR><TR><TD></TD><TD BGCOLOR="#ffffff">';
338   if ( $cust_main->daytime && $cust_main->night ) {
339     use FS::Msgcat;
340     $html .= ( FS::Msgcat::_gettext('daytime') || 'Day' ).
341              ' '. $cust_main->daytime.
342              '<BR>'. ( FS::Msgcat::_gettext('night') || 'Night' ).
343              ' '. $cust_main->night;
344   } elsif ( $cust_main->daytime || $cust_main->night ) {
345     $html .= $cust_main->daytime || $cust_main->night;
346   }
347   if ( $cust_main->fax ) {
348     $html .= '<BR>Fax '. $cust_main->fax;
349   }
350
351   $html .= '</TD></TR></TABLE></TD>';
352
353   if ( defined $cust_main->dbdef_table->column('ship_last') ) {
354
355     my $pre = $cust_main->ship_last ? 'ship_' : '';
356
357     $html .= '<TD VALIGN="top">'. ntable("#cccccc",2).
358       '<TR><TD ALIGN="right" VALIGN="top">Service<BR>Address</TD><TD BGCOLOR="#ffffff">'.
359       $cust_main->get("${pre}last"). ', '.
360       $cust_main->get("${pre}first"). '<BR>';
361     $html .= $cust_main->get("${pre}company"). '<BR>'
362       if $cust_main->get("${pre}company");
363     $html .= $cust_main->get("${pre}address1"). '<BR>';
364     $html .= $cust_main->get("${pre}address2"). '<BR>'
365       if $cust_main->get("${pre}address2");
366     $html .= $cust_main->get("${pre}city"). ', '.
367              $cust_main->get("${pre}state"). '  '.
368              $cust_main->get("${pre}ship_zip"). '<BR>';
369     $html .= $cust_main->get("${pre}country"). '<BR>'
370       if $cust_main->get("${pre}country")
371          && $cust_main->get("${pre}country") ne $countrydefault;
372
373     $html .= '</TD></TR><TR><TD></TD><TD BGCOLOR="#ffffff">';
374
375     if ( $cust_main->get("${pre}daytime") && $cust_main->get("${pre}night") ) {
376       use FS::Msgcat;
377       $html .= ( FS::Msgcat::_gettext('daytime') || 'Day' ).
378                ' '. $cust_main->get("${pre}daytime").
379                '<BR>'. ( FS::Msgcat::_gettext('night') || 'Night' ).
380                ' '. $cust_main->get("${pre}night");
381     } elsif ( $cust_main->get("${pre}daytime")
382               || $cust_main->get("${pre}night") ) {
383       $html .= $cust_main->get("${pre}daytime")
384                || $cust_main->get("${pre}night");
385     }
386     if ( $cust_main->get("${pre}fax") ) {
387       $html .= '<BR>Fax '. $cust_main->get("${pre}fax");
388     }
389
390     $html .= '</TD></TR></TABLE></TD>';
391   }
392
393   $html .= '</TR></TABLE>';
394
395   $html .= '<BR>Balance: <B>$'. $cust_main->balance. '</B><BR>'
396     unless $nobalance;
397
398   # last payment might be good here too?
399
400   $html;
401 }
402
403 =back
404
405 =head1 BUGS
406
407 Not OO.
408
409 Not complete.
410
411 small_custview sooooo doesn't belong here.  i should just switch to Mason.
412
413 =head1 SEE ALSO
414
415 L<CGI>, L<CGI::Base>
416
417 =cut
418
419 1;
420
421