fbedcaa26a5711d2a3860144b7116ec2bdd975d8
[freeside.git] / httemplate / search / elements / search.html
1 <%
2
3   my %opt = @_;
4   unless (exists($opt{'count_query'}) && length($opt{'count_query'})) {
5     ( $opt{'count_query'} = $opt{'query'} ) =~
6       s/^\s*SELECT\s*(.*)\s+FROM\s/SELECT COUNT(*) FROM /i;
7   }
8
9   my $conf = new FS::Conf;
10   my $maxrecords = $conf->config('maxsearchrecordsperpage');
11
12   my $limit = $maxrecords ? "LIMIT $maxrecords" : '';
13
14   my $offset = $cgi->param('offset') || 0;
15   $limit .= " OFFSET $offset" if $offset;
16
17   my $count_sth = dbh->prepare($opt{'count_query'})
18     or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
19   $count_sth->execute
20     or die "Error executing $opt{'count_query'}: ". $count_sth->errstr;
21   my $total = $count_sth->fetchrow_arrayref->[0];
22
23   my $sth = dbh->prepare("$opt{'query'} $limit")
24     or die "Error preparing $opt{'query'}: ". dbh->errstr;
25   $sth->execute
26     or die "Error executing $opt{'query'}: ". $sth->errstr;
27
28   #can get # of rows without fetching them all?
29   my $rows = $sth->fetchall_arrayref;
30
31 %>
32 <!-- mason kludge -->
33 <% my $pager = include ( '/elements/pager.html',
34                            'offset'     => $offset,
35                            'num_rows'   => scalar(@$rows),
36                            'total'      => $total,
37                            'maxrecords' => $maxrecords,
38                        );
39 %>
40
41 <%= $total %> total <%= $opt{'name'} %><BR><BR><%= $pager %>
42 <%= include( '/elements/table.html' ) %>
43   <TR>
44   <% foreach ( @{$sth->{NAME}} ) { %>
45        <TH><%= $_ %></TH>
46   <% } %>
47   </TR>
48   <% foreach my $row ( @$rows ) { %>
49        <TR>
50        <% foreach ( @$row ) { %>
51             <TD><%= $_ %></TD>
52        <% } %>
53        </TR>
54   <% } %>
55
56 </TABLE>
57 <%= $pager %>
58 </BODY>
59 </HTML>