- (finish) includes! (closes: Bug#551)
authorivan <ivan>
Fri, 8 Aug 2003 05:42:13 +0000 (05:42 +0000)
committerivan <ivan>
Fri, 8 Aug 2003 05:42:13 +0000 (05:42 +0000)
- (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

FS/FS/CGI.pm
bin/masonize
httemplate/elements/header.html [new file with mode: 0644]
httemplate/elements/menubar.html [new file with mode: 0644]
httemplate/elements/pager.html [new file with mode: 0644]
httemplate/elements/table.html [new file with mode: 0644]
httemplate/index.html
httemplate/search/elements/search.html
httemplate/search/sql.cgi [deleted file]
httemplate/search/sql.html

index 86d20f6..f615376 100644 (file)
@@ -44,8 +44,10 @@ Returns an HTML header.
 =cut
 
 sub header {
+  use Carp;
+  carp 'FS::CGI::header deprecated; include /elements/header.html instead';
+
   my($title,$menubar,$etc)=@_; #$etc is for things like onLoad= etc.
-  #use Carp;
   $etc = '' unless defined $etc;
 
   my $x =  <<END;
@@ -107,6 +109,9 @@ Returns an HTML menubar.
 =cut
 
 sub menubar { #$menubar=menubar('Main Menu', '../', 'Item', 'url', ... );
+  use Carp;
+  carp 'FS::CGI::menubar deprecated; include /elements/menubar.html instead';
+
   my($item,$url,@html);
   while (@_) {
     ($item,$url)=splice(@_,0,2);
@@ -225,6 +230,9 @@ Returns HTML tag for beginning a table.
 =cut
 
 sub table {
+  use Carp;
+  carp 'FS::CGI::table deprecated; include /elements/table.html instead';
+
   my $col = shift;
   if ( $col ) {
     qq!<TABLE BGCOLOR="$col" BORDER=1 WIDTH="100%" CELLSPACING=0 CELLPADDING=2 BORDERCOLOR="#999999">!;
index def0115..3139e0a 100755 (executable)
@@ -1,6 +1,7 @@
 #!/usr/bin/perl
 
-foreach $file ( split(/\n/, `find . -depth -print | grep cgi\$`) ) {
+foreach $file ( split(/\n/, `find . -depth -print`) ) {
+  next unless $file =~ /(cgi|html)$/;
   open(F,$file) or die "can't open $file for reading: $!";
   @file = <F>;
   #print "$file ". scalar(@file). "\n";
diff --git a/httemplate/elements/header.html b/httemplate/elements/header.html
new file mode 100644 (file)
index 0000000..581bbab
--- /dev/null
@@ -0,0 +1,19 @@
+<%
+  my($title, $menubar) = @_;
+  my $etc = @_ ? shift : ''; #$etc is for things like onLoad= etc.
+%>
+    <HTML>
+      <HEAD>
+        <TITLE>
+          <%= $title %>
+        </TITLE>
+        <META HTTP-Equiv="Cache-Control" Content="no-cache">
+        <META HTTP-Equiv="Pragma" Content="no-cache">
+        <META HTTP-Equiv="Expires" Content="0"> 
+      </HEAD>
+      <BODY BGCOLOR="#e8e8e8"<%= $etc %>>
+          <FONT SIZE=7>
+            <%= $title %>
+          </FONT>
+          <BR><BR>
+          <%= $menubar ? "$menubar<BR><BR>" : '' %>
diff --git a/httemplate/elements/menubar.html b/httemplate/elements/menubar.html
new file mode 100644 (file)
index 0000000..87a5031
--- /dev/null
@@ -0,0 +1,8 @@
+<%
+  my($item, $url, @html);
+  while (@_) {
+    ($item, $url) = splice(@_,0,2);
+    push @html, qq!<A HREF="$url">$item</A>!;
+  }
+%>
+<%= join(' | ', @html) %>
diff --git a/httemplate/elements/pager.html b/httemplate/elements/pager.html
new file mode 100644 (file)
index 0000000..db9ff83
--- /dev/null
@@ -0,0 +1,42 @@
+<%
+
+  my %opt = @_;
+
+  my $pager = '';
+  if ( $opt{'total'} != $opt{'num_rows'} && $opt{'maxrecords'} ) {
+    unless ( $opt{'offset'} == 0 ) {
+      $cgi->param('offset', $opt{'offset'} - $opt{'maxrecords'});
+%>
+
+      <A HREF="<%= $cgi->self_url %>"><B><FONT SIZE="+1">Previous</FONT></B></A>
+
+<%
+    }
+    my $page = 0;
+    for ( my $poff = 0; $poff < $opt{'total'}; $poff += $opt{'maxrecords'} ) {
+      $page++;
+      if ( $opt{'offset'} == $poff ) {
+%>
+
+        <FONT SIZE="+2"><%= $page %></FONT>
+
+<%
+      } else {
+        $cgi->param('offset', $poff);
+%>
+
+        <A HREF="<%= $cgi->self_url %>">$page</A>
+
+<%
+      }
+    }
+    unless ( $opt{'offset'} + $opt{'maxrecords'} > $opt{'total'} ) {
+      $cgi->param('offset', $opt{'offset'} + $opt{'maxrecords'});
+%>
+
+      <A HREF="<%= $cgi->self_url %>"><B><FONT SIZE="+1">Next</FONT></B></A>
+
+<%
+    }
+  }
+%>
diff --git a/httemplate/elements/table.html b/httemplate/elements/table.html
new file mode 100644 (file)
index 0000000..3b61087
--- /dev/null
@@ -0,0 +1,8 @@
+<%
+   my $color = shift;
+   if ( $color ) {
+%>
+    <TABLE BGCOLOR="<%= $color %>" BORDER=1 WIDTH="100%" CELLSPACING=0 CELLPADDING=2 BORDERCOLOR="#999999">
+<% } else { %>
+    <TABLE BORDER=1 CELLSPACING=0 CELLPADDING=2 BORDERCOLOR="#999999">
+<% } %>
index 99b321f..a898d91 100644 (file)
       <UL>
         <LI><A HREF="search/cust_main-otaker.cgi">Search customers by order-taker</A>
       </UL>
-    <FORM ACTION="search/sql.cgi" METHOD="POST">SQL query: <TT>SELECT </TT><INPUT TYPE="text" NAME="sql" SIZE=32><INPUT TYPE="submit" VALUE="Query"></FORM>
+    <FORM ACTION="search/sql.html" METHOD="POST">SQL query: <TT>SELECT </TT><INPUT TYPE="text" NAME="sql" SIZE=32><INPUT TYPE="submit" VALUE="Query"></FORM>
 
     <BR>
     </TD></TR>
index fba8ee8..fbedcaa 100644 (file)
@@ -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 (executable)
index b83ef03..0000000
+++ /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>";
-
-%>
index e61ce15..7d7fc08 100644 (file)
@@ -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') ),
+    )
 %>