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',
37 % # 'order_by' => 'ORDER BY something',
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 % # 'disableable' => 1, # set true if this table has a "disabled" field, to
58 % # # hide disabled records & have "show disabled" links
59 % # 'disabled_statuspos' => 3, #optional position (starting from 0) to insert
60 % # #a Status column when showing disabled records
61 % # #(query needs to be a qsearch hashref and
62 % # # header & fields need to be defined)
64 % # 'agent_virt' => 1, # set true if this search should be agent-virtualized
65 % # 'agent_null_right' => 'Access Right', #opt. right to view global records
66 % # 'agent_pos' => 3, #optional position (starting from 0) to insert
68 % # #(query needs to be a qsearch hashref and
69 % # # header & fields need to be defined)
71 % # #listref - each item is a literal column name (or method) or coderef
72 % # #if not specified all columns will be shown
75 % # sub { my $row = shift; $row->column; },
78 % # #listref of column footers
81 % # #listref - each item is the empty string, or a listref of ...
85 % # 'align' => 'lrc.', #one letter for each column, left/right/center/none
86 % # # can also pass a listref with full values:
87 % # # [ 'left', 'right', 'center', '' ]
90 % # #currently only HTML, maybe eventually Excel too
95 % # #redirect if there's only one item...
96 % # # listref of URL base and column name (or method)
97 % # # or a coderef that returns the same
100 % # #set to 1 (or column position for "disabled" status col) to enable
101 % # #"show disabled/hide disabled" links
102 % # #(can't be used with a literal query)
103 % # 'disableable' => 1,
106 % #warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
108 % my $curuser = $FS::CurrentUser::CurrentUser;
117 % $opt{align} = [ map $align{$_}, split(//, $opt{align}) ],
118 % unless !$opt{align} || ref($opt{align});
120 % if ( $opt{'agent_virt'} ) {
122 % my $agentnums_sql = $curuser->agentnums_sql(
123 % 'null_right' => $opt{'agent_null_right'}
126 % $opt{'query'}{'extra_sql'} .=
127 % ( $opt{'query'} =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
129 % $opt{'count_query'} .=
130 % ( $opt{'count_query'} =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
133 % if ( $opt{'agent_pos'} || $opt{'agent_pos'} eq '0'
134 % and scalar($curuser->agentnums) > 1 ) {
135 % #false laziness w/statuspos above
136 % my $pos = $opt{'agent_pos'};
138 % foreach my $att (qw( align style color size )) {
139 % $opt{$att} ||= [ map '', @{ $opt{'fields'} } ];
142 % splice @{ $opt{'header'} }, $pos, 0, 'Agent';
143 % splice @{ $opt{'align'} }, $pos, 0, 'c';
144 % splice @{ $opt{'style'} }, $pos, 0, '';
145 % splice @{ $opt{'size'} }, $pos, 0, '';
146 % splice @{ $opt{'fields'} }, $pos, 0,
147 % sub { $_[0]->agentnum ? $_[0]->agent->agent : '(global)'; };
148 % splice @{ $opt{'color'} }, $pos, 0, '';
149 % splice @{ $opt{'links'} }, $pos, 0, '' #[ 'agent link?', 'agentnum' ]
156 % if ( $opt{'disableable'} ) {
158 % unless ( $cgi->param('showdisabled') ) { #modify searches
160 % $opt{'query'}{'hashref'}{'disabled'} = '';
161 % $opt{'query'}{'extra_sql'} =~ s/^\s*WHERE/ AND/i;
163 % $opt{'count_query'} .=
164 % ( $opt{'count_query'} =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
165 % "( disabled = '' OR disabled IS NULL )";
167 % } elsif ( $opt{'disabled_statuspos'}
168 % || $opt{'disabled_statuspos'} eq '0' ) { #add status column
170 % my $pos = $opt{'disabled_statuspos'};
172 % foreach my $att (qw( align style color size )) {
173 % $opt{$att} ||= [ map '', @{ $opt{'fields'} } ];
176 % splice @{ $opt{'header'} }, $pos, 0, 'Status';
177 % splice @{ $opt{'align'} }, $pos, 0, 'c';
178 % splice @{ $opt{'style'} }, $pos, 0, 'b';
179 % splice @{ $opt{'size'} }, $pos, 0, '';
180 % splice @{ $opt{'fields'} }, $pos, 0,
181 % sub { shift->disabled ? 'DISABLED' : 'Active'; };
182 % splice @{ $opt{'color'} }, $pos, 0,
183 % sub { shift->disabled ? 'FF0000' : '00CC00'; };
184 % splice @{ $opt{'links'} }, $pos, 0, ''
188 % #add show/hide disabled links
189 % my $items = $opt{'name'} || PL($opt{'name_singular'});
190 % if ( $cgi->param('showdisabled') ) {
191 % $cgi->param('showdisabled', 0);
192 % $opt{'html_posttotal'} .=
193 % '( <a href="'. $cgi->self_url. qq!">hide disabled $items</a> )!;
194 % $cgi->param('showdisabled', 1);
196 % $cgi->param('showdisabled', 1);
197 % $opt{'html_posttotal'} .=
198 % '( <a href="'. $cgi->self_url. qq!">show disabled $items</a> )!;
199 % $cgi->param('showdisabled', 0);
206 % my($confmax, $maxrecords, $total, $offset, $count_arrayref);
208 % if ( $cgi->param('_type') =~ /^(csv|\w*\.xls)$/ ) {
212 % } else { #setup some pagination things if we're in html mode
214 % unless (exists($opt{count_query}) && length($opt{count_query})) {
215 % ( $opt{count_query} = $opt{query} ) =~
216 % s/^\s*SELECT\s*(.*?)\s+FROM\s/SELECT COUNT(*) FROM /i; #silly vim:/
219 % if ( $opt{disableable} && ! $cgi->param('showdisabled') ) {
220 % $opt{count_query} .=
221 % ( ( $opt{count_query} =~ /WHERE/i ) ? ' AND ' : ' WHERE ' ).
222 % "( disabled = '' OR disabled IS NULL )";
225 % my $conf = new FS::Conf;
226 % $confmax = $conf->config('maxsearchrecordsperpage');
227 % if ( $cgi->param('maxrecords') =~ /^(\d+)$/ ) {
230 % $maxrecords ||= $confmax;
233 % $limit = $maxrecords ? "LIMIT $maxrecords" : '';
235 % $offset = $cgi->param('offset') || 0;
236 % $limit .= " OFFSET $offset" if $offset;
238 % my $count_sth = dbh->prepare($opt{'count_query'})
239 % or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
240 % $count_sth->execute
241 % or die "Error executing $opt{'count_query'}: ". $count_sth->errstr;
242 % $count_arrayref = $count_sth->fetchrow_arrayref;
243 % $total = $count_arrayref->[0];
249 % my $header = $opt{header};
251 % if ( ref($opt{query}) ) {
253 % if ( $opt{disableable} && ! $cgi->param('showdisabled') ) {
254 % #%search = ( 'disabled' => '' );
255 % $opt{'query'}->{'hashref'}->{'disabled'} = '';
256 % $opt{'query'}->{'extra_sql'} =~ s/^\s*WHERE/ AND/i;
259 % #eval "use FS::$opt{'query'};";
260 % $rows = [ qsearch({
261 % 'select' => $opt{'query'}->{'select'},
262 % 'table' => $opt{'query'}->{'table'},
263 % 'addl_from' => (exists($opt{'query'}->{'addl_from'}) ? $opt{'query'}->{'addl_from'} : ''),
264 % 'hashref' => $opt{'query'}->{'hashref'} || {},
265 % 'extra_sql' => $opt{'query'}->{'extra_sql'},
266 % 'order_by' => $opt{'query'}->{'order_by'}. " $limit",
269 % my $sth = dbh->prepare("$opt{'query'} $limit")
270 % or die "Error preparing $opt{'query'}: ". dbh->errstr;
272 % or die "Error executing $opt{'query'}: ". $sth->errstr;
274 % #can get # of rows without fetching them all?
275 % $rows = $sth->fetchall_arrayref;
277 % $header ||= $sth->{NAME};
280 % if ( $type eq 'csv' ) {
282 % #http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
283 % http_header('Content-Type' => 'text/plain' );
285 % my $csv = new Text::CSV_XS { 'always_quote' => 1,
286 % 'eol' => "\n", #"\015\012", #"\012"
289 % $csv->combine(@$header); #or die $csv->status;
294 % foreach my $row ( @$rows ) {
296 % if ( $opt{'fields'} ) {
300 % foreach my $field ( @{$opt{'fields'}} ) {
301 % if ( ref($field) eq 'CODE' ) {
304 % ? '(N/A)' #unimplemented
309 % push @line, $row->$field();
313 % $csv->combine(@line); #or die $csv->status;
316 % $csv->combine(@$row); #or die $csv->status;
325 % #} elsif ( $type eq 'excel' ) {
326 % } elsif ( $type =~ /\.xls$/ ) {
328 % #http_header('Content-Type' => 'application/excel' ); #eww
329 % http_header('Content-Type' => 'application/vnd.ms-excel' );
330 % #http_header('Content-Type' => 'application/msexcel' ); #alas
333 % my $XLS = new IO::Scalar \$data;
334 % my $workbook = Spreadsheet::WriteExcel->new($XLS)
335 % or die "Error opening .xls file: $!";
337 % my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
341 % $worksheet->write($r, $c++, $_) foreach @$header;
343 % foreach my $row ( @$rows ) {
347 % if ( $opt{'fields'} ) {
349 % #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
350 % #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
352 % foreach my $field ( @{$opt{'fields'}} ) {
353 % #my $align = $aligns ? shift @$aligns : '';
354 % #$align = " ALIGN=$align" if $align;
357 % # my $link = shift @$links;
358 % # $link = &{$link}($row) if ref($link) eq 'CODE';
360 % # my( $url, $method ) = @{$link};
361 % # if ( ref($method) eq 'CODE' ) {
362 % # $a = $url. &{$method}($row);
364 % # $a = $url. $row->$method();
366 % # $a = qq(<A HREF="$a">);
369 % if ( ref($field) eq 'CODE' ) {
370 % foreach my $value ( &{$field}($row) ) {
371 % if ( ref($value) eq 'ARRAY' ) {
372 % $worksheet->write($r, $c++, '(N/A)' ); #unimplemented
374 % $worksheet->write($r, $c++, $value );
378 % $worksheet->write($r, $c++, $row->$field() );
383 % $worksheet->write($r, $c++, $_) foreach @$row;
388 % $workbook->close();# or die "Error creating .xls file: $!";
390 % http_header('Content-Length' => length($data) );
395 % } else { # regular HTML
397 % if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1 ) {
398 % my $redirect = $opt{'redirect'};
399 % $redirect = &{$redirect}($rows->[0]) if ref($redirect) eq 'CODE';
400 % my( $url, $method ) = @$redirect;
401 % redirect( $url. $rows->[0]->$method() );
403 % if ( $opt{'name_singular'} ) {
404 % $opt{'name'} = PL($opt{'name_singular'});
406 % ( my $xlsname = $opt{'name'} ) =~ s/\W//g;
407 % if ( $total == 1 ) {
408 % if ( $opt{'name_singular'} ) {
409 % $opt{'name'} = $opt{'name_singular'}
411 % #$opt{'name'} =~ s/s$// if $total == 1;
412 % $opt{'name'} =~ s/((s)e)?s$/$2/ if $total == 1;
417 % if ( $opt{'menubar'} ) {
418 % @menubar = @{ $opt{'menubar'} };
420 % # @menubar = ( 'Main menu' => $p );
423 <% include( '/elements/header.html', $opt{'title'},
424 include( '/elements/menubar.html', @menubar )
427 <% defined($opt{'html_init'})
428 ? ( ref($opt{'html_init'})
429 ? &{$opt{'html_init'}}()
435 % unless ( $total ) {
436 % unless ( $opt{'disable_nonefound'} ) {
438 No matching <% $opt{'name'} %> found.<BR>
449 <% $total %> total <% $opt{'name'} %>
451 % if ( $confmax && $total > $confmax ) {
452 % $cgi->delete('maxrecords');
453 % $cgi->param('_dummy', 1);
455 %# ( show <SELECT NAME="maxrecords" onChange="this.form.submit();">
456 ( show <SELECT NAME="maxrecords" onChange="window.location = '<% $cgi->self_url %>;maxrecords=' + this.options[this.selectedIndex].value;">
458 % foreach my $max ( map { $_ * $confmax } qw( 1 5 10 25 ) ) {
459 <OPTION VALUE="<% $max %>" <% ( $maxrecords == $max ) ? 'SELECTED' : '' %>><% $max %></OPTION>
464 % $cgi->param('maxrecords', $maxrecords);
467 <% defined($opt{'html_posttotal'})
468 ? ( ref($opt{'html_posttotal'})
469 ? &{$opt{'html_posttotal'}}()
470 : $opt{'html_posttotal'}
476 % if ( $opt{'count_addl'} ) {
477 % my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) {
479 <% sprintf( $count, $count_arrayref->[++$n] ) %><BR>
487 % unless ( $opt{'disable_download'} ) {
490 % $cgi->param('_type', "$xlsname.xls" );
492 Download full results<BR>
493 as <A HREF="<% $cgi->self_url %>">Excel spreadsheet</A><BR>
494 % $cgi->param('_type', 'csv');
496 as <A HREF="<% $cgi->self_url %>">CSV file</A>
498 % $cgi->param('_type', "html" );
505 <% my $pager = include ( '/elements/pager.html',
507 'num_rows' => scalar(@$rows),
509 'maxrecords' => $maxrecords,
512 <% defined($opt{'html_form'})
513 ? ( ref($opt{'html_form'})
514 ? &{$opt{'html_form'}}()
520 <% include('/elements/table-grid.html') %>
524 % foreach my $header ( @$header ) {
526 <TH CLASS="grid" BGCOLOR="#cccccc"><% $header %></TH>
530 % my $bgcolor1 = '#eeeeee';
531 % my $bgcolor2 = '#ffffff';
533 % foreach my $row ( @$rows ) {
534 % if ( $bgcolor eq $bgcolor1 ) {
535 % $bgcolor = $bgcolor2;
537 % $bgcolor = $bgcolor1;
542 % if ( $opt{'fields'} ) {
544 % my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
545 % my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
546 % my $colors = $opt{'color'} ? [ @{$opt{'color'}} ] : [];
547 % my $sizes = $opt{'size'} ? [ @{$opt{'size'}} ] : [];
548 % my $styles = $opt{'style'} ? [ @{$opt{'style'}} ] : [];
550 % foreach my $field (
553 % if ( ref($_) eq 'ARRAY' ) {
557 % '<TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0 WIDTH="100%">'.
571 % uc($_).'="'. $e->{$_}. '"';
573 % grep exists($e->{$_}),
574 % qw( align bgcolor colspan rowspan
575 % style valign width )
580 % ? '<A HREF="'. $e->{'link'}. '">'
584 % ? '<FONT SIZE="'.uc($e->{'size'}).'">'
587 % ( $e->{'data_style'}
588 % ? '<'. uc($e->{'data_style'}). '>'
592 % ( $e->{'data_style'}
593 % ? '</'. uc($e->{'data_style'}). '>'
596 % ( $e->{'size'} ? '</FONT>' : '' ).
597 % ( $e->{'link'} ? '</A>' : '' ).
613 % if ( ref($_) eq 'CODE' ) {
623 % my $class = ( $field =~ /^<TABLE/i ) ? 'inv' : 'grid';
625 % my $align = $aligns ? shift @$aligns : '';
626 % $align = " ALIGN=$align" if $align;
630 % my $link = shift @$links;
631 % $link = &{$link}($row) if ref($link) eq 'CODE';
633 % my( $url, $method ) = @{$link};
634 % if ( ref($method) eq 'CODE' ) {
635 % $a = $url. &{$method}($row);
637 % $a = $url. $row->$method();
639 % $a = qq(<A HREF="$a">);
644 % my $color = shift @$colors;
645 % $color = &{$color}($row) if ref($color) eq 'CODE';
646 % my $size = shift @$sizes;
647 % $size = &{$size}($row) if ref($size) eq 'CODE';
648 % if ( $color || $size ) {
650 % ( $color ? "COLOR=#$color " : '' ).
651 % ( $size ? qq(SIZE="$size" ) : '' ).
655 % my($s, $es) = ( '', '' );
656 % my $style = shift @$styles;
657 % $style = &{$style}($row) if ref($style) eq 'CODE';
659 % $s = join( '', map "<$_>", split('', $style) );
660 % $es = join( '', map "</$_>", split('', $style) );
665 <TD CLASS="<% $class %>" BGCOLOR="<% $bgcolor %>"<% $align %>><% $font %><% $a %><% $s %><% $field %><% $es %><% $a ? '</A>' : '' %><% $font ? '</FONT>' : '' %></TD>
668 % foreach ( @$row ) {
670 <TD CLASS="grid" BGCOLOR="$bgcolor"><% $_ %></TD>
676 % if ( $opt{'footer'} ) {
679 % foreach my $footer ( @{ $opt{'footer'} } ) {
681 <TD CLASS="grid" BGCOLOR="#dddddd" STYLE="border-top: dashed 1px black;"><i><% $footer %></i></TH>
696 <% defined($opt{'html_foot'})
697 ? ( ref($opt{'html_foot'})
698 ? &{$opt{'html_foot'}}()
703 <% include( '/elements/footer.html' ) %>