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