add ability to search on a date range of invoice events and then reprint or reemail...
[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, &{$field}($row);
95           } else {
96             push @line, $row->$field();
97           }
98         }
99
100         $csv->combine(@line); #or die $csv->status;
101
102       } else {
103         $csv->combine(@$row); #or die $csv->status;
104       }
105
106       %><%= $csv->string %><%
107
108     }
109
110   #} elsif ( $type eq 'excel' ) {
111   } elsif ( $type =~ /\.xls$/ ) {
112
113     #http_header('Content-Type' => 'application/excel' ); #eww
114     http_header('Content-Type' => 'application/vnd.ms-excel' );
115     #http_header('Content-Type' => 'application/msexcel' ); #alas
116
117     my $data = '';
118     my $XLS = new IO::Scalar \$data;
119     my $workbook = Spreadsheet::WriteExcel->new($XLS)
120       or die "Error opening .xls file: $!";
121
122     my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
123
124     my($r,$c) = (0,0);
125
126     $worksheet->write($r, $c++, $_) foreach @$header;
127
128     foreach my $row ( @$rows ) {
129       $r++;
130       $c = 0;
131
132       if ( $opt{'fields'} ) {
133
134         #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
135         #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
136
137         foreach my $field ( @{$opt{'fields'}} ) {
138           #my $align = $aligns ? shift @$aligns : '';
139           #$align = " ALIGN=$align" if $align;
140           #my $a = '';
141           #if ( $links ) {
142           #  my $link = shift @$links;
143           #  $link = &{$link}($row) if ref($link) eq 'CODE';
144           #  if ( $link ) {
145           #    my( $url, $method ) = @{$link};
146           #    if ( ref($method) eq 'CODE' ) {
147           #      $a = $url. &{$method}($row);
148           #    } else {
149           #      $a = $url. $row->$method();
150           #    }
151           #    $a = qq(<A HREF="$a">);
152           #  }
153           #}
154           if ( ref($field) eq 'CODE' ) {
155             $worksheet->write($r, $c++, &{$field}($row) );
156           } else {
157             $worksheet->write($r, $c++, $row->$field() );
158           }
159         }
160
161       } else {
162         $worksheet->write($r, $c++, $_) foreach @$row;
163       }
164
165     }
166
167     $workbook->close();# or die "Error creating .xls file: $!";
168
169     http_header('Content-Length' => length($data) );
170     %><%= $data %><%
171
172   } else { # regular HTML
173
174     if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1 ) {
175       my( $url, $method ) = @{$opt{'redirect'}};
176       redirect( $url. $rows->[0]->$method() );
177     } else {
178       ( my $xlsname = $opt{'name'} ) =~ s/\W//g;
179       $opt{'name'} =~ s/s$// if $total == 1;
180
181       my @menubar = ();
182       if ( $opt{'menubar'} ) {
183         @menubar = @{ $opt{'menubar'} };
184       } else {
185         @menubar = ( 'Main menu' => $p );
186       }
187   %>
188   <%= include( '/elements/header.html', $opt{'title'},
189                  include( '/elements/menubar.html', @menubar )
190              )
191   %>
192   <%= defined($opt{'html_init'}) ? $opt{'html_init'} : '' %>
193   <% my $pager = include ( '/elements/pager.html',
194                              'offset'     => $offset,
195                              'num_rows'   => scalar(@$rows),
196                              'total'      => $total,
197                              'maxrecords' => $maxrecords,
198                          );
199   %>
200   <% unless ( $total ) { %>
201     No matching <%= $opt{'name'} %> found.<BR>
202   <% } else { %>
203   
204     <TABLE>
205       <TR>
206         <TD VALIGN="bottom">
207           <%= $total %> total <%= $opt{'name'} %><BR>
208           <% if ( $opt{'count_addl'} ) { %>
209             <% my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) { %>
210               <%= sprintf( $count, $count_arrayref->[++$n] ) %><BR>
211             <% } %>
212           <% } %>
213         </TD>
214         <TD ALIGN="right">
215           <% $cgi->param('_type', "$xlsname.xls" ); %>
216           Download full results<BR>
217           as <A HREF="<%= $cgi->self_url %>">Excel spreadsheet</A><BR>
218           <% $cgi->param('_type', 'csv'); %>
219           as <A HREF="<%= $cgi->self_url %>">CSV file</A>
220         </TD>
221       </TR>
222       <TR>
223         <TD COLSPAN=2>
224   
225             <%= $pager %>
226             <STYLE TYPE="text/css">
227             .grid table { border: solid; empty-cells: show }
228             .grid TH { padding-left: 3px; padding-right: 3px; border: 1px solid #dddddd; border-bottom: dashed 1px black; border-right: none }
229             .grid TD { padding-left: 3px; padding-right: 3px; empty-cells: show; border: 1px solid #cccccc; border-bottom: none; border-right: none }
230             </STYLE>
231             <TABLE CLASS="grid" CELLSPACING=0 CELLPADDING=0 BORDER=1 BORDERCOLOR="#000000" STYLE="border: solid 1px black; empty-cells: show">
232               <TR>
233               <% foreach my $header ( @$header ) { %>
234                    <TH CLASS="grid" BGCOLOR="#cccccc"><%= $header %></TH>
235               <% } %>
236               </TR>
237               <% my $bgcolor1 = '#eeeeee';
238                  my $bgcolor2 = '#ffffff';
239                  my $bgcolor;
240                  foreach my $row ( @$rows ) {
241                    if ( $bgcolor eq $bgcolor1 ) {
242                      $bgcolor = $bgcolor2;
243                    } else {
244                      $bgcolor = $bgcolor1;
245                    }
246               %>
247                    <TR>
248                    <% if ( $opt{'fields'} ) {
249
250                         my $links  = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
251                         my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
252                         my $colors = $opt{'color'} ? [ @{$opt{'color'}} ] : [];
253                         my $sizes  = $opt{'size'}  ? [ @{$opt{'size'}}  ] : [];
254                         my $styles = $opt{'style'} ? [ @{$opt{'style'}} ] : [];
255
256                         foreach my $field ( @{$opt{'fields'}} ) {
257
258                           my $align = $aligns ? shift @$aligns : '';
259                           $align = " ALIGN=$align" if $align;
260
261                           my $a = '';
262                           if ( $links ) {
263                             my $link = shift @$links;
264                             $link = &{$link}($row) if ref($link) eq 'CODE';
265                             if ( $link ) {
266                               my( $url, $method ) = @{$link};
267                               if ( ref($method) eq 'CODE' ) {
268                                 $a = $url. &{$method}($row);
269                               } else {
270                                 $a = $url. $row->$method();
271                               }
272                               $a = qq(<A HREF="$a">);
273                             }
274                           }
275
276                           my $font = '';
277                           my $color = shift @$colors;
278                           $color = &{$color}($row) if ref($color) eq 'CODE';
279                           my $size = shift @$sizes;
280                           $size = &{$size}($row) if ref($size) eq 'CODE';
281                           if ( $color || $size ) {
282                             $font = '<FONT '.
283                                     ( $color ? "COLOR=#$color "   : '' ).
284                                     ( $size  ? qq(SIZE="$size" )  : '' ).
285                                     '>';
286                           }
287
288                           my($s, $es) = ( '', '' );
289                           my $style = shift @$styles;
290                           $style = &{$style}($row) if ref($style) eq 'CODE';
291                           if ( $style ) {
292                             $s = join( '', map "<$_>", split('', $style) );
293                             $es = join( '', map "</$_>", split('', $style) );
294                           }
295
296                        %>
297                        <% if ( ref($field) eq 'CODE' ) { %>
298                          <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"<%= $align %>><%= $font %><%= $a %><%= $s %><%= &{$field}($row) %><%= $es %><%= $a ? '</A>' : '' %><%= $font ? '</FONT>' : '' %></TD>
299                        <% } else { %>
300                          <TD CLASS="grid" BGCOLOR="<%= $bgcolor %>"<%= $align %>><%= $font %><%= $a %><%= $s %><%= $row->$field() %><%= $es %><%= $a ? '</A>' : '' %><%= $font ? '</FONT>' : '' %></TD>
301                        <% } %>
302                      <% } %>
303                    <% } else { %>
304                      <% foreach ( @$row ) { %>
305                           <TD CLASS="grid" BGCOLOR="$bgcolor"><%= $_ %></TD>
306                      <% } %>
307                    <% } %>
308                    </TR>
309               <% } %>
310
311               <% if ( $opt{'footer'} ) { %>
312                 <TR>
313                 <% foreach my $footer ( @{ $opt{'footer'} } ) { %>
314                      <TD CLASS="grid" BGCOLOR="#dddddd" STYLE="border-top: dashed 1px black;"><i><%= $footer %></i></TH>
315                 <% } %>
316                 </TR>
317               <% } %>
318             
319             </TABLE>
320             <%= $pager %>
321   
322           </TD>
323         </TR>
324       </TABLE>
325   
326   <% } %>
327   </BODY>
328   </HTML>
329   <% } %>
330 <% } %>