X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/cf32b30bf5fd61c825a62c444302365cef96a03f..9deadd1d1a37ce24b807132c00f4890785400683:/SL/Controller/Helper/GetModels.pm diff --git a/SL/Controller/Helper/GetModels.pm b/SL/Controller/Helper/GetModels.pm new file mode 100644 index 000000000..4658717f6 --- /dev/null +++ b/SL/Controller/Helper/GetModels.pm @@ -0,0 +1,166 @@ +package SL::Controller::Helper::GetModels; + +use strict; + +use Exporter qw(import); +our @EXPORT = qw(get_callback get_models); + +my $current_action; +my %registered_handlers = ( callback => [], get_models => [] ); + +sub register_get_models_handlers { + my ($class, %additional_handlers) = @_; + + my $only = delete($additional_handlers{ONLY}) || []; + $only = [ $only ] if !ref $only; + my %hook_params = @{ $only } ? ( only => $only ) : (); + + $class->run_before(sub { $current_action = $_[1]; }, %hook_params); + + map { push @{ $registered_handlers{$_} }, $additional_handlers{$_} if $additional_handlers{$_} } keys %registered_handlers; +} + +sub get_callback { + my ($self, %override_params) = @_; + + my %default_params = _run_handlers($self, 'callback', action => $current_action); + + return $self->url_for(%default_params, %override_params); +} + +sub get_models { + my ($self, %override_params) = @_; + + my %default_params = _run_handlers($self, 'get_models'); + + my %params = (%default_params, %override_params); + my $model = delete($params{model}) || die "No 'model' to work on"; + + return "SL::DB::Manager::${model}"->get_all(%params); +} + +# +# private/internal functions +# + +sub _run_handlers { + my ($self, $handler_type, %params) = @_; + + foreach my $sub (@{ $registered_handlers{$handler_type} }) { + if (ref $sub eq 'CODE') { + %params = $sub->($self, %params); + } elsif ($self->can($sub)) { + %params = $self->$sub(%params); + } else { + die "SL::Controller::Helper::GetModels::get_callback: Cannot call $sub on " . ref($self) . ")"; + } + } + + return %params; +} + +1; +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::Controller::Helper::GetModels - Base mixin for controller helpers +dealing with semi-automatic handling of sorting and paginating lists + +=head1 SYNOPSIS + +For a proper synopsis see L. + +=head1 OVERVIEW + +For a generic overview see L. + +This base module is the interface between a controller and specialized +helper modules that handle things like sorting and paginating. The +specialized helpers register themselves with this module via a call to +L during compilation time (e.g. in the +case of C this happens when the controller calls +L). + +A controller will later usually call the L +function. Templates will call the L function. Both +functions run the registered handlers handing over control to the +specialized helpers so that they may inject their parameters into the +call chain. + +The C helper hooks into the controller call to the action +via a C hook. This is done so that it can remember the +action called by the user. This is used for constructing the callback +in L. + +=head1 PACKAGE FUNCTIONS + +=over 4 + +=item C + +This function should only be called from other controller helpers like +C or C. It is not exported and must therefore be +called its full name. The first parameter C<$class> must be the actual +controller's class name. + +If C<%handlers> contains a key C then it is passed to the hook +registration in L. + +The C<%handlers> register callback functions in the specialized +controller helpers that are called during invocation of +L or L. Possible keys are C and +C. + +Each handler (the value in the hash) can be either a code reference +(in which case it is called directly) or the name of an instance +function callable on a controller instance. In both cases the handler +receives a hash of parameters built during this very call to +L or L respectively. The handler's return +value must be the new hash to be used in calls to further handlers and +to the actual database model functions later on. + +=back + +=head1 INSTANCE FUNCTIONS + +=over 4 + +=item C + +Return an URL suitable for use as a callback parameter. It maps to the +current controller and action. All registered handlers of type +'callback' (e.g. the ones by C and C) can inject +the parameters they need so that the same list view as is currently +visible can be re-rendered. + +Optional C<%params> passed to this function may override any parameter +set by the registered handlers. + +=item C + +Query the model manager via C and return its result. The +parameters to C are constructed by calling all registered +handlers of type 'models' (e.g. the ones by C and +C). + +Optional C<%params> passed to this function may override any parameter +set by the registered handlers. + +The return value is the an array reference of C models. + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut