doc
[freeside.git] / httemplate / search / elements / search.html
index f136832..5058a1a 100644 (file)
@@ -16,7 +16,7 @@ Example:
                                 # (deprecated, will be singularlized
                                 #  simplisticly)
 
-    #literal SQL query string (deprecated?) or qsearch hashref or arrayref
+    #literal SQL query string (corner cases only) or qsearch hashref or arrayref
     #of qsearch hashrefs for a union of qsearches
     'query'       => {
                        'table'     => 'tablename',
@@ -34,7 +34,7 @@ Example:
                      },
                      # "select * from tablename";
    
-    #required unless 'query' is an SQL query string (shouldn't be...)
+    #required (now even if 'query' is an SQL query string)
     'count_query' => 'SELECT COUNT(*) FROM tablename',
 
     ###
@@ -131,12 +131,15 @@ Example:
     'agent_pos'             => 3, # optional position (starting from 0) to
                                   # insert an Agent column (query needs to be a
                                   # qsearch hashref and header & fields need to
-                                  # be defined)cust_pkg_susp.html
+                                  # be defined)
 
     # sort, link & display properties for fields
 
-    'sort_fields' => [], #optional list of field names or SQL expressions for
-                         # sorts
+    'sort_fields' => [], #optional list of field names or SQL expressions for sorts
+
+    'order_by_sql' => {              #to keep complex SQL expressions out of cgi order_by value,
+      'fieldname' => 'sql snippet',  #  maps fields/sort_fields values to sql snippets
+    }
    
     #listref - each item is the empty string,
     #          or a listref of link and method name to append,
@@ -176,6 +179,7 @@ Example:
   &>
 
 </%doc>
+% # if changing this, also update saved search behavior to match!
 % if ( $type eq 'csv' ) {
 %
 <% include('search-csv.html',  header=>$header, rows=>$rows, opt=>\%opt ) %>
@@ -222,6 +226,18 @@ if ( !$curuser->access_right('Download report data') ) {
   $type = 'html';
 }
 
+# split/map aligns here before doing anything else
+my %align = (
+  'l' => 'left',
+  'r' => 'right',
+  'c' => 'center',
+  ' ' => '',
+  '.' => '',
+);
+
+$opt{align} = [ map $align{$_}, split(//, $opt{align}) ],
+  unless !$opt{align} || ref($opt{align});
+
 if($type =~ /csv|xls/) {
   my $h = $opt{'header'};
   my @del;
@@ -245,18 +261,6 @@ $opt{disable_download} = 0
 $opt{disable_download} = 1
   if $opt{really_disable_download};
 
-# split/map aligns here, so that agent_virt can add a column
-# (search-html.html will split aligns also if they aren't already split)
-my %align = (
-  'l' => 'left',
-  'r' => 'right',
-  'c' => 'center',
-  ' ' => '',
-  '.' => '',
-);
-$opt{align} = [ map $align{$_}, split(//, $opt{align}) ],
-  unless !$opt{align} || ref($opt{align});
-
 # get our queries ready
 my $query = $opt{query} or die "query required";
 my $count_query = $opt{count_query} or die "count_query required";
@@ -348,8 +352,8 @@ if ( $opt{'disableable'} ) {
       sub { shift->disabled ? 'FF0000'   : '00CC00'; };
     splice @{ $opt{'links'}  }, $pos, 0, ''
       if $opt{'links'};
-    splice @{ $opt{'link_onlicks'}  }, $pos, 0, ''
-      if $opt{'link_onlicks'};
+    splice @{ $opt{'link_onclicks'}  }, $pos, 0, ''
+      if $opt{'link_onclicks'};
   }
 
   #add show/hide disabled links
@@ -379,7 +383,7 @@ unless ( $type =~ /^(csv|xml|\w*.xls)$/) {
     #setup some pagination things if we're in html mode
 
     my $conf = new FS::Conf;
-    $confmax = $conf->config('maxsearchrecordsperpage');
+    $confmax = $conf->config('maxsearchrecordsperpage') || 100;
     if ( $cgi->param('maxrecords') =~ /^(\d+)$/ ) {
       $maxrecords = $1;
     } else {
@@ -406,6 +410,12 @@ $order_by = $cgi->param('order_by') if $cgi->param('order_by');
 my $header = [ map { ref($_) ? $_->{'label'} : $_ } @{$opt{header}} ];
 my $rows;
 
+my ($order_by_key,$order_by_desc) = ($order_by =~ /^\s*(.*?)(\s+DESC)?\s*$/i);
+$opt{'order_by_sql'} ||= {};
+$order_by_desc ||= '';
+$order_by = $opt{'order_by_sql'}{$order_by_key} . $order_by_desc
+  if $opt{'order_by_sql'}{$order_by_key};
+
 if ( ref $query ) {
   my @query;
   if (ref($query) eq 'HASH') {