move cust_pkg search to new template, add active/suspended/cancelled customer package...
[freeside.git] / httemplate / search / elements / search.html
1 <%
2
3   my(%opt) = @_;
4   #warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
5
6   my %align = (
7     'l' => 'left',
8     'r' => 'right',
9     'c' => 'center',
10     ' ' => '',
11     '.' => '',
12   );
13   $opt{align} = [ map $align{$_}, split(//, $opt{align}) ],
14     unless !$opt{align} || ref($opt{align});
15
16   my $type = '';
17   my $limit = '';
18   my($maxrecords, $total, $offset, $count_arrayref);
19
20   if ( $cgi->param('_type') =~ /^(csv|\w*\.xls)$/ ) {
21   
22     $type = $1;
23
24   } else { #setup some pagination things if we're in html mode
25
26     unless (exists($opt{'count_query'}) && length($opt{'count_query'})) {
27       ( $opt{'count_query'} = $opt{'query'} ) =~
28         s/^\s*SELECT\s*(.*?)\s+FROM\s/SELECT COUNT(*) FROM /i;
29     }
30
31     my $conf = new FS::Conf;
32     $maxrecords = $conf->config('maxsearchrecordsperpage');
33
34     $limit = $maxrecords ? "LIMIT $maxrecords" : '';
35
36     $offset = $cgi->param('offset') || 0;
37     $limit .= " OFFSET $offset" if $offset;
38
39     my $count_sth = dbh->prepare($opt{'count_query'})
40       or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
41     $count_sth->execute
42       or die "Error executing $opt{'count_query'}: ". $count_sth->errstr;
43     $count_arrayref = $count_sth->fetchrow_arrayref;
44     $total = $count_arrayref->[0];
45
46   }
47
48   # run the query
49
50   my $header = $opt{'header'};
51   my $rows;
52   if ( ref($opt{'query'}) ) {
53     #eval "use FS::$opt{'query'};";
54     $rows = [ qsearch(
55       $opt{'query'}->{'table'}, 
56       $opt{'query'}->{'hashref'} || {}, 
57       $opt{'query'}->{'select'},
58       $opt{'query'}->{'extra_sql'}. " $limit",
59       '',
60       (exists($opt{'query'}->{'addl_from'}) ? $opt{'query'}->{'addl_from'} : '')
61     ) ];
62   } else {
63     my $sth = dbh->prepare("$opt{'query'} $limit")
64       or die "Error preparing $opt{'query'}: ". dbh->errstr;
65     $sth->execute
66       or die "Error executing $opt{'query'}: ". $sth->errstr;
67
68     #can get # of rows without fetching them all?
69     $rows = $sth->fetchall_arrayref;
70
71     $header ||= $sth->{NAME};
72   }
73
74   if ( $type eq 'csv' ) {
75
76     #http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
77     http_header('Content-Type' => 'text/plain' );
78
79     my $csv = new Text::CSV_XS { 'always_quote' => 1,
80                                  'eol'          => "\n", #"\015\012", #"\012"
81                                };
82
83     $csv->combine(@$header); #or die $csv->status;
84     %><%= $csv->string %><%
85
86     foreach my $row ( @$rows ) {
87
88       if ( $opt{'fields'} ) {
89
90         my @line = ();
91
92         foreach my $field ( @{$opt{'fields'}} ) {
93           if ( ref($field) eq 'CODE' ) {
94             push @line, map {
95                               ref($_) eq 'ARRAY'
96                                 ? '(N/A)' #unimplemented
97                                 : $_;
98                             }
99                             &{$field}($row);
100           } else {
101             push @line, $row->$field();
102           }
103         }
104
105         $csv->combine(@line); #or die $csv->status;
106
107       } else {
108         $csv->combine(@$row); #or die $csv->status;
109       }
110
111       %><%= $csv->string %><%
112
113     }
114
115   #} elsif ( $type eq 'excel' ) {
116   } elsif ( $type =~ /\.xls$/ ) {
117
118     #http_header('Content-Type' => 'application/excel' ); #eww
119     http_header('Content-Type' => 'application/vnd.ms-excel' );
120     #http_header('Content-Type' => 'application/msexcel' ); #alas
121
122     my $data = '';
123     my $XLS = new IO::Scalar \$data;
124     my $workbook = Spreadsheet::WriteExcel->new($XLS)
125       or die "Error opening .xls file: $!";
126
127     my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
128
129     my($r,$c) = (0,0);
130
131     $worksheet->write($r, $c++, $_) foreach @$header;
132
133     foreach my $row ( @$rows ) {
134       $r++;
135       $c = 0;
136
137       if ( $opt{'fields'} ) {
138
139         #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
140         #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
141
142         foreach my $field ( @{$opt{'fields'}} ) {
143           #my $align = $aligns ? shift @$aligns : '';
144           #$align = " ALIGN=$align" if $align;
145           #my $a = '';
146           #if ( $links ) {
147           #  my $link = shift @$links;
148           #  $link = &{$link}($row) if ref($link) eq 'CODE';
149           #  if ( $link ) {
150           #    my( $url, $method ) = @{$link};
151           #    if ( ref($method) eq 'CODE' ) {
152           #      $a = $url. &{$method}($row);
153           #    } else {
154           #      $a = $url. $row->$method();
155           #    }
156           #    $a = qq(<A HREF="$a">);
157           #  }
158           #}
159           if ( ref($field) eq 'CODE' ) {
160             foreach my $value ( &{$field}($row) ) {
161               if ( ref($value) eq 'ARRAY' ) { 
162                 $worksheet->write($r, $c++, '(N/A)' ); #unimplemented
163               } else {
164                 $worksheet->write($r, $c++, $value );
165               }
166             }
167           } else {
168             $worksheet->write($r, $c++, $row->$field() );
169           }
170         }
171
172       } else {
173         $worksheet->write($r, $c++, $_) foreach @$row;
174       }
175
176     }
177
178     $workbook->close();# or die "Error creating .xls file: $!";
179
180     http_header('Content-Length' => length($data) );
181     %><%= $data %><%
182
183   } else { # regular HTML
184
185     if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1 ) {
186       my( $url, $method ) = @{$opt{'redirect'}};
187       redirect( $url. $rows->[0]->$method() );
188     } else {
189       ( my $xlsname = $opt{'name'} ) =~ s/\W//g;
190       $opt{'name'} =~ s/s$// if $total == 1;
191
192       my @menubar = ();
193       if ( $opt{'menubar'} ) {
194         @menubar = @{ $opt{'menubar'} };
195       } else {
196         @menubar = ( 'Main menu' => $p );
197       }
198   %>
199   <%= include( '/elements/header.html', $opt{'title'},
200                  include( '/elements/menubar.html', @menubar )
201              )
202   %>
203   <%= defined($opt{'html_init'}) ? $opt{'html_init'} : '' %>
204   <% my $pager = include ( '/elements/pager.html',
205                              'offset'     => $offset,
206                              'num_rows'   => scalar(@$rows),
207                              'total'      => $total,
208                              'maxrecords' => $maxrecords,
209                          );
210   %>
211   <% unless ( $total ) { %>
212     No matching <%= $opt{'name'} %> found.<BR>
213   <% } else { %>
214   
215     <TABLE>
216       <TR>
217         <TD VALIGN="bottom">
218           <%= $total %> total <%= $opt{'name'} %><BR>
219           <% if ( $opt{'count_addl'} ) { %>
220             <% my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) { %>
221               <%= sprintf( $count, $count_arrayref->[++$n] ) %><BR>
222             <% } %>
223           <% } %>
224         </TD>
225         <TD ALIGN="right">
226           <% $cgi->param('_type', "$xlsname.xls" ); %>
227           Download full results<BR>
228           as <A HREF="<%= $cgi->self_url %>">Excel spreadsheet</A><BR>
229           <% $cgi->param('_type', 'csv'); %>
230           as <A HREF="<%= $cgi->self_url %>">CSV file</A>
231         </TD>
232       </TR>
233       <TR>
234         <TD COLSPAN=2>
235   
236             <%= $pager %>
237             <STYLE TYPE="text/css">
238             .grid table { border: solid; empty-cells: show }
239             .grid TH { padding-left: 3px; padding-right: 3px; border: 1px solid #dddddd; border-bottom: dashed 1px black; border-right: none }
240             .grid TD { padding-left: 3px; padding-right: 3px; empty-cells: show; border: 1px solid #cccccc; border-bottom: none; border-right: none }
241             </STYLE>
242             <TABLE CLASS="grid" CELLSPACING=0 CELLPADDING=0 BORDER=1 BORDERCOLOR="#000000" STYLE="border: solid 1px black; empty-cells: show">
243               <TR>
244               <% foreach my $header ( @$header ) { %>
245                    <TH CLASS="grid" BGCOLOR="#cccccc"><%= $header %></TH>
246               <% } %>
247               </TR>
248               <% my $bgcolor1 = '#eeeeee';
249                  my $bgcolor2 = '#ffffff';
250                  my $bgcolor;
251                  foreach my $row ( @$rows ) {
252                    if ( $bgcolor eq $bgcolor1 ) {
253                      $bgcolor = $bgcolor2;
254                    } else {
255                      $bgcolor = $bgcolor1;
256                    }
257               %>
258                    <TR>
259                    <% if ( $opt{'fields'} ) {
260
261                         my $links  = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
262                         my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
263                         my $colors = $opt{'color'} ? [ @{$opt{'color'}} ] : [];
264                         my $sizes  = $opt{'size'}  ? [ @{$opt{'size'}}  ] : [];
265                         my $styles = $opt{'style'} ? [ @{$opt{'style'}} ] : [];
266
267                         foreach my $field (
268
269                           map {
270                                 if ( ref($_) eq 'ARRAY' ) {
271
272                                   my $tableref = $_;
273
274                                   '<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0'.
275                                   ' STYLE="border:none">'.
276
277                                   join('', map {
278
279                                     my $rowref = $_;
280
281                                     '<tr>'.
282
283                                     join('', map {
284
285                                       my $element = $_;
286
287                                       '<TD STYLE="border:none"'.
288                                       ( $element->{'align'}
289                                           ? ' ALIGN="'. $element->{'align'}. '"'
290                                           : ''
291                                       ). '>'.
292                                       ( $element->{'link'}
293                                           ? '<A HREF="'. $element->{'link'}.'">'
294                                           : ''
295                                       ).
296                                       $element->{'data'}.
297                                       ( $element->{'link'}
298                                           ? '</A>'
299                                           : ''
300                                       ).
301                                       '</td>';
302
303                                     } @$rowref ).
304
305                                     '</tr>';
306                                   } @$tableref ).
307
308                                   '</table>';
309
310                                 } else {
311                                   $_;
312                                 }
313                               }
314
315                           map {
316                                 if ( ref($_) eq 'CODE' ) {
317                                   &{$_}($row);
318                                 } else {
319                                   $row->$_();
320                                 }
321                               }
322                           @{$opt{'fields'}}
323
324                         ) {
325
326                           my $align = $aligns ? shift @$aligns : '';
327                           $align = " ALIGN=$align" if $align;
328
329                           my $a = '';
330                           if ( $links ) {
331                             my $link = shift @$links;
332                             $link = &{$link}($row) if ref($link) eq 'CODE';
333                             if ( $link ) {
334                               my( $url, $method ) = @{$link};
335                               if ( ref($method) eq 'CODE' ) {
336                                 $a = $url. &{$method}($row);
337                               } else {
338                                 $a = $url. $row->$method();
339                               }
340                               $a = qq(<A HREF="$a">);
341                             }
342                           }
343
344                           my $font = '';
345                           my $color = shift @$colors;
346                           $color = &{$color}($row) if ref($color) eq 'CODE';
347                           my $size = shift @$sizes;
348                           $size = &{$size}($row) if ref($size) eq 'CODE';
349                           if ( $color || $size ) {
350                             $font = '<FONT '.
351                                     ( $color ? "COLOR=#$color "   : '' ).
352                                     ( $size  ? qq(SIZE="$size" )  : '' ).
353                                     '>';
354                           }
355
356                           my($s, $es) = ( '', '' );
357                           my $style = shift @$styles;
358                           $style = &{$style}($row) if ref($style) eq 'CODE';
359                           if ( $style ) {
360                             $s = join( '', map "<$_>", split('', $style) );
361                             $es = join( '', map "</$_>", split('', $style) );
362                           }
363
364                        %>
365                        <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"<%= $align %>><%= $font %><%= $a %><%= $s %><%= $field %><%= $es %><%= $a ? '</A>' : '' %><%= $font ? '</FONT>' : '' %></TD>
366                      <% } %>
367                    <% } else { %>
368                      <% foreach ( @$row ) { %>
369                           <TD CLASS="grid" BGCOLOR="$bgcolor"><%= $_ %></TD>
370                      <% } %>
371                    <% } %>
372                    </TR>
373               <% } %>
374
375               <% if ( $opt{'footer'} ) { %>
376                 <TR>
377                 <% foreach my $footer ( @{ $opt{'footer'} } ) { %>
378                      <TD CLASS="grid" BGCOLOR="#dddddd" STYLE="border-top: dashed 1px black;"><i><%= $footer %></i></TH>
379                 <% } %>
380                 </TR>
381               <% } %>
382             
383             </TABLE>
384             <%= $pager %>
385   
386           </TD>
387         </TR>
388       </TABLE>
389   
390   <% } %>
391   </BODY>
392   </HTML>
393   <% } %>
394 <% } %>