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