GetModels: Disablen von Plugins auch für undef
[kivitendo-erp.git] / SL / Controller / Helper / GetModels.pm
index 481f1ee..cb73ab3 100644 (file)
@@ -11,7 +11,7 @@ use Scalar::Util qw(weaken);
 
 use Rose::Object::MakeMethods::Generic (
   scalar => [ qw(controller model query with_objects filtered sorted paginated finalized final_params) ],
-  'scalar --get_set_init' => [ qw(handlers source) ],
+  'scalar --get_set_init' => [ qw(handlers source additional_url_params) ],
   array => [ qw(plugins) ],
 );
 
@@ -27,6 +27,13 @@ sub get {
   return $self->manager->get_all(%params);
 }
 
+sub count {
+  my ($self) = @_;
+  my %params = $self->finalize;
+
+  return $self->manager->get_all_count(%params);
+}
+
 sub disable_plugin {
   my ($self, $plugin) = @_;
   die 'cannot change internal state after finalize was called' if $self->finalized;
@@ -88,7 +95,9 @@ sub init {
 
   my @plugins;
   for my $plugin (qw(filtered sorted paginated)) {
-    next unless my $spec = delete $params{$plugin} // {};
+    next if exists($params{$plugin}) && !$params{$plugin};
+
+    my $spec         = delete $params{$plugin} // {};
     my $plugin_class = "SL::Controller::Helper::GetModels::" . ucfirst $plugin;
     push @plugins, $self->$plugin($plugin_class->new(%$spec, get_models => $self));
   }
@@ -106,6 +115,8 @@ sub finalize {
 
   return %{ $self->final_params } if $self->finalized;
 
+  $self->register_handlers(callback => sub { shift; (@_, %{ $self->additional_url_params }) }) if %{ $self->additional_url_params };
+
   push @{ $params{query}        ||= [] }, @{ $self->query || [] };
   push @{ $params{with_objects} ||= [] }, @{ $self->with_objects || [] };
 
@@ -124,6 +135,14 @@ sub register_handlers {
   map { push @{ $handlers->{$_} }, $additional_handlers{$_} if $additional_handlers{$_} } keys %$handlers;
 }
 
+sub add_additional_url_params {
+  my ($self, %params) = @_;
+
+  $self->additional_url_params({ %{ $self->additional_url_params }, %params });
+
+  return $self;
+}
+
 sub get_models_url_params {
   my ($self, $sub_name_or_code) = @_;
 
@@ -137,13 +156,19 @@ sub get_models_url_params {
     );
   };
 
-  $self->registere_handlers('callback' => $callback);
+  $self->register_handlers('callback' => $callback);
 }
 
-sub get_callback {
+sub get_callback_params {
   my ($self, %override_params) = @_;
 
   my %default_params = $self->_run_handlers('callback', action => $self->controller->action_name);
+}
+
+sub get_callback {
+  my ($self, %override_params) = @_;
+
+  my %default_params = $self->get_callback_params(%override_params);
 
   return $self->controller->url_for(%default_params, %override_params);
 }
@@ -183,6 +208,8 @@ sub init_source {
   $::form
 }
 
+sub init_additional_url_params { +{} }
+
 1;
 __END__
 
@@ -192,7 +219,7 @@ __END__
 
 =head1 NAME
 
-SL::Controller::Helper::GetModels - Base class for the GetModels system.
+SL::Controller::Helper::GetModels - Base class for the GetModels system.
 
 =head1 SYNOPSIS
 
@@ -209,7 +236,7 @@ In controller:
 =head1 OVERVIEW
 
 Building a CRUD controller would be easy, were it not for those stupid
-list actions. People unreasonable expect stuff like filtering, sorting,
+list actions. People unreasonably expect stuff like filtering, sorting,
 paginating, exporting etc simply to work. Well, lets try to make it simply work
 a little.
 
@@ -218,7 +245,7 @@ helper modules that handle these things (sorting, paginating etc) and gives you
 the means to retrieve the information when needed to display sort headers or
 paginating footers.
 
-Information about the requested data query can be stored into the object up to
+Information about the requested data query can be stored in the object up to
 a certain point, from which on the object becomes locked and can only be
 accessed for information. (See C<STATES>).
 
@@ -247,6 +274,13 @@ one of the controller's functions.
 The value returned by C<SUB> must be either a single hash
 reference or a hash of key/value pairs to add to the URL.
 
+=item add_additional_url_params C<%params>
+
+Sets additional parameters that will be added to each URL generated by
+this model (e.g. for pagination/sorting). This is just sugar for a
+proper call to L<get_models_url_params> with an anonymous sub adding
+those parameters.
+
 =item get_callback
 
 Returns a URL suitable for use as a callback parameter. It maps to the
@@ -276,7 +310,7 @@ Forces finalized state. Can be used on finalized objects without error.
 
 Note that most higher functions will call this themselves to force a finalized
 state. If you do use it it must come before any other finalizing methods, and
-will most likely function as a reminder or maintainers where your codes
+will most likely function as a reminder for maintainers where your code
 switches from configuration to finalized state.
 
 =item source HASHREF
@@ -338,7 +372,7 @@ This is the state after creating a new object.
 
 =item Init
 
-In this state every information needed from the source ($::form) has been read
+In this state all the information needed from the source ($::form) has been read
 and subsequent changes to the source have no effect. In the current
 implementation this will happen during creation, so that the return value of
 C<new> is already in state C<Init>.
@@ -375,7 +409,7 @@ is inferred from the name of the controller class.
 =item filtered PARAMS
 
 Configuration for plugins. If the option for any plugin is omitted, it defaults
-to enabled and configured by default. Giving a falsish value as first argument
+to enabled and is configured by default. Giving a falsish value as first argument
 will disable the plugin.
 
 If the value is a hashref, it will be passed to the plugin's C<init> method.