[freeside-commits] freeside/httemplate/search/elements metasearch.html, 1.1, 1.2 search-xml.html, 1.1, 1.2 search-html.html, 1.6, 1.7 search.html, 1.49, 1.50

Jeff Finucane,420,, jeff at wavetail.420.am
Sat May 22 12:57:55 PDT 2010


Update of /home/cvs/cvsroot/freeside/httemplate/search/elements
In directory wavetail.420.am:/tmp/cvs-serv32168/httemplate/search/elements

Modified Files:
	search-html.html search.html 
Added Files:
	metasearch.html search-xml.html 
Log Message:
improved fcc 477 report #7783

Index: search.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/elements/search.html,v
retrieving revision 1.49
retrieving revision 1.50
diff -u -w -d -r1.49 -r1.50
--- search.html	20 May 2010 22:48:42 -0000	1.49
+++ search.html	22 May 2010 19:57:53 -0000	1.50
@@ -172,6 +172,10 @@
 %
 <% include('search-xls.html',  header=>$header, rows=>$rows, opt=>\%opt ) %>
 %
+% } elsif ( $type eq 'xml' ) {
+%
+<% include('search-xml.html',  rows=>$rows, opt=>\%opt ) %>
+%
 % } else { # regular HTML
 %
 <% include('search-html.html',
@@ -195,7 +199,7 @@
 
 my $curuser = $FS::CurrentUser::CurrentUser;
 
-my $type = $cgi->param('_type') =~ /^(csv|\w*\.xls|select|html(-print)?)$/
+my $type = $cgi->param('_type') =~ /^(csv|\w*\.xls|xml|select|html(-print)?)$/
            ? $1 : 'html' ;
 
 my %align = (
@@ -224,9 +228,13 @@
   } while ( exists($h->[$i]) );
 }
 
+# wtf?
 $opt{disable_download} = 0
   if $opt{disable_download} && $curuser->access_right('Configuration download');
 
+$opt{disable_download} = 1
+  if $opt{really_disable_download};
+
 my @link_agentnums = ();
 my $null_link = '';
 if ( $opt{'agent_virt'} ) {

--- NEW FILE: metasearch.html ---
<%doc>

Example:

  include( 'elements/metasearch.html',

    ###
    # required
    ###

    'title'         => 'Page title',
    
    #arrayref of hashrefs suited for passing to elements/search.html
    #see that documentation
    'search'      => [
                       {
                         query => { 'table'     => 'tablename',
                                    #everything else is optional...
                                    'hashref'   => { 'f1' => 'value',
                                                     'f2' => { 'op'   => '<',
                                                               'value' => '54',
                                                             },
                                                    },
                                    'select'    => '*',
                                    'order_by'  => 'ORDER BY something',
   
                                  },
                         count_query => 'SELECT COUNT(*) FROM tablename',
                       },
                       {
                         query => 'table'     => 'anothertablename',
                         count_query => 'SELECT COUNT(*) FROM anothertablename',
                       },
                     ],
   
    ###
    # optional
    ###
   
    # some HTML callbacks...
    'menubar'          => '', #menubar arrayref
    'html_init'        => '', #after the header/menubar and before the pager
    'html_form'        => '', #after the pager, right before the results
                              # (only shown if there are results)
                              # (use this for any form-opening tag rather than
                              #  html_init, to avoid a nested form)
    'html_foot'        => '', #at the bottom
    'html_posttotal'   => '', #at the bottom
                              # (these three can be strings or coderefs)
   
  );

</%doc>
% foreach my $search ( @{$opt{search}} ) {
<% include('search.html',
             %$search,
             'type'           => $type,
             'nohtmlheader'   => 1,
          )
%>
%
% } 
<%init>

my(%opt) = @_;
#warn join(' / ', map { "$_ => $opt{$_}" } keys %opt ). "\n";

my $type = $cgi->param('_type') =~ /^(csv|\w*\.xls|select|html(-print)?)$/
           ? $1 : 'html' ;

</%init>

--- NEW FILE: search-xml.html ---
% foreach my $row ( @$rows ) {
%
% if (&{$beginrow}($row)){
<% &{$beginrow}($row) %>
% }
%
%   foreach my $i ( 0 .. scalar( @{$opt{'fields'}} ) - 1 ) {
%     my $field = $opt{'fields'}->[$i];
%     my $value = '';
%     if ( ref($field) eq 'CODE' ) {
%       $value = &{$field}($row);
%       $value = '(N/A)' #unimplemented
%         if ref($value) eq 'ARRAY';
%     } else {
%       $value = $row->$field();
%     }
%
<% &{$beginfield}($row, $i) %><% $value |h %><% &{$endfield}($row, $i) %>
%
%   }
%
% if (&{$endrow}($row)) {
<% &{$endrow}($row) %>
% }
%
% }
<%init>

my %args = @_;
my $header = $args{'header'};
my $rows   = $args{'rows'};
my %opt    = %{ $args{'opt'} };

http_header('Content-Type' => 'application/XML' ); # So saith RFC 4180
http_header('Content-Disposition' => 
  'attachment;filename="'.($opt{'name'} || PL($opt{'name_singular'}) ).'.xml"');

unless ( $opt{'fields'} ) {
  foreach my $i ( 0 .. ( $#{ @$rows[0] } ) ) {
    $opt{'fields'}->[$i] = sub { my $row = shift; $row->[$i]; };
  }
}

my $beginrow = sub { return ''; };
my $endrow = sub { return ''; };
if ($opt{xml_row_element}) {
  $beginrow = sub { my ($row, $index) = @_;
                    my $value;
                    if ( ref($opt{xml_row_element}) eq 'CODE' ) {
                      $value = &{$opt{xml_row_element}}($row);
                    } else {
                      $value = $opt{xml_row_element};
                    }
                    return "<$value>";
                  };
  $endrow = sub { my ($row, $index) = @_;
                  my $value;
                  if ( ref($opt{xml_row_element}) eq 'CODE' ) {
                    $value = &{$opt{xml_row_element}}($row);
                  } else {
                    $value = $opt{xml_row_element};
                  }
                  return "</$value>";
                };
}
my $beginfield = sub { my ($row, $index) = @_;
                       my $value;
                       if ( ref($opt{xml_elements}->[$index]) eq 'CODE' ) {
                         $value = &{$opt{xml_elements}->[$index]}($row);
                       } else {
                         $value = $opt{xml_elements}->[$index];
                       }
                       return "<$value>";
                     };
my $endfield =   sub { my ($row, $index) = @_;
                       my $value;
                       if ( ref($opt{xml_elements}->[$index]) eq 'CODE' ) {
                         $value = &{$opt{xml_elements}->[$index]}($row);
                       } else {
                         $value = $opt{xml_elements}->[$index];
                       }
                       return "</$value>";
                     };

$beginfield = sub { return ''; } if $opt{no_field_elements};  #hmm
$endfield = sub { return ''; } if $opt{no_field_elements};  #hmm

</%init>

Index: search-html.html
===================================================================
RCS file: /home/cvs/cvsroot/freeside/httemplate/search/elements/search-html.html,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -w -d -r1.6 -r1.7
--- search-html.html	1 Apr 2010 04:43:55 -0000	1.6
+++ search-html.html	22 May 2010 19:57:53 -0000	1.7
@@ -32,11 +32,17 @@
 %
 %     if ( $type eq 'html-print' ) {
 
-        <% include( '/elements/header-popup.html', $opt{'title'} ) %>
+        <% $opt{nohtmlheader}
+             ? ''
+             : include( '/elements/header-popup.html', $opt{'title'} )
+        %>
 
 %     } elsif ( $type eq 'select' ) {
 
-        <% include( '/elements/header-popup.html', $opt{'title'} ) %>
+        <% $opt{nohtmlheader}
+             ? ''
+             : include( '/elements/header-popup.html', $opt{'title'} )
+        %>
         <% defined($opt{'html_init'}) 
               ? ( ref($opt{'html_init'})
                     ? &{$opt{'html_init'}}()
@@ -54,7 +60,9 @@
 %       #  @menubar = ( 'Main menu' => $p );
 %       }
 
-        <% include( '/elements/header.html', $opt{'title'},
+        <% $opt{nohtmlheader}
+             ? ''
+             : include( '/elements/header.html', $opt{'title'},
                       include( '/elements/menubar.html', @menubar )
                   )
         %>
@@ -95,7 +103,7 @@
 %                 $cgi->delete('maxrecords');
 %                 $cgi->param('_dummy', 1);
 
-                  ( show <SELECT NAME="maxrecords" onChange="window.location = '<% $cgi->self_url %>;maxrecords=' + this.options[this.selectedIndex].value;">
+                  ( show <SELECT NAME="maxrecords" onChange="window.location = '<% $self_url %>;maxrecords=' + this.options[this.selectedIndex].value;">
 
 %                   foreach my $max ( map { $_ * $confmax } qw( 1 5 10 25 ) ) {
                   <OPTION VALUE="<% $max %>" <% ( $maxrecords == $max ) ? 'SELECTED' : '' %>><% $max %></OPTION>
@@ -136,13 +144,18 @@
                 Download full results<BR>
 
 %               $cgi->param('_type', "$xlsname.xls" ); 
-                as <A HREF="<% $cgi->self_url %>">Excel spreadsheet</A><BR>
+                as <A HREF="<% $self_url %>">Excel spreadsheet</A><BR>
 
 %               $cgi->param('_type', 'csv'); 
-                as <A HREF="<% $cgi->self_url %>">CSV file</A><BR>
+                as <A HREF="<% $self_url %>">CSV file</A><BR>
+
+%             if ( defined($opt{xml_elements}) ) {
+%               $cgi->param('_type', 'xml'); 
+                as <A HREF="<% $self_url %>">XML file</A><BR>
+%             }
 
 %               $cgi->param('_type', 'html-print'); 
-                as <A HREF="<% $cgi->self_url %>">printable copy</A>
+                as <A HREF="<% $self_url %>">printable copy</A>
 
               </TD>
 %             $cgi->param('_type', "html" ); 
@@ -417,9 +430,11 @@
 %     }
 
 %     if ( $type eq 'html-print' ) {
+%       unless ( $opt{nohtmlheader} ) {
 
         </BODY></HTML>
       
+%       }
 %     } else {
 
         <% defined($opt{'html_foot'}) 
@@ -430,7 +445,10 @@
               : ''
         %>
 
-        <% include( '/elements/footer.html' ) %>
+        <% $opt{nohtmlheader}
+             ? ''
+             : include( '/elements/footer.html' )
+        %>
 
 %     }
 
@@ -447,6 +465,7 @@
 my $maxrecords     = $args{'maxrecords'};
 my $offset         = $args{'offset'};
 my %opt            = %{ $args{'opt'} };
+my $self_url       = $opt{'url'} || $cgi->self_url;
 
 my $count_sth = dbh->prepare($opt{'count_query'})
   or die "Error preparing $opt{'count_query'}: ". dbh->errstr;



More information about the freeside-commits mailing list