add customer # to payment reports, add table cell alignment option to general search...
[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   } else {
52     my $sth = dbh->prepare("$opt{'query'} $limit")
53       or die "Error preparing $opt{'query'}: ". dbh->errstr;
54     $sth->execute
55       or die "Error executing $opt{'query'}: ". $sth->errstr;
56
57     #can get # of rows without fetching them all?
58     $rows = $sth->fetchall_arrayref;
59
60     $header ||= $sth->{NAME};
61   }
62
63   if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1 ) {
64     my( $url, $method ) = @{$opt{'redirect'}};
65     redirect( $url. $rows->[0]->$method() );
66   } else {
67     $opt{'name'} =~ s/s$// if $total == 1;
68 %>
69 <%= include( '/elements/header.html', $opt{'title'},
70                include( '/elements/menubar.html', 'Main menu' => $p )
71            )
72 %>
73 <% my $pager = include ( '/elements/pager.html',
74                            'offset'     => $offset,
75                            'num_rows'   => scalar(@$rows),
76                            'total'      => $total,
77                            'maxrecords' => $maxrecords,
78                        );
79 %>
80 <% unless ( $total ) { %>
81   No matching <%= $opt{'name'} %> found.<BR>
82 <% } else { %>
83   <%= $total %> total <%= $opt{'name'} %><BR>
84   <% if ( $opt{'count_addl'} ) { %>
85     <% my $n=0; foreach my $count ( @{$opt{'count_addl'}} ) { %>
86       <%= sprintf( $count, $count_arrayref->[++$n] ) %><BR>
87     <% } %>
88   <% } %>
89   <BR><%= $pager %>
90   <%= include( '/elements/table.html' ) %>
91     <TR>
92     <% foreach my $header ( @$header ) { %>
93          <TH><%= $header %></TH>
94     <% } %>
95     </TR>
96     <% foreach my $row ( @$rows ) { %>
97          <TR>
98          <% if ( $opt{'fields'} ) {
99               my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
100               my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
101               foreach my $field ( @{$opt{'fields'}} ) {
102                 my $align = $aligns ? shift @$aligns : '';
103                 $align = " ALIGN=$align" if $align;
104                 my $a = '';
105                 if ( $links ) {
106                   my $link = shift @$links;
107                   $link = &{$link}($row) if ref($link) eq 'CODE';
108                   if ( $link ) {
109                     my( $url, $method ) = @{$link};
110                     if ( ref($method) eq 'CODE' ) {
111                       $a = $url. &{$method}($row);
112                     } else {
113                       $a = $url. $row->$method();
114                     }
115                     $a = qq(<A HREF="$a">);
116                   }
117                 }
118              %>
119              <% if ( ref($field) eq 'CODE' ) { %>
120                <TD<%= $align %>><%= $a %><%= &{$field}($row) %><%= $a ? '</A>' : '' %></TD>
121              <% } else { %>
122                <TD<%= $align %>><%= $a %><%= $row->$field() %><%= $a ? '</A>' : '' %></TD>
123              <% } %>
124            <% } %>
125          <% } else { %>
126            <% foreach ( @$row ) { %>
127                 <TD><%= $_ %></TD>
128            <% } %>
129          <% } %>
130          </TR>
131     <% } %>
132   
133   </TABLE>
134   <%= $pager %>
135 <% } %>
136 </BODY>
137 </HTML>
138 <% } %>
139