add agent selection to payment and credit reports, add link to agent browse, closes...
[freeside.git] / httemplate / search / elements / search.html
1 <%
2
3   my(%opt) = @_;
4
5   my %align = (
6     'l' => 'left',
7     'r' => 'right',
8     'c' => 'center',
9     ' ' => '',
10     '.' => '',
11   );
12   $opt{align} = [ map $align{$_}, split(//, $opt{align}) ],
13     unless !$opt{align} || ref($opt{align});
14
15   #if ( ref($opt{'query'}) ) {
16   #
17   #}
18
19   unless (exists($opt{'count_query'}) && length($opt{'count_query'})) {
20     ( $opt{'count_query'} = $opt{'query'} ) =~
21       s/^\s*SELECT\s*(.*?)\s+FROM\s/SELECT COUNT(*) FROM /i;
22   }
23
24   my $conf = new FS::Conf;
25   my $maxrecords = $conf->config('maxsearchrecordsperpage');
26
27   my $limit = $maxrecords ? "LIMIT $maxrecords" : '';
28
29   my $offset = $cgi->param('offset') || 0;
30   $limit .= " OFFSET $offset" if $offset;
31
32   my $count_sth = dbh->prepare($opt{'count_query'})
33     or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
34   $count_sth->execute
35     or die "Error executing $opt{'count_query'}: ". $count_sth->errstr;
36   my $count_arrayref = $count_sth->fetchrow_arrayref;
37   my $total = $count_arrayref->[0];
38
39   #warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
40
41   my $header = $opt{'header'};
42   my $rows;
43   if ( ref($opt{'query'}) ) {
44     #eval "use FS::$opt{'query'};";
45     $rows = [ qsearch(
46       $opt{'query'}->{'table'}, 
47       $opt{'query'}->{'hashref'} || {}, 
48       $opt{'query'}->{'select'},
49       $opt{'query'}->{'extra_sql'}. " $limit",
50       '',
51       (exists($opt{'query'}->{'addl_from'}) ? $opt{'query'}->{'addl_from'} : '')
52     ) ];
53   } else {
54     my $sth = dbh->prepare("$opt{'query'} $limit")
55       or die "Error preparing $opt{'query'}: ". dbh->errstr;
56     $sth->execute
57       or die "Error executing $opt{'query'}: ". $sth->errstr;
58
59     #can get # of rows without fetching them all?
60     $rows = $sth->fetchall_arrayref;
61
62     $header ||= $sth->{NAME};
63   }
64
65   if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1 ) {
66     my( $url, $method ) = @{$opt{'redirect'}};
67     redirect( $url. $rows->[0]->$method() );
68   } else {
69     $opt{'name'} =~ s/s$// if $total == 1;
70 %>
71 <%= include( '/elements/header.html', $opt{'title'},
72                include( '/elements/menubar.html', 'Main menu' => $p )
73            )
74 %>
75 <% my $pager = include ( '/elements/pager.html',
76                            'offset'     => $offset,
77                            'num_rows'   => scalar(@$rows),
78                            'total'      => $total,
79                            'maxrecords' => $maxrecords,
80                        );
81 %>
82 <% unless ( $total ) { %>
83   No matching <%= $opt{'name'} %> found.<BR>
84 <% } else { %>
85   <%= $total %> total <%= $opt{'name'} %><BR>
86   <% if ( $opt{'count_addl'} ) { %>
87     <% my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) { %>
88       <%= sprintf( $count, $count_arrayref->[++$n] ) %><BR>
89     <% } %>
90   <% } %>
91   <BR><%= $pager %>
92   <%= include( '/elements/table.html' ) %>
93     <TR>
94     <% foreach my $header ( @$header ) { %>
95          <TH><%= $header %></TH>
96     <% } %>
97     </TR>
98     <% foreach my $row ( @$rows ) { %>
99          <TR>
100          <% if ( $opt{'fields'} ) {
101               my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
102               my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
103               foreach my $field ( @{$opt{'fields'}} ) {
104                 my $align = $aligns ? shift @$aligns : '';
105                 $align = " ALIGN=$align" if $align;
106                 my $a = '';
107                 if ( $links ) {
108                   my $link = shift @$links;
109                   $link = &{$link}($row) if ref($link) eq 'CODE';
110                   if ( $link ) {
111                     my( $url, $method ) = @{$link};
112                     if ( ref($method) eq 'CODE' ) {
113                       $a = $url. &{$method}($row);
114                     } else {
115                       $a = $url. $row->$method();
116                     }
117                     $a = qq(<A HREF="$a">);
118                   }
119                 }
120              %>
121              <% if ( ref($field) eq 'CODE' ) { %>
122                <TD<%= $align %>><%= $a %><%= &{$field}($row) %><%= $a ? '</A>' : '' %></TD>
123              <% } else { %>
124                <TD<%= $align %>><%= $a %><%= $row->$field() %><%= $a ? '</A>' : '' %></TD>
125              <% } %>
126            <% } %>
127          <% } else { %>
128            <% foreach ( @$row ) { %>
129                 <TD><%= $_ %></TD>
130            <% } %>
131          <% } %>
132          </TR>
133     <% } %>
134   
135   </TABLE>
136   <%= $pager %>
137 <% } %>
138 </BODY>
139 </HTML>
140 <% } %>
141