X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FHelper%2FGetModels.pm;h=e353657170a44bffdc7ea7940475027cd6627cad;hb=d14f72e7f3ce339b7565e27fcb169181ff90d64b;hp=4658717f6bf4e4946c07adc2810ba3d75d294fde;hpb=9deadd1d1a37ce24b807132c00f4890785400683;p=kivitendo-erp.git diff --git a/SL/Controller/Helper/GetModels.pm b/SL/Controller/Helper/GetModels.pm index 4658717f6..e35365717 100644 --- a/SL/Controller/Helper/GetModels.pm +++ b/SL/Controller/Helper/GetModels.pm @@ -3,9 +3,10 @@ package SL::Controller::Helper::GetModels; use strict; use Exporter qw(import); -our @EXPORT = qw(get_callback get_models); +our @EXPORT = qw(get_models_url_params get_callback get_models); + +use constant PRIV => '__getmodelshelperpriv'; -my $current_action; my %registered_handlers = ( callback => [], get_models => [] ); sub register_get_models_handlers { @@ -15,15 +16,31 @@ sub register_get_models_handlers { $only = [ $only ] if !ref $only; my %hook_params = @{ $only } ? ( only => $only ) : (); - $class->run_before(sub { $current_action = $_[1]; }, %hook_params); + $class->run_before(sub { $_[0]->{PRIV()} = { current_action => $_[1] }; }, %hook_params); map { push @{ $registered_handlers{$_} }, $additional_handlers{$_} if $additional_handlers{$_} } keys %registered_handlers; } +sub get_models_url_params { + my ($class, $sub_name_or_code) = @_; + + my $code = (ref($sub_name_or_code) || '') eq 'CODE' ? $sub_name_or_code : sub { shift->$sub_name_or_code(@_) }; + my $callback = sub { + my ($self, %params) = @_; + my @additional_params = $code->($self); + return ( + %params, + (scalar(@additional_params) == 1) && (ref($additional_params[0]) eq 'HASH') ? %{ $additional_params[0] } : @additional_params, + ); + }; + + push @{ $registered_handlers{callback} }, $callback; +} + sub get_callback { my ($self, %override_params) = @_; - my %default_params = _run_handlers($self, 'callback', action => $current_action); + my %default_params = _run_handlers($self, 'callback', action => ($self->{PRIV()} || {})->{current_action}); return $self->url_for(%default_params, %override_params); } @@ -101,6 +118,19 @@ in L. =over 4 +=item C + +Register one of the controller's subs to be called whenever an URL has +to be generated (e.g. for sort and pagination links). This is a way +for the controller to add additional parameters to the URL (e.g. for +filter parameters). + +The C<$sub> parameter can be either a code reference or the name of +one of the controller's functions. + +The value returned by this C<$sub> must be either a single hash +reference or a hash of key/value pairs to add to the URL. + =item C This function should only be called from other controller helpers like