12 $opt{align} = [ map $align{$_}, split(//, $opt{align}) ],
13 unless !$opt{align} || ref($opt{align});
15 if ( ref($opt{'query'}) ) {
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;
24 my $conf = new FS::Conf;
25 my $maxrecords = $conf->config('maxsearchrecordsperpage');
27 my $limit = $maxrecords ? "LIMIT $maxrecords" : '';
29 my $offset = $cgi->param('offset') || 0;
30 $limit .= " OFFSET $offset" if $offset;
32 my $count_sth = dbh->prepare($opt{'count_query'})
33 or die "Error preparing $opt{'count_query'}: ". dbh->errstr;
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];
39 #warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";
41 my $header = $opt{'header'};
43 if ( ref($opt{'query'}) ) {
44 #eval "use FS::$opt{'query'};";
46 $opt{'query'}->{'table'},
47 $opt{'query'}->{'hashref'} || {},
48 $opt{'query'}->{'select'},
49 $opt{'query'}->{'extra_sql'}. " $limit",
52 my $sth = dbh->prepare("$opt{'query'} $limit")
53 or die "Error preparing $opt{'query'}: ". dbh->errstr;
55 or die "Error executing $opt{'query'}: ". $sth->errstr;
57 #can get # of rows without fetching them all?
58 $rows = $sth->fetchall_arrayref;
60 $header ||= $sth->{NAME};
63 if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1 ) {
64 my( $url, $method ) = @{$opt{'redirect'}};
65 redirect( $url. $rows->[0]->$method() );
67 $opt{'name'} =~ s/s$// if $total == 1;
69 <%= include( '/elements/header.html', $opt{'title'},
70 include( '/elements/menubar.html', 'Main menu' => $p )
73 <% my $pager = include ( '/elements/pager.html',
75 'num_rows' => scalar(@$rows),
77 'maxrecords' => $maxrecords,
80 <% unless ( $total ) { %>
81 No matching <%= $opt{'name'} %> found.<BR>
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>
90 <%= include( '/elements/table.html' ) %>
92 <% foreach my $header ( @$header ) { %>
93 <TH><%= $header %></TH>
96 <% foreach my $row ( @$rows ) { %>
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;
106 my $link = shift @$links;
107 $link = &{$link}($row) if ref($link) eq 'CODE';
109 my( $url, $method ) = @{$link};
110 if ( ref($method) eq 'CODE' ) {
111 $a = $url. &{$method}($row);
113 $a = $url. $row->$method();
115 $a = qq(<A HREF="$a">);
119 <% if ( ref($field) eq 'CODE' ) { %>
120 <TD<%= $align %>><%= $a %><%= &{$field}($row) %><%= $a ? '</A>' : '' %></TD>
122 <TD<%= $align %>><%= $a %><%= $row->$field() %><%= $a ? '</A>' : '' %></TD>
126 <% foreach ( @$row ) { %>