]> wagnertech.de Git - mfinanz.git/blobdiff - SL/Controller/Helper/GetModels.pm
Erste Version GetModels rewrite
[mfinanz.git] / SL / Controller / Helper / GetModels.pm
index 00ef31017d8db686066db7b9689f0bdc2b89d2da..ae9810b5c55d965cdd4f6f360fb3440ba4d2b371 100644 (file)
@@ -2,42 +2,98 @@ package SL::Controller::Helper::GetModels;
 
 use strict;
 
-use Exporter qw(import);
-our @EXPORT = qw(get_callback get_models);
+use parent 'Rose::Object';
+use SL::Controller::Helper::GetModels::Filtered;
+use SL::Controller::Helper::GetModels::Sorted;
+use SL::Controller::Helper::GetModels::Paginated;
+
+use Rose::Object::MakeMethods::Generic (
+  scalar => [ qw(controller model query with_objects filtered sorted paginated) ],
+  'scalar --get_set_init' => [ qw(handlers) ],
+);
 
 use constant PRIV => '__getmodelshelperpriv';
 
-my %registered_handlers = ( callback => [], get_models => [] );
+#my $registered_handlers = {};
+
+sub init {
+  my ($self, %params) = @_;
+
+#  for my $plugin (qw(filtered sorted paginated)) {
+#    next unless $params{$plugin};
+#    $self->${ \"make_$plugin" }(%{ delete $params{$plugin} || {} });
+#  }
+#
+  # TODO: default model
+  $self->model(delete $params{model});
 
-sub register_get_models_handlers {
-  my ($class, %additional_handlers) = @_;
+  for my $plugin (qw(filtered sorted paginated)) {
+    next unless my $spec = delete $params{$plugin} // {};
+    my $plugin_class = "SL::Controller::Helper::GetModels::" . ucfirst $plugin;
+    $self->$plugin($plugin_class->new(%$spec, get_models => $self));
+  }
+
+  $self->SUPER::init(%params);
+}
 
-  my $only        = delete($additional_handlers{ONLY}) || [];
-  $only           = [ $only ] if !ref $only;
-  my %hook_params = @{ $only } ? ( only => $only ) : ();
+sub register_handlers {
+  my ($self, %additional_handlers) = @_;
 
-  $class->run_before(sub { $_[0]->{PRIV()} = { current_action => $_[1] }; }, %hook_params);
+#  my $only        = delete($additional_handlers{ONLY}) || [];
+#  $only           = [ $only ] if !ref $only;
+#  my %hook_params = @{ $only } ? ( only => $only ) : ();
 
-  map { push @{ $registered_handlers{$_} }, $additional_handlers{$_} if $additional_handlers{$_} } keys %registered_handlers;
+  my $handlers    = $self->handlers;
+  map { push @{ $handlers->{$_} }, $additional_handlers{$_} if $additional_handlers{$_} } keys %$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($class)->{callback} }, $callback;
 }
 
 sub get_callback {
   my ($self, %override_params) = @_;
 
-  my %default_params = _run_handlers($self, 'callback', action => ($self->{PRIV()} || {})->{current_action});
+  my %default_params = $self->_run_handlers('callback', action => $self->controller->action_name);
 
-  return $self->url_for(%default_params, %override_params);
+  return $self->controller->url_for(%default_params, %override_params);
 }
 
-sub get_models {
-  my ($self, %override_params) = @_;
+sub get {
+  my ($self, %params) = @_;
 
-  my %default_params           = _run_handlers($self, 'get_models');
+  push @{ $params{query}        ||= [] }, @{ $self->query || [] };
+  push @{ $params{with_objects} ||= [] }, @{ $self->with_objects || [] };
 
-  my %params                   = (%default_params, %override_params);
-  my $model                    = delete($params{model}) || die "No 'model' to work on";
+  %params                      = $self->_run_handlers('get_models', %params);
 
-  return "SL::DB::Manager::${model}"->get_all(%params);
+  return $self->manager->get_all(%params);
+}
+
+sub get_paginate_args {
+  my ($self, %params) = @_;
+
+  push @{ $params{query}        ||= [] }, @{ $self->query || [] };
+  push @{ $params{with_objects} ||= [] }, @{ $self->with_objects || [] };
+
+  $self->paginated->get_current_paginate_params(%params);
+}
+
+sub manager {
+  die "No 'model' to work on" unless $_[0]->model;
+  "SL::DB::Manager::" . $_[0]->model;
 }
 
 #
@@ -47,7 +103,7 @@ sub get_models {
 sub _run_handlers {
   my ($self, $handler_type, %params) = @_;
 
-  foreach my $sub (@{ $registered_handlers{$handler_type} }) {
+  foreach my $sub (@{ $self->handlers->{$handler_type} }) {
     if (ref $sub eq 'CODE') {
       %params = $sub->($self, %params);
     } elsif ($self->can($sub)) {
@@ -60,6 +116,13 @@ sub _run_handlers {
   return %params;
 }
 
+sub init_handlers {
+  {
+    callback => [],
+    get_models => [],
+  }
+}
+
 1;
 __END__
 
@@ -102,6 +165,19 @@ in L<get_callback>.
 
 =over 4
 
+=item C<get_models_url_params $class, $sub>
+
+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<register_get_models_handlers $class, %handlers>
 
 This function should only be called from other controller helpers like