GetModels Filtered: manuelles Setzen von launder_to entfernt
[kivitendo-erp.git] / SL / Controller / Helper / GetModels / Filtered.pm
index c89d44d..849c0e0 100644 (file)
@@ -8,8 +8,8 @@ use SL::Controller::Helper::ParseFilter ();
 use List::MoreUtils qw(uniq);
 
 use Rose::Object::MakeMethods::Generic (
-  scalar => [ qw(filter_args filter_params orig_filter) ],
-  'scalar --get_set_init' => [ qw(form_params launder_to) ],
+  scalar => [ qw(filter_args filter_params orig_filter filter no_launder) ],
+  'scalar --get_set_init' => [ qw(form_params laundered) ],
 );
 
 sub init {
@@ -20,7 +20,6 @@ sub init {
 
   $self->get_models->register_handlers(
     callback   => sub { shift; $self->_callback_handler_for_filtered(@_) },
-    get_models => sub { shift; $self->_get_models_handler_for_filtered(@_) },
   );
 
   # $::lxdebug->dump(0, "CONSPEC", \%specs);
@@ -32,7 +31,7 @@ sub read_params {
   return %{ $self->filter_params } if $self->filter_params;
   my $source = $self->get_models->source;
 
-  my $filter            = $params{filter} // $source->{ $self->form_params } // {};
+  my $filter            = $self->filter // $source->{ $self->form_params } // {};
   $self->orig_filter($filter);
 
   my %filter_args       = $self->_get_filter_args;
@@ -40,24 +39,25 @@ sub read_params {
     class        => $self->get_models->manager,
     with_objects => $params{with_objects},
   );
-  my $laundered;
-  if ($self->launder_to eq '__INPLACE__') {
-    # nothing to do
-  } elsif ($self->launder_to) {
-    $laundered = {};
-    $parse_filter_args{launder_to} = $laundered;
+
+  # Store laundered result in $self->laundered.
+
+  if (!$self->no_launder) {
+    $self->laundered({});
+    $parse_filter_args{launder_to} = $self->laundered;
   } else {
+    $self->laundered(undef);
     $parse_filter_args{no_launder} = 1;
   }
 
   my %calculated_params = SL::Controller::Helper::ParseFilter::parse_filter($filter, %parse_filter_args);
   %calculated_params = $self->merge_args(\%calculated_params, \%filter_args, \%params);
 
-  if ($laundered) {
-    if ($self->get_models->controller->can($self->launder_to)) {
-      $self->get_models->controller->${\ $self->launder_to }($laundered);
+  if ($self->laundered) {
+    if ($self->get_models->controller->can('filter')) {
+      $self->get_models->controller->filter($self->laundered);
     } else {
-      $self->get_models->controller->{$self->launder_to} = $laundered;
+      $self->get_models->controller->{filter} = $self->laundered;
     }
   }
 
@@ -70,7 +70,13 @@ sub read_params {
 
 sub finalize {
   my ($self, %params) = @_;
-  %params;
+
+  my %filter_params;
+  %filter_params = $self->read_params(%params)  if $self->is_enabled;
+
+  # $::lxdebug->dump(0, "GM handler for filtered; params nach modif (is_enabled? " . $self->is_enabled . ")", \%params);
+
+  return $self->merge_args(\%params, \%filter_params);
 }
 
 #
@@ -98,27 +104,16 @@ sub _callback_handler_for_filtered {
   return %params;
 }
 
-sub _get_models_handler_for_filtered {
-  my ($self, %params)    = @_;
-
-  # $::lxdebug->dump(0,  "params in get_models_for_filtered", \%params);
-
-  my %filter_params;
-  %filter_params = $self->read_params(%params)  if $self->is_enabled;
-
-  # $::lxdebug->dump(0, "GM handler for filtered; params nach modif (is_enabled? " . $self->is_enabled . ")", \%params);
-
-  return $self->merge_args(\%params, \%filter_params);
-}
-
 sub init_form_params {
   'filter'
 }
 
-sub init_launder_to {
-  'filter'
-}
+sub init_laundered {
+  my ($self) = @_;
 
+  $self->get_models->finalize;
+  return $self->{laundered};
+}
 
 1;
 
@@ -130,126 +125,81 @@ __END__
 
 =head1 NAME
 
-SL::Controller::Helper::Filtered - A helper for semi-automatic handling
-of filtered lists of database models in a controller
+SL::Controller::Helper::GetModels::Filtered - Filter handling plugin for GetModels
 
 =head1 SYNOPSIS
 
 In a controller:
 
-  use SL::Controller::Helper::GetModels;
-  use SL::Controller::Helper::Filtered;
-
-  __PACKAGE__->make_filter(
-    MODEL       => 'Part',
-    ONLY        => [ qw(list) ],
-    FORM_PARAMS => [ qw(filter) ],
-  );
-
-  sub action_list {
-    my ($self) = @_;
+  SL::Controller::Helper::GetModels->new(
+    ...
+    filtered => {
+      filter      => HASHREF,
+      no_launder  => 0 | 1,
+    }
 
-    my $filtered_models = $self->get_models(%addition_filters);
-    $self->render('controller/list', ENTRIES => $filtered_models);
-  }
+    OR
 
+    filtered => 0,
+  );
 
 =head1 OVERVIEW
 
-This helper module enables use of the L<SL::Controller::Helper::ParseFilter>
-methods in conjunction with the L<SL::Controller::Helper::GetModels> style of
-plugins. Additional filters can be defined in the database models and filtering
-can be reduced to a minimum of work.
-
-This plugin can be combined with L<SL::Controller::Sorted> and
-L<SL::Controller::Paginated> for filtered, sorted and paginated lists.
-
-The controller has to provive information where to look for filter information
-at compile time. This call is L<make_filtered>.
+This C<GetModels> plugin enables use of the
+L<SL::Controller::Helper::ParseFilter> methods. Additional filters can be
+defined in the database models and filtering can be reduced to a minimum of
+work.
 
 The underlying functionality that enables the use of more than just
 the paginate helper is provided by the controller helper
-C<GetModels>. See the documentation for L<SL::Controller::Sorted> for
+C<GetModels>. See the documentation for L<SL::Controller::Helper::GetModels> for
 more information on it.
 
-=head1 PACKAGE FUNCTIONS
+=head1 OPTIONS
 
 =over 4
 
-=item C<make_filtered %filter_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.
+=item * C<filter>
 
-Careful: If you want to use this in conjunction with
-L<SL:Controller::Helper::Paginated>, you need to call C<make_filtered> first,
-or the paginating will not get all the relevant information to estimate the
-number of pages correctly. To ensure this does not happen, this module will
-croak when it detects such a scenario.
+Optional. Indicates a key in C<source> to be used as filter.
 
-The hash C<%filter_spec> can include the following parameters:
-
-=over 4
+Defaults to the value C<filter> if missing.
 
-=item * C<MODEL>
+=item * C<no_launder>
 
-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>).
+Optional. If given and trueish then laundering is disabled.
 
-=item * C<FORM_PARAMS>
+=back
 
-Optional. Indicates a key in C<$::form> to be used as filter.
+=head1 FILTER FORMAT
 
-Defaults to the values C<filter> if missing.
+See L<SL::Controller::Helper::ParseFilter> for a description of the filter format.
 
-=item * C<LAUNDER_TO>
+=head1 CUSTOM FILTERS
 
-Option. Indicates a target for laundered filter arguments in the controller.
-Can be set to C<undef> to disable laundering, and can be set to method named or
-hash keys of the controller. In the latter case the laundered structure will be
-put there.
+C<Filtered> will honor custom filters defined in RDBO managers. See
+L<SL::DB::Helper::Filtered> for an explanation fo those.
 
-Defaults to inplace laundering which is not normally settable.
+=head1 FUNCTIONS
 
-=item * C<ONLY>
+=over 4
 
-Optional. An array reference containing a list of action names for
-which the paginate parameters should be saved. If missing or empty then
-all actions invoked on the controller are monitored.
+=item C<laundered>
 
-=back
+Finalizes the object (which causes laundering of the filter structure)
+and returns a hashref of the laundered filter. If the plugin is
+configured not to launder then C<undef> will be returned.
 
 =back
 
-=head1 INSTANCE FUNCTIONS
-
-These functions are called on a controller instance.
+=head1 BUGS
 
 =over 4
 
-=item C<get_current_filter_params>
-
-Returns a hash to be used in manager C<get_all> calls or to be passed on to
-GetModels. Will only work if the get_models chain has been called at least
-once, because only then the full parameters can get parsed and stored. Will
-croak otherwise.
-
-=item C<disable_filtering>
-
-Disable filtering for the duration of the current action. Can be used
-when using the attribute C<ONLY> to L<make_filtered> does not
-cover all cases.
+=item * There is currently no easy way to filter for CVars.
 
 =back
 
-=head1 BUGS
-
-Nothing here yet.
-
 =head1 AUTHOR
 
 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>