This commit was generated by cvs2svn to compensate for changes in r4407,
[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 $redirect = $opt{'redirect'};
187       $redirect = &{$redirect}($rows->[0]) if ref($redirect) eq 'CODE';
188       my( $url, $method ) = @$redirect;
189       redirect( $url. $rows->[0]->$method() );
190     } else {
191       ( my $xlsname = $opt{'name'} ) =~ s/\W//g;
192       $opt{'name'} =~ s/s$// if $total == 1;
193
194       my @menubar = ();
195       if ( $opt{'menubar'} ) {
196         @menubar = @{ $opt{'menubar'} };
197       } else {
198         @menubar = ( 'Main menu' => $p );
199       }
200   %>
201   <%= include( '/elements/header.html', $opt{'title'},
202                  include( '/elements/menubar.html', @menubar )
203              )
204   %>
205   <%= defined($opt{'html_init'}) ? $opt{'html_init'} : '' %>
206   <% my $pager = include ( '/elements/pager.html',
207                              'offset'     => $offset,
208                              'num_rows'   => scalar(@$rows),
209                              'total'      => $total,
210                              'maxrecords' => $maxrecords,
211                          );
212   %>
213   <% unless ( $total ) { %>
214     No matching <%= $opt{'name'} %> found.<BR>
215   <% } else { %>
216   
217     <TABLE>
218       <TR>
219         <TD VALIGN="bottom">
220           <%= $total %> total <%= $opt{'name'} %><BR>
221           <% if ( $opt{'count_addl'} ) { %>
222             <% my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) { %>
223               <%= sprintf( $count, $count_arrayref->[++$n] ) %><BR>
224             <% } %>
225           <% } %>
226         </TD>
227         <TD ALIGN="right">
228           <% $cgi->param('_type', "$xlsname.xls" ); %>
229           Download full results<BR>
230           as <A HREF="<%= $cgi->self_url %>">Excel spreadsheet</A><BR>
231           <% $cgi->param('_type', 'csv'); %>
232           as <A HREF="<%= $cgi->self_url %>">CSV file</A>
233         </TD>
234       </TR>
235       <TR>
236         <TD COLSPAN=2>
237   
238             <%= $pager %>
239             <STYLE TYPE="text/css">
240             .grid table { border: solid; empty-cells: show }
241             .grid TH { padding-left: 3px; padding-right: 3px; border: 1px solid #dddddd; border-bottom: dashed 1px black; border-right: none }
242             .grid TD { padding-left: 3px; padding-right: 3px; empty-cells: show; border: 1px solid #cccccc; border-bottom: none; border-right: none }
243             </STYLE>
244             <TABLE CLASS="grid" CELLSPACING=0 CELLPADDING=0 BORDER=1 BORDERCOLOR="#000000" STYLE="border: solid 1px black; empty-cells: show">
245               <TR>
246               <% foreach my $header ( @$header ) { %>
247                    <TH CLASS="grid" BGCOLOR="#cccccc"><%= $header %></TH>
248               <% } %>
249               </TR>
250               <% my $bgcolor1 = '#eeeeee';
251                  my $bgcolor2 = '#ffffff';
252                  my $bgcolor;
253                  foreach my $row ( @$rows ) {
254                    if ( $bgcolor eq $bgcolor1 ) {
255                      $bgcolor = $bgcolor2;
256                    } else {
257                      $bgcolor = $bgcolor1;
258                    }
259               %>
260                    <TR>
261                    <% if ( $opt{'fields'} ) {
262
263                         my $links  = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
264                         my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
265                         my $colors = $opt{'color'} ? [ @{$opt{'color'}} ] : [];
266                         my $sizes  = $opt{'size'}  ? [ @{$opt{'size'}}  ] : [];
267                         my $styles = $opt{'style'} ? [ @{$opt{'style'}} ] : [];
268
269                         foreach my $field (
270
271                           map {
272                                 if ( ref($_) eq 'ARRAY' ) {
273
274                                   my $tableref = $_;
275
276                                   '<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0'.
277                                   ' STYLE="border:none">'.
278
279                                   join('', map {
280
281                                     my $rowref = $_;
282
283                                     '<tr>'.
284
285                                     join('', map {
286
287                                       my $element = $_;
288
289                                       '<TD STYLE="border:none"'.
290                                       ( $element->{'align'}
291                                           ? ' ALIGN="'. $element->{'align'}. '"'
292                                           : ''
293                                       ). '>'.
294                                       ( $element->{'link'}
295                                           ? '<A HREF="'. $element->{'link'}.'">'
296                                           : ''
297                                       ).
298                                       $element->{'data'}.
299                                       ( $element->{'link'}
300                                           ? '</A>'
301                                           : ''
302                                       ).
303                                       '</td>';
304
305                                     } @$rowref ).
306
307                                     '</tr>';
308                                   } @$tableref ).
309
310                                   '</table>';
311
312                                 } else {
313                                   $_;
314                                 }
315                               }
316
317                           map {
318                                 if ( ref($_) eq 'CODE' ) {
319                                   &{$_}($row);
320                                 } else {
321                                   $row->$_();
322                                 }
323                               }
324                           @{$opt{'fields'}}
325
326                         ) {
327
328                           my $align = $aligns ? shift @$aligns : '';
329                           $align = " ALIGN=$align" if $align;
330
331                           my $a = '';
332                           if ( $links ) {
333                             my $link = shift @$links;
334                             $link = &{$link}($row) if ref($link) eq 'CODE';
335                             if ( $link ) {
336                               my( $url, $method ) = @{$link};
337                               if ( ref($method) eq 'CODE' ) {
338                                 $a = $url. &{$method}($row);
339                               } else {
340                                 $a = $url. $row->$method();
341                               }
342                               $a = qq(<A HREF="$a">);
343                             }
344                           }
345
346                           my $font = '';
347                           my $color = shift @$colors;
348                           $color = &{$color}($row) if ref($color) eq 'CODE';
349                           my $size = shift @$sizes;
350                           $size = &{$size}($row) if ref($size) eq 'CODE';
351                           if ( $color || $size ) {
352                             $font = '<FONT '.
353                                     ( $color ? "COLOR=#$color "   : '' ).
354                                     ( $size  ? qq(SIZE="$size" )  : '' ).
355                                     '>';
356                           }
357
358                           my($s, $es) = ( '', '' );
359                           my $style = shift @$styles;
360                           $style = &{$style}($row) if ref($style) eq 'CODE';
361                           if ( $style ) {
362                             $s = join( '', map "<$_>", split('', $style) );
363                             $es = join( '', map "</$_>", split('', $style) );
364                           }
365
366                        %>
367                        <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"<%= $align %>><%= $font %><%= $a %><%= $s %><%= $field %><%= $es %><%= $a ? '</A>' : '' %><%= $font ? '</FONT>' : '' %></TD>
368                      <% } %>
369                    <% } else { %>
370                      <% foreach ( @$row ) { %>
371                           <TD CLASS="grid" BGCOLOR="$bgcolor"><%= $_ %></TD>
372                      <% } %>
373                    <% } %>
374                    </TR>
375               <% } %>
376
377               <% if ( $opt{'footer'} ) { %>
378                 <TR>
379                 <% foreach my $footer ( @{ $opt{'footer'} } ) { %>
380                      <TD CLASS="grid" BGCOLOR="#dddddd" STYLE="border-top: dashed 1px black;"><i><%= $footer %></i></TH>
381                 <% } %>
382                 </TR>
383               <% } %>
384             
385             </TABLE>
386             <%= $pager %>
387   
388           </TD>
389         </TR>
390       </TABLE>
391   
392   <% } %>
393   </BODY>
394   </HTML>
395   <% } %>
396 <% } %>