]> wagnertech.de Git - kivitendo-erp.git/blobdiff - SL/Controller/Helper/GetModels.pm
Erste Version GetModels rewrite
[kivitendo-erp.git] / SL / Controller / Helper / GetModels.pm
index 821d7a19424997516397b6af39c3b11023067b82..ae9810b5c55d965cdd4f6f360fb3440ba4d2b371 100644 (file)
@@ -2,21 +2,48 @@ package SL::Controller::Helper::GetModels;
 
 use strict;
 
-use Exporter qw(import);
-our @EXPORT = qw(get_models_url_params 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 = {};
+#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});
+
+  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);
+}
 
-sub register_get_models_handlers {
-  my ($class, %additional_handlers) = @_;
+sub register_handlers {
+  my ($self, %additional_handlers) = @_;
 
-  my $only        = delete($additional_handlers{ONLY}) || [];
-  $only           = [ $only ] if !ref $only;
-  my %hook_params = @{ $only } ? ( only => $only ) : ();
+#  my $only        = delete($additional_handlers{ONLY}) || [];
+#  $only           = [ $only ] if !ref $only;
+#  my %hook_params = @{ $only } ? ( only => $only ) : ();
 
-  my $handlers    = _registered_handlers($class);
+  my $handlers    = $self->handlers;
   map { push @{ $handlers->{$_} }, $additional_handlers{$_} if $additional_handlers{$_} } keys %$handlers;
 }
 
@@ -39,19 +66,34 @@ sub get_models_url_params {
 sub get_callback {
   my ($self, %override_params) = @_;
 
-  my %default_params = _run_handlers($self, 'callback', action => $self->action_name);
+  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) = @_;
+
+  push @{ $params{query}        ||= [] }, @{ $self->query || [] };
+  push @{ $params{with_objects} ||= [] }, @{ $self->with_objects || [] };
+
+  %params                      = $self->_run_handlers('get_models', %params);
+
+  return $self->manager->get_all(%params);
+}
+
+sub get_paginate_args {
+  my ($self, %params) = @_;
 
-  my %params                   = _run_handlers($self, 'get_models', %override_params);
+  push @{ $params{query}        ||= [] }, @{ $self->query || [] };
+  push @{ $params{with_objects} ||= [] }, @{ $self->with_objects || [] };
 
-  my $model                    = delete($params{model}) || die "No 'model' to work on";
+  $self->paginated->get_current_paginate_params(%params);
+}
 
-  return "SL::DB::Manager::${model}"->get_all(%params);
+sub manager {
+  die "No 'model' to work on" unless $_[0]->model;
+  "SL::DB::Manager::" . $_[0]->model;
 }
 
 #
@@ -61,7 +103,7 @@ sub get_models {
 sub _run_handlers {
   my ($self, $handler_type, %params) = @_;
 
-  foreach my $sub (@{ _registered_handlers(ref $self)->{$handler_type} }) {
+  foreach my $sub (@{ $self->handlers->{$handler_type} }) {
     if (ref $sub eq 'CODE') {
       %params = $sub->($self, %params);
     } elsif ($self->can($sub)) {
@@ -74,8 +116,11 @@ sub _run_handlers {
   return %params;
 }
 
-sub _registered_handlers {
-  $registered_handlers->{$_[0]} //= { callback => [], get_models => [] }
+sub init_handlers {
+  {
+    callback => [],
+    get_models => [],
+  }
 }
 
 1;