2 % # (everything not commented required is optional)
4 % # # basic options, required
5 % # 'title' => 'Page title',
7 % # 'name_singular' => 'item', #singular name for the records returned
8 % # #OR# # (preferred, will be pluralized automatically)
9 % # 'name' => 'items', #plural name for the records returned
10 % # # (deprecated, will be singularlized
13 % # # some HTML callbacks...
14 % # 'menubar' => '', #menubar arrayref
15 % # 'html_init' => '', #after the header/menubar and before the pager
16 % # 'html_form' => '', #after the pager, right before the results
17 % # # (only shown if there are results)
18 % # # (use this for any form-opening tag rather than
19 % # # html_init, to avoid a nested form)
20 % # 'html_foot' => '', #at the bottom
21 % # 'html_posttotal' => '', #at the bottom
22 % # # (these three can be strings or coderefs)
25 % # #literal SQL query string or qsearch hashref, required
27 % # 'table' => 'tablename',
28 % # #everything else is optional...
29 % # 'hashref' => { 'field' => 'value',
30 % # 'field' => { 'op' => '<',
35 % # 'addl_from' => '', #'LEFT JOIN othertable USING ( key )',
36 % # 'extra_sql' => '', #'AND otherstuff', #'WHERE onlystuff',
40 % # # "select * from tablename";
42 % # #required unless 'query' is an SQL query string (shouldn't be...)
43 % # 'count_query' => 'SELECT COUNT(*) FROM tablename',
45 % # 'count_addl' => [], #additional count fields listref of sprintf strings
46 % # # [ $money_char.'%.2f total paid', ],
48 % # #listref of column labels, <TH>
49 % # #required unless 'query' is an SQL query string
50 % # # (if not specified the database column names will be used)
51 % # 'header' => [ '#', 'Item' ],
53 % # 'disable_download' => '', # set true to hide the CSV/Excel download links
54 % # 'disable_nonefound' => '', # set true to disable the "No matching Xs found"
57 % # #listref - each item is a literal column name (or method) or coderef
58 % # #if not specified all columns will be shown
61 % # sub { my $row = shift; $row->column; },
64 % # #listref of column footers
67 % # #listref - each item is the empty string, or a listref of ...
71 % # 'align' => 'lrc.', #one letter for each column, left/right/center/none
72 % # # can also pass a listref with full values:
73 % # # [ 'left', 'right', 'center', '' ]
76 % # #currently only HTML, maybe eventually Excel too
81 % # #redirect if there's only one item...
82 % # # listref of URL base and column name (or method)
83 % # # or a coderef that returns the same
86 % # #set to 1 (or column position for "disabled" status col) to enable
87 % # #"show disabled/hide disabled" links
88 % # #(can't be used with a literal query)
89 % # 'disableable' => 1,
94 % #warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
103 % $opt{align} = [ map $align{$_}, split(//, $opt{align}) ],
104 % unless !$opt{align} || ref($opt{align});
108 % my($confmax, $maxrecords, $total, $offset, $count_arrayref);
110 % if ( $cgi->param('_type') =~ /^(csv|\w*\.xls)$/ ) {
114 % } else { #setup some pagination things if we're in html mode
116 % unless (exists($opt{count_query}) && length($opt{count_query})) {
117 % ( $opt{count_query} = $opt{query} ) =~
118 % s/^\s*SELECT\s*(.*?)\s+FROM\s/SELECT COUNT(*) FROM /i; #silly vim:/
121 % if ( $opt{disableable} && ! $cgi->param('showdisabled') ) {
122 % $opt{count_query} .=
123 % ( ( $opt{count_query} =~ /WHERE/i ) ? ' AND ' : ' WHERE ' ).
124 % "( disabled = '' OR disabled IS NULL )";
127 % my $conf = new FS::Conf;
128 % $confmax = $conf->config('maxsearchrecordsperpage');
129 % if ( $cgi->param('maxrecords') =~ /^(\d+)$/ ) {
132 % $maxrecords ||= $confmax;
135 % $limit = $maxrecords ? "LIMIT $maxrecords" : '';
137 % $offset = $cgi->param('offset') || 0;
138 % $limit .= " OFFSET $offset" if $offset;
140 % my $count_sth = dbh->prepare($opt{'count_query'})
141 % or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
142 % $count_sth->execute
143 % or die "Error executing $opt{'count_query'}: ". $count_sth->errstr;
144 % $count_arrayref = $count_sth->fetchrow_arrayref;
145 % $total = $count_arrayref->[0];
149 % #disableable handling
150 % my $posttotal = '';
151 % if ( $opt{disableable} ) {
153 % my $name= $opt{'name_singular'} ? PL($opt{'name_singular'}) : $opt{'name'};
155 % if ( $cgi->param('showdisabled') ) {
156 % $cgi->param('showdisabled', 0);
157 % $posttotal= '( <a href="'. $cgi->self_url. '">'.
158 % "hide disabled $name</a> )";
159 % $cgi->param('showdisabled', 1);
161 % $cgi->param('showdisabled', 1);
162 % $posttotal= '( <a href="'. $cgi->self_url. '">'.
163 % "show disabled $name</a> )";
164 % $cgi->param('showdisabled', 0);
167 % if ( $cgi->param('showdisabled') ) {
169 % my $offset = $opt{disableable};
171 % splice @{ $opt{header} }, $offset, 0, 'Status';
173 % splice @{ $opt{fields} }, $offset, 0,
174 % sub { shift->disabled ? 'DISABLED' : 'Active' };
176 % if ( $opt{links} && scalar( @{ $opt{links} } ) ) {
177 % splice @{ $opt{links} }, $offset, 0, '';
180 % if ( $opt{align} && scalar( @{ $opt{align} } ) ) {
181 % splice @{ $opt{align} }, $offset, 0, 'center';
184 % unless ( $opt{color} && scalar( @{ $opt{color} } ) ) {
185 % #$opt{color} = [ map { '000000'; } @{$opt{header}} ];
186 % $opt{color} = [ map { ''; } @{$opt{header}} ];
188 % splice @{ $opt{color} }, $offset, 0,
189 % sub { shift->disabled ? 'FF0000' : '00CC00'; };
191 % if ( $opt{size} && scalar( @{ $opt{size} } ) ) {
192 % splice @{ $opt{size} }, $offset, 0, '';
195 % unless ( $opt{style} && scalar( @{ $opt{style} } ) ) {
196 % $opt{style} = [ map { ''; } @{$opt{header}} ];
198 % splice @{ $opt{style} }, $offset, 0, 'b';
206 % my $header = $opt{header};
208 % if ( ref($opt{query}) ) {
210 % if ( $opt{disableable} && ! $cgi->param('showdisabled') ) {
211 % #%search = ( 'disabled' => '' );
212 % $opt{'query'}->{'hashref'}->{'disabled'} = '';
213 % $opt{'query'}->{'extra_sql'} =~ s/^\s*WHERE/ AND/i;
216 % #eval "use FS::$opt{'query'};";
218 % $opt{'query'}->{'table'},
219 % $opt{'query'}->{'hashref'} || {},
220 % $opt{'query'}->{'select'},
221 % $opt{'query'}->{'extra_sql'}. " $limit",
223 % (exists($opt{'query'}->{'addl_from'}) ? $opt{'query'}->{'addl_from'} : '')
228 % my $sth = dbh->prepare("$opt{'query'} $limit")
229 % or die "Error preparing $opt{'query'}: ". dbh->errstr;
231 % or die "Error executing $opt{'query'}: ". $sth->errstr;
233 % #can get # of rows without fetching them all?
234 % $rows = $sth->fetchall_arrayref;
236 % $header ||= $sth->{NAME};
240 % warn scalar(@$rows). ' rows returned from '.
241 % ( ref($opt{'query'}) ? 'qsearch query' : 'literal SQL query' )
242 % if $DEBUG || $opt{'debug'};
244 % # display the results - csv, xls or html
246 % if ( $type eq 'csv' ) {
248 % #http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
249 % http_header('Content-Type' => 'text/plain' );
251 % my $csv = new Text::CSV_XS { 'always_quote' => 1,
252 % 'eol' => "\n", #"\015\012", #"\012"
255 % $csv->combine(@$header); #or die $csv->status;
260 % foreach my $row ( @$rows ) {
262 % if ( $opt{'fields'} ) {
266 % foreach my $field ( @{$opt{'fields'}} ) {
267 % if ( ref($field) eq 'CODE' ) {
270 % ? '(N/A)' #unimplemented
275 % push @line, $row->$field();
279 % $csv->combine(@line); #or die $csv->status;
282 % $csv->combine(@$row); #or die $csv->status;
291 % #} elsif ( $type eq 'excel' ) {
292 % } elsif ( $type =~ /\.xls$/ ) {
294 % #http_header('Content-Type' => 'application/excel' ); #eww
295 % http_header('Content-Type' => 'application/vnd.ms-excel' );
296 % #http_header('Content-Type' => 'application/msexcel' ); #alas
299 % my $XLS = new IO::Scalar \$data;
300 % my $workbook = Spreadsheet::WriteExcel->new($XLS)
301 % or die "Error opening .xls file: $!";
303 % my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
307 % $worksheet->write($r, $c++, $_) foreach @$header;
309 % foreach my $row ( @$rows ) {
313 % if ( $opt{'fields'} ) {
315 % #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
316 % #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
318 % foreach my $field ( @{$opt{'fields'}} ) {
319 % #my $align = $aligns ? shift @$aligns : '';
320 % #$align = " ALIGN=$align" if $align;
323 % # my $link = shift @$links;
324 % # $link = &{$link}($row) if ref($link) eq 'CODE';
326 % # my( $url, $method ) = @{$link};
327 % # if ( ref($method) eq 'CODE' ) {
328 % # $a = $url. &{$method}($row);
330 % # $a = $url. $row->$method();
332 % # $a = qq(<A HREF="$a">);
335 % if ( ref($field) eq 'CODE' ) {
336 % foreach my $value ( &{$field}($row) ) {
337 % if ( ref($value) eq 'ARRAY' ) {
338 % $worksheet->write($r, $c++, '(N/A)' ); #unimplemented
340 % $worksheet->write($r, $c++, $value );
344 % $worksheet->write($r, $c++, $row->$field() );
349 % $worksheet->write($r, $c++, $_) foreach @$row;
354 % $workbook->close();# or die "Error creating .xls file: $!";
356 % http_header('Content-Length' => length($data) );
361 % } else { # regular HTML
363 % if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1 ) {
364 % my $redirect = $opt{'redirect'};
365 % $redirect = &{$redirect}($rows->[0]) if ref($redirect) eq 'CODE';
366 % my( $url, $method ) = @$redirect;
367 % redirect( $url. $rows->[0]->$method() );
369 % if ( $opt{'name_singular'} ) {
370 % $opt{'name'} = PL($opt{'name_singular'});
372 % ( my $xlsname = $opt{'name'} ) =~ s/\W//g;
373 % if ( $total == 1 ) {
374 % if ( $opt{'name_singular'} ) {
375 % $opt{'name'} = $opt{'name_singular'}
377 % #$opt{'name'} =~ s/s$// if $total == 1;
378 % $opt{'name'} =~ s/((s)e)?s$/$2/ if $total == 1;
383 % if ( $opt{'menubar'} ) {
384 % @menubar = @{ $opt{'menubar'} };
386 % # @menubar = ( 'Main menu' => $p );
389 <% include( '/elements/header.html', $opt{'title'},
390 include( '/elements/menubar.html', @menubar )
393 <% defined($opt{'html_init'})
394 ? ( ref($opt{'html_init'})
395 ? &{$opt{'html_init'}}()
401 % unless ( $total ) {
402 % unless ( $opt{'disable_nonefound'} ) {
404 No matching <% $opt{'name'} %> found.<BR>
415 <% $total %> total <% $opt{'name'} %>
417 % if ( $confmax && $total > $confmax ) {
418 % $cgi->delete('maxrecords');
419 % $cgi->param('_dummy', 1);
421 %# ( show <SELECT NAME="maxrecords" onChange="this.form.submit();">
422 ( show <SELECT NAME="maxrecords" onChange="window.location = '<% $cgi->self_url %>;maxrecords=' + this.options[this.selectedIndex].value;">
424 % foreach my $max ( map { $_ * $confmax } qw( 1 5 10 25 ) ) {
425 <OPTION VALUE="<% $max %>" <% ( $maxrecords == $max ) ? 'SELECTED' : '' %>><% $max %></OPTION>
430 % $cgi->param('maxrecords', $maxrecords);
435 <% defined($opt{'html_posttotal'})
436 ? ( ref($opt{'html_posttotal'})
437 ? &{$opt{'html_posttotal'}}()
438 : $opt{'html_posttotal'}
444 % if ( $opt{'count_addl'} ) {
445 % my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) {
447 <% sprintf( $count, $count_arrayref->[++$n] ) %><BR>
455 % unless ( $opt{'disable_download'} ) {
458 % $cgi->param('_type', "$xlsname.xls" );
460 Download full results<BR>
461 as <A HREF="<% $cgi->self_url %>">Excel spreadsheet</A><BR>
462 % $cgi->param('_type', 'csv');
464 as <A HREF="<% $cgi->self_url %>">CSV file</A>
466 % $cgi->param('_type', "html" );
473 <% my $pager = include ( '/elements/pager.html',
475 'num_rows' => scalar(@$rows),
477 'maxrecords' => $maxrecords,
480 <% defined($opt{'html_form'})
481 ? ( ref($opt{'html_form'})
482 ? &{$opt{'html_form'}}()
488 <% include('/elements/table-grid.html') %>
492 % foreach my $header ( @$header ) {
494 <TH CLASS="grid" BGCOLOR="#cccccc"><% $header %></TH>
498 % my $bgcolor1 = '#eeeeee';
499 % my $bgcolor2 = '#ffffff';
501 % foreach my $row ( @$rows ) {
502 % if ( $bgcolor eq $bgcolor1 ) {
503 % $bgcolor = $bgcolor2;
505 % $bgcolor = $bgcolor1;
510 % if ( $opt{'fields'} ) {
512 % my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
513 % my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
514 % my $colors = $opt{'color'} ? [ @{$opt{'color'}} ] : [];
515 % my $sizes = $opt{'size'} ? [ @{$opt{'size'}} ] : [];
516 % my $styles = $opt{'style'} ? [ @{$opt{'style'}} ] : [];
518 % foreach my $field (
521 % if ( ref($_) eq 'ARRAY' ) {
525 % '<TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0>'.
538 % ( $element->{'align'}
539 % ? ' ALIGN="'. $element->{'align'}. '"'
542 % ( $element->{'link'}
543 % ? '<A HREF="'. $element->{'link'}.'">'
546 % $element->{'data'}.
547 % ( $element->{'link'}
566 % if ( ref($_) eq 'CODE' ) {
576 % my $class = ( $field =~ /^<TABLE/i ) ? 'inv' : 'grid';
578 % my $align = $aligns ? shift @$aligns : '';
579 % $align = " ALIGN=$align" if $align;
583 % my $link = shift @$links;
584 % $link = &{$link}($row) if ref($link) eq 'CODE';
586 % my( $url, $method ) = @{$link};
587 % if ( ref($method) eq 'CODE' ) {
588 % $a = $url. &{$method}($row);
590 % $a = $url. $row->$method();
592 % $a = qq(<A HREF="$a">);
597 % my $color = shift @$colors;
598 % $color = &{$color}($row) if ref($color) eq 'CODE';
599 % my $size = shift @$sizes;
600 % $size = &{$size}($row) if ref($size) eq 'CODE';
601 % if ( $color || $size ) {
603 % ( $color ? "COLOR=#$color " : '' ).
604 % ( $size ? qq(SIZE="$size" ) : '' ).
608 % my($s, $es) = ( '', '' );
609 % my $style = shift @$styles;
610 % $style = &{$style}($row) if ref($style) eq 'CODE';
612 % $s = join( '', map "<$_>", split('', $style) );
613 % $es = join( '', map "</$_>", split('', $style) );
618 <TD CLASS="<% $class %>" BGCOLOR="<% $bgcolor %>"<% $align %>><% $font %><% $a %><% $s %><% $field %><% $es %><% $a ? '</A>' : '' %><% $font ? '</FONT>' : '' %></TD>
621 % foreach ( @$row ) {
623 <TD CLASS="grid" BGCOLOR="<% $bgcolor %>"><% $_ %></TD>
629 % if ( $opt{'footer'} ) {
632 % foreach my $footer ( @{ $opt{'footer'} } ) {
634 <TD CLASS="grid" BGCOLOR="#dddddd" STYLE="border-top: dashed 1px black;"><i><% $footer %></i></TH>
649 <% defined($opt{'html_foot'})
650 ? ( ref($opt{'html_foot'})
651 ? &{$opt{'html_foot'}}()
656 <% include( '/elements/footer.html' ) %>