From 93f51d6216763896c85506cb815a9acbd653d4d5 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Mon, 3 Sep 2012 17:08:20 +0200 Subject: [PATCH] Vermeidung von Package-Variablen --- SL/Controller/Helper/GetModels.pm | 7 ++++--- SL/Controller/Helper/Paginated.pm | 24 +++++++++++++++--------- SL/Controller/Helper/Sorted.pm | 28 +++++++++++++++++----------- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/SL/Controller/Helper/GetModels.pm b/SL/Controller/Helper/GetModels.pm index 4658717f6..00ef31017 100644 --- a/SL/Controller/Helper/GetModels.pm +++ b/SL/Controller/Helper/GetModels.pm @@ -5,7 +5,8 @@ use strict; use Exporter qw(import); our @EXPORT = qw(get_callback get_models); -my $current_action; +use constant PRIV => '__getmodelshelperpriv'; + my %registered_handlers = ( callback => [], get_models => [] ); sub register_get_models_handlers { @@ -15,7 +16,7 @@ sub register_get_models_handlers { $only = [ $only ] if !ref $only; my %hook_params = @{ $only } ? ( only => $only ) : (); - $class->run_before(sub { $current_action = $_[1]; }, %hook_params); + $class->run_before(sub { $_[0]->{PRIV()} = { current_action => $_[1] }; }, %hook_params); map { push @{ $registered_handlers{$_} }, $additional_handlers{$_} if $additional_handlers{$_} } keys %registered_handlers; } @@ -23,7 +24,7 @@ sub register_get_models_handlers { sub get_callback { my ($self, %override_params) = @_; - my %default_params = _run_handlers($self, 'callback', action => $current_action); + my %default_params = _run_handlers($self, 'callback', action => ($self->{PRIV()} || {})->{current_action}); return $self->url_for(%default_params, %override_params); } diff --git a/SL/Controller/Helper/Paginated.pm b/SL/Controller/Helper/Paginated.pm index f0d617d31..f4a319d2c 100644 --- a/SL/Controller/Helper/Paginated.pm +++ b/SL/Controller/Helper/Paginated.pm @@ -5,7 +5,9 @@ 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); -my ($controller_paginate_spec, $current_page, $current_per_page); +use constant PRIV => '__paginatedhelper_priv'; + +my $controller_paginate_spec; sub make_paginated { my ($class, %specs) = @_; @@ -43,8 +45,9 @@ sub get_current_paginate_params { my $spec = $self->get_paginate_spec; - $params{page} = $current_page unless defined $params{page}; - $params{per_page} = $current_per_page unless defined $params{per_page}; + my $priv = $self->{PRIV()} || {}; + $params{page} = $priv->{page} unless defined $params{page}; + $params{per_page} = $priv->{per_page} unless defined $params{per_page}; my %paginate_params = ( page => ($params{page} * 1) || 1, @@ -71,19 +74,22 @@ sub _save_current_paginate_params { my ($self) = @_; my $paginate_spec = $self->get_paginate_spec; - $current_page = $::form->{ $paginate_spec->{FORM_PARAMS}->[0] } || 1; - $current_per_page = $::form->{ $paginate_spec->{FORM_PARAMS}->[1] } * 1; + $self->{PRIV()} = { + page => $::form->{ $paginate_spec->{FORM_PARAMS}->[0] } || 1, + per_page => $::form->{ $paginate_spec->{FORM_PARAMS}->[1] } * 1, + }; - # $::lxdebug->message(0, "saving current paginate params to $current_page / $current_per_page"); + # $::lxdebug->message(0, "saving current paginate params to " . $self->{PRIV()}->{page} . ' / ' . $self->{PRIV()}->{per_page}); } sub _callback_handler_for_paginated { my ($self, %params) = @_; + my $priv = $self->{PRIV()} || {}; - if ($current_page) { + if ($priv->{page}) { my $paginate_spec = $self->get_paginate_spec; - $params{ $paginate_spec->{FORM_PARAMS}->[0] } = $current_page; - $params{ $paginate_spec->{FORM_PARAMS}->[1] } = $current_per_page if $current_per_page; + $params{ $paginate_spec->{FORM_PARAMS}->[0] } = $priv->{page}; + $params{ $paginate_spec->{FORM_PARAMS}->[1] } = $priv->{per_page} if $priv->{per_page}; } # $::lxdebug->dump(0, "CB handler for paginated; params nach modif:", \%params); diff --git a/SL/Controller/Helper/Sorted.pm b/SL/Controller/Helper/Sorted.pm index f522d9df6..4afae9d79 100644 --- a/SL/Controller/Helper/Sorted.pm +++ b/SL/Controller/Helper/Sorted.pm @@ -5,7 +5,9 @@ use strict; use Exporter qw(import); our @EXPORT = qw(make_sorted get_sort_spec get_current_sort_params _save_current_sort_params _get_models_handler_for_sorted _callback_handler_for_sorted); -my ($controller_sort_spec, $current_sort_by, $current_sort_dir); +use constant PRIV => '__sortedhelperpriv'; + +my $controller_sort_spec; sub make_sorted { my ($class, %specs) = @_; @@ -56,8 +58,9 @@ sub get_current_sort_params { my $sort_spec = $self->get_sort_spec; if (!$params{sort_by}) { - $params{sort_by} = $current_sort_by; - $params{sort_dir} = $current_sort_dir; + my $priv = $self->{PRIV()} || {}; + $params{sort_by} = $priv->{by}; + $params{sort_dir} = $priv->{dir}; } my $by = $params{sort_by} || $sort_spec->{DEFAULT_BY}; @@ -74,22 +77,25 @@ sub get_current_sort_params { # sub _save_current_sort_params { - my ($self) = @_; + my ($self) = @_; - my $sort_spec = $self->get_sort_spec; - $current_sort_by = $::form->{ $sort_spec->{FORM_PARAMS}->[0] }; - $current_sort_dir = !!$::form->{ $sort_spec->{FORM_PARAMS}->[1] } * 1; + my $sort_spec = $self->get_sort_spec; + $self->{PRIV()} = { + by => $::form->{ $sort_spec->{FORM_PARAMS}->[0] }, + dir => !!$::form->{ $sort_spec->{FORM_PARAMS}->[1] } * 1, + }; - # $::lxdebug->message(0, "saving current sort params to $current_sort_by / $current_sort_dir"); + # $::lxdebug->message(0, "saving current sort params to " . $self->{PRIV()}->{by} . ' / ' . $self->{PRIV()}->{dir}); } sub _callback_handler_for_sorted { my ($self, %params) = @_; - if ($current_sort_by) { + my $priv = $self->{PRIV()} || {}; + if ($priv->{by}) { my $sort_spec = $self->get_sort_spec; - $params{ $sort_spec->{FORM_PARAMS}->[0] } = $current_sort_by; - $params{ $sort_spec->{FORM_PARAMS}->[1] } = $current_sort_dir; + $params{ $sort_spec->{FORM_PARAMS}->[0] } = $priv->{by}; + $params{ $sort_spec->{FORM_PARAMS}->[1] } = $priv->{dir}; } # $::lxdebug->dump(0, "CB handler for sorted; params nach modif:", \%params); -- 2.20.1