X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FHelper%2FSorted.pm;h=98c0a7c1088707e6267cdd1c7c86eed5c7fd32f1;hb=a4f1494fd428b5388c92a0ad065ad807f32c2090;hp=ca8a565a801aa539280a6322a90ab08209c772ee;hpb=29637d3f54132b3c10293f3600902d107127cdbc;p=kivitendo-erp.git diff --git a/SL/Controller/Helper/Sorted.pm b/SL/Controller/Helper/Sorted.pm index ca8a565a8..98c0a7c10 100644 --- a/SL/Controller/Helper/Sorted.pm +++ b/SL/Controller/Helper/Sorted.pm @@ -2,12 +2,16 @@ package SL::Controller::Helper::Sorted; use strict; +use Carp; +use List::MoreUtils qw(uniq); + use Exporter qw(import); -our @EXPORT = qw(make_sorted get_sort_spec get_current_sort_params _save_current_sort_params _get_models_handler_for_sorted _callback_handler_for_sorted); +our @EXPORT = qw(make_sorted get_sort_spec get_current_sort_params set_report_generator_sort_options + _save_current_sort_params _get_models_handler_for_sorted _callback_handler_for_sorted); use constant PRIV => '__sortedhelperpriv'; -my $controller_sort_spec; +my %controller_sort_spec; sub make_sorted { my ($class, %specs) = @_; @@ -18,7 +22,7 @@ sub make_sorted { while (my ($column, $spec) = each %specs) { next if $column =~ m/^[A-Z_]+$/; - $spec = $specs{$column} = { title => $spec } if !ref $spec; + $spec = $specs{$column} = { title => $spec } if (ref($spec) || '') ne 'HASH'; $spec->{model} ||= $specs{MODEL}; $spec->{model_column} ||= $column; @@ -31,7 +35,7 @@ sub make_sorted { $specs{ONLY} ||= []; $specs{ONLY} = [ $specs{ONLY} ] if !ref $specs{ONLY}; - $controller_sort_spec = \%specs; + $controller_sort_spec{$class} = \%specs; my %hook_params = @{ $specs{ONLY} } ? ( only => $specs{ONLY} ) : (); $class->run_before('_save_current_sort_params', %hook_params); @@ -49,7 +53,7 @@ sub make_sorted { sub get_sort_spec { my ($class_or_self) = @_; - return $controller_sort_spec; + return $controller_sort_spec{ref($class_or_self) || $class_or_self}; } sub get_current_sort_params { @@ -72,6 +76,30 @@ sub get_current_sort_params { return %sort_params; } +sub set_report_generator_sort_options { + my ($self, %params) = @_; + + $params{$_} or croak("Missing parameter '$_'") for qw(report sortable_columns); + + my %current_sort_params = $self->get_current_sort_params; + + foreach my $col (@{ $params{sortable_columns} }) { + $params{report}->{columns}->{$col}->{link} = $self->get_callback( + sort_by => $col, + sort_dir => ($current_sort_params{by} eq $col ? 1 - $current_sort_params{dir} : $current_sort_params{dir}), + ); + } + + $params{report}->set_sort_indicator($current_sort_params{by}, 1 - $current_sort_params{dir}); + + if ($params{report}->{export}) { + $params{report}->{export}->{variable_list} = [ uniq( + @{ $params{report}->{export}->{variable_list} }, + @{ $self->get_sort_spec->{FORM_PARAMS} } + )]; + } +} + # # private functions # @@ -277,6 +305,9 @@ Required. A user-displayable title to be used by functions like the layout helper's C. Does not have a default value. +Note that this string must be the untranslated English version of the +string. The titles will be translated whenever they're requested. + =item * C Optional. The name of a Rose database model this sort index refers @@ -317,6 +348,31 @@ Returns a hash reference to the sort spec structure given in the call to L after normalization (hash reference construction, applying default parameters etc). +=item C + +This function does three things with an instance of +L: + +=over 4 + +=item 1. it sets the sort indicator, + +=item 2. it sets the the links for those column headers that are +sortable and + +=item 3. it adds the C fields to the list of variables in +the report generator's export options. + +=back + +The report generator instance must be passed as the parameter +C. The parameter C must be an array +reference of column names that are sortable. + +The report generator instance must already have its columns and export +options set via calls to its L and +L functions. + =back =head1 BUGS