]> wagnertech.de Git - kivitendo-erp.git/blobdiff - SL/Controller/Helper/GetModels/Sorted.pm
Doku
[kivitendo-erp.git] / SL / Controller / Helper / GetModels / Sorted.pm
index 9e133b8f6a0a4422bee79510a939e554e96fc6fb..14e733d452c397a96c8098f8e8d98c867e73c106 100644 (file)
@@ -7,7 +7,7 @@ use Carp;
 use List::MoreUtils qw(uniq);
 
 use Rose::Object::MakeMethods::Generic (
-  scalar => [ qw(by dir specs) ],
+  scalar => [ qw(by dir specs form_data) ],
   'scalar --get_set_init' => [ qw(form_params) ],
 );
 
@@ -37,22 +37,24 @@ sub init {
 
   $self->get_models->register_handlers(
     callback   => sub { shift; $self->_callback_handler_for_sorted(@_) },
-    get_models => sub { shift; $self->_get_models_handler_for_sorted(@_) },
   );
 
 #   $::lxdebug->dump(0, "CONSPEC", \%specs);
 }
 
-sub get_current_sort_params {
+sub read_params {
   my ($self, %params) = @_;
 
+  return %{ $self->form_data } if $self->form_data;
+
   my %sort_params;
   my ($by, $dir) = @{ $self->form_params };
+  my $source = $self->get_models->source;
 
-  if ($::form->{ $by }) {
+  if ($source->{ $by }) {
     %sort_params = (
-      sort_by  => $::form->{$by},
-      sort_dir => defined($::form->{$dir}) ? $::form->{$dir} * 1 : undef,
+      sort_by  => $source->{$by},
+      sort_dir => defined($source->{$dir}) ? $source->{$dir} * 1 : undef,
     );
   } elsif (!$self->by) {
     %sort_params = %params;
@@ -63,15 +65,28 @@ sub get_current_sort_params {
     );
   }
 
+  $self->form_data(\%sort_params);
+
   return %sort_params;
 }
 
+sub finalize {
+  my ($self, %params) = @_;
+
+  my %sort_params     = $self->read_params;
+  my $sort_spec       = $self->specs->{ $sort_params{sort_by} };
+
+  $params{sort_by}    = "SL::DB::Manager::$sort_spec->{model}"->make_sort_string(sort_by => $sort_spec->{model_column}, sort_dir => $sort_params{sort_dir});
+
+  %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;
+  my %current_sort_params = $self->read_params;
 
   foreach my $col (@{ $params{sortable_columns} }) {
     $params{report}->{columns}->{$col}->{link} = $self->get_models->get_callback(
@@ -96,7 +111,7 @@ sub set_report_generator_sort_options {
 
 sub _callback_handler_for_sorted {
   my ($self, %params) = @_;
-  my %spec = $self->get_current_sort_params;
+  my %spec = $self->read_params;
 
   if ($spec{sort_by}) {
     $params{ $self->form_params->[0] } = $spec{sort_by};
@@ -108,20 +123,6 @@ sub _callback_handler_for_sorted {
   return %params;
 }
 
-sub _get_models_handler_for_sorted {
-  my ($self, %params) = @_;
-
-  my %sort_params     = $self->get_current_sort_params;
-  my $sort_spec       = $self->specs->{ $sort_params{sort_by} };
-
-  $params{sort_by}    = "SL::DB::Manager::$sort_spec->{model}"->make_sort_string(sort_by => $sort_spec->{model_column}, sort_dir => $sort_params{sort_dir});
-
-  # $::lxdebug->dump(0, "GM handler for sorted; params nach modif:", \%params);
-
-  return %params;
-}
-
-
 sub init_form_params {
   [ qw(sort_by sort_dir) ]
 }
@@ -145,7 +146,7 @@ In a controller:
   use SL::Controller::Helper::GetModels;
   use SL::Controller::Helper::Sorted;
 
-  __PACKAGE__->make_sorted(
+  __PACKAGE__->make_sorted(                                     # update this
     DEFAULT_BY   => 'run_at',
     DEFAULT_DIR  => 1,
     MODEL        => 'BackgroundJobHistory',
@@ -169,7 +170,7 @@ In said template:
 
   <table>
    <tr>
-    <th>[% L.sortable_table_header('package_name') %]</th>
+    <th>[% L.sortable_table_header('package_name') %]</th>         # models
     <th>[% L.sortable_table_header('run_at') %]</th>
     <th>[% L.sortable_table_header('error') %]</th>
    </tr>
@@ -189,7 +190,7 @@ This specialized helper module enables controllers to display a
 sortable list of database models with as few lines as possible.
 
 For this to work the controller has to provide the information which
-indexes are eligible for sorting etc. by a call to L<make_sorted> at
+indexes are eligible for sorting etc. by a call to L<make_sorted> at  #not compiletime
 compile time.
 
 The underlying functionality that enables the use of more than just
@@ -213,7 +214,7 @@ parameters that were used in the current view.
 
 =over 4
 
-=item C<make_sorted %sort_spec>
+=item C<make_sorted %sort_spec>                                         # meh complete rewrite
 
 This function must be called by a controller at compile time. It is
 uesd to set the various parameters required for this helper to do its