37a3718ce2efc714a469a3a242749086345cdb85
[freeside.git] / httemplate / search / elements / search.html
1 <%
2
3   my(%opt) = @_;
4
5   if ( ref($opt{'query'}) ) {
6
7   }
8
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;
12   }
13
14   my $conf = new FS::Conf;
15   my $maxrecords = $conf->config('maxsearchrecordsperpage');
16
17   my $limit = $maxrecords ? "LIMIT $maxrecords" : '';
18
19   my $offset = $cgi->param('offset') || 0;
20   $limit .= " OFFSET $offset" if $offset;
21
22   my $count_sth = dbh->prepare($opt{'count_query'})
23     or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
24   $count_sth->execute
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];
28
29   #warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
30
31   my $header = $opt{'header'};
32   my $rows;
33   if ( ref($opt{'query'}) ) {
34     #eval "use FS::$opt{'query'};";
35     $rows = [ qsearch(
36       $opt{'query'}->{'table'}, 
37       $opt{'query'}->{'hashref'} || {}, 
38       $opt{'query'}->{'select'},
39       $opt{'query'}->{'extra_sql'}. " $limit",
40     ) ];
41   } else {
42     my $sth = dbh->prepare("$opt{'query'} $limit")
43       or die "Error preparing $opt{'query'}: ". dbh->errstr;
44     $sth->execute
45       or die "Error executing $opt{'query'}: ". $sth->errstr;
46
47     #can get # of rows without fetching them all?
48     $rows = $sth->fetchall_arrayref;
49
50     $header ||= $sth->{NAME};
51   }
52
53   if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 ) {
54     my( $url, $method ) = @{$opt{'redirect'}};
55     redirect( $url. $rows->[0]->$method() );
56   } else {
57
58 %>
59 <%= include( '/elements/header.html', $opt{'title'},
60                include( '/elements/menubar.html', 'Main menu' => $p )
61            )
62 %>
63 <% my $pager = include ( '/elements/pager.html',
64                            'offset'     => $offset,
65                            'num_rows'   => scalar(@$rows),
66                            'total'      => $total,
67                            'maxrecords' => $maxrecords,
68                        );
69 %>
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>
74   <% } %>
75 <% } %>
76 <BR><%= $pager %>
77 <%= include( '/elements/table.html' ) %>
78   <TR>
79   <% foreach my $header ( @$header ) { %>
80        <TH><%= $header %></TH>
81   <% } %>
82   </TR>
83   <% foreach my $row ( @$rows ) { %>
84        <TR>
85        <% if ( $opt{'fields'} ) { %>
86          <% my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : ''; %>
87          <% foreach my $field ( @{$opt{'fields'}} ) { %>
88            <% my $a = ''; %>
89            <% if ( $links ) {
90                 my( $url, $method ) = @{shift @$links};
91                 $a = $url. $row->$method();
92                 $a = qq(<A HREF="$a">);
93               }
94            %>
95            <% if ( ref($field) eq 'CODE' ) { %>
96              <TD><%= $a %><%= &{$field}($row) %><%= $a ? '</A>' : '' %></TD>
97            <% } else { %>
98              <TD><%= $a %><%= $row->$field() %><%= $a ? '</A>' : '' %></TD>
99            <% } %>
100          <% } %>
101        <% } else { %>
102          <% foreach ( @$row ) { %>
103               <TD><%= $_ %></TD>
104          <% } %>
105        <% } %>
106        </TR>
107   <% } %>
108
109 </TABLE>
110 <%= $pager %>
111 </BODY>
112 </HTML>
113 <% } %>