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",
51 (exists($opt{'query'}->{'addl_from'}) ? $opt{'query'}->{'addl_from'} : '')
54 my $sth = dbh->prepare("$opt{'query'} $limit")
55 or die "Error preparing $opt{'query'}: ". dbh->errstr;
57 or die "Error executing $opt{'query'}: ". $sth->errstr;
59 #can get # of rows without fetching them all?
60 $rows = $sth->fetchall_arrayref;
62 $header ||= $sth->{NAME};
65 if ( exists($opt{'redirect'}) && scalar(@$rows) == 1 && $total == 1 ) {
66 my( $url, $method ) = @{$opt{'redirect'}};
67 redirect( $url. $rows->[0]->$method() );
69 $opt{'name'} =~ s/s$// if $total == 1;
71 <%= include( '/elements/header.html', $opt{'title'},
72 include( '/elements/menubar.html', 'Main menu' => $p )
75 <% my $pager = include ( '/elements/pager.html',
77 'num_rows' => scalar(@$rows),
79 'maxrecords' => $maxrecords,
82 <% unless ( $total ) { %>
83 No matching <%= $opt{'name'} %> found.<BR>
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>
92 <%= include( '/elements/table.html' ) %>
94 <% foreach my $header ( @$header ) { %>
95 <TH><%= $header %></TH>
98 <% foreach my $row ( @$rows ) { %>
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;
108 my $link = shift @$links;
109 $link = &{$link}($row) if ref($link) eq 'CODE';
111 my( $url, $method ) = @{$link};
112 if ( ref($method) eq 'CODE' ) {
113 $a = $url. &{$method}($row);
115 $a = $url. $row->$method();
117 $a = qq(<A HREF="$a">);
121 <% if ( ref($field) eq 'CODE' ) { %>
122 <TD<%= $align %>><%= $a %><%= &{$field}($row) %><%= $a ? '</A>' : '' %></TD>
124 <TD<%= $align %>><%= $a %><%= $row->$field() %><%= $a ? '</A>' : '' %></TD>
128 <% foreach ( @$row ) { %>