diff options
| author | ivan <ivan> | 2010-11-11 01:44:43 +0000 |
|---|---|---|
| committer | ivan <ivan> | 2010-11-11 01:44:43 +0000 |
| commit | 033709735189caa804476fc17e9946809516fcf7 (patch) | |
| tree | 8e10a68a570f73bbff6199d6b574716c407f4576 /httemplate/search/elements | |
| parent | 315efbebcacc909a11eb0379bd2f67bd3145243c (diff) | |
sort by clicking on headers, RT#6465
Diffstat (limited to 'httemplate/search/elements')
| -rwxr-xr-x | httemplate/search/elements/cust_pay_or_refund.html | 9 | ||||
| -rw-r--r-- | httemplate/search/elements/search-html.html | 15 | ||||
| -rw-r--r-- | httemplate/search/elements/search.html | 26 |
3 files changed, 48 insertions, 2 deletions
diff --git a/httemplate/search/elements/cust_pay_or_refund.html b/httemplate/search/elements/cust_pay_or_refund.html index 6f4aaf848..fccb9eef7 100755 --- a/httemplate/search/elements/cust_pay_or_refund.html +++ b/httemplate/search/elements/cust_pay_or_refund.html @@ -48,6 +48,7 @@ Examples: 'redirect_empty' => $opt{'redirect_empty'}, 'header' => \@header, 'fields' => \@fields, + 'sort_fields' => \@sort_fields, 'align' => $align, 'links' => \@links, 'color' => \@color, @@ -135,6 +136,7 @@ if ( $cgi->param('tax_names') ) { my @header = (); my @fields = (); +my @sort_fields = (); my $align = ''; my @links = (); if ( $opt{'pre_header'} ) { @@ -142,6 +144,7 @@ if ( $opt{'pre_header'} ) { $align .= 'c' x scalar(@{ $opt{'pre_header'} }); push @links, map '', @{ $opt{'pre_header'} }; push @fields, @{ $opt{'pre_fields'} }; + push @sort_fields, @{ $opt{'pre_fields'} }; } push @header, "\u$name_singular", @@ -152,18 +155,21 @@ push @links, '', ''; push @fields, 'payby_payinfo_pretty', sub { sprintf('$%.2f', shift->$amount_field() ) }, ; +push @sort_fields, '', $amount_field; if ( $unapplied ) { push @header, 'Unapplied'; $align .= 'r'; push @links, ''; push @fields, sub { sprintf('$%.2f', shift->unapplied_amount) }; + push @sort_fields, ''; } push @header, 'Date'; $align .= 'r'; push @links, ''; push @fields, sub { time2str('%b %d %Y', shift->_date ) }; +push @sort_fields, '_date'; unless ( $opt{'disable_by'} ) { push @header, 'By'; @@ -414,7 +420,8 @@ if ( $cgi->param('magic') ) { 'table' => $table, 'select' => join(', ', @select), 'hashref' => {}, - 'extra_sql' => "$search $group_by ORDER BY $orderby", + 'extra_sql' => "$search $group_by", + 'order_by' => "ORDER BY $orderby", 'addl_from' => $addl_from, }; diff --git a/httemplate/search/elements/search-html.html b/httemplate/search/elements/search-html.html index 98f9c4476..d6cc62691 100644 --- a/httemplate/search/elements/search-html.html +++ b/httemplate/search/elements/search-html.html @@ -190,11 +190,26 @@ <TR> % my $h2 = 0; % my $colspan = 0; +% my @fields = @{ $opt{'sort_fields'} || $opt{'fields'}}; +% my $order_by = $cgi->param('order_by'); % foreach my $header ( @{ $opt{header} } ) { +% +% my $field = shift @fields; +% % $colspan-- if $colspan > 0; % next if $colspan; % % my $label = ref($header) ? $header->{label} : $header; +% unless ( ref($field) || !$field ) { +% if ( $order_by eq $field ) { +% $cgi->param('order_by', "$field DESC"); +% } else { +% $cgi->param('order_by', $field); +% } +% $label = qq(<A HREF="$self_url?). $cgi->query_string. +% qq(">$label</A>); +% } +% % $colspan = ref($header) ? $header->{colspan} : 0; % my $rowspan = 1; % my $style = ''; diff --git a/httemplate/search/elements/search.html b/httemplate/search/elements/search.html index 218816938..a8e9f086a 100644 --- a/httemplate/search/elements/search.html +++ b/httemplate/search/elements/search.html @@ -132,7 +132,10 @@ Example: # qsearch hashref and header & fields need to # be defined) - # link & display properties for fields + # sort, link & display properties for fields + + 'sort_fields' => [], #optional list of field names or SQL expressions for + # sorts #listref - each item is the empty string, # or a listref of link and method name to append, @@ -369,6 +372,13 @@ unless ( $type =~ /^(csv|\w*.xls)$/) { } +#order by override +my $order_by = ''; +#if ( $cgi->param('order_by') =~ /^([\w\, ]+)$/ ) { +# $order_by = $1; +#} +$order_by = $cgi->param('order_by') if $cgi->param('order_by'); + # run the query my $header = [ map { ref($_) ? $_->{'label'} : $_ } @{$opt{header}} ]; @@ -378,6 +388,20 @@ if ( ref($opt{query}) ) { my @query; if (ref($opt{query}) eq 'HASH') { @query = ( $opt{query} ); + + if ( $order_by ) { + if ( $opt{query}->{'order_by'} ) { + if ( $opt{query}->{'order_by'} =~ /^(\s*ORDER\s+BY\s+)?(\S.*)$/is ) { + $opt{query}->{'order_by'} = "ORDER BY $order_by, $2"; + } else { + warn "unparsable query order_by: ". $opt{query}->{'order_by'}; + die "unparsable query order_by: ". $opt{query}->{'order_by'}; + } + } else { + $opt{query}->{'order_by'} = "ORDER BY $order_by"; + } + } + } elsif (ref($opt{query}) eq 'ARRAY') { @query = @{ $opt{query} }; } else { |
