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