5 if ( ref($opt{'query'}) ) {
9 unless (exists($opt{'count_query'}) && length($opt{'count_query'})) {
10 ( $opt{'count_query'} = $opt{'query'} ) =~
11 s/^\s*SELECT\s*(.*?)\s+FROM\s/SELECT COUNT(*) FROM /i;
14 my $conf = new FS::Conf;
15 my $maxrecords = $conf->config('maxsearchrecordsperpage');
17 my $limit = $maxrecords ? "LIMIT $maxrecords" : '';
19 my $offset = $cgi->param('offset') || 0;
20 $limit .= " OFFSET $offset" if $offset;
22 my $count_sth = dbh->prepare($opt{'count_query'})
23 or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
25 or die "Error executing $opt{'count_query'}: ". $count_sth->errstr;
26 my $count_arrayref = $count_sth->fetchrow_arrayref;
27 my $total = $count_arrayref->[0];
29 #warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
31 my $header = $opt{'header'};
33 if ( ref($opt{'query'}) ) {
34 #eval "use FS::$opt{'query'};";
36 $opt{'query'}->{'table'},
37 $opt{'query'}->{'hashref'} || {},
38 $opt{'query'}->{'select'},
39 $opt{'query'}->{'extra_sql'}. " $limit",
42 my $sth = dbh->prepare("$opt{'query'} $limit")
43 or die "Error preparing $opt{'query'}: ". dbh->errstr;
45 or die "Error executing $opt{'query'}: ". $sth->errstr;
47 #can get # of rows without fetching them all?
48 $rows = $sth->fetchall_arrayref;
50 $header ||= $sth->{NAME};
53 if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1 ) {
54 my( $url, $method ) = @{$opt{'redirect'}};
55 redirect( $url. $rows->[0]->$method() );
57 $opt{'name'} =~ s/s$// if $total == 1;
59 <%= include( '/elements/header.html', $opt{'title'},
60 include( '/elements/menubar.html', 'Main menu' => $p )
63 <% my $pager = include ( '/elements/pager.html',
65 'num_rows' => scalar(@$rows),
67 'maxrecords' => $maxrecords,
70 <%= $total %> total <%= $opt{'name'} %><BR>
71 <% if ( $opt{'count_addl'} ) { %>
72 <% my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) { %>
73 <%= sprintf( $count, $count_arrayref->[++$n] ) %><BR>
77 <%= include( '/elements/table.html' ) %>
79 <% foreach my $header ( @$header ) { %>
80 <TH><%= $header %></TH>
83 <% foreach my $row ( @$rows ) { %>
85 <% if ( $opt{'fields'} ) { %>
86 <% my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : ''; %>
87 <% foreach my $field ( @{$opt{'fields'}} ) { %>
90 my $link = shift @$links;
91 $link = &{$link}($row) if ref($link) eq 'CODE';
93 my( $url, $method ) = @{$link};
94 if ( ref($method) eq 'CODE' ) {
95 $a = $url. &{$method}($row);
97 $a = $url. $row->$method();
99 $a = qq(<A HREF="$a">);
103 <% if ( ref($field) eq 'CODE' ) { %>
104 <TD><%= $a %><%= &{$field}($row) %><%= $a ? '</A>' : '' %></TD>
106 <TD><%= $a %><%= $row->$field() %><%= $a ? '</A>' : '' %></TD>
110 <% foreach ( @$row ) { %>