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