12ab83ba52e20429910fd604893048c10b0f2de4
[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 && $total == 1 ) {
54     my( $url, $method ) = @{$opt{'redirect'}};
55     redirect( $url. $rows->[0]->$method() );
56   } else {
57     $opt{'name'} =~ s/s$// if $total == 1;
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 <% unless ( $total ) { %>
71   No matching <%= $opt{'name'} %> found.<BR>
72 <% } else { %>
73   <%= $total %> total <%= $opt{'name'} %><BR>
74   <% if ( $opt{'count_addl'} ) { %>
75     <% my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) { %>
76       <%= sprintf( $count, $count_arrayref->[++$n] ) %><BR>
77     <% } %>
78   <% } %>
79   <BR><%= $pager %>
80   <%= include( '/elements/table.html' ) %>
81     <TR>
82     <% foreach my $header ( @$header ) { %>
83          <TH><%= $header %></TH>
84     <% } %>
85     </TR>
86     <% foreach my $row ( @$rows ) { %>
87          <TR>
88          <% if ( $opt{'fields'} ) { %>
89            <% my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : ''; %>
90            <% foreach my $field ( @{$opt{'fields'}} ) { %>
91              <% my $a = ''; %>
92              <% if ( $links ) {
93                   my $link = shift @$links;
94                   $link = &{$link}($row) if ref($link) eq 'CODE';
95                   if ( $link ) {
96                     my( $url, $method ) = @{$link};
97                     if ( ref($method) eq 'CODE' ) {
98                       $a = $url. &{$method}($row);
99                     } else {
100                       $a = $url. $row->$method();
101                     }
102                     $a = qq(<A HREF="$a">);
103                   }
104                 }
105              %>
106              <% if ( ref($field) eq 'CODE' ) { %>
107                <TD><%= $a %><%= &{$field}($row) %><%= $a ? '</A>' : '' %></TD>
108              <% } else { %>
109                <TD><%= $a %><%= $row->$field() %><%= $a ? '</A>' : '' %></TD>
110              <% } %>
111            <% } %>
112          <% } else { %>
113            <% foreach ( @$row ) { %>
114                 <TD><%= $_ %></TD>
115            <% } %>
116          <% } %>
117          </TR>
118     <% } %>
119   
120   </TABLE>
121   <%= $pager %>
122 <% } %>
123 </BODY>
124 </HTML>
125 <% } %>
126