%
my(%opt) = @_;
if ( ref($opt{'query'}) ) {
}
unless (exists($opt{'count_query'}) && length($opt{'count_query'})) {
( $opt{'count_query'} = $opt{'query'} ) =~
s/^\s*SELECT\s*(.*?)\s+FROM\s/SELECT COUNT(*) FROM /i;
}
my $conf = new FS::Conf;
my $maxrecords = $conf->config('maxsearchrecordsperpage');
my $limit = $maxrecords ? "LIMIT $maxrecords" : '';
my $offset = $cgi->param('offset') || 0;
$limit .= " OFFSET $offset" if $offset;
my $count_sth = dbh->prepare($opt{'count_query'})
or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
$count_sth->execute
or die "Error executing $opt{'count_query'}: ". $count_sth->errstr;
my $count_arrayref = $count_sth->fetchrow_arrayref;
my $total = $count_arrayref->[0];
#warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
my $header = $opt{'header'};
my $rows;
if ( ref($opt{'query'}) ) {
#eval "use FS::$opt{'query'};";
$rows = [ qsearch(
$opt{'query'}->{'table'},
$opt{'query'}->{'hashref'} || {},
$opt{'query'}->{'select'},
$opt{'query'}->{'extra_sql'}. " $limit",
) ];
} else {
my $sth = dbh->prepare("$opt{'query'} $limit")
or die "Error preparing $opt{'query'}: ". dbh->errstr;
$sth->execute
or die "Error executing $opt{'query'}: ". $sth->errstr;
#can get # of rows without fetching them all?
$rows = $sth->fetchall_arrayref;
$header ||= $sth->{NAME};
}
if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 ) {
my( $url, $method ) = @{$opt{'redirect'}};
redirect( $url. $rows->[0]->$method() );
} else {
%>
<%= include( '/elements/header.html', $opt{'title'},
include( '/elements/menubar.html', 'Main menu' => $p )
)
%>
<% my $pager = include ( '/elements/pager.html',
'offset' => $offset,
'num_rows' => scalar(@$rows),
'total' => $total,
'maxrecords' => $maxrecords,
);
%>
<%= $total %> total <%= $opt{'name'} %>
<% if ( $opt{'count_addl'} ) { %>
<% my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) { %>
<%= sprintf( $count, $count_arrayref->[++$n] ) %>
<% } %>
<% } %>
<%= $pager %>
<%= include( '/elements/table.html' ) %>