Inventory.pm - Whitespace entfernt
[kivitendo-erp.git] / SL / Controller / Helper / GetModels / Sorted.pm
index 9e133b8..73144db 100644 (file)
@@ -6,8 +6,10 @@ use parent 'SL::Controller::Helper::GetModels::Base';
 use Carp;
 use List::MoreUtils qw(uniq);
 
+use Data::Dumper;
+
 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 +39,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 +67,34 @@ 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} };
+
+  if (!$sort_spec) {
+    no warnings 'once';
+    $::lxdebug->show_backtrace(1);
+    die "Unknown sort spec '$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 +119,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 +131,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) ]
 }
@@ -142,28 +151,20 @@ of sorting lists of database models in a controller
 
 In a controller:
 
-  use SL::Controller::Helper::GetModels;
-  use SL::Controller::Helper::Sorted;
-
-  __PACKAGE__->make_sorted(
-    DEFAULT_BY   => 'run_at',
-    DEFAULT_DIR  => 1,
-    MODEL        => 'BackgroundJobHistory',
-    ONLY         => [ qw(list) ],
-
-    error        => $::locale->text('Error'),
-    package_name => $::locale->text('Package name'),
-    run_at       => $::locale->text('Run at'),
+  SL::Controller::Helper::GetModels->new(
+    ...
+    sorted => {
+      _default => {
+        by  => 'run_at',
+        dir => 1,
+      },
+      error        => $::locale->text('Error'),
+      package_name => $::locale->text('Package name'),
+      run_at       => $::locale->text('Run at'),
+    },
   );
 
-  sub action_list {
-    my ($self) = @_;
-
-    my $sorted_models = $self->get_models;
-    $self->render('controller/list', ENTRIES => $sorted_models);
-  }
-
-In said template:
+In template:
 
   [% USE L %]
 
@@ -185,95 +186,44 @@ In said template:
 
 =head1 OVERVIEW
 
-This specialized helper module enables controllers to display a
+This C<GetModels> plugin 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
-compile time.
-
-The underlying functionality that enables the use of more than just
-the sort helper is provided by the controller helper C<GetModels>. It
-provides mechanisms for helpers like this one to hook into certain
-calls made by the controller (C<get_callback> and C<get_models>) so
-that the specialized helpers can inject their parameters into the
-calls to e.g. C<SL::DB::Manager::SomeModel::get_all>.
+indexes are eligible for sorting etc. through it's configuration of
+C<GetModels>.
 
-A template on the other hand can use the method
-C<sortable_table_header> from the layout helper module C<L>.
+A template can then use the method C<sortable_table_header> from the layout
+helper module C<L>.
 
-This module requires that the Rose model managers use their C<Sorted>
-helper.
+This module requires that the Rose model managers use their
+C<SL::DB::Helper::Sorted> helper.
 
-The C<Sorted> helper hooks into the controller call to the action via
-a C<run_before> hook. This is done so that it can remember the sort
-parameters that were used in the current view.
-
-=head1 PACKAGE FUNCTIONS
+=head1 OPTIONS
 
 =over 4
 
-=item C<make_sorted %sort_spec>
-
-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
-magic.
-
-There are two sorts of keys in the hash C<%sort_spec>. The first kind
-is written in all upper-case. Those parameters are control
-parameters. The second kind are all lower-case and represent indexes
-that can be used for sorting (similar to database column names). The
-second kind are also the indexes you use in a template when calling
-C<[% L.sorted_table_header(...) %]>.
-
-Control parameters include the following:
-
-=over 4
+=item * C<_default HASHREF>
 
-=item * C<MODEL>
+Optional. If it exists, it is expected to contain the keys C<by> and C<dir> and
+will be used to set the default sorting if nothing is found in C<source>.
 
-Optional. A string: the name of the Rose database model that is used
-as a default in certain cases. If this parameter is missing then it is
-derived from the controller's package (e.g. for the controller
-C<SL::Controller::BackgroundJobHistory> the C<MODEL> would default to
-C<BackgroundJobHistory>).
+Defaults to the underlying database model's default.
 
-=item * C<DEFAULT_BY>
-
-Optional. A string: the index to sort by if the user hasn't clicked on
-any column yet (meaning: if the C<$::form> parameters for sorting do
-not contain a valid index).
-
-Defaults to the underlying database model's default sort column name.
-
-=item * C<DEFAULT_DIR>
-
-Optional. Default sort direction (ascending for trueish values,
-descrending for falsish values).
-
-Defaults to the underlying database model's default sort direction.
-
-=item * C<FORM_PARAMS>
+=item * C<form_params>
 
 Optional. An array reference with exactly two strings that name the
-indexes in C<$::form> in which the sort index (the first element in
+indexes in C<source> in which the sort index (the first element in
 the array) and sort direction (the second element in the array) are
 stored.
 
 Defaults to the values C<sort_by> and C<sort_dir> if missing.
 
-=item * C<ONLY>
-
-Optional. An array reference containing a list of action names for
-which the sort parameters should be saved. If missing or empty then
-all actions invoked on the controller are monitored.
-
 =back
 
-All keys that are written in all lower-case name indexes that can be
-used for sorting. Each value to such a key can be either a string or a
-hash reference containing certain elements. If the value is only a
-string then such a hash reference is constructed, and the string is
+All other keys can be used for sorting. Each value to such a key can be either
+a string or a hash reference containing certain elements. If the value is only
+a string then such a hash reference is constructed, and the string is
 used as the value for the C<title> key.
 
 These possible elements are:
@@ -306,11 +256,9 @@ reference is the value.
 
 =back
 
-=back
-
 =head1 INSTANCE FUNCTIONS
 
-These functions are called on a controller instance.
+These functions are called on a C<GetModels> instance and delegating to this plugin.
 
 =over 4
 
@@ -319,15 +267,15 @@ These functions are called on a controller instance.
 Returns a hash containing the currently active sort parameters.
 
 The key C<by> contains the active sort index referring to the
-C<%sort_spec> given to L<make_sorted>.
+C<%sort_spec> given by the configuration.
 
 The key C<dir> is either C<1> or C<0>.
 
 =item C<get_current_sort_params>
 
-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).
+Returns a hash reference to the sort spec structure given in the configuration
+after normalization (hash reference construction, applying default parameters
+etc).
 
 =item C<set_report_generator_sort_options %params>
 
@@ -341,7 +289,7 @@ L<SL::ReportGenerator>:
 =item 2. it sets the the links for those column headers that are
 sortable and
 
-=item 3. it adds the C<FORM_PARAMS> fields to the list of variables in
+=item 3. it adds the C<form_params> fields to the list of variables in
 the report generator's export options.
 
 =back
@@ -364,4 +312,6 @@ Nothing here yet.
 
 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
 
+Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
+
 =cut