From: Moritz Bunkus Date: Wed, 5 Sep 2012 07:00:35 +0000 (+0200) Subject: Paginate-Controller-Helper: Paginaten innerhalb einer Action abstellen können X-Git-Tag: release-3.0.0beta1~249^2~13 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=5e45f456a6790d3ecdf358edf76b3679a5cacd05;p=kivitendo-erp.git Paginate-Controller-Helper: Paginaten innerhalb einer Action abstellen können --- diff --git a/SL/Controller/Helper/Paginated.pm b/SL/Controller/Helper/Paginated.pm index 5792f6336..d3da20dc0 100644 --- a/SL/Controller/Helper/Paginated.pm +++ b/SL/Controller/Helper/Paginated.pm @@ -3,7 +3,7 @@ package SL::Controller::Helper::Paginated; use strict; use Exporter qw(import); -our @EXPORT = qw(make_paginated get_paginate_spec get_current_paginate_params _save_current_paginate_params _get_models_handler_for_paginated _callback_handler_for_paginated); +our @EXPORT = qw(make_paginated get_paginate_spec get_current_paginate_params _save_current_paginate_params _get_models_handler_for_paginated _callback_handler_for_paginated disable_pagination); use constant PRIV => '__paginatedhelper_priv'; @@ -18,6 +18,7 @@ sub make_paginated { $specs{FORM_PARAMS} ||= [ qw(page per_page) ]; $specs{ONLY} ||= []; $specs{ONLY} = [ $specs{ONLY} ] if !ref $specs{ONLY}; + $specs{ONLY_MAP} = @{ $specs{ONLY} } ? { map { ($_ => 1) } @{ $specs{ONLY} } } : { '__ALL__' => 1 }; $controller_paginate_spec = \%specs; @@ -45,7 +46,7 @@ sub get_current_paginate_params { my $spec = $self->get_paginate_spec; - my $priv = $self->{PRIV()} || {}; + my $priv = _priv($self); $params{page} = $priv->{page} unless defined $params{page}; $params{per_page} = $priv->{per_page} unless defined $params{per_page}; @@ -54,10 +55,10 @@ sub get_current_paginate_params { per_page => ($params{per_page} * 1) || $spec->{PER_PAGE}, ); - my $paginate_args = ref($spec->{PAGINATE_ARGS}) eq 'CODE' ? $spec->{PAGINATE_ARGS}->($self) - : $spec->{PAGINATE_ARGS} ? do { my $sub = $spec->{PAGINATE_ARGS}; $self->$sub() } - : {}; - my $calculated_params = "SL::DB::Manager::$spec->{MODEL}"->paginate(%paginate_params, args => $paginate_args); + my %paginate_args = ref($spec->{PAGINATE_ARGS}) eq 'CODE' ? %{ $spec->{PAGINATE_ARGS}->($self) } + : $spec->{PAGINATE_ARGS} ? do { my $sub = $spec->{PAGINATE_ARGS}; %{ $self->$sub() } } + : (); + my $calculated_params = "SL::DB::Manager::$spec->{MODEL}"->paginate(%paginate_params, args => \%paginate_args); %paginate_params = ( %paginate_params, num_pages => $calculated_params->{max}, @@ -69,6 +70,11 @@ sub get_current_paginate_params { return %paginate_params; } +sub disable_pagination { + my ($self) = @_; + _priv($self)->{disabled} = 1; +} + # # private functions # @@ -76,6 +82,8 @@ sub get_current_paginate_params { sub _save_current_paginate_params { my ($self) = @_; + return if !_is_enabled($self); + my $paginate_spec = $self->get_paginate_spec; $self->{PRIV()} = { page => $::form->{ $paginate_spec->{FORM_PARAMS}->[0] } || 1, @@ -87,9 +95,9 @@ sub _save_current_paginate_params { sub _callback_handler_for_paginated { my ($self, %params) = @_; - my $priv = $self->{PRIV()} || {}; + my $priv = _priv($self); - if ($priv->{page}) { + if (_is_enabled($self) && $priv->{page}) { my $paginate_spec = $self->get_paginate_spec; $params{ $paginate_spec->{FORM_PARAMS}->[0] } = $priv->{page}; $params{ $paginate_spec->{FORM_PARAMS}->[1] } = $priv->{per_page} if $priv->{per_page}; @@ -102,15 +110,27 @@ sub _callback_handler_for_paginated { sub _get_models_handler_for_paginated { my ($self, %params) = @_; - $params{model} ||= $self->get_paginate_spec->{MODEL}; + my $spec = $self->get_paginate_spec; + $params{model} ||= $spec->{MODEL}; - "SL::DB::Manager::$params{model}"->paginate($self->get_current_paginate_params, args => \%params); + "SL::DB::Manager::$params{model}"->paginate($self->get_current_paginate_params, args => \%params) if _is_enabled($self); - # $::lxdebug->dump(0, "GM handler for paginated; params nach modif:", \%params); + # $::lxdebug->dump(0, "GM handler for paginated; params nach modif (is_enabled? " . _is_enabled($self) . ")", \%params); return %params; } +sub _priv { + my ($self) = @_; + $self->{PRIV()} ||= {}; + return $self->{PRIV()}; +} + +sub _is_enabled { + my ($self) = @_; + return !_priv($self)->{disabled} && ($self->get_paginate_spec->{ONLY_MAP}->{$self->action_name} || $self->get_paginate_spec->{ONLY_MAP}->{'__ALL__'}); +} + 1; __END__ @@ -294,6 +314,12 @@ Returns a hash reference to the paginate spec structure given in the call to L after normalization (hash reference construction, applying default parameters etc). +=item C + +Disable pagination for the duration of the current action. Can be used +when using the attribute C to L does not +cover all cases. + =back =head1 BUGS