summaryrefslogtreecommitdiff
path: root/httemplate/search/elements/search.html
blob: fbedcaa26a5711d2a3860144b7116ec2bdd975d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<%

  my %opt = @_;
  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 $total = $count_sth->fetchrow_arrayref->[0];

  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?
  my $rows = $sth->fetchall_arrayref;

%>
<!-- mason kludge -->
<% my $pager = include ( '/elements/pager.html',
                           'offset'     => $offset,
                           'num_rows'   => scalar(@$rows),
                           'total'      => $total,
                           'maxrecords' => $maxrecords,
                       );
%>

<%= $total %> total <%= $opt{'name'} %><BR><BR><%= $pager %>
<%= include( '/elements/table.html' ) %>
  <TR>
  <% foreach ( @{$sth->{NAME}} ) { %>
       <TH><%= $_ %></TH>
  <% } %>
  </TR>
  <% foreach my $row ( @$rows ) { %>
       <TR>
       <% foreach ( @$row ) { %>
            <TD><%= $_ %></TD>
       <% } %>
       </TR>
  <% } %>

</TABLE>
<%= $pager %>
</BODY>
</HTML>