summaryrefslogtreecommitdiff
path: root/httemplate/search
diff options
context:
space:
mode:
authorivan <ivan>2003-08-08 05:42:13 +0000
committerivan <ivan>2003-08-08 05:42:13 +0000
commite283ab567e6890727e4d8e35c1d8097398678753 (patch)
tree2b15e95819c245027d1b8cb9f0976fd49b5b4190 /httemplate/search
parentef7bb336cc67f127fb1d77532ad3da1369c0ae36 (diff)
- (finish) includes! (closes: Bug#551)
- (finish) moving SQL search to including generic elements/search.html - new elements: menubar.html, header.html, pager.html and table.html - have masonize process .html files also
Diffstat (limited to 'httemplate/search')
-rw-r--r--httemplate/search/elements/search.html57
-rwxr-xr-xhttemplate/search/sql.cgi76
-rw-r--r--httemplate/search/sql.html12
3 files changed, 62 insertions, 83 deletions
diff --git a/httemplate/search/elements/search.html b/httemplate/search/elements/search.html
index fba8ee815..fbedcaa26 100644
--- a/httemplate/search/elements/search.html
+++ b/httemplate/search/elements/search.html
@@ -1,8 +1,59 @@
<%
-my $conf = new FS::Conf;
-my $maxrecords = $conf->config('maxsearchrecordsperpage');
+ my %opt = @_;
+ 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 $limit = $maxrecords ? "LIMIT $maxrecords" : '';
+ 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 $total = $count_sth->fetchrow_arrayref->[0];
+
+ 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?
+ my $rows = $sth->fetchall_arrayref;
+
+%>
+<!-- mason kludge -->
+<% my $pager = include ( '/elements/pager.html',
+ 'offset' => $offset,
+ 'num_rows' => scalar(@$rows),
+ 'total' => $total,
+ 'maxrecords' => $maxrecords,
+ );
%>
+
+<%= $total %> total <%= $opt{'name'} %><BR><BR><%= $pager %>
+<%= include( '/elements/table.html' ) %>
+ <TR>
+ <% foreach ( @{$sth->{NAME}} ) { %>
+ <TH><%= $_ %></TH>
+ <% } %>
+ </TR>
+ <% foreach my $row ( @$rows ) { %>
+ <TR>
+ <% foreach ( @$row ) { %>
+ <TD><%= $_ %></TD>
+ <% } %>
+ </TR>
+ <% } %>
+
+</TABLE>
+<%= $pager %>
+</BODY>
+</HTML>
diff --git a/httemplate/search/sql.cgi b/httemplate/search/sql.cgi
deleted file mode 100755
index b83ef039f..000000000
--- a/httemplate/search/sql.cgi
+++ /dev/null
@@ -1,76 +0,0 @@
-<%
-
-my $conf = new FS::Conf;
-my $maxrecords = $conf->config('maxsearchrecordsperpage');
-
-my $limit = '';
-$limit .= "LIMIT $maxrecords" if $maxrecords;
-
-my $offset = $cgi->param('offset') || 0;
-$limit .= " OFFSET $offset" if $offset;
-
-my $total;
-
-my $sql = $cgi->param('sql');
-$sql =~ s/^\s*SELECT//i;
-
-my $count_sql = $sql;
-$count_sql =~ s/^(.*)\s+FROM\s/COUNT(*) FROM /i;
-
-my $sth = dbh->prepare("SELECT $count_sql")
- or eidiot dbh->errstr. " doing $count_sql\n";
-$sth->execute or eidiot "Error executing \"$count_sql\": ". $sth->errstr;
-
-$total = $sth->fetchrow_arrayref->[0];
-
-my $sth = dbh->prepare("SELECT $sql $limit")
- or eidiot dbh->errstr. " doing $sql\n";
-$sth->execute or eidiot "Error executing \"$sql\": ". $sth->errstr;
-my $rows = $sth->fetchall_arrayref;
-
-%>
-<!-- mason kludge -->
-<%
-
- #begin pager
- my $pager = '';
- if ( $total != scalar(@$rows) && $maxrecords ) {
- unless ( $offset == 0 ) {
- $cgi->param('offset', $offset - $maxrecords);
- $pager .= '<A HREF="'. $cgi->self_url.
- '"><B><FONT SIZE="+1">Previous</FONT></B></A> ';
- }
- my $poff;
- my $page;
- for ( $poff = 0; $poff < $total; $poff += $maxrecords ) {
- $page++;
- if ( $offset == $poff ) {
- $pager .= qq!<FONT SIZE="+2">$page</FONT> !;
- } else {
- $cgi->param('offset', $poff);
- $pager .= qq!<A HREF="!. $cgi->self_url. qq!">$page</A> !;
- }
- }
- unless ( $offset + $maxrecords > $total ) {
- $cgi->param('offset', $offset + $maxrecords);
- $pager .= '<A HREF="'. $cgi->self_url.
- '"><B><FONT SIZE="+1">Next</FONT></B></A> ';
- }
- }
- #end pager
-
- print header('Query Results', menubar('Main Menu'=>$p) ).
- "$total total rows<BR><BR>$pager". table().
- "<TR>";
- print "<TH>$_</TH>" foreach @{$sth->{NAME}};
- print "</TR>";
-
- foreach $row ( @$rows ) {
- print "<TR>";
- print "<TD>$_</TD>" foreach @$row;
- print "</TR>";
- }
-
- print "</TABLE>$pager</BODY></HTML>";
-
-%>
diff --git a/httemplate/search/sql.html b/httemplate/search/sql.html
index e61ce1538..7d7fc0890 100644
--- a/httemplate/search/sql.html
+++ b/httemplate/search/sql.html
@@ -1,8 +1,12 @@
-<%= include( '/elements/header', 'Query Results',
- include( '/elements/menubar', 'Main Menu' => $p )
+<%= include( '/elements/header.html', 'Query Results',
+ include( '/elements/menubar.html', 'Main Menu' => $p )
+ )
%>
+
<%= include( 'elements/search.html',
- 'name' => 'rows',
-
+ 'name' => 'rows',
+ 'query' => 'SELECT '. ( $cgi->param('sql')
+ || eidiot('Empty query') ),
+ )
%>