X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;ds=sidebyside;f=SL%2FController%2FHelper%2FGetModels%2FPaginated.pm;h=ad5e10c147c6757016994c0f321b09e8504d118e;hb=32f83e8e1c88c04cd9a8686d8482b1473a9d73c5;hp=a40acaef975e3615371e36a3f8cd3665e15270a3;hpb=ec3a4636c1d58339915614120cd82759150d7641;p=kivitendo-erp.git diff --git a/SL/Controller/Helper/GetModels/Paginated.pm b/SL/Controller/Helper/GetModels/Paginated.pm index a40acaef9..ad5e10c14 100644 --- a/SL/Controller/Helper/GetModels/Paginated.pm +++ b/SL/Controller/Helper/GetModels/Paginated.pm @@ -6,7 +6,7 @@ use parent 'SL::Controller::Helper::GetModels::Base'; use List::Util qw(min); use Rose::Object::MakeMethods::Generic ( - scalar => [ qw(disabled per_page) ], + scalar => [ qw(per_page form_data paginated_args calculated_params) ], 'scalar --get_set_init' => [ qw(form_params paginate_args) ], ); @@ -20,46 +20,20 @@ sub init { $self->get_models->register_handlers( callback => sub { shift; $self->_callback_handler_for_paginated(@_) }, - get_models => sub { shift; $self->_get_models_handler_for_paginated(@_) }, ); # $::lxdebug->dump(0, "CONSPEC", \%specs); } -sub get_current_paginate_params { - my ($self, %args) = @_; - return () unless $self->is_enabled; - - my %paginate_params = $self->final_params(%args); - - # try to use Filtered if available and nothing else is configured, but don't - # blow up if the controller does not use Filtered - my %paginate_args = ref($self->paginate_args) eq 'CODE' ? %{ $self->paginate_args->($self) } - : $self->paginate_args eq '__FILTER__' - && $self->get_models->filtered ? %{ $self->get_models->filtered->get_current_filter_params } - : $self->paginate_args ne '__FILTER__' ? do { my $sub = $self->paginate_args; %{ $self->get_models->controller->$sub() } } - : (); - - %args = $self->merge_args(\%args, \%paginate_args); - - my $calculated_params = $self->get_models->manager->paginate(%paginate_params, args => \%args); - - # $::lxdebug->dump(0, "get_current_paginate_params: ", $calculated_params); - - return %{ $calculated_params }; -} - -sub disable_pagination { - my ($self) = @_; - $self->disabled(1); -} - -sub final_params { +sub read_params { my ($self, %params) = @_; + return %{ $self->form_data } if $self->form_data; + my $source = $self->get_models->source; + my $from_form = { - page => $::form->{ $self->form_params->[0] } || 1, - per_page => $::form->{ $self->form_params->[1] } * 1, + page => $source->{ $self->form_params->[0] } || 1, + per_page => $source->{ $self->form_params->[1] } * 1, }; # my $priv = _priv($self); @@ -69,25 +43,50 @@ sub final_params { $params{page} = ($params{page} * 1) || 1; $params{per_page} = ($params{per_page} * 1) || $self->per_page; + $self->form_data(\%params); + %params; } -# -# private functions -# +sub finalize { + my ($self, %args) = @_; -sub init_form_params { - [ qw(page per_page) ] + if ($self->is_enabled) { + my %paginate_params = $self->read_params; + + # try to use Filtered if available and nothing else is configured, but don't + # blow up if the controller does not use Filtered + my %paginate_args = ref($self->paginate_args) eq 'CODE' ? %{ $self->paginate_args->($self) } + : $self->paginate_args eq '__FILTER__' + && $self->get_models->filtered ? $self->get_models->filtered->read_params + : $self->paginate_args ne '__FILTER__' ? do { my $sub = $self->paginate_args; %{ $self->get_models->controller->$sub() } } + : (); + + %args = $self->merge_args(\%args, \%paginate_args); + + my $calculated_params = $self->get_models->manager->paginate(%paginate_params, args => \%args); + + $self->calculated_params($calculated_params); + } + + $self->paginated_args(\%args); + + return %args; } -sub init_paginate_args { - '__FILTER__' +sub get_current_paginate_params { + my ($self, %args) = @_; + return () unless $self->is_enabled; + %{ $self->calculated_params }; } +# +# private functions +# + sub _callback_handler_for_paginated { my ($self, %params) = @_; - my %form_params = $self->final_params; -# my $priv = _priv($self); + my %form_params = $self->read_params; if ($self->is_enabled && $form_params{page}) { $params{ $self->form_params->[0] } = $form_params{page}; @@ -99,19 +98,12 @@ sub _callback_handler_for_paginated { return %params; } -sub _get_models_handler_for_paginated { - my ($self, %params) = @_; - - $self->get_models->manager->paginate($self->final_params, args => \%params) if $self->is_enabled; - - # $::lxdebug->dump(0, "GM handler for paginated; params nach modif (is_enabled? " . _is_enabled($self) . ")", \%params); - - return %params; +sub init_form_params { + [ qw(page per_page) ] } -sub is_enabled { - my ($self) = @_; - return !$self->disabled; +sub init_paginate_args { + '__FILTER__' } 1; @@ -130,59 +122,30 @@ of paginating lists of database models in a controller In a controller: - use SL::Controller::Helper::GetModels; - use SL::Controller::Helper::Paginated; - - __PACKAGE__->make_paginated( - MODEL => 'BackgroundJobHistory', - ONLY => [ qw(list) ], - FORM_PARAMS => [ qw(page per_page) ], + SL::Controller::Helper::GetModels->new( + .. + paginated => { + form_params => [ qw(page per_page) ], + per_page => 20, + } ); - sub action_list { - my ($self) = @_; - - my $paginated_models = $self->get_models; - $self->render('controller/list', ENTRIES => $paginated_models); - } - In said template: - [% USE L %] - -