]> wagnertech.de Git - mfinanz.git/blobdiff - SL/Controller/Helper/Sorted.pm
Sorted-Controller-Helper: Hilfsfunktion zum Setzen der Parameter in einer ReportGener...
[mfinanz.git] / SL / Controller / Helper / Sorted.pm
index 4afae9d79f34ab2ff15a96392df339a230c3b240..57fb5e0bcf23c7b077045a524b24dc542bcea546 100644 (file)
@@ -2,8 +2,11 @@ package SL::Controller::Helper::Sorted;
 
 use strict;
 
+use Carp;
+
 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';
 
@@ -12,7 +15,7 @@ my $controller_sort_spec;
 sub make_sorted {
   my ($class, %specs) = @_;
 
-  $specs{MODEL} ||=  $class->_controller_name;
+  $specs{MODEL} ||=  $class->controller_name;
   $specs{MODEL}   =~ s{ ^ SL::DB:: (?: .* :: )? }{}x;
 
   while (my ($column, $spec) = each %specs) {
@@ -72,6 +75,23 @@ 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});
+}
+
 #
 # private functions
 #
@@ -277,6 +297,9 @@ Required. A user-displayable title to be used by functions like the
 layout helper's C<sortable_table_header>. 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<model>
 
 Optional. The name of a Rose database model this sort index refers
@@ -317,6 +340,19 @@ Returns a hash reference to the sort spec structure given in the call
 to L<make_sorted> after normalization (hash reference construction,
 applying default parameters etc).
 
+=item C<set_report_generator_sort_options %params>
+
+This function sets two things in an instance of
+L<SL::ReportGenerator>: the sort indicator and the links for those
+column headers that are sortable.
+
+The report generator instance must be passed as the parameter
+C<report>. The parameter C<sortable_columns> must be an array
+reference of column names that are sortable.
+
+The report generator instance must already have its columns set via a
+call to its L<SL::ReportGenerator::set_columns> function.
+
 =back
 
 =head1 BUGS