break down search.html into components, RT#5108
[freeside.git] / httemplate / search / elements / search-html.html
1 %
2 %   if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1
3 %        && $type ne 'html-print'
4 %      ) {
5 %     my $redirect = $opt{'redirect'};
6 %     $redirect = &{$redirect}($rows->[0], $cgi) if ref($redirect) eq 'CODE';
7 %     my( $url, $method ) = @$redirect;
8 %     redirect( $url. $rows->[0]->$method() );
9 %   } elsif ( exists($opt{'redirect_empty'}) && ! scalar(@$rows) && $total == 0
10 %             && $type ne 'html-print'
11 %             && $opt{'redirect_empty'}
12 %             && ( ref($opt{'redirect_empty'}) ne 'CODE'
13 %                  || &{$opt{'redirect_empty'}}($cgi)    )
14 %      ) {
15 %     my $redirect = $opt{'redirect_empty'};
16 %     $redirect = &{$redirect}($cgi) if ref($redirect) eq 'CODE';
17 %     redirect( $redirect );
18 %   } else {
19 %     if ( $opt{'name_singular'} ) {
20 %       $opt{'name'} = PL($opt{'name_singular'});
21 %     }
22 %     ( my $xlsname = $opt{'name'} ) =~ s/\W//g;
23 %     if ( $total == 1 ) {
24 %       if ( $opt{'name_singular'} ) {
25 %         $opt{'name'} = $opt{'name_singular'}
26 %       } else {
27 %         #$opt{'name'} =~ s/s$// if $total == 1;
28 %         $opt{'name'} =~ s/((s)e)?s$/$2/ if $total == 1;
29 %       }
30 %     }
31 %
32 %     if ( $type eq 'html-print' ) {
33
34         <% include( '/elements/header-popup.html', $opt{'title'} ) %>
35
36 %     } elsif ( $type eq 'select' ) {
37
38         <% include( '/elements/header-popup.html', $opt{'title'} ) %>
39         <% defined($opt{'html_init'}) 
40               ? ( ref($opt{'html_init'})
41                     ? &{$opt{'html_init'}}()
42                     : $opt{'html_init'}
43                 )
44               : ''
45         %>
46
47 %     } else {
48 %
49 %       my @menubar = ();
50 %       if ( $opt{'menubar'} ) {
51 %         @menubar = @{ $opt{'menubar'} };
52 %       #} else {
53 %       #  @menubar = ( 'Main menu' => $p );
54 %       }
55
56         <% include( '/elements/header.html', $opt{'title'},
57                       include( '/elements/menubar.html', @menubar )
58                   )
59         %>
60
61         <% defined($opt{'html_init'}) 
62               ? ( ref($opt{'html_init'})
63                     ? &{$opt{'html_init'}}()
64                     : $opt{'html_init'}
65                 )
66               : ''
67         %>
68
69 %     }
70
71 %     unless ( $total ) { 
72 %       unless ( $opt{'disable_nonefound'} ) { 
73           No matching <% $opt{'name'} %> found.<BR>
74 %       } 
75 %     }
76 %
77 %     if ( $total || $opt{'disableable'} ) { #hmm... and there *are* ones to show??
78
79         <TABLE>
80           <TR>
81
82             <TD VALIGN="bottom">
83
84               <FORM>
85
86 %               if (! $opt{'disable_total'}) {
87                   <% $total %> total <% $opt{'name'} %>
88 %               }
89
90 %               if ( $confmax && $total > $confmax
91 %                    && ! $opt{'disable_maxselect'}
92 %                    && $type ne 'html-print' )
93 %               {
94 %                 $cgi->delete('maxrecords');
95 %                 $cgi->param('_dummy', 1);
96
97                   ( show <SELECT NAME="maxrecords" onChange="window.location = '<% $cgi->self_url %>;maxrecords=' + this.options[this.selectedIndex].value;">
98
99 %                   foreach my $max ( map { $_ * $confmax } qw( 1 5 10 25 ) ) {
100                   <OPTION VALUE="<% $max %>" <% ( $maxrecords == $max ) ? 'SELECTED' : '' %>><% $max %></OPTION>
101 %                   }
102
103                   </SELECT> per page )
104
105 %                 $cgi->param('maxrecords', $maxrecords);
106 %               }
107
108 %               if ( defined($opt{'html_posttotal'}) && $type ne 'html-print' ) {
109                     <% ref($opt{'html_posttotal'})
110                          ? &{$opt{'html_posttotal'}}()
111                          : $opt{'html_posttotal'}
112                     %>
113 %               }
114                 <BR>
115
116 %               if ( $opt{'count_addl'} ) { 
117 %                 my $n=0;
118 %                 foreach my $count ( @{$opt{'count_addl'}} ) { 
119 %                   my $data = $count_arrayref->[++$n];
120 %                   if ( ref($count) ) {
121                       <% &{ $count }( $data ) %>
122 %                   } else {
123                       <% sprintf( $count, $data ) %><BR>
124 %                   }
125 %                 } 
126 %               } 
127               </FORM>
128
129             </TD>
130
131 %           unless ( $opt{'disable_download'} || $type eq 'html-print' ) { 
132
133               <TD ALIGN="right">
134
135                 Download full results<BR>
136
137 %               $cgi->param('_type', "$xlsname.xls" ); 
138                 as <A HREF="<% $cgi->self_url %>">Excel spreadsheet</A><BR>
139
140 %               $cgi->param('_type', 'csv'); 
141                 as <A HREF="<% $cgi->self_url %>">CSV file</A><BR>
142
143 %               $cgi->param('_type', 'html-print'); 
144                 as <A HREF="<% $cgi->self_url %>">printable copy</A>
145
146               <% $opt{'extra_choices_callback'}
147                  ? &{$opt{'extra_choices_callback'}}($cgi->query_string)
148                  : ''
149               %>
150
151               </TD>
152 %             $cgi->param('_type', "html" ); 
153 %           } 
154
155           </TR>
156           <TR>
157             <TD COLSPAN=2>
158
159 %             my $pager = '';
160 %             unless ( $type eq 'html_print' ) {
161
162                 <% $pager = include( '/elements/pager.html',
163                                        'offset'     => $offset,
164                                        'num_rows'   => scalar(@$rows),
165                                        'total'      => $total,
166                                        'maxrecords' => $maxrecords,
167                                    )
168                 %>
169
170                 <% defined($opt{'html_form'}) 
171                      ? ( ref($opt{'html_form'})
172                            ? &{$opt{'html_form'}}()
173                            : $opt{'html_form'}
174                        )
175                      : ''
176                 %>
177
178 %             }
179
180               <% include('/elements/table-grid.html') %>
181
182                 <TR>
183 %                 my $h2 = 0;
184 %                 foreach my $header ( @{ $opt{header} } ) { 
185 %                   my $label = ref($header) ? $header->{label} : $header;
186 %                   my $rowspan = 1;
187 %                   my $style = '';
188 %                   if ( $opt{header2} ) {
189 %                     if ( !length($opt{header2}->[$h2]) ) {
190 %                       $rowspan = 2;
191 %                       splice @{ $opt{header2} }, $h2, 1;
192 %                     } else {
193 %                       $h2++;
194 %                       $style = 'STYLE="border-bottom: none"'
195 %                     }
196 %                   }
197                     <TH CLASS   = "grid"
198                         BGCOLOR = "#cccccc"
199                         ROWSPAN = "<% $rowspan %>"
200                         <% $style %>
201
202                     >
203                       <% $label %>
204                     </TH>
205 %                 } 
206                 </TR>
207
208 %               if ( $opt{header2} ) {
209                   <TR>
210 %                   foreach my $header ( @{ $opt{header2} } ) { 
211 %                     my $label = ref($header) ? $header->{label} : $header;
212                       <TH CLASS="grid" BGCOLOR="#cccccc">
213                         <FONT SIZE="-1"><% $label %></FONT>
214                       </TH>
215 %                   } 
216                   </TR>
217 %               }
218
219 %               my $bgcolor1 = '#eeeeee';
220 %               my $bgcolor2 = '#ffffff';
221 %               my $bgcolor;
222 %
223 %               foreach my $row ( @$rows ) {
224 %
225 %                 if ( $bgcolor eq $bgcolor1 ) {
226 %                   $bgcolor = $bgcolor2;
227 %                 } else {
228 %                   $bgcolor = $bgcolor1;
229 %                 }
230
231                   <TR>
232
233 %                   if ( $opt{'fields'} ) {
234 %
235 %                     my $links    = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
236 %                     my $onclicks = $opt{'link_onclicks'} ? [ @{$opt{'link_onclicks'}} ] : [];
237 %                     my $aligns   = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
238 %                     my $colors   = $opt{'color'} ? [ @{$opt{'color'}} ] : [];
239 %                     my $sizes    = $opt{'size'}  ? [ @{$opt{'size'}}  ] : [];
240 %                     my $styles   = $opt{'style'} ? [ @{$opt{'style'}} ] : [];
241 %                     my $cstyles  = $opt{'cell_style'} ? [ @{$opt{'cell_style'}} ] : [];
242 %
243 %                     foreach my $field (
244 %
245 %                       map {
246 %                             if ( ref($_) eq 'ARRAY' ) {
247 %
248 %                               my $tableref = $_;
249 %
250 %                               '<TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0 WIDTH="100%">'.
251 %
252 %                               join('', map {
253 %
254 %                                 my $rowref = $_;
255 %
256 %                                 '<tr>'.
257 %
258 %                                 join('', map {
259 %
260 %                                   my $e = $_;
261 %
262 %                                   '<TD '.
263 %                                     join(' ', map {
264 %                                       uc($_).'="'. $e->{$_}. '"';
265 %                                     }
266 %                                     grep exists($e->{$_}),
267 %                                          qw( align bgcolor colspan rowspan
268 %                                              style valign width )
269 %                                     ).
270 %                                   '>'.
271 %
272 %                                   ( $e->{'link'}
273 %                                       ? '<A HREF="'. $e->{'link'}. '">'
274 %                                       : ''
275 %                                   ).
276 %                                   ( $e->{'size'}
277 %                                      ? '<FONT SIZE="'.uc($e->{'size'}).'">'
278 %                                      : ''
279 %                                   ).
280 %                                   ( $e->{'data_style'}
281 %                                       ? '<'. uc($e->{'data_style'}). '>'
282 %                                       : ''
283 %                                   ).
284 %                                   $e->{'data'}.
285 %                                   ( $e->{'data_style'}
286 %                                       ? '</'. uc($e->{'data_style'}). '>'
287 %                                       : ''
288 %                                   ).
289 %                                   ( $e->{'size'} ? '</FONT>' : '' ).
290 %                                   ( $e->{'link'} ? '</A>'    : '' ).
291 %                                   '</td>';
292 %
293 %                                 } @$rowref ).
294 %
295 %                                 '</tr>';
296 %                               } @$tableref ).
297 %
298 %                               '</table>';
299 %
300 %                             } else {
301 %                               $_;
302 %                             }
303 %                           }
304 %
305 %                       map {
306 %                             if ( ref($_) eq 'CODE' ) {
307 %                               &{$_}($row);
308 %                             } else {
309 %                               $row->$_();
310 %                             }
311 %                           }
312 %                       @{$opt{'fields'}}
313 %
314 %                     ) {
315 %
316 %                       my $class = ( $field =~ /^<TABLE/i ) ? 'inv' : 'grid';
317 %
318 %                       my $align = $aligns ? shift @$aligns : '';
319 %                       $align = " ALIGN=$align" if $align;
320 %
321 %                       my $a = '';
322 %                       if ( $links ) {
323 %                         my $link = shift @$links;
324 %                         my $onclick = shift @$onclicks;
325 %
326 %                         if (    ! $opt{'agent_virt'}
327 %                              || ( $null_link && ! $row->agentnum )
328 %                              || grep { $row->agentnum == $_ }
329 %                                      @link_agentnums
330 %                            ) {
331 %        
332 %                           $link = &{$link}($row)
333 %                             if ref($link) eq 'CODE';
334 %
335 %                           $onclick = &{$onclick}($row)
336 %                             if ref($onclick) eq 'CODE';
337 %                           $onclick = qq( onClick="$onclick") if $onclick;
338 %
339 %                           if ( $link ) {
340 %                             my( $url, $method ) = @{$link};
341 %                             if ( ref($method) eq 'CODE' ) {
342 %                               $a = $url. &{$method}($row);
343 %                             } else {
344 %                               $a = $url. $row->$method();
345 %                             }
346 %                             $a = qq(<A HREF="$a"$onclick>);
347 %                           }
348 %
349 %                         }
350 %
351 %                       }
352 %
353 %                       my $font = '';
354 %                       my $color = shift @$colors;
355 %                       $color = &{$color}($row) if ref($color) eq 'CODE';
356 %                       my $size = shift @$sizes;
357 %                       $size = &{$size}($row) if ref($size) eq 'CODE';
358 %                       if ( $color || $size ) {
359 %                         $font = '<FONT '.
360 %                                 ( $color ? "COLOR=#$color "   : '' ).
361 %                                 ( $size  ? qq(SIZE="$size" )  : '' ).
362 %                                 '>';
363 %                       }
364 %
365 %                       my($s, $es) = ( '', '' );
366 %                       my $style = shift @$styles;
367 %                       $style = &{$style}($row) if ref($style) eq 'CODE';
368 %                       if ( $style ) {
369 %                         $s = join( '', map "<$_>", split('', $style) );
370 %                         $es = join( '', map "</$_>", split('', $style) );
371 %                       }
372 %
373 %                       my $cstyle = shift @$cstyles;
374 %                       $cstyle = &{$cstyle}($row) if ref($cstyle) eq 'CODE';
375 %                       $cstyle = qq(STYLE="$cstyle")
376 %                         if $cstyle;
377
378                         <TD CLASS="<% $class %>" BGCOLOR="<% $bgcolor %>" <% $align %> <% $cstyle %>><% $font %><% $a %><% $s %><% $field %><% $es %><% $a ? '</A>' : '' %><% $font ? '</FONT>' : '' %></TD>
379
380 %                     } 
381 %
382 %                   } else { 
383 %
384 %                     foreach ( @$row ) { 
385                         <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $_ %></TD>
386 %                     }
387 %
388 %                   }
389
390                   </TR>
391
392 %               } 
393
394 %               if ( $opt{'footer'} ) { 
395
396                   <TR>
397
398 %                   foreach my $footer ( @{ $opt{'footer'} } ) { 
399                       <TD CLASS="grid" BGCOLOR="#dddddd" STYLE="border-top: dashed 1px black;"><i><% $footer %></i></TD>
400 %                   } 
401
402                   </TR>
403 %               } 
404             
405               </TABLE>
406
407               <% $pager %>
408   
409             </TD>
410           </TR>
411         </TABLE>
412 %     }
413
414 %     if ( $type eq 'html-print' ) {
415
416         </BODY></HTML>
417       
418 %     } else {
419
420         <% defined($opt{'html_foot'}) 
421               ? ( ref($opt{'html_foot'})
422                     ? &{$opt{'html_foot'}}()
423                     : $opt{'html_foot'}
424                 )
425               : ''
426         %>
427
428         <% include( '/elements/footer.html' ) %>
429
430 %     }
431
432 %   } 
433 <%init>
434
435 my %args = @_;
436 my $type           = $args{'type'};
437 my $header         = $args{'header'};
438 my $rows           = $args{'rows'};
439 my @link_agentnums = @{ $args{'link_agentnums'} };
440 my $null_link      = $args{'null_link'};
441 my $confmax        = $args{'confmax'};
442 my $maxrecords     = $args{'maxrecords'};
443 my $offset         = $args{'offset'};
444 my %opt            = %{ $args{'opt'} };
445
446 my $count_sth = dbh->prepare($opt{'count_query'})
447   or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
448 $count_sth->execute
449   or die "Error executing $opt{'count_query'}: ". $count_sth->errstr;
450 my $count_arrayref = $count_sth->fetchrow_arrayref;
451 my $total = $count_arrayref->[0];
452
453 </%init>