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)
63 % # 'agent_virt' => 1, # set true if this search should be agent-virtualized
64 % # 'agent_null_right' => 'Access Right', #opt. right to view global records
65 % # 'agent_pos' => 3, #optional position (starting from 0) to insert
67 % # #(query needs to be a qsearch hashref and
68 % # # header & fields need to be defined)
70 % # #listref - each item is a literal column name (or method) or coderef
71 % # #if not specified all columns will be shown
74 % # sub { my $row = shift; $row->column; },
77 % # #listref of column footers
80 % # #listref - each item is the empty string, or a listref of ...
84 % # 'align' => 'lrc.', #one letter for each column, left/right/center/none
85 % # # can also pass a listref with full values:
86 % # # [ 'left', 'right', 'center', '' ]
89 % # #currently only HTML, maybe eventually Excel too
94 % # #redirect if there's only one item...
95 % # # listref of URL base and column name (or method)
96 % # # or a coderef that returns the same
99 % # #set to 1 (or column position for "disabled" status col) to enable
100 % # #"show disabled/hide disabled" links
101 % # #(can't be used with a literal query)
102 % # 'disableable' => 1,
105 % #warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
107 % my $curuser = $FS::CurrentUser::CurrentUser;
116 % $opt{align} = [ map $align{$_}, split(//, $opt{align}) ],
117 % unless !$opt{align} || ref($opt{align});
119 % if ( $opt{'agent_virt'} ) {
121 % my $agentnums_sql = $curuser->agentnums_sql(
122 % 'null_right' => $opt{'agent_null_right'}
125 % $opt{'query'}{'extra_sql'} .=
126 % ( $opt{'query'} =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
128 % $opt{'count_query'} .=
129 % ( $opt{'count_query'} =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
132 % if ( $opt{'agent_pos'} || $opt{'agent_pos'} eq '0'
133 % and scalar($curuser->agentnums) > 1 ) {
134 % #false laziness w/statuspos above
135 % my $pos = $opt{'agent_pos'};
137 % foreach my $att (qw( align style color size )) {
138 % $opt{$att} ||= [ map '', @{ $opt{'fields'} } ];
141 % splice @{ $opt{'header'} }, $pos, 0, 'Agent';
142 % splice @{ $opt{'align'} }, $pos, 0, 'c';
143 % splice @{ $opt{'style'} }, $pos, 0, '';
144 % splice @{ $opt{'size'} }, $pos, 0, '';
145 % splice @{ $opt{'fields'} }, $pos, 0,
146 % sub { $_[0]->agentnum ? $_[0]->agent->agent : '(global)'; };
147 % splice @{ $opt{'color'} }, $pos, 0, '';
148 % splice @{ $opt{'links'} }, $pos, 0, '' #[ 'agent link?', 'agentnum' ]
155 % if ( $opt{'disableable'} ) {
157 % unless ( $cgi->param('showdisabled') ) { #modify searches
159 % $opt{'query'}{'hashref'}{'disabled'} = '';
160 % $opt{'query'}{'extra_sql'} =~ s/^\s*WHERE/ AND/i;
162 % $opt{'count_query'} .=
163 % ( $opt{'count_query'} =~ /WHERE/i ? ' AND ' : ' WHERE ' ).
164 % "( disabled = '' OR disabled IS NULL )";
166 % } elsif ( $opt{'disabled_statuspos'}
167 % || $opt{'disabled_statuspos'} eq '0' ) { #add status column
169 % my $pos = $opt{'disabled_statuspos'};
171 % foreach my $att (qw( align style color size )) {
172 % $opt{$att} ||= [ map '', @{ $opt{'fields'} } ];
175 % splice @{ $opt{'header'} }, $pos, 0, 'Status';
176 % splice @{ $opt{'align'} }, $pos, 0, 'c';
177 % splice @{ $opt{'style'} }, $pos, 0, 'b';
178 % splice @{ $opt{'size'} }, $pos, 0, '';
179 % splice @{ $opt{'fields'} }, $pos, 0,
180 % sub { shift->disabled ? 'DISABLED' : 'Active'; };
181 % splice @{ $opt{'color'} }, $pos, 0,
182 % sub { shift->disabled ? 'FF0000' : '00CC00'; };
183 % splice @{ $opt{'links'} }, $pos, 0, ''
187 % #add show/hide disabled links
188 % my $items = $opt{'name'} || PL($opt{'name_singular'});
189 % if ( $cgi->param('showdisabled') ) {
190 % $cgi->param('showdisabled', 0);
191 % $opt{'html_posttotal'} .=
192 % '( <a href="'. $cgi->self_url. qq!">hide disabled $items</a> )!;
193 % $cgi->param('showdisabled', 1);
195 % $cgi->param('showdisabled', 1);
196 % $opt{'html_posttotal'} .=
197 % '( <a href="'. $cgi->self_url. qq!">show disabled $items</a> )!;
198 % $cgi->param('showdisabled', 0);
205 % my($confmax, $maxrecords, $total, $offset, $count_arrayref);
207 % if ( $cgi->param('_type') =~ /^(csv|\w*\.xls)$/ ) {
211 % } else { #setup some pagination things if we're in html mode
213 % unless (exists($opt{count_query}) && length($opt{count_query})) {
214 % ( $opt{count_query} = $opt{query} ) =~
215 % s/^\s*SELECT\s*(.*?)\s+FROM\s/SELECT COUNT(*) FROM /i; #silly vim:/
218 % if ( $opt{disableable} && ! $cgi->param('showdisabled') ) {
219 % $opt{count_query} .=
220 % ( ( $opt{count_query} =~ /WHERE/i ) ? ' AND ' : ' WHERE ' ).
221 % "( disabled = '' OR disabled IS NULL )";
224 % my $conf = new FS::Conf;
225 % $confmax = $conf->config('maxsearchrecordsperpage');
226 % if ( $cgi->param('maxrecords') =~ /^(\d+)$/ ) {
229 % $maxrecords ||= $confmax;
232 % $limit = $maxrecords ? "LIMIT $maxrecords" : '';
234 % $offset = $cgi->param('offset') || 0;
235 % $limit .= " OFFSET $offset" if $offset;
237 % my $count_sth = dbh->prepare($opt{'count_query'})
238 % or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
239 % $count_sth->execute
240 % or die "Error executing $opt{'count_query'}: ". $count_sth->errstr;
241 % $count_arrayref = $count_sth->fetchrow_arrayref;
242 % $total = $count_arrayref->[0];
248 % my $header = $opt{header};
250 % if ( ref($opt{query}) ) {
252 % if ( $opt{disableable} && ! $cgi->param('showdisabled') ) {
253 % #%search = ( 'disabled' => '' );
254 % $opt{'query'}->{'hashref'}->{'disabled'} = '';
255 % $opt{'query'}->{'extra_sql'} =~ s/^\s*WHERE/ AND/i;
258 % #eval "use FS::$opt{'query'};";
259 % $rows = [ qsearch({
260 % 'select' => $opt{'query'}->{'select'},
261 % 'table' => $opt{'query'}->{'table'},
262 % 'addl_from' => (exists($opt{'query'}->{'addl_from'}) ? $opt{'query'}->{'addl_from'} : ''),
263 % 'hashref' => $opt{'query'}->{'hashref'} || {},
264 % 'extra_sql' => $opt{'query'}->{'extra_sql'},
265 % 'order_by' => $opt{'query'}->{'order_by'}. " $limit",
268 % my $sth = dbh->prepare("$opt{'query'} $limit")
269 % or die "Error preparing $opt{'query'}: ". dbh->errstr;
271 % or die "Error executing $opt{'query'}: ". $sth->errstr;
273 % #can get # of rows without fetching them all?
274 % $rows = $sth->fetchall_arrayref;
276 % $header ||= $sth->{NAME};
279 % if ( $type eq 'csv' ) {
281 % #http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
282 % http_header('Content-Type' => 'text/plain' );
284 % my $csv = new Text::CSV_XS { 'always_quote' => 1,
285 % 'eol' => "\n", #"\015\012", #"\012"
288 % $csv->combine(@$header); #or die $csv->status;
293 % foreach my $row ( @$rows ) {
295 % if ( $opt{'fields'} ) {
299 % foreach my $field ( @{$opt{'fields'}} ) {
300 % if ( ref($field) eq 'CODE' ) {
303 % ? '(N/A)' #unimplemented
308 % push @line, $row->$field();
312 % $csv->combine(@line); #or die $csv->status;
315 % $csv->combine(@$row); #or die $csv->status;
324 % #} elsif ( $type eq 'excel' ) {
325 % } elsif ( $type =~ /\.xls$/ ) {
327 % #http_header('Content-Type' => 'application/excel' ); #eww
328 % http_header('Content-Type' => 'application/vnd.ms-excel' );
329 % #http_header('Content-Type' => 'application/msexcel' ); #alas
332 % my $XLS = new IO::Scalar \$data;
333 % my $workbook = Spreadsheet::WriteExcel->new($XLS)
334 % or die "Error opening .xls file: $!";
336 % my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
340 % $worksheet->write($r, $c++, $_) foreach @$header;
342 % foreach my $row ( @$rows ) {
346 % if ( $opt{'fields'} ) {
348 % #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
349 % #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
351 % foreach my $field ( @{$opt{'fields'}} ) {
352 % #my $align = $aligns ? shift @$aligns : '';
353 % #$align = " ALIGN=$align" if $align;
356 % # my $link = shift @$links;
357 % # $link = &{$link}($row) if ref($link) eq 'CODE';
359 % # my( $url, $method ) = @{$link};
360 % # if ( ref($method) eq 'CODE' ) {
361 % # $a = $url. &{$method}($row);
363 % # $a = $url. $row->$method();
365 % # $a = qq(<A HREF="$a">);
368 % if ( ref($field) eq 'CODE' ) {
369 % foreach my $value ( &{$field}($row) ) {
370 % if ( ref($value) eq 'ARRAY' ) {
371 % $worksheet->write($r, $c++, '(N/A)' ); #unimplemented
373 % $worksheet->write($r, $c++, $value );
377 % $worksheet->write($r, $c++, $row->$field() );
382 % $worksheet->write($r, $c++, $_) foreach @$row;
387 % $workbook->close();# or die "Error creating .xls file: $!";
389 % http_header('Content-Length' => length($data) );
394 % } else { # regular HTML
396 % if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1 ) {
397 % my $redirect = $opt{'redirect'};
398 % $redirect = &{$redirect}($rows->[0]) if ref($redirect) eq 'CODE';
399 % my( $url, $method ) = @$redirect;
400 % redirect( $url. $rows->[0]->$method() );
402 % if ( $opt{'name_singular'} ) {
403 % $opt{'name'} = PL($opt{'name_singular'});
405 % ( my $xlsname = $opt{'name'} ) =~ s/\W//g;
406 % if ( $total == 1 ) {
407 % if ( $opt{'name_singular'} ) {
408 % $opt{'name'} = $opt{'name_singular'}
410 % #$opt{'name'} =~ s/s$// if $total == 1;
411 % $opt{'name'} =~ s/((s)e)?s$/$2/ if $total == 1;
416 % if ( $opt{'menubar'} ) {
417 % @menubar = @{ $opt{'menubar'} };
419 % # @menubar = ( 'Main menu' => $p );
422 <% include( '/elements/header.html', $opt{'title'},
423 include( '/elements/menubar.html', @menubar )
426 <% defined($opt{'html_init'})
427 ? ( ref($opt{'html_init'})
428 ? &{$opt{'html_init'}}()
434 % unless ( $total ) {
435 % unless ( $opt{'disable_nonefound'} ) {
437 No matching <% $opt{'name'} %> found.<BR>
448 <% $total %> total <% $opt{'name'} %>
450 % if ( $confmax && $total > $confmax ) {
451 % $cgi->delete('maxrecords');
452 % $cgi->param('_dummy', 1);
454 %# ( show <SELECT NAME="maxrecords" onChange="this.form.submit();">
455 ( show <SELECT NAME="maxrecords" onChange="window.location = '<% $cgi->self_url %>;maxrecords=' + this.options[this.selectedIndex].value;">
457 % foreach my $max ( map { $_ * $confmax } qw( 1 5 10 25 ) ) {
458 <OPTION VALUE="<% $max %>" <% ( $maxrecords == $max ) ? 'SELECTED' : '' %>><% $max %></OPTION>
463 % $cgi->param('maxrecords', $maxrecords);
466 <% defined($opt{'html_posttotal'})
467 ? ( ref($opt{'html_posttotal'})
468 ? &{$opt{'html_posttotal'}}()
469 : $opt{'html_posttotal'}
475 % if ( $opt{'count_addl'} ) {
476 % my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) {
478 <% sprintf( $count, $count_arrayref->[++$n] ) %><BR>
486 % unless ( $opt{'disable_download'} ) {
489 % $cgi->param('_type', "$xlsname.xls" );
491 Download full results<BR>
492 as <A HREF="<% $cgi->self_url %>">Excel spreadsheet</A><BR>
493 % $cgi->param('_type', 'csv');
495 as <A HREF="<% $cgi->self_url %>">CSV file</A>
497 % $cgi->param('_type', "html" );
504 <% my $pager = include ( '/elements/pager.html',
506 'num_rows' => scalar(@$rows),
508 'maxrecords' => $maxrecords,
511 <% defined($opt{'html_form'})
512 ? ( ref($opt{'html_form'})
513 ? &{$opt{'html_form'}}()
519 <% include('/elements/table-grid.html') %>
523 % foreach my $header ( @$header ) {
525 <TH CLASS="grid" BGCOLOR="#cccccc"><% $header %></TH>
529 % my $bgcolor1 = '#eeeeee';
530 % my $bgcolor2 = '#ffffff';
532 % foreach my $row ( @$rows ) {
533 % if ( $bgcolor eq $bgcolor1 ) {
534 % $bgcolor = $bgcolor2;
536 % $bgcolor = $bgcolor1;
541 % if ( $opt{'fields'} ) {
543 % my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
544 % my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
545 % my $colors = $opt{'color'} ? [ @{$opt{'color'}} ] : [];
546 % my $sizes = $opt{'size'} ? [ @{$opt{'size'}} ] : [];
547 % my $styles = $opt{'style'} ? [ @{$opt{'style'}} ] : [];
549 % foreach my $field (
552 % if ( ref($_) eq 'ARRAY' ) {
556 % '<TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0 WIDTH="100%">'.
570 % uc($_).'="'. $e->{$_}. '"';
572 % grep exists($e->{$_}),
573 % qw( align bgcolor colspan rowspan
574 % style valign width )
579 % ? '<A HREF="'. $e->{'link'}. '">'
583 % ? '<FONT SIZE="'.uc($e->{'size'}).'">'
586 % ( $e->{'data_style'}
587 % ? '<'. uc($e->{'data_style'}). '>'
591 % ( $e->{'data_style'}
592 % ? '</'. uc($e->{'data_style'}). '>'
595 % ( $e->{'size'} ? '</FONT>' : '' ).
596 % ( $e->{'link'} ? '</A>' : '' ).
612 % if ( ref($_) eq 'CODE' ) {
622 % my $class = ( $field =~ /^<TABLE/i ) ? 'inv' : 'grid';
624 % my $align = $aligns ? shift @$aligns : '';
625 % $align = " ALIGN=$align" if $align;
629 % my $link = shift @$links;
630 % $link = &{$link}($row) if ref($link) eq 'CODE';
632 % my( $url, $method ) = @{$link};
633 % if ( ref($method) eq 'CODE' ) {
634 % $a = $url. &{$method}($row);
636 % $a = $url. $row->$method();
638 % $a = qq(<A HREF="$a">);
643 % my $color = shift @$colors;
644 % $color = &{$color}($row) if ref($color) eq 'CODE';
645 % my $size = shift @$sizes;
646 % $size = &{$size}($row) if ref($size) eq 'CODE';
647 % if ( $color || $size ) {
649 % ( $color ? "COLOR=#$color " : '' ).
650 % ( $size ? qq(SIZE="$size" ) : '' ).
654 % my($s, $es) = ( '', '' );
655 % my $style = shift @$styles;
656 % $style = &{$style}($row) if ref($style) eq 'CODE';
658 % $s = join( '', map "<$_>", split('', $style) );
659 % $es = join( '', map "</$_>", split('', $style) );
664 <TD CLASS="<% $class %>" BGCOLOR="<% $bgcolor %>"<% $align %>><% $font %><% $a %><% $s %><% $field %><% $es %><% $a ? '</A>' : '' %><% $font ? '</FONT>' : '' %></TD>
667 % foreach ( @$row ) {
669 <TD CLASS="grid" BGCOLOR="$bgcolor"><% $_ %></TD>
675 % if ( $opt{'footer'} ) {
678 % foreach my $footer ( @{ $opt{'footer'} } ) {
680 <TD CLASS="grid" BGCOLOR="#dddddd" STYLE="border-top: dashed 1px black;"><i><% $footer %></i></TH>
695 <% defined($opt{'html_foot'})
696 ? ( ref($opt{'html_foot'})
697 ? &{$opt{'html_foot'}}()
702 <% include( '/elements/footer.html' ) %>